@jscad/modeling 3.0.3-alpha.0 → 3.0.5-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.
Files changed (214) hide show
  1. package/CHANGELOG.md +23 -0
  2. package/dist/jscad-modeling.es.js +2 -7
  3. package/dist/jscad-modeling.min.js +2 -7
  4. package/package.json +8 -9
  5. package/rollup.config.js +8 -4
  6. package/src/colors/colorize.js +17 -1
  7. package/src/curves/bezier/arcLengthToT.js +1 -1
  8. package/src/curves/bezier/create.js +1 -1
  9. package/src/curves/bezier/index.js +7 -7
  10. package/src/curves/bezier/length.js +1 -1
  11. package/src/curves/bezier/lengths.js +2 -1
  12. package/src/curves/bezier/tangentAt.js +1 -1
  13. package/src/curves/bezier/valueAt.js +1 -1
  14. package/src/curves/index.js +3 -3
  15. package/src/geometries/geom2/applyTransforms.js +3 -1
  16. package/src/geometries/geom2/clone.js +5 -1
  17. package/src/geometries/geom2/create.js +4 -14
  18. package/src/geometries/geom2/fromPoints.d.ts +4 -0
  19. package/src/geometries/geom2/fromPoints.js +28 -0
  20. package/src/geometries/geom2/fromPoints.test.js +22 -0
  21. package/src/geometries/geom2/fromSides.js +4 -2
  22. package/src/geometries/geom2/index.d.ts +1 -0
  23. package/src/geometries/geom2/index.js +22 -5
  24. package/src/geometries/geom2/isA.js +5 -1
  25. package/src/geometries/geom2/reverse.js +4 -2
  26. package/src/geometries/geom2/toOutlines.js +2 -1
  27. package/src/geometries/geom2/toPoints.js +5 -2
  28. package/src/geometries/geom2/toSides.js +4 -3
  29. package/src/geometries/geom2/toString.js +3 -2
  30. package/src/geometries/geom2/transform.js +4 -2
  31. package/src/geometries/geom2/validate.js +6 -2
  32. package/src/geometries/geom3/clone.js +5 -1
  33. package/src/geometries/geom3/create.js +5 -19
  34. package/src/geometries/geom3/fromVertices.js +13 -1
  35. package/src/geometries/geom3/fromVerticesConvex.js +1 -1
  36. package/src/geometries/geom3/index.d.ts +1 -0
  37. package/src/geometries/geom3/index.js +26 -4
  38. package/src/geometries/geom3/invert.js +5 -1
  39. package/src/geometries/geom3/isA.js +5 -1
  40. package/src/geometries/geom3/isConvex.d.ts +3 -0
  41. package/src/geometries/geom3/isConvex.js +65 -0
  42. package/src/geometries/geom3/isConvex.test.js +44 -0
  43. package/src/geometries/geom3/toPolygons.js +4 -2
  44. package/src/geometries/geom3/toString.js +3 -2
  45. package/src/geometries/geom3/toVertices.js +8 -4
  46. package/src/geometries/geom3/transform.js +5 -2
  47. package/src/geometries/geom3/validate.js +6 -2
  48. package/src/geometries/index.js +9 -7
  49. package/src/geometries/path2/appendArc.js +7 -5
  50. package/src/geometries/path2/appendArc.test.js +11 -15
  51. package/src/geometries/path2/appendBezier.js +6 -4
  52. package/src/geometries/path2/appendPoints.js +4 -2
  53. package/src/geometries/path2/applyTransforms.js +3 -0
  54. package/src/geometries/path2/clone.js +5 -1
  55. package/src/geometries/path2/close.js +5 -1
  56. package/src/geometries/path2/concat.js +3 -2
  57. package/src/geometries/path2/create.js +4 -15
  58. package/src/geometries/path2/equals.js +12 -7
  59. package/src/geometries/path2/fromPoints.js +5 -3
  60. package/src/geometries/path2/index.js +21 -4
  61. package/src/geometries/path2/isA.js +5 -1
  62. package/src/geometries/path2/reverse.js +4 -2
  63. package/src/geometries/path2/toPoints.js +5 -3
  64. package/src/geometries/path2/toString.js +3 -2
  65. package/src/geometries/path2/transform.js +4 -2
  66. package/src/geometries/path2/validate.js +5 -1
  67. package/src/geometries/path3/applyTransforms.js +1 -1
  68. package/src/geometries/path3/clone.d.ts +3 -0
  69. package/src/geometries/path3/clone.js +11 -0
  70. package/src/geometries/path3/close.js +4 -2
  71. package/src/geometries/path3/concat.js +2 -3
  72. package/src/geometries/path3/create.js +4 -20
  73. package/src/geometries/path3/equals.js +4 -2
  74. package/src/geometries/path3/fromVertices.js +2 -3
  75. package/src/geometries/path3/index.d.ts +1 -0
  76. package/src/geometries/path3/index.js +18 -1
  77. package/src/geometries/path3/isA.js +4 -2
  78. package/src/geometries/path3/reverse.js +2 -3
  79. package/src/geometries/path3/toString.js +2 -3
  80. package/src/geometries/path3/toVertices.js +2 -3
  81. package/src/geometries/path3/transform.js +2 -3
  82. package/src/geometries/path3/validate.js +6 -3
  83. package/src/geometries/poly2/arePointsInside.js +4 -1
  84. package/src/geometries/poly2/clone.js +4 -1
  85. package/src/geometries/poly2/create.js +2 -9
  86. package/src/geometries/poly2/index.js +16 -4
  87. package/src/geometries/poly2/isA.js +5 -1
  88. package/src/geometries/poly2/isConvex.js +5 -1
  89. package/src/geometries/poly2/isSimple.js +5 -1
  90. package/src/geometries/poly2/measureArea.js +4 -1
  91. package/src/geometries/poly2/measureBoundingBox.js +6 -1
  92. package/src/geometries/poly2/reverse.js +4 -1
  93. package/src/geometries/poly2/toPoints.js +6 -1
  94. package/src/geometries/poly2/toString.js +5 -1
  95. package/src/geometries/poly2/transform.js +5 -1
  96. package/src/geometries/poly2/type.d.ts +1 -5
  97. package/src/geometries/poly2/validate.js +6 -2
  98. package/src/geometries/poly3/clone.js +4 -1
  99. package/src/geometries/poly3/create.js +3 -11
  100. package/src/geometries/poly3/fromVerticesAndPlane.js +3 -1
  101. package/src/geometries/poly3/index.js +19 -4
  102. package/src/geometries/poly3/invert.js +4 -1
  103. package/src/geometries/poly3/isA.js +5 -1
  104. package/src/geometries/poly3/isConvex.js +5 -1
  105. package/src/geometries/poly3/measureArea.js +5 -1
  106. package/src/geometries/poly3/measureBoundingBox.js +4 -1
  107. package/src/geometries/poly3/measureBoundingSphere.js +4 -3
  108. package/src/geometries/poly3/measureSignedVolume.js +6 -1
  109. package/src/geometries/poly3/plane.js +6 -0
  110. package/src/geometries/poly3/toString.js +5 -1
  111. package/src/geometries/poly3/toVertices.js +6 -1
  112. package/src/geometries/poly3/transform.js +5 -1
  113. package/src/geometries/poly3/validate.js +6 -2
  114. package/src/geometries/slice/calculatePlane.js +3 -3
  115. package/src/geometries/slice/clone.js +4 -1
  116. package/src/geometries/slice/create.js +5 -10
  117. package/src/geometries/slice/equals.js +5 -1
  118. package/src/geometries/slice/fromOutlines.d.ts +5 -0
  119. package/src/geometries/slice/fromOutlines.js +16 -0
  120. package/src/geometries/slice/fromOutlines.test.js +17 -0
  121. package/src/geometries/slice/fromVertices.js +3 -3
  122. package/src/geometries/slice/index.d.ts +1 -1
  123. package/src/geometries/slice/index.js +20 -5
  124. package/src/geometries/slice/isA.js +5 -1
  125. package/src/geometries/slice/reverse.js +5 -2
  126. package/src/geometries/slice/toEdges.js +5 -3
  127. package/src/geometries/slice/toPolygons.js +5 -1
  128. package/src/geometries/slice/toString.js +5 -1
  129. package/src/geometries/slice/toVertices.js +5 -3
  130. package/src/geometries/slice/transform.js +4 -3
  131. package/src/geometries/slice/validate.js +3 -2
  132. package/src/index.d.ts +1 -0
  133. package/src/index.js +4 -0
  134. package/src/maths/constants.js +11 -7
  135. package/src/maths/index.js +2 -1
  136. package/src/maths/mat4/isOnlyTransformScale.js +1 -1
  137. package/src/maths/plane/fromNormalAndPoint.js +4 -6
  138. package/src/maths/plane/fromPoints.js +8 -7
  139. package/src/maths/plane/fromPointsRandom.js +13 -13
  140. package/src/measurements/measureAggregateEpsilon.js +3 -1
  141. package/src/measurements/measureAggregateEpsilon.test.js +1 -1
  142. package/src/measurements/measureArea.js +6 -4
  143. package/src/measurements/measureArea.test.js +4 -1
  144. package/src/measurements/measureBoundingBox.js +16 -2
  145. package/src/measurements/measureBoundingBox.test.js +4 -1
  146. package/src/measurements/measureBoundingSphere.js +38 -29
  147. package/src/measurements/measureBoundingSphere.test.js +4 -1
  148. package/src/measurements/measureCenterOfMass.js +3 -2
  149. package/src/measurements/measureEpsilon.js +4 -2
  150. package/src/operations/booleans/index.js +2 -0
  151. package/src/operations/booleans/intersect.js +0 -1
  152. package/src/operations/booleans/scission.js +0 -1
  153. package/src/operations/booleans/trees/splitLineSegmentByPlane.js +1 -4
  154. package/src/operations/booleans/trees/splitPolygonByPlane.d.ts +1 -3
  155. package/src/operations/booleans/trees/splitPolygonByPlane.test.js +138 -0
  156. package/src/operations/booleans/union.js +1 -1
  157. package/src/operations/booleans/unionGeom3.test.js +35 -0
  158. package/src/operations/extrusions/extrudeFromSlices.js +16 -6
  159. package/src/operations/extrusions/extrudeFromSlices.test.js +1 -1
  160. package/src/operations/extrusions/extrudeHelical.js +2 -1
  161. package/src/operations/extrusions/extrudeLinear.js +1 -1
  162. package/src/operations/extrusions/extrudeLinearGeom2.js +2 -1
  163. package/src/operations/extrusions/extrudeRotate.js +3 -2
  164. package/src/operations/extrusions/extrudeRotate.test.js +34 -0
  165. package/src/operations/extrusions/extrudeWalls.test.js +60 -0
  166. package/src/operations/hulls/hull.js +3 -2
  167. package/src/operations/hulls/toUniquePoints.js +3 -0
  168. package/src/operations/minkowski/index.d.ts +1 -0
  169. package/src/operations/minkowski/index.js +15 -0
  170. package/src/operations/minkowski/minkowskiSum.d.ts +4 -0
  171. package/src/operations/minkowski/minkowskiSum.js +223 -0
  172. package/src/operations/minkowski/minkowskiSum.test.js +199 -0
  173. package/src/operations/modifiers/generalize.js +9 -2
  174. package/src/operations/modifiers/reTesselateCoplanarPolygons.js +10 -3
  175. package/src/operations/modifiers/reTesselateCoplanarPolygons.test.js +36 -1
  176. package/src/operations/modifiers/retessellate.js +4 -2
  177. package/src/operations/modifiers/snap.js +22 -3
  178. package/src/operations/modifiers/snap.test.js +24 -15
  179. package/src/operations/offsets/offsetGeom3.test.js +5 -7
  180. package/src/operations/transforms/align.js +2 -1
  181. package/src/operations/transforms/align.test.js +1 -1
  182. package/src/operations/transforms/mirror.js +6 -2
  183. package/src/operations/transforms/rotate.js +6 -2
  184. package/src/operations/transforms/scale.js +6 -2
  185. package/src/operations/transforms/transform.js +6 -2
  186. package/src/operations/transforms/transform.test.js +16 -5
  187. package/src/operations/transforms/translate.js +6 -2
  188. package/src/primitives/arc.js +13 -12
  189. package/src/primitives/arc.test.js +104 -113
  190. package/src/primitives/circle.js +10 -9
  191. package/src/primitives/cube.js +5 -6
  192. package/src/primitives/cuboid.js +6 -6
  193. package/src/primitives/cylinder.js +8 -8
  194. package/src/primitives/cylinderElliptic.js +11 -11
  195. package/src/primitives/ellipse.js +10 -9
  196. package/src/primitives/ellipsoid.js +8 -8
  197. package/src/primitives/geodesicSphere.js +6 -6
  198. package/src/primitives/line.js +2 -0
  199. package/src/primitives/polygon.js +6 -7
  200. package/src/primitives/polyhedron.js +7 -8
  201. package/src/primitives/rectangle.js +6 -6
  202. package/src/primitives/roundedCuboid.js +8 -8
  203. package/src/primitives/roundedCylinder.js +9 -9
  204. package/src/primitives/roundedRectangle.js +8 -8
  205. package/src/primitives/sphere.js +7 -8
  206. package/src/primitives/square.js +6 -6
  207. package/src/primitives/star.js +10 -10
  208. package/src/primitives/torus.js +11 -11
  209. package/src/primitives/triangle.js +7 -6
  210. package/src/utils/areAllShapesTheSameType.js +4 -0
  211. package/src/utils/flatten.js +1 -1
  212. package/src/utils/flatten.test.js +94 -0
  213. package/src/geometries/slice/fromGeom2.d.ts +0 -5
  214. package/src/geometries/slice/fromGeom2.js +0 -17
