@jscad/stl-deserializer 2.1.27 → 3.0.1-alpha.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +10 -228
- package/LICENSE +1 -1
- package/README.md +3 -1
- package/dist/jscad-stl-deserializer.es.js +13 -0
- package/dist/jscad-stl-deserializer.min.js +14 -0
- package/package.json +22 -12
- package/rollup.config.js +27 -0
- package/{index.js → src/index.js} +20 -22
- package/tests/instantiate.test.js +17 -16
- package/tests/translate.test.js +16 -19
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
|
|
1
|
+
import { polyhedron, vec3 } from '@jscad/modeling'
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
import { BinaryReader } from '@jscad/io-utils'
|
|
4
4
|
|
|
5
|
-
const
|
|
5
|
+
const pkgversion = '[VI]{version}[/VI]' // version is injected by rollup
|
|
6
6
|
|
|
7
7
|
// STL function from http://jsfiddle.net/Riham/yzvGD/35/
|
|
8
8
|
// CC BY-SA by Riham
|
|
@@ -33,7 +33,7 @@ const packageVersion = require('./package.json').version
|
|
|
33
33
|
const deserialize = (options, stl) => {
|
|
34
34
|
const defaults = {
|
|
35
35
|
filename: 'stl',
|
|
36
|
-
version:
|
|
36
|
+
version: pkgversion,
|
|
37
37
|
addMetaData: true,
|
|
38
38
|
output: 'script'
|
|
39
39
|
}
|
|
@@ -116,14 +116,12 @@ const formatAsJscad = (data, addMetaData, version, filename) => {
|
|
|
116
116
|
//
|
|
117
117
|
`
|
|
118
118
|
}
|
|
119
|
-
code += '
|
|
119
|
+
code += 'import * from \'@jscad/modeling\'\n'
|
|
120
120
|
code += data.join('\n')
|
|
121
121
|
code += `
|
|
122
|
-
const main = () => {
|
|
122
|
+
export const main = () => {
|
|
123
123
|
return [${data.map((d, i) => `solid${i + 1}()`)}]
|
|
124
124
|
}
|
|
125
|
-
|
|
126
|
-
module.exports = {main}
|
|
127
125
|
`
|
|
128
126
|
return code
|
|
129
127
|
}
|
|
@@ -248,10 +246,10 @@ const deserializeBinarySTL = (stl, filename, version, elementFormatter) => {
|
|
|
248
246
|
// E2 = C - A
|
|
249
247
|
// test = dot( Normal, cross( E1, E2 ) )
|
|
250
248
|
// test > 0: cw, test < 0 : ccw
|
|
251
|
-
const e1 =
|
|
252
|
-
const e2 =
|
|
253
|
-
const cr =
|
|
254
|
-
const t =
|
|
249
|
+
const e1 = vec3.subtract(vec3.create(), v2, v1)
|
|
250
|
+
const e2 = vec3.subtract(vec3.create(), v3, v1)
|
|
251
|
+
const cr = vec3.cross(vec3.create(), e1, e2)
|
|
252
|
+
const t = vec3.dot(no, cr)
|
|
255
253
|
if (t > 0) { // 1,2,3 -> 3,2,1
|
|
256
254
|
const tmp = v3
|
|
257
255
|
v3 = v1
|
|
@@ -328,10 +326,10 @@ const deserializeAsciiSTL = (stl, filename, version, elementFormatter) => {
|
|
|
328
326
|
// E2 = C - A
|
|
329
327
|
// test = dot( Normal, cross( E1, E2 ) )
|
|
330
328
|
// test > 0: cw, test < 0: ccw
|
|
331
|
-
const e1 =
|
|
332
|
-
const e2 =
|
|
333
|
-
const cr =
|
|
334
|
-
const t =
|
|
329
|
+
const e1 = vec3.subtract(vec3.create(), v2, v1)
|
|
330
|
+
const e2 = vec3.subtract(vec3.create(), v3, v1)
|
|
331
|
+
const cr = vec3.cross(vec3.create(), e1, e2)
|
|
332
|
+
const t = vec3.dot(no, cr)
|
|
335
333
|
if (t > 0) { // 1,2,3 -> 3,2,1
|
|
336
334
|
const tmp = v3
|
|
337
335
|
v3 = v1
|
|
@@ -369,7 +367,7 @@ const toPolyhedron = (points, faces, normals, colors) => {
|
|
|
369
367
|
faces,
|
|
370
368
|
colors
|
|
371
369
|
}
|
|
372
|
-
return
|
|
370
|
+
return polyhedron(options)
|
|
373
371
|
}
|
|
374
372
|
|
|
375
373
|
/*
|
|
@@ -406,13 +404,13 @@ const solid${index} = () => {
|
|
|
406
404
|
} else {
|
|
407
405
|
src += ' const colors = null\n'
|
|
408
406
|
}
|
|
409
|
-
src += ' return
|
|
407
|
+
src += ' return polyhedron({points, faces, colors, orientation: \'inside\'})\n}\n'
|
|
410
408
|
return src
|
|
411
409
|
}
|
|
412
410
|
|
|
413
|
-
const
|
|
411
|
+
const mimeType = 'model/stl'
|
|
414
412
|
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
413
|
+
export {
|
|
414
|
+
mimeType,
|
|
415
|
+
deserialize
|
|
418
416
|
}
|
|
@@ -1,12 +1,13 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
const test = require('ava')
|
|
1
|
+
import fs from 'fs'
|
|
2
|
+
import path from 'path'
|
|
4
3
|
|
|
5
|
-
|
|
4
|
+
import test from 'ava'
|
|
6
5
|
|
|
7
|
-
|
|
6
|
+
import { geom3 } from '@jscad/modeling'
|
|
8
7
|
|
|
9
|
-
|
|
8
|
+
import { deserialize } from '../src/index.js'
|
|
9
|
+
|
|
10
|
+
const samplesPath = '../../../node_modules/@jscad/sample-files'
|
|
10
11
|
|
|
11
12
|
const toArray = (polygons) => polygons.map((p) => p.vertices.map((v) => ([v[0], v[1], v[2]])))
|
|
12
13
|
|
|
@@ -14,9 +15,9 @@ test('instantiate simple ascii stl to geometry', (t) => {
|
|
|
14
15
|
const inputPath = path.resolve(samplesPath, 'stl/testcube_ascii.stl')
|
|
15
16
|
const inputFile = fs.readFileSync(inputPath, 'utf8')
|
|
16
17
|
|
|
17
|
-
const observed =
|
|
18
|
+
const observed = deserialize({ output: 'geometry', addMetaData: false }, inputFile)
|
|
18
19
|
t.is(observed.length, 1)
|
|
19
|
-
const polygons =
|
|
20
|
+
const polygons = geom3.toPolygons(observed[0])
|
|
20
21
|
t.deepEqual(polygons.length, 12) // 6 faces, 12 polygons
|
|
21
22
|
|
|
22
23
|
const observedPolygons = toArray(polygons)
|
|
@@ -41,9 +42,9 @@ test('instantiate simple binary stl to geometry', (t) => {
|
|
|
41
42
|
const inputPath = path.resolve(samplesPath, 'stl/testcube_10mm.stl')
|
|
42
43
|
const inputFile = fs.readFileSync(inputPath)
|
|
43
44
|
|
|
44
|
-
const observed =
|
|
45
|
+
const observed = deserialize({ output: 'geometry', addMetaData: false }, inputFile)
|
|
45
46
|
t.is(observed.length, 1)
|
|
46
|
-
const polygons =
|
|
47
|
+
const polygons = geom3.toPolygons(observed[0])
|
|
47
48
|
t.deepEqual(polygons.length, 12) // 6 faces, 12 polygons
|
|
48
49
|
|
|
49
50
|
const observedPolygons = toArray(polygons)
|
|
@@ -68,9 +69,9 @@ test('instantiate medium complexity binary stl to geometry', (t) => {
|
|
|
68
69
|
const inputPath = path.resolve(samplesPath, 'stl/pr2_head_tilt.stl')
|
|
69
70
|
const inputFile = fs.readFileSync(inputPath)
|
|
70
71
|
|
|
71
|
-
const observed =
|
|
72
|
+
const observed = deserialize({ output: 'geometry', addMetaData: false }, inputFile)
|
|
72
73
|
t.is(observed.length, 1)
|
|
73
|
-
const polygons =
|
|
74
|
+
const polygons = geom3.toPolygons(observed[0])
|
|
74
75
|
t.deepEqual(polygons.length, 1052)
|
|
75
76
|
})
|
|
76
77
|
|
|
@@ -78,9 +79,9 @@ test('instantiate complex ascii stl to geometry', (t) => {
|
|
|
78
79
|
const inputPath = path.resolve(samplesPath, 'stl/herringbone-gear-large.stl')
|
|
79
80
|
const inputFile = fs.readFileSync(inputPath, 'utf8')
|
|
80
81
|
|
|
81
|
-
const observed =
|
|
82
|
+
const observed = deserialize({ output: 'geometry', addMetaData: false }, inputFile)
|
|
82
83
|
t.is(observed.length, 1)
|
|
83
|
-
const polygons =
|
|
84
|
+
const polygons = geom3.toPolygons(observed[0])
|
|
84
85
|
t.deepEqual(polygons.length, 17742)
|
|
85
86
|
})
|
|
86
87
|
|
|
@@ -88,8 +89,8 @@ test('instantiate complex binary stl to geometry', (t) => {
|
|
|
88
89
|
const inputPath = path.resolve(samplesPath, 'stl/UM2CableChain_BedEnd.STL')
|
|
89
90
|
const inputFile = fs.readFileSync(inputPath)
|
|
90
91
|
|
|
91
|
-
const observed =
|
|
92
|
+
const observed = deserialize({ output: 'geometry', addMetaData: false }, inputFile)
|
|
92
93
|
t.is(observed.length, 1)
|
|
93
|
-
const polygons =
|
|
94
|
+
const polygons = geom3.toPolygons(observed[0])
|
|
94
95
|
t.deepEqual(polygons.length, 12744)
|
|
95
96
|
})
|
package/tests/translate.test.js
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
const test = require('ava')
|
|
1
|
+
import fs from 'fs'
|
|
2
|
+
import path from 'path'
|
|
4
3
|
|
|
5
|
-
|
|
4
|
+
import test from 'ava'
|
|
6
5
|
|
|
7
|
-
|
|
6
|
+
import { deserialize } from '../src/index.js'
|
|
7
|
+
|
|
8
|
+
const samplesPath = '../../../node_modules/@jscad/sample-files'
|
|
8
9
|
|
|
9
10
|
const countOf = (search, string) => {
|
|
10
11
|
let count = 0
|
|
@@ -20,7 +21,7 @@ test('translate simple ascii stl to jscad code', (t) => {
|
|
|
20
21
|
const inputPath = path.resolve(samplesPath, 'stl/testcube_ascii.stl')
|
|
21
22
|
const inputFile = fs.readFileSync(inputPath, 'utf8')
|
|
22
23
|
|
|
23
|
-
const expected = `
|
|
24
|
+
const expected = `import * from '@jscad/modeling'
|
|
24
25
|
|
|
25
26
|
//
|
|
26
27
|
// solid 1 : 36 points, 12 faces, 0 colors
|
|
@@ -79,17 +80,15 @@ const solid1 = () => {
|
|
|
79
80
|
[33,34,35],
|
|
80
81
|
]
|
|
81
82
|
const colors = null
|
|
82
|
-
return
|
|
83
|
+
return polyhedron({points, faces, colors, orientation: 'inside'})
|
|
83
84
|
}
|
|
84
85
|
|
|
85
|
-
const main = () => {
|
|
86
|
+
export const main = () => {
|
|
86
87
|
return [solid1()]
|
|
87
88
|
}
|
|
88
|
-
|
|
89
|
-
module.exports = {main}
|
|
90
89
|
`
|
|
91
90
|
|
|
92
|
-
const observed =
|
|
91
|
+
const observed = deserialize({ filename: 'ascii', output: 'script', addMetaData: false }, inputFile)
|
|
93
92
|
t.deepEqual(observed, expected)
|
|
94
93
|
})
|
|
95
94
|
|
|
@@ -97,7 +96,7 @@ test('translate simple binary stl to jscad script', (t) => {
|
|
|
97
96
|
const inputPath = path.resolve(samplesPath, 'stl/testcube_10mm.stl')
|
|
98
97
|
const inputFile = fs.readFileSync(inputPath)
|
|
99
98
|
|
|
100
|
-
const expected = `
|
|
99
|
+
const expected = `import * from '@jscad/modeling'
|
|
101
100
|
|
|
102
101
|
//
|
|
103
102
|
// solid 1 : 36 points, 12 faces, 0 colors
|
|
@@ -156,17 +155,15 @@ const solid1 = () => {
|
|
|
156
155
|
[33,34,35],
|
|
157
156
|
]
|
|
158
157
|
const colors = null
|
|
159
|
-
return
|
|
158
|
+
return polyhedron({points, faces, colors, orientation: 'inside'})
|
|
160
159
|
}
|
|
161
160
|
|
|
162
|
-
const main = () => {
|
|
161
|
+
export const main = () => {
|
|
163
162
|
return [solid1()]
|
|
164
163
|
}
|
|
165
|
-
|
|
166
|
-
module.exports = {main}
|
|
167
164
|
`
|
|
168
165
|
|
|
169
|
-
const observed =
|
|
166
|
+
const observed = deserialize({ output: 'script', addMetaData: false }, inputFile)
|
|
170
167
|
t.deepEqual(observed, expected)
|
|
171
168
|
})
|
|
172
169
|
|
|
@@ -174,10 +171,10 @@ test('translate stl with colors to jscad script', (t) => {
|
|
|
174
171
|
const inputPath = path.resolve(samplesPath, 'stl/colors.stl')
|
|
175
172
|
const inputFile = fs.readFileSync(inputPath)
|
|
176
173
|
|
|
177
|
-
const observed =
|
|
174
|
+
const observed = deserialize({ output: 'script', addMetaData: false }, inputFile)
|
|
178
175
|
t.is(countOf('points', observed), 3) // comment, definition, useage
|
|
179
176
|
t.is(countOf('faces', observed), 3)
|
|
180
177
|
t.is(countOf('colors', observed), 3)
|
|
181
178
|
t.is(countOf('colors = [', observed), 1)
|
|
182
|
-
t.is(countOf('
|
|
179
|
+
t.is(countOf('polyhedron', observed), 1)
|
|
183
180
|
})
|