#337 Enabled conversion directly from Nef polyhedron to PolySets, making us able to deal with more manifold corner cases

master
Marius Kintel 2014-08-21 13:20:32 -04:00
parent 39fc1faee5
commit 51c43af4f4
6 changed files with 36 additions and 13 deletions

Binary file not shown.

View File

@ -53,6 +53,8 @@ size_t CGAL_Nef_polyhedron::memsize() const
Note: Can return NULL if an error occurred Note: Can return NULL if an error occurred
*/ */
// FIXME: Deprecated by CGALUtils::createPolySetFromNefPolyhedron3
#if 0
PolySet *CGAL_Nef_polyhedron::convertToPolyset() const PolySet *CGAL_Nef_polyhedron::convertToPolyset() const
{ {
if (this->isEmpty()) return new PolySet(3); if (this->isEmpty()) return new PolySet(3);
@ -83,6 +85,7 @@ PolySet *CGAL_Nef_polyhedron::convertToPolyset() const
CGAL::set_error_behaviour(old_behaviour); CGAL::set_error_behaviour(old_behaviour);
return ps; return ps;
} }
#endif
void CGAL_Nef_polyhedron::resize(Vector3d newsize, void CGAL_Nef_polyhedron::resize(Vector3d newsize,
const Eigen::Matrix<bool,3,1> &autosize) const Eigen::Matrix<bool,3,1> &autosize)

View File

@ -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 &operator-=(const CGAL_Nef_polyhedron &other); CGAL_Nef_polyhedron &operator-=(const CGAL_Nef_polyhedron &other);
CGAL_Nef_polyhedron &minkowski(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 transform( const Transform3d &matrix );
void resize(Vector3d newsize, const Eigen::Matrix<bool,3,1> &autosize); void resize(Vector3d newsize, const Eigen::Matrix<bool,3,1> &autosize);

View File

@ -60,8 +60,14 @@ shared_ptr<const Geometry> GeometryEvaluator::evaluateGeometry(const AbstractNod
if (!allownef) { if (!allownef) {
if (shared_ptr<const CGAL_Nef_polyhedron> N = dynamic_pointer_cast<const CGAL_Nef_polyhedron>(this->root)) { if (shared_ptr<const CGAL_Nef_polyhedron> N = dynamic_pointer_cast<const CGAL_Nef_polyhedron>(this->root)) {
PolySet *ps = new PolySet(3);
this->root.reset(N->convertToPolyset()); 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); smartCacheInsert(node, this->root);
} }
} }
@ -888,7 +894,14 @@ Response GeometryEvaluator::visit(State &state, const ProjectionNode &node)
if (!chPS) { if (!chPS) {
shared_ptr<const CGAL_Nef_polyhedron> chN = dynamic_pointer_cast<const CGAL_Nef_polyhedron>(chgeom); shared_ptr<const CGAL_Nef_polyhedron> chN = dynamic_pointer_cast<const CGAL_Nef_polyhedron>(chgeom);
if (chN) { 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); if (chPS) poly = PolysetUtils::project(*chPS);

View File

@ -302,7 +302,7 @@ static CGAL_Nef_polyhedron *createNefPolyhedronFromPolySet(const PolySet &ps)
bool err = CGALUtils::createPolyhedronFromPolySet(ps, P); bool err = CGALUtils::createPolyhedronFromPolySet(ps, P);
// if (!err) { // if (!err) {
// PRINTB("Polyhedron is closed: %d", P.is_closed()); // 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); 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 // In projection mode all the triangles are projected manually into the XY plane
else { else {
PolySet *ps3 = N.convertToPolyset(); PolySet ps(3);
if (!ps3) return poly; bool err = CGALUtils::createPolySetFromNefPolyhedron3(*N.p3, ps);
poly = PolysetUtils::project(*ps3); if (err) {
delete ps3; PRINT("ERROR: Nef->PolySet failed");
return poly;
}
poly = PolysetUtils::project(ps);
} }
return poly; return poly;
} }

View File

@ -134,11 +134,14 @@ void export_stl(const PolySet &ps, std::ostream &output)
setlocale(LC_NUMERIC, "C"); // Ensure radix is . (not ,) in output setlocale(LC_NUMERIC, "C"); // Ensure radix is . (not ,) in output
output << "solid OpenSCAD_Model\n"; output << "solid OpenSCAD_Model\n";
BOOST_FOREACH(const PolySet::Polygon &p, triangulated.polygons) { 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 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) { 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 << " endloop\n";
output << " endfacet\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"); PRINT("Object isn't a valid 2-manifold! Modify your design.\n");
} }
bool usePolySet = false; bool usePolySet = true;
if (usePolySet) { if (usePolySet) {
PolySet ps(3); PolySet ps(3);
bool err = CGALUtils::createPolySetFromNefPolyhedron3(*(root_N->p3), ps); bool err = CGALUtils::createPolySetFromNefPolyhedron3(*(root_N->p3), ps);