@@ -18,12 +18,10 @@ import * as vec3 from '../vec3/index.js'
18
18
  * @alias module:modeling/maths/plane.fromNormalAndPoint
19
19
  */
20
20
  export const fromNormalAndPoint = (out, normal, point) => {
21
- const u = vec3.normalize(vec3.create(), normal)
22
- const w = vec3.dot(point, u)
21
+ // normalize to out
22
+ vec3.normalize(out, normal)
23
+ // calculate distance
24
+ out[3] = vec3.dot(point, out)
23
25
 
24
- out[0] = u[0]
25
- out[1] = u[1]
26
- out[2] = u[2]
27
- out[3] = w
28
26
  return out
29
27
  }
@@ -1,10 +1,13 @@
1
1
  import * as vec3 from '../vec3/index.js'
2
2
 
3
+ const ba = vec3.create()
4
+ const ca = vec3.create()
5
+
3
6
  /**
4
7
  * Create a plane from the given points.
5
8
  *
6
9
  * @param {Plane} out - receiving plane
7
- * @param {Array} vertices - points on the plane
10
+ * @param {Array} vertices - list of points on the plane
8
11
  * @returns {Plane} out
9
12
  * @alias module:modeling/maths/plane.fromPoints
10
13
  */
@@ -13,8 +16,6 @@ export const fromPoints = (out, ...vertices) => {
13
16
 
14
17
  // Calculate normal vector for a single vertex
15
18
  // Inline to avoid allocations
16
- const ba = vec3.create()
17
- const ca = vec3.create()
18
19
  const vertexNormal = (index) => {
19
20
  const a = vertices[index]
20
21
  const b = vertices[(index + 1) % len]
@@ -26,18 +27,18 @@ export const fromPoints = (out, ...vertices) => {
26
27
  return ba
27
28
  }
28
29
 
29
- out[0] = 0
30
- out[1] = 0
31
- out[2] = 0
32
30
  if (len === 3) {
33
31
  // optimization for triangles, which are always coplanar
34
32
  vec3.copy(out, vertexNormal(0))
35
33
  } else {
36
34
  // sum of vertex normals
35
+ out[0] = 0
36
+ out[1] = 0
37
+ out[2] = 0
37
38
  vertices.forEach((v, i) => {
38
39
  vec3.add(out, out, vertexNormal(i))
39
40
  })
40
- // renormalize normal vector
41
+ // normalize sum
41
42
  vec3.normalize(out, out)
42
43
  }
43
44
  out[3] = vec3.dot(out, vertices[0])
@@ -15,26 +15,26 @@ import * as vec3 from '../vec3/index.js'
15
15
  * @alias module:modeling/maths/plane.fromPointsRandom
16
16
  */
17
17
  export const fromPointsRandom = (out, a, b, c) => {
18
- let ba = vec3.subtract(vec3.create(), b, a)
19
- let ca = vec3.subtract(vec3.create(), c, a)
18
+ const ba = vec3.subtract(vec3.create(), b, a)
19
+ const ca = vec3.subtract(vec3.create(), c, a)
20
20
  if (vec3.length(ba) < EPS) {
21
- ba = vec3.orthogonal(ba, ca)
21
+ vec3.orthogonal(ba, ca)
22
22
  }
23
23
  if (vec3.length(ca) < EPS) {
24
- ca = vec3.orthogonal(ca, ba)
24
+ vec3.orthogonal(ca, ba)
25
25
  }
26
- let normal = vec3.cross(vec3.create(), ba, ca)
26
+
27
+ // calculate plane normal
28
+ const normal = vec3.cross(out, ba, ca)
27
29
  if (vec3.length(normal) < EPS) {
28
30
  // this would mean that ba == ca.negated()
29
- ca = vec3.orthogonal(ca, ba)
30
- normal = vec3.cross(normal, ba, ca)
31
+ vec3.orthogonal(ca, ba)
32
+ vec3.cross(normal, ba, ca)
31
33
  }
32
- normal = vec3.normalize(normal, normal)
33
- const w = vec3.dot(normal, a)
34
+ vec3.normalize(normal, normal)
35
+
36
+ // and distance
37
+ out[3] = vec3.dot(normal, a)
34
38
 
35
- out[0] = normal[0]
36
- out[1] = normal[1]
37
- out[2] = normal[2]
38
- out[3] = w
39
39
  return out
40
40
  }
@@ -3,6 +3,8 @@ import { flatten } from '../utils/flatten.js'
3
3
  import * as geom2 from '../geometries/geom2/index.js'
4
4
  import * as geom3 from '../geometries/geom3/index.js'
5
5
  import * as path2 from '../geometries/path2/index.js'
6
+ import * as path3 from '../geometries/path3/index.js'
7
+ import * as slice from '../geometries/slice/index.js'
6
8
 
7
9
  import { measureAggregateBoundingBox } from './measureAggregateBoundingBox.js'
8
10
  import { calculateEpsilonFromBounds } from './calculateEpsilonFromBounds.js'
@@ -23,7 +25,7 @@ export const measureAggregateEpsilon = (...geometries) => {
23
25
  let dimensions = 0
24
26
  dimensions = geometries.reduce((dimensions, geometry) => {
25
27
  if (path2.isA(geometry) || geom2.isA(geometry)) return Math.max(dimensions, 2)
26
- if (geom3.isA(geometry)) return Math.max(dimensions, 3)
28
+ if (geom3.isA(geometry || path3.isA(geometry) || slice.isA(geometry))) return Math.max(dimensions, 3)
27
29
  return 0
28
30
  }, dimensions)
29
31
  return calculateEpsilonFromBounds(bounds, dimensions)
@@ -13,7 +13,7 @@ test('measureAggregateEpsilon (single objects)', (t) => {
13
13
  t.is(calculatedEpsilon, expectedEpsilon)
14
14
  })
15
15
 
16
- test('measureAggregateEpsilon (multiple objects)', (t) => {
16
+ test('measureAggregateEpsilon (multiple 3D objects)', (t) => {
17
17
  const highCube = cube({ size: 4, center: [-40, 100, 20] })
18
18
  const lowCube = cube({ size: 60, center: [20, -10, 20] })
19
19
  const calculatedEpsilon = measureAggregateEpsilon(highCube, lowCube)
@@ -3,6 +3,7 @@ import { flatten } from '../utils/flatten.js'
3
3
  import * as geom2 from '../geometries/geom2/index.js'
4
4
  import * as geom3 from '../geometries/geom3/index.js'
5
5
  import * as path2 from '../geometries/path2/index.js'
6
+ import * as path3 from '../geometries/path3/index.js'
6
7
  import * as poly3 from '../geometries/poly3/index.js'
7
8
  import * as slice from '../geometries/slice/index.js'
8
9
 
@@ -12,10 +13,10 @@ const cache = new WeakMap()
12
13
  * Measure the area of the given geometry.
13
14
  * NOTE: paths are infinitely narrow and do not have an area
14
15
  *
15
- * @param {Path2} geometry - geometry to measure
16
+ * @param {Path2|Path3} geometry - geometry to measure
16
17
  * @returns {number} area of the geometry
17
18
  */
18
- const measureAreaOfPath2 = () => 0
19
+ const measureAreaOfPath = () => 0
19
20
 
20
21
  /*
21
22
  * Measure the area of the given geometry.
@@ -87,9 +88,10 @@ export const measureArea = (...geometries) => {
87
88
  geometries = flatten(geometries)
88
89
 
89
90
  const results = geometries.map((geometry) => {
90
- if (path2.isA(geometry)) return measureAreaOfPath2(geometry)
91
- if (geom2.isA(geometry)) return measureAreaOfGeom2(geometry)
92
91
  if (geom3.isA(geometry)) return measureAreaOfGeom3(geometry)
92
+ if (geom2.isA(geometry)) return measureAreaOfGeom2(geometry)
93
+ if (path2.isA(geometry)) return measureAreaOfPath(geometry)
94
+ if (path3.isA(geometry)) return measureAreaOfPath(geometry)
93
95
  if (slice.isA(geometry)) return measureAreaOfSlice(geometry)
94
96
  return 0
95
97
  })
@@ -1,6 +1,6 @@
1
1
  import test from 'ava'
2
2
 
3
- import { geom2, geom3, path2, slice } from '../geometries/index.js'
3
+ import { geom2, geom3, path2, path3, slice } from '../geometries/index.js'
4
4
 
5
5
  import { line, rectangle, cuboid } from '../primitives/index.js'
6
6
 
@@ -12,6 +12,7 @@ test('measureArea: single objects', (t) => {
12
12
  const acube = cuboid()
13
13
 
14
14
  const apath2 = path2.create()
15
+ const apath3 = path3.create()
15
16
  const ageom2 = geom2.create()
16
17
  const ageom3 = geom3.create()
17
18
  const aslice = slice.create()
@@ -25,6 +26,7 @@ test('measureArea: single objects', (t) => {
25
26
  const carea = measureArea(acube)
26
27
 
27
28
  const p2area = measureArea(apath2)
29
+ const p3area = measureArea(apath3)
28
30
  const g2area = measureArea(ageom2)
29
31
  const g3area = measureArea(ageom3)
30
32
  const slarea = measureArea(aslice)
@@ -38,6 +40,7 @@ test('measureArea: single objects', (t) => {
38
40
  t.is(carea, 24) // 2x2x6
39
41
 
40
42
  t.is(p2area, 0)
43
+ t.is(p3area, 0)
41
44
  t.is(g2area, 0)
42
45
  t.is(g3area, 0)
43
46
  t.is(slarea, 0)
@@ -6,6 +6,7 @@ import * as vec3 from '../maths/vec3/index.js'
6
6
  import * as geom2 from '../geometries/geom2/index.js'
7
7
  import * as geom3 from '../geometries/geom3/index.js'
8
8
  import * as path2 from '../geometries/path2/index.js'
9
+ import * as path3 from '../geometries/path3/index.js'
9
10
  import * as poly3 from '../geometries/poly3/index.js'
10
11
  import * as slice from '../geometries/slice/index.js'
11
12
 
@@ -70,6 +71,18 @@ const measureBoundingBoxOfPath2 = (geometry) => {
70
71
  return boundingBox
71
72
  }
72
73
 
74
+ /*
75
+ * Measure the min and max bounds of the given (path3) geometry.
76
+ * @return {Array[]} the min and max bounds for the geometry
77
+ */
78
+ const measureBoundingBoxOfPath3 = (geometry) => {
79
+ const boundingBox = []
80
+ path3.toVertices(geometry).forEach((vertice) => {
81
+ expand3(boundingBox, vertice)
82
+ })
83
+ return boundingBox
84
+ }
85
+
73
86
  /*
74
87
  * Measure the min and max bounds of the given (geom2) geometry.
75
88
  * @return {Array[]} the min and max bounds for the geometry
@@ -123,9 +136,10 @@ export const measureBoundingBox = (...geometries) => {
123
136
  geometries = flatten(geometries)
124
137
 
125
138
  const results = geometries.map((geometry) => {
126
- if (path2.isA(geometry)) return measureCached(geometry, measureBoundingBoxOfPath2)
127
- if (geom2.isA(geometry)) return measureCached(geometry, measureBoundingBoxOfGeom2)
128
139
  if (geom3.isA(geometry)) return measureCached(geometry, measureBoundingBoxOfGeom3)
140
+ if (geom2.isA(geometry)) return measureCached(geometry, measureBoundingBoxOfGeom2)
141
+ if (path2.isA(geometry)) return measureCached(geometry, measureBoundingBoxOfPath2)
142
+ if (path3.isA(geometry)) return measureCached(geometry, measureBoundingBoxOfPath3)
129
143
  if (slice.isA(geometry)) return measureCached(geometry, measureBoundingBoxOfSlice)
130
144
  return [[0, 0, 0], [0, 0, 0]]
131
145
  })
@@ -1,6 +1,6 @@
1
1
  import test from 'ava'
2
2
 
3
- import { geom2, geom3, path2, slice } from '../geometries/index.js'
3
+ import { geom2, geom3, path2, path3, slice } from '../geometries/index.js'
4
4
 
5
5
  import { line, rectangle, cuboid } from '../primitives/index.js'
6
6
 
@@ -14,6 +14,7 @@ test('measureBoundingBox (single objects)', (t) => {
14
14
  const acube = cuboid()
15
15
 
16
16
  const apath2 = path2.create()
17
+ const apath3 = path3.create()
17
18
  const ageom2 = geom2.create()
18
19
  const ageom3 = geom3.create()
19
20
  const aslice = slice.create()
@@ -27,6 +28,7 @@ test('measureBoundingBox (single objects)', (t) => {
27
28
  const cbounds = measureBoundingBox(acube)
28
29
 
29
30
  const p2bounds = measureBoundingBox(apath2)
31
+ const p3bounds = measureBoundingBox(apath3)
30
32
  const g2bounds = measureBoundingBox(ageom2)
31
33
  const g3bounds = measureBoundingBox(ageom3)
32
34
  const slbounds = measureBoundingBox(aslice)
@@ -40,6 +42,7 @@ test('measureBoundingBox (single objects)', (t) => {
40
42
  t.deepEqual(cbounds, [[-1, -1, -1], [1, 1, 1]])
41
43
 
42
44
  t.deepEqual(p2bounds, [[0, 0, 0], [0, 0, 0]])
45
+ t.deepEqual(p3bounds, [[0, 0, 0], [0, 0, 0]])
43
46
  t.deepEqual(g2bounds, [[0, 0, 0], [0, 0, 0]])
44
47
  t.deepEqual(g3bounds, [[0, 0, 0], [0, 0, 0]])
45
48
  t.deepEqual(slbounds, [[0, 0, 0], [0, 0, 0]])
@@ -6,6 +6,7 @@ import * as vec3 from '../maths/vec3/index.js'
6
6
  import * as geom2 from '../geometries/geom2/index.js'
7
7
  import * as geom3 from '../geometries/geom3/index.js'
8
8
  import * as path2 from '../geometries/path2/index.js'
9
+ import * as path3 from '../geometries/path3/index.js'
9
10
  import * as poly3 from '../geometries/poly3/index.js'
10
11
  import * as slice from '../geometries/slice/index.js'
11
12
 
@@ -60,6 +61,33 @@ const measureBoundingSphereOfPoints = (points) => {
60
61
  return [centroid, radius]
61
62
  }
62
63
 
64
+ /*
65
+ * Measure the bounding sphere of the given 2D points.
66
+ * @return {[[x, y, z], radius]} the bounding sphere for the points
67
+ */
68
+ const measureBoundingSphereOfVertices = (vertices) => {
69
+ const centroid = vec3.create()
70
+ let radius = 0
71
+
72
+ if (vertices.length > 0) {
73
+ // calculate the centroid of the vertices
74
+ let numVertices = 0
75
+ vertices.forEach((vertex) => {
76
+ vec3.add(centroid, centroid, vertex)
77
+ numVertices++
78
+ })
79
+ vec3.scale(centroid, centroid, 1 / numVertices)
80
+
81
+ // find the farthest vertex from the centroid
82
+ vertices.forEach((vertex) => {
83
+ radius = Math.max(radius, vec3.squaredDistance(centroid, vertex))
84
+ })
85
+ radius = Math.sqrt(radius)
86
+ }
87
+
88
+ return [centroid, radius]
89
+ }
90
+
63
91
  /*
64
92
  * Measure the bounding sphere of the given (path2) geometry.
65
93
  * @return {[[x, y, z], radius]} the bounding sphere for the geometry
@@ -106,36 +134,16 @@ const measureBoundingSphereOfGeom3 = (geometry) => {
106
134
  }
107
135
 
108
136
  /*
109
- * Measure the bounding sphere of the given (geom3) geometry.
137
+ * Measure the bounding sphere of the given (slice) geometry.
110
138
  * @return {[[x, y, z], radius]} the bounding sphere for the geometry
111
139
  */
112
- const measureBoundingSphereOfSlice = (geometry) => {
113
- const centroid = vec3.create()
114
- let radius = 0
115
- let numVertices = 0
140
+ const measureBoundingSphereOfPath3 = (geometry) => measureBoundingSphereOfVertices(path3.toVertices(geometry))
116
141
 
117
- // calculate the centroid of the geometry
118
- geometry.contours.forEach((contour) => {
119
- contour.forEach((vertex) => {
120
- vec3.add(centroid, centroid, vertex)
121
- numVertices++
122
- })
123
- })
124
-
125
- if (numVertices > 0) {
126
- vec3.scale(centroid, centroid, 1 / numVertices)
127
-
128
- // find the farthest vertex from the centroid
129
- geometry.contours.forEach((contour) => {
130
- contour.forEach((vertex) => {
131
- radius = Math.max(radius, vec3.squaredDistance(centroid, vertex))
132
- })
133
- })
134
- radius = Math.sqrt(radius)
135
- }
136
-
137
- return [centroid, radius]
138
- }
142
+ /*
143
+ * Measure the bounding sphere of the given (slice) geometry.
144
+ * @return {[[x, y, z], radius]} the bounding sphere for the geometry
145
+ */
146
+ const measureBoundingSphereOfSlice = (geometry) => measureBoundingSphereOfVertices(slice.toVertices(geometry))
139
147
 
140
148
  /**
141
149
  * Measure the (approximate) bounding sphere of the given geometries.
@@ -151,9 +159,10 @@ export const measureBoundingSphere = (...geometries) => {
151
159
  geometries = flatten(geometries)
152
160
 
153
161
  const results = geometries.map((geometry) => {
154
- if (path2.isA(geometry)) return measureCached(geometry, measureBoundingSphereOfPath2)
155
- if (geom2.isA(geometry)) return measureCached(geometry, measureBoundingSphereOfGeom2)
156
162
  if (geom3.isA(geometry)) return measureCached(geometry, measureBoundingSphereOfGeom3)
163
+ if (geom2.isA(geometry)) return measureCached(geometry, measureBoundingSphereOfGeom2)
164
+ if (path2.isA(geometry)) return measureCached(geometry, measureBoundingSphereOfPath2)
165
+ if (path3.isA(geometry)) return measureCached(geometry, measureBoundingSphereOfPath3)
157
166
  if (slice.isA(geometry)) return measureCached(geometry, measureBoundingSphereOfSlice)
158
167
  return [[0, 0, 0], 0]
159
168
  })
@@ -1,6 +1,6 @@
1
1
  import test from 'ava'
2
2
 
3
- import { geom2, geom3, path2, slice } from '../geometries/index.js'
3
+ import { geom2, geom3, path2, path3, slice } from '../geometries/index.js'
4
4
 
5
5
  import { line, rectangle, ellipsoid } from '../primitives/index.js'
6
6
 
@@ -12,6 +12,7 @@ test('measureBoundingSphere (single objects)', (t) => {
12
12
  const aellipsoid = ellipsoid({ radius: [5, 10, 15], center: [5, 5, 5] })
13
13
 
14
14
  const apath2 = path2.create()
15
+ const apath3 = path3.create()
15
16
  const ageom2 = geom2.create()
16
17
  const ageom3 = geom3.create()
17
18
  const aslice = slice.create()
@@ -25,6 +26,7 @@ test('measureBoundingSphere (single objects)', (t) => {
25
26
  const cbounds = measureBoundingSphere(aellipsoid)
26
27
 
27
28
  const p2bounds = measureBoundingSphere(apath2)
29
+ const p3bounds = measureBoundingSphere(apath3)
28
30
  const g2bounds = measureBoundingSphere(ageom2)
29
31
  const g3bounds = measureBoundingSphere(ageom3)
30
32
  const slbounds = measureBoundingSphere(aslice)
@@ -38,6 +40,7 @@ test('measureBoundingSphere (single objects)', (t) => {
38
40
  t.deepEqual(cbounds, [[5.000000000000018, 4.999999999999983, 5.000000000000001], 15])
39
41
 
40
42
  t.deepEqual(p2bounds, [[0, 0, 0], 0])
43
+ t.deepEqual(p3bounds, [[0, 0, 0], 0])
41
44
  t.deepEqual(g2bounds, [[0, 0, 0], 0])
42
45
  t.deepEqual(g3bounds, [[0, 0, 0], 0])
43
46
  t.deepEqual(slbounds, [[0, 0, 0], 0])
@@ -95,9 +95,10 @@ export const measureCenterOfMass = (...geometries) => {
95
95
  geometries = flatten(geometries)
96
96
 
97
97
  const results = geometries.map((geometry) => {
98
- // NOTE: center of mass for geometry path2 is not possible
99
- if (geom2.isA(geometry)) return measureCenterOfMassGeom2(geometry)
100
98
  if (geom3.isA(geometry)) return measureCenterOfMassGeom3(geometry)
99
+ if (geom2.isA(geometry)) return measureCenterOfMassGeom2(geometry)
100
+ // TODO if (slice.isA(geometry)) return measureCenterOfMassSlice(geometry)
101
+ // NOTE: center of mass for geometry path2 and path3 is not possible
101
102
  return [0, 0, 0]
102
103
  })
103
104
  return results.length === 1 ? results[0] : results
@@ -3,6 +3,7 @@ import { flatten } from '../utils/flatten.js'
3
3
  import * as geom2 from '../geometries/geom2/index.js'
4
4
  import * as geom3 from '../geometries/geom3/index.js'
5
5
  import * as path2 from '../geometries/path2/index.js'
6
+ import * as path3 from '../geometries/path3/index.js'
6
7
  import * as slice from '../geometries/slice/index.js'
7
8
 
8
9
  import { calculateEpsilonFromBounds } from './calculateEpsilonFromBounds.js'
@@ -22,9 +23,10 @@ export const measureEpsilon = (...geometries) => {
22
23
  geometries = flatten(geometries)
23
24
 
24
25
  const results = geometries.map((geometry) => {
25
- if (path2.isA(geometry)) return calculateEpsilonFromBounds(measureBoundingBox(geometry), 2)
26
- if (geom2.isA(geometry)) return calculateEpsilonFromBounds(measureBoundingBox(geometry), 2)
27
26
  if (geom3.isA(geometry)) return calculateEpsilonFromBounds(measureBoundingBox(geometry), 3)
27
+ if (geom2.isA(geometry)) return calculateEpsilonFromBounds(measureBoundingBox(geometry), 2)
28
+ if (path2.isA(geometry)) return calculateEpsilonFromBounds(measureBoundingBox(geometry), 2)
29
+ if (path3.isA(geometry)) return calculateEpsilonFromBounds(measureBoundingBox(geometry), 3)
28
30
  if (slice.isA(geometry)) return calculateEpsilonFromBounds(measureBoundingBox(geometry), 3)
29
31
  return 0
30
32
  })
@@ -1,8 +1,10 @@
1
1
  /**
2
2
  * All shapes (primitives or the results of operations) can be passed to boolean functions
3
3
  * to perform logical operations, e.g. remove a hole from a board.
4
+ *
4
5
  * In all cases, the function returns the results, and never changes the original shapes.
5
6
  * @module modeling/booleans
7
+ *
6
8
  * @example
7
9
  * import { intersect, scission, subtract, union } from '@jscad/modeling'
8
10
  */
@@ -6,7 +6,6 @@ import * as geom3 from '../../geometries/geom3/index.js'
6
6
 
7
7
  import { intersectGeom2 } from './intersectGeom2.js'
8
8
  import { intersectGeom3 } from './intersectGeom3.js'
9
-
10
9
  /**
11
10
  * Return a new geometry representing space in both the first geometry and
12
11
  * all subsequent geometries.
@@ -1,7 +1,6 @@
1
1
  import * as geom3 from '../../geometries/geom3/index.js'
2
2
 
3
3
  import { scissionGeom3 } from './scissionGeom3.js'
4
-
5
4
  /**
6
5
  * Scission (divide) the given geometry into the component pieces.
7
6
  *
@@ -4,10 +4,7 @@ export const splitLineSegmentByPlane = (plane, p1, p2) => {
4
4
  const direction = vec3.subtract(vec3.create(), p2, p1)
5
5
  let lambda = (plane[3] - vec3.dot(plane, p1)) / vec3.dot(plane, direction)
6
6
 
7
- Number.isNaN(lambda) ? lambda = 0
8
- : lambda > 1 ? lambda = 1
9
- : lambda < 0 ? lambda = 0
10
- : lambda
7
+ Number.isNaN(lambda) ? lambda = 0 : lambda > 1 ? lambda = 1 : lambda < 0 ? lambda = 0 : lambda
11
8
 
12
9
  vec3.scale(direction, direction, lambda)
13
10
  vec3.add(direction, p1, direction)
@@ -28,6 +28,4 @@ interface SplitRes
28
28
  // In case the polygon is spanning, returns:
29
29
  // .front: a Polygon3 of the front part
30
30
  // .back: a Polygon3 of the back part
31
- declare function splitPolygonByPlane(plane: Plane, polygon: Poly3): SplitRes;
32
-
33
- export default splitPolygonByPlane;
31
+ export declare function splitPolygonByPlane(plane: Plane, polygon: Poly3): SplitRes;
@@ -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
+ })
@@ -37,7 +37,7 @@ export const union = (...geometries) => {
37
37
  }
38
38
 
39
39
  const geometry = geometries[0]
40
- // if (path.isA(geometry)) return unionPath(matrix, geometries)
40
+ // TODO if (path2.isA(geometry)) return unionPath(geometries)
41
41
  if (geom2.isA(geometry)) return unionGeom2(geometries)
42
42
  if (geom3.isA(geometry)) return unionGeom3(geometries)
43
43
  throw new Error('union unsupported geometry type')