@jscad/stl-serializer 2.1.20 → 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 -218
- package/LICENSE +1 -1
- package/README.md +3 -1
- package/dist/jscad-stl-serializer.es.js +13 -0
- package/dist/jscad-stl-serializer.min.js +14 -0
- package/package.json +21 -12
- package/rollup.config.js +25 -0
- package/{index.js → src/index.js} +10 -27
- package/{CSGToStlb.js → src/serializeBinary.js} +5 -9
- package/{CSGToStla.js → src/serializeText.js} +4 -8
- package/tests/ascii.test.js +7 -7
- package/tests/binary.test.js +5 -5
- package/tests/progress.test.js +5 -5
package/rollup.config.js
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import banner from 'rollup-plugin-banner'
|
|
2
|
+
import { nodeResolve } from '@rollup/plugin-node-resolve'
|
|
3
|
+
import terser from '@rollup/plugin-terser'
|
|
4
|
+
|
|
5
|
+
export default {
|
|
6
|
+
input: 'src/index.js',
|
|
7
|
+
|
|
8
|
+
output: [
|
|
9
|
+
{
|
|
10
|
+
file: 'dist/jscad-stl-serializer.min.js',
|
|
11
|
+
format: 'umd',
|
|
12
|
+
name: 'jscadStlSerializer'
|
|
13
|
+
},
|
|
14
|
+
{
|
|
15
|
+
file: 'dist/jscad-stl-serializer.es.js',
|
|
16
|
+
format: 'es'
|
|
17
|
+
}
|
|
18
|
+
],
|
|
19
|
+
|
|
20
|
+
plugins: [
|
|
21
|
+
nodeResolve(),
|
|
22
|
+
banner('<%= pkg.description %>\n@module <%= pkg.name %>\n@version <%= pkg.version %>\n@license <%= pkg.license %>'),
|
|
23
|
+
terser({ compress: { module: true }, mangle: false, format: { comments: 'some' } })
|
|
24
|
+
]
|
|
25
|
+
}
|
|
@@ -1,21 +1,3 @@
|
|
|
1
|
-
/*
|
|
2
|
-
JSCAD Geometry to STL Format Serialization
|
|
3
|
-
|
|
4
|
-
## License
|
|
5
|
-
|
|
6
|
-
Copyright (c) 2018-2019 JSCAD Organization https://github.com/jscad
|
|
7
|
-
|
|
8
|
-
All code released under MIT license
|
|
9
|
-
|
|
10
|
-
Notes:
|
|
11
|
-
1) geom2 conversion to:
|
|
12
|
-
none
|
|
13
|
-
2) geom3 conversion to:
|
|
14
|
-
STL mesh
|
|
15
|
-
3) path2 conversion to:
|
|
16
|
-
none
|
|
17
|
-
*/
|
|
18
|
-
|
|
19
1
|
/**
|
|
20
2
|
* Serializer of JSCAD geometries to STL mesh.
|
|
21
3
|
* @module io/stl-serializer
|
|
@@ -23,17 +5,18 @@ Notes:
|
|
|
23
5
|
* const { serializer, mimeType } = require('@jscad/stl-serializer')
|
|
24
6
|
*/
|
|
25
7
|
|
|
26
|
-
|
|
8
|
+
import { generalize, geom3 } from '@jscad/modeling'
|
|
27
9
|
|
|
28
|
-
|
|
10
|
+
import { flatten, toArray } from '@jscad/array-utils'
|
|
29
11
|
|
|
30
|
-
|
|
31
|
-
|
|
12
|
+
import { serializeBinary } from './serializeBinary.js'
|
|
13
|
+
import { serializeText } from './serializeText.js'
|
|
32
14
|
|
|
33
|
-
const mimeType = '
|
|
15
|
+
const mimeType = 'model/stl'
|
|
34
16
|
|
|
35
17
|
/**
|
|
36
18
|
* Serialize the give objects to STL mesh.
|
|
19
|
+
* @see http://en.wikipedia.org/wiki/STL_%28file_format%29#Binary_STL
|
|
37
20
|
* @param {Object} options - options for serialization
|
|
38
21
|
* @param {String} [options.binary='true'] - target format for data
|
|
39
22
|
* @param {Function} [options.statusCallback] - call back function for progress ({ progress: 0-100 })
|
|
@@ -41,7 +24,7 @@ const mimeType = 'application/sla'
|
|
|
41
24
|
* @returns {Array} serialized contents with one STL mesh (either string or binary data)
|
|
42
25
|
* @alias module:io/stl-serializer.serialize
|
|
43
26
|
* @example
|
|
44
|
-
* const geometry =
|
|
27
|
+
* const geometry = cube()
|
|
45
28
|
* const stlData = serializer({binary: false}, geometry)
|
|
46
29
|
*/
|
|
47
30
|
const serialize = (options, ...objects) => {
|
|
@@ -54,18 +37,18 @@ const serialize = (options, ...objects) => {
|
|
|
54
37
|
objects = flatten(objects)
|
|
55
38
|
|
|
56
39
|
// convert only 3D geometries
|
|
57
|
-
let objects3d = objects.filter((object) =>
|
|
40
|
+
let objects3d = objects.filter((object) => geom3.isA(object))
|
|
58
41
|
|
|
59
42
|
if (objects3d.length === 0) throw new Error('only 3D geometries can be serialized to STL')
|
|
60
43
|
if (objects.length !== objects3d.length) console.warn('some objects could not be serialized to STL')
|
|
61
44
|
|
|
62
45
|
// convert to triangles
|
|
63
|
-
objects3d = toArray(
|
|
46
|
+
objects3d = toArray(generalize({ snap: true, triangulate: true }, objects3d))
|
|
64
47
|
|
|
65
48
|
return options.binary ? serializeBinary(objects3d, options) : serializeText(objects3d, options)
|
|
66
49
|
}
|
|
67
50
|
|
|
68
|
-
|
|
51
|
+
export {
|
|
69
52
|
mimeType,
|
|
70
53
|
serialize
|
|
71
54
|
}
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
|
|
1
|
+
import { geom3, poly3 } from '@jscad/modeling'
|
|
2
2
|
|
|
3
3
|
// see http://en.wikipedia.org/wiki/STL_%28file_format%29#Binary_STL
|
|
4
4
|
|
|
5
5
|
// objects must be an array of 3D geometries
|
|
6
|
-
const serializeBinary = (objects, options) => {
|
|
6
|
+
export const serializeBinary = (objects, options) => {
|
|
7
7
|
options.statusCallback && options.statusCallback({ progress: 0 })
|
|
8
8
|
|
|
9
9
|
// first check if the host is little-endian:
|
|
@@ -18,7 +18,7 @@ const serializeBinary = (objects, options) => {
|
|
|
18
18
|
let numtriangles = 0
|
|
19
19
|
let numpolygons = 0
|
|
20
20
|
objects.forEach((object, i) => {
|
|
21
|
-
const polygons =
|
|
21
|
+
const polygons = geom3.toPolygons(object)
|
|
22
22
|
polygons.forEach((polygon) => {
|
|
23
23
|
const numvertices = polygon.vertices.length
|
|
24
24
|
const thisnumtriangles = (numvertices >= 3) ? numvertices - 2 : 0
|
|
@@ -53,11 +53,11 @@ const serializeBinary = (objects, options) => {
|
|
|
53
53
|
let byteoffset = 0
|
|
54
54
|
|
|
55
55
|
objects.forEach((object) => {
|
|
56
|
-
const polygons =
|
|
56
|
+
const polygons = geom3.toPolygons(object)
|
|
57
57
|
polygons.forEach((polygon, index) => {
|
|
58
58
|
const vertices = polygon.vertices
|
|
59
59
|
const numvertices = vertices.length
|
|
60
|
-
const plane =
|
|
60
|
+
const plane = poly3.plane(polygon)
|
|
61
61
|
for (let i = 0; i < numvertices - 2; i++) {
|
|
62
62
|
triangleFloat32array[0] = plane[0]
|
|
63
63
|
triangleFloat32array[1] = plane[1]
|
|
@@ -82,7 +82,3 @@ const serializeBinary = (objects, options) => {
|
|
|
82
82
|
options.statusCallback && options.statusCallback({ progress: 100 })
|
|
83
83
|
return [headerarray.buffer, ar1.buffer, allTrianglesBuffer] // 'blobable array'
|
|
84
84
|
}
|
|
85
|
-
|
|
86
|
-
module.exports = {
|
|
87
|
-
serializeBinary
|
|
88
|
-
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
|
|
1
|
+
import { geom3, poly3 } from '@jscad/modeling'
|
|
2
2
|
|
|
3
3
|
// objects must be an array of 3D geomertries (with polygons)
|
|
4
|
-
const serializeText = (objects, options) => {
|
|
4
|
+
export const serializeText = (objects, options) => {
|
|
5
5
|
options.statusCallback && options.statusCallback({ progress: 0 })
|
|
6
6
|
|
|
7
7
|
const result = `solid JSCAD
|
|
@@ -23,7 +23,7 @@ const convertToStl = (objects, options) => {
|
|
|
23
23
|
|
|
24
24
|
const convertToFacets = (object, options) => {
|
|
25
25
|
const result = []
|
|
26
|
-
const polygons =
|
|
26
|
+
const polygons = geom3.toPolygons(object)
|
|
27
27
|
polygons.forEach((polygon, i) => {
|
|
28
28
|
result.push(convertToFacet(polygon))
|
|
29
29
|
})
|
|
@@ -40,7 +40,7 @@ const convertToFacet = (polygon) => {
|
|
|
40
40
|
// STL requires triangular polygons. If our polygon has more vertices, create multiple triangles:
|
|
41
41
|
const firstVertexStl = vertextoStlString(polygon.vertices[0])
|
|
42
42
|
for (let i = 0; i < polygon.vertices.length - 2; i++) {
|
|
43
|
-
const facet = `facet normal ${vector3DtoStlString(
|
|
43
|
+
const facet = `facet normal ${vector3DtoStlString(poly3.plane(polygon))}
|
|
44
44
|
outer loop
|
|
45
45
|
${firstVertexStl}
|
|
46
46
|
${vertextoStlString(polygon.vertices[i + 1])}
|
|
@@ -52,7 +52,3 @@ endfacet`
|
|
|
52
52
|
}
|
|
53
53
|
return result.join('\n')
|
|
54
54
|
}
|
|
55
|
-
|
|
56
|
-
module.exports = {
|
|
57
|
-
serializeText
|
|
58
|
-
}
|
package/tests/ascii.test.js
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
|
-
|
|
1
|
+
import test from 'ava'
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
import { cube, translate } from '@jscad/modeling'
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
import { serialize } from '../src/index.js'
|
|
6
6
|
|
|
7
7
|
test('serialize objects to stl (ascii)', (t) => {
|
|
8
|
-
const object1 =
|
|
9
|
-
const observed1 =
|
|
8
|
+
const object1 = cube({ size: 10 }) // .setColor([0, 0, 1, 1])
|
|
9
|
+
const observed1 = serialize({ binary: false }, object1)
|
|
10
10
|
t.deepEqual(observed1, [expected1])
|
|
11
11
|
|
|
12
|
-
const object2 =
|
|
13
|
-
const observed2 =
|
|
12
|
+
const object2 = translate([5, 5, 5], object1) // .setColor([1, 0, 0, 1])
|
|
13
|
+
const observed2 = serialize({ binary: false }, object1, object2)
|
|
14
14
|
t.deepEqual(observed2, [expected2])
|
|
15
15
|
})
|
|
16
16
|
|
package/tests/binary.test.js
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
|
|
1
|
+
import test from 'ava'
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
import { cube } from '@jscad/modeling'
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
import { serialize } from '../src/index.js'
|
|
6
6
|
|
|
7
7
|
test('serialize objects to stl (binary)', (t) => {
|
|
8
|
-
const object1 =
|
|
9
|
-
const observed =
|
|
8
|
+
const object1 = cube()
|
|
9
|
+
const observed = serialize({ binary: true }, object1)
|
|
10
10
|
|
|
11
11
|
// TODO: VERY shallow testing ... improve
|
|
12
12
|
t.deepEqual(observed[0].byteLength, 80)
|
package/tests/progress.test.js
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
|
-
|
|
1
|
+
import test from 'ava'
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
import { cube } from '@jscad/modeling'
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
import { serialize } from '../src/index.js'
|
|
6
6
|
|
|
7
7
|
test('progress status callback', (t) => {
|
|
8
|
-
const input =
|
|
8
|
+
const input = cube()
|
|
9
9
|
const progresses = []
|
|
10
10
|
const statusCallback = (statusObj) => {
|
|
11
11
|
progresses.push(statusObj.progress)
|
|
12
12
|
}
|
|
13
|
-
const observed =
|
|
13
|
+
const observed = serialize({ statusCallback: statusCallback }, input)
|
|
14
14
|
|
|
15
15
|
t.is(observed.length, 3)
|
|
16
16
|
t.deepEqual(0, progresses[0])
|