mirror of
https://github.com/xibyte/jsketcher
synced 2026-02-14 11:30:19 +01:00
binary stl reader
This commit is contained in:
parent
b1e46be9eb
commit
2ca29145d4
1 changed files with 38 additions and 0 deletions
38
web/app/3d/stl/stl-binary-reader.js
Normal file
38
web/app/3d/stl/stl-binary-reader.js
Normal file
|
|
@ -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;
|
||||
}
|
||||
Loading…
Reference in a new issue