Skip to content

Commit

Permalink
fix(modeling): expose hullPoints2 and hullPoints3
Browse files Browse the repository at this point in the history
  • Loading branch information
z3dev authored Jun 22, 2024
1 parent 9634e5e commit 01ceae8
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 15 deletions.
3 changes: 3 additions & 0 deletions packages/modeling/src/operations/hulls/hullGeom2.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ const toUniquePoints = require('./toUniquePoints')

/*
* Create a convex hull of the given geom2 geometries.
*
* NOTE: The given geometries must be valid geom2 geometries.
*
* @param {...geometries} geometries - list of geom2 geometries
* @returns {geom2} new geometry
*/
Expand Down
17 changes: 6 additions & 11 deletions packages/modeling/src/operations/hulls/hullGeom3.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
const flatten = require('../../utils/flatten')

const geom3 = require('../../geometries/geom3')
const poly3 = require('../../geometries/poly3')

const quickhull = require('./quickhull')
const toUniquePoints = require('./toUniquePoints')
const hullPoints3 = require('./hullPoints3')

/*
* Create a convex hull of the given geometries (geom3).
* Create a convex hull of the given geom3 geometries.
*
* NOTE: The given geometries must be valid geom3 geometries.
*
* @param {...geometries} geometries - list of geom3 geometries
* @returns {geom3} new geometry
*/
Expand All @@ -19,14 +21,7 @@ const hullGeom3 = (...geometries) => {
// extract the unique vertices from the geometries
const unique = toUniquePoints(geometries)

const faces = quickhull(unique, { skipTriangulation: true })

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

return geom3.create(polygons)
return geom3.create(hullPoints3(unique))
}

module.exports = hullGeom3
5 changes: 4 additions & 1 deletion packages/modeling/src/operations/hulls/hullPath2.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ const hullPoints2 = require('./hullPoints2')
const toUniquePoints = require('./toUniquePoints')

/*
* Create a convex hull of the given geometries (path2).
* Create a convex hull of the given path2 geometries.
*
* NOTE: The given geometries must be valid path2 geometry.
*
* @param {...geometries} geometries - list of path2 geometries
* @returns {path2} new geometry
*/
Expand Down
5 changes: 5 additions & 0 deletions packages/modeling/src/operations/hulls/hullPoints2.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import Vec2 from '../../maths/vec2/type'

export default hullPoints2

declare function hullPoints2(uniquePoints: Array<Vec2>): Array<Vec2>
6 changes: 4 additions & 2 deletions packages/modeling/src/operations/hulls/hullPoints2.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
const vec2 = require('../../maths/vec2')

/*
/**
* Create a convex hull of the given set of points, where each point is an array of [x,y].
* Uses https://en.wikipedia.org/wiki/Graham_scan
* @see https://en.wikipedia.org/wiki/Graham_scan
*
* @param {Array} uniquePoints - list of UNIQUE points from which to create a hull
* @returns {Array} a list of points that form the hull
* @alias module:modeling/hulls.hullPoints2
*/
const hullPoints2 = (uniquePoints) => {
// find min point
Expand Down
6 changes: 6 additions & 0 deletions packages/modeling/src/operations/hulls/hullPoints3.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import Vec3 from '../../maths/vec3/type'
import Poly3 from '../../geometries/poly3/type'

export default hullPoints3

declare function hullPoints3(uniquePoints: Array<Vec3>): Array<Poly3>
23 changes: 23 additions & 0 deletions packages/modeling/src/operations/hulls/hullPoints3.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const poly3 = require('../../geometries/poly3')

const quickhull = require('./quickhull')

/**
* Create a convex hull of the given set of points, where each point is an array of [x,y,z].
*
* @param {Array} uniquePoints - list of UNIQUE points from which to create a hull
* @returns {Array} a list of polygons (poly3)
* @alias module:modeling/hulls.hullPoints3
*/
const hullPoints3 = (uniquePoints) => {
const faces = quickhull(uniquePoints, { skipTriangulation: true })

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

return polygons
}

module.exports = hullPoints3
2 changes: 2 additions & 0 deletions packages/modeling/src/operations/hulls/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export { default as hull } from './hull'
export { default as hullChain } from './hullChain'
export { default as hullPoints2 } from './hullPoints2'
export { default as hullPoints3 } from './hullPoints3'

export as namespace hulls
4 changes: 3 additions & 1 deletion packages/modeling/src/operations/hulls/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,7 @@
*/
module.exports = {
hull: require('./hull'),
hullChain: require('./hullChain')
hullChain: require('./hullChain'),
hullPoints2: require('./hullPoints2'),
hullPoints3: require('./hullPoints3')
}

0 comments on commit 01ceae8

Please sign in to comment.