EMODnet Quantized Mesh Generator for Cesium
polyhedron_builder_from_c3t3_boundary.h
1 // Copyright (c) 2018 Coronis Computing S.L. (Spain)
2 // All rights reserved.
3 //
4 // This file is part of EMODnet Quantized Mesh Generator for Cesium.
5 //
6 // This program is free software: you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation, either version 3 of the License, or
9 // (at your option) any later version.
10 //
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with this program. If not, see <https://www.gnu.org/licenses/>.
18 //
19 // Author: Ricard Campos (ricardcd@gmail.com)
20 
21 #ifndef EMODNET_QMGC_POLYHEDRON_BUILDER_FROM_C3T3_BOUNDARY_H
22 #define EMODNET_QMGC_POLYHEDRON_BUILDER_FROM_C3T3_BOUNDARY_H
23 
24 #include <CGAL/Polyhedron_incremental_builder_3.h>
25 #include <CGAL/Polygon_mesh_processing/orient_polygon_soup.h>
26 #include "tin_creation/tin_creation_cgal_types.h"
27 #include <CGAL/array.h>
28 #include <map>
29 
30 
31 
36 template<class C3T3, class HDS>
37 class PolyhedronBuilderFromC3T3Boundary : public CGAL::Modifier_base<HDS> {
38 public:
39  C3T3 m_c3t3 ;
40  typename C3T3::Subdomain_index m_index ;
41  typedef typename C3T3::Point::Point Point_3;
42 
43  PolyhedronBuilderFromC3T3Boundary( const C3T3& c,
44  const typename C3T3::Subdomain_index& index ) : m_c3t3(c), m_index(index) {}
45 
46  void operator()( HDS& hds )
47  {
48  // Run over the data needed to create the Polyhedron
49  typedef typename C3T3::Triangulation Triangulation;
50  typedef typename Triangulation::Vertex_handle Vertex_handle;
51 
52  // Vertex-->index map
53  std::map<Vertex_handle, std::size_t> V;
54 
55  std::vector<Point_3 > vertices ;
56  std::vector<std::vector<std::size_t>> facesIndices ;
57 
58  std::size_t numVerts = 0;
59  std::size_t numFacets = 0;
60  for(typename C3T3::Facets_in_complex_iterator
61  fit = m_c3t3.facets_in_complex_begin(),
62  end = m_c3t3.facets_in_complex_end();
63  fit != end; ++fit)
64  {
65  typename C3T3::Subdomain_index cell_sd = m_c3t3.subdomain_index(fit->first);
66  typename C3T3::Subdomain_index opp_sd = m_c3t3.subdomain_index(fit->first->neighbor(fit->second));
67 
68  if (cell_sd!=m_index && opp_sd!=m_index) continue;
69 
70  numFacets++;
71  int j=0;
72 
73  std::vector<std::size_t> indices; indices.resize(3);
74  for (int i = 0; i < 4; ++i) {
75  if (i != fit->second) {
76  // Try to insert the point in the map
77  Vertex_handle v = (*fit).first->vertex(i) ;
78  std::pair<typename std::map<Vertex_handle, std::size_t>::iterator, bool> res = V.insert(std::make_pair(v,numVerts));
79 
80  if (res.second){
81  // The vertex was not in the map, add it to the Polygon
82  numVerts++;
83  vertices.push_back( res.first->first->point().point() );
84  }
85  indices[j++] = res.first->second;
86  }
87  }
88  facesIndices.push_back(indices);
89  }
90 
91  // Coherently orient the faces of the polyhedron
92  CGAL::Polygon_mesh_processing::orient_polygon_soup(vertices, facesIndices);
93 
94  // Polyhedron_3 incremental builder
95  CGAL::Polyhedron_incremental_builder_3<HDS> B( hds, true);
96 
97  // Begin the surface construction
98  B.begin_surface( vertices.size(), facesIndices.size() );
99 
100  for( typename std::vector<Point_3>::iterator it = vertices.begin(); it != vertices.end(); ++it )
101  {
102  B.add_vertex( *it );
103  }
104 
105  for( std::vector<std::vector<std::size_t>>::iterator it = facesIndices.begin(); it != facesIndices.end(); ++it )
106  {
107 // if( B.test_facet(it->begin(), it->end()) == true ) {
108 // std::cout << "Test passed: " << (*it)[0] << ", " << (*it)[1] << ", " << (*it)[2] << std::endl;
109 // std::cout << "adding: " << (*it)[0] << ", " << (*it)[1] << ", " << (*it)[2] << std::endl;
111 // B.begin_facet();
112 // B.add_vertex_to_facet( (*it)[0] );
113 // B.add_vertex_to_facet( (*it)[1] );
114 // B.add_vertex_to_facet( (*it)[2] );
115 // B.end_facet();
116 // }
117 // else {
118 // std::cout << "Test failed: " << (*it)[0] << ", " << (*it)[1] << ", " << (*it)[2] << std::endl;
119 //
120 // std::vector<std::size_t> tri = {(*it)[0], (*it)[2], (*it)[1]};
121 // if( B.test_facet(tri.begin(), tri.end()) == true ) {
122 // std::cout << "adding: " << (*it)[0] << ", " << (*it)[2] << ", " << (*it)[1] << std::endl;
124 // B.begin_facet();
125 // B.add_vertex_to_facet((*it)[0]);
126 // B.add_vertex_to_facet((*it)[2]);
127 // B.add_vertex_to_facet((*it)[1]);
128 // B.end_facet();
129 // }
130 // else {
131 // std::cout << "Test failed again: " << (*it)[0] << ", " << (*it)[2] << ", " << (*it)[1] << std::endl;
132 // std::cout << "adding: " << (*it)[1] << ", " << (*it)[0] << ", " << (*it)[2] << std::endl;
134 // B.begin_facet();
135 // B.add_vertex_to_facet((*it)[1]);
136 // B.add_vertex_to_facet((*it)[0]);
137 // B.add_vertex_to_facet((*it)[2]);
138 // B.end_facet();
139 // }
140 // }
141  B.begin_facet();
142  B.add_vertex_to_facet( (*it)[0] );
143  B.add_vertex_to_facet( (*it)[1] );
144  B.add_vertex_to_facet( (*it)[2] );
145  B.end_facet();
146  }
147 
148  // End the surface
149  B.end_surface();
150  }
151 };
152 
153 #endif //EMODNET_QMGC_POLYHEDRON_BUILDER_FROM_C3T3_BOUNDARY_H
A modifier creating a Polyhedron_3 structure with the incremental builder from the boundary in a C3T3...
Definition: polyhedron_builder_from_c3t3_boundary.h:37