-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(tools/mesh): create categorizeVertices mesh tool
- Loading branch information
1 parent
7e4a187
commit c25e0ae
Showing
3 changed files
with
42 additions
and
38 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,39 @@ | ||
import { BufferGeometry } from 'three' | ||
import { Vector3 } from '@src/Vector3.js' | ||
import { countBy, partition } from '@src/faux-ramda.js' | ||
import { getNonIndexedVertices } from '@tools/mesh/getVertices.js' | ||
|
||
type HashAndAmount = [string, number] | ||
|
||
const unpackCoords = (coords: HashAndAmount[]) => { | ||
return coords.map(([hash]) => { | ||
const [x, y, z] = hash.split('|').map((x) => parseFloat(x)) | ||
return new Vector3(x, y, z) | ||
}) | ||
} | ||
|
||
/** | ||
* This function expects geometry to be triangulated, no quads or anything | ||
*/ | ||
export const categorizeVertices = (geometry: BufferGeometry) => { | ||
const polygons = getNonIndexedVertices(geometry) | ||
|
||
const summary = Object.entries( | ||
countBy(({ vector }) => `${vector.x}|${vector.y}|${vector.z}`, polygons), | ||
) as HashAndAmount[] | ||
|
||
const [corner, edgeOrMiddle] = partition(([, amount]) => amount === 1 || amount === 2 || amount === 5, summary) | ||
const [edge, middle] = partition(([, amount]) => amount === 3, edgeOrMiddle) | ||
|
||
/* | ||
// TODO: for quadified meshes | ||
const [corner, edgeOrMiddle] = partition(([hash, amount]) => amount === 1 || amount === 3, summary) | ||
const [edge, middle] = partition(([hash, amount]) => amount === 2, edgeOrMiddle) | ||
*/ | ||
|
||
return { | ||
corners: unpackCoords(corner), | ||
edges: unpackCoords(edge), | ||
middles: unpackCoords(middle), | ||
} | ||
} |
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