From 2ca29145d446287286f3e4f16b14e9e19e374ec5 Mon Sep 17 00:00:00 2001 From: Val Erastov Date: Wed, 23 Nov 2016 19:24:44 -0800 Subject: [PATCH] binary stl reader --- web/app/3d/stl/stl-binary-reader.js | 38 +++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 web/app/3d/stl/stl-binary-reader.js diff --git a/web/app/3d/stl/stl-binary-reader.js b/web/app/3d/stl/stl-binary-reader.js new file mode 100644 index 00000000..c7546c7d --- /dev/null +++ b/web/app/3d/stl/stl-binary-reader.js @@ -0,0 +1,38 @@ +import {StlSolid, StlFace} from './stl-data-structure' + +function readVector(dataView, off) { + return [ + dataView.getFloat32(off + 0, true), + dataView.getFloat32(off + 4, true), + dataView.getFloat32(off + 8, true) + ]; +} + +export function parse(dataView) { + const solid = new StlSolid('binary'); + let off = 80; // skip header + + let triangleCount = dataView.getUint32(off, true); + off += 4; + + let cells = []; + let positions = []; + let faceNormals = []; + + for (let i = 0; i < triangleCount; i++) { + let cell = []; + let normal = readVector(dataView, off); + off += 12; // 3 floats + + let face = new StlFace(normal); + + for (let j = 0; j < 3; j++) { + let position = readVector(dataView, off); + off += 12; + face.vertices.push(position); + } + solid.faces.push(face); + off += 2; // skip attribute byte count + } + return solid; +} \ No newline at end of file