Skip to content

Instantly share code, notes, and snippets.

@kintel
Created November 7, 2014 17:56
Show Gist options
  • Save kintel/2554b19d3214fe025f23 to your computer and use it in GitHub Desktop.
Save kintel/2554b19d3214fe025f23 to your computer and use it in GitHub Desktop.
#ifdef ENABLE_CGAL
#include "cgalutils.h"
#include "polyset.h"
#include "cgal.h"
namespace CGALUtils {
/*
Create a PolySet from a Nef Polyhedron 3. return false on success,
true on failure. The trick to this is that Nef Polyhedron3 faces have
'holes' in them. . . while PolySet (and many other 3d polyhedron
formats) do not allow for holes in their faces. The function documents
the method used to deal with this
*/
bool createPolySetFromNefPolyhedron3(const CGAL_Nef_polyhedron3 &N, PolySet &ps)
{
bool err = false;
CGAL_Nef_polyhedron3::Halffacet_const_iterator hfaceti;
CGAL_forall_halffacets( hfaceti, N ) {
CGAL::Plane_3<CGAL_Kernel3> plane( hfaceti->plane() );
std::vector<CGAL_Polygon_3> polygons;
// the 0-mark-volume is the 'empty' volume of space. skip it.
if (hfaceti->incident_volume()->mark()) continue;
CGAL_Nef_polyhedron3::Halffacet_cycle_const_iterator cyclei;
CGAL_forall_facet_cycles_of( cyclei, hfaceti ) {
CGAL_Nef_polyhedron3::SHalfedge_around_facet_const_circulator c1(cyclei);
CGAL_Nef_polyhedron3::SHalfedge_around_facet_const_circulator c2(c1);
CGAL_Polygon_3 polygon;
CGAL_For_all( c1, c2 ) {
CGAL_Point_3 p = c1->source()->center_vertex()->point();
polygon.push_back( p );
}
polygons.push_back( polygon );
}
/* at this stage, we have a sequence of polygons. the first
is the "outside edge' or 'body' or 'border', and the rest of the
polygons are 'holes' within the first. there are several
options here to get rid of the holes. we choose to go ahead
and let the tessellater deal with the holes, and then
just output the resulting 3d triangles*/
std::vector<CGAL_Polygon_3> triangles;
bool err = CGALUtils::tessellate3DFaceWithHoles(polygons, triangles, plane);
if (!err) {
for (size_t i=0;i<triangles.size();i++) {
if (triangles[i].size()!=3) {
PRINT("WARNING: triangle doesn't have 3 points. skipping");
continue;
}
ps.append_poly();
for (int j=2;j>=0;j--) {
double x1,y1,z1;
x1 = CGAL::to_double(triangles[i][j].x());
y1 = CGAL::to_double(triangles[i][j].y());
z1 = CGAL::to_double(triangles[i][j].z());
ps.append_vertex(x1,y1,z1);
}
}
}
}
return err;
}
}; // namespace CGALUtils
#endif /* ENABLE_CGAL */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment