ug4
grid_util_impl.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2009-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__LIB_GRID__GRID_UTIL_IMPL__
34 #define __H__LIB_GRID__GRID_UTIL_IMPL__
35 
36 #include <vector>
37 #include "grid_util.h"
38 #include "common/assert.h"
39 #include "common/common.h"
40 
41 namespace ug
42 {
44 // EdgeContains
45 inline bool EdgeContains(EdgeVertices* e, Vertex* vrt)
46 {
47  return e->vertex(0) == vrt || e->vertex(1) == vrt;
48 }
49 
50 inline bool EdgeContains(EdgeVertices* e, Vertex* vrt1, Vertex* vrt2)
51 {
52  return ((e->vertex(0) == vrt1 && e->vertex(1) == vrt2)
53  || (e->vertex(1) == vrt1 && e->vertex(0) == vrt2));
54 }
55 
58 // new methods
59 
61 template <class TVrtContainer1, class TVrtContainer2>
62 bool CompareVertexContainer(const TVrtContainer1& con1,
63  const TVrtContainer2& con2)
64 {
65  int con1Size = (int)con1.size();
66 
67  if(con1Size != con2.size())
68  return false;
69 
70  for(int i = 0; i < con1Size; ++i)
71  {
72  int j;
73  for(j = 0; j < con1Size; ++j)
74  {
75  if(con1[i] == con2[j])
76  break;
77  }
78 
79  // check whether we found a matching vertex
80  if(j == con1Size)
81  return false;
82  }
83 
84  return true;
85 }
86 
87 
88 inline bool CompareVertices(const EdgeVertices* ev1,
89  const EdgeVertices* ev2)
90 {
91  if((ev1->vertex(0) == ev2->vertex(0) && ev1->vertex(1) == ev2->vertex(1)) ||
92  (ev1->vertex(0) == ev2->vertex(1) && ev1->vertex(1) == ev2->vertex(0)))
93  return true;
94  return false;
95 }
96 
98 // GetVertex
99 inline Vertex* GetVertex(Vertex* vrt, size_t i)
100 {
101  UG_ASSERT(i < 1, "A Vertex has only one vertex");
102  return vrt;
103 }
104 
105 inline Vertex* GetVertex(Edge* edge, size_t i)
106 {
107  UG_ASSERT(i < edge->num_vertices(), "Wrong number of vertex");
108  return edge->vertex(i);
109 }
110 
111 inline Vertex* GetVertex(Face* face, size_t i)
112 {
113  UG_ASSERT(i < face->num_vertices(), "Wrong number of vertex");
114  return face->vertex(i);
115 }
116 
117 inline Vertex* GetVertex(Volume* vol, size_t i)
118 {
119  UG_ASSERT(i < vol->num_vertices(), "Wrong number of vertex");
120  return vol->vertex(i);
121 }
122 
123 inline size_t NumVertices(Vertex* elem)
124 {
125  return 1;
126 }
127 
128 inline size_t NumVertices(Edge* elem)
129 {
130  return elem->num_vertices();
131 }
132 
133 inline size_t NumVertices(Face* elem)
134 {
135  return elem->num_vertices();
136 }
137 
138 inline size_t NumVertices(Volume* elem)
139 {
140  return elem->num_vertices();
141 }
142 
143 
145 inline void CollectAssociated(std::vector<Vertex*>& vVertexOut,
146  Grid& grid, Vertex* v, bool clearContainer)
147 {
148  CollectVertices(vVertexOut, grid, v, clearContainer);
149 }
150 
151 inline void CollectAssociated(std::vector<Vertex*>& vVertexOut,
152  Grid& grid, Edge* e, bool clearContainer)
153 {
154  CollectVertices(vVertexOut, grid, e, clearContainer);
155 }
156 
157 inline void CollectAssociated(std::vector<Vertex*>& vVertexOut,
158  Grid& grid, Face* f, bool clearContainer)
159 {
160  CollectVertices(vVertexOut, grid, f, clearContainer);
161 }
162 
163 inline void CollectAssociated(std::vector<Vertex*>& vVertexOut,
164  Grid& grid, Volume* v, bool clearContainer)
165 {
166  CollectVertices(vVertexOut, grid, v, clearContainer);
167 }
168 
169 inline void CollectAssociated(std::vector<Vertex*>& vVertexOut,
170  Grid& grid, GridObject* obj, bool clearContainer)
171 {
172  uint type = obj->base_object_id();
173  switch(type)
174  {
175  case VERTEX:CollectAssociated(vVertexOut, grid, reinterpret_cast<Vertex*>(obj), clearContainer); return;
176  case EDGE: CollectAssociated(vVertexOut, grid, reinterpret_cast<Edge*>(obj), clearContainer); return;
177  case FACE: CollectAssociated(vVertexOut, grid, reinterpret_cast<Face*>(obj), clearContainer); return;
178  case VOLUME:CollectAssociated(vVertexOut, grid, reinterpret_cast<Volume*>(obj), clearContainer); return;
179  }
180  throw(UGError("GeomObject type not known."));
181 }
182 
183 
185 inline void CollectAssociated(std::vector<Edge*>& vEdgesOut,
186  Grid& grid, Vertex* vrt, bool clearContainer)
187 {
188  CollectEdges(vEdgesOut, grid, vrt, clearContainer);
189 }
190 
191 inline void CollectAssociated(std::vector<Edge*>& vEdgesOut,
192  Grid& grid, Edge* e, bool clearContainer)
193 {
194  CollectEdges(vEdgesOut, grid, e, clearContainer);
195 }
196 
197 inline void CollectAssociated(std::vector<Edge*>& vEdgesOut,
198  Grid& grid, Face* f, bool clearContainer)
199 {
200  CollectEdges(vEdgesOut, grid, f, clearContainer);
201 }
202 
203 inline void CollectAssociated(std::vector<Edge*>& vEdgesOut,
204  Grid& grid, Volume* v, bool clearContainer)
205 {
206  CollectEdges(vEdgesOut, grid, v, clearContainer);
207 }
208 
209 inline void CollectAssociated(std::vector<Edge*>& vEdgesOut,
210  Grid& grid, GridObject* obj, bool clearContainer)
211 {
212  uint type = obj->base_object_id();
213  switch(type)
214  {
215  case VERTEX:CollectAssociated(vEdgesOut, grid, reinterpret_cast<Vertex*>(obj), clearContainer); return;
216  case EDGE: CollectAssociated(vEdgesOut, grid, reinterpret_cast<Edge*>(obj), clearContainer); return;
217  case FACE: CollectAssociated(vEdgesOut, grid, reinterpret_cast<Face*>(obj), clearContainer); return;
218  case VOLUME:CollectAssociated(vEdgesOut, grid, reinterpret_cast<Volume*>(obj), clearContainer); return;
219  }
220  throw(UGError("GeomObject type not known."));
221 }
222 
223 
225 inline void CollectAssociated(std::vector<Face*>& vFacesOut,
226  Grid& grid, Vertex* vrt, bool clearContainer)
227 {
228  CollectFaces(vFacesOut, grid, vrt, clearContainer);
229 }
230 
231 inline void CollectAssociated(std::vector<Face*>& vFacesOut,
232  Grid& grid, Edge* e, bool clearContainer)
233 {
234  CollectFaces(vFacesOut, grid, e, clearContainer);
235 }
236 
237 inline void CollectAssociated(std::vector<Face*>& vFacesOut,
238  Grid& grid, Face* f, bool clearContainer)
239 {
240  CollectFaces(vFacesOut, grid, f, clearContainer);
241 }
242 
243 inline void CollectAssociated(std::vector<Face*>& vFacesOut,
244  Grid& grid, Volume* v, bool clearContainer)
245 {
246  CollectFaces(vFacesOut, grid, v, clearContainer);
247 }
248 
249 inline void CollectAssociated(std::vector<Face*>& vFacesOut,
250  Grid& grid, GridObject* obj, bool clearContainer)
251 {
252  uint type = obj->base_object_id();
253  switch(type)
254  {
255  case VERTEX:CollectAssociated(vFacesOut, grid, reinterpret_cast<Vertex*>(obj), clearContainer); return;
256  case EDGE: CollectAssociated(vFacesOut, grid, reinterpret_cast<Edge*>(obj), clearContainer); return;
257  case FACE: CollectAssociated(vFacesOut, grid, reinterpret_cast<Face*>(obj), clearContainer); return;
258  case VOLUME:CollectAssociated(vFacesOut, grid, reinterpret_cast<Volume*>(obj), clearContainer); return;
259  }
260  throw(UGError("GeomObject type not known."));
261 }
262 
263 
265 inline void CollectAssociated(std::vector<Volume*>& vVolumesOut,
266  Grid& grid, Vertex* vrt, bool clearContainer)
267 {
268  CollectVolumes(vVolumesOut, grid, vrt, clearContainer);
269 }
270 
271 inline void CollectAssociated(std::vector<Volume*>& vVolumesOut,
272  Grid& grid, Edge* e, bool clearContainer)
273 {
274  CollectVolumes(vVolumesOut, grid, e, clearContainer);
275 }
276 
277 inline void CollectAssociated(std::vector<Volume*>& vVolumesOut,
278  Grid& grid, Face* f, bool clearContainer,
279  bool ignoreAssociatedVolumes)
280 {
281  CollectVolumes(vVolumesOut, grid, f, clearContainer, ignoreAssociatedVolumes);
282 }
283 
284 inline void CollectAssociated(std::vector<Volume*>& vVolumesOut,
285  Grid& grid, Volume* vol, bool clearContainer)
286 {
287  CollectVolumes(vVolumesOut, grid, vol, clearContainer);
288 }
289 
290 inline void CollectAssociated(std::vector<Volume*>& vVolumesOut,
291  Grid& grid, FaceDescriptor& fd, bool clearContainer)
292 {
293  CollectVolumes(vVolumesOut, grid, fd, clearContainer);
294 }
295 
296 inline void CollectAssociated(std::vector<Volume*>& vVolumesOut,
297  Grid& grid, GridObject* obj, bool clearContainer)
298 {
299  uint type = obj->base_object_id();
300  switch(type)
301  {
302  case VERTEX:CollectAssociated(vVolumesOut, grid, reinterpret_cast<Vertex*>(obj), clearContainer); return;
303  case EDGE: CollectAssociated(vVolumesOut, grid, reinterpret_cast<Edge*>(obj), clearContainer); return;
304  case FACE: CollectAssociated(vVolumesOut, grid, reinterpret_cast<Face*>(obj), clearContainer); return;
305  case VOLUME:CollectAssociated(vVolumesOut, grid, reinterpret_cast<Volume*>(obj), clearContainer); return;
306  }
307  throw(UGError("GeomObject type not known."));
308 }
309 
312 
313 inline void CollectVertices(std::vector<Vertex*>& vVertexOut, Grid& grid,
314  GridObject* obj, bool clearContainer)
315 {
316  switch(obj->base_object_id())
317  {
318  case VERTEX:CollectVertices(vVertexOut, grid, static_cast<Vertex*>(obj), clearContainer); return;
319  case EDGE: CollectVertices(vVertexOut, grid, static_cast<Edge*>(obj), clearContainer); return;
320  case FACE: CollectVertices(vVertexOut, grid, static_cast<Face*>(obj), clearContainer); return;
321  case VOLUME:CollectVertices(vVertexOut, grid, static_cast<Volume*>(obj), clearContainer); return;
322  }
323  throw(UGError("GeomObject type not known."));
324 }
325 
326 inline void CollectEdgesSorted(std::vector<Edge*>& vEdgesOut, Grid& grid,
327  GridObject* obj, bool clearContainer)
328 {
329  switch(obj->base_object_id())
330  {
331  case VERTEX:CollectEdgesSorted(vEdgesOut, grid, static_cast<Vertex*>(obj), clearContainer); return;
332  case EDGE: CollectEdgesSorted(vEdgesOut, grid, static_cast<Edge*>(obj), clearContainer); return;
333  case FACE: CollectEdgesSorted(vEdgesOut, grid, static_cast<Face*>(obj), clearContainer); return;
334  case VOLUME:CollectEdgesSorted(vEdgesOut, grid, static_cast<Volume*>(obj), clearContainer); return;
335  }
336  throw(UGError("GeomObject type not known."));
337 }
338 
339 inline void CollectFacesSorted(std::vector<Face*>& vFacesOut, Grid& grid,
340  GridObject* obj, bool clearContainer)
341 {
342  switch(obj->base_object_id())
343  {
344  case VERTEX:CollectFacesSorted(vFacesOut, grid, static_cast<Vertex*>(obj), clearContainer); return;
345  case EDGE: CollectFacesSorted(vFacesOut, grid, static_cast<Edge*>(obj), clearContainer); return;
346  case FACE: CollectFacesSorted(vFacesOut, grid, static_cast<Face*>(obj), clearContainer); return;
347  case VOLUME:CollectFacesSorted(vFacesOut, grid, static_cast<Volume*>(obj), clearContainer); return;
348  }
349  throw(UGError("GeomObject type not known."));
350 }
351 
352 }// end of namespace libGrid
353 
354 #endif
Base-class for edges.
Definition: grid_base_objects.h:397
holds the vertices of an Edge or an EdgeDescriptor.
Definition: grid_base_objects.h:362
virtual Vertex * vertex(size_t index) const
Definition: grid_base_objects.h:366
virtual size_t num_vertices() const
Definition: grid_base_objects.h:368
Can be queried for the edges and vertices of a face.
Definition: grid_base_objects.h:684
Faces are 2-dimensional objects.
Definition: grid_base_objects.h:510
virtual Vertex * vertex(size_t index) const
Definition: grid_base_objects.h:486
virtual size_t num_vertices() const
Definition: grid_base_objects.h:488
Manages the elements of a grid and their interconnection.
Definition: grid.h:132
The base class for all geometric objects, such as vertices, edges, faces, volumes,...
Definition: grid_base_objects.h:157
virtual int base_object_id() const =0
Instances of this class or of derived classes are thrown if errors arise.
Definition: error.h:104
Base-class for all vertex-types.
Definition: grid_base_objects.h:231
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
bool EdgeContains(EdgeVertices *e, Vertex *vrt)
Definition: grid_util_impl.hpp:45
bool CompareVertices(const FaceVertices *fv1, const FaceVertices *fv2)
Checks whether fv1 and fv2 contain the same vertices.
Definition: grid_util.cpp:44
void CollectVolumes(std::vector< Volume * > &vVolumesOut, Grid &grid, Vertex *vrt, bool clearContainer)
Collects all volumes that exist in the given grid which contain the given vertex.
Definition: grid_util.cpp:592
void CollectEdgesSorted(vector< Edge * > &vEdgesOut, Grid &grid, Vertex *v, bool clearContainer)
Collects all edges of a vertex, thus, none.
Definition: grid_util.cpp:205
void CollectEdges(std::vector< Edge * > &vEdgesOut, Grid &grid, Vertex *vrt, bool clearContainer)
Collects all edges which exist in the given grid and which are part of the given vertex.
Definition: grid_util.cpp:295
void CollectFacesSorted(vector< Face * > &vFacesOut, Grid &grid, Vertex *v, bool clearContainer)
Collects all Faces of a Vertex, thus, none.
Definition: grid_util.cpp:401
void CollectFaces(std::vector< Face * > &vFacesOut, Grid &grid, Vertex *vrt, bool clearContainer)
Collects all faces that exist in the given grid which contain the given vertex.
Definition: grid_util.cpp:458
void CollectVertices(std::vector< Vertex * > &vVertexOut, Grid &grid, Vertex *v, bool clearContainer)
Collects all vertices.
Definition: grid_util.cpp:141
UG_API void CollectAssociated(std::vector< Vertex * > &vVertexOut, Grid &grid, GridObject *obj, bool clearContainer=true)
Definition: grid_util_impl.hpp:169
UG_API bool CompareVertexContainer(const TVrtContainer1 &con1, const TVrtContainer2 &con2)
compares vertices in a container
Definition: grid_util_impl.hpp:62
Vertex * GetVertex(Vertex *v, size_t i)
returns the i'th vertex of a vertex
Definition: grid_util_impl.hpp:99
size_t NumVertices(Vertex *elem)
Returns the number of vertices of the given geometric object.
Definition: grid_util_impl.hpp:123
#define UG_ASSERT(expr, msg)
Definition: assert.h:70
unsigned int uint
Definition: types.h:114
int num_vertices(ug::BidirectionalMatrix< T > const &M)
Definition: bidirectional_boost.h:70
the ug namespace
@ VOLUME
Definition: grid_base_objects.h:63
@ VERTEX
Definition: grid_base_objects.h:60
@ EDGE
Definition: grid_base_objects.h:61
@ FACE
Definition: grid_base_objects.h:62