From 0a2b7ca0d4d7fb1acda9996d69b25ff6b5495856 Mon Sep 17 00:00:00 2001 From: Don Bright Date: Sat, 14 Dec 2013 17:20:35 -0600 Subject: [PATCH] FIXME leaking polyset by altering functions signatures --- src/CGALEvaluator.cc | 26 ++++++++++++++++++++------ src/CGAL_Nef_polyhedron.cc | 5 ++++- src/cgalutils.h | 4 ++-- src/dxftess-cgal.cc | 16 ++++++++++++++++ src/dxftess.h | 1 + src/import.cc | 3 ++- src/polyset.h | 1 - 7 files changed, 45 insertions(+), 11 deletions(-) diff --git a/src/CGALEvaluator.cc b/src/CGALEvaluator.cc index ec013153..26d3e4ce 100644 --- a/src/CGALEvaluator.cc +++ b/src/CGALEvaluator.cc @@ -668,16 +668,30 @@ CGAL_Nef_polyhedron CGALEvaluator::evaluateCGALMesh(const PolySet &ps) else // not (this->is2d) { CGAL_Nef_polyhedron3 *N = NULL; + bool plane_error = false; CGAL::Failure_behaviour old_behaviour = CGAL::set_error_behaviour(CGAL::THROW_EXCEPTION); try { - // FIXME: Are we leaking memory for the CGAL_Polyhedron object? - CGAL_Polyhedron *P = createPolyhedronFromPolySet(ps); - if (P) { - N = new CGAL_Nef_polyhedron3(*P); - } + CGAL_Polyhedron P; + createPolyhedronFromPolySet(ps,P); + N = new CGAL_Nef_polyhedron3(P); } catch (const CGAL::Assertion_exception &e) { - PRINTB("CGAL error in CGAL_Nef_polyhedron3(): %s", e.what()); + if (std::string(e.what()).find("Plane_constructor")!=std::string::npos) { + PRINT("PolySet has nonplanar faces. Attempting alternate construction"); + plane_error=true; + } else { + PRINTB("CGAL error in CGAL_Nef_polyhedron3(): %s", e.what()); + } + } + if (plane_error) try { + PolySet ps2; + CGAL_Polyhedron P; + tessellate_faces( ps, ps2 ); + createPolyhedronFromPolySet(ps2,P); + N = new CGAL_Nef_polyhedron3(P); + } + catch (const CGAL::Assertion_exception &e) { + PRINTB("Alternate construction failed. CGAL error in CGAL_Nef_polyhedron3(): %s", e.what()); } CGAL::set_error_behaviour(old_behaviour); return CGAL_Nef_polyhedron(N); diff --git a/src/CGAL_Nef_polyhedron.cc b/src/CGAL_Nef_polyhedron.cc index 440f4edd..2d5bba58 100644 --- a/src/CGAL_Nef_polyhedron.cc +++ b/src/CGAL_Nef_polyhedron.cc @@ -97,11 +97,14 @@ PolySet *CGAL_Nef_polyhedron::convertToPolyset() else if (this->dim == 3) { CGAL::Failure_behaviour old_behaviour = CGAL::set_error_behaviour(CGAL::THROW_EXCEPTION); try { + ps = new PolySet(); CGAL_Polyhedron P; this->p3->convert_to_Polyhedron(P); - ps = createPolySetFromPolyhedron(P); + bool err = createPolySetFromPolyhedron(P, ps); + if (err) delete ps; } catch (const CGAL::Precondition_exception &e) { + delete ps; PRINTB("CGAL error in CGAL_Nef_polyhedron::convertToPolyset(): %s", e.what()); } CGAL::set_error_behaviour(old_behaviour); diff --git a/src/cgalutils.h b/src/cgalutils.h index d25fd4ca..50b819ad 100644 --- a/src/cgalutils.h +++ b/src/cgalutils.h @@ -2,8 +2,8 @@ #define CGALUTILS_H_ #include -class PolySet *createPolySetFromPolyhedron(const CGAL_Polyhedron &p); -CGAL_Polyhedron *createPolyhedronFromPolySet(const class PolySet &ps); +bool createPolySetFromPolyhedron(const CGAL_Polyhedron &p, class PolySet &ps); +bool createPolyhedronFromPolySet(const class PolySet &ps, class CGAL_Polyhedron &p); CGAL_Iso_cuboid_3 bounding_box( const CGAL_Nef_polyhedron3 &N ); CGAL_Iso_rectangle_2e bounding_box( const CGAL_Nef_polyhedron2 &N ); diff --git a/src/dxftess-cgal.cc b/src/dxftess-cgal.cc index 16eaf9fb..a3ebccf5 100644 --- a/src/dxftess-cgal.cc +++ b/src/dxftess-cgal.cc @@ -335,3 +335,19 @@ void dxf_tesselate(PolySet *ps, DxfData &dxf, double rot, Vector2d scale, bool u dxf.paths[path[2]].is_inner = !up; } } + +void triangulate_polygon( const PolySet::Polygon &pgon, std::vector &triangles ) +{ +} + +/* given a 3d PolySet with 'near planar' faces, triangulate the faces +so CGAL Nef Polyhedron will accept them. */ +void tessellate_3d_faces( PolySet &inps, PolySet &outps ) { + for (size_t i = 0; i < inps.polygons.size(); i++) { + const PolySet::Polygon *pgon = &inps.polygons[i]; + for (size_t j = 0; j < pgon->size(); j++) { + Vector3d v = pgon->at(j); + } + } +} + diff --git a/src/dxftess.h b/src/dxftess.h index f0f27b5c..3d96747f 100644 --- a/src/dxftess.h +++ b/src/dxftess.h @@ -7,5 +7,6 @@ class DxfData; class PolySet; void dxf_tesselate(PolySet *ps, DxfData &dxf, double rot, Vector2d scale, bool up, bool do_triangle_splitting, double h); void dxf_border_to_ps(PolySet *ps, const DxfData &dxf); +void tessellate_3d_faces( PolySet &inps, PolySet &outps ); #endif diff --git a/src/import.cc b/src/import.cc index 38973312..af905fcb 100644 --- a/src/import.cc +++ b/src/import.cc @@ -284,7 +284,8 @@ PolySet *ImportNode::evaluate_polyset(class PolySetEvaluator *) const file >> poly; file.close(); - p = createPolySetFromPolyhedron(poly); + p = new PolySet(); + bool err = createPolySetFromPolyhedron(poly, *p); } #else PRINT("WARNING: OFF import requires CGAL."); diff --git a/src/polyset.h b/src/polyset.h index 6626f797..5c973961 100644 --- a/src/polyset.h +++ b/src/polyset.h @@ -26,7 +26,6 @@ public: void append_vertex(double x, double y, double z = 0.0); void insert_vertex(double x, double y, double z = 0.0); size_t memsize() const; - BoundingBox getBoundingBox() const; #define CSGMODE_DIFFERENCE_FLAG 0x10