@jscad/modeling 3.0.3-alpha.0 → 3.0.4-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 +17 -0
- package/dist/jscad-modeling.es.js +2 -7
- package/dist/jscad-modeling.min.js +2 -7
- package/package.json +6 -7
- package/rollup.config.js +8 -4
- package/src/curves/bezier/arcLengthToT.js +1 -1
- package/src/curves/bezier/create.js +1 -1
- package/src/curves/bezier/index.js +7 -7
- package/src/curves/bezier/length.js +1 -1
- package/src/curves/bezier/lengths.js +2 -1
- package/src/curves/bezier/tangentAt.js +1 -1
- package/src/curves/bezier/valueAt.js +1 -1
- package/src/curves/index.js +3 -3
- package/src/geometries/geom2/applyTransforms.js +3 -1
- package/src/geometries/geom2/clone.js +5 -1
- package/src/geometries/geom2/create.js +4 -14
- package/src/geometries/geom2/fromSides.js +4 -2
- package/src/geometries/geom2/index.js +21 -5
- package/src/geometries/geom2/isA.js +5 -1
- package/src/geometries/geom2/reverse.js +4 -2
- package/src/geometries/geom2/toOutlines.js +2 -1
- package/src/geometries/geom2/toPoints.js +5 -2
- package/src/geometries/geom2/toSides.js +4 -3
- package/src/geometries/geom2/toString.js +3 -2
- package/src/geometries/geom2/transform.js +4 -2
- package/src/geometries/geom2/validate.js +6 -2
- package/src/geometries/geom3/clone.js +5 -1
- package/src/geometries/geom3/create.js +5 -19
- package/src/geometries/geom3/fromVertices.js +13 -1
- package/src/geometries/geom3/fromVerticesConvex.js +1 -1
- package/src/geometries/geom3/index.d.ts +1 -0
- package/src/geometries/geom3/index.js +26 -4
- package/src/geometries/geom3/invert.js +5 -1
- package/src/geometries/geom3/isA.js +5 -1
- package/src/geometries/geom3/isConvex.d.ts +3 -0
- package/src/geometries/geom3/isConvex.js +65 -0
- package/src/geometries/geom3/isConvex.test.js +44 -0
- package/src/geometries/geom3/toPolygons.js +4 -2
- package/src/geometries/geom3/toString.js +3 -2
- package/src/geometries/geom3/toVertices.js +8 -4
- package/src/geometries/geom3/transform.js +5 -2
- package/src/geometries/geom3/validate.js +6 -2
- package/src/geometries/index.js +9 -7
- package/src/geometries/path2/appendArc.js +7 -5
- package/src/geometries/path2/appendArc.test.js +11 -15
- package/src/geometries/path2/appendBezier.js +6 -4
- package/src/geometries/path2/appendPoints.js +4 -2
- package/src/geometries/path2/applyTransforms.js +3 -0
- package/src/geometries/path2/clone.js +5 -1
- package/src/geometries/path2/close.js +5 -1
- package/src/geometries/path2/concat.js +3 -2
- package/src/geometries/path2/create.js +4 -15
- package/src/geometries/path2/equals.js +12 -7
- package/src/geometries/path2/fromPoints.js +5 -3
- package/src/geometries/path2/index.js +21 -4
- package/src/geometries/path2/isA.js +5 -1
- package/src/geometries/path2/reverse.js +4 -2
- package/src/geometries/path2/toPoints.js +5 -3
- package/src/geometries/path2/toString.js +3 -2
- package/src/geometries/path2/transform.js +4 -2
- package/src/geometries/path2/validate.js +5 -1
- package/src/geometries/path3/applyTransforms.js +1 -1
- package/src/geometries/path3/close.js +4 -2
- package/src/geometries/path3/concat.js +2 -3
- package/src/geometries/path3/create.js +4 -20
- package/src/geometries/path3/equals.js +4 -2
- package/src/geometries/path3/fromVertices.js +2 -3
- package/src/geometries/path3/index.js +17 -1
- package/src/geometries/path3/isA.js +4 -2
- package/src/geometries/path3/reverse.js +2 -3
- package/src/geometries/path3/toString.js +2 -3
- package/src/geometries/path3/toVertices.js +2 -3
- package/src/geometries/path3/transform.js +2 -3
- package/src/geometries/path3/validate.js +6 -3
- package/src/geometries/poly2/arePointsInside.js +4 -1
- package/src/geometries/poly2/clone.js +4 -1
- package/src/geometries/poly2/create.js +2 -9
- package/src/geometries/poly2/index.js +16 -4
- package/src/geometries/poly2/isA.js +5 -1
- package/src/geometries/poly2/isConvex.js +5 -1
- package/src/geometries/poly2/isSimple.js +5 -1
- package/src/geometries/poly2/measureArea.js +4 -1
- package/src/geometries/poly2/measureBoundingBox.js +6 -1
- package/src/geometries/poly2/reverse.js +4 -1
- package/src/geometries/poly2/toPoints.js +6 -1
- package/src/geometries/poly2/toString.js +5 -1
- package/src/geometries/poly2/transform.js +5 -1
- package/src/geometries/poly2/validate.js +6 -2
- package/src/geometries/poly3/clone.js +4 -1
- package/src/geometries/poly3/create.js +3 -11
- package/src/geometries/poly3/fromVerticesAndPlane.js +3 -1
- package/src/geometries/poly3/index.js +19 -4
- package/src/geometries/poly3/invert.js +4 -1
- package/src/geometries/poly3/isA.js +5 -1
- package/src/geometries/poly3/isConvex.js +5 -1
- package/src/geometries/poly3/measureArea.js +5 -1
- package/src/geometries/poly3/measureBoundingBox.js +4 -1
- package/src/geometries/poly3/measureBoundingSphere.js +4 -3
- package/src/geometries/poly3/measureSignedVolume.js +6 -1
- package/src/geometries/poly3/plane.js +6 -0
- package/src/geometries/poly3/toString.js +5 -1
- package/src/geometries/poly3/toVertices.js +6 -1
- package/src/geometries/poly3/transform.js +5 -1
- package/src/geometries/poly3/validate.js +6 -2
- package/src/geometries/slice/calculatePlane.js +3 -3
- package/src/geometries/slice/clone.js +4 -1
- package/src/geometries/slice/create.js +5 -10
- package/src/geometries/slice/equals.js +5 -1
- package/src/geometries/slice/fromGeom2.js +1 -1
- package/src/geometries/slice/fromVertices.js +3 -3
- package/src/geometries/slice/index.js +19 -4
- package/src/geometries/slice/isA.js +5 -1
- package/src/geometries/slice/reverse.js +5 -2
- package/src/geometries/slice/toEdges.js +5 -3
- package/src/geometries/slice/toPolygons.js +5 -1
- package/src/geometries/slice/toString.js +5 -1
- package/src/geometries/slice/toVertices.js +5 -3
- package/src/geometries/slice/transform.js +4 -3
- package/src/geometries/slice/validate.js +3 -2
- package/src/index.d.ts +1 -0
- package/src/index.js +4 -0
- package/src/maths/constants.js +11 -7
- package/src/maths/index.js +2 -1
- package/src/maths/mat4/isOnlyTransformScale.js +1 -1
- package/src/operations/booleans/index.js +2 -0
- package/src/operations/booleans/intersect.js +0 -1
- package/src/operations/booleans/scission.js +0 -1
- package/src/operations/booleans/trees/splitLineSegmentByPlane.js +1 -4
- package/src/operations/booleans/trees/splitPolygonByPlane.test.js +138 -0
- package/src/operations/booleans/unionGeom3.test.js +35 -0
- package/src/operations/extrusions/extrudeFromSlices.js +15 -5
- package/src/operations/extrusions/extrudeRotate.js +2 -1
- package/src/operations/extrusions/extrudeRotate.test.js +34 -0
- package/src/operations/extrusions/extrudeWalls.test.js +60 -0
- package/src/operations/minkowski/index.d.ts +1 -0
- package/src/operations/minkowski/index.js +15 -0
- package/src/operations/minkowski/minkowskiSum.d.ts +4 -0
- package/src/operations/minkowski/minkowskiSum.js +223 -0
- package/src/operations/minkowski/minkowskiSum.test.js +199 -0
- package/src/operations/modifiers/reTesselateCoplanarPolygons.js +10 -3
- package/src/operations/modifiers/reTesselateCoplanarPolygons.test.js +36 -1
- package/src/operations/modifiers/retessellate.js +4 -2
- package/src/operations/modifiers/snap.test.js +24 -15
- package/src/operations/offsets/offsetGeom3.test.js +5 -7
- package/src/primitives/arc.js +2 -2
- package/src/primitives/arc.test.js +104 -113
- package/src/utils/flatten.js +1 -1
- package/src/utils/flatten.test.js +94 -0
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import test from 'ava'
|
|
2
|
+
|
|
3
|
+
import { poly3 } from '../../../geometries/index.js'
|
|
4
|
+
import { plane } from '../../../maths/index.js'
|
|
5
|
+
|
|
6
|
+
import { splitPolygonByPlane } from './splitPolygonByPlane.js'
|
|
7
|
+
|
|
8
|
+
test('splitPolygonByPlane: test coplanar-front polygon returns type 0.', (t) => {
|
|
9
|
+
// Polygon in XY plane at z=0
|
|
10
|
+
const polygon = poly3.create([[0, 0, 0], [1, 0, 0], [1, 1, 0], [0, 1, 0]])
|
|
11
|
+
// Plane is also XY plane at z=0, normal pointing up
|
|
12
|
+
const splane = plane.fromPoints(plane.create(), [0, 0, 0], [1, 0, 0], [1, 1, 0])
|
|
13
|
+
|
|
14
|
+
const result = {}
|
|
15
|
+
splitPolygonByPlane(result, splane, polygon)
|
|
16
|
+
t.is(result.type, 0) // coplanar-front
|
|
17
|
+
t.is(result.front, undefined)
|
|
18
|
+
t.is(result.back, undefined)
|
|
19
|
+
})
|
|
20
|
+
|
|
21
|
+
test('splitPolygonByPlane: test polygon entirely in front returns type 2.', (t) => {
|
|
22
|
+
// Polygon at z=5
|
|
23
|
+
const polygon = poly3.create([[0, 0, 5], [1, 0, 5], [1, 1, 5], [0, 1, 5]])
|
|
24
|
+
// Plane at z=0
|
|
25
|
+
const splane = [0, 0, 1, 0] // normal (0,0,1), w=0
|
|
26
|
+
|
|
27
|
+
const result = {}
|
|
28
|
+
splitPolygonByPlane(result, splane, polygon)
|
|
29
|
+
t.is(result.type, 2) // front
|
|
30
|
+
t.is(result.front, undefined)
|
|
31
|
+
t.is(result.back, undefined)
|
|
32
|
+
})
|
|
33
|
+
|
|
34
|
+
test('splitPolygonByPlane: test polygon entirely in back returns type 3.', (t) => {
|
|
35
|
+
// Polygon at z=-5
|
|
36
|
+
const polygon = poly3.create([[0, 0, -5], [1, 0, -5], [1, 1, -5], [0, 1, -5]])
|
|
37
|
+
// Plane at z=0
|
|
38
|
+
const splane = [0, 0, 1, 0] // normal (0,0,1), w=0
|
|
39
|
+
|
|
40
|
+
const result = {}
|
|
41
|
+
splitPolygonByPlane(result, splane, polygon)
|
|
42
|
+
t.is(result.type, 3) // back
|
|
43
|
+
t.is(result.front, undefined)
|
|
44
|
+
t.is(result.back, undefined)
|
|
45
|
+
})
|
|
46
|
+
|
|
47
|
+
test('splitPolygonByPlane: test spanning polygon returns type 4 with front and back.', (t) => {
|
|
48
|
+
// Polygon spanning z=0 plane (from z=-1 to z=1)
|
|
49
|
+
const polygon = poly3.create([[0, 0, -1], [1, 0, -1], [1, 0, 1], [0, 0, 1]])
|
|
50
|
+
// Plane at z=0
|
|
51
|
+
const splane = [0, 0, 1, 0] // normal (0,0,1), w=0
|
|
52
|
+
|
|
53
|
+
const result = {}
|
|
54
|
+
splitPolygonByPlane(result, splane, polygon)
|
|
55
|
+
t.is(result.type, 4) // spanning
|
|
56
|
+
t.not(result.front, undefined)
|
|
57
|
+
t.not(result.back, undefined)
|
|
58
|
+
|
|
59
|
+
// Front polygon should have z >= 0
|
|
60
|
+
const frontPoints = poly3.toVertices(result.front)
|
|
61
|
+
t.true(frontPoints.length >= 3)
|
|
62
|
+
frontPoints.forEach((p) => {
|
|
63
|
+
t.true(p[2] >= -1e-5, `front point z=${p[2]} should be >= 0`)
|
|
64
|
+
})
|
|
65
|
+
|
|
66
|
+
// Back polygon should have z <= 0
|
|
67
|
+
const backPoints = poly3.toVertices(result.back)
|
|
68
|
+
t.true(backPoints.length >= 3)
|
|
69
|
+
backPoints.forEach((p) => {
|
|
70
|
+
t.true(p[2] <= 1e-5, `back point z=${p[2]} should be <= 0`)
|
|
71
|
+
})
|
|
72
|
+
})
|
|
73
|
+
|
|
74
|
+
test('splitPolygonByPlane: test duplicate vertices are removed from split result.', (t) => {
|
|
75
|
+
// Create a polygon that when split would produce duplicate vertices
|
|
76
|
+
// Triangle with one vertex on the plane
|
|
77
|
+
const polygon = poly3.create([[0, 0, 0], [1, 0, 1], [1, 0, -1]])
|
|
78
|
+
// Plane at z=0
|
|
79
|
+
const splane = [0, 0, 1, 0]
|
|
80
|
+
|
|
81
|
+
const result = {}
|
|
82
|
+
splitPolygonByPlane(result, splane, polygon)
|
|
83
|
+
t.is(result.type, 4) // spanning
|
|
84
|
+
|
|
85
|
+
// Verify no consecutive duplicate vertices in front
|
|
86
|
+
if (result.front) {
|
|
87
|
+
const frontPoints = poly3.toVertices(result.front)
|
|
88
|
+
for (let i = 0; i < frontPoints.length; i++) {
|
|
89
|
+
const curr = frontPoints[i]
|
|
90
|
+
const next = frontPoints[(i + 1) % frontPoints.length]
|
|
91
|
+
const dx = curr[0] - next[0]
|
|
92
|
+
const dy = curr[1] - next[1]
|
|
93
|
+
const dz = curr[2] - next[2]
|
|
94
|
+
const distSq = dx * dx + dy * dy + dz * dz
|
|
95
|
+
t.true(distSq > 1e-10, 'front polygon should not have duplicate consecutive vertices')
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
// Verify no consecutive duplicate vertices in back
|
|
100
|
+
if (result.back) {
|
|
101
|
+
const backPoints = poly3.toVertices(result.back)
|
|
102
|
+
for (let i = 0; i < backPoints.length; i++) {
|
|
103
|
+
const curr = backPoints[i]
|
|
104
|
+
const next = backPoints[(i + 1) % backPoints.length]
|
|
105
|
+
const dx = curr[0] - next[0]
|
|
106
|
+
const dy = curr[1] - next[1]
|
|
107
|
+
const dz = curr[2] - next[2]
|
|
108
|
+
const distSq = dx * dx + dy * dy + dz * dz
|
|
109
|
+
t.true(distSq > 1e-10, 'back polygon should not have duplicate consecutive vertices')
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
})
|
|
113
|
+
|
|
114
|
+
test('splitPolygonByPlane: test complex spanning polygon splits correctly.', (t) => {
|
|
115
|
+
// Hexagon spanning the XY plane
|
|
116
|
+
const polygon = poly3.create([
|
|
117
|
+
[1, 0, -1],
|
|
118
|
+
[0.5, 0.866, -1],
|
|
119
|
+
[-0.5, 0.866, 1],
|
|
120
|
+
[-1, 0, 1],
|
|
121
|
+
[-0.5, -0.866, 1],
|
|
122
|
+
[0.5, -0.866, -1]
|
|
123
|
+
])
|
|
124
|
+
// Plane at z=0
|
|
125
|
+
const splane = [0, 0, 1, 0]
|
|
126
|
+
|
|
127
|
+
const result = {}
|
|
128
|
+
splitPolygonByPlane(result, splane, polygon)
|
|
129
|
+
t.is(result.type, 4) // spanning
|
|
130
|
+
t.not(result.front, undefined)
|
|
131
|
+
t.not(result.back, undefined)
|
|
132
|
+
|
|
133
|
+
// Both resulting polygons should be valid (at least 3 vertices)
|
|
134
|
+
const frontPoints = poly3.toVertices(result.front)
|
|
135
|
+
const backPoints = poly3.toVertices(result.back)
|
|
136
|
+
t.true(frontPoints.length >= 3, 'front polygon should have at least 3 vertices')
|
|
137
|
+
t.true(backPoints.length >= 3, 'back polygon should have at least 3 vertices')
|
|
138
|
+
})
|
|
@@ -144,3 +144,38 @@ test('union of geom3 with rounding issues #137', (t) => {
|
|
|
144
144
|
t.is(measureVolume(result), 7779.201144000001)
|
|
145
145
|
t.is(pts.length, 6) // number of polygons in union
|
|
146
146
|
})
|
|
147
|
+
|
|
148
|
+
// Test for push loop optimization: verify union works correctly with multiple geometries
|
|
149
|
+
// This ensures the concat-to-push-loop optimization handles array merging properly
|
|
150
|
+
test('union of geom3 with multiple overlapping geometries', (t) => {
|
|
151
|
+
// Create several overlapping cuboids to generate a complex union
|
|
152
|
+
const geometry1 = cuboid({ size: [10, 10, 10] })
|
|
153
|
+
const geometry2 = center({ relativeTo: [5, 0, 0] }, cuboid({ size: [10, 10, 10] }))
|
|
154
|
+
const geometry3 = center({ relativeTo: [0, 5, 0] }, cuboid({ size: [10, 10, 10] }))
|
|
155
|
+
|
|
156
|
+
// Union should work correctly with multiple geometries
|
|
157
|
+
const obs = union(geometry1, geometry2, geometry3)
|
|
158
|
+
const pts = geom3.toVertices(obs)
|
|
159
|
+
|
|
160
|
+
// Skip manifold validation - focus on testing polygon merging works correctly
|
|
161
|
+
// (CSG on overlapping boxes can produce non-manifold edges at coplanar faces)
|
|
162
|
+
// Should produce a merged geometry with polygons from all inputs
|
|
163
|
+
t.true(pts.length > 6) // more than a single cube
|
|
164
|
+
})
|
|
165
|
+
|
|
166
|
+
// Test for push loop optimization: verify non-overlapping geometries combine correctly
|
|
167
|
+
test('union of multiple non-overlapping geom3 preserves all polygons', (t) => {
|
|
168
|
+
// Create multiple small cuboids that don't overlap
|
|
169
|
+
const cubes = []
|
|
170
|
+
for (let i = 0; i < 10; i++) {
|
|
171
|
+
cubes.push(center({ relativeTo: [i * 5, 0, 0] }, cuboid({ size: [2, 2, 2] })))
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
// Union all of them
|
|
175
|
+
const obs = union(...cubes)
|
|
176
|
+
const pts = geom3.toVertices(obs)
|
|
177
|
+
|
|
178
|
+
t.notThrows(() => geom3.validate(obs))
|
|
179
|
+
// Each cuboid has 6 faces, so 10 cuboids = 60 polygons
|
|
180
|
+
t.is(pts.length, 60)
|
|
181
|
+
})
|
|
@@ -59,7 +59,7 @@ export const extrudeFromSlices = (options, base) => {
|
|
|
59
59
|
let startSlice = null
|
|
60
60
|
let endSlice = null
|
|
61
61
|
let prevSlice = null
|
|
62
|
-
|
|
62
|
+
const polygons = []
|
|
63
63
|
for (let s = 0; s < numberOfSlices; s++) {
|
|
64
64
|
// invoke the callback function to get the next slice
|
|
65
65
|
// NOTE: callback can return null to skip the slice
|
|
@@ -71,7 +71,10 @@ export const extrudeFromSlices = (options, base) => {
|
|
|
71
71
|
if (currentSlice.contours.length === 0) throw new Error('the callback function must return slices with one or more contours')
|
|
72
72
|
|
|
73
73
|
if (prevSlice) {
|
|
74
|
-
|
|
74
|
+
const walls = extrudeWalls(prevSlice, currentSlice)
|
|
75
|
+
for (let i = 0; i < walls.length; i++) {
|
|
76
|
+
polygons.push(walls[i])
|
|
77
|
+
}
|
|
75
78
|
}
|
|
76
79
|
|
|
77
80
|
// save start and end slices for caps if necessary
|
|
@@ -85,17 +88,24 @@ export const extrudeFromSlices = (options, base) => {
|
|
|
85
88
|
if (capEnd) {
|
|
86
89
|
// create a cap at the end
|
|
87
90
|
const endPolygons = slice.toPolygons(endSlice)
|
|
88
|
-
|
|
91
|
+
for (let i = 0; i < endPolygons.length; i++) {
|
|
92
|
+
polygons.push(endPolygons[i])
|
|
93
|
+
}
|
|
89
94
|
}
|
|
90
95
|
if (capStart) {
|
|
91
96
|
// create a cap at the start
|
|
92
97
|
const startPolygons = slice.toPolygons(startSlice).map(poly3.invert)
|
|
93
|
-
|
|
98
|
+
for (let i = 0; i < startPolygons.length; i++) {
|
|
99
|
+
polygons.push(startPolygons[i])
|
|
100
|
+
}
|
|
94
101
|
}
|
|
95
102
|
if (!capStart && !capEnd) {
|
|
96
103
|
// create walls between end and start slices
|
|
97
104
|
if (close && !slice.equals(endSlice, startSlice)) {
|
|
98
|
-
|
|
105
|
+
const walls = extrudeWalls(endSlice, startSlice)
|
|
106
|
+
for (let i = 0; i < walls.length; i++) {
|
|
107
|
+
polygons.push(walls[i])
|
|
108
|
+
}
|
|
99
109
|
}
|
|
100
110
|
}
|
|
101
111
|
return geom3.create(polygons)
|
|
@@ -110,13 +110,14 @@ export const extrudeRotate = (options, geometry) => {
|
|
|
110
110
|
baseSlice = slice.reverse(baseSlice)
|
|
111
111
|
|
|
112
112
|
const matrix = mat4.create()
|
|
113
|
+
const xRotationMatrix = mat4.fromXRotation(mat4.create(), TAU / 4) // compute once, reuse
|
|
113
114
|
const createSlice = (progress, index, base) => {
|
|
114
115
|
let Zrotation = rotationPerSlice * index + startAngle
|
|
115
116
|
// fix rounding error when rotating TAU radians
|
|
116
117
|
if (totalRotation === TAU && index === segments) {
|
|
117
118
|
Zrotation = startAngle
|
|
118
119
|
}
|
|
119
|
-
mat4.multiply(matrix, mat4.fromZRotation(matrix, Zrotation),
|
|
120
|
+
mat4.multiply(matrix, mat4.fromZRotation(matrix, Zrotation), xRotationMatrix)
|
|
120
121
|
|
|
121
122
|
return slice.transform(matrix, base)
|
|
122
123
|
}
|
|
@@ -194,4 +194,38 @@ test('extrudeRotate: (overlap +/-) extruding of a geom2 produces an expected geo
|
|
|
194
194
|
t.true(comparePolygonsAsPoints(pts, exp))
|
|
195
195
|
})
|
|
196
196
|
|
|
197
|
+
// Test for mat4 reuse optimization: verify rotation matrices are computed correctly
|
|
198
|
+
// This ensures the optimization of computing xRotationMatrix once doesn't break anything
|
|
199
|
+
test('extrudeRotate: (mat4 reuse) rotation matrices produce correct geometry', (t) => {
|
|
200
|
+
// Simple rectangle that will be rotated to form a tube-like shape
|
|
201
|
+
const geometry2 = geom2.create([[[6, 1], [5, 1], [5, -1], [6, -1]]])
|
|
202
|
+
|
|
203
|
+
// Full rotation with many segments to test matrix reuse across iterations
|
|
204
|
+
const geometry3 = extrudeRotate({ segments: 32 }, geometry2)
|
|
205
|
+
const pts = geom3.toVertices(geometry3)
|
|
206
|
+
|
|
207
|
+
t.notThrows(() => geom3.validate(geometry3))
|
|
208
|
+
// 32 segments * 8 walls per segment (4 edges * 2 triangles) = 256 polygons
|
|
209
|
+
t.is(pts.length, 256)
|
|
210
|
+
|
|
211
|
+
// Verify the geometry is closed (first and last slices connect properly)
|
|
212
|
+
// This tests the Zrotation rounding error fix at index === segments
|
|
213
|
+
const obs = extrudeRotate({ segments: 16 }, geometry2)
|
|
214
|
+
t.notThrows(() => geom3.validate(obs))
|
|
215
|
+
t.is(measureArea(obs), 204.69587079560992)
|
|
216
|
+
t.is(measureVolume(obs), 67.35228409625583)
|
|
217
|
+
})
|
|
218
|
+
|
|
219
|
+
// Test for mat4 reuse with partial rotation (tests both capped and matrix reuse)
|
|
220
|
+
test('extrudeRotate: (mat4 reuse) partial rotation produces correct caps', (t) => {
|
|
221
|
+
const geometry2 = geom2.create([[[6, 1], [5, 1], [5, -1], [6, -1]]])
|
|
222
|
+
|
|
223
|
+
// Quarter rotation - should have start and end caps
|
|
224
|
+
const obs = extrudeRotate({ segments: 8, angle: TAU / 4 }, geometry2)
|
|
225
|
+
|
|
226
|
+
t.notThrows(() => geom3.validate(obs))
|
|
227
|
+
t.is(measureArea(obs), 53.232491234231944)
|
|
228
|
+
t.is(measureVolume(obs), 15.556349186104049)
|
|
229
|
+
})
|
|
230
|
+
|
|
197
231
|
// TEST HOLES
|
|
@@ -56,3 +56,63 @@ test('extrudeWalls (different shapes)', (t) => {
|
|
|
56
56
|
walls = extrudeWalls(slice3, slice.transform(matrix, slice2))
|
|
57
57
|
t.is(walls.length, 24)
|
|
58
58
|
})
|
|
59
|
+
|
|
60
|
+
// Test for vec3 reuse optimization in repartitionEdges
|
|
61
|
+
// When shapes have different edge counts, edges are repartitioned using vec3 operations
|
|
62
|
+
test('extrudeWalls (repartitionEdges vec3 reuse)', (t) => {
|
|
63
|
+
const matrix = mat4.fromTranslation(mat4.create(), [0, 0, 5])
|
|
64
|
+
|
|
65
|
+
// Triangle (3 edges)
|
|
66
|
+
const triangle = slice.create([[
|
|
67
|
+
[0, 10, 0], [-8.66, -5, 0], [8.66, -5, 0]
|
|
68
|
+
]])
|
|
69
|
+
|
|
70
|
+
// Hexagon (6 edges) - LCM with triangle is 6, so triangle edges get split
|
|
71
|
+
const hexagon = slice.create([[
|
|
72
|
+
[0, 10, 0], [-8.66, 5, 0],
|
|
73
|
+
[-8.66, -5, 0],
|
|
74
|
+
[0, -10, 0],
|
|
75
|
+
[8.66, -5, 0],
|
|
76
|
+
[8.66, 5, 0]
|
|
77
|
+
]])
|
|
78
|
+
|
|
79
|
+
// Triangle to hexagon requires repartitioning (3 -> 6 edges)
|
|
80
|
+
// This exercises the vec3 reuse optimization in repartitionEdges
|
|
81
|
+
const walls = extrudeWalls(triangle, slice.transform(matrix, hexagon))
|
|
82
|
+
|
|
83
|
+
// 6 edges * 2 triangles per edge = 12 wall polygons
|
|
84
|
+
t.is(walls.length, 12)
|
|
85
|
+
|
|
86
|
+
// Verify all walls are valid triangles
|
|
87
|
+
walls.forEach((wall) => {
|
|
88
|
+
t.is(wall.vertices.length, 3)
|
|
89
|
+
})
|
|
90
|
+
})
|
|
91
|
+
|
|
92
|
+
// Test for vec3 reuse with higher repartition multiple
|
|
93
|
+
test('extrudeWalls (repartitionEdges with high multiple)', (t) => {
|
|
94
|
+
const matrix = mat4.fromTranslation(mat4.create(), [0, 0, 10])
|
|
95
|
+
|
|
96
|
+
// Square (4 edges)
|
|
97
|
+
const square = slice.create([[
|
|
98
|
+
[-5, 5, 0], [-5, -5, 0], [5, -5, 0], [5, 5, 0]
|
|
99
|
+
]])
|
|
100
|
+
|
|
101
|
+
// Octagon (8 edges) - LCM with square is 8, so square edges get doubled
|
|
102
|
+
const octagon = slice.create([[
|
|
103
|
+
[0, 5, 0],
|
|
104
|
+
[-3.54, 3.54, 0],
|
|
105
|
+
[-5, 0, 0],
|
|
106
|
+
[-3.54, -3.54, 0],
|
|
107
|
+
[0, -5, 0],
|
|
108
|
+
[3.54, -3.54, 0],
|
|
109
|
+
[5, 0, 0],
|
|
110
|
+
[3.54, 3.54, 0]
|
|
111
|
+
]])
|
|
112
|
+
|
|
113
|
+
// Square to octagon requires repartitioning (4 -> 8 edges)
|
|
114
|
+
const walls = extrudeWalls(square, slice.transform(matrix, octagon))
|
|
115
|
+
|
|
116
|
+
// 8 edges * 2 triangles per edge = 16 wall polygons
|
|
117
|
+
t.is(walls.length, 16)
|
|
118
|
+
})
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { minkowskiSum } from './minkowskiSum'
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Minkowski sum operations for 3D geometries.
|
|
3
|
+
*
|
|
4
|
+
* The Minkowski sum of two shapes A and B is the set of all points that are
|
|
5
|
+
* the sum of a point in A and a point in B. This is useful for:
|
|
6
|
+
* - Offsetting/inflating shapes (using a sphere creates rounded edges)
|
|
7
|
+
* - Collision detection (shapes collide iff their Minkowski difference contains origin)
|
|
8
|
+
* - Motion planning and swept volumes
|
|
9
|
+
*
|
|
10
|
+
* @module modeling/minkowski
|
|
11
|
+
* @example
|
|
12
|
+
* import { minkowskiSum } from '@jscad/modeling'
|
|
13
|
+
* const rounded = minkowskiSum(cube, sphere)
|
|
14
|
+
*/
|
|
15
|
+
export { minkowskiSum } from './minkowskiSum.js'
|
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
import { flatten } from '../../utils/flatten.js'
|
|
2
|
+
|
|
3
|
+
import { geom3, poly3 } from '../../geometries/index.js'
|
|
4
|
+
|
|
5
|
+
import { hullPoints3 } from '../hulls/hullPoints3.js'
|
|
6
|
+
import { unionGeom3 } from '../booleans/unionGeom3.js'
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Compute the Minkowski sum of two 3D geometries.
|
|
10
|
+
*
|
|
11
|
+
* The Minkowski sum A ⊕ B is the set of all points a + b where a ∈ A and b ∈ B.
|
|
12
|
+
* Geometrically, this "inflates" geometry A by the shape of geometry B.
|
|
13
|
+
*
|
|
14
|
+
* Common use cases:
|
|
15
|
+
* - Offset a solid by a sphere to round all edges and corners
|
|
16
|
+
* - Offset a solid by a cube to create chamfered edges
|
|
17
|
+
* - Collision detection (if Minkowski sum contains origin, shapes overlap)
|
|
18
|
+
*
|
|
19
|
+
* For best performance, use convex geometries. Non-convex geometries are supported
|
|
20
|
+
* when the second operand is convex, but require decomposition and are slower.
|
|
21
|
+
*
|
|
22
|
+
* @param {...Object} geometries - two geom3 geometries (second should be convex for non-convex first)
|
|
23
|
+
* @returns {geom3} new 3D geometry representing the Minkowski sum
|
|
24
|
+
* @alias module:modeling/minkowski.minkowskiSum
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* const { primitives, minkowski } = require('@jscad/modeling')
|
|
28
|
+
* const cube = primitives.cuboid({ size: [10, 10, 10] })
|
|
29
|
+
* const sphere = primitives.sphere({ radius: 2, segments: 16 })
|
|
30
|
+
* const rounded = minkowski.minkowskiSum(cube, sphere)
|
|
31
|
+
*/
|
|
32
|
+
export const minkowskiSum = (...geometries) => {
|
|
33
|
+
geometries = flatten(geometries)
|
|
34
|
+
|
|
35
|
+
if (geometries.length !== 2) {
|
|
36
|
+
throw new Error('minkowskiSum requires exactly two geometries')
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const [geomA, geomB] = geometries
|
|
40
|
+
|
|
41
|
+
if (!geom3.isA(geomA) || !geom3.isA(geomB)) {
|
|
42
|
+
throw new Error('minkowskiSum requires geom3 geometries')
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const aConvex = geom3.isConvex(geomA)
|
|
46
|
+
const bConvex = geom3.isConvex(geomB)
|
|
47
|
+
|
|
48
|
+
// Fast path: both convex
|
|
49
|
+
if (aConvex && bConvex) {
|
|
50
|
+
return minkowskiSumConvex(geomA, geomB)
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// Non-convex A + convex B: decompose A into tetrahedra
|
|
54
|
+
if (!aConvex && bConvex) {
|
|
55
|
+
return minkowskiSumNonConvexConvex(geomA, geomB)
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// Convex A + non-convex B: swap operands (Minkowski sum is commutative)
|
|
59
|
+
if (aConvex && !bConvex) {
|
|
60
|
+
return minkowskiSumNonConvexConvex(geomB, geomA)
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// Both non-convex: not yet supported
|
|
64
|
+
throw new Error('minkowskiSum of two non-convex geometries is not yet supported')
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/*
|
|
68
|
+
* Compute Minkowski sum of non-convex A with convex B.
|
|
69
|
+
*
|
|
70
|
+
* Decomposes A into tetrahedra, computes Minkowski sum of each with B,
|
|
71
|
+
* then unions all results.
|
|
72
|
+
*/
|
|
73
|
+
const minkowskiSumNonConvexConvex = (geomA, geomB) => {
|
|
74
|
+
const tetrahedra = decomposeIntoTetrahedra(geomA)
|
|
75
|
+
|
|
76
|
+
if (tetrahedra.length === 0) {
|
|
77
|
+
return geom3.create()
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// Compute Minkowski sum for each tetrahedron
|
|
81
|
+
const parts = tetrahedra.map((tet) => minkowskiSumConvex(tet, geomB))
|
|
82
|
+
|
|
83
|
+
// Union all parts using internal unionGeom3
|
|
84
|
+
if (parts.length === 1) {
|
|
85
|
+
return parts[0]
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
return unionGeom3(parts)
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/*
|
|
92
|
+
* Decompose a geom3 into tetrahedra using face-local apex points.
|
|
93
|
+
* Each resulting tetrahedron is guaranteed to be convex.
|
|
94
|
+
*
|
|
95
|
+
* Unlike centroid-based decomposition, this approach works correctly for
|
|
96
|
+
* shapes where the centroid is outside the geometry (e.g., torus, U-shapes).
|
|
97
|
+
* Each polygon gets its own apex point, offset inward along its normal.
|
|
98
|
+
*/
|
|
99
|
+
const decomposeIntoTetrahedra = (geometry) => {
|
|
100
|
+
const polygons = geom3.toPolygons(geometry)
|
|
101
|
+
|
|
102
|
+
if (polygons.length === 0) {
|
|
103
|
+
return []
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
const tetrahedra = []
|
|
107
|
+
|
|
108
|
+
// For each polygon, compute a face-local apex and create tetrahedra
|
|
109
|
+
for (let i = 0; i < polygons.length; i++) {
|
|
110
|
+
const polygon = polygons[i]
|
|
111
|
+
const vertices = polygon.vertices
|
|
112
|
+
|
|
113
|
+
// Compute polygon center
|
|
114
|
+
let cx = 0
|
|
115
|
+
let cy = 0
|
|
116
|
+
let cz = 0
|
|
117
|
+
for (let k = 0; k < vertices.length; k++) {
|
|
118
|
+
cx += vertices[k][0]
|
|
119
|
+
cy += vertices[k][1]
|
|
120
|
+
cz += vertices[k][2]
|
|
121
|
+
}
|
|
122
|
+
cx /= vertices.length
|
|
123
|
+
cy /= vertices.length
|
|
124
|
+
cz /= vertices.length
|
|
125
|
+
|
|
126
|
+
// Get polygon plane (normal + offset)
|
|
127
|
+
const plane = poly3.plane(polygon)
|
|
128
|
+
const nx = plane[0]
|
|
129
|
+
const ny = plane[1]
|
|
130
|
+
const nz = plane[2]
|
|
131
|
+
|
|
132
|
+
// Offset inward along negative normal to create face-local apex
|
|
133
|
+
// The normal points outward, so we go in the negative direction
|
|
134
|
+
// Use a small offset - the actual distance doesn't matter much
|
|
135
|
+
// as long as the apex is on the interior side of the face
|
|
136
|
+
const offset = 0.1
|
|
137
|
+
const apex = [ // Vertex used as apex in tetrahedron polygons below
|
|
138
|
+
cx - nx * offset,
|
|
139
|
+
cy - ny * offset,
|
|
140
|
+
cz - nz * offset
|
|
141
|
+
]
|
|
142
|
+
|
|
143
|
+
// Fan triangulate the polygon and create tetrahedra from apex
|
|
144
|
+
for (let j = 1; j < vertices.length - 1; j++) {
|
|
145
|
+
const v0 = vertices[0]
|
|
146
|
+
const v1 = vertices[j]
|
|
147
|
+
const v2 = vertices[j + 1]
|
|
148
|
+
|
|
149
|
+
// Create tetrahedron from apex and triangle
|
|
150
|
+
const tetPolygons = createTetrahedronPolygons(apex, v0, v1, v2)
|
|
151
|
+
tetrahedra.push(geom3.create(tetPolygons))
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
return tetrahedra
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/*
|
|
159
|
+
* Create the 4 triangular faces of a tetrahedron.
|
|
160
|
+
*
|
|
161
|
+
* Tetrahedron has 4 faces, each a triangle
|
|
162
|
+
*/
|
|
163
|
+
const createTetrahedronPolygons = (p0, p1, p2, p3) => [
|
|
164
|
+
poly3.create([p0, p2, p1]), // base seen from p3
|
|
165
|
+
poly3.create([p0, p1, p3]), // face opposite p2
|
|
166
|
+
poly3.create([p1, p2, p3]), // face opposite p0
|
|
167
|
+
poly3.create([p2, p0, p3]) // face opposite p1
|
|
168
|
+
]
|
|
169
|
+
|
|
170
|
+
/*
|
|
171
|
+
* Compute Minkowski sum of two convex polyhedra.
|
|
172
|
+
*
|
|
173
|
+
* For convex polyhedra, the Minkowski sum equals the convex hull of
|
|
174
|
+
* all pairwise vertex sums. This is O(n*m) for n and m vertices,
|
|
175
|
+
* plus the cost of the convex hull algorithm.
|
|
176
|
+
*/
|
|
177
|
+
const minkowskiSumConvex = (geomA, geomB) => {
|
|
178
|
+
const pointsA = extractUniqueVertices(geomA)
|
|
179
|
+
const pointsB = extractUniqueVertices(geomB)
|
|
180
|
+
|
|
181
|
+
if (pointsA.length === 0 || pointsB.length === 0) {
|
|
182
|
+
return geom3.create()
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
// Compute all pairwise sums
|
|
186
|
+
const summedPoints = []
|
|
187
|
+
for (let i = 0; i < pointsA.length; i++) {
|
|
188
|
+
const a = pointsA[i]
|
|
189
|
+
for (let j = 0; j < pointsB.length; j++) {
|
|
190
|
+
const b = pointsB[j]
|
|
191
|
+
summedPoints.push([a[0] + b[0], a[1] + b[1], a[2] + b[2]])
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
// Compute convex hull of the summed points
|
|
196
|
+
const hullPolygons = hullPoints3(summedPoints)
|
|
197
|
+
|
|
198
|
+
return geom3.create(hullPolygons)
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/*
|
|
202
|
+
* Extract unique vertices from a geom3.
|
|
203
|
+
* Uses a Set with string keys for deduplication.
|
|
204
|
+
*/
|
|
205
|
+
const extractUniqueVertices = (geometry) => {
|
|
206
|
+
const found = new Set()
|
|
207
|
+
const unique = []
|
|
208
|
+
|
|
209
|
+
const polygons = geom3.toPolygons(geometry)
|
|
210
|
+
for (let i = 0; i < polygons.length; i++) {
|
|
211
|
+
const vertices = polygons[i].vertices
|
|
212
|
+
for (let j = 0; j < vertices.length; j++) {
|
|
213
|
+
const v = vertices[j]
|
|
214
|
+
const key = `${v[0]},${v[1]},${v[2]}`
|
|
215
|
+
if (!found.has(key)) {
|
|
216
|
+
found.add(key)
|
|
217
|
+
unique.push(v)
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
return unique
|
|
223
|
+
}
|