@jscad/modeling 2.8.0 → 2.9.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 +11 -0
- package/dist/jscad-modeling.min.js +159 -132
- package/package.json +2 -2
- package/src/geometries/poly3/invert.js +7 -1
- package/src/operations/expansions/expand.test.js +1 -1
- package/src/operations/expansions/expandShell.js +2 -2
- package/src/operations/extrusions/earcut/assignHoles.js +87 -0
- package/src/operations/extrusions/earcut/assignHoles.test.js +28 -0
- package/src/operations/extrusions/earcut/eliminateHoles.js +131 -0
- package/src/operations/extrusions/earcut/index.js +252 -0
- package/src/operations/extrusions/earcut/linkedList.js +58 -0
- package/src/operations/extrusions/earcut/linkedListSort.js +54 -0
- package/src/operations/extrusions/earcut/linkedPolygon.js +197 -0
- package/src/operations/extrusions/earcut/polygonHierarchy.js +64 -0
- package/src/operations/extrusions/earcut/triangle.js +16 -0
- package/src/operations/extrusions/extrudeFromSlices.js +10 -3
- package/src/operations/extrusions/extrudeFromSlices.test.js +33 -23
- package/src/operations/extrusions/extrudeLinear.js +4 -3
- package/src/operations/extrusions/extrudeLinear.test.js +54 -28
- package/src/operations/extrusions/extrudeLinearGeom2.js +5 -2
- package/src/operations/extrusions/extrudeRectangular.test.js +7 -7
- package/src/operations/extrusions/extrudeRotate.test.js +19 -27
- package/src/operations/extrusions/slice/calculatePlane.js +7 -4
- package/src/operations/extrusions/slice/repairSlice.js +47 -0
- package/src/operations/extrusions/slice/toPolygons.js +24 -60
- package/src/primitives/torus.test.js +1 -1
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
const { Node, insertNode, removeNode } = require('./linkedList')
|
|
2
|
+
const { area } = require('./triangle')
|
|
3
|
+
|
|
4
|
+
/*
|
|
5
|
+
* create a circular doubly linked list from polygon points in the specified winding order
|
|
6
|
+
*/
|
|
7
|
+
const linkedPolygon = (data, start, end, dim, clockwise) => {
|
|
8
|
+
let last
|
|
9
|
+
|
|
10
|
+
if (clockwise === (signedArea(data, start, end, dim) > 0)) {
|
|
11
|
+
for (let i = start; i < end; i += dim) {
|
|
12
|
+
last = insertNode(i, data[i], data[i + 1], last)
|
|
13
|
+
}
|
|
14
|
+
} else {
|
|
15
|
+
for (let i = end - dim; i >= start; i -= dim) {
|
|
16
|
+
last = insertNode(i, data[i], data[i + 1], last)
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
if (last && equals(last, last.next)) {
|
|
21
|
+
removeNode(last)
|
|
22
|
+
last = last.next
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
return last
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/*
|
|
29
|
+
* eliminate colinear or duplicate points
|
|
30
|
+
*/
|
|
31
|
+
const filterPoints = (start, end) => {
|
|
32
|
+
if (!start) return start
|
|
33
|
+
if (!end) end = start
|
|
34
|
+
|
|
35
|
+
let p = start
|
|
36
|
+
let again
|
|
37
|
+
do {
|
|
38
|
+
again = false
|
|
39
|
+
|
|
40
|
+
if (!p.steiner && (equals(p, p.next) || area(p.prev, p, p.next) === 0)) {
|
|
41
|
+
removeNode(p)
|
|
42
|
+
p = end = p.prev
|
|
43
|
+
if (p === p.next) break
|
|
44
|
+
again = true
|
|
45
|
+
} else {
|
|
46
|
+
p = p.next
|
|
47
|
+
}
|
|
48
|
+
} while (again || p !== end)
|
|
49
|
+
|
|
50
|
+
return end
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/*
|
|
54
|
+
* go through all polygon nodes and cure small local self-intersections
|
|
55
|
+
*/
|
|
56
|
+
const cureLocalIntersections = (start, triangles, dim) => {
|
|
57
|
+
let p = start
|
|
58
|
+
do {
|
|
59
|
+
const a = p.prev
|
|
60
|
+
const b = p.next.next
|
|
61
|
+
|
|
62
|
+
if (!equals(a, b) && intersects(a, p, p.next, b) && locallyInside(a, b) && locallyInside(b, a)) {
|
|
63
|
+
triangles.push(a.i / dim)
|
|
64
|
+
triangles.push(p.i / dim)
|
|
65
|
+
triangles.push(b.i / dim)
|
|
66
|
+
|
|
67
|
+
// remove two nodes involved
|
|
68
|
+
removeNode(p)
|
|
69
|
+
removeNode(p.next)
|
|
70
|
+
|
|
71
|
+
p = start = b
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
p = p.next
|
|
75
|
+
} while (p !== start)
|
|
76
|
+
|
|
77
|
+
return filterPoints(p)
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/*
|
|
81
|
+
* check if a polygon diagonal intersects any polygon segments
|
|
82
|
+
*/
|
|
83
|
+
const intersectsPolygon = (a, b) => {
|
|
84
|
+
let p = a
|
|
85
|
+
do {
|
|
86
|
+
if (p.i !== a.i && p.next.i !== a.i && p.i !== b.i && p.next.i !== b.i &&
|
|
87
|
+
intersects(p, p.next, a, b)) return true
|
|
88
|
+
p = p.next
|
|
89
|
+
} while (p !== a)
|
|
90
|
+
|
|
91
|
+
return false
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/*
|
|
95
|
+
* check if a polygon diagonal is locally inside the polygon
|
|
96
|
+
*/
|
|
97
|
+
const locallyInside = (a, b) => area(a.prev, a, a.next) < 0
|
|
98
|
+
? area(a, b, a.next) >= 0 && area(a, a.prev, b) >= 0
|
|
99
|
+
: area(a, b, a.prev) < 0 || area(a, a.next, b) < 0
|
|
100
|
+
|
|
101
|
+
/*
|
|
102
|
+
* check if the middle point of a polygon diagonal is inside the polygon
|
|
103
|
+
*/
|
|
104
|
+
const middleInside = (a, b) => {
|
|
105
|
+
let p = a
|
|
106
|
+
let inside = false
|
|
107
|
+
const px = (a.x + b.x) / 2
|
|
108
|
+
const py = (a.y + b.y) / 2
|
|
109
|
+
do {
|
|
110
|
+
if (((p.y > py) !== (p.next.y > py)) && p.next.y !== p.y &&
|
|
111
|
+
(px < (p.next.x - p.x) * (py - p.y) / (p.next.y - p.y) + p.x)) { inside = !inside }
|
|
112
|
+
p = p.next
|
|
113
|
+
} while (p !== a)
|
|
114
|
+
|
|
115
|
+
return inside
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/*
|
|
119
|
+
* link two polygon vertices with a bridge; if the vertices belong to the same ring, it splits polygon into two
|
|
120
|
+
* if one belongs to the outer ring and another to a hole, it merges it into a single ring
|
|
121
|
+
*/
|
|
122
|
+
const splitPolygon = (a, b) => {
|
|
123
|
+
const a2 = new Node(a.i, a.x, a.y)
|
|
124
|
+
const b2 = new Node(b.i, b.x, b.y)
|
|
125
|
+
const an = a.next
|
|
126
|
+
const bp = b.prev
|
|
127
|
+
|
|
128
|
+
a.next = b
|
|
129
|
+
b.prev = a
|
|
130
|
+
|
|
131
|
+
a2.next = an
|
|
132
|
+
an.prev = a2
|
|
133
|
+
|
|
134
|
+
b2.next = a2
|
|
135
|
+
a2.prev = b2
|
|
136
|
+
|
|
137
|
+
bp.next = b2
|
|
138
|
+
b2.prev = bp
|
|
139
|
+
|
|
140
|
+
return b2
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/*
|
|
144
|
+
* check if a diagonal between two polygon nodes is valid (lies in polygon interior)
|
|
145
|
+
*/
|
|
146
|
+
const isValidDiagonal = (a, b) => a.next.i !== b.i &&
|
|
147
|
+
a.prev.i !== b.i &&
|
|
148
|
+
!intersectsPolygon(a, b) && // doesn't intersect other edges
|
|
149
|
+
(
|
|
150
|
+
locallyInside(a, b) && locallyInside(b, a) && middleInside(a, b) && // locally visible
|
|
151
|
+
(area(a.prev, a, b.prev) || area(a, b.prev, b)) || // does not create opposite-facing sectors
|
|
152
|
+
equals(a, b) && area(a.prev, a, a.next) > 0 && area(b.prev, b, b.next) > 0
|
|
153
|
+
)
|
|
154
|
+
|
|
155
|
+
/*
|
|
156
|
+
* check if two segments intersect
|
|
157
|
+
*/
|
|
158
|
+
const intersects = (p1, q1, p2, q2) => {
|
|
159
|
+
const o1 = Math.sign(area(p1, q1, p2))
|
|
160
|
+
const o2 = Math.sign(area(p1, q1, q2))
|
|
161
|
+
const o3 = Math.sign(area(p2, q2, p1))
|
|
162
|
+
const o4 = Math.sign(area(p2, q2, q1))
|
|
163
|
+
|
|
164
|
+
if (o1 !== o2 && o3 !== o4) return true // general case
|
|
165
|
+
|
|
166
|
+
if (o1 === 0 && onSegment(p1, p2, q1)) return true // p1, q1 and p2 are colinear and p2 lies on p1q1
|
|
167
|
+
if (o2 === 0 && onSegment(p1, q2, q1)) return true // p1, q1 and q2 are colinear and q2 lies on p1q1
|
|
168
|
+
if (o3 === 0 && onSegment(p2, p1, q2)) return true // p2, q2 and p1 are colinear and p1 lies on p2q2
|
|
169
|
+
if (o4 === 0 && onSegment(p2, q1, q2)) return true // p2, q2 and q1 are colinear and q1 lies on p2q2
|
|
170
|
+
|
|
171
|
+
return false
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/*
|
|
175
|
+
* for colinear points p, q, r, check if point q lies on segment pr
|
|
176
|
+
*/
|
|
177
|
+
const onSegment = (p, q, r) => q.x <= Math.max(p.x, r.x) &&
|
|
178
|
+
q.x >= Math.min(p.x, r.x) &&
|
|
179
|
+
q.y <= Math.max(p.y, r.y) &&
|
|
180
|
+
q.y >= Math.min(p.y, r.y)
|
|
181
|
+
|
|
182
|
+
const signedArea = (data, start, end, dim) => {
|
|
183
|
+
let sum = 0
|
|
184
|
+
for (let i = start, j = end - dim; i < end; i += dim) {
|
|
185
|
+
sum += (data[j] - data[i]) * (data[i + 1] + data[j + 1])
|
|
186
|
+
j = i
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
return sum
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/*
|
|
193
|
+
* check if two points are equal
|
|
194
|
+
*/
|
|
195
|
+
const equals = (p1, p2) => p1.x === p2.x && p1.y === p2.y
|
|
196
|
+
|
|
197
|
+
module.exports = { cureLocalIntersections, filterPoints, isValidDiagonal, linkedPolygon, locallyInside, splitPolygon }
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
const geom2 = require('../../../geometries/geom2')
|
|
2
|
+
const plane = require('../../../maths/plane')
|
|
3
|
+
const vec2 = require('../../../maths/vec2')
|
|
4
|
+
const vec3 = require('../../../maths/vec3')
|
|
5
|
+
const calculatePlane = require('../slice/calculatePlane')
|
|
6
|
+
const assignHoles = require('./assignHoles')
|
|
7
|
+
|
|
8
|
+
/*
|
|
9
|
+
* Constructs a polygon hierarchy which associates holes with their outer solids.
|
|
10
|
+
* This class maps a 3D polygon onto a 2D space using an orthonormal basis.
|
|
11
|
+
* It tracks the mapping so that points can be reversed back to 3D losslessly.
|
|
12
|
+
*/
|
|
13
|
+
class PolygonHierarchy {
|
|
14
|
+
constructor (slice) {
|
|
15
|
+
this.plane = calculatePlane(slice)
|
|
16
|
+
|
|
17
|
+
// create an orthonormal basis
|
|
18
|
+
// choose an arbitrary right hand vector, making sure it is somewhat orthogonal to the plane normal
|
|
19
|
+
const rightvector = vec3.orthogonal(vec3.create(), this.plane)
|
|
20
|
+
const perp = vec3.cross(vec3.create(), this.plane, rightvector)
|
|
21
|
+
this.v = vec3.normalize(perp, perp)
|
|
22
|
+
this.u = vec3.cross(vec3.create(), this.v, this.plane)
|
|
23
|
+
|
|
24
|
+
// map from 2D to original 3D points
|
|
25
|
+
this.basisMap = new Map()
|
|
26
|
+
|
|
27
|
+
// project slice onto 2D plane
|
|
28
|
+
const projected = slice.edges.map((e) => e.map((v) => this.to2D(v)))
|
|
29
|
+
|
|
30
|
+
// compute polygon hierarchies, assign holes to solids
|
|
31
|
+
const geometry = geom2.create(projected)
|
|
32
|
+
this.roots = assignHoles(geometry)
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/*
|
|
36
|
+
* project a 3D point onto the 2D plane
|
|
37
|
+
*/
|
|
38
|
+
to2D (vector3) {
|
|
39
|
+
const vector2 = vec2.fromValues(vec3.dot(vector3, this.u), vec3.dot(vector3, this.v))
|
|
40
|
+
this.basisMap.set(vector2, vector3)
|
|
41
|
+
return vector2
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/*
|
|
45
|
+
* un-project a 2D point back into 3D
|
|
46
|
+
*/
|
|
47
|
+
to3D (vector2) {
|
|
48
|
+
// use a map to get the original 3D, no floating point error
|
|
49
|
+
const original = this.basisMap.get(vector2)
|
|
50
|
+
if (original) {
|
|
51
|
+
return original
|
|
52
|
+
} else {
|
|
53
|
+
console.log('Warning: point not in original slice')
|
|
54
|
+
const v1 = vec3.scale(vec3.create(), this.u, vector2[0])
|
|
55
|
+
const v2 = vec3.scale(vec3.create(), this.v, vector2[1])
|
|
56
|
+
|
|
57
|
+
const planeOrigin = vec3.scale(vec3.create(), plane, plane[3])
|
|
58
|
+
const v3 = vec3.add(v1, v1, planeOrigin)
|
|
59
|
+
return vec3.add(v2, v2, v3)
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
module.exports = PolygonHierarchy
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
|
|
2
|
+
/*
|
|
3
|
+
* check if a point lies within a convex triangle
|
|
4
|
+
*/
|
|
5
|
+
const pointInTriangle = (ax, ay, bx, by, cx, cy, px, py) => (
|
|
6
|
+
(cx - px) * (ay - py) - (ax - px) * (cy - py) >= 0 &&
|
|
7
|
+
(ax - px) * (by - py) - (bx - px) * (ay - py) >= 0 &&
|
|
8
|
+
(bx - px) * (cy - py) - (cx - px) * (by - py) >= 0
|
|
9
|
+
)
|
|
10
|
+
|
|
11
|
+
/*
|
|
12
|
+
* signed area of a triangle
|
|
13
|
+
*/
|
|
14
|
+
const area = (p, q, r) => (q.y - p.y) * (r.x - q.x) - (q.x - p.x) * (r.y - q.y)
|
|
15
|
+
|
|
16
|
+
module.exports = { area, pointInTriangle }
|
|
@@ -5,6 +5,7 @@ const geom3 = require('../../geometries/geom3')
|
|
|
5
5
|
const poly3 = require('../../geometries/poly3')
|
|
6
6
|
|
|
7
7
|
const slice = require('./slice')
|
|
8
|
+
const repairSlice = require('./slice/repairSlice')
|
|
8
9
|
|
|
9
10
|
const extrudeWalls = require('./extrudeWalls')
|
|
10
11
|
|
|
@@ -25,6 +26,7 @@ const defaultCallback = (progress, index, base) => {
|
|
|
25
26
|
* @param {Boolean} [options.capStart=true] the solid should have a cap at the start
|
|
26
27
|
* @param {Boolean} [options.capEnd=true] the solid should have a cap at the end
|
|
27
28
|
* @param {Boolean} [options.close=false] the solid should have a closing section between start and end
|
|
29
|
+
* @param {Boolean} [options.repair=true] - repair gaps in the geometry
|
|
28
30
|
* @param {Function} [options.callback] the callback function that generates each slice
|
|
29
31
|
* @param {Object} base - the base object which is used to create slices (see the example for callback information)
|
|
30
32
|
* @return {geom3} the extruded shape
|
|
@@ -48,12 +50,18 @@ const extrudeFromSlices = (options, base) => {
|
|
|
48
50
|
capStart: true,
|
|
49
51
|
capEnd: true,
|
|
50
52
|
close: false,
|
|
53
|
+
repair: true,
|
|
51
54
|
callback: defaultCallback
|
|
52
55
|
}
|
|
53
|
-
const { numberOfSlices, capStart, capEnd, close, callback: generate } = Object.assign({ }, defaults, options)
|
|
56
|
+
const { numberOfSlices, capStart, capEnd, close, repair, callback: generate } = Object.assign({ }, defaults, options)
|
|
54
57
|
|
|
55
58
|
if (numberOfSlices < 2) throw new Error('numberOfSlices must be 2 or more')
|
|
56
59
|
|
|
60
|
+
// Repair gaps in the base slice
|
|
61
|
+
if (repair) {
|
|
62
|
+
repairSlice(base)
|
|
63
|
+
}
|
|
64
|
+
|
|
57
65
|
const sMax = numberOfSlices - 1
|
|
58
66
|
|
|
59
67
|
let startSlice = null
|
|
@@ -90,8 +98,7 @@ const extrudeFromSlices = (options, base) => {
|
|
|
90
98
|
}
|
|
91
99
|
if (capStart) {
|
|
92
100
|
// create a cap at the start
|
|
93
|
-
slice.
|
|
94
|
-
const startPolygons = slice.toPolygons(startSlice)
|
|
101
|
+
const startPolygons = slice.toPolygons(startSlice).map(poly3.invert)
|
|
95
102
|
polygons = polygons.concat(startPolygons)
|
|
96
103
|
}
|
|
97
104
|
if (!capStart && !capEnd) {
|
|
@@ -16,25 +16,27 @@ test('extrudeFromSlices (defaults)', (t) => {
|
|
|
16
16
|
let geometry3 = extrudeFromSlices({ }, geometry2)
|
|
17
17
|
let pts = geom3.toPoints(geometry3)
|
|
18
18
|
const exp = [
|
|
19
|
-
[[10
|
|
20
|
-
[[10
|
|
21
|
-
[[10
|
|
22
|
-
[[10
|
|
23
|
-
[[-10
|
|
24
|
-
[[-10
|
|
25
|
-
[[-10
|
|
26
|
-
[[-10
|
|
27
|
-
[[10,
|
|
28
|
-
[[10,
|
|
19
|
+
[[10, -10, 0], [10, 10, 0], [10, 10, 1]],
|
|
20
|
+
[[10, -10, 0], [10, 10, 1], [10, -10, 1]],
|
|
21
|
+
[[10, 10, 0], [-10, 10, 0], [-10, 10, 1]],
|
|
22
|
+
[[10, 10, 0], [-10, 10, 1], [10, 10, 1]],
|
|
23
|
+
[[-10, 10, 0], [-10, -10, 0], [-10, -10, 1]],
|
|
24
|
+
[[-10, 10, 0], [-10, -10, 1], [-10, 10, 1]],
|
|
25
|
+
[[-10, -10, 0], [10, -10, 0], [10, -10, 1]],
|
|
26
|
+
[[-10, -10, 0], [10, -10, 1], [-10, -10, 1]],
|
|
27
|
+
[[-10, -10, 1], [10, -10, 1], [10, 10, 1]],
|
|
28
|
+
[[10, 10, 1], [-10, 10, 1], [-10, -10, 1]],
|
|
29
|
+
[[10, 10, 0], [10, -10, 0], [-10, -10, 0]],
|
|
30
|
+
[[-10, -10, 0], [-10, 10, 0], [10, 10, 0]]
|
|
29
31
|
]
|
|
30
|
-
t.is(pts.length,
|
|
32
|
+
t.is(pts.length, 12)
|
|
31
33
|
t.true(comparePolygonsAsPoints(pts, exp))
|
|
32
34
|
|
|
33
35
|
const poly2 = poly3.fromPoints([[10, 10, 0], [-10, 10, 0], [-10, -10, 0], [10, -10, 0]])
|
|
34
36
|
geometry3 = extrudeFromSlices({ }, poly2)
|
|
35
37
|
pts = geom3.toPoints(geometry3)
|
|
36
38
|
|
|
37
|
-
t.is(pts.length,
|
|
39
|
+
t.is(pts.length, 12)
|
|
38
40
|
t.true(comparePolygonsAsPoints(pts, exp))
|
|
39
41
|
})
|
|
40
42
|
|
|
@@ -84,7 +86,7 @@ test('extrudeFromSlices (same shape, changing dimensions)', (t) => {
|
|
|
84
86
|
}, base
|
|
85
87
|
)
|
|
86
88
|
const pts = geom3.toPoints(geometry3)
|
|
87
|
-
t.is(pts.length,
|
|
89
|
+
t.is(pts.length, 26)
|
|
88
90
|
})
|
|
89
91
|
|
|
90
92
|
test('extrudeFromSlices (changing shape, changing dimensions)', (t) => {
|
|
@@ -101,7 +103,7 @@ test('extrudeFromSlices (changing shape, changing dimensions)', (t) => {
|
|
|
101
103
|
}, base
|
|
102
104
|
)
|
|
103
105
|
const pts = geom3.toPoints(geometry3)
|
|
104
|
-
t.is(pts.length,
|
|
106
|
+
t.is(pts.length, 304)
|
|
105
107
|
})
|
|
106
108
|
|
|
107
109
|
test('extrudeFromSlices (holes)', (t) => {
|
|
@@ -136,15 +138,23 @@ test('extrudeFromSlices (holes)', (t) => {
|
|
|
136
138
|
[[5, 5, 0], [5, -5, 1], [5, 5, 1]],
|
|
137
139
|
[[-5, 5, 0], [5, 5, 0], [5, 5, 1]],
|
|
138
140
|
[[-5, 5, 0], [5, 5, 1], [-5, 5, 1]],
|
|
139
|
-
[[
|
|
140
|
-
[[
|
|
141
|
-
[[10, 10, 1], [5,
|
|
142
|
-
[[5, 5, 1], [
|
|
143
|
-
[[-10, 10,
|
|
144
|
-
[[
|
|
145
|
-
[[10, -
|
|
146
|
-
[[5,
|
|
141
|
+
[[10, -10, 1], [10, 10, 1], [5, 5, 1]],
|
|
142
|
+
[[-5, 5, 1], [5, 5, 1], [10, 10, 1]],
|
|
143
|
+
[[10, -10, 1], [5, 5, 1], [5, -5, 1]],
|
|
144
|
+
[[-5, 5, 1], [10, 10, 1], [-10, 10, 1]],
|
|
145
|
+
[[-10, -10, 1], [10, -10, 1], [5, -5, 1]],
|
|
146
|
+
[[-5, -5, 1], [-5, 5, 1], [-10, 10, 1]],
|
|
147
|
+
[[-10, -10, 1], [5, -5, 1], [-5, -5, 1]],
|
|
148
|
+
[[-5, -5, 1], [-10, 10, 1], [-10, -10, 1]],
|
|
149
|
+
[[5, 5, 0], [10, 10, 0], [10, -10, 0]],
|
|
150
|
+
[[10, 10, 0], [5, 5, 0], [-5, 5, 0]],
|
|
151
|
+
[[5, -5, 0], [5, 5, 0], [10, -10, 0]],
|
|
152
|
+
[[-10, 10, 0], [10, 10, 0], [-5, 5, 0]],
|
|
153
|
+
[[5, -5, 0], [10, -10, 0], [-10, -10, 0]],
|
|
154
|
+
[[-10, 10, 0], [-5, 5, 0], [-5, -5, 0]],
|
|
155
|
+
[[-5, -5, 0], [5, -5, 0], [-10, -10, 0]],
|
|
156
|
+
[[-10, -10, 0], [-10, 10, 0], [-5, -5, 0]]
|
|
147
157
|
]
|
|
148
|
-
t.is(pts.length,
|
|
158
|
+
t.is(pts.length, 32)
|
|
149
159
|
t.true(comparePolygonsAsPoints(pts, exp))
|
|
150
160
|
})
|
|
@@ -25,14 +25,15 @@ const extrudeLinear = (options, ...objects) => {
|
|
|
25
25
|
const defaults = {
|
|
26
26
|
height: 1,
|
|
27
27
|
twistAngle: 0,
|
|
28
|
-
twistSteps: 1
|
|
28
|
+
twistSteps: 1,
|
|
29
|
+
repair: true
|
|
29
30
|
}
|
|
30
|
-
const { height, twistAngle, twistSteps } = Object.assign({ }, defaults, options)
|
|
31
|
+
const { height, twistAngle, twistSteps, repair } = Object.assign({ }, defaults, options)
|
|
31
32
|
|
|
32
33
|
objects = flatten(objects)
|
|
33
34
|
if (objects.length === 0) throw new Error('wrong number of arguments')
|
|
34
35
|
|
|
35
|
-
options = { offset: [0, 0, height], twistAngle
|
|
36
|
+
options = { offset: [0, 0, height], twistAngle, twistSteps, repair }
|
|
36
37
|
|
|
37
38
|
const results = objects.map((object) => {
|
|
38
39
|
if (path2.isA(object)) return extrudeLinearPath2(options, object)
|
|
@@ -20,10 +20,12 @@ test('extrudeLinear (defaults)', (t) => {
|
|
|
20
20
|
[[-5, 5, 0], [-5, -5, 1], [-5, 5, 1]],
|
|
21
21
|
[[-5, -5, 0], [5, -5, 0], [5, -5, 1]],
|
|
22
22
|
[[-5, -5, 0], [5, -5, 1], [-5, -5, 1]],
|
|
23
|
-
[[5,
|
|
24
|
-
[[5,
|
|
23
|
+
[[-5, -5, 1], [5, -5, 1], [5, 5, 1]],
|
|
24
|
+
[[5, 5, 1], [-5, 5, 1], [-5, -5, 1]],
|
|
25
|
+
[[5, 5, 0], [5, -5, 0], [-5, -5, 0]],
|
|
26
|
+
[[-5, -5, 0], [-5, 5, 0], [5, 5, 0]]
|
|
25
27
|
]
|
|
26
|
-
t.is(pts.length,
|
|
28
|
+
t.is(pts.length, 12)
|
|
27
29
|
t.true(comparePolygonsAsPoints(pts, exp))
|
|
28
30
|
})
|
|
29
31
|
|
|
@@ -41,10 +43,12 @@ test('extrudeLinear (no twist)', (t) => {
|
|
|
41
43
|
[[-5, 5, 0], [-5, -5, 15], [-5, 5, 15]],
|
|
42
44
|
[[-5, -5, 0], [5, -5, 0], [5, -5, 15]],
|
|
43
45
|
[[-5, -5, 0], [5, -5, 15], [-5, -5, 15]],
|
|
44
|
-
[[5,
|
|
45
|
-
[[5,
|
|
46
|
+
[[-5, -5, 15], [5, -5, 15], [5, 5, 15]],
|
|
47
|
+
[[5, 5, 15], [-5, 5, 15], [-5, -5, 15]],
|
|
48
|
+
[[5, 5, 0], [5, -5, 0], [-5, -5, 0]],
|
|
49
|
+
[[-5, -5, 0], [-5, 5, 0], [5, 5, 0]]
|
|
46
50
|
]
|
|
47
|
-
t.is(pts.length,
|
|
51
|
+
t.is(pts.length, 12)
|
|
48
52
|
t.true(comparePolygonsAsPoints(pts, exp))
|
|
49
53
|
|
|
50
54
|
geometry3 = extrudeLinear({ height: -15 }, geometry2)
|
|
@@ -58,10 +62,12 @@ test('extrudeLinear (no twist)', (t) => {
|
|
|
58
62
|
[[-5, -5, 0], [-5, 5, -15], [-5, -5, -15]],
|
|
59
63
|
[[5, -5, 0], [-5, -5, 0], [-5, -5, -15]],
|
|
60
64
|
[[5, -5, 0], [-5, -5, -15], [5, -5, -15]],
|
|
61
|
-
[[5,
|
|
62
|
-
[[5, 5,
|
|
65
|
+
[[-5, 5, -15], [5, 5, -15], [5, -5, -15]],
|
|
66
|
+
[[5, -5, -15], [-5, -5, -15], [-5, 5, -15]],
|
|
67
|
+
[[5, -5, 0], [5, 5, 0], [-5, 5, 0]],
|
|
68
|
+
[[-5, 5, 0], [-5, -5, 0], [5, -5, 0]]
|
|
63
69
|
]
|
|
64
|
-
t.is(pts.length,
|
|
70
|
+
t.is(pts.length, 12)
|
|
65
71
|
t.true(comparePolygonsAsPoints(pts, exp))
|
|
66
72
|
})
|
|
67
73
|
|
|
@@ -79,11 +85,20 @@ test('extrudeLinear (twist)', (t) => {
|
|
|
79
85
|
[[-5, 5, 0], [-7.0710678118654755, -4.440892098500626e-16, 15], [-4.440892098500626e-16, 7.0710678118654755, 15]],
|
|
80
86
|
[[-5, -5, 0], [5, -5, 0], [4.440892098500626e-16, -7.0710678118654755, 15]],
|
|
81
87
|
[[-5, -5, 0], [4.440892098500626e-16, -7.0710678118654755, 15], [-7.0710678118654755, -4.440892098500626e-16, 15]],
|
|
82
|
-
[
|
|
83
|
-
[-7.
|
|
84
|
-
|
|
88
|
+
[
|
|
89
|
+
[-7.0710678118654755, -4.440892098500626e-16, 15],
|
|
90
|
+
[4.440892098500626e-16, -7.0710678118654755, 15],
|
|
91
|
+
[7.0710678118654755, 4.440892098500626e-16, 15]
|
|
92
|
+
],
|
|
93
|
+
[
|
|
94
|
+
[7.0710678118654755, 4.440892098500626e-16, 15],
|
|
95
|
+
[-4.440892098500626e-16, 7.0710678118654755, 15],
|
|
96
|
+
[-7.0710678118654755, -4.440892098500626e-16, 15]
|
|
97
|
+
],
|
|
98
|
+
[[5, 5, 0], [5, -5, 0], [-5, -5, 0]],
|
|
99
|
+
[[-5, -5, 0], [-5, 5, 0], [5, 5, 0]]
|
|
85
100
|
]
|
|
86
|
-
t.is(pts.length,
|
|
101
|
+
t.is(pts.length, 12)
|
|
87
102
|
t.true(comparePolygonsAsPoints(pts, exp))
|
|
88
103
|
|
|
89
104
|
geometry3 = extrudeLinear({ height: 15, twistAngle: Math.PI / 2, twistSteps: 3 }, geometry2)
|
|
@@ -113,15 +128,17 @@ test('extrudeLinear (twist)', (t) => {
|
|
|
113
128
|
[[-6.830127018922193, -1.8301270189221923, 10], [5, -5, 15], [-5, -5, 15]],
|
|
114
129
|
[[1.8301270189221923, -6.830127018922193, 10], [6.830127018922193, 1.8301270189221923, 10], [5, 5, 15]],
|
|
115
130
|
[[1.8301270189221923, -6.830127018922193, 10], [5, 5, 15], [5, -5, 15]],
|
|
116
|
-
[[
|
|
117
|
-
[[5,
|
|
131
|
+
[[5, -5, 15], [5, 5, 15], [-5, 5, 15]],
|
|
132
|
+
[[-5, 5, 15], [-5, -5, 15], [5, -5, 15]],
|
|
133
|
+
[[5, 5, 0], [5, -5, 0], [-5, -5, 0]],
|
|
134
|
+
[[-5, -5, 0], [-5, 5, 0], [5, 5, 0]]
|
|
118
135
|
]
|
|
119
|
-
t.is(pts.length,
|
|
136
|
+
t.is(pts.length, 28)
|
|
120
137
|
t.true(comparePolygonsAsPoints(pts, exp))
|
|
121
138
|
|
|
122
139
|
geometry3 = extrudeLinear({ height: 15, twistAngle: Math.PI / 2, twistSteps: 30 }, geometry2)
|
|
123
140
|
pts = geom3.toPoints(geometry3)
|
|
124
|
-
t.is(pts.length,
|
|
141
|
+
t.is(pts.length, 244)
|
|
125
142
|
})
|
|
126
143
|
|
|
127
144
|
test('extrudeLinear (holes)', (t) => {
|
|
@@ -154,16 +171,24 @@ test('extrudeLinear (holes)', (t) => {
|
|
|
154
171
|
[[2, 2, 0], [2, -2, 15], [2, 2, 15]],
|
|
155
172
|
[[-2, 2, 0], [2, 2, 0], [2, 2, 15]],
|
|
156
173
|
[[-2, 2, 0], [2, 2, 15], [-2, 2, 15]],
|
|
157
|
-
[[
|
|
158
|
-
[[
|
|
159
|
-
[[5, 5, 15], [2,
|
|
160
|
-
[[2, 2, 15], [
|
|
161
|
-
[[-5, 5,
|
|
162
|
-
[[
|
|
163
|
-
[[5, -
|
|
164
|
-
[[2,
|
|
174
|
+
[[5, -5, 15], [5, 5, 15], [2, 2, 15]],
|
|
175
|
+
[[-2, 2, 15], [2, 2, 15], [5, 5, 15]],
|
|
176
|
+
[[5, -5, 15], [2, 2, 15], [2, -2, 15]],
|
|
177
|
+
[[-2, 2, 15], [5, 5, 15], [-5, 5, 15]],
|
|
178
|
+
[[-5, -5, 15], [5, -5, 15], [2, -2, 15]],
|
|
179
|
+
[[-2, -2, 15], [-2, 2, 15], [-5, 5, 15]],
|
|
180
|
+
[[-5, -5, 15], [2, -2, 15], [-2, -2, 15]],
|
|
181
|
+
[[-2, -2, 15], [-5, 5, 15], [-5, -5, 15]],
|
|
182
|
+
[[2, 2, 0], [5, 5, 0], [5, -5, 0]],
|
|
183
|
+
[[5, 5, 0], [2, 2, 0], [-2, 2, 0]],
|
|
184
|
+
[[2, -2, 0], [2, 2, 0], [5, -5, 0]],
|
|
185
|
+
[[-5, 5, 0], [5, 5, 0], [-2, 2, 0]],
|
|
186
|
+
[[2, -2, 0], [5, -5, 0], [-5, -5, 0]],
|
|
187
|
+
[[-5, 5, 0], [-2, 2, 0], [-2, -2, 0]],
|
|
188
|
+
[[-2, -2, 0], [2, -2, 0], [-5, -5, 0]],
|
|
189
|
+
[[-5, -5, 0], [-5, 5, 0], [-2, -2, 0]]
|
|
165
190
|
]
|
|
166
|
-
t.is(pts.length,
|
|
191
|
+
t.is(pts.length, 32)
|
|
167
192
|
t.true(comparePolygonsAsPoints(pts, exp))
|
|
168
193
|
})
|
|
169
194
|
|
|
@@ -179,9 +204,10 @@ test('extrudeLinear (path2)', (t) => {
|
|
|
179
204
|
[[0, 0, 0], [12, 0, 15], [0, 0, 15]],
|
|
180
205
|
[[12, 0, 0], [6, 10, 0], [6, 10, 15]],
|
|
181
206
|
[[12, 0, 0], [6, 10, 15], [12, 0, 15]],
|
|
182
|
-
[[
|
|
183
|
-
[[
|
|
207
|
+
[[12, 0, 15], [6, 10, 15], [0, 0, 15]],
|
|
208
|
+
[[0, 0, 0], [6, 10, 0], [12, 0, 0]]
|
|
184
209
|
]
|
|
210
|
+
|
|
185
211
|
t.is(pts.length, 8)
|
|
186
212
|
t.true(comparePolygonsAsPoints(pts, exp))
|
|
187
213
|
})
|
|
@@ -14,6 +14,7 @@ const extrudeFromSlices = require('./extrudeFromSlices')
|
|
|
14
14
|
* @param {Array} [options.offset] - the direction of the extrusion as a 3D vector
|
|
15
15
|
* @param {Number} [options.twistAngle] - the final rotation (RADIANS) about the origin
|
|
16
16
|
* @param {Integer} [options.twistSteps] - the number of steps created to produce the twist (if any)
|
|
17
|
+
* @param {Boolean} [options.repair] - repair gaps in the geometry
|
|
17
18
|
* @param {geom2} geometry - the geometry to extrude
|
|
18
19
|
* @returns {geom3} the extruded 3D geometry
|
|
19
20
|
*/
|
|
@@ -21,9 +22,10 @@ const extrudeGeom2 = (options, geometry) => {
|
|
|
21
22
|
const defaults = {
|
|
22
23
|
offset: [0, 0, 1],
|
|
23
24
|
twistAngle: 0,
|
|
24
|
-
twistSteps: 12
|
|
25
|
+
twistSteps: 12,
|
|
26
|
+
repair: true
|
|
25
27
|
}
|
|
26
|
-
let { offset, twistAngle, twistSteps } = Object.assign({ }, defaults, options)
|
|
28
|
+
let { offset, twistAngle, twistSteps, repair } = Object.assign({ }, defaults, options)
|
|
27
29
|
|
|
28
30
|
if (twistSteps < 1) throw new Error('twistSteps must be 1 or more')
|
|
29
31
|
|
|
@@ -53,6 +55,7 @@ const extrudeGeom2 = (options, geometry) => {
|
|
|
53
55
|
numberOfSlices: twistSteps + 1,
|
|
54
56
|
capStart: true,
|
|
55
57
|
capEnd: true,
|
|
58
|
+
repair,
|
|
56
59
|
callback: createTwist
|
|
57
60
|
}
|
|
58
61
|
return extrudeFromSlices(options, baseSlice)
|
|
@@ -12,11 +12,11 @@ test('extrudeRectangular (defaults)', (t) => {
|
|
|
12
12
|
|
|
13
13
|
let obs = extrudeRectangular({ }, geometry1)
|
|
14
14
|
let pts = geom3.toPoints(obs)
|
|
15
|
-
t.is(pts.length,
|
|
15
|
+
t.is(pts.length, 44)
|
|
16
16
|
|
|
17
17
|
obs = extrudeRectangular({ }, geometry2)
|
|
18
18
|
pts = geom3.toPoints(obs)
|
|
19
|
-
t.is(pts.length,
|
|
19
|
+
t.is(pts.length, 32)
|
|
20
20
|
})
|
|
21
21
|
|
|
22
22
|
test('extrudeRectangular (chamfer)', (t) => {
|
|
@@ -25,11 +25,11 @@ test('extrudeRectangular (chamfer)', (t) => {
|
|
|
25
25
|
|
|
26
26
|
let obs = extrudeRectangular({ corners: 'chamfer' }, geometry1)
|
|
27
27
|
let pts = geom3.toPoints(obs)
|
|
28
|
-
t.is(pts.length,
|
|
28
|
+
t.is(pts.length, 60)
|
|
29
29
|
|
|
30
30
|
obs = extrudeRectangular({ corners: 'chamfer' }, geometry2)
|
|
31
31
|
pts = geom3.toPoints(obs)
|
|
32
|
-
t.is(pts.length,
|
|
32
|
+
t.is(pts.length, 48)
|
|
33
33
|
})
|
|
34
34
|
|
|
35
35
|
test('extrudeRectangular (segments = 8, round)', (t) => {
|
|
@@ -38,11 +38,11 @@ test('extrudeRectangular (segments = 8, round)', (t) => {
|
|
|
38
38
|
|
|
39
39
|
let obs = extrudeRectangular({ segments: 8, corners: 'round' }, geometry1)
|
|
40
40
|
let pts = geom3.toPoints(obs)
|
|
41
|
-
t.is(pts.length,
|
|
41
|
+
t.is(pts.length, 84)
|
|
42
42
|
|
|
43
43
|
obs = extrudeRectangular({ segments: 8, corners: 'round' }, geometry2)
|
|
44
44
|
pts = geom3.toPoints(obs)
|
|
45
|
-
t.is(pts.length,
|
|
45
|
+
t.is(pts.length, 64)
|
|
46
46
|
})
|
|
47
47
|
|
|
48
48
|
test('extrudeRectangular (holes)', (t) => {
|
|
@@ -59,5 +59,5 @@ test('extrudeRectangular (holes)', (t) => {
|
|
|
59
59
|
|
|
60
60
|
const obs = extrudeRectangular({ size: 2, height: 15, segments: 16, corners: 'round' }, geometry2)
|
|
61
61
|
const pts = geom3.toPoints(obs)
|
|
62
|
-
t.is(pts.length,
|
|
62
|
+
t.is(pts.length, 192)
|
|
63
63
|
})
|