ug4
Loading...
Searching...
No Matches
duplicate.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2011-2015: G-CSC, Goethe University Frankfurt
3 * Author: Sebastian Reiter
4 *
5 * This file is part of UG4.
6 *
7 * UG4 is free software: you can redistribute it and/or modify it under the
8 * terms of the GNU Lesser General Public License version 3 (as published by the
9 * Free Software Foundation) with the following additional attribution
10 * requirements (according to LGPL/GPL v3 §7):
11 *
12 * (1) The following notice must be displayed in the Appropriate Legal Notices
13 * of covered and combined works: "Based on UG4 (www.ug4.org/license)".
14 *
15 * (2) The following notice must be displayed at a prominent place in the
16 * terminal output of covered works: "Based on UG4 (www.ug4.org/license)".
17 *
18 * (3) The following bibliography is recommended for citation and must be
19 * preserved in all covered files:
20 * "Reiter, S., Vogel, A., Heppner, I., Rupp, M., and Wittum, G. A massively
21 * parallel geometric multigrid solver on hierarchically distributed grids.
22 * Computing and visualization in science 16, 4 (2013), 151-164"
23 * "Vogel, A., Reiter, S., Rupp, M., Nägel, A., and Wittum, G. UG4 -- a novel
24 * flexible software system for simulating pde based models on high performance
25 * computers. Computing and visualization in science 16, 4 (2013), 165-179"
26 *
27 * This program is distributed in the hope that it will be useful,
28 * but WITHOUT ANY WARRANTY; without even the implied warranty of
29 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 * GNU Lesser General Public License for more details.
31 */
32
33#ifndef __H__UG__duplicate__
34#define __H__UG__duplicate__
35
36#include <vector>
37#include "lib_grid/lg_base.h"
38#include "selection_util.h"
39
40namespace ug
41{
43
50template <class TAPos>
51bool Duplicate(Grid& grid, Selector& sel, const typename TAPos::ValueType& offset,
52 TAPos& aPos, bool deselectOld = true, bool selectNew = true)
53{
54 using namespace std;
55
56 if(!grid.has_vertex_attachment(aPos)){
57 UG_LOG("ERROR in Duplicate. Given position attachment not attached to vertices.\n");
58 }
59
61
62
63// first of all we have to select all associated elements
65
66// store the currently selected elements in arrays
67 vector<Vertex*> oldVrts;
68 vector<Edge*> oldEdges;
69 vector<Face*> oldFaces;
70 vector<Volume*> oldVols;
71 oldVrts.reserve(sel.num<Vertex>());
72 oldVrts.assign(sel.vertices_begin(), sel.vertices_end());
73 oldEdges.reserve(sel.num<Edge>());
74 oldEdges.assign(sel.edges_begin(), sel.edges_end());
75 oldFaces.reserve(sel.num<Face>());
76 oldFaces.assign(sel.faces_begin(), sel.faces_end());
77 oldVols.reserve(sel.num<Volume>());
78 oldVols.assign(sel.volumes_begin(), sel.volumes_end());
79
80// depending on the options we'll now clear the selector and enable autoselection
81 bool autoselectionEnabled = sel.autoselection_enabled();
82 bool selectionInheritanceEnabled = sel.selection_inheritance_enabled();
83 if(deselectOld)
84 sel.clear();
85 if(selectNew)
86 sel.enable_autoselection(true);
87 else{
88 sel.enable_autoselection(false);
90 }
91
92// new vertices have to be stored in an array, so that we can access them later on
93 vector<Vertex*> vrts;
94 vrts.reserve(sel.num<Vertex>());
95
96// attach an integer to the vertices. This is required, since
97// we have to access them by index later on.
98 AInt aInt;
99 grid.attach_to_vertices(aInt);
100 Grid::VertexAttachmentAccessor<AInt> aaInt(grid, aInt);
101
102// create new vertices and push them to the vrts array.
103// Store the new index in the aInt attachment of old ones.
104 for(vector<Vertex*>::iterator iter = oldVrts.begin();
105 iter != oldVrts.end(); ++iter)
106 {
107 Vertex* oldVrt = *iter;
108 Vertex* vrt = *grid.create_by_cloning(oldVrt, oldVrt);
109 VecAdd(aaPos[vrt], aaPos[oldVrt], offset);
110 aaInt[oldVrt] = (int)vrts.size();
111 vrts.push_back(vrt);
112 }
113
114// create new elements for all vector-entries
115// edges
116 for(size_t i = 0; i < oldEdges.size(); ++i){
117 Edge* oldElem = oldEdges[i];
118 grid.create_by_cloning(oldElem, EdgeDescriptor(vrts[aaInt[oldElem->vertex(0)]],
119 vrts[aaInt[oldElem->vertex(1)]]),
120 oldElem);
121 }
122
123// faces
124 {
125 FaceDescriptor desc;
126 for(size_t i = 0; i < oldFaces.size(); ++i){
127 Face* oldElem = oldFaces[i];
128
129 desc.set_num_vertices(oldElem->num_vertices());
130 for(size_t j = 0; j < desc.num_vertices(); ++j)
131 desc.set_vertex(j, vrts[aaInt[oldElem->vertex(j)]]);
132
133 grid.create_by_cloning(oldElem, desc, oldElem);
134 }
135 }
136
137// volumes
138 {
139 VolumeDescriptor desc;
140 for(size_t i = 0; i < oldVols.size(); ++i){
141 Volume* oldElem = oldVols[i];
142
143 desc.set_num_vertices(oldElem->num_vertices());
144 for(size_t j = 0; j < desc.num_vertices(); ++j)
145 desc.set_vertex(j, vrts[aaInt[oldElem->vertex(j)]]);
146
147 grid.create_by_cloning(oldElem, desc, oldElem);
148 }
149 }
150
151// restore autoselection property of the selector
152 sel.enable_autoselection(autoselectionEnabled);
153 sel.enable_selection_inheritance(selectionInheritanceEnabled);
154 return true;
155}
156
157}// end of namespace
158
159#endif
Can be used to store information about an edge and to construct an edge.
Definition grid_base_objects.h:464
Base-class for edges.
Definition grid_base_objects.h:397
virtual Vertex * vertex(size_t index) const
Definition grid_base_objects.h:366
Can be queried for the edges and vertices of a face.
Definition grid_base_objects.h:684
void set_num_vertices(uint numVertices)
Definition grid_base_objects.h:705
virtual size_t num_vertices() const
Definition grid_base_objects.h:703
void set_vertex(uint index, Vertex *vrt)
Definition grid_base_objects.h:706
Faces are 2-dimensional objects.
Definition grid_base_objects.h:510
virtual size_t num_vertices() const
Definition grid_base_objects.h:488
virtual Vertex * vertex(size_t index) const
Definition grid_base_objects.h:486
Manages the elements of a grid and their interconnection.
Definition grid.h:132
VertexIterator create_by_cloning(Vertex *pCloneMe, GridObject *pParent=NULL)
this method creates a new vertex, which has the same type as pCloneMe.
Definition grid.cpp:419
bool has_vertex_attachment(IAttachment &attachment)
Definition grid.h:798
void attach_to_vertices(IAttachment &attachment, bool passOnValues)
Definition grid.h:728
void enable_selection_inheritance(bool bEnable)
Definition selector_interface.cpp:195
bool selection_inheritance_enabled()
Definition selector_interface.h:233
void enable_autoselection(bool bEnable)
Definition selector_interface.cpp:190
bool autoselection_enabled()
Definition selector_interface.h:229
specialization of ISelector for a grid of class Grid.
Definition selector_grid.h:96
VertexIterator vertices_end()
Definition selector_grid.h:173
virtual void clear()
Definition selector_grid.cpp:155
VolumeIterator volumes_end()
Definition selector_grid.h:179
size_t num() const
Definition selector_grid_impl.hpp:74
VolumeIterator volumes_begin()
Definition selector_grid.h:178
VertexIterator vertices_begin()
Definition selector_grid.h:172
EdgeIterator edges_end()
Definition selector_grid.h:175
EdgeIterator edges_begin()
Definition selector_grid.h:174
FaceIterator faces_end()
Definition selector_grid.h:177
FaceIterator faces_begin()
Definition selector_grid.h:176
Base-class for all vertex-types.
Definition grid_base_objects.h:231
Holds a set of vertices which represent the corners of a volume element.
Definition grid_base_objects.h:951
void set_vertex(uint index, Vertex *vrt)
Definition grid_base_objects.h:967
virtual size_t num_vertices() const
Definition grid_base_objects.h:964
void set_num_vertices(uint numVertices)
Definition grid_base_objects.h:966
Volumes are 3-dimensional objects.
Definition grid_base_objects.h:754
virtual Vertex * vertex(size_t index) const
Definition grid_base_objects.h:727
virtual size_t num_vertices() const
Definition grid_base_objects.h:729
void SelectAssociatedGridObjects(TSelector &sel, ISelector::status_t status)
selects associated geometric objects of selected ones on each level.
Definition selection_util.cpp:221
#define UG_LOG(msg)
Definition log.h:367
void VecAdd(vector_t &vOut, const vector_t &v1, const vector_t &v2)
adds two MathVector<N>s and stores the result in a third one
Definition math_vector_functions_common_impl.hpp:185
Definition smart_pointer.h:814
the ug namespace
bool Duplicate(Grid &grid, Selector &sel, const typename TAPos::ValueType &offset, TAPos &aPos, bool deselectOld=true, bool selectNew=true)
Duplicates the selected part of a grid and translates it by an offset.
Definition duplicate.h:51