1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
|
// Copyright (c) 2016 GeometryFactory (France).
// All rights reserved.
//
// This file is part of CGAL (www.cgal.org).
//
// $URL: https://github.com/CGAL/cgal/blob/v5.2/Nef_3/include/CGAL/boost/graph/convert_nef_polyhedron_to_polygon_mesh.h $
// $Id: convert_nef_polyhedron_to_polygon_mesh.h 0779373 2020-03-26T13:31:46+01:00 Sébastien Loriot
// SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-Commercial
//
//
// Author(s) : Sebastien Loriot
#ifndef CGAL_BOOST_GRAPH_NEF_POLYHEDRON_TO_POLYGON_MESH_H
#define CGAL_BOOST_GRAPH_NEF_POLYHEDRON_TO_POLYGON_MESH_H
#include <CGAL/license/Nef_3.h>
#include <CGAL/boost/graph/helpers.h>
#include <CGAL/algorithm.h>
#include <CGAL/Polygon_mesh_processing/polygon_soup_to_polygon_mesh.h>
#include <CGAL/circulator.h>
#include <CGAL/Cartesian_converter.h>
#include <boost/unordered_map.hpp>
#include <CGAL/Constrained_Delaunay_triangulation_2.h>
#include <CGAL/Triangulation_2_projection_traits_3.h>
#include <CGAL/Triangulation_vertex_base_with_info_2.h>
#include <CGAL/Triangulation_face_base_with_info_2.h>
#include <CGAL/Kernel/global_functions_3.h>
namespace CGAL{
namespace nef_to_pm{
// Visitor used to collect and index vertices of a shell
template<class Nef_polyhedron, class PointRange, class Converter>
struct Shell_vertex_index_visitor
{
typedef boost::unordered_map<
typename Nef_polyhedron::Vertex_const_handle, std::size_t> Vertex_index_map;
typedef typename PointRange::value_type Point_3;
PointRange& points;
Vertex_index_map vertex_indices;
const Converter& converter;
Shell_vertex_index_visitor(PointRange& points, const Converter& converter)
:points(points), converter(converter)
{}
void visit(typename Nef_polyhedron::Vertex_const_handle vh)
{
std::pair<typename Vertex_index_map::iterator, bool> insert_res =
vertex_indices.insert( std::make_pair(vh, points.size()) );
if (insert_res.second)
points.push_back( converter(vh->point()));
}
void visit(typename Nef_polyhedron::Halfedge_const_handle )
{}
void visit(typename Nef_polyhedron::Halffacet_const_handle )
{}
void visit(typename Nef_polyhedron::SHalfedge_const_handle )
{}
void visit(typename Nef_polyhedron::SHalfloop_const_handle )
{}
void visit(typename Nef_polyhedron::SFace_const_handle )
{}
};
struct FaceInfo2
{
FaceInfo2() : visited(false) {}
bool visited;
};
//Visitor used to collect polygons
template <class Nef_polyhedron, typename PolygonRange>
struct Shell_polygons_visitor
{
typedef boost::unordered_map<typename Nef_polyhedron::Vertex_const_handle, std::size_t> Vertex_index_map;
typedef typename PolygonRange::value_type Polygon;
Vertex_index_map& vertex_indices;
PolygonRange& polygons;
bool triangulate_all_faces;
Shell_polygons_visitor(Vertex_index_map& vertex_indices,
PolygonRange& polygons,
bool triangulate_all_faces)
: vertex_indices( vertex_indices )
, polygons(polygons)
, triangulate_all_faces(triangulate_all_faces)
{}
std::size_t get_cycle_length( typename Nef_polyhedron::Halffacet_cycle_const_iterator hfc) const
{
typename Nef_polyhedron::SHalfedge_around_facet_const_circulator hc_start(hfc), hc_end(hc_start);
std::size_t i=0;
CGAL_For_all(hc_start,hc_end)
{
++i;
}
return i;
}
void visit(typename Nef_polyhedron::Halffacet_const_handle opposite_facet)
{
bool is_marked=opposite_facet->incident_volume()->mark();
CGAL_assertion(Nef_polyhedron::Infi_box::is_standard(opposite_facet->plane()));
typename Nef_polyhedron::SHalfedge_const_handle se;
typename Nef_polyhedron::Halffacet_cycle_const_iterator fc;
typename Nef_polyhedron::Halffacet_const_handle f = opposite_facet->twin();
if (std::next(f->facet_cycles_begin())==f->facet_cycles_end())
{
std::size_t cycle_length = 3;
if (triangulate_all_faces)
cycle_length = get_cycle_length(f->facet_cycles_begin());
switch(cycle_length)
{
case 4:
{
//collect vertices
std::vector<typename Nef_polyhedron::Vertex_const_handle> v;
fc = f->facet_cycles_begin();
se = typename Nef_polyhedron::SHalfedge_const_handle(fc);
CGAL_assertion(se!=0);
typename Nef_polyhedron::SHalfedge_around_facet_const_circulator
hc_start(se), hc_end(hc_start);
// insert the vertex indices of the new polygon
CGAL_For_all(hc_start,hc_end)
v.push_back(hc_start->source()->center_vertex());
if( CGAL::cross_product(v[1]->point()-v[0]->point(),v[1]->point()-v[2]->point()) *
CGAL::cross_product(v[3]->point()-v[2]->point(),v[3]->point()-v[0]->point())
>
CGAL::cross_product(v[2]->point()-v[1]->point(),v[3]->point()-v[2]->point()) *
CGAL::cross_product(v[0]->point()-v[3]->point(),v[1]->point()-v[0]->point()))
{
if (is_marked)
{
polygons.push_back( Polygon() );
polygons.back().push_back(vertex_indices[v[0]]);
polygons.back().push_back(vertex_indices[v[1]]);
polygons.back().push_back(vertex_indices[v[2]]);
polygons.push_back( Polygon() );
polygons.back().push_back(vertex_indices[v[0]]);
polygons.back().push_back(vertex_indices[v[2]]);
polygons.back().push_back(vertex_indices[v[3]]);
}
else
{
polygons.push_back( Polygon() );
polygons.back().push_back(vertex_indices[v[1]]);
polygons.back().push_back(vertex_indices[v[0]]);
polygons.back().push_back(vertex_indices[v[2]]);
polygons.push_back( Polygon() );
polygons.back().push_back(vertex_indices[v[2]]);
polygons.back().push_back(vertex_indices[v[0]]);
polygons.back().push_back(vertex_indices[v[3]]);
}
}
else
{
if (is_marked)
{
polygons.push_back( Polygon() );
polygons.back().push_back(vertex_indices[v[0]]);
polygons.back().push_back(vertex_indices[v[1]]);
polygons.back().push_back(vertex_indices[v[3]]);
polygons.push_back( Polygon() );
polygons.back().push_back(vertex_indices[v[3]]);
polygons.back().push_back(vertex_indices[v[1]]);
polygons.back().push_back(vertex_indices[v[2]]);
}
else
{
polygons.push_back( Polygon() );
polygons.back().push_back(vertex_indices[v[0]]);
polygons.back().push_back(vertex_indices[v[3]]);
polygons.back().push_back(vertex_indices[v[1]]);
polygons.push_back( Polygon() );
polygons.back().push_back(vertex_indices[v[1]]);
polygons.back().push_back(vertex_indices[v[3]]);
polygons.back().push_back(vertex_indices[v[2]]);
}
}
return;
}
case 3:
{
// create a new polygon
polygons.push_back( Polygon() );
fc = f->facet_cycles_begin();
se = typename Nef_polyhedron::SHalfedge_const_handle(fc);
CGAL_assertion(se!=0);
typename Nef_polyhedron::SHalfedge_around_facet_const_circulator
hc_start(se), hc_end(hc_start);
// insert the vertex indices of the new polygon
CGAL_For_all(hc_start,hc_end)
polygons.back().push_back(vertex_indices[hc_start->source()->center_vertex()]);
if (!is_marked)
std::reverse(polygons.back().begin(), polygons.back().end());
return;
}
default:
break;
}
}
// cases where a cdt is needed
typedef typename Nef_polyhedron::Kernel Kernel;
typedef Triangulation_2_projection_traits_3<Kernel> P_traits;
typedef Triangulation_vertex_base_with_info_2<std::size_t, P_traits> Vb;
typedef Triangulation_face_base_with_info_2<FaceInfo2,P_traits> Fbb;
typedef Constrained_triangulation_face_base_2<P_traits,Fbb> Fb;
typedef Triangulation_data_structure_2<Vb,Fb> TDS;
typedef Exact_predicates_tag Itag;
typedef Constrained_Delaunay_triangulation_2<P_traits, TDS, Itag> CDT;
P_traits p_traits(opposite_facet->plane().orthogonal_vector());
CDT cdt(p_traits);
// insert each connected component of the boundary of the face as
// a polygonal constraints
typename Nef_polyhedron::Halffacet_cycle_const_iterator
hfc_start=f->facet_cycles_begin(), hfc_end=f->facet_cycles_end();
CGAL_For_all(hfc_start, hfc_end)
{
fc=hfc_start;
se = typename Nef_polyhedron::SHalfedge_const_handle(fc);
CGAL_assertion(se!=0);
typename Nef_polyhedron::SHalfedge_around_facet_const_circulator
hc_start(se), hc_end(hc_start);
// collect contour vertices
std::vector< typename CDT::Vertex_handle > polygon;
CGAL_For_all(hc_start,hc_end)
{
typename CDT::Vertex_handle vh=cdt.insert(hc_start->source()->center_vertex()->point());
vh->info() = vertex_indices[hc_start->source()->center_vertex()];
polygon.push_back(vh);
}
std::size_t nb_constraints = polygon.size();
polygon.push_back(polygon.front());
for(std::size_t i=0; i<nb_constraints; ++i)
cdt.insert_constraint(polygon[i], polygon[i+1]);
}
//look for a triangle inside the domain of the face
typename CDT::Face_handle fh = cdt.infinite_face();
fh->info().visited=true;
std::vector<typename CDT::Edge> queue;
for (int i=0; i<3; ++i)
queue.push_back(typename CDT::Edge(fh, i) );
while(true)
{
typename CDT::Edge e = queue.back();
queue.pop_back();
e=cdt.mirror_edge(e);
if (e.first->info().visited) continue;
if (cdt.is_constrained(e))
{
queue.clear();
queue.push_back(e);
break;
}
else
{
for(int i=1; i<3; ++i)
{
typename CDT::Edge candidate(e.first, (e.second+i)%3);
if (!candidate.first->neighbor(candidate.second)->info().visited)
queue.push_back( candidate );
}
e.first->info().visited=true;
}
}
// now extract triangles inside the face
while(!queue.empty())
{
typename CDT::Edge e = queue.back();
queue.pop_back();
if (e.first->info().visited) continue;
e.first->info().visited=true;
polygons.push_back(Polygon());
if (is_marked)
for (int i=2; i>=0; --i)
polygons.back().push_back(e.first->vertex(i)->info());
else
for (int i=0; i<3; ++i)
polygons.back().push_back(e.first->vertex(i)->info());
for(int i=1; i<3; ++i)
{
typename CDT::Edge candidate(e.first, (e.second+i)%3);
if (!cdt.is_constrained(candidate) &&
!candidate.first->neighbor(candidate.second)->info().visited)
{
queue.push_back( cdt.mirror_edge(candidate) );
}
}
}
}
void visit(typename Nef_polyhedron::SFace_const_handle)
{}
void visit(typename Nef_polyhedron::Halfedge_const_handle)
{}
void visit(typename Nef_polyhedron::Vertex_const_handle)
{}
void visit(typename Nef_polyhedron::SHalfedge_const_handle)
{}
void visit(typename Nef_polyhedron::SHalfloop_const_handle)
{}
};
template <typename PointRange, class Nef_polyhedron, class Converter, typename PolygonRange>
void collect_polygon_mesh_info(
PointRange& points,
PolygonRange& polygons,
Nef_polyhedron& nef,
typename Nef_polyhedron::Shell_entry_const_iterator shell,
const Converter& converter,
bool triangulate_all_faces)
{
// collect points and set vertex indices
Shell_vertex_index_visitor<Nef_polyhedron, PointRange, Converter> vertex_index_visitor(points, converter);
nef.visit_shell_objects(typename Nef_polyhedron::SFace_const_handle(shell), vertex_index_visitor);
// collect polygons
Shell_polygons_visitor<Nef_polyhedron, PolygonRange> polygon_visitor(
vertex_index_visitor.vertex_indices,
polygons,
triangulate_all_faces);
nef.visit_shell_objects(typename Nef_polyhedron::SFace_const_handle(shell), polygon_visitor);
}
} //end of namespace nef_to_pm
template < class Nef_polyhedron, typename PolygonRange, typename PointRange>
void convert_nef_polyhedron_to_polygon_soup(const Nef_polyhedron& nef,
PointRange& points,
PolygonRange& polygons,
bool triangulate_all_faces = false)
{
typedef typename Nef_polyhedron::Point_3 Point_3;
typedef typename Kernel_traits<Point_3>::Kernel Nef_Kernel;
typedef typename PointRange::value_type Out_Point;
typedef typename Kernel_traits<Out_Point>::Kernel Output_kernel;
typedef Cartesian_converter<Nef_Kernel, Output_kernel> Converter;
typename Nef_polyhedron::Volume_const_iterator vol_it = nef.volumes_begin(),
vol_end = nef.volumes_end();
if ( Nef_polyhedron::Infi_box::extended_kernel() ) ++vol_it; // skip Infi_box
CGAL_assertion ( vol_it != vol_end );
++vol_it; // skip unbounded volume
Converter to_output;
for (;vol_it!=vol_end;++vol_it)
nef_to_pm::collect_polygon_mesh_info(points,
polygons,
nef,
vol_it->shells_begin(),
to_output,
triangulate_all_faces);
}
template <class Nef_polyhedron, class Polygon_mesh>
void convert_nef_polyhedron_to_polygon_mesh(const Nef_polyhedron& nef, Polygon_mesh& pm, bool triangulate_all_faces = false)
{
typedef typename boost::property_traits<typename boost::property_map<Polygon_mesh, vertex_point_t>::type>::value_type PM_Point;
std::vector<PM_Point> points;
std::vector<std::vector<std::size_t> > polygons;
convert_nef_polyhedron_to_polygon_soup(nef, points, polygons, triangulate_all_faces);
Polygon_mesh_processing::polygon_soup_to_polygon_mesh(points, polygons, pm);
}
} //end of namespace CGAL
#endif // CGAL_BOOST_GRAPH_NEF_POLYHEDRON_TO_POLYGON_MESH_H
|