11import * as vec2 from '../../maths/vec2/index.js'
2+ import * as vec3 from '../../maths/vec2/index.js'
23
34import * as geom2 from '../../geometries/geom2/index.js'
45import * as geom3 from '../../geometries/geom3/index.js'
56import * as path2 from '../../geometries/path2/index.js'
7+ import * as path3 from '../../geometries/path3/index.js'
68import * as poly2 from '../../geometries/poly2/index.js'
79
810import { measureEpsilon } from '../../measurements/measureEpsilon.js'
911
1012import { snapPolygons } from './snapPolygons.js'
1113
14+ /*
15+ */
1216const 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+ */
2036const 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+ */
4159const snapGeom3 = ( geometry ) => {
4260 const epsilon = measureEpsilon ( geometry )
4361 const polygons = geom3 . toPolygons ( geometry )
@@ -54,9 +72,10 @@ const snapGeom3 = (geometry) => {
5472 */
5573export 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 } )
0 commit comments