diff options
author | Marius Kintel <marius@kintel.net> | 2012-02-19 12:09:02 (GMT) |
---|---|---|
committer | Marius Kintel <marius@kintel.net> | 2012-02-19 12:09:02 (GMT) |
commit | c5703b44077ca6185f7611d6ffba68f4955c1cbd (patch) | |
tree | d427746f36983ea977a84e0d04a04d179592ce7c /src | |
parent | 955e0f1344d772119e2d7af1afb7658df7e8c43e (diff) |
bugfix: Fix crash bug when using zero scale factors. Reported by Alan Cox
Diffstat (limited to 'src')
-rw-r--r-- | src/CGALEvaluator.cc | 58 | ||||
-rw-r--r-- | src/CGAL_Nef_polyhedron.cc | 2 | ||||
-rw-r--r-- | src/PolySetCGALEvaluator.cc | 28 | ||||
-rw-r--r-- | src/transform.cc | 9 | ||||
-rw-r--r-- | src/value.cc | 4 | ||||
-rw-r--r-- | src/value.h | 2 |
6 files changed, 61 insertions, 42 deletions
diff --git a/src/CGALEvaluator.cc b/src/CGALEvaluator.cc index 15fa746..0e849a8 100644 --- a/src/CGALEvaluator.cc +++ b/src/CGALEvaluator.cc @@ -252,31 +252,45 @@ Response CGALEvaluator::visit(State &state, const TransformNode &node) // objects. So we convert in to our internal 2d data format, transform it, // tesselate it and create a new CGAL_Nef_polyhedron2 from it.. What a hack! - CGAL_Aff_transformation2 t( - node.matrix(0,0), node.matrix(0,1), node.matrix(0,3), - node.matrix(1,0), node.matrix(1,1), node.matrix(1,3), node.matrix(3,3)); - - DxfData *dd = N.convertToDxfData(); - for (size_t i=0; i < dd->points.size(); i++) { - CGAL_Kernel2::Point_2 p = CGAL_Kernel2::Point_2(dd->points[i][0], dd->points[i][1]); - p = t.transform(p); - dd->points[i][0] = to_double(p.x()); - dd->points[i][1] = to_double(p.y()); + Eigen::Matrix2f testmat; + testmat << node.matrix(0,0), node.matrix(0,1), node.matrix(1,0), node.matrix(1,1); + if (testmat.determinant() == 0) { + PRINT("Warning: Scaling a 2D object with 0 - removing object"); + N.p2.reset(); + } + else { + CGAL_Aff_transformation2 t( + node.matrix(0,0), node.matrix(0,1), node.matrix(0,3), + node.matrix(1,0), node.matrix(1,1), node.matrix(1,3), node.matrix(3,3)); + + DxfData *dd = N.convertToDxfData(); + for (size_t i=0; i < dd->points.size(); i++) { + CGAL_Kernel2::Point_2 p = CGAL_Kernel2::Point_2(dd->points[i][0], dd->points[i][1]); + p = t.transform(p); + dd->points[i][0] = to_double(p.x()); + dd->points[i][1] = to_double(p.y()); + } + + PolySet ps; + ps.is2d = true; + dxf_tesselate(&ps, *dd, 0, true, false, 0); + + N = evaluateCGALMesh(ps); + delete dd; } - - PolySet ps; - ps.is2d = true; - dxf_tesselate(&ps, *dd, 0, true, false, 0); - - N = evaluateCGALMesh(ps); - delete dd; } else if (N.dim == 3) { - CGAL_Aff_transformation t( - node.matrix(0,0), node.matrix(0,1), node.matrix(0,2), node.matrix(0,3), - node.matrix(1,0), node.matrix(1,1), node.matrix(1,2), node.matrix(1,3), - node.matrix(2,0), node.matrix(2,1), node.matrix(2,2), node.matrix(2,3), node.matrix(3,3)); - N.p3->transform(t); + if (node.matrix.matrix().determinant() == 0) { + PRINT("Warning: Scaling a 3D object with 0 - removing object"); + N.p3.reset(); + } + else { + CGAL_Aff_transformation t( + node.matrix(0,0), node.matrix(0,1), node.matrix(0,2), node.matrix(0,3), + node.matrix(1,0), node.matrix(1,1), node.matrix(1,2), node.matrix(1,3), + node.matrix(2,0), node.matrix(2,1), node.matrix(2,2), node.matrix(2,3), node.matrix(3,3)); + N.p3->transform(t); + } } } else { diff --git a/src/CGAL_Nef_polyhedron.cc b/src/CGAL_Nef_polyhedron.cc index 04783e5..ba298ad 100644 --- a/src/CGAL_Nef_polyhedron.cc +++ b/src/CGAL_Nef_polyhedron.cc @@ -61,6 +61,8 @@ CGAL_Nef_polyhedron &CGAL_Nef_polyhedron::minkowski(const CGAL_Nef_polyhedron &o int CGAL_Nef_polyhedron::weight() const { + if (this->empty()) return 0; + size_t memsize = sizeof(CGAL_Nef_polyhedron); if (this->dim == 2) { memsize += sizeof(CGAL_Nef_polyhedron2) + diff --git a/src/PolySetCGALEvaluator.cc b/src/PolySetCGALEvaluator.cc index 81ae31e..6ed1ab4 100644 --- a/src/PolySetCGALEvaluator.cc +++ b/src/PolySetCGALEvaluator.cc @@ -281,12 +281,14 @@ PolySet *PolySetCGALEvaluator::evaluatePolySet(const LinearExtrudeNode &node) BOOST_FOREACH (AbstractNode * v, node.getChildren()) { if (v->modinst->isBackground()) continue; CGAL_Nef_polyhedron N = this->cgalevaluator.evaluateCGALMesh(*v); - if (N.dim != 2) { - PRINT("ERROR: linear_extrude() is not defined for 3D child objects!"); - } - else { - if (sum.empty()) sum = N.copy(); - else sum += N; + if (!N.empty()) { + if (N.dim != 2) { + PRINT("ERROR: linear_extrude() is not defined for 3D child objects!"); + } + else { + if (sum.empty()) sum = N.copy(); + else sum += N; + } } } @@ -379,12 +381,14 @@ PolySet *PolySetCGALEvaluator::evaluatePolySet(const RotateExtrudeNode &node) BOOST_FOREACH (AbstractNode * v, node.getChildren()) { if (v->modinst->isBackground()) continue; CGAL_Nef_polyhedron N = this->cgalevaluator.evaluateCGALMesh(*v); - if (N.dim != 2) { - PRINT("ERROR: rotate_extrude() is not defined for 3D child objects!"); - } - else { - if (sum.empty()) sum = N.copy(); - else sum += N; + if (!N.empty()) { + if (N.dim != 2) { + PRINT("ERROR: rotate_extrude() is not defined for 3D child objects!"); + } + else { + if (sum.empty()) sum = N.copy(); + else sum += N; + } } } diff --git a/src/transform.cc b/src/transform.cc index f5038b1..c2ac194 100644 --- a/src/transform.cc +++ b/src/transform.cc @@ -87,11 +87,10 @@ AbstractNode *TransformModule::evaluate(const Context *ctx, const ModuleInstanti { Vector3d scalevec(1,1,1); Value v = c.lookup_variable("v"); - v.getnum(scalevec[0]); - v.getnum(scalevec[1]); - v.getnum(scalevec[2]); - v.getv3(scalevec[0], scalevec[1], scalevec[2]); - if (scalevec[2] == 0) scalevec[2] = 1; + if (!v.getv3(scalevec[0], scalevec[1], scalevec[2], 1.0)) { + double num; + if (v.getnum(num)) scalevec.setConstant(num); + } node->matrix.scale(scalevec); } else if (this->type == ROTATE) diff --git a/src/value.cc b/src/value.cc index c9dbd55..93c4d5e 100644 --- a/src/value.cc +++ b/src/value.cc @@ -359,11 +359,11 @@ bool Value::getv2(double &x, double &y) const return true; } -bool Value::getv3(double &x, double &y, double &z) const +bool Value::getv3(double &x, double &y, double &z, double defaultval) const { if (this->type == VECTOR && this->vec.size() == 2) { if (getv2(x, y)) { - z = 0; + z = defaultval; return true; } return false; diff --git a/src/value.h b/src/value.h index a2cfbdf..4a67fbc 100644 --- a/src/value.h +++ b/src/value.h @@ -73,7 +73,7 @@ public: bool getnum(double &v) const; bool getv2(double &x, double &y) const; - bool getv3(double &x, double &y, double &z) const; + bool getv3(double &x, double &y, double &z, double defaultval = 0.0) const; std::string toString() const; |