Memosa-FVM  0.2
AABB.cpp
Go to the documentation of this file.
1 // This file os part of FVM
2 // Copyright (c) 2012 FVM Authors
3 // See LICENSE file for terms.
4 
5 #include "AABB.h"
6 #include "CRConnectivity.h"
7 
8 #include <stack>
9 
10 AABB::AABB(const Mesh& mesh)
11 {
12  _is2D = mesh.getDimension() == 2;
13 
14  const Array<Vector<double,3> >& meshCoords = mesh.getNodeCoordinates();
15 
16 
17  foreach(const FaceGroupPtr fgPtr, mesh.getBoundaryFaceGroups())
18  {
19  const FaceGroup& fg = *fgPtr;
20  const StorageSite& faces = fg.site;
21  const CRConnectivity& faceNodes = mesh.getFaceNodes(faces);
22 
23  const int nFaces = faces.getCount();
24 
25  for(int f=0; f<nFaces; f++)
26  {
27  if (_is2D)
28  {
29  _triangles.push_back(new MyTriangle(f,meshCoords,
30  faceNodes(f,0),
31  faceNodes(f,1)));
32  }
33  else
34  {
35  _triangles.push_back(new MyTriangle(f,0,meshCoords,
36  faceNodes(f,0),
37  faceNodes(f,1),
38  faceNodes(f,2)));
39  if (faceNodes.getCount(f) == 4)
40  {
41 
42  _triangles.push_back(new MyTriangle(f,1,meshCoords,
43  faceNodes(f,2),
44  faceNodes(f,3),
45  faceNodes(f,0)));
46  }
47  }
48  }
49  }
50 
51  if (_is2D)
52  _tree_2D = boost::shared_ptr<CGAL_Tree_2D>(new CGAL_Tree_2D(_triangles.begin(),
53  _triangles.end()));
54  else
55  {
56  _tree = boost::shared_ptr<CGAL_Tree>(new CGAL_Tree(_triangles.begin(),
57  _triangles.end()));
58 
59  // _bbox = _tree->bbox();
60  }
61 }
62 
63 bool
65 {
66  if (_is2D)
67  {
68  // can't do this check sine we are using 3d segments
69  return false;
70  }
71  else
72  {
73  K::Segment_3 query(K::Point_3(a[0], a[1], a[2]),
74  K::Point_3(b[0], b[1], b[2]));
75  return _tree->do_intersect(query);
76  }
77 }
78 
79 
80 bool
82 {
83  K::Triangle_3 query(K::Point_3(a[0], a[1], a[2]),
84  K::Point_3(b[0], b[1], b[2]),
85  K::Point_3(c[0], c[1], c[2])
86  );
87  if (_is2D)
88  return _tree_2D->do_intersect(query);
89  else
90  return _tree->do_intersect(query);
91 }
92 
93 int
95 {
96  const Array<Vector<double,3> >& meshCoords = mesh.getNodeCoordinates();
97  int nIntersections = 0;
98 
99  if (_is2D)
100  {
101  const StorageSite& cells = mesh.getCells();
102  const CRConnectivity& cellNodes = mesh.getCellNodes();
103 
104  const int nCells = cells.getSelfCount();
105  for(int n=0; n<nCells; n++)
106  {
107  const Vec3D& a = meshCoords[cellNodes(n,0)];
108  const Vec3D& b = meshCoords[cellNodes(n,1)];
109  const Vec3D& c = meshCoords[cellNodes(n,2)];
110 
111  if (hasIntersectionWithTriangle(a,b,c))
112  {
113  nIntersections++;
114  }
115  else if (cellNodes.getCount(n) == 4)
116  {
117 
118  const Vec3D& d = meshCoords[cellNodes(n,3)];
119  if (hasIntersectionWithTriangle(c,d,a))
120  {
121  nIntersections++;
122  }
123  }
124  }
125  }
126  else
127  {
128  const StorageSite& faces = mesh.getFaces();
129  const CRConnectivity& faceNodes = mesh.getAllFaceNodes();
130 
131  const int nFaces = faces.getCount();
132  for(int n=0; n<nFaces; n++)
133  {
134  const Vec3D& a = meshCoords[faceNodes(n,0)];
135  const Vec3D& b = meshCoords[faceNodes(n,1)];
136  const Vec3D& c = meshCoords[faceNodes(n,2)];
137 
138  if (hasIntersectionWithTriangle(a,b,c))
139  {
140  nIntersections++;
141  }
142  else if (faceNodes.getCount(n) == 4)
143  {
144 
145  const Vec3D& d = meshCoords[faceNodes(n,3)];
146  if (hasIntersectionWithTriangle(c,d,a))
147  {
148  nIntersections++;
149  }
150  }
151  }
152  }
153  return nIntersections;
154 }
155 
157 {
158  if (_is2D)
159  {
160  K::Point_2 query(p[0], p[1]);
161  foreach(const MyTriangle* myt, _triangles)
162  {
163  Line2D line = myt->getLine2D();
164  CGAL::Oriented_side orientation = line.oriented_side(query);
165  if (orientation == CGAL::ON_POSITIVE_SIDE)
166  return 1;
167  else if (orientation == CGAL::ON_ORIENTED_BOUNDARY)
168  {
169  K::Segment_2 segment = myt->getSegment2D();
170  if (segment.has_on(query))
171  return 0;
172  }
173  }
174  return -1;
175  }
176  else
177  {
178  K::Point_3 query(p[0], p[1], p[2]);
179  foreach(const MyTriangle* myt, _triangles)
180  {
181  Plane plane = myt->getPlane();
182  CGAL::Oriented_side orientation = plane.oriented_side(query);
183  if (orientation == CGAL::ON_POSITIVE_SIDE)
184  return 1;
185  else if (orientation == CGAL::ON_ORIENTED_BOUNDARY)
186  {
187  Triangle triangle = myt->getTriangle();
188  if (triangle.has_on(query))
189  return 0;
190  }
191  }
192  return -1;
193  }
194 }
195 
const CRConnectivity & getAllFaceNodes() const
Definition: Mesh.cpp:368
const FaceGroupList & getBoundaryFaceGroups() const
Definition: Mesh.h:187
int getCount(const int i) const
AABB(const Mesh &mesh)
Definition: AABB.cpp:10
shared_ptr< FaceGroup > FaceGroupPtr
Definition: Mesh.h:46
std::vector< MyTriangle * > _triangles
Definition: AABB.h:261
int getSelfCount() const
Definition: StorageSite.h:40
bool hasIntersectionWithSegment(Vec3D a, Vec3D b)
Definition: AABB.cpp:64
K::Triangle_3 Triangle
Definition: AABB.h:71
Definition: Mesh.h:28
int meshIntersections(const Mesh &mesh)
Definition: AABB.cpp:94
Line2D getLine2D() const
Definition: AABB.h:151
bool hasIntersectionWithTriangle(Vec3D a, Vec3D b, Vec3D c)
Definition: AABB.cpp:81
Definition: Mesh.h:49
const CRConnectivity & getFaceNodes(const StorageSite &site) const
Definition: Mesh.cpp:402
Plane getPlane() const
Definition: AABB.h:144
K::Plane_3 Plane
Definition: AABB.h:72
const Array< VecD3 > & getNodeCoordinates() const
Definition: Mesh.h:218
K::Segment_2 getSegment2D() const
Definition: AABB.h:132
bool _is2D
Definition: AABB.h:260
K::Line_2 Line2D
Definition: AABB.h:73
const StorageSite & getFaces() const
Definition: Mesh.h:108
const StorageSite & getCells() const
Definition: Mesh.h:109
int findOrientedSide(Vec3D p)
Definition: AABB.cpp:156
CGAL::AABB_tree< My_AABB_traits_2D > CGAL_Tree_2D
Definition: AABB.h:258
Definition: Array.h:14
Triangle getTriangle() const
Definition: AABB.h:125
int getCount() const
Definition: StorageSite.h:39
boost::shared_ptr< CGAL_Tree > _tree
Definition: AABB.h:262
boost::shared_ptr< CGAL_Tree_2D > _tree_2D
Definition: AABB.h:263
int getDimension() const
Definition: Mesh.h:105
CGAL::AABB_tree< My_AABB_traits > CGAL_Tree
Definition: AABB.h:255
const CRConnectivity & getCellNodes() const
Definition: Mesh.cpp:426
StorageSite site
Definition: Mesh.h:40