-
Notifications
You must be signed in to change notification settings - Fork 520
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(modeling): added geom3.fromPointsConvex()
- Loading branch information
JAG
committed
Jun 4, 2024
1 parent
5917d67
commit e23a5ab
Showing
5 changed files
with
59 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
25
packages/modeling/src/geometries/geom3/fromPointsConvex.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
28
packages/modeling/src/geometries/geom3/fromPointsConvex.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters