diff --git a/doc/OpenSCAD-polygons.graffle b/doc/OpenSCAD-polygons.graffle index 63985dcb..cda8bc76 100644 Binary files a/doc/OpenSCAD-polygons.graffle and b/doc/OpenSCAD-polygons.graffle differ diff --git a/src/CGAL_Nef_polyhedron.cc b/src/CGAL_Nef_polyhedron.cc index a3a0e0e0..dfe16536 100644 --- a/src/CGAL_Nef_polyhedron.cc +++ b/src/CGAL_Nef_polyhedron.cc @@ -53,6 +53,8 @@ size_t CGAL_Nef_polyhedron::memsize() const Note: Can return NULL if an error occurred */ +// FIXME: Deprecated by CGALUtils::createPolySetFromNefPolyhedron3 +#if 0 PolySet *CGAL_Nef_polyhedron::convertToPolyset() const { if (this->isEmpty()) return new PolySet(3); @@ -83,6 +85,7 @@ PolySet *CGAL_Nef_polyhedron::convertToPolyset() const CGAL::set_error_behaviour(old_behaviour); return ps; } +#endif void CGAL_Nef_polyhedron::resize(Vector3d newsize, const Eigen::Matrix &autosize) diff --git a/src/CGAL_Nef_polyhedron.h b/src/CGAL_Nef_polyhedron.h index c1346710..f3585469 100644 --- a/src/CGAL_Nef_polyhedron.h +++ b/src/CGAL_Nef_polyhedron.h @@ -27,7 +27,8 @@ public: CGAL_Nef_polyhedron &operator*=(const CGAL_Nef_polyhedron &other); CGAL_Nef_polyhedron &operator-=(const CGAL_Nef_polyhedron &other); CGAL_Nef_polyhedron &minkowski(const CGAL_Nef_polyhedron &other); - class PolySet *convertToPolyset() const; +// FIXME: Deprecated by CGALUtils::createPolySetFromNefPolyhedron3 +// class PolySet *convertToPolyset() const; void transform( const Transform3d &matrix ); void resize(Vector3d newsize, const Eigen::Matrix &autosize); diff --git a/src/GeometryEvaluator.cc b/src/GeometryEvaluator.cc index 23ae1f0a..1ba6f169 100644 --- a/src/GeometryEvaluator.cc +++ b/src/GeometryEvaluator.cc @@ -60,8 +60,14 @@ shared_ptr GeometryEvaluator::evaluateGeometry(const AbstractNod if (!allownef) { if (shared_ptr N = dynamic_pointer_cast(this->root)) { - - this->root.reset(N->convertToPolyset()); + PolySet *ps = new PolySet(3); + ps->setConvexity(N->getConvexity()); + this->root.reset(ps); + bool err = CGALUtils::createPolySetFromNefPolyhedron3(*N->p3, *ps); + if (err) { + PRINT("ERROR: Nef->PolySet failed"); + } + smartCacheInsert(node, this->root); } } @@ -888,7 +894,14 @@ Response GeometryEvaluator::visit(State &state, const ProjectionNode &node) if (!chPS) { shared_ptr chN = dynamic_pointer_cast(chgeom); if (chN) { - chPS.reset(chN->convertToPolyset()); + PolySet *ps = new PolySet(3); + bool err = CGALUtils::createPolySetFromNefPolyhedron3(*chN->p3, *ps); + if (err) { + PRINT("ERROR: Nef->PolySet failed"); + } + else { + chPS.reset(ps); + } } } if (chPS) poly = PolysetUtils::project(*chPS); diff --git a/src/cgalutils.cc b/src/cgalutils.cc index 0221757a..fb5fcbbb 100644 --- a/src/cgalutils.cc +++ b/src/cgalutils.cc @@ -302,7 +302,7 @@ static CGAL_Nef_polyhedron *createNefPolyhedronFromPolySet(const PolySet &ps) bool err = CGALUtils::createPolyhedronFromPolySet(ps, P); // if (!err) { // PRINTB("Polyhedron is closed: %d", P.is_closed()); - // PRINTB("Polyhedron is valid: %d", P.is_valid(true, 0)); + // PRINTB("Polyhedron is valid: %d", P.is_valid(false, 0)); // } if (!err) N = new CGAL_Nef_polyhedron3(P); @@ -888,10 +888,13 @@ namespace CGALUtils { } // In projection mode all the triangles are projected manually into the XY plane else { - PolySet *ps3 = N.convertToPolyset(); - if (!ps3) return poly; - poly = PolysetUtils::project(*ps3); - delete ps3; + PolySet ps(3); + bool err = CGALUtils::createPolySetFromNefPolyhedron3(*N.p3, ps); + if (err) { + PRINT("ERROR: Nef->PolySet failed"); + return poly; + } + poly = PolysetUtils::project(ps); } return poly; } diff --git a/src/export.cc b/src/export.cc index 5d8c8115..c9fe378c 100644 --- a/src/export.cc +++ b/src/export.cc @@ -134,11 +134,14 @@ void export_stl(const PolySet &ps, std::ostream &output) setlocale(LC_NUMERIC, "C"); // Ensure radix is . (not ,) in output output << "solid OpenSCAD_Model\n"; BOOST_FOREACH(const PolySet::Polygon &p, triangulated.polygons) { - output << " facet normal 0 0 0\n"; - output << " outer loop\n"; assert(p.size() == 3); // STL only allows triangles + Vector3d normal = (p[1] - p[0]).cross(p[2] - p[0]); + normal.normalize(); + output << " facet normal " << normal[0] << " " << normal[1] << " " << normal[2] << "\n"; + output << " outer loop\n"; + BOOST_FOREACH(const Vector3d &v, p) { - output << "vertex " << v[0] << " " << v[1] << " " << v[2] << "\n"; + output << " vertex " << v[0] << " " << v[1] << " " << v[2] << "\n"; } output << " endloop\n"; output << " endfacet\n"; @@ -230,7 +233,7 @@ void export_stl(const CGAL_Nef_polyhedron *root_N, std::ostream &output) PRINT("Object isn't a valid 2-manifold! Modify your design.\n"); } - bool usePolySet = false; + bool usePolySet = true; if (usePolySet) { PolySet ps(3); bool err = CGALUtils::createPolySetFromNefPolyhedron3(*(root_N->p3), ps);