@jscad/modeling 2.9.6 → 2.11.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 +33 -0
- package/README.md +12 -2
- package/dist/jscad-modeling.min.js +154 -151
- package/package.json +2 -2
- package/src/geometries/path2/appendArc.js +6 -5
- package/src/geometries/path2/appendArc.test.js +3 -1
- package/src/geometries/path2/appendBezier.js +2 -1
- package/src/geometries/path2/transform.js +1 -1
- package/src/index.d.ts +1 -0
- package/src/maths/constants.js +10 -0
- package/src/maths/mat4/fromRotation.js +1 -1
- package/src/maths/mat4/fromTaitBryanRotation.js +1 -1
- package/src/maths/mat4/fromXRotation.js +1 -1
- package/src/maths/mat4/fromYRotation.js +1 -1
- package/src/maths/mat4/fromZRotation.js +1 -1
- package/src/maths/mat4/invert.test.js +5 -2
- package/src/maths/mat4/isOnlyTransformScale.js +1 -1
- package/src/maths/mat4/isOnlyTransformScale.test.js +3 -1
- package/src/maths/rotation.test.js +5 -2
- package/src/maths/utils/trigonometry.js +6 -6
- package/src/maths/utils/trigonometry.test.js +10 -8
- package/src/maths/vec2/fromAngleDegrees.js +1 -1
- package/src/maths/vec2/fromAngleRadians.test.js +4 -1
- package/src/maths/vec2/normal.js +3 -1
- package/src/maths/vec2/rotate.test.js +4 -1
- package/src/maths/vec3/rotateX.test.js +4 -1
- package/src/maths/vec3/rotateY.test.js +4 -1
- package/src/maths/vec3/rotateZ.test.js +4 -1
- package/src/measurements/index.d.ts +4 -0
- package/src/measurements/measureBoundingSphere.d.ts +9 -0
- package/src/measurements/measureCenter.d.ts +7 -0
- package/src/measurements/measureCenterOfMass.d.ts +7 -0
- package/src/measurements/measureDimensions.d.ts +7 -0
- package/src/operations/expansions/expand.test.js +3 -1
- package/src/operations/expansions/expandShell.js +4 -4
- package/src/operations/expansions/offsetFromPoints.js +2 -2
- package/src/operations/extrusions/extrudeFromSlices.test.js +3 -2
- package/src/operations/extrusions/extrudeHelical.d.ts +14 -0
- package/src/operations/extrusions/extrudeHelical.js +114 -0
- package/src/operations/extrusions/extrudeHelical.test.js +62 -0
- package/src/operations/extrusions/extrudeLinear.test.js +5 -3
- package/src/operations/extrusions/extrudeRectangular.js +1 -1
- package/src/operations/extrusions/extrudeRectangular.test.js +5 -3
- package/src/operations/extrusions/extrudeRotate.js +14 -13
- package/src/operations/extrusions/extrudeRotate.test.js +7 -5
- package/src/operations/extrusions/index.js +1 -0
- package/src/operations/extrusions/slice/calculatePlane.test.js +3 -2
- package/src/operations/extrusions/slice/index.js +2 -0
- package/src/operations/modifiers/generalize.d.ts +12 -0
- package/src/operations/modifiers/generalize.test.js +3 -1
- package/src/operations/modifiers/index.d.ts +2 -0
- package/src/operations/modifiers/snap.d.ts +6 -0
- package/src/operations/modifiers/snap.test.js +5 -3
- package/src/operations/transforms/rotate.js +1 -1
- package/src/operations/transforms/rotate.test.js +13 -11
- package/src/operations/transforms/transform.js +1 -1
- package/src/primitives/arc.js +8 -8
- package/src/primitives/arc.test.js +9 -8
- package/src/primitives/circle.js +4 -2
- package/src/primitives/circle.test.js +6 -5
- package/src/primitives/cuboid.js +1 -1
- package/src/primitives/cylinderElliptic.js +11 -11
- package/src/primitives/cylinderElliptic.test.js +3 -2
- package/src/primitives/ellipse.js +10 -10
- package/src/primitives/ellipse.test.js +6 -5
- package/src/primitives/ellipsoid.js +3 -2
- package/src/primitives/roundedCuboid.js +6 -6
- package/src/primitives/roundedCylinder.js +3 -3
- package/src/primitives/roundedRectangle.js +5 -5
- package/src/primitives/star.js +3 -2
- package/src/primitives/torus.js +4 -2
- package/src/primitives/torus.test.js +5 -3
- package/src/primitives/triangle.test.js +2 -1
- package/src/utils/degToRad.test.js +5 -5
- package/src/utils/insertSorted.js +1 -1
- package/src/utils/radToDeg.test.js +6 -6
- package/src/utils/radiusToSegments.js +6 -4
- package/src/utils/radiusToSegments.test.js +5 -3
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
const { TAU } = require('../../maths/constants')
|
|
2
|
+
const slice = require('./slice')
|
|
3
|
+
const mat4 = require('../../maths/mat4')
|
|
4
|
+
const extrudeFromSlices = require('./extrudeFromSlices')
|
|
5
|
+
const geom2 = require('../../geometries/geom2')
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Perform a helical extrude of the geometry, using the given options.
|
|
9
|
+
*
|
|
10
|
+
* @param {Object} options - options for extrusion
|
|
11
|
+
* @param {Number} [options.angle=TAU] - angle of the extrusion (RADIANS) positive for right-hand rotation, negative for left-hand
|
|
12
|
+
* @param {Number} [options.startAngle=0] - start angle of the extrusion (RADIANS)
|
|
13
|
+
* @param {Number} [options.pitch=10] - elevation gain for each turn
|
|
14
|
+
* @param {Number} [options.height] - total height of the helix path. Ignored if pitch is set.
|
|
15
|
+
* @param {Number} [options.endOffset=0] - offset the final radius of the extrusion, allowing for tapered helix, and or spiral
|
|
16
|
+
* @param {Number} [options.segmentsPerRotation=32] - number of segments per full rotation of the extrusion
|
|
17
|
+
* @param {geom2} geometry - the geometry to extrude
|
|
18
|
+
* @returns {geom3} the extruded geometry
|
|
19
|
+
* @alias module:modeling/extrusions.extrudeHelical
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* const myshape = extrudeHelical(
|
|
23
|
+
* {
|
|
24
|
+
* angle: Math.PI * 4,
|
|
25
|
+
* pitch: 10,
|
|
26
|
+
* segmentsPerRotation: 64
|
|
27
|
+
* },
|
|
28
|
+
* circle({size: 3, center: [10, 0]})
|
|
29
|
+
* )
|
|
30
|
+
*/
|
|
31
|
+
const extrudeHelical = (options, geometry) => {
|
|
32
|
+
const defaults = {
|
|
33
|
+
angle: TAU,
|
|
34
|
+
startAngle: 0,
|
|
35
|
+
pitch: 10,
|
|
36
|
+
endOffset: 0,
|
|
37
|
+
segmentsPerRotation: 32
|
|
38
|
+
}
|
|
39
|
+
const { angle, endOffset, segmentsPerRotation, startAngle } = Object.assign({}, defaults, options)
|
|
40
|
+
|
|
41
|
+
let pitch
|
|
42
|
+
// ignore height if pitch is set
|
|
43
|
+
if(!options.pitch && options.height) {
|
|
44
|
+
pitch = options.height / (angle / TAU)
|
|
45
|
+
} else {
|
|
46
|
+
pitch = options.pitch ? options.pitch : defaults.pitch
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// needs at least 3 segments for each revolution
|
|
50
|
+
const minNumberOfSegments = 3
|
|
51
|
+
|
|
52
|
+
if (segmentsPerRotation < minNumberOfSegments)
|
|
53
|
+
throw new Error(`The number of segments per rotation needs to be at least 3.`)
|
|
54
|
+
|
|
55
|
+
let shapeSides = geom2.toSides(geometry)
|
|
56
|
+
if (shapeSides.length === 0) throw new Error('the given geometry cannot be empty')
|
|
57
|
+
|
|
58
|
+
// const pointsWithNegativeX = shapeSides.filter((s) => (s[0][0] < 0))
|
|
59
|
+
const pointsWithPositiveX = shapeSides.filter((s) => (s[0][0] >= 0))
|
|
60
|
+
|
|
61
|
+
let baseSlice = slice.fromSides(shapeSides)
|
|
62
|
+
|
|
63
|
+
if(pointsWithPositiveX.length === 0) {
|
|
64
|
+
// only points in negative x plane, reverse
|
|
65
|
+
baseSlice = slice.reverse(baseSlice)
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const calculatedSegments = Math.round(segmentsPerRotation / TAU * Math.abs(angle))
|
|
69
|
+
const segments = calculatedSegments >= 2 ? calculatedSegments : 2
|
|
70
|
+
// define transform matrix variables for performance increase
|
|
71
|
+
const step1 = mat4.create()
|
|
72
|
+
let matrix
|
|
73
|
+
const sliceCallback = (progress, index, base) => {
|
|
74
|
+
const zRotation = startAngle + angle / segments * index
|
|
75
|
+
const xOffset = endOffset / segments * index
|
|
76
|
+
const zOffset = (zRotation - startAngle) / TAU * pitch
|
|
77
|
+
|
|
78
|
+
// TODO: check for valid geometry after translations
|
|
79
|
+
// ie all the points have to be either x > -xOffset or x < -xOffset
|
|
80
|
+
// this would have to be checked for every transform, and handled
|
|
81
|
+
//
|
|
82
|
+
// not implementing, as this currently doesn't break anything,
|
|
83
|
+
// only creates inside-out polygons
|
|
84
|
+
|
|
85
|
+
// create transformation matrix
|
|
86
|
+
mat4.multiply(
|
|
87
|
+
step1,
|
|
88
|
+
// then apply offsets
|
|
89
|
+
mat4.fromTranslation(mat4.create(), [xOffset, 0, zOffset * Math.sign(angle)]),
|
|
90
|
+
// first rotate "flat" 2D shape from XY to XZ plane
|
|
91
|
+
mat4.fromXRotation(mat4.create(), -TAU / 4 * Math.sign(angle)) // rotate the slice correctly to not create inside-out polygon
|
|
92
|
+
)
|
|
93
|
+
|
|
94
|
+
matrix = mat4.create()
|
|
95
|
+
mat4.multiply(
|
|
96
|
+
matrix,
|
|
97
|
+
// finally rotate around Z axis
|
|
98
|
+
mat4.fromZRotation(mat4.create(), zRotation),
|
|
99
|
+
step1
|
|
100
|
+
)
|
|
101
|
+
return slice.transform(matrix, base)
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
return extrudeFromSlices(
|
|
105
|
+
{
|
|
106
|
+
// "base" slice is counted as segment, so add one for complete final rotation
|
|
107
|
+
numberOfSlices: segments + 1,
|
|
108
|
+
callback: sliceCallback
|
|
109
|
+
},
|
|
110
|
+
baseSlice
|
|
111
|
+
)
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
module.exports = extrudeHelical
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
const test = require('ava')
|
|
2
|
+
const { comparePoints, comparePolygonsAsPoints } = require('../../../test/helpers')
|
|
3
|
+
const { TAU } = require('../../maths/constants')
|
|
4
|
+
const { geom2, geom3 } = require('../../geometries')
|
|
5
|
+
const { circle } = require('../../primitives')
|
|
6
|
+
|
|
7
|
+
const { extrudeHelical } = require('./index')
|
|
8
|
+
|
|
9
|
+
test('extrudeHelical: (defaults) extruding of a geom2 produces an expected geom3', (t) => {
|
|
10
|
+
const geometry2 = geom2.fromPoints([[10, 8], [10, -8], [26, -8], [26, 8]])
|
|
11
|
+
|
|
12
|
+
const geometry3 = extrudeHelical({}, geometry2)
|
|
13
|
+
const pts = geom3.toPoints(geometry3)
|
|
14
|
+
t.notThrows(() => geom3.validate(geometry3))
|
|
15
|
+
})
|
|
16
|
+
|
|
17
|
+
test('extrudeHelical: (defaults) extruding of a circle produces an expected geom3', (t) => {
|
|
18
|
+
const geometry2 = circle({ size: 3, center: [10, 0] })
|
|
19
|
+
|
|
20
|
+
const geometry3 = extrudeHelical({}, geometry2)
|
|
21
|
+
t.notThrows(() => geom3.validate(geometry3))
|
|
22
|
+
})
|
|
23
|
+
|
|
24
|
+
test('extrudeHelical: (angle) extruding of a circle produces an expected geom3', (t) => {
|
|
25
|
+
const maxRevolutions = 10
|
|
26
|
+
const geometry2 = circle({ size: 3, center: [10, 0] })
|
|
27
|
+
for (const index of [...Array(maxRevolutions).keys()]) {
|
|
28
|
+
// also test negative angles
|
|
29
|
+
const geometry3 = extrudeHelical({angle: TAU * (index - maxRevolutions / 2)}, geometry2)
|
|
30
|
+
t.notThrows(() => geom3.validate(geometry3))
|
|
31
|
+
}
|
|
32
|
+
})
|
|
33
|
+
|
|
34
|
+
test('extrudeHelical: (pitch) extruding of a circle produces an expected geom3', (t) => {
|
|
35
|
+
const startPitch = -10
|
|
36
|
+
const geometry2 = circle({ size: 3, center: [10, 0] })
|
|
37
|
+
for (const index of [...Array(20).keys()]) {
|
|
38
|
+
// also test negative pitches
|
|
39
|
+
const geometry3 = extrudeHelical({pitch: startPitch + index}, geometry2)
|
|
40
|
+
t.notThrows(() => geom3.validate(geometry3))
|
|
41
|
+
}
|
|
42
|
+
})
|
|
43
|
+
|
|
44
|
+
test('extrudeHelical: (endRadiusOffset) extruding of a circle produces an expected geom3', (t) => {
|
|
45
|
+
const startOffset = -5
|
|
46
|
+
const geometry2 = circle({ size: 3, center: [10, 0] })
|
|
47
|
+
for (const index of [...Array(10).keys()]) {
|
|
48
|
+
// also test negative pitches
|
|
49
|
+
const geometry3 = extrudeHelical({endRadiusOffset: startOffset + index}, geometry2)
|
|
50
|
+
t.notThrows(() => geom3.validate(geometry3))
|
|
51
|
+
}
|
|
52
|
+
})
|
|
53
|
+
|
|
54
|
+
test('extrudeHelical: (segments) extruding of a circle produces an expected geom3', (t) => {
|
|
55
|
+
const startSegments = 3
|
|
56
|
+
const geometry2 = circle({ size: 3, center: [10, 0] })
|
|
57
|
+
for (const index of [...Array(30).keys()]) {
|
|
58
|
+
// also test negative pitches
|
|
59
|
+
const geometry3 = extrudeHelical({segments: startSegments + index}, geometry2)
|
|
60
|
+
t.notThrows(() => geom3.validate(geometry3))
|
|
61
|
+
}
|
|
62
|
+
})
|
|
@@ -2,6 +2,8 @@ const test = require('ava')
|
|
|
2
2
|
|
|
3
3
|
const comparePolygonsAsPoints = require('../../../test/helpers/comparePolygonsAsPoints')
|
|
4
4
|
|
|
5
|
+
const { TAU } = require('../../maths/constants')
|
|
6
|
+
|
|
5
7
|
const { geom2, geom3, path2 } = require('../../geometries')
|
|
6
8
|
|
|
7
9
|
const { extrudeLinear } = require('./index')
|
|
@@ -77,7 +79,7 @@ test('extrudeLinear (no twist)', (t) => {
|
|
|
77
79
|
test('extrudeLinear (twist)', (t) => {
|
|
78
80
|
const geometry2 = geom2.fromPoints([[5, 5], [-5, 5], [-5, -5], [5, -5]])
|
|
79
81
|
|
|
80
|
-
let geometry3 = extrudeLinear({ height: 15, twistAngle:
|
|
82
|
+
let geometry3 = extrudeLinear({ height: 15, twistAngle: -TAU / 8 }, geometry2)
|
|
81
83
|
let pts = geom3.toPoints(geometry3)
|
|
82
84
|
let exp = [
|
|
83
85
|
[[5, -5, 0], [5, 5, 0], [7.0710678118654755, 4.440892098500626e-16, 15]],
|
|
@@ -105,7 +107,7 @@ test('extrudeLinear (twist)', (t) => {
|
|
|
105
107
|
t.is(pts.length, 12)
|
|
106
108
|
t.true(comparePolygonsAsPoints(pts, exp))
|
|
107
109
|
|
|
108
|
-
geometry3 = extrudeLinear({ height: 15, twistAngle:
|
|
110
|
+
geometry3 = extrudeLinear({ height: 15, twistAngle: TAU / 4, twistSteps: 3 }, geometry2)
|
|
109
111
|
pts = geom3.toPoints(geometry3)
|
|
110
112
|
exp = [
|
|
111
113
|
[[5, -5, 0], [5, 5, 0], [1.830127018922194, 6.830127018922193, 5]],
|
|
@@ -140,7 +142,7 @@ test('extrudeLinear (twist)', (t) => {
|
|
|
140
142
|
t.is(pts.length, 28)
|
|
141
143
|
t.true(comparePolygonsAsPoints(pts, exp))
|
|
142
144
|
|
|
143
|
-
geometry3 = extrudeLinear({ height: 15, twistAngle:
|
|
145
|
+
geometry3 = extrudeLinear({ height: 15, twistAngle: TAU / 2, twistSteps: 30 }, geometry2)
|
|
144
146
|
pts = geom3.toPoints(geometry3)
|
|
145
147
|
t.notThrows(() => geom3.validate(geometry3))
|
|
146
148
|
t.is(pts.length, 244)
|
|
@@ -18,7 +18,7 @@ const extrudeRectangularGeom2 = require('./extrudeRectangularGeom2')
|
|
|
18
18
|
*
|
|
19
19
|
* @example
|
|
20
20
|
* let mywalls = extrudeRectangular({size: 1, height: 3}, square({size: 20}))
|
|
21
|
-
* let mywalls = extrudeRectangular({size: 1, height: 300, twistAngle:
|
|
21
|
+
* let mywalls = extrudeRectangular({size: 1, height: 300, twistAngle: TAU / 2}, square({size: 20}))
|
|
22
22
|
*/
|
|
23
23
|
const extrudeRectangular = (options, ...objects) => {
|
|
24
24
|
const defaults = {
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
const test = require('ava')
|
|
2
2
|
|
|
3
|
+
const { TAU } = require('../../maths/constants')
|
|
4
|
+
|
|
3
5
|
const { geom2, geom3 } = require('../../geometries')
|
|
4
6
|
|
|
5
7
|
const { arc, rectangle } = require('../../primitives')
|
|
@@ -7,7 +9,7 @@ const { arc, rectangle } = require('../../primitives')
|
|
|
7
9
|
const { extrudeRectangular } = require('./index')
|
|
8
10
|
|
|
9
11
|
test('extrudeRectangular (defaults)', (t) => {
|
|
10
|
-
const geometry1 = arc({ radius: 5, endAngle:
|
|
12
|
+
const geometry1 = arc({ radius: 5, endAngle: TAU / 4, segments: 16 })
|
|
11
13
|
const geometry2 = rectangle({ size: [5, 5] })
|
|
12
14
|
|
|
13
15
|
let obs = extrudeRectangular({ }, geometry1)
|
|
@@ -22,7 +24,7 @@ test('extrudeRectangular (defaults)', (t) => {
|
|
|
22
24
|
})
|
|
23
25
|
|
|
24
26
|
test('extrudeRectangular (chamfer)', (t) => {
|
|
25
|
-
const geometry1 = arc({ radius: 5, endAngle:
|
|
27
|
+
const geometry1 = arc({ radius: 5, endAngle: TAU / 4, segments: 16 })
|
|
26
28
|
const geometry2 = rectangle({ size: [5, 5] })
|
|
27
29
|
|
|
28
30
|
let obs = extrudeRectangular({ corners: 'chamfer' }, geometry1)
|
|
@@ -37,7 +39,7 @@ test('extrudeRectangular (chamfer)', (t) => {
|
|
|
37
39
|
})
|
|
38
40
|
|
|
39
41
|
test('extrudeRectangular (segments = 8, round)', (t) => {
|
|
40
|
-
const geometry1 = arc({ radius: 5, endAngle:
|
|
42
|
+
const geometry1 = arc({ radius: 5, endAngle: TAU / 4, segments: 16 })
|
|
41
43
|
const geometry2 = rectangle({ size: [5, 5] })
|
|
42
44
|
|
|
43
45
|
let obs = extrudeRectangular({ segments: 8, corners: 'round' }, geometry1)
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
const { TAU } = require('../../maths/constants')
|
|
1
2
|
const mat4 = require('../../maths/mat4')
|
|
2
3
|
|
|
3
4
|
const { mirrorX } = require('../transforms/mirror')
|
|
@@ -12,7 +13,7 @@ const extrudeFromSlices = require('./extrudeFromSlices')
|
|
|
12
13
|
* Rotate extrude the given geometry using the given options.
|
|
13
14
|
*
|
|
14
15
|
* @param {Object} options - options for extrusion
|
|
15
|
-
* @param {Number} [options.angle=
|
|
16
|
+
* @param {Number} [options.angle=TAU] - angle of the extrusion (RADIANS)
|
|
16
17
|
* @param {Number} [options.startAngle=0] - start angle of the extrusion (RADIANS)
|
|
17
18
|
* @param {String} [options.overflow='cap'] - what to do with points outside of bounds (+ / - x) :
|
|
18
19
|
* defaults to capping those points to 0 (only supported behaviour for now)
|
|
@@ -22,24 +23,24 @@ const extrudeFromSlices = require('./extrudeFromSlices')
|
|
|
22
23
|
* @alias module:modeling/extrusions.extrudeRotate
|
|
23
24
|
*
|
|
24
25
|
* @example
|
|
25
|
-
* const myshape = extrudeRotate({segments: 8, angle:
|
|
26
|
+
* const myshape = extrudeRotate({segments: 8, angle: TAU / 2}, circle({size: 3, center: [4, 0]}))
|
|
26
27
|
*/
|
|
27
28
|
const extrudeRotate = (options, geometry) => {
|
|
28
29
|
const defaults = {
|
|
29
30
|
segments: 12,
|
|
30
31
|
startAngle: 0,
|
|
31
|
-
angle:
|
|
32
|
+
angle: TAU,
|
|
32
33
|
overflow: 'cap'
|
|
33
34
|
}
|
|
34
35
|
let { segments, startAngle, angle, overflow } = Object.assign({}, defaults, options)
|
|
35
36
|
|
|
36
37
|
if (segments < 3) throw new Error('segments must be greater then 3')
|
|
37
38
|
|
|
38
|
-
startAngle = Math.abs(startAngle) >
|
|
39
|
-
angle = Math.abs(angle) >
|
|
39
|
+
startAngle = Math.abs(startAngle) > TAU ? startAngle % TAU : startAngle
|
|
40
|
+
angle = Math.abs(angle) > TAU ? angle % TAU : angle
|
|
40
41
|
|
|
41
42
|
let endAngle = startAngle + angle
|
|
42
|
-
endAngle = Math.abs(endAngle) >
|
|
43
|
+
endAngle = Math.abs(endAngle) > TAU ? endAngle % TAU : endAngle
|
|
43
44
|
|
|
44
45
|
if (endAngle < startAngle) {
|
|
45
46
|
const x = startAngle
|
|
@@ -47,11 +48,11 @@ const extrudeRotate = (options, geometry) => {
|
|
|
47
48
|
endAngle = x
|
|
48
49
|
}
|
|
49
50
|
let totalRotation = endAngle - startAngle
|
|
50
|
-
if (totalRotation <= 0.0) totalRotation =
|
|
51
|
+
if (totalRotation <= 0.0) totalRotation = TAU
|
|
51
52
|
|
|
52
|
-
if (Math.abs(totalRotation) <
|
|
53
|
+
if (Math.abs(totalRotation) < TAU) {
|
|
53
54
|
// adjust the segments to achieve the total rotation requested
|
|
54
|
-
const anglePerSegment =
|
|
55
|
+
const anglePerSegment = TAU / segments
|
|
55
56
|
segments = Math.floor(Math.abs(totalRotation) / anglePerSegment)
|
|
56
57
|
if (Math.abs(totalRotation) > (segments * anglePerSegment)) segments++
|
|
57
58
|
}
|
|
@@ -108,18 +109,18 @@ const extrudeRotate = (options, geometry) => {
|
|
|
108
109
|
}
|
|
109
110
|
|
|
110
111
|
const rotationPerSlice = totalRotation / segments
|
|
111
|
-
const isCapped = Math.abs(totalRotation) <
|
|
112
|
+
const isCapped = Math.abs(totalRotation) < TAU
|
|
112
113
|
const baseSlice = slice.fromSides(geom2.toSides(geometry))
|
|
113
114
|
slice.reverse(baseSlice, baseSlice)
|
|
114
115
|
|
|
115
116
|
const matrix = mat4.create()
|
|
116
117
|
const createSlice = (progress, index, base) => {
|
|
117
118
|
let Zrotation = rotationPerSlice * index + startAngle
|
|
118
|
-
// fix rounding error when rotating
|
|
119
|
-
if (totalRotation ===
|
|
119
|
+
// fix rounding error when rotating TAU radians
|
|
120
|
+
if (totalRotation === TAU && index === segments) {
|
|
120
121
|
Zrotation = startAngle
|
|
121
122
|
}
|
|
122
|
-
mat4.multiply(matrix, mat4.fromZRotation(matrix, Zrotation), mat4.fromXRotation(mat4.create(),
|
|
123
|
+
mat4.multiply(matrix, mat4.fromZRotation(matrix, Zrotation), mat4.fromXRotation(mat4.create(), TAU / 4))
|
|
123
124
|
|
|
124
125
|
return slice.transform(matrix, base)
|
|
125
126
|
}
|
|
@@ -2,6 +2,8 @@ const test = require('ava')
|
|
|
2
2
|
|
|
3
3
|
const { comparePoints, comparePolygonsAsPoints } = require('../../../test/helpers')
|
|
4
4
|
|
|
5
|
+
const { TAU } = require('../../maths/constants')
|
|
6
|
+
|
|
5
7
|
const { geom2, geom3 } = require('../../geometries')
|
|
6
8
|
|
|
7
9
|
const { extrudeRotate } = require('./index')
|
|
@@ -19,7 +21,7 @@ test('extrudeRotate: (angle) extruding of a geom2 produces an expected geom3', (
|
|
|
19
21
|
const geometry2 = geom2.fromPoints([[10, 8], [10, -8], [26, -8], [26, 8]])
|
|
20
22
|
|
|
21
23
|
// test angle
|
|
22
|
-
let geometry3 = extrudeRotate({ segments: 4, angle:
|
|
24
|
+
let geometry3 = extrudeRotate({ segments: 4, angle: TAU / 8 }, geometry2)
|
|
23
25
|
let pts = geom3.toPoints(geometry3)
|
|
24
26
|
const exp = [
|
|
25
27
|
[[10, 0, 8], [26, 0, 8], [18.38477631085024, 18.384776310850235, 8]],
|
|
@@ -54,7 +56,7 @@ test('extrudeRotate: (startAngle) extruding of a geom2 produces an expected geom
|
|
|
54
56
|
const geometry2 = geom2.fromPoints([[10, 8], [10, -8], [26, -8], [26, 8]])
|
|
55
57
|
|
|
56
58
|
// test startAngle
|
|
57
|
-
let geometry3 = extrudeRotate({ segments: 5, startAngle:
|
|
59
|
+
let geometry3 = extrudeRotate({ segments: 5, startAngle: TAU / 8 }, geometry2)
|
|
58
60
|
let pts = geom3.toPoints(geometry3)
|
|
59
61
|
let exp = [
|
|
60
62
|
[7.0710678118654755, 7.071067811865475, 8],
|
|
@@ -65,7 +67,7 @@ test('extrudeRotate: (startAngle) extruding of a geom2 produces an expected geom
|
|
|
65
67
|
t.is(pts.length, 40)
|
|
66
68
|
t.true(comparePoints(pts[0], exp))
|
|
67
69
|
|
|
68
|
-
geometry3 = extrudeRotate({ segments: 5, startAngle:
|
|
70
|
+
geometry3 = extrudeRotate({ segments: 5, startAngle: -TAU / 8 }, geometry2)
|
|
69
71
|
pts = geom3.toPoints(geometry3)
|
|
70
72
|
exp = [
|
|
71
73
|
[7.0710678118654755, -7.071067811865475, 8],
|
|
@@ -110,7 +112,7 @@ test('extrudeRotate: (overlap +/-) extruding of a geom2 produces an expected geo
|
|
|
110
112
|
// overlap of Y axis; even number of + and - points
|
|
111
113
|
let geometry = geom2.fromPoints([[-1, 8], [-1, -8], [7, -8], [7, 8]])
|
|
112
114
|
|
|
113
|
-
let obs = extrudeRotate({ segments: 4, angle:
|
|
115
|
+
let obs = extrudeRotate({ segments: 4, angle: TAU / 4 }, geometry)
|
|
114
116
|
let pts = geom3.toPoints(obs)
|
|
115
117
|
let exp = [
|
|
116
118
|
[[0, 0, 8], [7, 0, 8], [0, 7, 8]],
|
|
@@ -129,7 +131,7 @@ test('extrudeRotate: (overlap +/-) extruding of a geom2 produces an expected geo
|
|
|
129
131
|
// overlap of Y axis; larger number of - points
|
|
130
132
|
geometry = geom2.fromPoints([[-1, 8], [-2, 4], [-1, -8], [7, -8], [7, 8]])
|
|
131
133
|
|
|
132
|
-
obs = extrudeRotate({ segments: 8, angle:
|
|
134
|
+
obs = extrudeRotate({ segments: 8, angle: TAU / 4 }, geometry)
|
|
133
135
|
pts = geom3.toPoints(obs)
|
|
134
136
|
exp = [
|
|
135
137
|
[[1, 0, -8], [0, 0, -8], [0.7071067811865476, 0.7071067811865475, -8]],
|
|
@@ -10,6 +10,7 @@ module.exports = {
|
|
|
10
10
|
extrudeLinear: require('./extrudeLinear'),
|
|
11
11
|
extrudeRectangular: require('./extrudeRectangular'),
|
|
12
12
|
extrudeRotate: require('./extrudeRotate'),
|
|
13
|
+
extrudeHelical: require('./extrudeHelical'),
|
|
13
14
|
project: require('./project'),
|
|
14
15
|
slice: require('./slice')
|
|
15
16
|
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
const test = require('ava')
|
|
2
2
|
|
|
3
|
+
const { TAU } = require('../../../maths/constants')
|
|
3
4
|
const { mat4 } = require('../../../maths')
|
|
4
5
|
|
|
5
6
|
const { calculatePlane, fromPoints, transform } = require('./index')
|
|
@@ -15,11 +16,11 @@ test('slice: calculatePlane() returns correct plans for various slices', (t) =>
|
|
|
15
16
|
const plane2 = calculatePlane(slice2)
|
|
16
17
|
t.true(compareVectors(plane2, [0, 0, 1, 0]))
|
|
17
18
|
|
|
18
|
-
const slice3 = transform(mat4.fromXRotation(mat4.create(),
|
|
19
|
+
const slice3 = transform(mat4.fromXRotation(mat4.create(), TAU / 4), slice2)
|
|
19
20
|
const plane3 = calculatePlane(slice3)
|
|
20
21
|
t.true(compareVectors(plane3, [0, -1, 0, 0]))
|
|
21
22
|
|
|
22
|
-
const slice4 = transform(mat4.fromZRotation(mat4.create(),
|
|
23
|
+
const slice4 = transform(mat4.fromZRotation(mat4.create(), TAU / 4), slice3)
|
|
23
24
|
const plane4 = calculatePlane(slice4)
|
|
24
25
|
t.true(compareVectors(plane4, [1, 0, 0, 0]))
|
|
25
26
|
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { Geometry } from '../../geometries/types'
|
|
2
|
+
import RecursiveArray from '../../utils/recursiveArray'
|
|
3
|
+
|
|
4
|
+
export interface GeneralizeOptions {
|
|
5
|
+
snap?: boolean
|
|
6
|
+
simplify?: boolean
|
|
7
|
+
triangulate?: boolean
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export function generalize<T extends Geometry>(options: GeneralizeOptions, geometry: T): T
|
|
11
|
+
export function generalize<T extends Geometry>(options: GeneralizeOptions, ...geometries: RecursiveArray<T>): Array<T>
|
|
12
|
+
export function generalize(options: GeneralizeOptions, ...geometries: RecursiveArray<Geometry>): Array<Geometry>
|
|
@@ -2,6 +2,8 @@ const test = require('ava')
|
|
|
2
2
|
|
|
3
3
|
const { comparePolygonsAsPoints } = require('../../../test/helpers')
|
|
4
4
|
|
|
5
|
+
const { TAU } = require('../../maths/constants')
|
|
6
|
+
|
|
5
7
|
const { geom3 } = require('../../geometries')
|
|
6
8
|
|
|
7
9
|
const { cuboid } = require('../../primitives')
|
|
@@ -9,7 +11,7 @@ const { cuboid } = require('../../primitives')
|
|
|
9
11
|
const { generalize } = require('./index')
|
|
10
12
|
|
|
11
13
|
test('generalize: generalize of a geom3 produces an expected geom3', (t) => {
|
|
12
|
-
const geometry1 = cuboid({ size: [
|
|
14
|
+
const geometry1 = cuboid({ size: [TAU / 2, TAU / 4, TAU] })
|
|
13
15
|
|
|
14
16
|
// apply no modifications
|
|
15
17
|
let result = generalize({}, geometry1)
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { Geometry } from '../../geometries/types'
|
|
2
|
+
import RecursiveArray from '../../utils/recursiveArray'
|
|
3
|
+
|
|
4
|
+
export function snap<T extends Geometry>(geometry: T): T
|
|
5
|
+
export function snap<T extends Geometry>(...geometries: RecursiveArray<T>): Array<T>
|
|
6
|
+
export function snap(...geometries: RecursiveArray<Geometry>): Array<Geometry>
|
|
@@ -2,6 +2,8 @@ const test = require('ava')
|
|
|
2
2
|
|
|
3
3
|
const { comparePoints, comparePolygonsAsPoints } = require('../../../test/helpers')
|
|
4
4
|
|
|
5
|
+
const { TAU } = require('../../maths/constants')
|
|
6
|
+
|
|
5
7
|
const { geom2, geom3, path2 } = require('../../geometries')
|
|
6
8
|
|
|
7
9
|
const { arc, rectangle, cuboid } = require('../../primitives')
|
|
@@ -12,7 +14,7 @@ test('snap: snap of a path2 produces an expected path2', (t) => {
|
|
|
12
14
|
const geometry1 = path2.create()
|
|
13
15
|
const geometry2 = arc({ radius: 1 / 2, segments: 8 })
|
|
14
16
|
const geometry3 = arc({ radius: 1.3333333333333333 / 2, segments: 8 })
|
|
15
|
-
const geometry4 = arc({ radius:
|
|
17
|
+
const geometry4 = arc({ radius: TAU / 4 * 1000, segments: 8 })
|
|
16
18
|
|
|
17
19
|
const results = snap(geometry1, geometry2, geometry3, geometry4)
|
|
18
20
|
t.is(results.length, 4)
|
|
@@ -56,7 +58,7 @@ test('snap: snap of a geom2 produces an expected geom2', (t) => {
|
|
|
56
58
|
const geometry1 = geom2.create()
|
|
57
59
|
const geometry2 = rectangle({ size: [1, 1, 1] })
|
|
58
60
|
const geometry3 = rectangle({ size: [1.3333333333333333, 1.3333333333333333, 1.3333333333333333] })
|
|
59
|
-
const geometry4 = rectangle({ size: [
|
|
61
|
+
const geometry4 = rectangle({ size: [TAU / 2 * 1000, TAU / 2 * 1000, TAU / 2 * 1000] })
|
|
60
62
|
|
|
61
63
|
const results = snap(geometry1, geometry2, geometry3, geometry4)
|
|
62
64
|
t.is(results.length, 4)
|
|
@@ -88,7 +90,7 @@ test('snap: snap of a geom3 produces an expected geom3', (t) => {
|
|
|
88
90
|
const geometry1 = geom3.create()
|
|
89
91
|
const geometry2 = cuboid({ size: [1, 1, 1] })
|
|
90
92
|
const geometry3 = cuboid({ size: [1.3333333333333333, 1.3333333333333333, 1.3333333333333333] })
|
|
91
|
-
const geometry4 = cuboid({ size: [
|
|
93
|
+
const geometry4 = cuboid({ size: [TAU / 2 * 1000, TAU / 2 * 1000, TAU / 2 * 1000] })
|
|
92
94
|
|
|
93
95
|
const results = snap(geometry1, geometry2, geometry3, geometry4)
|
|
94
96
|
t.is(results.length, 4)
|
|
@@ -14,7 +14,7 @@ const path2 = require('../../geometries/path2')
|
|
|
14
14
|
* @alias module:modeling/transforms.rotate
|
|
15
15
|
*
|
|
16
16
|
* @example
|
|
17
|
-
* const newsphere = rotate([
|
|
17
|
+
* const newsphere = rotate([TAU / 8, 0, 0], sphere())
|
|
18
18
|
*/
|
|
19
19
|
const rotate = (angles, ...objects) => {
|
|
20
20
|
if (!Array.isArray(angles)) throw new Error('angles must be an array')
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
const test = require('ava')
|
|
2
2
|
|
|
3
|
+
const { TAU } = require('../../maths/constants')
|
|
4
|
+
|
|
3
5
|
const { geom2, geom3, path2 } = require('../../geometries')
|
|
4
6
|
|
|
5
7
|
const { rotate, rotateX, rotateY, rotateZ } = require('./index')
|
|
@@ -10,7 +12,7 @@ test('rotate: rotating of a path2 produces expected changes to points', (t) => {
|
|
|
10
12
|
const geometry = path2.fromPoints({}, [[1, 0], [0, 1], [-1, 0]])
|
|
11
13
|
|
|
12
14
|
// rotate about Z
|
|
13
|
-
let rotated = rotate([0, 0,
|
|
15
|
+
let rotated = rotate([0, 0, TAU / 4], geometry)
|
|
14
16
|
let obs = path2.toPoints(rotated)
|
|
15
17
|
const exp = [
|
|
16
18
|
new Float32Array([0, 1]),
|
|
@@ -19,7 +21,7 @@ test('rotate: rotating of a path2 produces expected changes to points', (t) => {
|
|
|
19
21
|
]
|
|
20
22
|
t.true(comparePoints(obs, exp))
|
|
21
23
|
|
|
22
|
-
rotated = rotateZ(
|
|
24
|
+
rotated = rotateZ(TAU / 4, geometry)
|
|
23
25
|
obs = path2.toPoints(rotated)
|
|
24
26
|
t.notThrows(() => path2.validate(rotated))
|
|
25
27
|
t.true(comparePoints(obs, exp))
|
|
@@ -29,7 +31,7 @@ test('rotate: rotating of a geom2 produces expected changes to points', (t) => {
|
|
|
29
31
|
const geometry = geom2.fromPoints([[0, 0], [1, 0], [0, 1]])
|
|
30
32
|
|
|
31
33
|
// rotate about Z
|
|
32
|
-
let rotated = rotate([0, 0, -
|
|
34
|
+
let rotated = rotate([0, 0, -TAU / 4], geometry)
|
|
33
35
|
let obs = geom2.toPoints(rotated)
|
|
34
36
|
const exp = [
|
|
35
37
|
new Float32Array([0, 0]),
|
|
@@ -39,7 +41,7 @@ test('rotate: rotating of a geom2 produces expected changes to points', (t) => {
|
|
|
39
41
|
t.notThrows(() => geom2.validate(rotated))
|
|
40
42
|
t.true(comparePoints(obs, exp))
|
|
41
43
|
|
|
42
|
-
rotated = rotateZ(-
|
|
44
|
+
rotated = rotateZ(-TAU / 4, geometry)
|
|
43
45
|
obs = geom2.toPoints(rotated)
|
|
44
46
|
t.notThrows(() => geom2.validate(rotated))
|
|
45
47
|
t.true(comparePoints(obs, exp))
|
|
@@ -57,7 +59,7 @@ test('rotate: rotating of a geom3 produces expected changes to polygons', (t) =>
|
|
|
57
59
|
const geometry = geom3.fromPoints(points)
|
|
58
60
|
|
|
59
61
|
// rotate about X
|
|
60
|
-
let rotated = rotate([
|
|
62
|
+
let rotated = rotate([TAU / 4], geometry)
|
|
61
63
|
let obs = geom3.toPoints(rotated)
|
|
62
64
|
let exp = [
|
|
63
65
|
[[-2, 12, -7.000000000000001], [-2, -18, -6.999999999999999],
|
|
@@ -76,13 +78,13 @@ test('rotate: rotating of a geom3 produces expected changes to polygons', (t) =>
|
|
|
76
78
|
t.notThrows(() => geom3.validate(rotated))
|
|
77
79
|
t.true(comparePolygonsAsPoints(obs, exp))
|
|
78
80
|
|
|
79
|
-
rotated = rotateX(
|
|
81
|
+
rotated = rotateX(TAU / 4, geometry)
|
|
80
82
|
obs = geom3.toPoints(rotated)
|
|
81
83
|
t.notThrows(() => geom3.validate(rotated))
|
|
82
84
|
t.true(comparePolygonsAsPoints(obs, exp))
|
|
83
85
|
|
|
84
86
|
// rotate about Y
|
|
85
|
-
rotated = rotate([0, -
|
|
87
|
+
rotated = rotate([0, -TAU / 4], geometry)
|
|
86
88
|
obs = geom3.toPoints(rotated)
|
|
87
89
|
exp = [
|
|
88
90
|
[[12, -7, -2.000000000000001], [-18, -7, -1.999999999999999],
|
|
@@ -101,12 +103,12 @@ test('rotate: rotating of a geom3 produces expected changes to polygons', (t) =>
|
|
|
101
103
|
t.notThrows(() => geom3.validate(rotated))
|
|
102
104
|
t.true(comparePolygonsAsPoints(obs, exp))
|
|
103
105
|
|
|
104
|
-
rotated = rotateY(-
|
|
106
|
+
rotated = rotateY(-TAU / 4, geometry)
|
|
105
107
|
obs = geom3.toPoints(rotated)
|
|
106
108
|
t.true(comparePolygonsAsPoints(obs, exp))
|
|
107
109
|
|
|
108
110
|
// rotate about Z
|
|
109
|
-
rotated = rotate([0, 0,
|
|
111
|
+
rotated = rotate([0, 0, TAU / 2], geometry)
|
|
110
112
|
obs = geom3.toPoints(rotated)
|
|
111
113
|
exp = [
|
|
112
114
|
[[2.000000000000001, 7, -12], [2.000000000000001, 7, 18],
|
|
@@ -125,7 +127,7 @@ test('rotate: rotating of a geom3 produces expected changes to polygons', (t) =>
|
|
|
125
127
|
t.notThrows(() => geom3.validate(rotated))
|
|
126
128
|
t.true(comparePolygonsAsPoints(obs, exp))
|
|
127
129
|
|
|
128
|
-
rotated = rotateZ(
|
|
130
|
+
rotated = rotateZ(TAU / 2, geometry)
|
|
129
131
|
obs = geom3.toPoints(rotated)
|
|
130
132
|
t.notThrows(() => geom3.validate(rotated))
|
|
131
133
|
t.true(comparePolygonsAsPoints(obs, exp))
|
|
@@ -136,7 +138,7 @@ test('rotate: rotating of multiple objects produces expected changes', (t) => {
|
|
|
136
138
|
const geometry1 = path2.fromPoints({}, [[-5, 5], [5, 5], [-5, -5], [10, -5]])
|
|
137
139
|
const geometry2 = geom2.fromPoints([[-5, -5], [0, 5], [10, -5]])
|
|
138
140
|
|
|
139
|
-
const rotated = rotate([0, 0,
|
|
141
|
+
const rotated = rotate([0, 0, TAU / 4], junk, geometry1, geometry2)
|
|
140
142
|
|
|
141
143
|
t.is(rotated[0], junk)
|
|
142
144
|
|
|
@@ -12,7 +12,7 @@ const path2 = require('../../geometries/path2')
|
|
|
12
12
|
* @alias module:modeling/transforms.transform
|
|
13
13
|
*
|
|
14
14
|
* @example
|
|
15
|
-
* const newsphere = transform(mat4.rotateX(
|
|
15
|
+
* const newsphere = transform(mat4.rotateX(TAU / 8), sphere())
|
|
16
16
|
*/
|
|
17
17
|
const transform = (matrix, ...objects) => {
|
|
18
18
|
// TODO how to check that the matrix is REAL?
|