@jscad/modeling 3.0.2-alpha.0 → 3.0.3-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 +8 -0
- package/dist/jscad-modeling.es.js +2 -2
- package/dist/jscad-modeling.min.js +2 -2
- package/package.json +2 -2
- package/src/colors/colorize.test.js +1 -1
- package/src/geometries/geom2/index.d.ts +0 -2
- package/src/geometries/geom2/index.js +0 -2
- package/src/geometries/geom3/applyTransforms.test.js +2 -2
- package/src/geometries/geom3/clone.test.js +2 -2
- package/src/geometries/geom3/create.js +1 -9
- package/src/geometries/geom3/{fromPoints.d.ts → fromVertices.d.ts} +1 -1
- package/src/geometries/geom3/{fromPoints.js → fromVertices.js} +3 -2
- package/src/geometries/geom3/{fromPoints.test.js → fromVertices.test.js} +6 -6
- package/src/geometries/geom3/{fromPointsConvex.d.ts → fromVerticesConvex.d.ts} +1 -1
- package/src/geometries/geom3/fromVerticesConvex.js +25 -0
- package/src/geometries/geom3/{fromPointsConvex.test.js → fromVerticesConvex.test.js} +3 -3
- package/src/geometries/geom3/index.d.ts +3 -5
- package/src/geometries/geom3/index.js +4 -6
- package/src/geometries/geom3/invert.test.js +2 -2
- package/src/geometries/geom3/isA.test.js +2 -2
- package/src/geometries/geom3/toString.test.js +2 -2
- package/src/geometries/geom3/{toPoints.d.ts → toVertices.d.ts} +1 -1
- package/src/geometries/geom3/{toPoints.js → toVertices.js} +3 -2
- package/src/geometries/geom3/{toPoints.test.js → toVertices.test.js} +4 -4
- package/src/geometries/geom3/transform.test.js +2 -2
- package/src/geometries/geom3/validate.test.js +4 -4
- package/src/geometries/index.d.ts +1 -0
- package/src/geometries/index.js +1 -0
- package/src/geometries/path2/create.js +1 -10
- package/src/geometries/path2/index.d.ts +0 -2
- package/src/geometries/path2/index.js +0 -2
- package/src/geometries/path3/applyTransforms.js +22 -0
- package/src/geometries/path3/applyTransforms.test.js +28 -0
- package/src/geometries/path3/close.d.ts +3 -0
- package/src/geometries/path3/close.js +31 -0
- package/src/geometries/path3/close.test.js +43 -0
- package/src/geometries/path3/concat.d.ts +3 -0
- package/src/geometries/path3/concat.js +36 -0
- package/src/geometries/path3/concat.test.js +35 -0
- package/src/geometries/path3/create.d.ts +4 -0
- package/src/geometries/path3/create.js +30 -0
- package/src/geometries/path3/create.test.js +8 -0
- package/src/geometries/path3/equals.d.ts +3 -0
- package/src/geometries/path3/equals.js +48 -0
- package/src/geometries/path3/equals.test.js +38 -0
- package/src/geometries/path3/fromVertices.d.ts +8 -0
- package/src/geometries/path3/fromVertices.js +45 -0
- package/src/geometries/path3/fromVertices.test.js +33 -0
- package/src/geometries/path3/index.d.ts +13 -0
- package/src/geometries/path3/index.js +21 -0
- package/src/geometries/path3/isA.d.ts +3 -0
- package/src/geometries/path3/isA.js +20 -0
- package/src/geometries/path3/isA.test.js +19 -0
- package/src/geometries/path3/reverse.d.ts +3 -0
- package/src/geometries/path3/reverse.js +19 -0
- package/src/geometries/path3/reverse.test.js +9 -0
- package/src/geometries/path3/toString.d.ts +3 -0
- package/src/geometries/path3/toString.js +24 -0
- package/src/geometries/path3/toVertices.d.ts +4 -0
- package/src/geometries/path3/toVertices.js +16 -0
- package/src/geometries/path3/toVertices.test.js +13 -0
- package/src/geometries/path3/transform.d.ts +4 -0
- package/src/geometries/path3/transform.js +21 -0
- package/src/geometries/path3/transform.test.js +50 -0
- package/src/geometries/path3/type.d.ts +10 -0
- package/src/geometries/path3/validate.d.ts +1 -0
- package/src/geometries/path3/validate.js +41 -0
- package/src/geometries/poly2/create.js +1 -6
- package/src/geometries/poly3/create.js +1 -6
- package/src/operations/booleans/intersectGeom3.test.js +4 -4
- package/src/operations/booleans/subtractGeom3.test.js +4 -4
- package/src/operations/booleans/unionGeom3.test.js +5 -5
- package/src/operations/extrusions/extrudeFromSlices.test.js +6 -6
- package/src/operations/extrusions/extrudeLinear.test.js +8 -8
- package/src/operations/extrusions/extrudeRotate.test.js +12 -12
- package/src/operations/hulls/hull.test.js +5 -5
- package/src/operations/hulls/hullChain.test.js +5 -5
- package/src/operations/hulls/toUniquePoints.js +2 -2
- package/src/operations/modifiers/generalize.test.js +6 -6
- package/src/operations/modifiers/insertTjunctions.test.js +2 -2
- package/src/operations/modifiers/retessellate.test.js +10 -10
- package/src/operations/modifiers/snap.test.js +4 -4
- package/src/operations/offsets/offsetGeom3.test.js +4 -4
- package/src/operations/transforms/center.test.js +7 -7
- package/src/operations/transforms/mirror.test.js +7 -7
- package/src/operations/transforms/rotate.test.js +7 -7
- package/src/operations/transforms/scale.test.js +7 -7
- package/src/operations/transforms/transform.test.js +2 -2
- package/src/operations/transforms/translate.test.js +7 -7
- package/src/primitives/cube.test.js +4 -4
- package/src/primitives/cuboid.test.js +4 -4
- package/src/primitives/cylinder.test.js +5 -5
- package/src/primitives/cylinderElliptic.test.js +9 -9
- package/src/primitives/ellipsoid.test.js +5 -5
- package/src/primitives/geodesicSphere.test.js +4 -4
- package/src/primitives/polyhedron.test.js +2 -2
- package/src/primitives/roundedCuboid.test.js +7 -7
- package/src/primitives/roundedCylinder.test.js +9 -9
- package/src/primitives/sphere.test.js +5 -5
- package/src/primitives/torus.test.js +4 -4
- package/src/geometries/geom2/fromCompactBinary.d.ts +0 -3
- package/src/geometries/geom2/fromCompactBinary.js +0 -40
- package/src/geometries/geom2/fromToCompactBinary.test.js +0 -100
- package/src/geometries/geom2/toCompactBinary.d.ts +0 -3
- package/src/geometries/geom2/toCompactBinary.js +0 -56
- package/src/geometries/geom3/fromCompactBinary.d.ts +0 -3
- package/src/geometries/geom3/fromCompactBinary.js +0 -42
- package/src/geometries/geom3/fromPointsConvex.js +0 -25
- package/src/geometries/geom3/fromToCompactBinary.test.js +0 -139
- package/src/geometries/geom3/toCompactBinary.d.ts +0 -3
- package/src/geometries/geom3/toCompactBinary.js +0 -66
- package/src/geometries/path2/fromCompactBinary.d.ts +0 -3
- package/src/geometries/path2/fromCompactBinary.js +0 -31
- package/src/geometries/path2/fromToCompactBinary.test.js +0 -114
- package/src/geometries/path2/toCompactBinary.d.ts +0 -3
- package/src/geometries/path2/toCompactBinary.js +0 -50
|
@@ -10,7 +10,7 @@ import { comparePolygonsAsPoints } from '../../test/helpers/index.js'
|
|
|
10
10
|
|
|
11
11
|
test('roundedCylinder (defaults)', (t) => {
|
|
12
12
|
const obs = roundedCylinder()
|
|
13
|
-
const pts = geom3.
|
|
13
|
+
const pts = geom3.toVertices(obs)
|
|
14
14
|
|
|
15
15
|
t.notThrows(() => geom3.validate(obs))
|
|
16
16
|
t.is(measureArea(obs), 16.844951865908268)
|
|
@@ -20,7 +20,7 @@ test('roundedCylinder (defaults)', (t) => {
|
|
|
20
20
|
|
|
21
21
|
test('roundedCylinder (zero height)', (t) => {
|
|
22
22
|
const obs = roundedCylinder({ height: 0 })
|
|
23
|
-
const pts = geom3.
|
|
23
|
+
const pts = geom3.toVertices(obs)
|
|
24
24
|
t.notThrows(() => geom3.validate(obs))
|
|
25
25
|
t.is(measureArea(obs), 0)
|
|
26
26
|
t.is(measureVolume(obs), 0)
|
|
@@ -29,7 +29,7 @@ test('roundedCylinder (zero height)', (t) => {
|
|
|
29
29
|
|
|
30
30
|
test('roundedCylinder (zero radius)', (t) => {
|
|
31
31
|
const obs = roundedCylinder({ radius: 0, roundRadius: 0 })
|
|
32
|
-
const pts = geom3.
|
|
32
|
+
const pts = geom3.toVertices(obs)
|
|
33
33
|
t.notThrows(() => geom3.validate(obs))
|
|
34
34
|
t.is(measureArea(obs), 0)
|
|
35
35
|
t.is(measureVolume(obs), 0)
|
|
@@ -38,7 +38,7 @@ test('roundedCylinder (zero radius)', (t) => {
|
|
|
38
38
|
|
|
39
39
|
test('roundedCylinder (zero roundRadius)', (t) => {
|
|
40
40
|
const obs = roundedCylinder({ roundRadius: 0 })
|
|
41
|
-
const pts = geom3.
|
|
41
|
+
const pts = geom3.toVertices(obs)
|
|
42
42
|
t.notThrows(() => geom3.validate(obs))
|
|
43
43
|
t.is(measureArea(obs), 18.789084266699856)
|
|
44
44
|
t.is(measureVolume(obs), 6.2428903045161)
|
|
@@ -48,7 +48,7 @@ test('roundedCylinder (zero roundRadius)', (t) => {
|
|
|
48
48
|
test('roundedCylinder (options)', (t) => {
|
|
49
49
|
// test segments
|
|
50
50
|
let obs = roundedCylinder({ segments: 5 })
|
|
51
|
-
let pts = geom3.
|
|
51
|
+
let pts = geom3.toVertices(obs)
|
|
52
52
|
let exp = [
|
|
53
53
|
]
|
|
54
54
|
|
|
@@ -59,7 +59,7 @@ test('roundedCylinder (options)', (t) => {
|
|
|
59
59
|
|
|
60
60
|
// test center
|
|
61
61
|
obs = roundedCylinder({ segments: 5, center: [-5, -5, -5] })
|
|
62
|
-
pts = geom3.
|
|
62
|
+
pts = geom3.toVertices(obs)
|
|
63
63
|
exp = [
|
|
64
64
|
[[-4.048943483704846, -4.6909830056250525, -5.8], [-5, -4, -5.8],
|
|
65
65
|
[-5, -4, -4.2], [-4.048943483704846, -4.6909830056250525, -4.2]],
|
|
@@ -91,7 +91,7 @@ test('roundedCylinder (options)', (t) => {
|
|
|
91
91
|
|
|
92
92
|
// test height
|
|
93
93
|
obs = roundedCylinder({ segments: 5, height: 20 })
|
|
94
|
-
pts = geom3.
|
|
94
|
+
pts = geom3.toVertices(obs)
|
|
95
95
|
exp = [
|
|
96
96
|
[[0.9510565162951535, 0.30901699437494745, -9.8], [0, 1, -9.8],
|
|
97
97
|
[0, 1, 9.8], [0.9510565162951535, 0.30901699437494745, 9.8]],
|
|
@@ -123,7 +123,7 @@ test('roundedCylinder (options)', (t) => {
|
|
|
123
123
|
|
|
124
124
|
// test radius
|
|
125
125
|
obs = roundedCylinder({ segments: 5, radius: 10 })
|
|
126
|
-
pts = geom3.
|
|
126
|
+
pts = geom3.toVertices(obs)
|
|
127
127
|
exp = [
|
|
128
128
|
[[9.510565162951535, 3.0901699437494745, -0.8], [0, 10, -0.8],
|
|
129
129
|
[0, 10, 0.8], [9.510565162951535, 3.0901699437494745, 0.8]],
|
|
@@ -154,7 +154,7 @@ test('roundedCylinder (options)', (t) => {
|
|
|
154
154
|
|
|
155
155
|
// test round radius
|
|
156
156
|
obs = roundedCylinder({ segments: 5, height: 20, radius: 5, roundRadius: 2 })
|
|
157
|
-
pts = geom3.
|
|
157
|
+
pts = geom3.toVertices(obs)
|
|
158
158
|
exp = [
|
|
159
159
|
[[4.755282581475767, 1.5450849718747373, -8], [0, 5, -8],
|
|
160
160
|
[0, 5, 8], [4.755282581475767, 1.5450849718747373, 8]],
|
|
@@ -10,7 +10,7 @@ import { comparePolygonsAsPoints } from '../../test/helpers/index.js'
|
|
|
10
10
|
|
|
11
11
|
test('sphere (defaults)', (t) => {
|
|
12
12
|
const obs = sphere()
|
|
13
|
-
const pts = geom3.
|
|
13
|
+
const pts = geom3.toVertices(obs)
|
|
14
14
|
|
|
15
15
|
t.notThrows(() => geom3.validate(obs))
|
|
16
16
|
t.is(measureArea(obs), 12.465694088650583)
|
|
@@ -21,7 +21,7 @@ test('sphere (defaults)', (t) => {
|
|
|
21
21
|
test('sphere (options)', (t) => {
|
|
22
22
|
// test radius
|
|
23
23
|
let obs = sphere({ radius: 5, segments: 12 })
|
|
24
|
-
let pts = geom3.
|
|
24
|
+
let pts = geom3.toVertices(obs)
|
|
25
25
|
let exp = []
|
|
26
26
|
t.notThrows(() => geom3.validate(obs))
|
|
27
27
|
t.is(measureArea(obs), 296.5322084069296)
|
|
@@ -31,7 +31,7 @@ test('sphere (options)', (t) => {
|
|
|
31
31
|
|
|
32
32
|
// test segments
|
|
33
33
|
obs = sphere({ segments: 8 })
|
|
34
|
-
pts = geom3.
|
|
34
|
+
pts = geom3.toVertices(obs)
|
|
35
35
|
exp = [
|
|
36
36
|
[[1, 0, 0], [0.7071067811865476, -0.7071067811865475, 0],
|
|
37
37
|
[0.5000000000000001, -0.5, -0.7071067811865475], [0.7071067811865476, 0, -0.7071067811865475]],
|
|
@@ -116,7 +116,7 @@ test('sphere (options)', (t) => {
|
|
|
116
116
|
|
|
117
117
|
// test center
|
|
118
118
|
obs = sphere({ center: [-3, 5, 7], segments: 8 })
|
|
119
|
-
pts = geom3.
|
|
119
|
+
pts = geom3.toVertices(obs)
|
|
120
120
|
exp = [
|
|
121
121
|
[[-2, 5, 7], [-2.2928932188134525, 4.292893218813452, 7],
|
|
122
122
|
[-2.5, 4.5, 6.292893218813452], [-2.2928932188134525, 5, 6.292893218813452]],
|
|
@@ -176,7 +176,7 @@ test('sphere (options)', (t) => {
|
|
|
176
176
|
|
|
177
177
|
test('sphere (zero radius)', (t) => {
|
|
178
178
|
const obs = sphere({ radius: 0 })
|
|
179
|
-
const pts = geom3.
|
|
179
|
+
const pts = geom3.toVertices(obs)
|
|
180
180
|
t.notThrows(() => geom3.validate(obs))
|
|
181
181
|
t.is(measureArea(obs), 0)
|
|
182
182
|
t.is(measureVolume(obs), 0)
|
|
@@ -12,7 +12,7 @@ import { comparePoints } from '../../test/helpers/index.js'
|
|
|
12
12
|
|
|
13
13
|
test('torus (defaults)', (t) => {
|
|
14
14
|
const obs = torus()
|
|
15
|
-
const pts = geom3.
|
|
15
|
+
const pts = geom3.toVertices(obs)
|
|
16
16
|
|
|
17
17
|
t.notThrows(() => geom3.validate(obs))
|
|
18
18
|
t.is(measureArea(obs), 157.0282327749074)
|
|
@@ -26,7 +26,7 @@ test('torus (defaults)', (t) => {
|
|
|
26
26
|
|
|
27
27
|
test('torus (simple options)', (t) => {
|
|
28
28
|
const obs = torus({ innerRadius: 0.5, innerSegments: 4, outerRadius: 5, outerSegments: 8 })
|
|
29
|
-
const pts = geom3.
|
|
29
|
+
const pts = geom3.toVertices(obs)
|
|
30
30
|
t.notThrows(() => geom3.validate(obs))
|
|
31
31
|
t.is(measureArea(obs), 83.36086132479792)
|
|
32
32
|
t.is(measureVolume(obs), 14.14213562373095)
|
|
@@ -39,7 +39,7 @@ test('torus (simple options)', (t) => {
|
|
|
39
39
|
|
|
40
40
|
test('torus (complex options)', (t) => {
|
|
41
41
|
const obs = torus({ innerRadius: 1, outerRadius: 5, innerSegments: 32, outerSegments: 72, startAngle: TAU / 4, outerRotation: TAU / 4 })
|
|
42
|
-
const pts = geom3.
|
|
42
|
+
const pts = geom3.toVertices(obs)
|
|
43
43
|
t.notThrows(() => geom3.validate(obs))
|
|
44
44
|
t.is(measureArea(obs), 55.472610544494)
|
|
45
45
|
t.is(measureVolume(obs), 24.484668362201525)
|
|
@@ -52,7 +52,7 @@ test('torus (complex options)', (t) => {
|
|
|
52
52
|
|
|
53
53
|
test('torus (startAngle)', (t) => {
|
|
54
54
|
const obs = torus({ startAngle: 1, endAngle: 1 + TAU })
|
|
55
|
-
const pts = geom3.
|
|
55
|
+
const pts = geom3.toVertices(obs)
|
|
56
56
|
t.notThrows(() => geom3.validate(obs))
|
|
57
57
|
t.is(measureArea(obs), 157.0282327749074)
|
|
58
58
|
t.is(measureVolume(obs), 77.94735870844195)
|
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
import * as mat4 from '../../maths/mat4/index.js'
|
|
2
|
-
import * as vec2 from '../../maths/vec2/index.js'
|
|
3
|
-
|
|
4
|
-
import { create } from './create.js'
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Create a new 2D geometry from the given compact binary data.
|
|
8
|
-
* @param {Array} data - compact binary data
|
|
9
|
-
* @returns {Geom2} a new geometry
|
|
10
|
-
* @alias module:modeling/geometries/geom2.fromCompactBinary
|
|
11
|
-
*/
|
|
12
|
-
export const fromCompactBinary = (data) => {
|
|
13
|
-
if (data[0] !== 0) throw new Error('invalid compact binary data')
|
|
14
|
-
|
|
15
|
-
const created = create()
|
|
16
|
-
|
|
17
|
-
created.transforms = mat4.clone(data.slice(1, 17))
|
|
18
|
-
|
|
19
|
-
for (let i = 21; i < data.length;) {
|
|
20
|
-
const length = data[i++] // number of points for this polygon
|
|
21
|
-
if (length < 0 || i + length * 2 > data.length) {
|
|
22
|
-
throw new Error('invalid compact binary data')
|
|
23
|
-
}
|
|
24
|
-
const outline = []
|
|
25
|
-
for (let j = 0; j < length; j++) {
|
|
26
|
-
const x = data[i + j * 2]
|
|
27
|
-
const y = data[i + j * 2 + 1]
|
|
28
|
-
outline.push(vec2.fromValues(x, y))
|
|
29
|
-
}
|
|
30
|
-
created.outlines.push(outline)
|
|
31
|
-
i += length * 2
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
// transfer known properties, i.e. color
|
|
35
|
-
if (data[17] >= 0) {
|
|
36
|
-
created.color = [data[17], data[18], data[19], data[20]]
|
|
37
|
-
}
|
|
38
|
-
// TODO: how about custom properties or fields ?
|
|
39
|
-
return created
|
|
40
|
-
}
|
|
@@ -1,100 +0,0 @@
|
|
|
1
|
-
import test from 'ava'
|
|
2
|
-
|
|
3
|
-
import { fromCompactBinary, toCompactBinary, create } from './index.js'
|
|
4
|
-
|
|
5
|
-
test('toCompactBinary: converts geom2 into a compact form', (t) => {
|
|
6
|
-
const geometry1 = create()
|
|
7
|
-
const compacted1 = toCompactBinary(geometry1)
|
|
8
|
-
const expected1 = new Float32Array([
|
|
9
|
-
0, // type flag
|
|
10
|
-
1, 0, 0, 0, // transforms
|
|
11
|
-
0, 1, 0, 0,
|
|
12
|
-
0, 0, 1, 0,
|
|
13
|
-
0, 0, 0, 1,
|
|
14
|
-
-1, -1, -1, -1 // color
|
|
15
|
-
])
|
|
16
|
-
t.deepEqual(compacted1, expected1)
|
|
17
|
-
|
|
18
|
-
// geometry with a hole
|
|
19
|
-
const geometry2 = create([
|
|
20
|
-
[[10, 10], [-10, -10], [10, -10]],
|
|
21
|
-
[[5, -5], [6, -4], [6, -5]]
|
|
22
|
-
])
|
|
23
|
-
const compacted2 = toCompactBinary(geometry2)
|
|
24
|
-
const expected2 = new Float32Array([
|
|
25
|
-
0, // type flag
|
|
26
|
-
1, 0, 0, 0, // transforms
|
|
27
|
-
0, 1, 0, 0,
|
|
28
|
-
0, 0, 1, 0,
|
|
29
|
-
0, 0, 0, 1,
|
|
30
|
-
-1, -1, -1, -1, // color
|
|
31
|
-
3, 10, 10, -10, -10, 10, -10, // poly1
|
|
32
|
-
3, 5, -5, 6, -4, 6, -5 // poly2
|
|
33
|
-
])
|
|
34
|
-
t.deepEqual(compacted2, expected2)
|
|
35
|
-
|
|
36
|
-
// test color as well
|
|
37
|
-
geometry2.color = [1, 2, 3, 4]
|
|
38
|
-
const compacted3 = toCompactBinary(geometry2)
|
|
39
|
-
const expected3 = new Float32Array([
|
|
40
|
-
0, // type flag
|
|
41
|
-
1, 0, 0, 0, // transforms
|
|
42
|
-
0, 1, 0, 0,
|
|
43
|
-
0, 0, 1, 0,
|
|
44
|
-
0, 0, 0, 1,
|
|
45
|
-
1, 2, 3, 4, // color
|
|
46
|
-
3, 10, 10, -10, -10, 10, -10, // poly1
|
|
47
|
-
3, 5, -5, 6, -4, 6, -5 // poly2
|
|
48
|
-
])
|
|
49
|
-
t.deepEqual(compacted3, expected3)
|
|
50
|
-
})
|
|
51
|
-
|
|
52
|
-
test('fromCompactBinary: convert a compact form into a geom2', (t) => {
|
|
53
|
-
const compacted1 = [
|
|
54
|
-
0, // type flag
|
|
55
|
-
1, 0, 0, 0, // transforms
|
|
56
|
-
0, 1, 0, 0,
|
|
57
|
-
0, 0, 1, 0,
|
|
58
|
-
0, 0, 0, 1,
|
|
59
|
-
-1, -1, -1, -1 // color
|
|
60
|
-
]
|
|
61
|
-
const expected1 = create()
|
|
62
|
-
const geometry1 = fromCompactBinary(compacted1)
|
|
63
|
-
|
|
64
|
-
t.deepEqual(geometry1, expected1)
|
|
65
|
-
|
|
66
|
-
// geometry with a hole
|
|
67
|
-
const compacted2 = [
|
|
68
|
-
0, // type flag
|
|
69
|
-
1, 0, 0, 0, // transforms
|
|
70
|
-
0, 1, 0, 0,
|
|
71
|
-
0, 0, 1, 0,
|
|
72
|
-
0, 0, 0, 1,
|
|
73
|
-
-1, -1, -1, -1, // color
|
|
74
|
-
3, 10, 10, -10, -10, 10, -10, // poly1
|
|
75
|
-
3, 5, -5, 6, -5, 6, -4 // poly2
|
|
76
|
-
]
|
|
77
|
-
const expected2 = create([
|
|
78
|
-
[[10, 10], [-10, -10], [10, -10]],
|
|
79
|
-
[[5, -5], [6, -5], [6, -4]]
|
|
80
|
-
])
|
|
81
|
-
const geometry2 = fromCompactBinary(compacted2)
|
|
82
|
-
|
|
83
|
-
t.deepEqual(geometry2, expected2)
|
|
84
|
-
|
|
85
|
-
// test color as well
|
|
86
|
-
const compacted3 = [
|
|
87
|
-
0, // type flag
|
|
88
|
-
1, 0, 0, 0, // transforms
|
|
89
|
-
0, 1, 0, 0,
|
|
90
|
-
0, 0, 1, 0,
|
|
91
|
-
0, 0, 0, 1,
|
|
92
|
-
4, 5, 6, 7, // color
|
|
93
|
-
3, 10, 10, -10, -10, 10, -10, // poly1
|
|
94
|
-
3, 5, -5, 6, -5, 6, -4 // poly2
|
|
95
|
-
]
|
|
96
|
-
expected2.color = [4, 5, 6, 7]
|
|
97
|
-
const geometry3 = fromCompactBinary(compacted3)
|
|
98
|
-
|
|
99
|
-
t.deepEqual(geometry3, expected2)
|
|
100
|
-
})
|
|
@@ -1,56 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Produces a compact binary representation from the given geometry.
|
|
3
|
-
* @param {Geom2} geometry - the geometry
|
|
4
|
-
* @returns {TypedArray} compact binary representation
|
|
5
|
-
* @alias module:modeling/geometries/geom2.toCompactBinary
|
|
6
|
-
*/
|
|
7
|
-
export const toCompactBinary = (geometry) => {
|
|
8
|
-
const transforms = geometry.transforms
|
|
9
|
-
let color = [-1, -1, -1, -1]
|
|
10
|
-
if (geometry.color) color = geometry.color
|
|
11
|
-
|
|
12
|
-
// Compute array size
|
|
13
|
-
let size = 21
|
|
14
|
-
geometry.outlines.forEach((outline) => {
|
|
15
|
-
size += 2 * outline.length + 1
|
|
16
|
-
})
|
|
17
|
-
|
|
18
|
-
// FIXME why Float32Array?
|
|
19
|
-
const compacted = new Float32Array(size) // type + transforms + color + points
|
|
20
|
-
|
|
21
|
-
compacted[0] = 0 // type code: 0 => geom2, 1 => geom3 , 2 => path2
|
|
22
|
-
|
|
23
|
-
compacted[1] = transforms[0]
|
|
24
|
-
compacted[2] = transforms[1]
|
|
25
|
-
compacted[3] = transforms[2]
|
|
26
|
-
compacted[4] = transforms[3]
|
|
27
|
-
compacted[5] = transforms[4]
|
|
28
|
-
compacted[6] = transforms[5]
|
|
29
|
-
compacted[7] = transforms[6]
|
|
30
|
-
compacted[8] = transforms[7]
|
|
31
|
-
compacted[9] = transforms[8]
|
|
32
|
-
compacted[10] = transforms[9]
|
|
33
|
-
compacted[11] = transforms[10]
|
|
34
|
-
compacted[12] = transforms[11]
|
|
35
|
-
compacted[13] = transforms[12]
|
|
36
|
-
compacted[14] = transforms[13]
|
|
37
|
-
compacted[15] = transforms[14]
|
|
38
|
-
compacted[16] = transforms[15]
|
|
39
|
-
|
|
40
|
-
compacted[17] = color[0]
|
|
41
|
-
compacted[18] = color[1]
|
|
42
|
-
compacted[19] = color[2]
|
|
43
|
-
compacted[20] = color[3]
|
|
44
|
-
|
|
45
|
-
let index = 21
|
|
46
|
-
geometry.outlines.forEach((outline) => {
|
|
47
|
-
compacted[index++] = outline.length
|
|
48
|
-
outline.forEach((point) => {
|
|
49
|
-
compacted[index++] = point[0]
|
|
50
|
-
compacted[index++] = point[1]
|
|
51
|
-
})
|
|
52
|
-
})
|
|
53
|
-
|
|
54
|
-
// TODO: how about custom properties or fields ?
|
|
55
|
-
return compacted
|
|
56
|
-
}
|
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
import * as vec3 from '../../maths/vec3/index.js'
|
|
2
|
-
import * as mat4 from '../../maths/mat4/index.js'
|
|
3
|
-
|
|
4
|
-
import * as poly3 from '../poly3/index.js'
|
|
5
|
-
|
|
6
|
-
import { create } from './create.js'
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* Construct a new 3D geometry from the given compact binary data.
|
|
10
|
-
* @param {TypedArray} data - compact binary data
|
|
11
|
-
* @returns {Geom3} a new geometry
|
|
12
|
-
* @alias module:modeling/geometries/geom3.fromCompactBinary
|
|
13
|
-
*/
|
|
14
|
-
export const fromCompactBinary = (data) => {
|
|
15
|
-
if (data[0] !== 1) throw new Error('invalid compact binary data')
|
|
16
|
-
|
|
17
|
-
const created = create()
|
|
18
|
-
|
|
19
|
-
created.transforms = mat4.clone(data.slice(1, 17))
|
|
20
|
-
|
|
21
|
-
const numberOfVertices = data[21]
|
|
22
|
-
let ci = 22
|
|
23
|
-
let vi = data.length - (numberOfVertices * 3)
|
|
24
|
-
while (vi < data.length) {
|
|
25
|
-
const verticesPerPolygon = data[ci]
|
|
26
|
-
ci++
|
|
27
|
-
|
|
28
|
-
const vertices = []
|
|
29
|
-
for (let i = 0; i < verticesPerPolygon; i++) {
|
|
30
|
-
vertices.push(vec3.fromValues(data[vi], data[vi + 1], data[vi + 2]))
|
|
31
|
-
vi += 3
|
|
32
|
-
}
|
|
33
|
-
created.polygons.push(poly3.create(vertices))
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
// transfer known properties, i.e. color
|
|
37
|
-
if (data[17] >= 0) {
|
|
38
|
-
created.color = [data[17], data[18], data[19], data[20]]
|
|
39
|
-
}
|
|
40
|
-
// TODO: how about custom properties or fields ?
|
|
41
|
-
return created
|
|
42
|
-
}
|
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
import { runner } from '../../operations/hulls/quickhull/index.js'
|
|
2
|
-
import { create } from './create.js'
|
|
3
|
-
import * as poly3 from '../poly3/index.js'
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Construct a new convex 3D geometry from a list of unique points.
|
|
7
|
-
*
|
|
8
|
-
* @param {Array} uniquePoints - list of points to construct convex 3D geometry
|
|
9
|
-
* @returns {geom3} a new geometry
|
|
10
|
-
* @alias module:modeling/geometries/geom3.fromPointsConvex
|
|
11
|
-
*/
|
|
12
|
-
export const fromPointsConvex = (uniquePoints) => {
|
|
13
|
-
if (!Array.isArray(uniquePoints)) {
|
|
14
|
-
throw new Error('the given points must be an array')
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
const faces = runner(uniquePoints, { skipTriangulation: true })
|
|
18
|
-
|
|
19
|
-
const polygons = faces.map((face) => {
|
|
20
|
-
const vertices = face.map((index) => uniquePoints[index])
|
|
21
|
-
return poly3.create(vertices)
|
|
22
|
-
})
|
|
23
|
-
|
|
24
|
-
return create(polygons)
|
|
25
|
-
}
|
|
@@ -1,139 +0,0 @@
|
|
|
1
|
-
import test from 'ava'
|
|
2
|
-
|
|
3
|
-
import { fromCompactBinary, toCompactBinary, create, fromPoints } from './index.js'
|
|
4
|
-
|
|
5
|
-
test('toCompactBinary: converts geom3 (default)', (t) => {
|
|
6
|
-
const geometry = create()
|
|
7
|
-
const compacted = toCompactBinary(geometry)
|
|
8
|
-
const expected = new Float32Array(
|
|
9
|
-
[
|
|
10
|
-
1, // type
|
|
11
|
-
1, 0, 0, 0, // transforms
|
|
12
|
-
0, 1, 0, 0,
|
|
13
|
-
0, 0, 1, 0,
|
|
14
|
-
0, 0, 0, 1,
|
|
15
|
-
-1, -1, -1, -1, // color
|
|
16
|
-
0 // number of vertices
|
|
17
|
-
]
|
|
18
|
-
)
|
|
19
|
-
t.deepEqual(compacted, expected)
|
|
20
|
-
})
|
|
21
|
-
|
|
22
|
-
test('toCompactBinary: converts geom3 into a compact form', (t) => {
|
|
23
|
-
// two polygons; 3 vertices, 4 vertices
|
|
24
|
-
const vertices = [[[0, 0, 0], [1, 0, 0], [2, 0, 2]], [[0, 0, 0], [1, 0, 0], [2, 0, 2], [-3, 0, 3]]]
|
|
25
|
-
const geometry = fromPoints(vertices)
|
|
26
|
-
const compacted = toCompactBinary(geometry)
|
|
27
|
-
const expected = new Float32Array(
|
|
28
|
-
[
|
|
29
|
-
1, // type
|
|
30
|
-
1, 0, 0, 0, // transforms
|
|
31
|
-
0, 1, 0, 0,
|
|
32
|
-
0, 0, 1, 0,
|
|
33
|
-
0, 0, 0, 1,
|
|
34
|
-
-1, -1, -1, -1, // color
|
|
35
|
-
7, // number of vertices
|
|
36
|
-
3, // number of vertices per polygon (2)
|
|
37
|
-
4,
|
|
38
|
-
0, 0, 0, // vertices (7)
|
|
39
|
-
1, 0, 0,
|
|
40
|
-
2, 0, 2,
|
|
41
|
-
0, 0, 0,
|
|
42
|
-
1, 0, 0,
|
|
43
|
-
2, 0, 2,
|
|
44
|
-
-3, 0, 3
|
|
45
|
-
]
|
|
46
|
-
)
|
|
47
|
-
|
|
48
|
-
t.deepEqual(compacted, expected)
|
|
49
|
-
|
|
50
|
-
// test color as well
|
|
51
|
-
geometry.color = [1, 2, 3, 4]
|
|
52
|
-
const compacted2 = toCompactBinary(geometry)
|
|
53
|
-
const expected2 = new Float32Array(
|
|
54
|
-
[
|
|
55
|
-
1, // type
|
|
56
|
-
1, 0, 0, 0, // transforms
|
|
57
|
-
0, 1, 0, 0,
|
|
58
|
-
0, 0, 1, 0,
|
|
59
|
-
0, 0, 0, 1,
|
|
60
|
-
1, 2, 3, 4, // color
|
|
61
|
-
7, // number of vertices
|
|
62
|
-
3, // number of vertices per polygon (2)
|
|
63
|
-
4,
|
|
64
|
-
0, 0, 0, // vertices (7)
|
|
65
|
-
1, 0, 0,
|
|
66
|
-
2, 0, 2,
|
|
67
|
-
0, 0, 0,
|
|
68
|
-
1, 0, 0,
|
|
69
|
-
2, 0, 2,
|
|
70
|
-
-3, 0, 3
|
|
71
|
-
]
|
|
72
|
-
)
|
|
73
|
-
|
|
74
|
-
t.deepEqual(compacted2, expected2)
|
|
75
|
-
})
|
|
76
|
-
|
|
77
|
-
test('fromCompactBinary: convert a compact form into a geom3', (t) => {
|
|
78
|
-
const compactedDefault = [
|
|
79
|
-
1, // type
|
|
80
|
-
1, 0, 0, 0, // transforms
|
|
81
|
-
0, 1, 0, 0,
|
|
82
|
-
0, 0, 1, 0,
|
|
83
|
-
0, 0, 0, 1,
|
|
84
|
-
-1, -1, -1, -1, // color
|
|
85
|
-
0 // number of vertices
|
|
86
|
-
]
|
|
87
|
-
let expected = create()
|
|
88
|
-
let geometry = fromCompactBinary(compactedDefault)
|
|
89
|
-
|
|
90
|
-
t.deepEqual(geometry, expected)
|
|
91
|
-
|
|
92
|
-
const compacted1 = [
|
|
93
|
-
1, // type
|
|
94
|
-
1, 0, 0, 0, // transforms
|
|
95
|
-
0, 1, 0, 0,
|
|
96
|
-
0, 0, 1, 0,
|
|
97
|
-
0, 0, 0, 1,
|
|
98
|
-
-1, -1, -1, -1, // color
|
|
99
|
-
7, // number of vertices
|
|
100
|
-
3, // number of vertices per polygon (2)
|
|
101
|
-
4,
|
|
102
|
-
0, 0, 0, // vertices (7)
|
|
103
|
-
1, 0, 0,
|
|
104
|
-
2, 0, 2,
|
|
105
|
-
0, 0, 0,
|
|
106
|
-
1, 0, 0,
|
|
107
|
-
2, 0, 2,
|
|
108
|
-
-3, 0, 3
|
|
109
|
-
]
|
|
110
|
-
const vertices = [[[0, 0, 0], [1, 0, 0], [2, 0, 2]], [[0, 0, 0], [1, 0, 0], [2, 0, 2], [-3, 0, 3]]]
|
|
111
|
-
expected = fromPoints(vertices)
|
|
112
|
-
geometry = fromCompactBinary(compacted1)
|
|
113
|
-
|
|
114
|
-
t.deepEqual(geometry, expected)
|
|
115
|
-
|
|
116
|
-
// test color as well
|
|
117
|
-
const compacted2 = [
|
|
118
|
-
1, // type
|
|
119
|
-
1, 0, 0, 0, // transforms
|
|
120
|
-
0, 1, 0, 0,
|
|
121
|
-
0, 0, 1, 0,
|
|
122
|
-
0, 0, 0, 1,
|
|
123
|
-
4, 5, 6, 7, // color
|
|
124
|
-
7, // number of vertices
|
|
125
|
-
3, // number of vertices per polygon (2)
|
|
126
|
-
4,
|
|
127
|
-
0, 0, 0, // vertices (7)
|
|
128
|
-
1, 0, 0,
|
|
129
|
-
2, 0, 2,
|
|
130
|
-
0, 0, 0,
|
|
131
|
-
1, 0, 0,
|
|
132
|
-
2, 0, 2,
|
|
133
|
-
-3, 0, 3
|
|
134
|
-
]
|
|
135
|
-
expected.color = [4, 5, 6, 7]
|
|
136
|
-
geometry = fromCompactBinary(compacted2)
|
|
137
|
-
|
|
138
|
-
t.deepEqual(geometry, expected)
|
|
139
|
-
})
|
|
@@ -1,66 +0,0 @@
|
|
|
1
|
-
import * as poly3 from '../poly3/index.js'
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Return the given geometry in compact binary representation.
|
|
5
|
-
* @param {Geom3} geometry - the geometry
|
|
6
|
-
* @return {TypedArray} compact binary representation
|
|
7
|
-
* @alias module:modeling/geometries/geom3.toCompactBinary
|
|
8
|
-
*/
|
|
9
|
-
export const toCompactBinary = (geometry) => {
|
|
10
|
-
const polygons = geometry.polygons
|
|
11
|
-
const transforms = geometry.transforms
|
|
12
|
-
|
|
13
|
-
const numberOfPolygons = polygons.length
|
|
14
|
-
const numberOfVertices = polygons.reduce((count, polygon) => count + polygon.vertices.length, 0)
|
|
15
|
-
let color = [-1, -1, -1, -1]
|
|
16
|
-
if (geometry.color) color = geometry.color
|
|
17
|
-
|
|
18
|
-
// FIXME why Float32Array?
|
|
19
|
-
const compacted = new Float32Array(1 + 16 + 4 + 1 + numberOfPolygons + (numberOfVertices * 3))
|
|
20
|
-
// type + transforms + color + numberOfPolygons + numberOfVerticesPerPolygon[] + vertices data[]
|
|
21
|
-
|
|
22
|
-
compacted[0] = 1 // type code: 0 => geom2, 1 => geom3 , 2 => path2
|
|
23
|
-
|
|
24
|
-
compacted[1] = transforms[0]
|
|
25
|
-
compacted[2] = transforms[1]
|
|
26
|
-
compacted[3] = transforms[2]
|
|
27
|
-
compacted[4] = transforms[3]
|
|
28
|
-
compacted[5] = transforms[4]
|
|
29
|
-
compacted[6] = transforms[5]
|
|
30
|
-
compacted[7] = transforms[6]
|
|
31
|
-
compacted[8] = transforms[7]
|
|
32
|
-
compacted[9] = transforms[8]
|
|
33
|
-
compacted[10] = transforms[9]
|
|
34
|
-
compacted[11] = transforms[10]
|
|
35
|
-
compacted[12] = transforms[11]
|
|
36
|
-
compacted[13] = transforms[12]
|
|
37
|
-
compacted[14] = transforms[13]
|
|
38
|
-
compacted[15] = transforms[14]
|
|
39
|
-
compacted[16] = transforms[15]
|
|
40
|
-
|
|
41
|
-
compacted[17] = color[0]
|
|
42
|
-
compacted[18] = color[1]
|
|
43
|
-
compacted[19] = color[2]
|
|
44
|
-
compacted[20] = color[3]
|
|
45
|
-
|
|
46
|
-
compacted[21] = numberOfVertices
|
|
47
|
-
|
|
48
|
-
let ci = 22
|
|
49
|
-
let vi = ci + numberOfPolygons
|
|
50
|
-
polygons.forEach((polygon) => {
|
|
51
|
-
const vertices = poly3.toVertices(polygon)
|
|
52
|
-
// record the number of vertices per polygon
|
|
53
|
-
compacted[ci] = vertices.length
|
|
54
|
-
ci++
|
|
55
|
-
// convert the vertices
|
|
56
|
-
for (let i = 0; i < vertices.length; i++) {
|
|
57
|
-
const vertex = vertices[i]
|
|
58
|
-
compacted[vi + 0] = vertex[0]
|
|
59
|
-
compacted[vi + 1] = vertex[1]
|
|
60
|
-
compacted[vi + 2] = vertex[2]
|
|
61
|
-
vi += 3
|
|
62
|
-
}
|
|
63
|
-
})
|
|
64
|
-
// TODO: how about custom properties or fields ?
|
|
65
|
-
return compacted
|
|
66
|
-
}
|
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
import * as mat4 from '../../maths/mat4/index.js'
|
|
2
|
-
import * as vec2 from '../../maths/vec2/index.js'
|
|
3
|
-
|
|
4
|
-
import { create } from './create.js'
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Create a new path from the given compact binary data.
|
|
8
|
-
* @param {TypedArray} data - compact binary data
|
|
9
|
-
* @returns {Path2} a new path
|
|
10
|
-
* @alias module:modeling/geometries/path2.fromCompactBinary
|
|
11
|
-
*/
|
|
12
|
-
export const fromCompactBinary = (data) => {
|
|
13
|
-
if (data[0] !== 2) throw new Error('invalid compact binary data')
|
|
14
|
-
|
|
15
|
-
const created = create()
|
|
16
|
-
|
|
17
|
-
created.transforms = mat4.clone(data.slice(1, 17))
|
|
18
|
-
|
|
19
|
-
created.isClosed = !!data[17]
|
|
20
|
-
|
|
21
|
-
for (let i = 22; i < data.length; i += 2) {
|
|
22
|
-
const point = vec2.fromValues(data[i], data[i + 1])
|
|
23
|
-
created.points.push(point)
|
|
24
|
-
}
|
|
25
|
-
// transfer known properties, i.e. color
|
|
26
|
-
if (data[18] >= 0) {
|
|
27
|
-
created.color = [data[18], data[19], data[20], data[21]]
|
|
28
|
-
}
|
|
29
|
-
// TODO: how about custom properties or fields ?
|
|
30
|
-
return created
|
|
31
|
-
}
|