Skip to content

Commit 4af841d

Browse files
committed
fix(modeling): added path3 to operations
1 parent 943ba1d commit 4af841d

7 files changed

Lines changed: 59 additions & 9 deletions

File tree

packages/modeling/src/colors/colorize.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import * as geom2 from '../geometries/geom2/index.js'
22
import * as geom3 from '../geometries/geom3/index.js'
33
import * as path2 from '../geometries/path2/index.js'
4+
import * as path3 from '../geometries/path3/index.js'
5+
import * as slice from '../geometries/slice/index.js'
46
import * as poly3 from '../geometries/poly3/index.js'
57

68
const colorGeom2 = (color, object) => {
@@ -21,6 +23,18 @@ const colorPath2 = (color, object) => {
2123
return newPath2
2224
}
2325

26+
const colorPath3 = (color, object) => {
27+
const newPath3 = path3.clone(object)
28+
newPath3.color = color
29+
return newPath3
30+
}
31+
32+
const colorSlice = (color, object) => {
33+
const newSlice = slice.clone(object)
34+
newSlice.color = color
35+
return newSlice
36+
}
37+
2438
const colorPoly3 = (color, object) => {
2539
const newPoly = poly3.clone(object)
2640
newPoly.color = color
@@ -46,9 +60,11 @@ export const colorize = (color, ...objects) => {
4660
if (color.length === 3) color = [color[0], color[1], color[2], 1.0] // add alpha
4761

4862
const results = objects.map((object) => {
49-
if (geom2.isA(object)) return colorGeom2(color, object)
5063
if (geom3.isA(object)) return colorGeom3(color, object)
64+
if (geom2.isA(object)) return colorGeom2(color, object)
5165
if (path2.isA(object)) return colorPath2(color, object)
66+
if (path3.isA(object)) return colorPath3(color, object)
67+
if (slice.isA(object)) return colorSlice(color, object)
5268
if (poly3.isA(object)) return colorPoly3(color, object)
5369
if (Array.isArray(object)) return colorize(color, ...object)
5470

packages/modeling/src/operations/booleans/union.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export const union = (...geometries) => {
3737
}
3838

3939
const geometry = geometries[0]
40-
// if (path.isA(geometry)) return unionPath(matrix, geometries)
40+
// TODO if (path2.isA(geometry)) return unionPath(geometries)
4141
if (geom2.isA(geometry)) return unionGeom2(geometries)
4242
if (geom3.isA(geometry)) return unionGeom3(geometries)
4343
throw new Error('union unsupported geometry type')

packages/modeling/src/operations/hulls/hull.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,10 @@ export const hull = (...geometries) => {
4242
}
4343

4444
const geometry = geometries[0]
45-
if (path2.isA(geometry)) return hullPath2(geometries)
46-
if (geom2.isA(geometry)) return hullGeom2(geometries)
4745
if (geom3.isA(geometry)) return hullGeom3(geometries)
46+
if (geom2.isA(geometry)) return hullGeom2(geometries)
47+
if (path2.isA(geometry)) return hullPath2(geometries)
48+
// FIXME return geom3? if (path3.isA(geometry)) return hullPath3(geometries)
4849

4950
// FIXME should this throw an error for unknown geometries?
5051
return geometry

packages/modeling/src/operations/hulls/toUniquePoints.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as geom2 from '../../geometries/geom2/index.js'
22
import * as geom3 from '../../geometries/geom3/index.js'
33
import * as path2 from '../../geometries/path2/index.js'
4+
import * as path3 from '../../geometries/path3/index.js'
45

56
/*
67
* Return the unique vertices of a geometry
@@ -25,6 +26,8 @@ export const toUniquePoints = (geometries) => {
2526
geom3.toVertices(geometry).forEach((vertices) => vertices.forEach(addPoint))
2627
} else if (path2.isA(geometry)) {
2728
path2.toPoints(geometry).forEach(addPoint)
29+
} else if (path3.isA(geometry)) {
30+
path3.toVertices(geometry).forEach(addPoint)
2831
}
2932
})
3033

packages/modeling/src/operations/modifiers/generalize.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import { measureEpsilon } from '../../measurements/measureEpsilon.js'
33
import * as geom2 from '../../geometries/geom2/index.js'
44
import * as geom3 from '../../geometries/geom3/index.js'
55
import * as path2 from '../../geometries/path2/index.js'
6+
import * as path3 from '../../geometries/path3/index.js'
7+
import * as slice from '../../geometries/slice/index.js'
68

79
import { snapPolygons } from './snapPolygons.js'
810
import { mergePolygons } from './mergePolygons.js'
@@ -13,6 +15,10 @@ import { triangulatePolygons } from './triangulatePolygons.js'
1315
*/
1416
const generalizePath2 = (options, geometry) => geometry
1517

18+
/*
19+
*/
20+
const generalizePath3 = (options, geometry) => geometry
21+
1622
/*
1723
*/
1824
const generalizeGeom2 = (options, geometry) => geometry
@@ -66,9 +72,10 @@ const generalizeGeom3 = (options, geometry) => {
6672
*/
6773
export const generalize = (options, ...geometries) => {
6874
const results = geometries.map((geometry) => {
69-
if (path2.isA(geometry)) return generalizePath2(options, geometry)
70-
if (geom2.isA(geometry)) return generalizeGeom2(options, geometry)
7175
if (geom3.isA(geometry)) return generalizeGeom3(options, geometry)
76+
if (geom2.isA(geometry)) return generalizeGeom2(options, geometry)
77+
if (path2.isA(geometry)) return generalizePath2(options, geometry)
78+
if (path3.isA(geometry)) return generalizePath3(options, geometry)
7279
if (Array.isArray(geometry)) return generalize(options, ...geometry)
7380
return geometry
7481
})

packages/modeling/src/operations/modifiers/snap.js

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,38 @@
11
import * as vec2 from '../../maths/vec2/index.js'
2+
import * as vec3 from '../../maths/vec2/index.js'
23

34
import * as geom2 from '../../geometries/geom2/index.js'
45
import * as geom3 from '../../geometries/geom3/index.js'
56
import * as path2 from '../../geometries/path2/index.js'
7+
import * as path3 from '../../geometries/path3/index.js'
68
import * as poly2 from '../../geometries/poly2/index.js'
79

810
import { measureEpsilon } from '../../measurements/measureEpsilon.js'
911

1012
import { snapPolygons } from './snapPolygons.js'
1113

14+
/*
15+
*/
1216
const snapPath2 = (geometry) => {
1317
const epsilon = measureEpsilon(geometry)
1418
const points = path2.toPoints(geometry)
1519
const newPoints = points.map((point) => vec2.snap(vec2.create(), point, epsilon))
1620
// snap can produce duplicate points, remove those
17-
return path2.create(newPoints)
21+
return path2.fromPoints({}, newPoints)
22+
}
23+
24+
/*
25+
*/
26+
const snapPath3 = (geometry) => {
27+
const epsilon = measureEpsilon(geometry)
28+
const vertices = path3.toVertices(geometry)
29+
const newVertices = vertices.map((vertice) => vec3.snap(vec3.create(), vertice, epsilon))
30+
// snap can produce duplicate points, remove those
31+
return path3.fromVertices({}, newVertices)
1832
}
1933

34+
/*
35+
*/
2036
const snapGeom2 = (geometry) => {
2137
const epsilon = measureEpsilon(geometry)
2238
const outlines = geom2.toOutlines(geometry)
@@ -38,6 +54,8 @@ const snapGeom2 = (geometry) => {
3854
return geom2.create(newOutlines)
3955
}
4056

57+
/*
58+
*/
4159
const snapGeom3 = (geometry) => {
4260
const epsilon = measureEpsilon(geometry)
4361
const polygons = geom3.toPolygons(geometry)
@@ -54,9 +72,10 @@ const snapGeom3 = (geometry) => {
5472
*/
5573
export const snap = (...geometries) => {
5674
const results = geometries.map((geometry) => {
57-
if (path2.isA(geometry)) return snapPath2(geometry)
58-
if (geom2.isA(geometry)) return snapGeom2(geometry)
5975
if (geom3.isA(geometry)) return snapGeom3(geometry)
76+
if (geom2.isA(geometry)) return snapGeom2(geometry)
77+
if (path2.isA(geometry)) return snapPath2(geometry)
78+
if (path3.isA(geometry)) return snapPath3(geometry)
6079
if (Array.isArray(geometry)) return snap(...geometry)
6180
return geometry
6281
})

packages/modeling/src/utils/areAllShapesTheSameType.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
import * as geom2 from '../geometries/geom2/index.js'
33
import * as geom3 from '../geometries/geom3/index.js'
44
import * as path2 from '../geometries/path2/index.js'
5+
import * as path3 from '../geometries/path3/index.js'
6+
import * as slice from '../geometries/slice/index.js'
57

68
/**
79
* @param {Array} shapes - list of shapes to compare
@@ -15,6 +17,8 @@ export const areAllShapesTheSameType = (shapes) => {
1517
if (geom2.isA(shape)) currentType = 1
1618
if (geom3.isA(shape)) currentType = 2
1719
if (path2.isA(shape)) currentType = 3
20+
if (path3.isA(shape)) currentType = 4
21+
if (slice.isA(shape)) currentType = 5
1822

1923
if (previousType && currentType !== previousType) return false
2024
previousType = currentType

0 commit comments

Comments
 (0)