@jscad/modeling 2.9.4 → 2.10.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.
Files changed (106) hide show
  1. package/CHANGELOG.md +50 -0
  2. package/README.md +12 -2
  3. package/dist/jscad-modeling.min.js +148 -151
  4. package/package.json +2 -2
  5. package/src/colors/colorize.d.ts +6 -5
  6. package/src/geometries/geom2/type.d.ts +3 -2
  7. package/src/geometries/geom3/type.d.ts +3 -2
  8. package/src/geometries/path2/appendArc.js +6 -5
  9. package/src/geometries/path2/appendArc.test.js +3 -1
  10. package/src/geometries/path2/appendBezier.js +2 -1
  11. package/src/geometries/path2/appendPoints.js +3 -12
  12. package/src/geometries/path2/appendPoints.test.js +16 -0
  13. package/src/geometries/path2/concat.js +9 -8
  14. package/src/geometries/path2/concat.test.js +13 -7
  15. package/src/geometries/path2/transform.js +1 -1
  16. package/src/geometries/path2/type.d.ts +3 -2
  17. package/src/geometries/poly3/measureBoundingSphere.d.ts +2 -2
  18. package/src/geometries/poly3/measureBoundingSphere.js +46 -8
  19. package/src/geometries/poly3/measureBoundingSphere.test.js +16 -26
  20. package/src/geometries/poly3/type.d.ts +3 -2
  21. package/src/geometries/types.d.ts +4 -2
  22. package/src/index.d.ts +1 -0
  23. package/src/maths/constants.js +10 -0
  24. package/src/maths/mat4/fromRotation.js +10 -8
  25. package/src/maths/mat4/fromTaitBryanRotation.js +9 -7
  26. package/src/maths/mat4/fromXRotation.js +5 -3
  27. package/src/maths/mat4/fromYRotation.js +5 -3
  28. package/src/maths/mat4/fromZRotation.js +5 -3
  29. package/src/maths/mat4/invert.test.js +5 -2
  30. package/src/maths/mat4/isOnlyTransformScale.js +1 -1
  31. package/src/maths/mat4/isOnlyTransformScale.test.js +3 -1
  32. package/src/maths/mat4/rotate.js +9 -5
  33. package/src/maths/mat4/rotateX.js +4 -2
  34. package/src/maths/mat4/rotateY.js +4 -2
  35. package/src/maths/mat4/rotateZ.js +4 -2
  36. package/src/maths/mat4/translate.test.js +2 -3
  37. package/src/maths/rotation.test.js +5 -2
  38. package/src/maths/utils/index.d.ts +1 -0
  39. package/src/maths/utils/index.js +2 -0
  40. package/src/{utils → maths/utils}/trigonometry.d.ts +0 -0
  41. package/src/{utils → maths/utils}/trigonometry.js +7 -7
  42. package/src/{utils → maths/utils}/trigonometry.test.js +10 -8
  43. package/src/maths/vec2/distance.js +1 -1
  44. package/src/maths/vec2/fromAngleDegrees.js +1 -1
  45. package/src/maths/vec2/fromAngleRadians.js +4 -2
  46. package/src/maths/vec2/fromAngleRadians.test.js +4 -1
  47. package/src/maths/vec2/length.js +1 -1
  48. package/src/maths/vec2/length.test.js +0 -10
  49. package/src/maths/vec2/normal.js +3 -1
  50. package/src/maths/vec2/rotate.test.js +4 -1
  51. package/src/maths/vec3/angle.js +2 -2
  52. package/src/maths/vec3/angle.test.js +0 -12
  53. package/src/maths/vec3/distance.js +1 -1
  54. package/src/maths/vec3/length.js +1 -1
  55. package/src/maths/vec3/length.test.js +0 -10
  56. package/src/maths/vec3/rotateX.test.js +4 -1
  57. package/src/maths/vec3/rotateY.test.js +4 -1
  58. package/src/maths/vec3/rotateZ.test.js +4 -1
  59. package/src/operations/booleans/trees/PolygonTreeNode.js +2 -2
  60. package/src/operations/expansions/expand.test.js +3 -1
  61. package/src/operations/expansions/expandShell.js +4 -4
  62. package/src/operations/expansions/offsetFromPoints.js +2 -2
  63. package/src/operations/extrusions/extrudeFromSlices.test.js +3 -2
  64. package/src/operations/extrusions/extrudeLinear.test.js +5 -3
  65. package/src/operations/extrusions/extrudeRectangular.js +1 -1
  66. package/src/operations/extrusions/extrudeRectangular.test.js +5 -3
  67. package/src/operations/extrusions/extrudeRotate.js +14 -13
  68. package/src/operations/extrusions/extrudeRotate.test.js +49 -47
  69. package/src/operations/extrusions/project.test.js +2 -2
  70. package/src/operations/extrusions/slice/calculatePlane.test.js +3 -2
  71. package/src/operations/extrusions/slice/index.js +2 -0
  72. package/src/operations/extrusions/slice/repair.js +1 -1
  73. package/src/operations/modifiers/generalize.d.ts +12 -0
  74. package/src/operations/modifiers/generalize.test.js +3 -1
  75. package/src/operations/modifiers/index.d.ts +2 -0
  76. package/src/operations/modifiers/insertTjunctions.js +34 -35
  77. package/src/operations/modifiers/snap.d.ts +6 -0
  78. package/src/operations/modifiers/snap.test.js +5 -3
  79. package/src/operations/transforms/rotate.js +1 -1
  80. package/src/operations/transforms/rotate.test.js +13 -11
  81. package/src/operations/transforms/transform.js +1 -1
  82. package/src/primitives/arc.js +8 -8
  83. package/src/primitives/arc.test.js +9 -8
  84. package/src/primitives/circle.js +4 -2
  85. package/src/primitives/circle.test.js +12 -4
  86. package/src/primitives/cylinderElliptic.js +13 -11
  87. package/src/primitives/cylinderElliptic.test.js +9 -2
  88. package/src/primitives/ellipse.js +11 -11
  89. package/src/primitives/ellipse.test.js +12 -4
  90. package/src/primitives/ellipsoid.js +4 -3
  91. package/src/primitives/geodesicSphere.js +3 -2
  92. package/src/primitives/roundedCuboid.js +10 -8
  93. package/src/primitives/roundedCylinder.js +4 -4
  94. package/src/primitives/roundedRectangle.js +5 -5
  95. package/src/primitives/star.js +3 -2
  96. package/src/primitives/torus.js +4 -2
  97. package/src/primitives/torus.test.js +11 -5
  98. package/src/primitives/triangle.test.js +2 -1
  99. package/src/utils/degToRad.test.js +5 -5
  100. package/src/utils/index.d.ts +0 -1
  101. package/src/utils/index.js +1 -3
  102. package/src/utils/radToDeg.test.js +6 -6
  103. package/src/utils/radiusToSegments.js +6 -4
  104. package/src/utils/radiusToSegments.test.js +5 -3
  105. package/src/maths/mat4/constants.d.ts +0 -1
  106. package/src/maths/mat4/constants.js +0 -5
