# three-mesh-halfedge

> A typescript implementation of the Halfedge structure for three.js geometries.

Latest version **1.0.3** (published 2022-11-29) · MIT license · 0 weekly downloads

## Install

```sh
npm install three-mesh-halfedge
pnpm add three-mesh-halfedge
yarn add three-mesh-halfedge
bun add three-mesh-halfedge
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.3 |
| Published | 2022-11-29 |
| First published | 2022-11-15 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 204.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Axel Antoine |
| Maintainers | esquisse |
| Keywords | threejs, geometry, mesh, csg, halfedge |

## Links

- npm: https://www.npmjs.com/package/three-mesh-halfedge
- Repository: https://github.com/LokiResearch/three-mesh-halfedge
- Homepage: https://lokiresearch.github.io/three-mesh-halfedge/
- Issues: https://github.com/LokiResearch/three-mesh-halfedge/issues
- npm.io page: https://npm.io/package/three-mesh-halfedge

## Recent versions

- 1.0.3 (latest) — 2022-11-29
- 1.0.2 — 2022-11-24
- 1.0.1 — 2022-11-18
- 1.0.0 — 2022-11-15

## README

# three-mesh-halfedge

[![npm release](https://img.shields.io/npm/v/three-mesh-halfedge)](https://www.npmjs.com/package/three-mesh-halfedge)
[![build](https://img.shields.io/github/workflow/status/LokiResearch/three-svg-renderer/build)](https://github.com/LokiResearch/three-mesh-halfedge/actions)
[![Documentation](https://img.shields.io/badge/view-Documentation-blue?label=Open)](https://LokiResearch.github.io/three-mesh-halfedge/doc/index.html)
[![License](https://img.shields.io/badge/License-MIT-green)](#license)

A typescript implementation of the Halfedge structure for three.js geometries.

<img src="./images/halfedge.png" width="49%"/><img src="./images/contours.png" width="49%"/>

Supports multiple topologies:
- Multiple edges between the same vertices
- Isolated polygons
- Isolated edges
- Isolated vertices
- Mixed wireframe and polygons
- Polygons with an arbitrary number of vertices and edges
- Polygons meeting only at one vertex

## Examples

- [HalfedgeDS Visualisation](https://LokiResearch.github.io/three-mesh-halfedge/build-examples/HalfedgeDSVisualisation.html)
- [Realtime contours extraction](https://LokiResearch.github.io/three-mesh-halfedge/build-examples/ExtractContours.html)

## Installation
```bash
npm install three-mesh-halfedge
```

## Documentation

[![Documentation](https://img.shields.io/badge/view-Documentation-blue?label=Open)](https://LokiResearch.github.io/three-mesh-halfedge/doc/index.html)

*Documentation is in progress.*

## Code snippets

##### Example 1: Build the Halfedge structure
```ts
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);
```

##### Example 2: Extract the boundary halfedges of a mesh
```ts
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);
```


##### Example 3: Get the front faces of a mesh
```ts
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);
```
## Useful links and references

[Kalle Rutanen Homepage - Halfedge data structures](https://kaba.hilvi.org/homepage/blog/halfedge/halfedge.htm)

---
_Source: https://npm.io/package/three-mesh-halfedge · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
