@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
@@ -2,13 +2,15 @@ import { clone } from './clone.js'
2
2
 
3
3
  /**
4
4
  * Reverses the path so that the points are in the opposite order.
5
+ *
5
6
  * This swaps the left (interior) and right (exterior) edges.
7
+ *
6
8
  * @param {Path2} geometry - the path to reverse
7
9
  * @returns {Path2} a new path
8
- * @alias module:modeling/geometries/path2.reverse
10
+ * @alias module:modeling/path2.reverse
9
11
  *
10
12
  * @example
11
- * let newPath = reverse(myPath)
13
+ * let newPath = path2.reverse(oldPath)
12
14
  */
13
15
  export const reverse = (geometry) => {
14
16
  // NOTE: this only updates the order of the points
@@ -2,12 +2,14 @@ import { applyTransforms } from './applyTransforms.js'
2
2
 
3
3
  /**
4
4
  * Produces an array of points from the given geometry.
5
- * The returned array should not be modified as the data is shared with the geometry.
5
+ *
6
+ * NOTE: The returned array should not be modified as the data is shared with the geometry.
7
+ *
6
8
  * @param {Path2} geometry - the geometry
7
9
  * @returns {Array} an array of points
8
- * @alias module:modeling/geometries/path2.toPoints
10
+ * @alias module:modeling/path2.toPoints
9
11
  *
10
12
  * @example
11
- * let sharedPoints = toPoints(geometry)
13
+ * let sharedPoints = path2.toPoints(geometry)
12
14
  */
13
15
  export const toPoints = (geometry) => applyTransforms(geometry).points
@@ -4,12 +4,13 @@ import { toPoints } from './toPoints.js'
4
4
 
5
5
  /**
6
6
  * Create a string representing the contents of the given path.
7
+ *
7
8
  * @param {Path2} geometry - the path
8
9
  * @returns {string} a representative string
9
- * @alias module:modeling/geometries/path2.toString
10
+ * @alias module:modeling/path2.toString
10
11
  *
11
12
  * @example
12
- * console.out(toString(path))
13
+ * console.out(path2.toString(path))
13
14
  */
14
15
  export const toString = (geometry) => {
15
16
  const points = toPoints(geometry)
@@ -2,15 +2,17 @@ import * as mat4 from '../../maths/mat4/index.js'
2
2
 
3
3
  /**
4
4
  * Transform the given geometry using the given matrix.
5
+ *
5
6
  * This is a lazy transform of the points, as this function only adjusts the transforms.
6
7
  * The transforms are applied when accessing the points via toPoints().
8
+ *
7
9
  * @param {Mat4} matrix - the matrix to transform with
8
10
  * @param {Path2} geometry - the geometry to transform
9
11
  * @returns {Path2} a new path
10
- * @alias module:modeling/geometries/path2.transform
12
+ * @alias module:modeling/path2.transform
11
13
  *
12
14
  * @example
13
- * let newPath = transform(fromZRotation(TAU / 8), path)
15
+ * let newPath = path2.transform(mat4.fromZRotation(TAU / 8), path)
14
16
  */
15
17
  export const transform = (matrix, geometry) => {
16
18
  const transforms = mat4.multiply(mat4.create(), matrix, geometry.transforms)
@@ -4,13 +4,17 @@ import { isA } from './isA.js'
4
4
 
5
5
  /**
6
6
  * Determine if the given object is a valid path2.
7
+ *
7
8
  * Checks for valid data points, and duplicate points.
8
9
  *
9
10
  * **If the geometry is not valid, an exception will be thrown with details of the geometry error.**
10
11
  *
11
12
  * @param {object} object - the object to interrogate
12
13
  * @throws {Error} error if the geometry is not valid
13
- * @alias module:modeling/geometries/path2.validate
14
+ * @alias module:modeling/path2.validate
15
+ *
16
+ * @example
17
+ * path2.validate(geometry)
14
18
  */
15
19
  export const validate = (object) => {
16
20
  if (!isA(object)) {
@@ -11,7 +11,7 @@ import * as vec3 from '../../maths/vec3/index.js'
11
11
  * @function
12
12
  *
13
13
  * @example
14
- * geometry = applyTransforms(geometry)
14
+ * geometry = path3.applyTransforms(geometry)
15
15
  */
16
16
  export const applyTransforms = (geometry) => {
17
17
  if (mat4.isIdentity(geometry.transforms)) return geometry
@@ -0,0 +1,3 @@
1
+ import type { Path3 } from './type.d.ts'
2
+
3
+ export function clone(geometry: Path3): Path3
@@ -0,0 +1,11 @@
1
+ /**
2
+ * Performs a shallow clone of the give geometry.
3
+ *
4
+ * @param {Path3} geometry - the geometry to clone
5
+ * @returns {Path3} a new path
6
+ * @alias module:modeling/path3.clone
7
+ *
8
+ * @example
9
+ * let newPath = path3.clone(oldPath)
10
+ */
11
+ export const clone = (geometry) => Object.assign({}, geometry)
@@ -7,8 +7,10 @@ import * as vec3 from '../../maths/vec3/index.js'
7
7
  *
8
8
  * @param {Path3} geometry - the path to close
9
9
  * @returns {Path3} a new path
10
- * @function
11
- * @alias module:modeling/geometries/path3.close
10
+ * @alias module:modeling/path3.close
11
+ *
12
+ * @example
13
+ * const newPath = path3.close(oldPath)
12
14
  */
13
15
  export const close = (geometry) => {
14
16
  if (geometry.isClosed) return geometry
@@ -13,11 +13,10 @@ import { toVertices } from './toVertices.js'
13
13
  *
14
14
  * @param {...Path3} paths - the paths to concatenate
15
15
  * @returns {Path3} a new path
16
- * @function
17
- * @alias module:modeling/geometries/path3.concat
16
+ * @alias module:modeling/path3.concat
18
17
  *
19
18
  * @example
20
- * let newPath = concat(fromVertices({}, [[1, 2, 3]]), fromVertices({}, [[4, 5, 6]]))
19
+ * let newPath = path3.concat(path3.fromVertices({}, [[1, 2, 3]]), path3.fromVertices({}, [[4, 5, 6]]))
21
20
  */
22
21
  export const concat = (...paths) => {
23
22
  // Only the last path can be closed, producing a closed path.
@@ -1,30 +1,14 @@
1
1
  import * as mat4 from '../../maths/mat4/index.js'
2
2
 
3
- /**
4
- * Represents a 3D geometry consisting of a list of ordered vertices.
5
- *
6
- * @typedef {Object} Path3
7
- * @property {Array} vertices - list of ordered vertices
8
- * @property {boolean} isClosed - true if the path is closed where start and end vertices are the same
9
- * @property {Mat4} transforms - transforms to apply to the vertices, see transform()
10
- *
11
- * @example
12
- * {
13
- * vertices: [[0,0,0], [4,0,0], [4,3,0]],
14
- * isClosed: true,
15
- * transforms: [1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1],
16
- * }
17
- */
18
-
19
3
  /**
20
4
  * Create an empty, open path.
21
5
  *
6
+ * @param {Array} [vertices] - a list of vertices of which to create the path
22
7
  * @returns {Path3} a new path
23
- * @function
24
- * @alias module:modeling/geometries/path3.create
8
+ * @alias module:modeling/path3.create
25
9
  *
26
10
  * @example
27
- * let pathA = create()
28
- * let pathB = create([[0,0,0], [4,0,0], [4,3,0]])
11
+ * let pathA = path3.create()
12
+ * let pathB = path3.create([[0,0,0], [4,0,0], [4,3,0]])
29
13
  */
30
14
  export const create = (vertices = []) => ({ vertices: vertices, isClosed: false, transforms: mat4.create() })
@@ -10,8 +10,10 @@ import { toVertices } from './toVertices.js'
10
10
  * @param {Path3} a - the first path to compare
11
11
  * @param {Path3} b - the second path to compare
12
12
  * @returns {boolean}
13
- * @function
14
- * @alias module:modeling/geometries/path3.equals
13
+ * @alias module:modeling/path3.equals
14
+ *
15
+ * @example
16
+ * if (path3.equals(pathA, pathB)) { ... }
15
17
  */
16
18
  export const equals = (a, b) => {
17
19
  if (a.isClosed !== b.isClosed) {
@@ -15,11 +15,10 @@ import { create } from './create.js'
15
15
  * @param {boolean} [options.closed=false] - if the path should be open or closed
16
16
  * @param {Array} vertices - array of vertices (3D) from which to create the path
17
17
  * @returns {Path3} a new path
18
- * @function
19
- * @alias module:modeling/geometries/path3.fromVertices
18
+ * @alias module:modeling/path3.fromVertices
20
19
  *
21
20
  * @example
22
- * my newPath = fromVertices({closed: true}, [[10, 10, 10], [-10, 10, -10]])
21
+ * my newPath = path3.fromVertices({closed: true}, [[10, 10, 10], [-10, 10, -10]])
23
22
  */
24
23
  export const fromVertices = (options, vertices) => {
25
24
  const defaults = { closed: false }
@@ -1,3 +1,4 @@
1
+ export { clone } from './clone.js'
1
2
  export { close } from './close.js'
2
3
  export { concat } from './concat.js'
3
4
  export { create } from './create.js'
@@ -2,12 +2,29 @@
2
2
  * Represents a 3D geometry consisting of a list of ordered vertices.
3
3
  *
4
4
  * @see {@link Path3} for data structure information.
5
- * @module modeling/geometries/path3
5
+ * @module modeling/path3
6
6
  *
7
7
  * @example
8
8
  * import { path3 } from '@jscad/modeling'
9
9
  * let myShape = path3.fromVertices({ closed: true }, [[0,0,0], [4,0,0], [4,3,0]])
10
10
  */
11
+
12
+ /**
13
+ * @typedef {Object} Path3
14
+ * @property {Array} vertices - list of ordered vertices
15
+ * @property {boolean} isClosed - true if the path is closed where start and end vertices are the same
16
+ * @property {Mat4} transforms - transforms to apply to the vertices, see transform()
17
+ *
18
+ * @example
19
+ * // data structure
20
+ * {
21
+ * vertices: [[0,0,0], [4,0,0], [4,3,0]],
22
+ * isClosed: true,
23
+ * transforms: [1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1],
24
+ * }
25
+ */
26
+
27
+ export { clone } from './clone.js'
11
28
  export { close } from './close.js'
12
29
  export { concat } from './concat.js'
13
30
  export { create } from './create.js'
@@ -3,8 +3,10 @@
3
3
  *
4
4
  * @param {Object} object - the object to interrogate
5
5
  * @returns {boolean} true if the object matches a path3
6
- * @function
7
- * @alias module:modeling/geometries/path3.isA
6
+ * @alias module:modeling/path3.isA
7
+ *
8
+ * @example
9
+ * if (path3.isA(geometry)) { ... }
8
10
  */
9
11
  export const isA = (object) => {
10
12
  if (object && typeof object === 'object') {
@@ -5,11 +5,10 @@
5
5
  *
6
6
  * @param {Path3} geometry - the path to reverse
7
7
  * @returns {Path3} a new path
8
- * @function
9
- * @alias module:modeling/geometries/path3.reverse
8
+ * @alias module:modeling/path3.reverse
10
9
  *
11
10
  * @example
12
- * let newPath = reverse(path)
11
+ * let newPath = path3.reverse(oldPath)
13
12
  */
14
13
  export const reverse = (geometry) => {
15
14
  // NOTE: this only updates the order of the vertices
@@ -7,11 +7,10 @@ import { toVertices } from './toVertices.js'
7
7
  *
8
8
  * @param {path} geometry - the path
9
9
  * @returns {string} a representative string
10
- * @function
11
- * @alias module:modeling/geometries/path3.toString
10
+ * @alias module:modeling/path3.toString
12
11
  *
13
12
  * @example
14
- * console.out(toString(path))
13
+ * console.out(path3.toString(geometry))
15
14
  */
16
15
  export const toString = (geometry) => {
17
16
  const vertices = toVertices(geometry)
@@ -7,10 +7,9 @@ import { applyTransforms } from './applyTransforms.js'
7
7
  *
8
8
  * @param {Path3} geometry - the geometry
9
9
  * @returns {Array} an array of vertices
10
- * @function
11
- * @alias module:modeling/geometries/path3.toVertices
10
+ * @alias module:modeling/path3.toVertices
12
11
  *
13
12
  * @example
14
- * let sharedVertices = toVertices(path)
13
+ * let sharedVertices = path3.toVertices(geometry)
15
14
  */
16
15
  export const toVertices = (geometry) => applyTransforms(geometry).vertices
@@ -9,11 +9,10 @@ import * as mat4 from '../../maths/mat4/index.js'
9
9
  * @param {Mat4} matrix - the matrix to transform with
10
10
  * @param {Path3} geometry - the geometry to transform
11
11
  * @returns {Path3} a new path
12
- * @function
13
- * @alias module:modeling/geometries/path3.transform
12
+ * @alias module:modeling/path3.transform
14
13
  *
15
14
  * @example
16
- * let newPath = transform(fromZRotation(TAU / 8), path)
15
+ * let newPath = path3.transform(mat4.fromZRotation(TAU / 8), oldPath)
17
16
  */
18
17
  export const transform = (matrix, geometry) => {
19
18
  const transforms = mat4.multiply(mat4.create(), matrix, geometry.transforms)
@@ -4,14 +4,17 @@ import { isA } from './isA.js'
4
4
 
5
5
  /**
6
6
  * Determine if the given object is a valid path3.
7
+ *
7
8
  * Checks for valid vertices, and duplicate vertices.
8
9
  *
9
- * **If the geometry is not valid, an exception will be thrown with details of the geometry error.**
10
+ * **NOTE: If the geometry is not valid, an exception will be thrown with details of the geometry error.**
10
11
  *
11
12
  * @param {Object} object - the object to interrogate
12
13
  * @throws {Error} error if the geometry is not valid
13
- * @function
14
- * @alias module:modeling/geometries/path3.validate
14
+ * @alias module:modeling/path3.validate
15
+ *
16
+ * @example
17
+ * path3.vaidate(geometry)
15
18
  */
16
19
  export const validate = (object) => {
17
20
  if (!isA(object)) {
@@ -7,7 +7,10 @@ import { reverse } from './reverse.js'
7
7
  * @param {Array} points - a list of points, where each point is an array with X and Y values
8
8
  * @param {Poly2} polygon - a 2D polygon
9
9
  * @return {number} 1 if all points are inside, 0 if some or none are inside
10
- * @alias module:modeling/geometries/poly2.arePointsInside
10
+ * @alias module:modeling/poly2.arePointsInside
11
+ *
12
+ * @example
13
+ * if (poly2.arePointsInside([[0,0], [4,0], [4,3]], geometry) { ... }
11
14
  */
12
15
  export const arePointsInside = (points, polygon) => {
13
16
  if (points.length === 0) return 0 // nothing to check
@@ -3,6 +3,9 @@
3
3
  *
4
4
  * @param {Poly2} polygon - polygon to clone
5
5
  * @returns {Poly2} a new polygon
6
- * @alias module:modeling/geometries/poly2.clone
6
+ * @alias module:modeling/poly2.clone
7
+ *
8
+ * @example
9
+ * const newPoly = poly2.clone(oldPoly)
7
10
  */
8
11
  export const clone = (polygon) => Object.assign({}, polygon)
@@ -1,18 +1,11 @@
1
- /**
2
- * Represents a 2D polygon consisting of a list of ordered points
3
- * which is closed between start and end points.
4
- * @see https://en.wikipedia.org/wiki/Polygon
5
- * @property {Array} points - list of ordered points (2D)
6
- */
7
-
8
1
  /**
9
2
  * Creates a new polygon with initial values.
10
3
  *
11
4
  * @param {Array} [points] - list of points (2D)
12
5
  * @returns {Poly2} a new polygon
13
- * @alias module:modeling/geometries/poly2.create
6
+ * @alias module:modeling/poly2.create
14
7
  *
15
8
  * @example
16
- * let polygon = create([[0,0], [4,0], [4,3]])
9
+ * let polygon = poly2.create([[0,0], [4,0], [4,3]])
17
10
  */
18
11
  export const create = (points = []) => ({ points })
@@ -1,12 +1,24 @@
1
1
  /**
2
2
  * Represents a 2D polygon consisting of a list of ordered points.
3
- * @see {@link poly2} for data structure information.
4
- * @module modeling/geometries/poly2
3
+ * @see {@link Poly2} for data structure information.
4
+ * @module modeling/poly2
5
5
  *
6
6
  * @example
7
- * import { geometries } from '@jscad/modeling'
8
- * const p1 = geometries.poly2.create([[0,0], [4,0], [4,3]])
7
+ * import { poly2 } from '@jscad/modeling'
8
+ * const p1 = poly2.create([[0,0], [4,0], [4,3]])
9
9
  */
10
+
11
+ /**
12
+ * @typedef {Object} Poly2
13
+ * @property {Array} points - list of ordered points (2D)
14
+ *
15
+ * @example
16
+ * // data structure
17
+ * {
18
+ * points: [[0,0], [4,0], [4,3]],
19
+ * }
20
+ */
21
+
10
22
  export { arePointsInside } from './arePointsInside.js'
11
23
  export { clone } from './clone.js'
12
24
  export { create } from './create.js'
@@ -1,8 +1,12 @@
1
1
  /**
2
2
  * Determine if the given object is a 2D polygon.
3
+ *
3
4
  * @param {object} object - the object to interrogate
4
5
  * @returns {Boolean} true if the object matches a poly2
5
- * @alias module:modeling/geometries/poly2.isA
6
+ * @alias module:modeling/poly2.isA
7
+ *
8
+ * @example
9
+ * if (poly2.isA(geometry)) { ... }
6
10
  */
7
11
  export const isA = (object) => {
8
12
  if (object && typeof object === 'object') {
@@ -1,8 +1,12 @@
1
1
  /**
2
2
  * Check whether the given polygon is convex.
3
+ *
3
4
  * @param {Poly2} polygon - the polygon to interrogate
4
5
  * @returns {Boolean} true if convex
5
- * @alias module:modeling/geometries/poly2.isConvex
6
+ * @alias module:modeling/poly2.isConvex
7
+ *
8
+ * @example
9
+ * if (poly2.isConvex(geometry)) { ... }
6
10
  */
7
11
  export const isConvex = (polygon) => {
8
12
  const numPoints = polygon.points.length
@@ -3,9 +3,13 @@ import { intersect } from '../../maths/utils/intersect.js'
3
3
  /**
4
4
  * Check whether the given polygon is simple, i.e. does not intersect itself.
5
5
  * @see https://en.wikipedia.org/wiki/Simple_polygon
6
+ *
6
7
  * @param {Poly2} polygon - the polygon to interrogate
7
8
  * @returns {Boolean} true if simple
8
- * @alias module:modeling/geometries/poly2.isSimple
9
+ * @alias module:modeling/poly2.isSimple
10
+ *
11
+ * @example
12
+ * if (poly2.isSimple(geometry)) { ... }
9
13
  */
10
14
  export const isSimple = (polygon) => {
11
15
  const numPoints = polygon.points.length
@@ -5,6 +5,9 @@ import { area } from '../../maths/utils/area.js'
5
5
  *
6
6
  * @param {Poly2} polygon - the polygon to measure
7
7
  * @return {number} the area of the polygon
8
- * @alias module:modeling/geometries/poly2.measureArea
8
+ * @alias module:modeling/poly2.measureArea
9
+ *
10
+ * @example
11
+ * const area = poly2.measureArea(geometry)
9
12
  */
10
13
  export const measureArea = (polygon) => area(polygon.points)
@@ -1,9 +1,14 @@
1
1
  import * as vec2 from '../../maths/vec2/index.js'
2
2
 
3
3
  /**
4
+ * Measure the min and max bounds of the given polygon.
5
+ *
4
6
  * @param {Poly2} polygon - the polygon to measure
5
7
  * @returns {Array} an array of two vectors (2D); minimum and maximum coordinates
6
- * @alias module:modeling/geometries/poly2.measureBoundingBox
8
+ * @alias module:modeling/poly2.measureBoundingBox
9
+ *
10
+ * @example
11
+ * const bounds = poly2.measureBoundingBox(geometry)
7
12
  */
8
13
  export const measureBoundingBox = (polygon) => {
9
14
  const points = polygon.points
@@ -5,7 +5,10 @@ import { create } from './create.js'
5
5
  *
6
6
  * @param {Poly2} polygon - the polygon to reverse
7
7
  * @returns {Poly2} a new polygon
8
- * @alias module:modeling/geometries/poly2.reverse
8
+ * @alias module:modeling/poly2.reverse
9
+ *
10
+ * @example
11
+ * let newPoly = poly2.reverse(oldPoly)
9
12
  */
10
13
  export const reverse = (polygon) => {
11
14
  const points = polygon.points.slice().reverse()
@@ -1,8 +1,13 @@
1
1
  /**
2
2
  * Return the given polygon as a list of points.
3
+ *
3
4
  * NOTE: The returned array should not be modified as the points are shared with the geometry.
5
+ *
4
6
  * @param {Poly2} polygon - the polygon
5
7
  * @return {Array} list of points (2D)
6
- * @alias module:modeling/geometries/poly2.toPoints
8
+ * @alias module:modeling/poly2.toPoints
9
+ *
10
+ * @example
11
+ * const sharedPoints = poly2.toPoints(geometry)
7
12
  */
8
13
  export const toPoints = (polygon) => polygon.points
@@ -2,8 +2,12 @@ import * as vec2 from '../../maths/vec2/index.js'
2
2
 
3
3
  /**
4
4
  * Convert the given polygon to a readable string.
5
+ *
5
6
  * @param {Poly2} polygon - the polygon to convert
6
7
  * @return {String} the string representation
7
- * @alias module:modeling/geometries/poly2.toString
8
+ * @alias module:modeling/poly2.toString
9
+ *
10
+ * @example
11
+ * console.log(poly2.toString(geometry))
8
12
  */
9
13
  export const toString = (polygon) => `poly2: [${polygon.points.map(vec2.toString).join(', ')}]`
@@ -5,10 +5,14 @@ import { create } from './create.js'
5
5
 
6
6
  /**
7
7
  * Transform the given polygon using the given matrix.
8
+ *
8
9
  * @param {Mat4} matrix - the matrix to transform with
9
10
  * @param {Poly2} polygon - the polygon to transform
10
11
  * @returns {Poly2} a new polygon
11
- * @alias module:modeling/geometries/poly2.transform
12
+ * @alias module:modeling/poly2.transform
13
+ *
14
+ * @example
15
+ * let newPoly = poly2.transfrom(oldPoly)
12
16
  */
13
17
  export const transform = (matrix, polygon) => {
14
18
  const points = polygon.points.map((point) => vec2.transform(vec2.create(), point, matrix))
@@ -1,7 +1,3 @@
1
1
  import type { Vec2 } from '../../maths/vec2/type.d.ts'
2
2
 
3
- export declare interface Poly2 {
4
- points: Array<Vec2>
5
- }
6
-
7
- export default Poly2
3
+ export declare interface Poly2 { points: Array<Vec2> }
@@ -5,13 +5,17 @@ import { measureArea } from './measureArea.js'
5
5
 
6
6
  /**
7
7
  * Determine if the given object is a valid polygon.
8
+ *
8
9
  * Checks for valid data structure, convex polygons, and duplicate points.
9
10
  *
10
- * **If the geometry is not valid, an exception will be thrown with details of the geometry error.**
11
+ * **NOTE: If the geometry is not valid, an exception will be thrown with details of the geometry error.**
11
12
  *
12
13
  * @param {object} object - the object to interrogate
13
14
  * @throws {Error} error if the geometry is not valid
14
- * @alias module:modeling/geometries/poly2.validate
15
+ * @alias module:modeling/poly2.validate
16
+ *
17
+ * @example
18
+ * poly2.validate(geometry)
15
19
  */
16
20
  export const validate = (object) => {
17
21
  if (!isA(object)) {
@@ -8,7 +8,10 @@ import { create } from './create.js'
8
8
  * @param {Poly3} [out] - receiving polygon
9
9
  * @param {Poly3} polygon - polygon to clone
10
10
  * @returns {Poly3} a new polygon
11
- * @alias module:modeling/geometries/poly3.clone
11
+ * @alias module:modeling/poly3.clone
12
+ *
13
+ * @example
14
+ * const newPoly = poly3.clone(oldPoly)
12
15
  */
13
16
  export const clone = (...params) => {
14
17
  let out
@@ -1,19 +1,11 @@
1
- /**
2
- * Represents a convex 3D polygon. The vertices used to initialize a polygon must
3
- * be coplanar and form a convex shape. The vertices do not have to be `vec3`
4
- * instances but they must behave similarly.
5
- * @property {Array} vertices - list of ordered vertices (3D)
6
- * @example
7
- * {"vertices": [[0,0,0], [4,0,0], [4,3,12]]}
8
- */
9
-
10
1
  /**
11
2
  * Creates a new 3D polygon with initial values.
12
3
  *
13
4
  * @param {Array} [vertices] - a list of vertices (3D)
14
5
  * @returns {Poly3} a new polygon
15
- * @alias module:modeling/geometries/poly3.create
6
+ * @alias module:modeling/poly3.create
7
+ *
16
8
  * @example
17
- * const polygon = create([[1, 0], [0, 1], [0, 0]])
9
+ * const polygon = poly3.create([[1, 0], [0, 1], [0, 0]])
18
10
  */
19
11
  export const create = (vertices = []) => ({ vertices })
@@ -2,11 +2,13 @@ import { create } from './create.js'
2
2
 
3
3
  /**
4
4
  * Create a polygon from the given vertices and plane.
5
+ *
5
6
  * NOTE: No checks are performed on the parameters.
7
+ *
6
8
  * @param {Array} vertices - list of vertices (3D)
7
9
  * @param {Plane} plane - plane of the polygon
8
10
  * @returns {Poly3} a new polygon
9
- * @alias module:modeling/geometries/poly3.fromVerticesAndPlane
11
+ * @alias module:modeling/poly3.fromVerticesAndPlane
10
12
  */
11
13
  export const fromVerticesAndPlane = (vertices, plane) => {
12
14
  const poly = create(vertices)