@@ -1,9 +1,11 @@
1
1
  const test = require('ava')
2
2
 
3
+ const { TAU } = require('../constants')
4
+
3
5
  const { isOnlyTransformScale, create, fromTranslation, fromTaitBryanRotation, fromScaling, invert, multiply } = require('./index')
4
6
 
5
7
  test('mat4: isOnlyTransformScale() should return true for right angles', (t) => {
6
- let someRotation = fromTaitBryanRotation(create(), Math.PI, 0, 0)
8
+ let someRotation = fromTaitBryanRotation(create(), TAU / 2, 0, 0)
7
9
  t.true(isOnlyTransformScale(someRotation))
8
10
  t.true(isOnlyTransformScale(invert(create(), someRotation)))
9
11
 
@@ -1,3 +1,7 @@
1
+ const { EPS } = require('../constants')
2
+
3
+ const { sin, cos } = require('../utils/trigonometry')
4
+
1
5
  const copy = require('./copy')
2
6
 
3
7
  /**
@@ -12,20 +16,20 @@ const copy = require('./copy')
12
16
  */
13
17
  const rotate = (out, matrix, radians, axis) => {
14
18
  let [x, y, z] = axis
15
- let len = Math.hypot(x, y, z)
19
+ const lengthSquared = x * x + y * y + z * z
16
20
 
17
- if (Math.abs(len) < 0.000001) {
21
+ if (Math.abs(lengthSquared) < EPS) {
18
22
  // axis is 0,0,0 or almost
19
23
  return copy(out, matrix)
20
24
  }
21
25
 
22
- len = 1 / len
26
+ const len = 1 / Math.sqrt(lengthSquared)
23
27
  x *= len
24
28
  y *= len
25
29
  z *= len
26
30
 
27
- const s = Math.sin(radians)
28
- const c = Math.cos(radians)
31
+ const s = sin(radians)
32
+ const c = cos(radians)
29
33
  const t = 1 - c
30
34
 
31
35
  const a00 = matrix[0]
@@ -1,3 +1,5 @@
1
+ const { sin, cos } = require('../utils/trigonometry')
2
+
1
3
  /**
2
4
  * Rotates a matrix by the given angle around the X axis.
3
5
  *
@@ -8,8 +10,8 @@
8
10
  * @alias module:modeling/maths/mat4.rotateX
9
11
  */
10
12
  const rotateX = (out, matrix, radians) => {
11
- const s = Math.sin(radians)
12
- const c = Math.cos(radians)
13
+ const s = sin(radians)
14
+ const c = cos(radians)
13
15
  const a10 = matrix[4]
14
16
  const a11 = matrix[5]
15
17
  const a12 = matrix[6]
@@ -1,3 +1,5 @@
1
+ const { sin, cos } = require('../utils/trigonometry')
2
+
1
3
  /**
2
4
  * Rotates a matrix by the given angle around the Y axis.
3
5
  *
@@ -8,8 +10,8 @@
8
10
  * @alias module:modeling/maths/mat4.rotateY
9
11
  */
10
12
  const rotateY = (out, matrix, radians) => {
11
- const s = Math.sin(radians)
12
- const c = Math.cos(radians)
13
+ const s = sin(radians)
14
+ const c = cos(radians)
13
15
  const a00 = matrix[0]
14
16
  const a01 = matrix[1]
15
17
  const a02 = matrix[2]
@@ -1,3 +1,5 @@
1
+ const { sin, cos } = require('../utils/trigonometry')
2
+
1
3
  /**
2
4
  * Rotates a matrix by the given angle around the Z axis.
3
5
  *
@@ -8,8 +10,8 @@
8
10
  * @alias module:modeling/maths/mat4.rotateZ
9
11
  */
10
12
  const rotateZ = (out, matrix, radians) => {
11
- const s = Math.sin(radians)
12
- const c = Math.cos(radians)
13
+ const s = sin(radians)
14
+ const c = cos(radians)
13
15
  const a00 = matrix[0]
14
16
  const a01 = matrix[1]
15
17
  const a02 = matrix[2]
@@ -52,10 +52,9 @@ test('mat4: translate() called with three parameters should update a mat4 with c
52
52
  t.true(compareVectors(obs4, [1, 0, 0, 0, 0, 3, 0, 0, 0, 0, 5, 0, 2, 9, 30, 1]))
53
53
  t.true(compareVectors(ret4, [1, 0, 0, 0, 0, 3, 0, 0, 0, 0, 5, 0, 2, 9, 30, 1]))
54
54
 
55
- const r = (90 * 0.017453292519943295)
56
55
  const rotateZMatrix = [
57
- Math.cos(r), -Math.sin(r), 0, 0,
58
- Math.sin(r), Math.cos(r), 0, 0,
56
+ 0, -1, 0, 0,
57
+ 1, 0, 0, 0,
59
58
  0, 0, 1, 0,
60
59
  0, 0, 0, 1
61
60
  ]
@@ -2,15 +2,18 @@ const test = require('ava')
2
2
 
3
3
  const { compareVectors } = require('../../test/helpers/index')
4
4
 
5
- const { mat4, vec2, vec3 } = require('./index')
5
+ const { constants, mat4, vec2, vec3 } = require('./index')
6
6
 
7
7
  // ALL POSITIVE ROTATIONS ARE CLOCKWISE
8
8
  // see https://webglfundamentals.org/webgl/lessons/webgl-3d-orthographic.html
9
9
  // IN A LEFT-HANDED COORDINATE SYSTEM
10
10
 
11
+ // JSCAD IS RIGHT-HANDED COORDINATE SYSTEM
12
+ // WHERE POSITIVE ROTATIONS ARE COUNTER-CLOCKWISE
13
+
11
14
  // identity matrices for comparisons
12
15
 
13
- const rad90 = Math.PI / 2
16
+ const rad90 = constants.TAU / 4
14
17
 
15
18
  // +90 degree rotation about X
16
19
  const cwX90Matrix = [
@@ -3,5 +3,6 @@ export { default as area } from './area'
3
3
  export { default as interpolateBetween2DPointsForY } from './interpolateBetween2DPointsForY'
4
4
  export { default as intersect } from './intersect'
5
5
  export { default as solve2Linear } from './solve2Linear'
6
+ export { sin, cos } from './trigonometry'
6
7
 
7
8
  export as namespace utils
@@ -7,7 +7,9 @@
7
7
  module.exports = {
8
8
  aboutEqualNormals: require('./aboutEqualNormals'),
9
9
  area: require('./area'),
10
+ cos: require('./trigonometry').cos,
10
11
  interpolateBetween2DPointsForY: require('./interpolateBetween2DPointsForY'),
11
12
  intersect: require('./intersect'),
13
+ sin: require('./trigonometry').sin,
12
14
  solve2Linear: require('./solve2Linear')
13
15
  }
File without changes
@@ -1,4 +1,4 @@
1
- const { NEPS } = require('../maths/constants')
1
+ const { NEPS } = require('../constants')
2
2
 
3
3
  /*
4
4
  * Returns zero if n is within epsilon of zero, otherwise return n
@@ -6,28 +6,28 @@ const { NEPS } = require('../maths/constants')
6
6
  const rezero = (n) => Math.abs(n) < NEPS ? 0 : n
7
7
 
8
8
  /**
9
- * Return Math.sin but accurate for 90 degree rotations.
9
+ * Return Math.sin but accurate for TAU / 4 rotations.
10
10
  * Fixes rounding errors when sin should be 0.
11
11
  *
12
12
  * @param {Number} radians - angle in radians
13
13
  * @returns {Number} sine of the given angle
14
14
  * @alias module:modeling/utils.sin
15
15
  * @example
16
- * sin(Math.PI) == 0
17
- * sin(2 * Math.PI) == 0
16
+ * sin(TAU / 2) == 0
17
+ * sin(TAU) == 0
18
18
  */
19
19
  const sin = (radians) => rezero(Math.sin(radians))
20
20
 
21
21
  /**
22
- * Return Math.cos but accurate for 90 degree rotations.
22
+ * Return Math.cos but accurate for TAU / 4 rotations.
23
23
  * Fixes rounding errors when cos should be 0.
24
24
  *
25
25
  * @param {Number} radians - angle in radians
26
26
  * @returns {Number} cosine of the given angle
27
27
  * @alias module:modeling/utils.cos
28
28
  * @example
29
- * cos(0.5 * Math.PI) == 0
30
- * cos(1.5 * Math.PI) == 0
29
+ * cos(TAU * 0.25) == 0
30
+ * cos(TAU * 0.75) == 0
31
31
  */
32
32
  const cos = (radians) => rezero(Math.cos(radians))
33
33
 
@@ -1,14 +1,16 @@
1
1
  const test = require('ava')
2
2
 
3
+ const { TAU } = require('../constants')
4
+
3
5
  const { cos, sin } = require('./trigonometry')
4
6
 
5
7
  test('utils: sin() should return rounded values', (t) => {
6
8
  t.is(sin(0), 0)
7
9
  t.is(sin(9), Math.sin(9))
8
- t.is(sin(0.5 * Math.PI), 1)
9
- t.is(sin(1.0 * Math.PI), 0)
10
- t.is(sin(1.5 * Math.PI), -1)
11
- t.is(sin(2.0 * Math.PI), 0)
10
+ t.is(sin(0.25 * TAU), 1)
11
+ t.is(sin(0.5 * TAU), 0)
12
+ t.is(sin(0.75 * TAU), -1)
13
+ t.is(sin(TAU), 0)
12
14
  t.is(sin(NaN), NaN)
13
15
  t.is(sin(Infinity), NaN)
14
16
  })
@@ -16,10 +18,10 @@ test('utils: sin() should return rounded values', (t) => {
16
18
  test('utils: cos() should return rounded values', (t) => {
17
19
  t.is(cos(0), 1)
18
20
  t.is(cos(9), Math.cos(9))
19
- t.is(cos(0.5 * Math.PI), 0)
20
- t.is(cos(1.0 * Math.PI), -1)
21
- t.is(cos(1.5 * Math.PI), 0)
22
- t.is(cos(2.0 * Math.PI), 1)
21
+ t.is(cos(0.25 * TAU), 0)
22
+ t.is(cos(0.5 * TAU), -1)
23
+ t.is(cos(0.75 * TAU), 0)
24
+ t.is(cos(TAU), 1)
23
25
  t.is(cos(NaN), NaN)
24
26
  t.is(cos(Infinity), NaN)
25
27
  })
@@ -9,7 +9,7 @@
9
9
  const distance = (a, b) => {
10
10
  const x = b[0] - a[0]
11
11
  const y = b[1] - a[1]
12
- return Math.hypot(x, y)
12
+ return Math.sqrt(x * x + y * y)
13
13
  }
14
14
 
15
15
  module.exports = distance
@@ -8,6 +8,6 @@ const fromAngleRadians = require('./fromAngleRadians')
8
8
  * @returns {vec2} out
9
9
  * @alias module:modeling/maths/vec2.fromAngleDegrees
10
10
  */
11
- const fromAngleDegrees = (out, degrees) => fromAngleRadians(out, Math.PI * degrees / 180)
11
+ const fromAngleDegrees = (out, degrees) => fromAngleRadians(out, degrees * 0.017453292519943295)
12
12
 
13
13
  module.exports = fromAngleDegrees
@@ -1,3 +1,5 @@
1
+ const { sin, cos } = require('../utils/trigonometry')
2
+
1
3
  /**
2
4
  * Create a new vector in the direction of the given angle.
3
5
  *
@@ -7,8 +9,8 @@
7
9
  * @alias module:modeling/maths/vec2.fromAngleRadians
8
10
  */
9
11
  const fromAngleRadians = (out, radians) => {
10
- out[0] = Math.cos(radians)
11
- out[1] = Math.sin(radians)
12
+ out[0] = cos(radians)
13
+ out[1] = sin(radians)
12
14
  return out
13
15
  }
14
16
 
@@ -1,4 +1,7 @@
1
1
  const test = require('ava')
2
+
3
+ const { TAU } = require('../constants')
4
+
2
5
  const { fromAngleRadians, create } = require('./index')
3
6
 
4
7
  const { compareVectors } = require('../../../test/helpers/index')
@@ -7,6 +10,6 @@ test('vec2: fromAngleRadians() should return a new vec2 with correct values', (t
7
10
  const obs1 = fromAngleRadians(create(), 0)
8
11
  t.true(compareVectors(obs1, [1.0, 0.0]))
9
12
 
10
- const obs2 = fromAngleRadians(obs1, Math.PI)
13
+ const obs2 = fromAngleRadians(obs1, TAU / 2)
11
14
  t.true(compareVectors(obs2, [-1, 1.2246468525851679e-16]))
12
15
  })
@@ -5,6 +5,6 @@
5
5
  * @returns {Number} length
6
6
  * @alias module:modeling/maths/vec2.length
7
7
  */
8
- const length = (vector) => Math.hypot(vector[0], vector[1])
8
+ const length = (vector) => Math.sqrt(vector[0] * vector[0] + vector[1] * vector[1])
9
9
 
10
10
  module.exports = length
@@ -25,15 +25,5 @@ test('vec2: length() should return correct values', (t) => {
25
25
  const length5 = length(vec5)
26
26
  nearlyEqual(t, length5, 2.23606, EPS)
27
27
 
28
- // huge vector
29
- const vec6 = fromValues(1e200, 1e200)
30
- const length6 = length(vec6)
31
- nearlyEqual(t, length6, Math.SQRT2 * 1e200, EPS)
32
-
33
- // tiny vector
34
- const vec7 = fromValues(1e-200, 1e-200)
35
- const length7 = length(vec7)
36
- nearlyEqual(t, length7, Math.SQRT2 * 1e-200, EPS)
37
-
38
28
  t.true(true)
39
29
  })
@@ -1,3 +1,5 @@
1
+ const { TAU } = require('../constants')
2
+
1
3
  const create = require('./create')
2
4
  const rotate = require('./rotate')
3
5
 
@@ -10,6 +12,6 @@ const rotate = require('./rotate')
10
12
  * @returns {vec2} out
11
13
  * @alias module:modeling/maths/vec2.normal
12
14
  */
13
- const normal = (out, vector) => rotate(out, vector, create(), (Math.PI / 2))
15
+ const normal = (out, vector) => rotate(out, vector, create(), (TAU / 4))
14
16
 
15
17
  module.exports = normal
@@ -1,10 +1,13 @@
1
1
  const test = require('ava')
2
+
3
+ const { TAU } = require('../constants')
4
+
2
5
  const { rotate, fromValues } = require('./index')
3
6
 
4
7
  const { compareVectors } = require('../../../test/helpers/index')
5
8
 
6
9
  test('vec2: rotate() called with three parameters should update a vec2 with correct values', (t) => {
7
- const radians = 90 * Math.PI / 180
10
+ const radians = TAU / 4
8
11
 
9
12
  const obs1 = fromValues(0, 0)
10
13
  const ret1 = rotate(obs1, [0, 0], [0, 0], 0)
@@ -15,8 +15,8 @@ const angle = (a, b) => {
15
15
  const bx = b[0]
16
16
  const by = b[1]
17
17
  const bz = b[2]
18
- const mag1 = Math.hypot(ax, ay, az)
19
- const mag2 = Math.hypot(bx, by, bz)
18
+ const mag1 = Math.sqrt(ax * ax + ay * ay + az * az)
19
+ const mag2 = Math.sqrt(bx * bx + by * by + bz * bz)
20
20
  const mag = mag1 * mag2
21
21
  const cosine = mag && dot(a, b) / mag
22
22
  return Math.acos(Math.min(Math.max(cosine, -1), 1))
@@ -30,17 +30,5 @@ test('vec3: angle() should return correct values', (t) => {
30
30
  const angle5 = angle(vec5a, vec5b)
31
31
  nearlyEqual(t, angle5, 0.785398, EPS)
32
32
 
33
- // tiny values
34
- const vec6a = fromValues(1, 0, 0)
35
- const vec6b = fromValues(1e-200, 1e-200, 0)
36
- const angle6 = angle(vec6a, vec6b)
37
- nearlyEqual(t, angle6, 0.785398, EPS)
38
-
39
- // huge values
40
- const vec7a = fromValues(1, 0, 0)
41
- const vec7b = fromValues(1e200, 1e200, 0)
42
- const angle7 = angle(vec7a, vec7b)
43
- nearlyEqual(t, angle7, 0.785398, EPS)
44
-
45
33
  t.true(true)
46
34
  })
@@ -10,7 +10,7 @@ const distance = (a, b) => {
10
10
  const x = b[0] - a[0]
11
11
  const y = b[1] - a[1]
12
12
  const z = b[2] - a[2]
13
- return Math.hypot(x, y, z)
13
+ return Math.sqrt(x * x + y * y + z * z)
14
14
  }
15
15
 
16
16
  module.exports = distance
@@ -9,7 +9,7 @@ const length = (vector) => {
9
9
  const x = vector[0]
10
10
  const y = vector[1]
11
11
  const z = vector[2]
12
- return Math.hypot(x, y, z)
12
+ return Math.sqrt(x * x + y * y + z * z)
13
13
  }
14
14
 
15
15
  module.exports = length
@@ -41,15 +41,5 @@ test('vec3: length() should return correct values', (t) => {
41
41
  const length9 = length(vec9)
42
42
  nearlyEqual(t, length9, 3.74165, EPS)
43
43
 
44
- // huge vector
45
- const vec10 = fromValues(1e200, 0, 1e200)
46
- const length10 = length(vec10)
47
- nearlyEqual(t, length10, Math.SQRT2 * 1e200, EPS)
48
-
49
- // tiny vector
50
- const vec11 = fromValues(1e-200, 0, 1e-200)
51
- const length11 = length(vec11)
52
- nearlyEqual(t, length11, Math.SQRT2 * 1e-200, EPS)
53
-
54
44
  t.true(true)
55
45
  })
@@ -1,10 +1,13 @@
1
1
  const test = require('ava')
2
+
3
+ const { TAU } = require('../constants')
4
+
2
5
  const { rotateX, fromValues } = require('./index')
3
6
 
4
7
  const { compareVectors } = require('../../../test/helpers/index')
5
8
 
6
9
  test('vec3: rotateX() called with four parameters should update a vec3 with correct values', (t) => {
7
- const radians = 90 * Math.PI / 180
10
+ const radians = TAU / 4
8
11
 
9
12
  const obs1 = fromValues(0, 0, 0)
10
13
  const ret1 = rotateX(obs1, [0, 0, 0], [0, 0, 0], 0)
@@ -1,10 +1,13 @@
1
1
  const test = require('ava')
2
+
3
+ const { TAU } = require('../constants')
4
+
2
5
  const { rotateY, fromValues } = require('./index')
3
6
 
4
7
  const { compareVectors } = require('../../../test/helpers/index')
5
8
 
6
9
  test('vec3: rotateY() called with three parameters should update a vec3 with correct values', (t) => {
7
- const radians = 90 * Math.PI / 180
10
+ const radians = TAU / 4
8
11
 
9
12
  const obs1 = fromValues(0, 0, 0)
10
13
  const ret1 = rotateY(obs1, [0, 0, 0], [0, 0, 0], 0)
@@ -1,10 +1,13 @@
1
1
  const test = require('ava')
2
+
3
+ const { TAU } = require('../constants')
4
+
2
5
  const { rotateZ, fromValues } = require('./index')
3
6
 
4
7
  const { compareVectors } = require('../../../test/helpers/index')
5
8
 
6
9
  test('vec3: rotateZ() called with four parameters should update a vec3 with correct values', (t) => {
7
- const radians = 90 * Math.PI / 180
10
+ const radians = TAU / 4
8
11
 
9
12
  const obs1 = fromValues(0, 0, 0)
10
13
  const ret1 = rotateZ(obs1, [0, 0, 0], [0, 0, 0], 0)
@@ -132,8 +132,8 @@ class PolygonTreeNode {
132
132
  const polygon = this.polygon
133
133
  if (polygon) {
134
134
  const bound = poly3.measureBoundingSphere(polygon)
135
- const sphereradius = bound[1] + EPS // ensure radius is LARGER then polygon
136
- const spherecenter = bound[0]
135
+ const sphereradius = bound[3] + EPS // ensure radius is LARGER then polygon
136
+ const spherecenter = bound
137
137
  const d = vec3.dot(splane, spherecenter) - splane[3]
138
138
  if (d > sphereradius) {
139
139
  frontnodes.push(this)
@@ -1,9 +1,11 @@
1
1
  const test = require('ava')
2
2
 
3
3
  const { comparePoints, nearlyEqual } = require('../../../test/helpers')
4
+
4
5
  const { geom2, geom3, path2 } = require('../../geometries')
5
6
  const measureBoundingBox = require('../../measurements/measureBoundingBox')
6
7
  const area = require('../../maths/utils/area')
8
+ const { TAU } = require('../../maths/constants')
7
9
  const sphere = require('../../primitives/sphere')
8
10
 
9
11
  const { expand } = require('./index')
@@ -51,7 +53,7 @@ test('expand: round-expanding a bent line produces expected geometry', (t) => {
51
53
  const expandedPoints = geom2.toPoints(expandedPathGeom2)
52
54
 
53
55
  t.notThrows(() => geom2.validate(expandedPathGeom2))
54
- const expectedArea = 56 + 2 * Math.PI * delta * 1.25 // shape will have 1 and 1/4 circles
56
+ const expectedArea = 56 + TAU * delta * 1.25 // shape will have 1 and 1/4 circles
55
57
  nearlyEqual(t, area(expandedPoints), expectedArea, 0.01, 'Measured area should be pretty close')
56
58
  const boundingBox = measureBoundingBox(expandedPathGeom2)
57
59
  t.true(comparePoints(boundingBox, [[-7, -2, 0], [2, 12, 0]]), 'Unexpected bounding box: ' + JSON.stringify(boundingBox))
@@ -1,4 +1,4 @@
1
- const { EPS } = require('../../maths/constants')
1
+ const { EPS, TAU } = require('../../maths/constants')
2
2
 
3
3
  const mat4 = require('../../maths/mat4')
4
4
  const vec3 = require('../../maths/vec3')
@@ -120,7 +120,7 @@ const expandShell = (options, geometry) => {
120
120
 
121
121
  // first of all equally spaced around the cylinder:
122
122
  for (let i = 0; i < segments; i++) {
123
- addUniqueAngle(angles, (i * Math.PI * 2 / segments))
123
+ addUniqueAngle(angles, (i * TAU / segments))
124
124
  }
125
125
 
126
126
  // and also at every normal of all touching planes:
@@ -130,10 +130,10 @@ const expandShell = (options, geometry) => {
130
130
  const co = vec3.dot(xbase, planenormal)
131
131
  let angle = Math.atan2(si, co)
132
132
 
133
- if (angle < 0) angle += Math.PI * 2
133
+ if (angle < 0) angle += TAU
134
134
  addUniqueAngle(angles, angle)
135
135
  angle = Math.atan2(-si, -co)
136
- if (angle < 0) angle += Math.PI * 2
136
+ if (angle < 0) angle += TAU
137
137
  addUniqueAngle(angles, angle)
138
138
  }
139
139
 
@@ -1,4 +1,4 @@
1
- const { EPS } = require('../../maths/constants')
1
+ const { EPS, TAU } = require('../../maths/constants')
2
2
 
3
3
  const intersect = require('../../maths/utils/intersect')
4
4
  const line2 = require('../../maths/line2')
@@ -139,7 +139,7 @@ const offsetFromPoints = (options, points) => {
139
139
 
140
140
  if (rotation !== 0.0) {
141
141
  // generate the segments
142
- cornersegments = Math.floor(segments * (Math.abs(rotation) / (2 * Math.PI)))
142
+ cornersegments = Math.floor(segments * (Math.abs(rotation) / TAU))
143
143
  const step = rotation / cornersegments
144
144
  const start = vec2.angle(vec2.subtract(v0, corner.s0[1], corner.c))
145
145
  const cornerpoints = []
@@ -2,6 +2,7 @@ const test = require('ava')
2
2
 
3
3
  const comparePolygonsAsPoints = require('../../../test/helpers/comparePolygonsAsPoints')
4
4
 
5
+ const { TAU } = require('../../maths/constants')
5
6
  const mat4 = require('../../maths/mat4')
6
7
 
7
8
  const { geom2, geom3, poly3 } = require('../../geometries')
@@ -56,10 +57,10 @@ test('extrudeFromSlices (torus)', (t) => {
56
57
  hex = poly3.transform(mat4.fromTranslation(mat4.create(), [0, 20, 0]), hex)
57
58
  hex = slice.fromPoints(poly3.toPoints(hex))
58
59
 
59
- const angle = Math.PI / 4
60
+ const angle = TAU / 8
60
61
  const geometry3 = extrudeFromSlices(
61
62
  {
62
- numberOfSlices: Math.PI * 2 / angle,
63
+ numberOfSlices: TAU / angle,
63
64
  capStart: false,
64
65
  capEnd: false,
65
66
  close: true,
@@ -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: Math.PI / -4 }, geometry2)
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: Math.PI / 2, twistSteps: 3 }, geometry2)
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: Math.PI / 2, twistSteps: 30 }, geometry2)
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: Math.PI}, square({size: 20}))
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: Math.PI / 2, segments: 16 })
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: Math.PI / 2, segments: 16 })
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: Math.PI / 2, segments: 16 })
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)