Skip to content

Commit

Permalink
feat(tools/mesh): create categorizeVertices mesh tool
Browse files Browse the repository at this point in the history
  • Loading branch information
meszaros-lajos-gyorgy committed Aug 19, 2023
1 parent 7e4a187 commit c25e0ae
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 38 deletions.
39 changes: 39 additions & 0 deletions src/tools/mesh/categorizeVertices.ts
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),
}
}
40 changes: 2 additions & 38 deletions src/tools/mesh/connectEdgeTo.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,6 @@
import { BufferAttribute, BufferGeometry } from 'three'
import { Vector3 } from '@src/Vector3.js'
import { countBy, partition } from '@src/faux-ramda.js'
import { getNonIndexedVertices, getVertices } from '@tools/mesh/getVertices.js'

type HashAndAmount = [string, number]

const unpackCoords = (coords: HashAndAmount[]) => {
return coords.map(([hash, amount]) => {
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
*/
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),
}
}
import { getVertices } from '@tools/mesh/getVertices.js'
import { categorizeVertices } from './categorizeVertices.js'

/**
* Connect the edge vertices of "source" to the edge vertices of "target"
Expand Down
1 change: 1 addition & 0 deletions src/tools/mesh/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ export { toArxCoordinateSystem } from '@tools/mesh/toArxCoordinateSystem.js'
export { transformEdge } from '@tools/mesh/transformEdge.js'
export { translateUV } from '@tools/mesh/translateUV.js'
export { connectEdgeTo } from '@tools/mesh/connectEdgeTo.js'
export { categorizeVertices } from '@tools/mesh/categorizeVertices.js'

export type { GeometryVertex } from '@tools/mesh/getVertices.js'

0 comments on commit c25e0ae

Please sign in to comment.