Skip to content

Commit

Permalink
fix(modeling): added geom3.fromPointsConvex()
Browse files Browse the repository at this point in the history
  • Loading branch information
JAG committed Jun 4, 2024
1 parent 5917d67 commit e23a5ab
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 0 deletions.
4 changes: 4 additions & 0 deletions packages/modeling/src/geometries/geom3/fromPointsConvex.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import type { Geom3 } from './type.d.ts'
import type { Vec3 } from '../../maths/vec3/type.d.ts'

export function fromPointsConvex(points: Array<Array<Vec3>>): Geom3
25 changes: 25 additions & 0 deletions packages/modeling/src/geometries/geom3/fromPointsConvex.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { runner } from '../../quickhull/index.js'
import { create } from './create.js'
import * as poly3 from '../poly3/index.js'

/**
* Construct a new convex 3D geometry from a list of unique points.
*
* @param {Array} uniquePoints - list of points to construct convex 3D geometry
* @returns {geom3} a new geometry
* @alias module:modeling/geometries/geom3.fromPointsConvex
*/
export const fromPointsConvex = (uniquePoints) => {
if (!Array.isArray(uniquePoints)) {
throw new Error('the given points must be an array')
}

const faces = runner(uniquePoints, { skipTriangulation: true })

const polygons = faces.map((face) => {
const vertices = face.map((index) => uniquePoints[index])
return poly3.create(vertices)
})

return create(polygons)
}
28 changes: 28 additions & 0 deletions packages/modeling/src/geometries/geom3/fromPointsConvex.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import test from 'ava'

import { fromPointsConvex, validate } from './index.js'

test('fromPointsConvex (uniquePoints)', (t) => {
let out = []
for(x=-9;x<=9;++x)
for(y=-9;y<=9;++y)
for(z=-9;z<=9;++z)
if (x*x+y*y+z*z <= 96)
out.push([x,y,z])

let obs = fromPointsConvex(out)
validate(obs)
t.is(obs.polygons.length, 170)
t.true(obs.polygons.every((f) => ([3,4,8,9].indexOf(f.vertices.length) !== -1)))

let c = [0,0,0,0,0,0,0,0,0,0]
obs.polygons.forEach((f) => c[f.vertices.length]++)
t.is(c[3], 120);
t.is(c[4], 24);
t.is(c[8], 18);
t.is(c[9], 8);

let edges2 = 336*2
obs.polygons.forEach((f) => edges2 -= f.vertices.length)
t.is(edges2, 0);
})
1 change: 1 addition & 0 deletions packages/modeling/src/geometries/geom3/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export { clone } from './clone.js'
export { create } from './create.js'
export { fromPoints } from './fromPoints.js'
export { fromPointsConvex } from './fromPointsConvex.js'
export { fromCompactBinary } from './fromCompactBinary.js'
export { invert } from './invert.js'
export { isA } from './isA.js'
Expand Down
1 change: 1 addition & 0 deletions packages/modeling/src/geometries/geom3/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
export { clone } from './clone.js'
export { create } from './create.js'
export { fromPoints } from './fromPoints.js'
export { fromPointsConvex } from './fromPointsConvex.js'
export { fromCompactBinary } from './fromCompactBinary.js'
export { invert } from './invert.js'
export { isA } from './isA.js'
Expand Down

0 comments on commit e23a5ab

Please sign in to comment.