MEPP2 Project
boolops_cpolyhedron_builder.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/Polyhedron_incremental_builder_3.h>
21 
23 
24 
27 typedef typename EnrichedPolyhedron::Point_3 Point3d;
28 
33 template <class HDS>
34 class CPolyhedron_from_polygon_builder_3 : public CGAL::Modifier_base<HDS> {
35 
36 public:
41  typedef typename HDS::Traits::Point_3 Point_3;
42 
47  typedef typename std::vector<unsigned long> Indices;
48 
53  typedef typename CGAL::Polyhedron_incremental_builder_3<HDS> Builder;
54 
55 private:
56  // Member variables
58  std::vector<Point_3> m_Sorted_vertices;
60  std::vector<Indices> m_Facets_indices;
61 
62 public:
63  // Constructors
68 
74  void add_triangle(Facet_handle &f, bool invert)
75  {
76  //initially, the label of the vertices is 0xFFFFFFFF. if a vertex is added to the result, the tag is set to
77  //the number of vertices added.
78 
79  //creation of a list of indices
80  Indices vi;
81 
82  //adding the first vertex to the result and adding its label to the list of indices.
83  //if the vertex is already added (its label is not 0xFFFFFFFF) we only need to add
84  //this label to "vi" without adding the vertex.
85  Halfedge_handle he = f->facet_begin();
86  if(he->vertex()->Label == 0xFFFFFFFF) add_vertex(he->vertex()->point(), he->vertex()->Label);
87  vi.push_back(he->vertex()->Label);
88 
89  //the order of the two other vertices depends on the orientation of the facet
90  if(!invert)
91  {
92  if(he->next()->vertex()->Label == 0xFFFFFFFF) add_vertex(he->next()->vertex()->point(), he->next()->vertex()->Label);
93  vi.push_back(he->next()->vertex()->Label);
94  if(he->next()->next()->vertex()->Label == 0xFFFFFFFF) add_vertex(he->next()->next()->vertex()->point(), he->next()->next()->vertex()->Label);
95  vi.push_back(he->next()->next()->vertex()->Label);
96  }
97  else
98  {
99  if(he->next()->next()->vertex()->Label == 0xFFFFFFFF) add_vertex(he->next()->next()->vertex()->point(), he->next()->next()->vertex()->Label);
100  vi.push_back(he->next()->next()->vertex()->Label);
101  if(he->next()->vertex()->Label == 0xFFFFFFFF) add_vertex(he->next()->vertex()->point(), he->next()->vertex()->Label);
102  vi.push_back(he->next()->vertex()->Label);
103  }
104 
105  //finally, "vi" is added to the list of the facets
106  m_Facets_indices.push_back(vi);
107  }
108 
114  void add_triangle(std::vector< std::vector< unsigned long > > &T, Halfedge_handle &he)
115  {
116  //For each triangle of the vector T...
117  for(unsigned int i = 0;i != T.size();++i)
118  {
119  //we verify that the indices are valid.
120  //if one of the indices equals to 0xFFFFFFFF, 0xFFFFFFFE or 0XFFFFFFFD, it means that
121  //the corresponding vertex is respectively the first, the second or the third vertex
122  //of the facet.
123  for(unsigned int j = 0 ; j != 3 ; ++j)
124  {
125  switch (T[i][j])
126  {
127  case 0xFFFFFFFF:
128  if(he->vertex()->Label != 0xFFFFFFFF)
129  {
130  T[i][j] = he->vertex()->Label;
131  }
132  else
133  {
134  T[i][j] = static_cast< unsigned long >(m_Sorted_vertices.size());
135  he->vertex()->Label = T[i][j];
136  m_Sorted_vertices.push_back(he->vertex()->point());
137  }
138  break;
139  case 0xFFFFFFFE:
140  if(he->next()->vertex()->Label != 0xFFFFFFFF)
141  {
142  T[i][j] = he->next()->vertex()->Label;
143  }
144  else
145  {
146  T[i][j] = static_cast< unsigned long >(m_Sorted_vertices.size());
147  he->next()->vertex()->Label = T[i][j];
148  m_Sorted_vertices.push_back(he->next()->vertex()->point());
149  }
150  break;
151  case 0xFFFFFFFD:
152  if(he->next()->next()->vertex()->Label != 0xFFFFFFFF)
153  {
154  T[i][j] = he->next()->next()->vertex()->Label;
155  }
156  else
157  {
158  T[i][j] = static_cast< unsigned long >(m_Sorted_vertices.size());
159  he->next()->next()->vertex()->Label = T[i][j];
160  m_Sorted_vertices.push_back(he->next()->next()->vertex()->point());
161  }
162  break;
163  }
164  }
165 
166  //finally, the facet is added to the list of the facets
167  m_Facets_indices.push_back(T[i]);
168  }
169  }
170 
176  void add_vertex(Point3d p, unsigned long &l) // MT: suppression référence
177  {
178  //The value of the label is updated
179  l = static_cast< unsigned long >(m_Sorted_vertices.size());
180  //The vertex is added
181  m_Sorted_vertices.push_back(p);
182  }
183 
188  void operator()(HDS& hds)
189  {
190  Builder B(hds, true);
191  B.begin_surface(3,1);
192  add_vertices(B);
193  add_facets(B);
194  B.end_surface();
195  }
196 
197 private:
198 
204  {
205  for(int i = 0; i != (int)this->m_Sorted_vertices.size(); i++)
206  {
207  B.add_vertex(this->m_Sorted_vertices[i]);
208  }
209  }
210 
216  {
217  for(int i = 0; i != (int)this->m_Facets_indices.size(); i++)
218  {
219  B.begin_facet();
220  for(int j = 0; j != (int)this->m_Facets_indices[i].size(); j++)
221  {
222  B.add_vertex_to_facet(this->m_Facets_indices[i][j]);
223  }
224  B.end_facet();
225  }
226  }
227 };
HDS
EnrichedPolyhedron::HalfedgeDS HDS
Definition: boolops_polyhedra.hpp:45
CPolyhedron_from_polygon_builder_3::add_triangle
void add_triangle(Facet_handle &f, bool invert)
Adds a triangular facet from a facet of a polyhedron.
Definition: boolops_cpolyhedron_builder.hpp:74
CPolyhedron_from_polygon_builder_3::CPolyhedron_from_polygon_builder_3
CPolyhedron_from_polygon_builder_3()
Constructor.
Definition: boolops_cpolyhedron_builder.hpp:67
CPolyhedron_from_polygon_builder_3::add_triangle
void add_triangle(std::vector< std::vector< unsigned long > > &T, Halfedge_handle &he)
Adds a list of triangular facet from an intersected facet of a polyhedron.
Definition: boolops_cpolyhedron_builder.hpp:114
boolops_enriched_polyhedron.hpp
CPolyhedron_from_polygon_builder_3::Point_3
HDS::Traits::Point_3 Point_3
3d point of an halfedge data structure
Definition: boolops_cpolyhedron_builder.hpp:41
CPolyhedron_from_polygon_builder_3::add_vertex
void add_vertex(Point3d p, unsigned long &l)
Adds a Vertex.
Definition: boolops_cpolyhedron_builder.hpp:176
CPolyhedron_from_polygon_builder_3
A polyhedron incremental builder.
Definition: boolops_cpolyhedron_builder.hpp:34
CPolyhedron_from_polygon_builder_3::add_vertices
void add_vertices(Builder &B)
Used to build the vertices of the polyhedron.
Definition: boolops_cpolyhedron_builder.hpp:203
CPolyhedron_from_polygon_builder_3::Builder
CGAL::Polyhedron_incremental_builder_3< HDS > Builder
The polyhedron incremental builder.
Definition: boolops_cpolyhedron_builder.hpp:53
CPolyhedron_from_polygon_builder_3::Indices
std::vector< unsigned long > Indices
A list of indices (unsigned long) to describe a facet.
Definition: boolops_cpolyhedron_builder.hpp:47
Halfedge_handle
EnrichedPolyhedron::Halfedge_handle Halfedge_handle
Definition: boolops_cpolyhedron_builder.hpp:25
Facet_handle
EnrichedPolyhedron::Facet_handle Facet_handle
Definition: boolops_cpolyhedron_builder.hpp:26
CPolyhedron_from_polygon_builder_3::operator()
void operator()(HDS &hds)
this method builds the polyhedron, using the vertices and the facets stored
Definition: boolops_cpolyhedron_builder.hpp:188
Point3d
EnrichedPolyhedron::Point_3 Point3d
Definition: boolops_cpolyhedron_builder.hpp:27
CPolyhedron_from_polygon_builder_3::add_facets
void add_facets(Builder &B)
Used to build the facets of the polyhedron.
Definition: boolops_cpolyhedron_builder.hpp:215
CPolyhedron_from_polygon_builder_3::m_Sorted_vertices
std::vector< Point_3 > m_Sorted_vertices
List of the vertices.
Definition: boolops_cpolyhedron_builder.hpp:58
CPolyhedron_from_polygon_builder_3::m_Facets_indices
std::vector< Indices > m_Facets_indices
List of the facets.
Definition: boolops_cpolyhedron_builder.hpp:60