A typescript implementation of the Halfedge structure for three.js geometries.
Supports multiple topologies:
npm install three-mesh-halfedge
Documentation is in progress.
import * as THREE from 'three';
import { HalfedgeDS } from 'three-mesh-halfedge';
// Build the Halfedge structure from a BoxGeometry
const geometry = new THREE.BoxGeometry();
const struct = new HalfedgeDS();
struct.setFromGeometry(geometry, 1e-10);
const struct = new HalfedgeDS();
struct.setFromGeometry(mesh.geometry);
// Get the boundary edges (keep only one halfedge for each pair)
const boundaries = new Set<Halfedge>();
for (const halfedge of struct.halfedges) {
if (!boundaries.has(halfedge.twin) && !halfedge.face) {
boundaries.add(halfedge);
}
}
console.log("Boundary halfedges", boundaries);
const struct = new HalfedgeDS();
struct.setFromGeometry(mesh.geometry);
// Get the camera position in mesh's space
const localCameraPos = mesh.worldToLocal(camera.position.clone());
// Get the front faces
const array = [];
for (const face of struct.faces) {
// /!\ Attention: position is considered in geometry local system
if (face.isFront(localCameraPos)) {
array.push(face);
}
}
console.log("Front faces", array);