@jscad/modeling
Version:
Constructive Solid Geometry (CSG) Library for JSCAD
156 lines (132 loc) • 7.54 kB
JavaScript
const test = require('ava')
const { comparePoints, comparePolygonsAsPoints } = require('../../../test/helpers')
const { TAU } = require('../../maths/constants')
const { geom2, geom3, path2 } = require('../../geometries')
const { arc, rectangle, cuboid } = require('../../primitives')
const { snap } = require('./index')
test('snap: snap of a path2 produces an expected path2', (t) => {
const geometry1 = path2.create()
const geometry2 = arc({ radius: 1 / 2, segments: 8 })
const geometry3 = arc({ radius: 1.3333333333333333 / 2, segments: 8 })
const geometry4 = arc({ radius: TAU / 4 * 1000, segments: 8 })
const results = snap(geometry1, geometry2, geometry3, geometry4)
t.is(results.length, 4)
let pts = path2.toPoints(results[0])
let exp = []
t.true(comparePoints(pts, exp))
pts = path2.toPoints(results[1])
exp = [
[ 0.5, 0 ],
[ 0.35355000000000003, 0.35355000000000003 ],
[ 0, 0.5 ],
[ -0.35355000000000003, 0.35355000000000003 ],
[ -0.5, 0 ],
[ -0.35355000000000003, -0.35355000000000003 ],
[ 0, -0.5 ],
[ 0.35355000000000003, -0.35355000000000003 ]
]
t.true(comparePoints(pts, exp))
pts = path2.toPoints(results[2])
exp = [
[ 0.6666666666666666, 0 ],
[ 0.4714, 0.4714 ],
[ 0, 0.6666666666666666 ],
[ -0.4714, 0.4714 ],
[ -0.6666666666666666, 0 ],
[ -0.4714, -0.4714 ],
[ 0, -0.6666666666666666 ],
[ 0.4714, -0.4714 ]
]
t.true(comparePoints(pts, exp))
pts = path2.toPoints(results[3])
exp = [
[ 1570.7963267948967, 0 ],
[ 1110.7100826766714, 1110.7100826766714 ],
[ 0, 1570.7963267948967 ],
[ -1110.7100826766714, 1110.7100826766714 ],
[ -1570.7963267948967, 0 ],
[ -1110.7100826766714, -1110.7100826766714 ],
[ 0, -1570.7963267948967 ],
[ 1110.7100826766714, -1110.7100826766714 ]
]
t.true(comparePoints(pts, exp))
})
test('snap: snap of a geom2 produces an expected geom2', (t) => {
const geometry1 = geom2.create()
const geometry2 = rectangle({ size: [1, 1, 1] })
const geometry3 = rectangle({ size: [1.3333333333333333, 1.3333333333333333, 1.3333333333333333] })
const geometry4 = rectangle({ size: [TAU / 2 * 1000, TAU / 2 * 1000, TAU / 2 * 1000] })
const results = snap(geometry1, geometry2, geometry3, geometry4)
t.is(results.length, 4)
let pts = geom2.toPoints(results[0])
let exp = []
t.true(comparePoints(pts, exp))
pts = geom2.toPoints(results[1])
exp = [[-0.5, -0.5], [0.5, -0.5], [0.5, 0.5], [-0.5, 0.5]]
t.true(comparePoints(pts, exp))
pts = geom2.toPoints(results[2])
exp = [
[-0.6666666666666666, -0.6666666666666666], [0.6666666666666666, -0.6666666666666666],
[0.6666666666666666, 0.6666666666666666], [-0.6666666666666666, 0.6666666666666666]
]
t.true(comparePoints(pts, exp))
pts = geom2.toPoints(results[3])
exp = [
[-1570.7963267948967, -1570.7963267948967], [1570.7963267948967, -1570.7963267948967],
[1570.7963267948967, 1570.7963267948967], [-1570.7963267948967, 1570.7963267948967]
]
t.true(comparePoints(pts, exp))
})
test('snap: snap of a geom3 produces an expected geom3', (t) => {
const geometry1 = geom3.create()
const geometry2 = cuboid({ size: [1, 1, 1] })
const geometry3 = cuboid({ size: [1.3333333333333333, 1.3333333333333333, 1.3333333333333333] })
const geometry4 = cuboid({ size: [TAU / 2 * 1000, TAU / 2 * 1000, TAU / 2 * 1000] })
const results = snap(geometry1, geometry2, geometry3, geometry4)
t.is(results.length, 4)
let pts = geom3.toPoints(results[0])
let exp = []
t.true(comparePolygonsAsPoints(pts, exp))
pts = geom3.toPoints(results[1])
exp = [
[[-0.5, -0.5, -0.5], [-0.5, -0.5, 0.5], [-0.5, 0.5, 0.5], [-0.5, 0.5, -0.5]],
[[0.5, -0.5, -0.5], [0.5, 0.5, -0.5], [0.5, 0.5, 0.5], [0.5, -0.5, 0.5]],
[[-0.5, -0.5, -0.5], [0.5, -0.5, -0.5], [0.5, -0.5, 0.5], [-0.5, -0.5, 0.5]],
[[-0.5, 0.5, -0.5], [-0.5, 0.5, 0.5], [0.5, 0.5, 0.5], [0.5, 0.5, -0.5]],
[[-0.5, -0.5, -0.5], [-0.5, 0.5, -0.5], [0.5, 0.5, -0.5], [0.5, -0.5, -0.5]],
[[-0.5, -0.5, 0.5], [0.5, -0.5, 0.5], [0.5, 0.5, 0.5], [-0.5, 0.5, 0.5]]
]
t.true(comparePolygonsAsPoints(pts, exp))
pts = geom3.toPoints(results[2])
exp = [
[[-0.6666666666666667, -0.6666666666666667, -0.6666666666666667], [-0.6666666666666667, -0.6666666666666667, 0.6666666666666667],
[-0.6666666666666667, 0.6666666666666667, 0.6666666666666667], [-0.6666666666666667, 0.6666666666666667, -0.6666666666666667]],
[[0.6666666666666667, -0.6666666666666667, -0.6666666666666667], [0.6666666666666667, 0.6666666666666667, -0.6666666666666667],
[0.6666666666666667, 0.6666666666666667, 0.6666666666666667], [0.6666666666666667, -0.6666666666666667, 0.6666666666666667]],
[[-0.6666666666666667, -0.6666666666666667, -0.6666666666666667], [0.6666666666666667, -0.6666666666666667, -0.6666666666666667],
[0.6666666666666667, -0.6666666666666667, 0.6666666666666667], [-0.6666666666666667, -0.6666666666666667, 0.6666666666666667]],
[[-0.6666666666666667, 0.6666666666666667, -0.6666666666666667], [-0.6666666666666667, 0.6666666666666667, 0.6666666666666667],
[0.6666666666666667, 0.6666666666666667, 0.6666666666666667], [0.6666666666666667, 0.6666666666666667, -0.6666666666666667]],
[[-0.6666666666666667, -0.6666666666666667, -0.6666666666666667], [-0.6666666666666667, 0.6666666666666667, -0.6666666666666667],
[0.6666666666666667, 0.6666666666666667, -0.6666666666666667], [0.6666666666666667, -0.6666666666666667, -0.6666666666666667]],
[[-0.6666666666666667, -0.6666666666666667, 0.6666666666666667], [0.6666666666666667, -0.6666666666666667, 0.6666666666666667],
[0.6666666666666667, 0.6666666666666667, 0.6666666666666667], [-0.6666666666666667, 0.6666666666666667, 0.6666666666666667]]
]
t.true(comparePolygonsAsPoints(pts, exp))
pts = geom3.toPoints(results[3])
exp = [
[[-1570.7963267948967, -1570.7963267948967, -1570.7963267948967], [-1570.7963267948967, -1570.7963267948967, 1570.7963267948967],
[-1570.7963267948967, 1570.7963267948967, 1570.7963267948967], [-1570.7963267948967, 1570.7963267948967, -1570.7963267948967]],
[[1570.7963267948967, -1570.7963267948967, -1570.7963267948967], [1570.7963267948967, 1570.7963267948967, -1570.7963267948967],
[1570.7963267948967, 1570.7963267948967, 1570.7963267948967], [1570.7963267948967, -1570.7963267948967, 1570.7963267948967]],
[[-1570.7963267948967, -1570.7963267948967, -1570.7963267948967], [1570.7963267948967, -1570.7963267948967, -1570.7963267948967],
[1570.7963267948967, -1570.7963267948967, 1570.7963267948967], [-1570.7963267948967, -1570.7963267948967, 1570.7963267948967]],
[[-1570.7963267948967, 1570.7963267948967, -1570.7963267948967], [-1570.7963267948967, 1570.7963267948967, 1570.7963267948967],
[1570.7963267948967, 1570.7963267948967, 1570.7963267948967], [1570.7963267948967, 1570.7963267948967, -1570.7963267948967]],
[[-1570.7963267948967, -1570.7963267948967, -1570.7963267948967], [-1570.7963267948967, 1570.7963267948967, -1570.7963267948967],
[1570.7963267948967, 1570.7963267948967, -1570.7963267948967], [1570.7963267948967, -1570.7963267948967, -1570.7963267948967]],
[[-1570.7963267948967, -1570.7963267948967, 1570.7963267948967], [1570.7963267948967, -1570.7963267948967, 1570.7963267948967],
[1570.7963267948967, 1570.7963267948967, 1570.7963267948967], [-1570.7963267948967, 1570.7963267948967, 1570.7963267948967]]
]
t.true(comparePolygonsAsPoints(pts, exp))
})