@jscad/modeling 2.9.1 → 2.9.4
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 +41 -0
- package/README.md +4 -4
- package/dist/jscad-modeling.min.js +414 -420
- package/package.json +3 -2
- package/src/colors/colorize.test.js +1 -1
- package/src/geometries/geom2/toOutlines.js +66 -52
- package/src/geometries/geom3/applyTransforms.js +1 -2
- package/src/geometries/geom3/create.js +1 -1
- package/src/geometries/geom3/create.test.js +1 -1
- package/src/geometries/geom3/fromPoints.js +1 -1
- package/src/geometries/path2/index.d.ts +0 -1
- package/src/geometries/path2/index.js +0 -1
- package/src/geometries/poly3/create.js +1 -1
- package/src/geometries/poly3/validate.js +14 -0
- package/src/maths/constants.d.ts +1 -0
- package/src/maths/constants.js +11 -0
- package/src/maths/mat4/isMirroring.js +11 -11
- package/src/maths/utils/aboutEqualNormals.js +1 -5
- package/src/operations/booleans/intersectGeom2.test.js +69 -0
- package/src/operations/booleans/intersectGeom3.js +2 -1
- package/src/operations/booleans/{intersect.test.js → intersectGeom3.test.js} +3 -71
- package/src/operations/booleans/subtractGeom2.test.js +72 -0
- package/src/operations/booleans/subtractGeom3.js +2 -1
- package/src/operations/booleans/{subtract.test.js → subtractGeom3.test.js} +3 -74
- package/src/operations/booleans/to3DWalls.js +1 -1
- package/src/operations/booleans/unionGeom2.test.js +166 -0
- package/src/operations/booleans/unionGeom3.js +2 -1
- package/src/operations/booleans/{union.test.js → unionGeom3.test.js} +3 -168
- package/src/operations/expansions/expandGeom3.test.js +14 -14
- package/src/operations/expansions/expandShell.js +5 -4
- package/src/operations/expansions/extrudePolygon.js +7 -7
- package/src/operations/extrusions/extrudeFromSlices.js +3 -2
- package/src/operations/extrusions/extrudeFromSlices.test.js +2 -2
- package/src/operations/extrusions/extrudeRotate.js +5 -1
- package/src/operations/extrusions/extrudeRotate.test.js +5 -5
- package/src/operations/extrusions/extrudeWalls.js +2 -2
- package/src/operations/extrusions/project.js +11 -14
- package/src/operations/extrusions/project.test.js +50 -50
- package/src/operations/extrusions/slice/repair.js +62 -0
- package/src/operations/hulls/hullChain.test.js +4 -4
- package/src/operations/hulls/hullGeom2.js +6 -18
- package/src/operations/hulls/hullGeom3.js +5 -18
- package/src/operations/hulls/hullPath2.js +4 -14
- package/src/operations/hulls/hullPoints2.js +43 -92
- package/src/operations/hulls/toUniquePoints.js +34 -0
- package/src/operations/modifiers/generalize.js +2 -13
- package/src/operations/modifiers/generalize.test.js +0 -32
- package/src/operations/modifiers/insertTjunctions.js +1 -1
- package/src/operations/modifiers/insertTjunctions.test.js +21 -21
- package/src/operations/modifiers/mergePolygons.js +11 -14
- package/src/operations/{booleans → modifiers}/reTesselateCoplanarPolygons.js +33 -32
- package/src/operations/{booleans → modifiers}/reTesselateCoplanarPolygons.test.js +5 -5
- package/src/operations/{booleans → modifiers}/retessellate.js +2 -9
- package/src/operations/{booleans → modifiers}/retessellate.test.js +0 -0
- package/src/operations/modifiers/snapPolygons.test.js +12 -12
- package/src/operations/modifiers/triangulatePolygons.js +3 -3
- package/src/operations/transforms/center.js +1 -1
- package/src/primitives/cuboid.js +1 -1
- package/src/primitives/cylinderElliptic.js +1 -1
- package/src/primitives/ellipsoid.js +2 -2
- package/src/primitives/polyhedron.js +1 -1
- package/src/primitives/roundedCuboid.js +6 -6
- package/src/primitives/roundedCylinder.js +1 -1
- package/src/primitives/torus.test.js +6 -6
- package/src/primitives/triangle.js +1 -2
- package/src/utils/trigonometry.js +1 -2
- package/src/geometries/path2/eachPoint.d.ts +0 -9
- package/src/geometries/path2/eachPoint.js +0 -17
- package/src/geometries/path2/eachPoint.test.js +0 -11
- package/src/operations/extrusions/slice/repairSlice.js +0 -47
- package/src/operations/modifiers/edges.js +0 -195
- package/src/operations/modifiers/repairTjunctions.js +0 -44
package/package.json
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jscad/modeling",
|
|
3
|
-
"version": "2.9.
|
|
3
|
+
"version": "2.9.4",
|
|
4
4
|
"description": "Constructive Solid Geometry (CSG) Library for JSCAD",
|
|
5
|
+
"homepage": "https://openjscad.xyz/",
|
|
5
6
|
"repository": "https://github.com/jscad/OpenJSCAD.org",
|
|
6
7
|
"main": "src/index.js",
|
|
7
8
|
"types": "src/index.d.ts",
|
|
@@ -60,5 +61,5 @@
|
|
|
60
61
|
"nyc": "15.1.0",
|
|
61
62
|
"uglifyify": "5.0.2"
|
|
62
63
|
},
|
|
63
|
-
"gitHead": "
|
|
64
|
+
"gitHead": "662965f40a1ce628aa97f30b814586e72a3acb36"
|
|
64
65
|
}
|
|
@@ -38,7 +38,7 @@ test('color (rgba on geometry)', (t) => {
|
|
|
38
38
|
const obj0 = geom2.fromPoints([[0, 0], [1, 0], [0, 1]])
|
|
39
39
|
const obj1 = geom3.fromPoints([[[0, 0, 0], [1, 0, 0], [1, 0, 1]]])
|
|
40
40
|
const obj2 = path2.fromPoints({ closed: true }, [[0, 0], [1, 0], [1, 1]])
|
|
41
|
-
const obj3 = poly3.
|
|
41
|
+
const obj3 = poly3.create([[0, 0, 0], [1, 0, 0], [1, 1, 0]])
|
|
42
42
|
|
|
43
43
|
const obs = colorize([1, 1, 0.5, 0.8], obj0, obj1, obj2, obj3)
|
|
44
44
|
t.is(obs.length, 4)
|
|
@@ -6,22 +6,42 @@ const toSides = require('./toSides')
|
|
|
6
6
|
* Create a list of edges which SHARE vertices.
|
|
7
7
|
* This allows the edges to be traversed in order.
|
|
8
8
|
*/
|
|
9
|
-
const
|
|
10
|
-
const
|
|
9
|
+
const toSharedVertices = (sides) => {
|
|
10
|
+
const unique = new Map() // {key: vertex}
|
|
11
11
|
const getUniqueVertex = (vertex) => {
|
|
12
12
|
const key = vertex.toString()
|
|
13
|
-
if (
|
|
14
|
-
|
|
13
|
+
if (unique.has(key)) {
|
|
14
|
+
return unique.get(key)
|
|
15
|
+
} else {
|
|
16
|
+
unique.set(key, vertex)
|
|
17
|
+
return vertex
|
|
15
18
|
}
|
|
16
|
-
return vertices[key]
|
|
17
19
|
}
|
|
18
20
|
|
|
19
21
|
return sides.map((side) => side.map(getUniqueVertex))
|
|
20
22
|
}
|
|
21
23
|
|
|
24
|
+
/*
|
|
25
|
+
* Convert a list of sides into a map from vertex to edges.
|
|
26
|
+
*/
|
|
27
|
+
const toVertexMap = (sides) => {
|
|
28
|
+
const vertexMap = new Map()
|
|
29
|
+
// first map to edges with shared vertices
|
|
30
|
+
const edges = toSharedVertices(sides)
|
|
31
|
+
// construct adjacent edges map
|
|
32
|
+
edges.forEach((edge) => {
|
|
33
|
+
if (vertexMap.has(edge[0])) {
|
|
34
|
+
vertexMap.get(edge[0]).push(edge)
|
|
35
|
+
} else {
|
|
36
|
+
vertexMap.set(edge[0], [edge])
|
|
37
|
+
}
|
|
38
|
+
})
|
|
39
|
+
return vertexMap
|
|
40
|
+
}
|
|
41
|
+
|
|
22
42
|
/**
|
|
23
43
|
* Create the outline(s) of the given geometry.
|
|
24
|
-
* @param
|
|
44
|
+
* @param {geom2} geometry - geometry to create outlines from
|
|
25
45
|
* @returns {Array} an array of outlines, where each outline is an array of ordered points
|
|
26
46
|
* @alias module:modeling/geometries/geom2.toOutlines
|
|
27
47
|
*
|
|
@@ -30,65 +50,35 @@ const toEdges = (sides) => {
|
|
|
30
50
|
* let outlines = toOutlines(geometry) // returns two outlines
|
|
31
51
|
*/
|
|
32
52
|
const toOutlines = (geometry) => {
|
|
33
|
-
const vertexMap =
|
|
34
|
-
const edges = toEdges(toSides(geometry))
|
|
35
|
-
edges.forEach((edge) => {
|
|
36
|
-
if (!(vertexMap.has(edge[0]))) {
|
|
37
|
-
vertexMap.set(edge[0], [])
|
|
38
|
-
}
|
|
39
|
-
const sideslist = vertexMap.get(edge[0])
|
|
40
|
-
sideslist.push(edge)
|
|
41
|
-
})
|
|
42
|
-
|
|
53
|
+
const vertexMap = toVertexMap(toSides(geometry)) // {vertex: [edges]}
|
|
43
54
|
const outlines = []
|
|
44
55
|
while (true) {
|
|
45
|
-
let
|
|
56
|
+
let startSide
|
|
46
57
|
for (const [vertex, edges] of vertexMap) {
|
|
47
|
-
|
|
48
|
-
if (!
|
|
58
|
+
startSide = edges.shift()
|
|
59
|
+
if (!startSide) {
|
|
49
60
|
vertexMap.delete(vertex)
|
|
50
61
|
continue
|
|
51
62
|
}
|
|
52
63
|
break
|
|
53
64
|
}
|
|
54
|
-
if (
|
|
65
|
+
if (startSide === undefined) break // all starting sides have been visited
|
|
55
66
|
|
|
56
67
|
const connectedVertexPoints = []
|
|
57
|
-
const
|
|
58
|
-
const v0 = vec2.create()
|
|
68
|
+
const startVertex = startSide[0]
|
|
59
69
|
while (true) {
|
|
60
|
-
connectedVertexPoints.push(
|
|
61
|
-
const
|
|
62
|
-
if (
|
|
63
|
-
const
|
|
64
|
-
if (!
|
|
65
|
-
throw new Error(
|
|
70
|
+
connectedVertexPoints.push(startSide[0])
|
|
71
|
+
const nextVertex = startSide[1]
|
|
72
|
+
if (nextVertex === startVertex) break // the outline has been closed
|
|
73
|
+
const nextPossibleSides = vertexMap.get(nextVertex)
|
|
74
|
+
if (!nextPossibleSides) {
|
|
75
|
+
throw new Error(`geometry is not closed at vertex ${nextVertex}`)
|
|
66
76
|
}
|
|
67
|
-
|
|
68
|
-
if (
|
|
69
|
-
|
|
70
|
-
} else {
|
|
71
|
-
// more than one side starting at the same vertex
|
|
72
|
-
let bestangle
|
|
73
|
-
const startangle = vec2.angleDegrees(vec2.subtract(v0, startside[1], startside[0]))
|
|
74
|
-
for (let sideindex = 0; sideindex < nextpossiblesides.length; sideindex++) {
|
|
75
|
-
const nextpossibleside = nextpossiblesides[sideindex]
|
|
76
|
-
const nextangle = vec2.angleDegrees(vec2.subtract(v0, nextpossibleside[1], nextpossibleside[0]))
|
|
77
|
-
let angledif = nextangle - startangle
|
|
78
|
-
if (angledif < -180) angledif += 360
|
|
79
|
-
if (angledif >= 180) angledif -= 360
|
|
80
|
-
if ((nextsideindex < 0) || (angledif > bestangle)) {
|
|
81
|
-
nextsideindex = sideindex
|
|
82
|
-
bestangle = angledif
|
|
83
|
-
}
|
|
84
|
-
}
|
|
77
|
+
const nextSide = popNextSide(startSide, nextPossibleSides)
|
|
78
|
+
if (nextPossibleSides.length === 0) {
|
|
79
|
+
vertexMap.delete(nextVertex)
|
|
85
80
|
}
|
|
86
|
-
|
|
87
|
-
nextpossiblesides.splice(nextsideindex, 1) // remove side from list
|
|
88
|
-
if (nextpossiblesides.length === 0) {
|
|
89
|
-
vertexMap.delete(nextvertex)
|
|
90
|
-
}
|
|
91
|
-
startside = nextside
|
|
81
|
+
startSide = nextSide
|
|
92
82
|
} // inner loop
|
|
93
83
|
|
|
94
84
|
// due to the logic of fromPoints()
|
|
@@ -102,4 +92,28 @@ const toOutlines = (geometry) => {
|
|
|
102
92
|
return outlines
|
|
103
93
|
}
|
|
104
94
|
|
|
95
|
+
// find the first counter-clockwise edge from startSide and pop from nextSides
|
|
96
|
+
const popNextSide = (startSide, nextSides) => {
|
|
97
|
+
if (nextSides.length === 1) {
|
|
98
|
+
return nextSides.pop()
|
|
99
|
+
}
|
|
100
|
+
const v0 = vec2.create()
|
|
101
|
+
const startAngle = vec2.angleDegrees(vec2.subtract(v0, startSide[1], startSide[0]))
|
|
102
|
+
let bestAngle
|
|
103
|
+
let bestIndex
|
|
104
|
+
nextSides.forEach((nextSide, index) => {
|
|
105
|
+
const nextAngle = vec2.angleDegrees(vec2.subtract(v0, nextSide[1], nextSide[0]))
|
|
106
|
+
let angle = nextAngle - startAngle
|
|
107
|
+
if (angle < -180) angle += 360
|
|
108
|
+
if (angle >= 180) angle -= 360
|
|
109
|
+
if (bestIndex === undefined || angle > bestAngle) {
|
|
110
|
+
bestIndex = index
|
|
111
|
+
bestAngle = angle
|
|
112
|
+
}
|
|
113
|
+
})
|
|
114
|
+
const nextSide = nextSides[bestIndex]
|
|
115
|
+
nextSides.splice(bestIndex, 1) // remove side from list
|
|
116
|
+
return nextSide
|
|
117
|
+
}
|
|
118
|
+
|
|
105
119
|
module.exports = toOutlines
|
|
@@ -14,9 +14,8 @@ const applyTransforms = (geometry) => {
|
|
|
14
14
|
if (mat4.isIdentity(geometry.transforms)) return geometry
|
|
15
15
|
|
|
16
16
|
// apply transforms to each polygon
|
|
17
|
-
// const isMirror = mat4.isMirroring(geometry.transforms)
|
|
18
|
-
// TBD if (isMirror) newvertices.reverse()
|
|
19
17
|
geometry.polygons = geometry.polygons.map((polygon) => poly3.transform(geometry.transforms, polygon))
|
|
18
|
+
// reset transforms
|
|
20
19
|
geometry.transforms = mat4.create()
|
|
21
20
|
return geometry
|
|
22
21
|
}
|
|
@@ -14,7 +14,7 @@ test('create: Creates an empty geom3', (t) => {
|
|
|
14
14
|
|
|
15
15
|
test('create: Creates a populated geom3', (t) => {
|
|
16
16
|
const points = [[0, 0, 0], [0, 10, 0], [0, 10, 10]]
|
|
17
|
-
const polygon = poly3.
|
|
17
|
+
const polygon = poly3.create(points)
|
|
18
18
|
|
|
19
19
|
const polygons = [polygon]
|
|
20
20
|
const expected = {
|
|
@@ -18,7 +18,7 @@ const fromPoints = (listofpoints) => {
|
|
|
18
18
|
|
|
19
19
|
const polygons = listofpoints.map((points, index) => {
|
|
20
20
|
// TODO catch the error, and rethrow with index
|
|
21
|
-
const polygon = poly3.
|
|
21
|
+
const polygon = poly3.create(points)
|
|
22
22
|
return polygon
|
|
23
23
|
})
|
|
24
24
|
const result = create(polygons)
|
|
@@ -5,7 +5,6 @@ export { default as clone } from './clone'
|
|
|
5
5
|
export { default as close } from './close'
|
|
6
6
|
export { default as concat } from './concat'
|
|
7
7
|
export { default as create } from './create'
|
|
8
|
-
export { default as eachPoint, EachPointOptions } from './eachPoint'
|
|
9
8
|
export { default as equals } from './equals'
|
|
10
9
|
export { default as fromPoints, FromPointsOptions } from './fromPoints'
|
|
11
10
|
export { default as fromCompactBinary } from './fromCompactBinary'
|
|
@@ -22,7 +22,6 @@ module.exports = {
|
|
|
22
22
|
close: require('./close'),
|
|
23
23
|
concat: require('./concat'),
|
|
24
24
|
create: require('./create'),
|
|
25
|
-
eachPoint: require('./eachPoint'),
|
|
26
25
|
equals: require('./equals'),
|
|
27
26
|
fromPoints: require('./fromPoints'),
|
|
28
27
|
fromCompactBinary: require('./fromCompactBinary'),
|
|
@@ -1,7 +1,10 @@
|
|
|
1
|
+
const signedDistanceToPoint = require('../../maths/plane/signedDistanceToPoint')
|
|
2
|
+
const { NEPS } = require('../../maths/constants')
|
|
1
3
|
const vec3 = require('../../maths/vec3')
|
|
2
4
|
const isA = require('./isA')
|
|
3
5
|
const isConvex = require('./isConvex')
|
|
4
6
|
const measureArea = require('./measureArea')
|
|
7
|
+
const plane = require('./plane')
|
|
5
8
|
|
|
6
9
|
/**
|
|
7
10
|
* Determine if the given object is a valid polygon.
|
|
@@ -45,6 +48,17 @@ const validate = (object) => {
|
|
|
45
48
|
throw new Error(`poly3 invalid vertex ${vertex}`)
|
|
46
49
|
}
|
|
47
50
|
})
|
|
51
|
+
|
|
52
|
+
// check that points are co-planar
|
|
53
|
+
if (object.vertices.length > 3) {
|
|
54
|
+
const normal = plane(object)
|
|
55
|
+
object.vertices.forEach((vertex) => {
|
|
56
|
+
const dist = Math.abs(signedDistanceToPoint(normal, vertex))
|
|
57
|
+
if (dist > NEPS) {
|
|
58
|
+
throw new Error(`poly3 must be coplanar: vertex ${vertex} distance ${dist}`)
|
|
59
|
+
}
|
|
60
|
+
})
|
|
61
|
+
}
|
|
48
62
|
}
|
|
49
63
|
|
|
50
64
|
module.exports = validate
|
package/src/maths/constants.d.ts
CHANGED
package/src/maths/constants.js
CHANGED
|
@@ -14,7 +14,18 @@ const spatialResolution = 1e5
|
|
|
14
14
|
*/
|
|
15
15
|
const EPS = 1e-5
|
|
16
16
|
|
|
17
|
+
/**
|
|
18
|
+
* Smaller epsilon used for measuring near zero distances.
|
|
19
|
+
* @default
|
|
20
|
+
* @alias module:modeling/maths.NEPS
|
|
21
|
+
*/
|
|
22
|
+
const NEPS = 1e-13
|
|
23
|
+
// NEPS is derived from a series of tests to determine the optimal precision
|
|
24
|
+
// for comparing coplanar polygons, as provided by the sphere primitive at high
|
|
25
|
+
// segmentation. NEPS is for 64 bit Number values.
|
|
26
|
+
|
|
17
27
|
module.exports = {
|
|
18
28
|
EPS,
|
|
29
|
+
NEPS,
|
|
19
30
|
spatialResolution
|
|
20
31
|
}
|
|
@@ -1,7 +1,3 @@
|
|
|
1
|
-
const cross = require('../vec3/cross')
|
|
2
|
-
const dot = require('../vec3/dot')
|
|
3
|
-
const fromValues = require('../vec3/fromValues')
|
|
4
|
-
|
|
5
1
|
/**
|
|
6
2
|
* Determine whether the given matrix is a mirroring transformation.
|
|
7
3
|
*
|
|
@@ -10,15 +6,19 @@ const fromValues = require('../vec3/fromValues')
|
|
|
10
6
|
* @alias module:modeling/maths/mat4.isMirroring
|
|
11
7
|
*/
|
|
12
8
|
const isMirroring = (matrix) => {
|
|
13
|
-
const
|
|
14
|
-
const
|
|
15
|
-
const
|
|
9
|
+
// const xVector = [matrix[0], matrix[4], matrix[8]]
|
|
10
|
+
// const yVector = [matrix[1], matrix[5], matrix[9]]
|
|
11
|
+
// const zVector = [matrix[2], matrix[6], matrix[10]]
|
|
16
12
|
|
|
17
|
-
// for a true orthogonal, non-mirrored base,
|
|
13
|
+
// for a true orthogonal, non-mirrored base, xVector.cross(yVector) == zVector
|
|
18
14
|
// If they have an opposite direction then we are mirroring
|
|
19
|
-
|
|
20
|
-
const
|
|
21
|
-
|
|
15
|
+
// calcuate xVector.cross(yVector)
|
|
16
|
+
const x = matrix[4] * matrix[9] - matrix[8] * matrix[5]
|
|
17
|
+
const y = matrix[8] * matrix[1] - matrix[0] * matrix[9]
|
|
18
|
+
const z = matrix[0] * matrix[5] - matrix[4] * matrix[1]
|
|
19
|
+
// calcualte dot(cross, zVector)
|
|
20
|
+
const d = x * matrix[2] + y * matrix[6] + z * matrix[10]
|
|
21
|
+
return (d < 0)
|
|
22
22
|
}
|
|
23
23
|
|
|
24
24
|
module.exports = isMirroring
|
|
@@ -1,8 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
// This EPS is derived from a series of tests to determine the optimal precision for comparing coplanar polygons,
|
|
3
|
-
// as provided by the sphere primitive at high segmentation
|
|
4
|
-
// This EPS is for 64 bit Number values
|
|
5
|
-
const NEPS = 1e-13
|
|
1
|
+
const { NEPS } = require('../constants')
|
|
6
2
|
|
|
7
3
|
/**
|
|
8
4
|
* Compare two normals (unit vectors) for near equality.
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
const test = require('ava')
|
|
2
|
+
|
|
3
|
+
const { comparePoints } = require('../../../test/helpers')
|
|
4
|
+
|
|
5
|
+
const { geom2 } = require('../../geometries')
|
|
6
|
+
|
|
7
|
+
const { circle, rectangle } = require('../../primitives')
|
|
8
|
+
|
|
9
|
+
const { intersect } = require('./index')
|
|
10
|
+
|
|
11
|
+
const { center } = require('../transforms/center')
|
|
12
|
+
|
|
13
|
+
test('intersect: intersect of one or more geom2 objects produces expected geometry', (t) => {
|
|
14
|
+
const geometry1 = circle({ radius: 2, segments: 8 })
|
|
15
|
+
|
|
16
|
+
// intersect of one object
|
|
17
|
+
const result1 = intersect(geometry1)
|
|
18
|
+
let obs = geom2.toPoints(result1)
|
|
19
|
+
let exp = [
|
|
20
|
+
[2, 0],
|
|
21
|
+
[1.4142000000000001, 1.4142000000000001],
|
|
22
|
+
[0, 2],
|
|
23
|
+
[-1.4142000000000001, 1.4142000000000001],
|
|
24
|
+
[-2, 0],
|
|
25
|
+
[-1.4142000000000001, -1.4142000000000001],
|
|
26
|
+
[0, -2],
|
|
27
|
+
[1.4142000000000001, -1.4142000000000001]
|
|
28
|
+
]
|
|
29
|
+
t.notThrows(() => geom2.validate(result1))
|
|
30
|
+
t.is(obs.length, 8)
|
|
31
|
+
t.true(comparePoints(obs, exp))
|
|
32
|
+
|
|
33
|
+
// intersect of two non-overlapping objects
|
|
34
|
+
const geometry2 = center({ relativeTo: [10, 10, 0] }, rectangle({ size: [4, 4] }))
|
|
35
|
+
|
|
36
|
+
const result2 = intersect(geometry1, geometry2)
|
|
37
|
+
obs = geom2.toPoints(result2)
|
|
38
|
+
t.notThrows(() => geom2.validate(result2))
|
|
39
|
+
t.is(obs.length, 0)
|
|
40
|
+
|
|
41
|
+
// intersect of two partially overlapping objects
|
|
42
|
+
const geometry3 = rectangle({ size: [18, 18] })
|
|
43
|
+
|
|
44
|
+
const result3 = intersect(geometry2, geometry3)
|
|
45
|
+
obs = geom2.toPoints(result3)
|
|
46
|
+
exp = [
|
|
47
|
+
[9, 9], [8, 9], [8, 8], [9, 8]
|
|
48
|
+
]
|
|
49
|
+
t.notThrows(() => geom2.validate(result3))
|
|
50
|
+
t.is(obs.length, 4)
|
|
51
|
+
t.true(comparePoints(obs, exp))
|
|
52
|
+
|
|
53
|
+
// intersect of two completely overlapping objects
|
|
54
|
+
const result4 = intersect(geometry1, geometry3)
|
|
55
|
+
obs = geom2.toPoints(result4)
|
|
56
|
+
exp = [
|
|
57
|
+
[2, 0],
|
|
58
|
+
[1.4142000000000001, 1.4142000000000001],
|
|
59
|
+
[0, 2],
|
|
60
|
+
[-1.4142000000000001, 1.4142000000000001],
|
|
61
|
+
[-2, 0],
|
|
62
|
+
[-1.4142000000000001, -1.4142000000000001],
|
|
63
|
+
[0, -2],
|
|
64
|
+
[1.4142000000000001, -1.4142000000000001]
|
|
65
|
+
]
|
|
66
|
+
t.notThrows(() => geom2.validate(result4))
|
|
67
|
+
t.is(obs.length, 8)
|
|
68
|
+
t.true(comparePoints(obs, exp))
|
|
69
|
+
})
|
|
@@ -1,83 +1,15 @@
|
|
|
1
1
|
const test = require('ava')
|
|
2
2
|
|
|
3
|
-
const { comparePolygonsAsPoints
|
|
3
|
+
const { comparePolygonsAsPoints } = require('../../../test/helpers')
|
|
4
4
|
|
|
5
|
-
const {
|
|
5
|
+
const { geom3 } = require('../../geometries')
|
|
6
6
|
|
|
7
|
-
const {
|
|
7
|
+
const { sphere, cuboid } = require('../../primitives')
|
|
8
8
|
|
|
9
9
|
const { intersect } = require('./index')
|
|
10
10
|
|
|
11
11
|
const { center } = require('../transforms/center')
|
|
12
12
|
|
|
13
|
-
// test('intersect: intersect of a path produces expected changes to points', (t) => {
|
|
14
|
-
// let geometry = path.fromPoints({}, [[0, 1, 0], [1, 0, 0]])
|
|
15
|
-
//
|
|
16
|
-
// geometry = intersect({normal: [1, 0, 0]}, geometry)
|
|
17
|
-
// let obs = path.toPoints(geometry)
|
|
18
|
-
// let exp = []
|
|
19
|
-
//
|
|
20
|
-
// t.deepEqual(obs, exp)
|
|
21
|
-
// })
|
|
22
|
-
|
|
23
|
-
test('intersect: intersect of one or more geom2 objects produces expected geometry', (t) => {
|
|
24
|
-
const geometry1 = circle({ radius: 2, segments: 8 })
|
|
25
|
-
|
|
26
|
-
// intersect of one object
|
|
27
|
-
const result1 = intersect(geometry1)
|
|
28
|
-
let obs = geom2.toPoints(result1)
|
|
29
|
-
let exp = [
|
|
30
|
-
[2, 0],
|
|
31
|
-
[1.4142000000000001, 1.4142000000000001],
|
|
32
|
-
[0, 2],
|
|
33
|
-
[-1.4142000000000001, 1.4142000000000001],
|
|
34
|
-
[-2, 0],
|
|
35
|
-
[-1.4142000000000001, -1.4142000000000001],
|
|
36
|
-
[0, -2],
|
|
37
|
-
[1.4142000000000001, -1.4142000000000001]
|
|
38
|
-
]
|
|
39
|
-
t.notThrows(() => geom2.validate(result1))
|
|
40
|
-
t.is(obs.length, 8)
|
|
41
|
-
t.true(comparePoints(obs, exp))
|
|
42
|
-
|
|
43
|
-
// intersect of two non-overlapping objects
|
|
44
|
-
const geometry2 = center({ relativeTo: [10, 10, 0] }, rectangle({ size: [4, 4] }))
|
|
45
|
-
|
|
46
|
-
const result2 = intersect(geometry1, geometry2)
|
|
47
|
-
obs = geom2.toPoints(result2)
|
|
48
|
-
t.notThrows(() => geom2.validate(result2))
|
|
49
|
-
t.is(obs.length, 0)
|
|
50
|
-
|
|
51
|
-
// intersect of two partially overlapping objects
|
|
52
|
-
const geometry3 = rectangle({ size: [18, 18] })
|
|
53
|
-
|
|
54
|
-
const result3 = intersect(geometry2, geometry3)
|
|
55
|
-
obs = geom2.toPoints(result3)
|
|
56
|
-
exp = [
|
|
57
|
-
[9, 9], [8, 9], [8, 8], [9, 8]
|
|
58
|
-
]
|
|
59
|
-
t.notThrows(() => geom2.validate(result3))
|
|
60
|
-
t.is(obs.length, 4)
|
|
61
|
-
t.true(comparePoints(obs, exp))
|
|
62
|
-
|
|
63
|
-
// intersect of two completely overlapping objects
|
|
64
|
-
const result4 = intersect(geometry1, geometry3)
|
|
65
|
-
obs = geom2.toPoints(result4)
|
|
66
|
-
exp = [
|
|
67
|
-
[2, 0],
|
|
68
|
-
[1.4142000000000001, 1.4142000000000001],
|
|
69
|
-
[0, 2],
|
|
70
|
-
[-1.4142000000000001, 1.4142000000000001],
|
|
71
|
-
[-2, 0],
|
|
72
|
-
[-1.4142000000000001, -1.4142000000000001],
|
|
73
|
-
[0, -2],
|
|
74
|
-
[1.4142000000000001, -1.4142000000000001]
|
|
75
|
-
]
|
|
76
|
-
t.notThrows(() => geom2.validate(result4))
|
|
77
|
-
t.is(obs.length, 8)
|
|
78
|
-
t.true(comparePoints(obs, exp))
|
|
79
|
-
})
|
|
80
|
-
|
|
81
13
|
test('intersect: intersect of one or more geom3 objects produces expected geometry', (t) => {
|
|
82
14
|
const geometry1 = sphere({ radius: 2, segments: 8 })
|
|
83
15
|
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
const test = require('ava')
|
|
2
|
+
|
|
3
|
+
const { comparePoints } = require('../../../test/helpers')
|
|
4
|
+
|
|
5
|
+
const { geom2 } = require('../../geometries')
|
|
6
|
+
|
|
7
|
+
const { circle, rectangle } = require('../../primitives')
|
|
8
|
+
|
|
9
|
+
const { subtract } = require('./index')
|
|
10
|
+
|
|
11
|
+
const { center } = require('../transforms/center')
|
|
12
|
+
|
|
13
|
+
test('subtract: subtract of one or more geom2 objects produces expected geometry', (t) => {
|
|
14
|
+
const geometry1 = circle({ radius: 2, segments: 8 })
|
|
15
|
+
|
|
16
|
+
// subtract of one object
|
|
17
|
+
const result1 = subtract(geometry1)
|
|
18
|
+
let obs = geom2.toPoints(result1)
|
|
19
|
+
let exp = [
|
|
20
|
+
[2, 0],
|
|
21
|
+
[1.4142000000000001, 1.4142000000000001],
|
|
22
|
+
[0, 2],
|
|
23
|
+
[-1.4142000000000001, 1.4142000000000001],
|
|
24
|
+
[-2, 0],
|
|
25
|
+
[-1.4142000000000001, -1.4142000000000001],
|
|
26
|
+
[0, -2],
|
|
27
|
+
[1.4142000000000001, -1.4142000000000001]
|
|
28
|
+
]
|
|
29
|
+
t.notThrows(() => geom2.validate(result1))
|
|
30
|
+
t.is(obs.length, 8)
|
|
31
|
+
t.true(comparePoints(obs, exp))
|
|
32
|
+
|
|
33
|
+
// subtract of two non-overlapping objects
|
|
34
|
+
const geometry2 = center({ relativeTo: [10, 10, 0] }, rectangle({ size: [4, 4] }))
|
|
35
|
+
|
|
36
|
+
const result2 = subtract(geometry1, geometry2)
|
|
37
|
+
obs = geom2.toPoints(result2)
|
|
38
|
+
exp = [
|
|
39
|
+
[2, 0],
|
|
40
|
+
[1.4142000000000001, 1.4142000000000001],
|
|
41
|
+
[0, 2],
|
|
42
|
+
[-1.4142000000000001, 1.4142000000000001],
|
|
43
|
+
[-2, 0],
|
|
44
|
+
[-1.4142000000000001, -1.4142000000000001],
|
|
45
|
+
[0, -2],
|
|
46
|
+
[1.4142000000000001, -1.4142000000000001]
|
|
47
|
+
]
|
|
48
|
+
t.notThrows(() => geom2.validate(result2))
|
|
49
|
+
t.is(obs.length, 8)
|
|
50
|
+
t.true(comparePoints(obs, exp))
|
|
51
|
+
|
|
52
|
+
// subtract of two partially overlapping objects
|
|
53
|
+
const geometry3 = rectangle({ size: [18, 18] })
|
|
54
|
+
|
|
55
|
+
const result3 = subtract(geometry2, geometry3)
|
|
56
|
+
obs = geom2.toPoints(result3)
|
|
57
|
+
exp = [
|
|
58
|
+
[12, 12], [9, 9], [8, 9], [8, 12], [9, 8], [12, 8]
|
|
59
|
+
]
|
|
60
|
+
t.notThrows(() => geom2.validate(result3))
|
|
61
|
+
t.is(obs.length, 6)
|
|
62
|
+
t.true(comparePoints(obs, exp))
|
|
63
|
+
|
|
64
|
+
// subtract of two completely overlapping objects
|
|
65
|
+
const result4 = subtract(geometry1, geometry3)
|
|
66
|
+
obs = geom2.toPoints(result4)
|
|
67
|
+
exp = [
|
|
68
|
+
]
|
|
69
|
+
t.notThrows(() => geom2.validate(result4))
|
|
70
|
+
t.is(obs.length, 0)
|
|
71
|
+
t.deepEqual(obs, exp)
|
|
72
|
+
})
|