MEPP2 Project
boolops_triangulation.hpp
Go to the documentation of this file.
1 // Copyright (c) 2012-2019 University of Lyon and CNRS (France).
2 // All rights reserved.
3 //
4 // This file is part of MEPP2; you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published
6 // by the Free Software Foundation; either version 3 of the License,
7 // or (at your option) any later version.
8 //
9 // This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
10 // WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
11 
12 #pragma once
13 
20 #include <CGAL/Triangulation_2.h>
21 #include <CGAL/Constrained_Delaunay_triangulation_2.h>
22 
27 template <class Gt, class Vb = CGAL::Triangulation_vertex_base_2<Gt> >
28 class Enriched_vertex_base : public Vb
29 {
30 public:
31  template < typename TDS2 >
32  struct Rebind_TDS {
33  typedef typename Vb::template Rebind_TDS<TDS2>::Other Vb2;
35  };
36 
37 private:
39  unsigned long m_Label;
40 
41 public:
44  void set_Label(unsigned long Label) {m_Label = Label;}
47  unsigned long get_Label() {return m_Label;}
48 };
49 
54 template <class Gt, class Fb = CGAL::Constrained_triangulation_face_base_2<Gt> >
55 class Enriched_face_base : public Fb
56 {
57 public:
62  typedef typename Fb::Triangulation_data_structure::Vertex_handle Vertex_handle;
63 
68  typedef typename Fb::Triangulation_data_structure::Face_handle Face_handle;
69 
70  template < typename TDS2 >
71  struct Rebind_TDS {
72  typedef typename Fb::template Rebind_TDS<TDS2>::Other Fb2;
74  };
75 
76 private:
78  bool m_OK;
80  bool m_Ext;
81 
82 public:
83  Enriched_face_base() : Fb() {}
86  Face_handle n0, Face_handle n1, Face_handle n2) : Fb(v0,v1,v2,n0,n1,n2) {}
88  bool c0, bool c1, bool c2 ) : Fb(v0,v1,v2,n0,n1,n2) {}
91  void set_Ext(bool Ext) {m_Ext = Ext;}
94  bool get_Ext() {return m_Ext;}
97  void set_OK(bool OK) {m_OK = OK;}
100  bool get_OK() {return m_OK;}
101 };
102 
107 template <class K>
109 {
114  typedef typename K::Point_3 Point_3;
115 
120  typedef /*typename*/ Enriched_vertex_base<K> Tri_vb;
121 
126  typedef /*typename*/ Enriched_face_base<K> Tri_fb;
127 
132  typedef typename CGAL::Triangulation_data_structure_2<Tri_vb,Tri_fb> Tri_DS;
133 
138 //DBG #define STR(x) #x
139 //DBG #define XSTR(x) STR(x)
140 //DBG #pragma message("CGAL_VERSION_NR=" XSTR(CGAL_VERSION_NR))
141 //DBG #pragma message("CGAL_VERSION_STR=" XSTR(CGAL_VERSION_STR))
142 
143 // Note: the following test should be
144 // #if( CGAL_VERSION_NR >= CGAL_VERSION_NUMBER(5,1,0) )
145 // but CGAL_VERSION_NUMBER(5,1,0)=1050101000 whereas the version of CGAL
146 // packaged with vcpkg has CGAL_VERSION_NR=1050100900 (it seems that it
147 // is not a public release).
148 #if( CGAL_VERSION_NR >= 1050100000 )
149  typedef typename CGAL::No_constraint_intersection_requiring_constructions_tag
150  Itag;
151 #else
152  typedef typename CGAL::No_intersection_tag Itag;
153 #endif
154 
159  typedef typename CGAL::Constrained_Delaunay_triangulation_2<K, Tri_DS, Itag> Constrained_Delaunay_tri;
160 
165  typedef typename Constrained_Delaunay_tri::Vertex_handle Vertex_handle_tri;
166 
171  typedef typename Constrained_Delaunay_tri::Face_handle Face_handle_tri;
172 
178 
183  typedef typename Constrained_Delaunay_tri::Face_iterator Face_iterator_tri;
184 
185 public:
192  {
193  //find the longest coordinate of the normal vector, and its sign
194  double x = to_double(norm_dir.x());
195  double y = to_double(norm_dir.y());
196  double z = to_double(norm_dir.z());
197  double absx = std::abs(x);
198  double absy = std::abs(y);
199  double absz = std::abs(z);
200 
201  //this information is stored using a code :
202  //0 : The coordinate X is the longest, and positive
203  //1 : The coordinate Y is the longest, and positive
204  //2 : The coordinate Z is the longest, and positive
205  //3 : The coordinate X is the longest, and negative
206  //4 : The coordinate Y is the longest, and negative
207  //5 : The coordinate Z is the longest, and negative
208  if (absx >= absy && absx >= absz) max_coordinate = (x>0)?0:3;
209  else if (absy >= absx && absy >= absz) max_coordinate = (y>0)?1:4;
210  else if (absz >= absx && absz >= absy) max_coordinate = (z>0)?2:5;
211 
212  //we add the three vertices of the facet to the triangulation
213  //The Label of these vertices is set for the corresponding point in the triangulation
214  v1 = add_new_pt(point_to_exact(he->vertex()->point()), he->vertex()->Label);
215  v2 = add_new_pt(point_to_exact(he->next()->vertex()->point()), he->next()->vertex()->Label);
216  v3 = add_new_pt(point_to_exact(he->next()->next()->vertex()->point()), he->next()->next()->vertex()->Label);
217 
218  //if the vertices does not have an Id (Label = OxFFFFFFFF), the labels
219  //of the points in the triangulation is set as follows :
220  //0xFFFFFFFF for the first point
221  //0xFFFFFFFE for the second point
222  //0xFFFFFFFD for the third point
223  if(v2->get_Label() == 0xFFFFFFFF) v2->set_Label(0xFFFFFFFE);
224  if(v3->get_Label() == 0xFFFFFFFF) v3->set_Label(0xFFFFFFFD);
225  }
226 
233  {
234  switch(max_coordinate)
235  {
236  case 0:
237  return Point_tri(p.y(),p.z());
238  break;
239  case 1:
240  return Point_tri(p.z(),p.x());
241  break;
242  case 2:
243  return Point_tri(p.x(),p.y());
244  break;
245  case 3:
246  return Point_tri(p.z(),p.y());
247  break;
248  case 4:
249  return Point_tri(p.x(),p.z());
250  break;
251  case 5:
252  return Point_tri(p.y(),p.x());
253  break;
254  default:
255  return Point_tri(p.y(),p.z());
256  }
257  }
258 
265  Vertex_handle_tri add_new_pt(Point_3 p, unsigned long &Label) // MT: suppression r�f�rence
266  {
267  //if the point is not a new one, we verify that the point has not already been added
268  if(Label != 0xFFFFFFFF)
269  for(unsigned int i = 0;i != pts_point.size();++i)
270  if(Label == pts_point[i])
271  //if the point is already in the triangulation, we return its handle
272  return pts_vertex[i];
274  v = ct.insert(get_minvar_point_2(p));
275  v->set_Label(Label);
276  pts_point.push_back(Label);
277  pts_vertex.push_back(v);
278  return v;
279  }
280 
288  void add_segment(Point_3 &p1, Point_3 &p2, unsigned long &Label1, unsigned long &Label2)
289  {
290  // we add the two points in the triangulation and store their handles in c1 and c2
291  c1 = add_new_pt(p1, Label1);
292  c2 = add_new_pt(p2, Label2);
293  // we set a constrained segment between these two points
294  ct.insert_constraint(c1, c2);
295  // if an other segment is added, we will overwrite c1 and c2
296  // what is important is to memorize the handles of the last segment added
297  }
298 
307  std::vector< std::vector<unsigned long> > get_triangles(bool inv_triangles, bool *IsExt)
308  {
309  //init
310  IsExt[0] = false;
311  IsExt[1] = false;
312  IsExt[2] = false;
313  std::vector<std::vector<unsigned long> > tris;
314  for(Face_iterator_tri fi = ct.faces_begin();fi != ct.faces_end();fi++)
315  fi->set_OK(false);
316 
317  //the constrained segments are oriented, we search the triangle (c1, c2, X), (X, c1, c2) or (c2, X, c1)
318  //where c1 and c2 are the two points related to the last constrained segment added, and X another point
319  //this triangle belongs to the result (thanks to the orientation of the segments)
320  Face_handle_tri f, f2 = c1->face();
321  int i=0; // MT
322  do {
323  f = f2;
324  f->has_vertex(c1,i);
325  f2 = f->neighbor(f->ccw(i));
326  } while( ! ( f->has_vertex(c2) && f2->has_vertex(c2) ) );
327 
328  //dans le cas particulier ou la frontiere se trouve exactement sur un bord de la triangulation,
329  //et que ce triangle n'appartient pas a la triangulation, on d�marrera avec l'autre triangle
330  //incluant le segment c1, c2 (et donc, n'appartenant pas au r�sultat
331 
332  //if the segment is exactly on the border of the triangulation, the triangle could be outside the triangulation
333  //in that case, we will search the other triangle including the points c1 and c2
334  //this triangle does not belong to the result
335  if(f->has_vertex(ct.infinite_vertex()))
336  {
337  f = f2;
338  f->set_Ext(false);
339  }
340  else
341  {
342  f->set_Ext(true);
343  }
344 
345  std::stack<Face_handle_tri> sfh;
346  f->set_OK(true);
347  sfh.push(f);
348 
349  //we decide for all the triangles, if they belongs to the result, starting from the first triangle f,
350  //by moving on the triangulation using the connectivity between the triangles.
351  //If a constrained segment is crossed, the value of the tag "isext" is inverted
352  while(!sfh.empty())
353  {
354  f = sfh.top();
355  sfh.pop();
356 
357  if(f->get_Ext())
358  {
359  std::vector<unsigned long> tri;
360  int i;
361  tri.push_back(f->vertex(0)->get_Label());
362 
363  //verify if the neighboring facets belongs to the result or not
364  if(f->has_vertex(v1,i) && f->neighbor(f->ccw(i))->has_vertex(ct.infinite_vertex())) IsExt[0] = true;
365  if(f->has_vertex(v2,i) && f->neighbor(f->ccw(i))->has_vertex(ct.infinite_vertex())) IsExt[1] = true;
366  if(f->has_vertex(v3,i) && f->neighbor(f->ccw(i))->has_vertex(ct.infinite_vertex())) IsExt[2] = true;
367 
368  if(inv_triangles)
369  {
370  tri.push_back(f->vertex(2)->get_Label());
371  tri.push_back(f->vertex(1)->get_Label());
372  }
373  else
374  {
375  tri.push_back(f->vertex(1)->get_Label());
376  tri.push_back(f->vertex(2)->get_Label());
377  }
378  tris.push_back(tri);
379  }
380  for(i = 0;i!=3;i++)
381  {
382  if(!(f->neighbor(i)->get_OK() || f->neighbor(i)->has_vertex(ct.infinite_vertex())))
383  {
384  f->neighbor(i)->set_OK(true);
385  f->neighbor(i)->set_Ext((f->is_constrained(i))?!f->get_Ext():f->get_Ext());
386  sfh.push(f->neighbor(i));
387  }
388  }
389  }
390  return tris;
391  }
392 
399  std::vector< std::vector<unsigned long> > get_all_triangles(bool inv_triangles)
400  {
401  std::vector< std::vector<unsigned long> > tris;
402  for(Face_iterator_tri f = ct.faces_begin();f != ct.faces_end();f++)
403  {
404  std::vector<unsigned long> tri;
405  tri.push_back(f->vertex(0)->get_Label());
406  if(inv_triangles)
407  {
408  tri.push_back(f->vertex(2)->get_Label());
409  tri.push_back(f->vertex(1)->get_Label());
410  }
411  else
412  {
413  tri.push_back(f->vertex(1)->get_Label());
414  tri.push_back(f->vertex(2)->get_Label());
415  }
416  tris.push_back(tri);
417  }
418  return tris;
419  }
420 
421 private:
425  std::vector<InterId> pts_point;
427  std::vector<Vertex_handle_tri> pts_vertex;
447 };
448 
Enriched_vertex_base::Rebind_TDS
Definition: boolops_triangulation.hpp:32
Enriched_face_base::Vertex_handle
Fb::Triangulation_data_structure::Vertex_handle Vertex_handle
Handle for a vertex of a triangulation.
Definition: boolops_triangulation.hpp:62
Enriched_face_base::Enriched_face_base
Enriched_face_base(Vertex_handle v0, Vertex_handle v1, Vertex_handle v2)
Definition: boolops_triangulation.hpp:84
Triangulation::pts_vertex
std::vector< Vertex_handle_tri > pts_vertex
List of the handles of the points added in the triangulation.
Definition: boolops_triangulation.hpp:427
Triangulation::Face_iterator_tri
Constrained_Delaunay_tri::Face_iterator Face_iterator_tri
Iterator for the faces of the triangulation.
Definition: boolops_triangulation.hpp:183
Triangulation::v2
Vertex_handle_tri v2
Handle of the point corresponding to the second vertex of the facet.
Definition: boolops_triangulation.hpp:431
Triangulation::Point_tri
Constrained_Delaunay_tri::Point Point_tri
2d point for the triangulation
Definition: boolops_triangulation.hpp:177
Triangulation::add_segment
void add_segment(Point_3 &p1, Point_3 &p2, unsigned long &Label1, unsigned long &Label2)
Adds a constrained segment in the triangulation.
Definition: boolops_triangulation.hpp:288
Triangulation::max_coordinate
int max_coordinate
Code identifying the plane where the triangulation is done 0 : Plane (y, z) 1 : Plane (z,...
Definition: boolops_triangulation.hpp:446
Point
AIFMesh::Point Point
Definition: Graph_properties_aif.h:21
Triangulation::Tri_vb
Enriched_vertex_base< K > Tri_vb
Vertex base.
Definition: boolops_triangulation.hpp:120
Enriched_face_base
Enriches the faces of a triangulation.
Definition: boolops_triangulation.hpp:56
Enriched_face_base::Face_handle
Fb::Triangulation_data_structure::Face_handle Face_handle
Handle for a face of a triangulation.
Definition: boolops_triangulation.hpp:68
Triangulation::ct
Constrained_Delaunay_tri ct
The triangulation.
Definition: boolops_triangulation.hpp:423
Enriched_face_base::Rebind_TDS
Definition: boolops_triangulation.hpp:71
Enriched_face_base::Enriched_face_base
Enriched_face_base()
Definition: boolops_triangulation.hpp:83
Enriched_vertex_base::m_Label
unsigned long m_Label
An Id for the vertex.
Definition: boolops_triangulation.hpp:39
Triangulation::Tri_fb
Enriched_face_base< K > Tri_fb
Face base.
Definition: boolops_triangulation.hpp:126
Enriched_vertex_base
Enriches the vertices of a triangulation.
Definition: boolops_triangulation.hpp:29
Enriched_face_base::set_OK
void set_OK(bool OK)
Accessor.
Definition: boolops_triangulation.hpp:97
Enriched_face_base::Rebind_TDS::Fb2
Fb::template Rebind_TDS< TDS2 >::Other Fb2
Definition: boolops_triangulation.hpp:72
Enriched_face_base::Enriched_face_base
Enriched_face_base(Vertex_handle v0, Vertex_handle v1, Vertex_handle v2, Face_handle n0, Face_handle n1, Face_handle n2, bool c0, bool c1, bool c2)
Definition: boolops_triangulation.hpp:87
Enriched_face_base::Rebind_TDS::Other
Enriched_face_base< Gt, Fb2 > Other
Definition: boolops_triangulation.hpp:73
Vector_exact
CGAL::Vector_3< Exact_Kernel > Vector_exact
3d vector using exact number type
Definition: boolops_definitions.hpp:73
Triangulation::Tri_DS
CGAL::Triangulation_data_structure_2< Tri_vb, Tri_fb > Tri_DS
Data structure of the triangulation.
Definition: boolops_triangulation.hpp:132
Triangulation::get_minvar_point_2
Point_tri get_minvar_point_2(Point_3 &p)
Compute the orthogonal projection of a point to the plane defined by the longest coordinate of the no...
Definition: boolops_triangulation.hpp:232
Triangulation::c1
Vertex_handle_tri c1
Handle of the point corresponding to the first vertex of the last segment added.
Definition: boolops_triangulation.hpp:435
Enriched_face_base::m_Ext
bool m_Ext
True if the vertex belongs to the result.
Definition: boolops_triangulation.hpp:80
Triangulation::v3
Vertex_handle_tri v3
Handle of the point corresponding to the third vertex of the facet.
Definition: boolops_triangulation.hpp:433
point_to_exact
Point3d_exact point_to_exact(Point3d &p)
Convertion from a Point3d (double) to a Point3d_exact (exact)
Definition: boolops_definitions.hpp:87
Triangulation::Face_handle_tri
Constrained_Delaunay_tri::Face_handle Face_handle_tri
Face handle for the triangulation.
Definition: boolops_triangulation.hpp:171
Triangulation::add_new_pt
Vertex_handle_tri add_new_pt(Point_3 p, unsigned long &Label)
Adds a point in the triangulation.
Definition: boolops_triangulation.hpp:265
Triangulation
To subdivide a facet. (the kernel K must be exact)
Definition: boolops_triangulation.hpp:109
Enriched_face_base::get_Ext
bool get_Ext()
Accessor.
Definition: boolops_triangulation.hpp:94
Enriched_vertex_base::Rebind_TDS::Other
Enriched_vertex_base< Gt, Vb2 > Other
Definition: boolops_triangulation.hpp:34
Triangulation::Point_3
K::Point_3 Point_3
3d point using exact number type
Definition: boolops_triangulation.hpp:114
Enriched_vertex_base::set_Label
void set_Label(unsigned long Label)
Accessor.
Definition: boolops_triangulation.hpp:44
Triangulation::get_triangles
std::vector< std::vector< unsigned long > > get_triangles(bool inv_triangles, bool *IsExt)
Gets the triangles of the triangulation that belongs to the result and deduce for the three neighbori...
Definition: boolops_triangulation.hpp:307
Triangulation::Vertex_handle_tri
Constrained_Delaunay_tri::Vertex_handle Vertex_handle_tri
Vertex handle for the triangulation.
Definition: boolops_triangulation.hpp:165
Triangulation::Constrained_Delaunay_tri
CGAL::Constrained_Delaunay_triangulation_2< K, Tri_DS, Itag > Constrained_Delaunay_tri
2d constrained Delaunay triangulation
Definition: boolops_triangulation.hpp:159
Halfedge_handle
EnrichedPolyhedron::Halfedge_handle Halfedge_handle
Definition: boolops_cpolyhedron_builder.hpp:25
Enriched_face_base::get_OK
bool get_OK()
Accessor.
Definition: boolops_triangulation.hpp:100
Triangulation::c2
Vertex_handle_tri c2
Handle of the point corresponding to the second vertex of the last segment added.
Definition: boolops_triangulation.hpp:437
Enriched_face_base::m_OK
bool m_OK
True if the vertex has been processed.
Definition: boolops_triangulation.hpp:78
Triangulation::v1
Vertex_handle_tri v1
Handle of the point corresponding to the first vertex of the facet.
Definition: boolops_triangulation.hpp:429
Triangulation::get_all_triangles
std::vector< std::vector< unsigned long > > get_all_triangles(bool inv_triangles)
Gets all the triangles of the triangulation.
Definition: boolops_triangulation.hpp:399
Enriched_face_base::set_Ext
void set_Ext(bool Ext)
Accessor.
Definition: boolops_triangulation.hpp:91
Enriched_vertex_base::Rebind_TDS::Vb2
Vb::template Rebind_TDS< TDS2 >::Other Vb2
Definition: boolops_triangulation.hpp:33
Enriched_vertex_base::get_Label
unsigned long get_Label()
Accessor.
Definition: boolops_triangulation.hpp:47
Enriched_face_base::Enriched_face_base
Enriched_face_base(Vertex_handle v0, Vertex_handle v1, Vertex_handle v2, Face_handle n0, Face_handle n1, Face_handle n2)
Definition: boolops_triangulation.hpp:85
Triangulation::pts_point
std::vector< InterId > pts_point
List of the id of the points added in the triangulation.
Definition: boolops_triangulation.hpp:425
Triangulation::Triangulation
Triangulation(Halfedge_handle &he, Vector_exact &norm_dir)
Constructor.
Definition: boolops_triangulation.hpp:191
Triangulation::Itag
CGAL::No_intersection_tag Itag
No intersection tag.
Definition: boolops_triangulation.hpp:152