kovacsv / JSModeler

A JavaScript framework to create and visualize 3D models.
MIT License
647 stars 124 forks source link

ExportBodyToStl produces non-manifold stl files due to roundoff issues on the vertex #49

Open R7President opened 3 years ago

R7President commented 3 years ago

all of these vertexes I think are supposed to be the same, but because of roundoff errors in the 11+th decimal point they end up looking like different vertex, that creates tiny holes in the mesh: vertex 0.5 -0.5000000000000001 -0.20710678118654358 vertex 0.5 0.2071067811865434 -0.5 vertex 0.5 0.2071067811865434 -0.5 vertex 0.5 0.20710678118654346 -0.5 vertex 0.5 0.2071067811865435 -0.5 vertex 0.5 0.20710678118655118 0.5 vertex 0.5 0.20710678118655124 0.5 vertex 0.5 0.20710678118655135 0.5 vertex 0.5 0.20710678118655135 0.5 vertex 0.5 0.49999999999999983 -0.2071067811865514 vertex 0.5 0.49999999999999983 -0.2071067811865514 vertex 0.5 0.5 -0.20710678118655118 vertex 0.5 0.5 -0.20710678118655118 recommended solution in exporter.js: function fToStr(f) { return (+f).toExponential(11).replace(/0+e/,'e'); } function AddTriangleToContent (normal, vertex1, vertex2, vertex3) { AddLineToContent ('\tfacet normal ' + fToStr(normal.x) + ' ' + fToStr(normal.y) + ' ' + fToStr(normal.z)); AddLineToContent ('\t\touter loop'); AddLineToContent ('\t\t\tvertex ' + fToStr(vertex1.x) + ' ' + fToStr(vertex1.y) + ' ' + fToStr(vertex1.z)); AddLineToContent ('\t\t\tvertex ' + fToStr(vertex2.x) + ' ' + fToStr(vertex2.y) + ' ' + fToStr(vertex2.z)); AddLineToContent ('\t\t\tvertex ' + fToStr(vertex3.x) + ' ' + fToStr(vertex3.y) + ' ' + fToStr(vertex3.z)); AddLineToContent ('\t\tendloop'); AddLineToContent ('\tendfacet'); }

unfortunately this prints "0.5" as "5.e-1" but this is necessary to get the maximum resolution. I did find that toExponential(12) does occasionally still produce an error. The replace portion is just to remove trailing zeros thus shortening the output.

kovacsv commented 3 years ago

Thank you for the report. If the proposed solution fixes the issue, please open a pull request with that!