diff options
Diffstat (limited to 'src/export.cc')
-rw-r--r-- | src/export.cc | 39 |
1 files changed, 15 insertions, 24 deletions
diff --git a/src/export.cc b/src/export.cc index 5ce2d15..8abd5fa 100644 --- a/src/export.cc +++ b/src/export.cc @@ -84,31 +84,22 @@ void export_stl(CGAL_Nef_polyhedron *root_N, std::ostream &output, QProgressDial stream << x3 << " " << y3 << " " << z3; std::string vs3 = stream.str(); if (vs1 != vs2 && vs1 != vs3 && vs2 != vs3) { - // The above condition ensures that vs1-vs2, vs1-vs3, and their cross - // product are non-zero. Floating point arithmetic may however truncate - // small values to 0. This can be avoided by first scaling the components - // of vs1-vs2 and vs1-vs3. This has no effect on the resulting unit - // normal vector. - double dn[6] = { x1-x2, y1-y2, z1-z2, x1-x3, y1-y3, z1-z3 }; - double maxdn = 0; - int i; - for (i = 0; i < 6; ++i) { - double dx = dn[i]; - if (dx < 0) dx = -dx; - if (dx > maxdn) maxdn = dx; + // The above condition ensures that there are 3 distinct vertices, but + // they may be collinear. If they are, the unit normal is meaningless + // so the default value of "1 0 0" can be used. If the vertices are not + // collinear then the unit normal must be calculated from the + // components. + if (!CGAL::collinear(v1.point(),v2.point(),v3.point())) { + CGAL_Polyhedron::Traits::Vector_3 normal = CGAL::normal(v1.point(),v2.point(),v3.point()); + output << " facet normal " + << CGAL::sign(normal.x()) * sqrt(CGAL::to_double(normal.x()*normal.x()/normal.squared_length())) + << " " + << CGAL::sign(normal.y()) * sqrt(CGAL::to_double(normal.y()*normal.y()/normal.squared_length())) + << " " + << CGAL::sign(normal.z()) * sqrt(CGAL::to_double(normal.z()*normal.z()/normal.squared_length())) + << "\n"; } - for (i = 0; i < 6; ++i) dn[i] /= maxdn; - double nx = dn[1]*dn[5] - dn[2]*dn[4]; - double ny = dn[2]*dn[3] - dn[0]*dn[5]; - double nz = dn[0]*dn[4] - dn[1]*dn[3]; - double nlength = sqrt(nx*nx + ny*ny + nz*nz); - // Avoid generating normals for polygons with zero area - double eps = 0.000001; - if (nlength < eps) nlength = 1.0; - output << " facet normal " - << nx / nlength << " " - << ny / nlength << " " - << nz / nlength << "\n"; + else output << " facet normal 1 0 0\n"; output << " outer loop\n"; output << " vertex " << vs1 << "\n"; output << " vertex " << vs2 << "\n"; |