This repository has been archived by the owner on Jul 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathCSGToStlb.js
70 lines (67 loc) · 2.78 KB
/
CSGToStlb.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// see http://en.wikipedia.org/wiki/STL_%28file_format%29#Binary_STL
function serialize (CSG, options) {
options && options.statusCallback && options.statusCallback({progress: 0})
// first check if the host is little-endian:
var buffer = new ArrayBuffer(4)
var int32buffer = new Int32Array(buffer, 0, 1)
var int8buffer = new Int8Array(buffer, 0, 4)
int32buffer[0] = 0x11223344
if (int8buffer[0] !== 0x44) {
throw new Error('Binary STL output is currently only supported on little-endian (Intel) processors')
}
var numtriangles = 0
CSG.polygons.map(function (p) {
var numvertices = p.vertices.length
var thisnumtriangles = (numvertices >= 3) ? numvertices - 2 : 0
numtriangles += thisnumtriangles
})
var headerarray = new Uint8Array(80)
for (var i = 0; i < 80; i++) {
headerarray[i] = 65
}
var ar1 = new Uint32Array(1)
ar1[0] = numtriangles
// write the triangles to allTrianglesBuffer:
var allTrianglesBuffer = new ArrayBuffer(50 * numtriangles)
var allTrianglesBufferAsInt8 = new Int8Array(allTrianglesBuffer)
// a tricky problem is that a Float32Array must be aligned at 4-byte boundaries (at least in certain browsers)
// while each triangle takes 50 bytes. Therefore we write each triangle to a temporary buffer, and copy that
// into allTrianglesBuffer:
var triangleBuffer = new ArrayBuffer(50)
var triangleBufferAsInt8 = new Int8Array(triangleBuffer)
// each triangle consists of 12 floats:
var triangleFloat32array = new Float32Array(triangleBuffer, 0, 12)
// and one uint16:
var triangleUint16array = new Uint16Array(triangleBuffer, 48, 1)
var byteoffset = 0
CSG.polygons.map(function (p, i) {
var numvertices = p.vertices.length
for (var i = 0; i < numvertices - 2; i++) {
var normal = p.plane.normal
triangleFloat32array[0] = normal._x
triangleFloat32array[1] = normal._y
triangleFloat32array[2] = normal._z
var arindex = 3
for (var v = 0; v < 3; v++) {
var vv = v + ((v > 0) ? i : 0)
var vertexpos = p.vertices[vv].pos
triangleFloat32array[arindex++] = vertexpos._x
triangleFloat32array[arindex++] = vertexpos._y
triangleFloat32array[arindex++] = vertexpos._z
}
triangleUint16array[0] = 0
// copy the triangle into allTrianglesBuffer:
allTrianglesBufferAsInt8.set(triangleBufferAsInt8, byteoffset)
byteoffset += 50
}
options && options.statusCallback && options.statusCallback({progress: 100 * i / CSG.polygons.length})
})
options && options.statusCallback && options.statusCallback({progress: 100})
return [headerarray.buffer, ar1.buffer, allTrianglesBuffer]// 'blobable array'
/* return new Blob([headerarray.buffer, ar1.buffer, allTrianglesBuffer], {
type: mimeType
}) */
}
module.exports = {
serialize
}