@jscad/modeling 3.0.2-alpha.0 → 3.0.4-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.
- package/CHANGELOG.md +25 -0
- package/dist/jscad-modeling.es.js +2 -7
- package/dist/jscad-modeling.min.js +2 -7
- package/package.json +6 -7
- package/rollup.config.js +8 -4
- package/src/colors/colorize.test.js +1 -1
- package/src/curves/bezier/arcLengthToT.js +1 -1
- package/src/curves/bezier/create.js +1 -1
- package/src/curves/bezier/index.js +7 -7
- package/src/curves/bezier/length.js +1 -1
- package/src/curves/bezier/lengths.js +2 -1
- package/src/curves/bezier/tangentAt.js +1 -1
- package/src/curves/bezier/valueAt.js +1 -1
- package/src/curves/index.js +3 -3
- package/src/geometries/geom2/applyTransforms.js +3 -1
- package/src/geometries/geom2/clone.js +5 -1
- package/src/geometries/geom2/create.js +4 -14
- package/src/geometries/geom2/fromSides.js +4 -2
- package/src/geometries/geom2/index.d.ts +0 -2
- package/src/geometries/geom2/index.js +21 -7
- package/src/geometries/geom2/isA.js +5 -1
- package/src/geometries/geom2/reverse.js +4 -2
- package/src/geometries/geom2/toOutlines.js +2 -1
- package/src/geometries/geom2/toPoints.js +5 -2
- package/src/geometries/geom2/toSides.js +4 -3
- package/src/geometries/geom2/toString.js +3 -2
- package/src/geometries/geom2/transform.js +4 -2
- package/src/geometries/geom2/validate.js +6 -2
- package/src/geometries/geom3/applyTransforms.test.js +2 -2
- package/src/geometries/geom3/clone.js +5 -1
- package/src/geometries/geom3/clone.test.js +2 -2
- package/src/geometries/geom3/create.js +6 -28
- package/src/geometries/geom3/{fromPoints.d.ts → fromVertices.d.ts} +1 -1
- package/src/geometries/geom3/{fromPoints.js → fromVertices.js} +15 -2
- package/src/geometries/geom3/{fromPoints.test.js → fromVertices.test.js} +6 -6
- package/src/geometries/geom3/{fromPointsConvex.d.ts → fromVerticesConvex.d.ts} +1 -1
- package/src/geometries/geom3/fromVerticesConvex.js +25 -0
- package/src/geometries/geom3/{fromPointsConvex.test.js → fromVerticesConvex.test.js} +3 -3
- package/src/geometries/geom3/index.d.ts +4 -5
- package/src/geometries/geom3/index.js +29 -9
- package/src/geometries/geom3/invert.js +5 -1
- package/src/geometries/geom3/invert.test.js +2 -2
- package/src/geometries/geom3/isA.js +5 -1
- package/src/geometries/geom3/isA.test.js +2 -2
- package/src/geometries/geom3/isConvex.d.ts +3 -0
- package/src/geometries/geom3/isConvex.js +65 -0
- package/src/geometries/geom3/isConvex.test.js +44 -0
- package/src/geometries/geom3/toPolygons.js +4 -2
- package/src/geometries/geom3/toString.js +3 -2
- package/src/geometries/geom3/toString.test.js +2 -2
- package/src/geometries/geom3/{toPoints.d.ts → toVertices.d.ts} +1 -1
- package/src/geometries/geom3/toVertices.js +20 -0
- package/src/geometries/geom3/{toPoints.test.js → toVertices.test.js} +4 -4
- package/src/geometries/geom3/transform.js +5 -2
- package/src/geometries/geom3/transform.test.js +2 -2
- package/src/geometries/geom3/validate.js +6 -2
- package/src/geometries/geom3/validate.test.js +4 -4
- package/src/geometries/index.d.ts +1 -0
- package/src/geometries/index.js +10 -7
- package/src/geometries/path2/appendArc.js +7 -5
- package/src/geometries/path2/appendArc.test.js +11 -15
- package/src/geometries/path2/appendBezier.js +6 -4
- package/src/geometries/path2/appendPoints.js +4 -2
- package/src/geometries/path2/applyTransforms.js +3 -0
- package/src/geometries/path2/clone.js +5 -1
- package/src/geometries/path2/close.js +5 -1
- package/src/geometries/path2/concat.js +3 -2
- package/src/geometries/path2/create.js +5 -25
- package/src/geometries/path2/equals.js +12 -7
- package/src/geometries/path2/fromPoints.js +5 -3
- package/src/geometries/path2/index.d.ts +0 -2
- package/src/geometries/path2/index.js +21 -6
- package/src/geometries/path2/isA.js +5 -1
- package/src/geometries/path2/reverse.js +4 -2
- package/src/geometries/path2/toPoints.js +5 -3
- package/src/geometries/path2/toString.js +3 -2
- package/src/geometries/path2/transform.js +4 -2
- package/src/geometries/path2/validate.js +5 -1
- package/src/geometries/path3/applyTransforms.js +22 -0
- package/src/geometries/path3/applyTransforms.test.js +28 -0
- package/src/geometries/path3/close.d.ts +3 -0
- package/src/geometries/path3/close.js +33 -0
- package/src/geometries/path3/close.test.js +43 -0
- package/src/geometries/path3/concat.d.ts +3 -0
- package/src/geometries/path3/concat.js +35 -0
- package/src/geometries/path3/concat.test.js +35 -0
- package/src/geometries/path3/create.d.ts +4 -0
- package/src/geometries/path3/create.js +14 -0
- package/src/geometries/path3/create.test.js +8 -0
- package/src/geometries/path3/equals.d.ts +3 -0
- package/src/geometries/path3/equals.js +50 -0
- package/src/geometries/path3/equals.test.js +38 -0
- package/src/geometries/path3/fromVertices.d.ts +8 -0
- package/src/geometries/path3/fromVertices.js +44 -0
- package/src/geometries/path3/fromVertices.test.js +33 -0
- package/src/geometries/path3/index.d.ts +13 -0
- package/src/geometries/path3/index.js +37 -0
- package/src/geometries/path3/isA.d.ts +3 -0
- package/src/geometries/path3/isA.js +22 -0
- package/src/geometries/path3/isA.test.js +19 -0
- package/src/geometries/path3/reverse.d.ts +3 -0
- package/src/geometries/path3/reverse.js +18 -0
- package/src/geometries/path3/reverse.test.js +9 -0
- package/src/geometries/path3/toString.d.ts +3 -0
- package/src/geometries/path3/toString.js +23 -0
- package/src/geometries/path3/toVertices.d.ts +4 -0
- package/src/geometries/path3/toVertices.js +15 -0
- package/src/geometries/path3/toVertices.test.js +13 -0
- package/src/geometries/path3/transform.d.ts +4 -0
- package/src/geometries/path3/transform.js +20 -0
- package/src/geometries/path3/transform.test.js +50 -0
- package/src/geometries/path3/type.d.ts +10 -0
- package/src/geometries/path3/validate.d.ts +1 -0
- package/src/geometries/path3/validate.js +44 -0
- package/src/geometries/poly2/arePointsInside.js +4 -1
- package/src/geometries/poly2/clone.js +4 -1
- package/src/geometries/poly2/create.js +3 -15
- package/src/geometries/poly2/index.js +16 -4
- package/src/geometries/poly2/isA.js +5 -1
- package/src/geometries/poly2/isConvex.js +5 -1
- package/src/geometries/poly2/isSimple.js +5 -1
- package/src/geometries/poly2/measureArea.js +4 -1
- package/src/geometries/poly2/measureBoundingBox.js +6 -1
- package/src/geometries/poly2/reverse.js +4 -1
- package/src/geometries/poly2/toPoints.js +6 -1
- package/src/geometries/poly2/toString.js +5 -1
- package/src/geometries/poly2/transform.js +5 -1
- package/src/geometries/poly2/validate.js +6 -2
- package/src/geometries/poly3/clone.js +4 -1
- package/src/geometries/poly3/create.js +4 -17
- package/src/geometries/poly3/fromVerticesAndPlane.js +3 -1
- package/src/geometries/poly3/index.js +19 -4
- package/src/geometries/poly3/invert.js +4 -1
- package/src/geometries/poly3/isA.js +5 -1
- package/src/geometries/poly3/isConvex.js +5 -1
- package/src/geometries/poly3/measureArea.js +5 -1
- package/src/geometries/poly3/measureBoundingBox.js +4 -1
- package/src/geometries/poly3/measureBoundingSphere.js +4 -3
- package/src/geometries/poly3/measureSignedVolume.js +6 -1
- package/src/geometries/poly3/plane.js +6 -0
- package/src/geometries/poly3/toString.js +5 -1
- package/src/geometries/poly3/toVertices.js +6 -1
- package/src/geometries/poly3/transform.js +5 -1
- package/src/geometries/poly3/validate.js +6 -2
- package/src/geometries/slice/calculatePlane.js +3 -3
- package/src/geometries/slice/clone.js +4 -1
- package/src/geometries/slice/create.js +5 -10
- package/src/geometries/slice/equals.js +5 -1
- package/src/geometries/slice/fromGeom2.js +1 -1
- package/src/geometries/slice/fromVertices.js +3 -3
- package/src/geometries/slice/index.js +19 -4
- package/src/geometries/slice/isA.js +5 -1
- package/src/geometries/slice/reverse.js +5 -2
- package/src/geometries/slice/toEdges.js +5 -3
- package/src/geometries/slice/toPolygons.js +5 -1
- package/src/geometries/slice/toString.js +5 -1
- package/src/geometries/slice/toVertices.js +5 -3
- package/src/geometries/slice/transform.js +4 -3
- package/src/geometries/slice/validate.js +3 -2
- package/src/index.d.ts +1 -0
- package/src/index.js +4 -0
- package/src/maths/constants.js +11 -7
- package/src/maths/index.js +2 -1
- package/src/maths/mat4/isOnlyTransformScale.js +1 -1
- package/src/operations/booleans/index.js +2 -0
- package/src/operations/booleans/intersect.js +0 -1
- package/src/operations/booleans/intersectGeom3.test.js +4 -4
- package/src/operations/booleans/scission.js +0 -1
- package/src/operations/booleans/subtractGeom3.test.js +4 -4
- package/src/operations/booleans/trees/splitLineSegmentByPlane.js +1 -4
- package/src/operations/booleans/trees/splitPolygonByPlane.test.js +138 -0
- package/src/operations/booleans/unionGeom3.test.js +40 -5
- package/src/operations/extrusions/extrudeFromSlices.js +15 -5
- package/src/operations/extrusions/extrudeFromSlices.test.js +6 -6
- package/src/operations/extrusions/extrudeLinear.test.js +8 -8
- package/src/operations/extrusions/extrudeRotate.js +2 -1
- package/src/operations/extrusions/extrudeRotate.test.js +46 -12
- package/src/operations/extrusions/extrudeWalls.test.js +60 -0
- package/src/operations/hulls/hull.test.js +5 -5
- package/src/operations/hulls/hullChain.test.js +5 -5
- package/src/operations/hulls/toUniquePoints.js +2 -2
- package/src/operations/minkowski/index.d.ts +1 -0
- package/src/operations/minkowski/index.js +15 -0
- package/src/operations/minkowski/minkowskiSum.d.ts +4 -0
- package/src/operations/minkowski/minkowskiSum.js +223 -0
- package/src/operations/minkowski/minkowskiSum.test.js +199 -0
- package/src/operations/modifiers/generalize.test.js +6 -6
- package/src/operations/modifiers/insertTjunctions.test.js +2 -2
- package/src/operations/modifiers/reTesselateCoplanarPolygons.js +10 -3
- package/src/operations/modifiers/reTesselateCoplanarPolygons.test.js +36 -1
- package/src/operations/modifiers/retessellate.js +4 -2
- package/src/operations/modifiers/retessellate.test.js +10 -10
- package/src/operations/modifiers/snap.test.js +28 -19
- package/src/operations/offsets/offsetGeom3.test.js +9 -11
- package/src/operations/transforms/center.test.js +7 -7
- package/src/operations/transforms/mirror.test.js +7 -7
- package/src/operations/transforms/rotate.test.js +7 -7
- package/src/operations/transforms/scale.test.js +7 -7
- package/src/operations/transforms/transform.test.js +2 -2
- package/src/operations/transforms/translate.test.js +7 -7
- package/src/primitives/arc.js +2 -2
- package/src/primitives/arc.test.js +104 -113
- package/src/primitives/cube.test.js +4 -4
- package/src/primitives/cuboid.test.js +4 -4
- package/src/primitives/cylinder.test.js +5 -5
- package/src/primitives/cylinderElliptic.test.js +9 -9
- package/src/primitives/ellipsoid.test.js +5 -5
- package/src/primitives/geodesicSphere.test.js +4 -4
- package/src/primitives/polyhedron.test.js +2 -2
- package/src/primitives/roundedCuboid.test.js +7 -7
- package/src/primitives/roundedCylinder.test.js +9 -9
- package/src/primitives/sphere.test.js +5 -5
- package/src/primitives/torus.test.js +4 -4
- package/src/utils/flatten.js +1 -1
- package/src/utils/flatten.test.js +94 -0
- package/src/geometries/geom2/fromCompactBinary.d.ts +0 -3
- package/src/geometries/geom2/fromCompactBinary.js +0 -40
- package/src/geometries/geom2/fromToCompactBinary.test.js +0 -100
- package/src/geometries/geom2/toCompactBinary.d.ts +0 -3
- package/src/geometries/geom2/toCompactBinary.js +0 -56
- package/src/geometries/geom3/fromCompactBinary.d.ts +0 -3
- package/src/geometries/geom3/fromCompactBinary.js +0 -42
- package/src/geometries/geom3/fromPointsConvex.js +0 -25
- package/src/geometries/geom3/fromToCompactBinary.test.js +0 -139
- package/src/geometries/geom3/toCompactBinary.d.ts +0 -3
- package/src/geometries/geom3/toCompactBinary.js +0 -66
- package/src/geometries/geom3/toPoints.js +0 -15
- package/src/geometries/path2/fromCompactBinary.d.ts +0 -3
- package/src/geometries/path2/fromCompactBinary.js +0 -31
- package/src/geometries/path2/fromToCompactBinary.test.js +0 -114
- package/src/geometries/path2/toCompactBinary.d.ts +0 -3
- package/src/geometries/path2/toCompactBinary.js +0 -50
|
@@ -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/
|
|
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/
|
|
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/
|
|
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/
|
|
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/
|
|
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))
|
|
@@ -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/
|
|
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/
|
|
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,24 +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/
|
|
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
|
-
export const create = (vertices) => {
|
|
20
|
-
if (vertices === undefined || vertices.length < 3) {
|
|
21
|
-
vertices = [] // empty contents
|
|
22
|
-
}
|
|
23
|
-
return { vertices }
|
|
24
|
-
}
|
|
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/
|
|
11
|
+
* @alias module:modeling/poly3.fromVerticesAndPlane
|
|
10
12
|
*/
|
|
11
13
|
export const fromVerticesAndPlane = (vertices, plane) => {
|
|
12
14
|
const poly = create(vertices)
|
|
@@ -1,12 +1,27 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Represents a convex 3D polygon consisting of a list of ordered vertices.
|
|
3
|
-
*
|
|
4
|
-
*
|
|
3
|
+
*
|
|
4
|
+
* The vertices used to initialize a polygon must be coplanar and form a convex shape.
|
|
5
|
+
*
|
|
6
|
+
* @see {@link Poly3} for data structure information.
|
|
7
|
+
* @module modeling/poly3
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* import { poly3 } from '@jscad/modeling'
|
|
11
|
+
* const polygon = poly3.create([[0,0,0], [4,0,0], [4,3,12]])
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* @typedef {Object} Poly3
|
|
16
|
+
* @property {Array} vertices - list of ordered vertices (3D)
|
|
5
17
|
*
|
|
6
18
|
* @example
|
|
7
|
-
*
|
|
8
|
-
*
|
|
19
|
+
* // data structure
|
|
20
|
+
* {
|
|
21
|
+
* vertices: [[0,0,0], [4,0,0], [4,3,12]]
|
|
22
|
+
* }
|
|
9
23
|
*/
|
|
24
|
+
|
|
10
25
|
export { clone } from './clone.js'
|
|
11
26
|
export { create } from './create.js'
|
|
12
27
|
export { fromVerticesAndPlane } from './fromVerticesAndPlane.js'
|
|
@@ -7,7 +7,10 @@ import { create } from './create.js'
|
|
|
7
7
|
*
|
|
8
8
|
* @param {Poly3} polygon - the polygon to invert
|
|
9
9
|
* @returns {Poly3} a new poly3
|
|
10
|
-
* @alias module:modeling/
|
|
10
|
+
* @alias module:modeling/poly3.invert
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* const newPoly = poly3.invert(oldPoly)
|
|
11
14
|
*/
|
|
12
15
|
export const invert = (polygon) => {
|
|
13
16
|
const vertices = polygon.vertices.slice().reverse()
|
|
@@ -1,8 +1,12 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Determine if the given object is a polygon.
|
|
3
|
+
*
|
|
3
4
|
* @param {object} object - the object to interrogate
|
|
4
5
|
* @returns {Boolean} true if the object matches a poly3
|
|
5
|
-
* @alias module:modeling/
|
|
6
|
+
* @alias module:modeling/poly3.isA
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* if (poly3.isA(geometry)) { ... }
|
|
6
10
|
*/
|
|
7
11
|
export const isA = (object) => {
|
|
8
12
|
if (object && typeof object === 'object') {
|
|
@@ -3,9 +3,13 @@ import * as vec3 from '../../maths/vec3/index.js'
|
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* Check whether the given polygon is convex.
|
|
6
|
+
*
|
|
6
7
|
* @param {Poly3} polygon - the polygon to interrogate
|
|
7
8
|
* @returns {Boolean} true if convex
|
|
8
|
-
* @alias module:modeling/
|
|
9
|
+
* @alias module:modeling/poly3.isConvex
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* if (poly3.isConvex(geometry)) { ... }
|
|
9
13
|
*/
|
|
10
14
|
export const isConvex = (polygon) => areVerticesConvex(polygon.vertices)
|
|
11
15
|
|
|
@@ -3,9 +3,13 @@ import { plane } from './plane.js'
|
|
|
3
3
|
/**
|
|
4
4
|
* Measure the area of the given polygon.
|
|
5
5
|
* @see 2000 softSurfer http://geomalgorithms.com
|
|
6
|
+
*
|
|
6
7
|
* @param {Poly3} polygon - the polygon to measure
|
|
7
8
|
* @return {number} area of the polygon
|
|
8
|
-
* @alias module:modeling/
|
|
9
|
+
* @alias module:modeling/poly3.measureArea
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* const area = poly3.measureArea(polyA)
|
|
9
13
|
*/
|
|
10
14
|
export const measureArea = (polygon) => {
|
|
11
15
|
const n = polygon.vertices.length
|
|
@@ -5,7 +5,10 @@ import * as vec3 from '../../maths/vec3/index.js'
|
|
|
5
5
|
*
|
|
6
6
|
* @param {Poly3} polygon - the polygon to measure
|
|
7
7
|
* @returns {Array} an array of two vectors (3D); minimum and maximum coordinates
|
|
8
|
-
* @alias module:modeling/
|
|
8
|
+
* @alias module:modeling/poly3.measureBoundingBox
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* const bounds = poly3.measureBoundingBox(polyA)
|
|
9
12
|
*/
|
|
10
13
|
export const measureBoundingBox = (polygon) => {
|
|
11
14
|
const vertices = polygon.vertices
|
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
import * as vec4 from '../../maths/vec4/index.js'
|
|
2
|
-
|
|
3
1
|
const cache = new WeakMap()
|
|
4
2
|
|
|
5
3
|
/**
|
|
@@ -8,7 +6,10 @@ const cache = new WeakMap()
|
|
|
8
6
|
* @param {Vec4} out - receiving vector
|
|
9
7
|
* @param {Poly3} polygon - the polygon to measure
|
|
10
8
|
* @returns {Vec4} the computed bounding sphere; center vertex (3D) and radius
|
|
11
|
-
* @alias module:modeling/
|
|
9
|
+
* @alias module:modeling/poly3.measureBoundingSphere
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* const bounds = poly2.measureBoundingSphere(polyA)
|
|
12
13
|
*/
|
|
13
14
|
export const measureBoundingSphere = (out, polygon) => {
|
|
14
15
|
const vertices = polygon.vertices
|
|
@@ -2,12 +2,17 @@ import * as vec3 from '../../maths/vec3/index.js'
|
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* Measure the signed volume of the given polygon, which must be convex.
|
|
5
|
+
*
|
|
5
6
|
* The volume is that formed by the tetrahedron connected to the axis [0,0,0],
|
|
6
7
|
* and will be positive or negative based on the rotation of the vertices.
|
|
7
8
|
* @see http://chenlab.ece.cornell.edu/Publication/Cha/icip01_Cha.pdf
|
|
9
|
+
*
|
|
8
10
|
* @param {Poly3} polygon - the polygon to measure
|
|
9
11
|
* @return {number} volume of the polygon
|
|
10
|
-
* @alias module:modeling/
|
|
12
|
+
* @alias module:modeling/poly3.measureSignedVolume
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* const volume = poly3.measureSignedVolumne(polyA)
|
|
11
16
|
*/
|
|
12
17
|
export const measureSignedVolume = (polygon) => {
|
|
13
18
|
let signedVolume = 0
|
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
import * as mplane from '../../maths/plane/index.js'
|
|
2
2
|
|
|
3
|
+
/**
|
|
4
|
+
* Determine the plane of the given polygon.
|
|
5
|
+
*
|
|
6
|
+
* @param {Poly3} polygon - the polygon of which to fetch the plane
|
|
7
|
+
* @alias module:modeling/poly3.plane
|
|
8
|
+
*/
|
|
3
9
|
export const plane = (polygon) => {
|
|
4
10
|
if (!polygon.plane) {
|
|
5
11
|
polygon.plane = mplane.fromPoints(mplane.create(), ...polygon.vertices)
|
|
@@ -2,8 +2,12 @@ import * as vec3 from '../../maths/vec3/index.js'
|
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* Convert the given polygon to a readable string.
|
|
5
|
+
*
|
|
5
6
|
* @param {Poly3} polygon - the polygon to convert
|
|
6
7
|
* @return {String} the string representation
|
|
7
|
-
* @alias module:modeling/
|
|
8
|
+
* @alias module:modeling/poly3.toString
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* console.log(poly3.toString(polyA))
|
|
8
12
|
*/
|
|
9
13
|
export const toString = (polygon) => `poly3: [${polygon.vertices.map(vec3.toString).join(', ')}]`
|
|
@@ -1,8 +1,13 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Return the given polygon as a list of vertices.
|
|
3
|
+
*
|
|
3
4
|
* NOTE: The returned array should not be modified as the vertices are shared with the geometry.
|
|
5
|
+
*
|
|
4
6
|
* @param {Poly3} polygon - the polygon
|
|
5
7
|
* @return {Array} list of vertices (3D)
|
|
6
|
-
* @alias module:modeling/
|
|
8
|
+
* @alias module:modeling/poly3.toVertices
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* const sharedVertices = poly3.toVertices(polyA)
|
|
7
12
|
*/
|
|
8
13
|
export const toVertices = (polygon) => polygon.vertices
|
|
@@ -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 {Poly3} polygon - the polygon to transform
|
|
10
11
|
* @returns {Poly3} a new polygon
|
|
11
|
-
* @alias module:modeling/
|
|
12
|
+
* @alias module:modeling/poly3.transform
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* const newPoly = poly3.transform(oldPoly)
|
|
12
16
|
*/
|
|
13
17
|
export const transform = (matrix, polygon) => {
|
|
14
18
|
const vertices = polygon.vertices.map((vertex) => vec3.transform(vec3.create(), vertex, matrix))
|
|
@@ -9,13 +9,17 @@ import { plane } from './plane.js'
|
|
|
9
9
|
|
|
10
10
|
/**
|
|
11
11
|
* Determine if the given object is a valid polygon.
|
|
12
|
+
*
|
|
12
13
|
* Checks for valid data structure, convex polygons, and duplicate vertices.
|
|
13
14
|
*
|
|
14
|
-
* **If the geometry is not valid, an exception will be thrown with details of the geometry error.**
|
|
15
|
+
* **NOTE: If the geometry is not valid, an exception will be thrown with details of the geometry error.**
|
|
15
16
|
*
|
|
16
17
|
* @param {object} object - the object to interrogate
|
|
17
18
|
* @throws {Error} error if the geometry is not valid
|
|
18
|
-
* @alias module:modeling/
|
|
19
|
+
* @alias module:modeling/poly3.validate
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* poly3.validate(geometry)
|
|
19
23
|
*/
|
|
20
24
|
export const validate = (object) => {
|
|
21
25
|
if (!isA(object)) {
|
|
@@ -3,13 +3,13 @@ import * as vec3 from '../../maths/vec3/index.js'
|
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* Calculate the plane of the given slice.
|
|
6
|
-
*
|
|
6
|
+
*
|
|
7
7
|
* @param {Slice} slice - the slice
|
|
8
8
|
* @returns {Plane} the plane of the slice
|
|
9
|
-
* @alias module:modeling/
|
|
9
|
+
* @alias module:modeling/slice.calculatePlane
|
|
10
10
|
*
|
|
11
11
|
* @example
|
|
12
|
-
*
|
|
12
|
+
* const plane = slice.calculatePlane(sliceA)
|
|
13
13
|
*/
|
|
14
14
|
export const calculatePlane = (slice) => {
|
|
15
15
|
if (slice.contours.length < 1) {
|
|
@@ -4,6 +4,9 @@
|
|
|
4
4
|
*
|
|
5
5
|
* @param {Slice} slice - slice to clone
|
|
6
6
|
* @returns {Slice} a new slice
|
|
7
|
-
* @alias module:modeling/
|
|
7
|
+
* @alias module:modeling/slice.clone
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* const newSlice = slice.clone(oldSlice)
|
|
8
11
|
*/
|
|
9
12
|
export const clone = (slice) => Object.assign({}, slice)
|
|
@@ -1,18 +1,13 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Represents a 3D geometry consisting of a list of contours,
|
|
3
|
-
* where each contour consists of a list of planar vertices.
|
|
4
|
-
* @property {Array} contours - list of contours, each contour containing a list of 3D vertices
|
|
5
|
-
* @example
|
|
6
|
-
* {"contours": [[[0,0,1], [4,0,1], [4,3,1]]]}
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
1
|
/**
|
|
10
2
|
* Creates a new slice from the given contours.
|
|
11
3
|
*
|
|
4
|
+
* NOTE: The slice (and all vertices) are assumed to be planar from the beginning.
|
|
5
|
+
*
|
|
12
6
|
* @param {Array} [contours] - a list of contours, where each contour contains a list of vertices (3D)
|
|
13
7
|
* @returns {Slice} a new slice
|
|
14
|
-
* @alias module:modeling/
|
|
8
|
+
* @alias module:modeling/slice.create
|
|
9
|
+
*
|
|
15
10
|
* @example
|
|
16
|
-
* const slice = create([ [[0,0,1], [4,0,1], [4,3,1]] ])
|
|
11
|
+
* const slice = slice.create([ [[0,0,1], [4,0,1], [4,3,1]] ])
|
|
17
12
|
*/
|
|
18
13
|
export const create = (contours = []) => ({ contours })
|
|
@@ -2,10 +2,14 @@ import * as vec3 from '../../maths/vec3/index.js'
|
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* Determine if the given slices have the same contours.
|
|
5
|
+
*
|
|
5
6
|
* @param {Slice} a - the first slice to compare
|
|
6
7
|
* @param {Slice} b - the second slice to compare
|
|
7
8
|
* @returns {Boolean} true if the slices are equal
|
|
8
|
-
* @alias module:modeling/
|
|
9
|
+
* @alias module:modeling/slice.equals
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* if (slice.equals(sliceA, sliceB)) { ... }
|
|
9
13
|
*/
|
|
10
14
|
export const equals = (a, b) => {
|
|
11
15
|
if (a.contours.length !== b.contours.length) {
|
|
@@ -8,7 +8,7 @@ import { create } from './create.js'
|
|
|
8
8
|
*
|
|
9
9
|
* @param {object} geometry - the 2D geometry to create a slice from
|
|
10
10
|
* @returns {Slice} a new slice
|
|
11
|
-
* @alias module:modeling/
|
|
11
|
+
* @alias module:modeling/slice.fromGeom2
|
|
12
12
|
*/
|
|
13
13
|
export const fromGeom2 = (geometry) => {
|
|
14
14
|
// Convert from 2D points to 3D vertices
|
|
@@ -3,11 +3,11 @@ import * as vec3 from '../../maths/vec3/index.js'
|
|
|
3
3
|
import { create } from './create.js'
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
|
-
* Create a slice from the given vertices.
|
|
6
|
+
* Create a slice with a single contour from the given vertices.
|
|
7
7
|
*
|
|
8
8
|
* @param {Array} vertices - list of vertices, where each vertex is either 2D or 3D
|
|
9
9
|
* @returns {Slice} a new slice
|
|
10
|
-
* @alias module:modeling/
|
|
10
|
+
* @alias module:modeling/slice.fromVertices
|
|
11
11
|
*
|
|
12
12
|
* @example
|
|
13
13
|
* const vertices = [
|
|
@@ -15,7 +15,7 @@ import { create } from './create.js'
|
|
|
15
15
|
* [0, 10, 3],
|
|
16
16
|
* [0, 10, 6]
|
|
17
17
|
* ]
|
|
18
|
-
* const slice = fromVertices(vertices)
|
|
18
|
+
* const slice = slice.fromVertices(vertices)
|
|
19
19
|
*/
|
|
20
20
|
export const fromVertices = (vertices) => {
|
|
21
21
|
if (!Array.isArray(vertices)) throw new Error('the given vertices must be an array')
|
|
@@ -1,12 +1,27 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Represents a 3D geometry consisting of a list of contours, where each contour consists of a list of planar vertices.
|
|
3
|
-
* @see {@link
|
|
4
|
-
* @module modeling/
|
|
3
|
+
* @see {@link Slice} for data structure information.
|
|
4
|
+
* @module modeling/slice
|
|
5
5
|
*
|
|
6
6
|
* @example
|
|
7
|
-
* import {
|
|
8
|
-
* const slice =
|
|
7
|
+
* import { slice } from '@jscad/modeling'
|
|
8
|
+
* const slice = slice.create([[[0,0,0], [4,0,0], [4,3,12]]])
|
|
9
9
|
*/
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* @typedef Slice
|
|
13
|
+
* @type {Object}
|
|
14
|
+
* @property {Array} contours - list of contours, each contour containing a list of 3D vertices
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* // data structure
|
|
18
|
+
* {
|
|
19
|
+
* contours: [
|
|
20
|
+
* [[0,0,1], [4,0,1], [4,3,1]]
|
|
21
|
+
* ]
|
|
22
|
+
* }
|
|
23
|
+
*/
|
|
24
|
+
|
|
10
25
|
export { calculatePlane } from './calculatePlane.js'
|
|
11
26
|
export { clone } from './clone.js'
|
|
12
27
|
export { create } from './create.js'
|
|
@@ -1,8 +1,12 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Determine if the given object is a slice.
|
|
3
|
+
*
|
|
3
4
|
* @param {Slice} object - the object to interrogate
|
|
4
5
|
* @returns {Boolean} true if the object matches a slice
|
|
5
|
-
* @alias module:modeling/
|
|
6
|
+
* @alias module:modeling/slice.isA
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* if (slice.isA(geometry)) { ... }
|
|
6
10
|
*/
|
|
7
11
|
export const isA = (object) => {
|
|
8
12
|
if (object && typeof object === 'object') {
|
|
@@ -1,11 +1,14 @@
|
|
|
1
1
|
import { create } from './create.js'
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
|
-
* Reverse the
|
|
4
|
+
* Reverse the contours of the given slice.
|
|
5
5
|
*
|
|
6
6
|
* @param {Slice} slice - slice to reverse
|
|
7
7
|
* @returns {Slice} reverse of the slice
|
|
8
|
-
* @alias module:modeling/
|
|
8
|
+
* @alias module:modeling/slice.reverse
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* const newSlice = slice.reverse(oldSlice)
|
|
9
12
|
*/
|
|
10
13
|
export const reverse = (slice) => {
|
|
11
14
|
// reverse each contour
|
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Produces an array of edges from the given slice.
|
|
3
|
-
*
|
|
3
|
+
*
|
|
4
|
+
* NOTE: The returned array should not be modified as the data is shared with the slice.
|
|
5
|
+
*
|
|
4
6
|
* @param {Slice} slice - the slice
|
|
5
7
|
* @returns {Array} an array of edges, each edge contains an array of two vertices (3D)
|
|
6
|
-
* @alias module:modeling/
|
|
8
|
+
* @alias module:modeling/slice.toEdges
|
|
7
9
|
*
|
|
8
10
|
* @example
|
|
9
|
-
* let sharedEdges = toEdges(slice)
|
|
11
|
+
* let sharedEdges = slice.toEdges(slice)
|
|
10
12
|
*/
|
|
11
13
|
export const toEdges = (slice) => {
|
|
12
14
|
const edges = []
|
|
@@ -5,9 +5,13 @@ import { PolygonHierarchy } from './earcut/polygonHierarchy.js'
|
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
7
|
* Return a list of polygons which are enclosed by the slice.
|
|
8
|
+
*
|
|
8
9
|
* @param {Slice} slice - the slice
|
|
9
10
|
* @return {Array} a list of polygons (3D)
|
|
10
|
-
* @alias module:modeling/
|
|
11
|
+
* @alias module:modeling/slice.toPolygons
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* const polygons = slice.toPolygons(sliceA)
|
|
11
15
|
*/
|
|
12
16
|
export const toPolygons = (slice) => {
|
|
13
17
|
const hierarchy = new PolygonHierarchy(slice)
|
|
@@ -2,9 +2,13 @@ import * as vec3 from '../../maths/vec3/index.js'
|
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* Convert the given slice to a readable string.
|
|
5
|
+
*
|
|
5
6
|
* @param {Slice} slice - the slice
|
|
6
7
|
* @return {String} the string representation
|
|
7
|
-
* @alias module:modeling/
|
|
8
|
+
* @alias module:modeling/slice.toString
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* console.log(slice.toString(sliceA))
|
|
8
12
|
*/
|
|
9
13
|
export const toString = (slice) => {
|
|
10
14
|
let result = 'slice (' + slice.contours.length + ' contours):\n[\n'
|
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Produces an array of vertices from the given slice.
|
|
3
|
-
*
|
|
3
|
+
*
|
|
4
|
+
* NOTE: The returned array should not be modified as the data is shared with the slice.
|
|
5
|
+
*
|
|
4
6
|
* @param {Slice} slice - the slice
|
|
5
7
|
* @returns {Array} an array of 3D vertices
|
|
6
|
-
* @alias module:modeling/
|
|
8
|
+
* @alias module:modeling/slice.toVertices
|
|
7
9
|
*
|
|
8
10
|
* @example
|
|
9
|
-
* let sharedVertices = toVertices(slice)
|
|
11
|
+
* let sharedVertices = slice.toVertices(slice)
|
|
10
12
|
*/
|
|
11
13
|
export const toVertices = (slice) => {
|
|
12
14
|
const vertices = []
|
|
@@ -4,14 +4,15 @@ import { create } from './create.js'
|
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
* Transform the given slice using the given matrix.
|
|
7
|
+
*
|
|
7
8
|
* @param {Mat4} matrix - transform matrix
|
|
8
9
|
* @param {Slice} slice - slice to transform
|
|
9
10
|
* @returns {Slice} the transformed slice
|
|
10
|
-
* @alias module:modeling/
|
|
11
|
+
* @alias module:modeling/slice.transform
|
|
11
12
|
*
|
|
12
13
|
* @example
|
|
13
|
-
*
|
|
14
|
-
*
|
|
14
|
+
* const matrix = mat4.fromTranslation([1, 2, 3])
|
|
15
|
+
* const newSlice = slice.transform(matrix, oldSlice)
|
|
15
16
|
*/
|
|
16
17
|
export const transform = (matrix, slice) => {
|
|
17
18
|
const contours = slice.contours.map((contour) => contour.map((vertex) => vec3.transform(vec3.create(), vertex, matrix)))
|