@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.
@@ -1,8 +1,8 @@
1
- const { maths, primitives } = require('@jscad/modeling')
1
+ import { polyhedron, vec3 } from '@jscad/modeling'
2
2
 
3
- const { BinaryReader } = require('@jscad/io-utils')
3
+ import { BinaryReader } from '@jscad/io-utils'
4
4
 
5
- const packageVersion = require('./package.json').version
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: packageVersion,
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 += 'const {primitives} = require(\'@jscad/modeling\')\n'
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 = maths.vec3.subtract(maths.vec3.create(), v2, v1)
252
- const e2 = maths.vec3.subtract(maths.vec3.create(), v3, v1)
253
- const cr = maths.vec3.cross(maths.vec3.create(), e1, e2)
254
- const t = maths.vec3.dot(no, cr)
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 = maths.vec3.subtract(maths.vec3.create(), v2, v1)
332
- const e2 = maths.vec3.subtract(maths.vec3.create(), v3, v1)
333
- const cr = maths.vec3.cross(maths.vec3.create(), e1, e2)
334
- const t = maths.vec3.dot(no, cr)
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 primitives.polyhedron(options)
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 primitives.polyhedron({points, faces, colors, orientation: \'inside\'})\n}\n'
407
+ src += ' return polyhedron({points, faces, colors, orientation: \'inside\'})\n}\n'
410
408
  return src
411
409
  }
412
410
 
413
- const extension = 'stl'
411
+ const mimeType = 'model/stl'
414
412
 
415
- module.exports = {
416
- deserialize,
417
- extension
413
+ export {
414
+ mimeType,
415
+ deserialize
418
416
  }
@@ -1,12 +1,13 @@
1
- const fs = require('fs')
2
- const path = require('path')
3
- const test = require('ava')
1
+ import fs from 'fs'
2
+ import path from 'path'
4
3
 
5
- const { geometries } = require('@jscad/modeling')
4
+ import test from 'ava'
6
5
 
7
- const deserializer = require('../index.js')
6
+ import { geom3 } from '@jscad/modeling'
8
7
 
9
- const samplesPath = path.dirname(require.resolve('@jscad/sample-files/package.json'))
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 = deserializer.deserialize({ output: 'geometry', addMetaData: false }, inputFile)
18
+ const observed = deserialize({ output: 'geometry', addMetaData: false }, inputFile)
18
19
  t.is(observed.length, 1)
19
- const polygons = geometries.geom3.toPolygons(observed[0])
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 = deserializer.deserialize({ output: 'geometry', addMetaData: false }, inputFile)
45
+ const observed = deserialize({ output: 'geometry', addMetaData: false }, inputFile)
45
46
  t.is(observed.length, 1)
46
- const polygons = geometries.geom3.toPolygons(observed[0])
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 = deserializer.deserialize({ output: 'geometry', addMetaData: false }, inputFile)
72
+ const observed = deserialize({ output: 'geometry', addMetaData: false }, inputFile)
72
73
  t.is(observed.length, 1)
73
- const polygons = geometries.geom3.toPolygons(observed[0])
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 = deserializer.deserialize({ output: 'geometry', addMetaData: false }, inputFile)
82
+ const observed = deserialize({ output: 'geometry', addMetaData: false }, inputFile)
82
83
  t.is(observed.length, 1)
83
- const polygons = geometries.geom3.toPolygons(observed[0])
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 = deserializer.deserialize({ output: 'geometry', addMetaData: false }, inputFile)
92
+ const observed = deserialize({ output: 'geometry', addMetaData: false }, inputFile)
92
93
  t.is(observed.length, 1)
93
- const polygons = geometries.geom3.toPolygons(observed[0])
94
+ const polygons = geom3.toPolygons(observed[0])
94
95
  t.deepEqual(polygons.length, 12744)
95
96
  })
@@ -1,10 +1,11 @@
1
- const fs = require('fs')
2
- const path = require('path')
3
- const test = require('ava')
1
+ import fs from 'fs'
2
+ import path from 'path'
4
3
 
5
- const deserializer = require('../index.js')
4
+ import test from 'ava'
6
5
 
7
- const samplesPath = path.dirname(require.resolve('@jscad/sample-files/package.json'))
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 = `const {primitives} = require('@jscad/modeling')
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 primitives.polyhedron({points, faces, colors, orientation: 'inside'})
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 = deserializer.deserialize({ filename: 'ascii', output: 'script', addMetaData: false }, inputFile)
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 = `const {primitives} = require('@jscad/modeling')
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 primitives.polyhedron({points, faces, colors, orientation: 'inside'})
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 = deserializer.deserialize({ output: 'script', addMetaData: false }, inputFile)
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 = deserializer.deserialize({ output: 'script', addMetaData: false }, inputFile)
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('primitives.polyhedron', observed), 1)
179
+ t.is(countOf('polyhedron', observed), 1)
183
180
  })