Memosa-FVM  0.2
AABB.h
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 #ifndef _AABB_H_
6 #define _AABB_H_
7 
8 #include "GeomFields.h"
9 
10 #include "Mesh.h"
11 
12 #include <CGAL/AABB_tree.h> // must be inserted before kernel
13 #include <CGAL/AABB_traits.h>
14 
15 #include <CGAL/Simple_cartesian.h>
16 
17 
18 
29 class AABB
30 {
31 public:
33 
34  AABB(const Mesh& mesh);
35 
43 
51 
52 
53  int meshIntersections(const Mesh& mesh);
54 
63  int findOrientedSide(Vec3D p);
64 
65 private:
66 
67  typedef CGAL::Simple_cartesian<double> K;
68  typedef K::Point_3 Point; // CGAL 3D point type
69  typedef K::Point_2 Point2D; // CGAL 2D point type
70 
71  typedef K::Triangle_3 Triangle; // CGAL 3D triangle type
72  typedef K::Plane_3 Plane; // CGAL 3D plane
73  typedef K::Line_2 Line2D; // CGAL 2d line
74 
75  //typedef CGAL::Bbox_3 BoundingBox;
76 
77 
78  struct MyTriangle
79  {
80  const int faceIndex;
81  const int subFaceIndex;
83  int vertices[3];
84 
85  MyTriangle(const int faceIndex_,
86  const int subFaceIndex_,
87  const Array<Vec3D>& coordArray_,
88  const int v0_,
89  const int v1_,
90  const int v2_) :
91  faceIndex(faceIndex_),
92  subFaceIndex(subFaceIndex_),
93  coordArray(coordArray_)
94  {
95  vertices[0] = v0_;
96  vertices[1] = v1_;
97  vertices[2] = v2_;
98  }
99 
100  MyTriangle(const int faceIndex_,
101  const Array<Vec3D>& coordArray_,
102  const int v0_,
103  const int v1_) :
104  faceIndex(faceIndex_),
105  subFaceIndex(0),
106  coordArray(coordArray_)
107  {
108  vertices[0] = v0_;
109  vertices[1] = v1_;
110  vertices[2] = -1;
111  }
112 
113  Point getVertex(const int n) const
114  {
115  const Vec3D& v = coordArray[vertices[n]];
116  return Point(v[0], v[1], v[2]);
117  }
118 
119  Point2D getVertex2D(const int n) const
120  {
121  const Vec3D& v = coordArray[vertices[n]];
122  return Point2D(v[0], v[1]);
123  }
124 
126  {
127  return Triangle(getVertex(0),
128  getVertex(1),
129  getVertex(2));
130  }
131 
132  K::Segment_2 getSegment2D() const
133  {
134  return K::Segment_2(getVertex2D(0),
135  getVertex2D(1));
136  }
137 
138  K::Segment_3 getSegment() const
139  {
140  return K::Segment_3(getVertex(0),
141  getVertex(1));
142  }
143 
144  Plane getPlane() const
145  {
146  return Plane(getVertex(0),
147  getVertex(1),
148  getVertex(2));
149  }
150 
152  {
153  // CGAL's line orientation is opposite ours in 2d
154  return Line2D(getVertex2D(1),
155  getVertex2D(0));
156  }
157 
158  };
159 
160  // the custom triangles are stored into a vector
161  typedef std::vector<MyTriangle*>::const_iterator MyTriangleIterator;
162 
163  // The following primitive provides the conversion facilities between
164  // the custom triangle and point types and the CGAL ones
166  {
167  public:
168 
169  // this is the type of data that the queries returns
170 
171  typedef const MyTriangle* Id;
172 
173  // CGAL types returned
174  typedef K::Triangle_3 Datum; // CGAL 3D triangle type
175 
176 
177  // default constructor needed
179  {}
180 
182  m_pt(t)
183  {}
184 
185  // the following constructor is the one that receives the iterators from the
186  // iterator range given as input to the AABB_tree
188  : m_pt(*it)
189  {}
190 
191  const Id& id() const { return m_pt; }
192 
193  // on the fly conversion from the internal data to the CGAL types
194  Datum datum() const
195  {
196  return m_pt->getTriangle();
197  }
198 
199  // returns a reference point which must be on the primitive
201  { return m_pt->getVertex(0);}
202 
203  private:
204  Id m_pt; // this is what the AABB tree stores internally
205 
206  };
207 
208  // The following primitive provides the conversion facilities between
209  // the custom triangle and point types and the CGAL ones
211  {
212  public:
213 
214  // this is the type of data that the queries returns
215 
216  typedef const MyTriangle* Id;
217 
218  // CGAL types returned
219  typedef K::Segment_3 Datum;
220 
221 
222  // default constructor needed
224  {}
225 
227  m_pt(t)
228  {}
229 
230  // the following constructor is the one that receives the iterators from the
231  // iterator range given as input to the AABB_tree
233  : m_pt(*it)
234  {}
235 
236  const Id& id() const { return m_pt; }
237 
238  // on the fly conversion from the internal data to the CGAL types
239  Datum datum() const
240  {
241  return m_pt->getSegment();
242  }
243 
244  // returns a reference point which must be on the primitive
246  { return m_pt->getVertex(0);}
247 
248  private:
249  Id m_pt; // this is what the AABB tree stores internally
250 
251  };
252 
253 
254  typedef CGAL::AABB_traits<K, MyTrianglePrimitive> My_AABB_traits;
255  typedef CGAL::AABB_tree<My_AABB_traits> CGAL_Tree;
256 
257  typedef CGAL::AABB_traits<K, MySegmentPrimitive> My_AABB_traits_2D;
258  typedef CGAL::AABB_tree<My_AABB_traits_2D> CGAL_Tree_2D;
259 
260  bool _is2D;
261  std::vector<MyTriangle*> _triangles;
262  boost::shared_ptr<CGAL_Tree> _tree;
263  boost::shared_ptr<CGAL_Tree_2D> _tree_2D;
264  //BoundingBox _bbox;
265 };
266 
267 #endif
K::Point_2 Point2D
Definition: AABB.h:69
AABB(const Mesh &mesh)
Definition: AABB.cpp:10
std::vector< MyTriangle * > _triangles
Definition: AABB.h:261
Point reference_point() const
Definition: AABB.h:245
bool hasIntersectionWithSegment(Vec3D a, Vec3D b)
Definition: AABB.cpp:64
K::Triangle_3 Triangle
Definition: AABB.h:71
const Array< Vec3D > & coordArray
Definition: AABB.h:82
const MyTriangle * Id
Definition: AABB.h:216
int meshIntersections(const Mesh &mesh)
Definition: AABB.cpp:94
MySegmentPrimitive(MyTriangleIterator it)
Definition: AABB.h:232
Line2D getLine2D() const
Definition: AABB.h:151
std::vector< MyTriangle * >::const_iterator MyTriangleIterator
Definition: AABB.h:161
const int subFaceIndex
Definition: AABB.h:81
MySegmentPrimitive(MyTriangle *t)
Definition: AABB.h:226
bool hasIntersectionWithTriangle(Vec3D a, Vec3D b, Vec3D c)
Definition: AABB.cpp:81
Definition: Mesh.h:49
Point reference_point() const
Definition: AABB.h:200
Datum datum() const
Definition: AABB.h:194
Point getVertex(const int n) const
Definition: AABB.h:113
const int faceIndex
Definition: AABB.h:80
Datum datum() const
Definition: AABB.h:239
Plane getPlane() const
Definition: AABB.h:144
K::Plane_3 Plane
Definition: AABB.h:72
K::Segment_2 getSegment2D() const
Definition: AABB.h:132
MyTriangle(const int faceIndex_, const Array< Vec3D > &coordArray_, const int v0_, const int v1_)
Definition: AABB.h:100
const MyTriangle * Id
Definition: AABB.h:171
Definition: AABB.h:29
bool _is2D
Definition: AABB.h:260
K::Line_2 Line2D
Definition: AABB.h:73
CGAL::AABB_traits< K, MyTrianglePrimitive > My_AABB_traits
Definition: AABB.h:254
MyTrianglePrimitive(MyTriangleIterator it)
Definition: AABB.h:187
MyTrianglePrimitive(MyTriangle *t)
Definition: AABB.h:181
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
const Id & id() const
Definition: AABB.h:191
MyTriangle(const int faceIndex_, const int subFaceIndex_, const Array< Vec3D > &coordArray_, const int v0_, const int v1_, const int v2_)
Definition: AABB.h:85
boost::shared_ptr< CGAL_Tree > _tree
Definition: AABB.h:262
K::Triangle_3 Datum
Definition: AABB.h:174
Point2D getVertex2D(const int n) const
Definition: AABB.h:119
boost::shared_ptr< CGAL_Tree_2D > _tree_2D
Definition: AABB.h:263
int vertices[3]
Definition: AABB.h:83
CGAL::AABB_tree< My_AABB_traits > CGAL_Tree
Definition: AABB.h:255
K::Point_3 Point
Definition: AABB.h:68
CGAL::Simple_cartesian< double > K
Definition: AABB.h:67
Vector< double, 3 > Vec3D
Definition: AABB.h:32
CGAL::AABB_traits< K, MySegmentPrimitive > My_AABB_traits_2D
Definition: AABB.h:257
K::Segment_3 Datum
Definition: AABB.h:219
const Id & id() const
Definition: AABB.h:236
K::Segment_3 getSegment() const
Definition: AABB.h:138