# triangles-distance-fast

> signed or unsigned distance function for triangles using r-tree and/or bvh and/or kd-tree

Latest version **1.0.76** (published 2023-10-25) · MIT license · 0 weekly downloads

## Install

```sh
npm install triangles-distance-fast
pnpm add triangles-distance-fast
yarn add triangles-distance-fast
bun add triangles-distance-fast
```

## Health

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

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.76 |
| Published | 2023-10-25 |
| First published | 2022-01-20 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 7 |
| Unpacked size | 258.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | stonkpunk |
| Maintainers | stonkpunk |
| Keywords | triangles, distance, function |

## Links

- npm: https://www.npmjs.com/package/triangles-distance-fast
- Homepage: https://github.com/stonkpunk/my-npm-modules/tree/main/triangles-distance-fast
- npm.io page: https://npm.io/package/triangles-distance-fast

## Dependencies (7)

- [rbush-3d](https://npm.io/package/rbush-3d.md) 0.0.4
- [underscore](https://npm.io/package/underscore.md) ^1.13.2
- [rbush-3d-knn](https://npm.io/package/rbush-3d-knn.md) ^0.0.1
- [bvh-tree-plus](https://npm.io/package/bvh-tree-plus.md) ^1.0.2
- [static-kdtree](https://npm.io/package/static-kdtree.md) ^1.0.2
- [triangle-distance](https://npm.io/package/triangle-distance.md) ^1.0.11
- [triangles-mesh-random-pts](https://npm.io/package/triangles-mesh-random-pts.md) ^0.0.2

## Recent versions

- 1.0.76 (latest) — 2023-10-25
- 1.0.75 — 2023-10-25
- 1.0.74 — 2023-10-24
- 1.0.73 — 2023-10-24
- 1.0.72 — 2023-10-22
- 1.0.71 — 2023-08-22
- 1.0.7 — 2022-04-05
- 1.0.6 — 2022-04-05
- 1.0.5 — 2022-04-05
- 1.0.4 — 2022-04-05
- 1.0.3 — 2022-02-06
- 1.0.2 — 2022-01-21
- 1.0.1 — 2022-01-20

## README

# triangles-distance-fast

signed or unsigned distance to triangles mesh using rtree and/or bvh tree 

uses `bvh-tree-plus` for the bvh tree and `rbush-3d` for the rtree. 

we use the rtree to quickly get a list of triangles potentially inside a bounding box, and the bvh tree to determine if a point is inside/outside the mesh via raycasting.

```sh
npm i triangles-distance-fast
```

```javascript
//triangle = [[x,y,z],[x,y,z],[x,y,z]]
//aabb = [[x,y,z],[x,y,z]] //axis aligned bounding box, [min, max]

//newest, no need for dist limit
// trianglesDistanceKnn(pt, triangles, nNeighbors=10, trisRTree)

//older ones 
// trianglesDistance(tris, radius, rtree?) // unsigned distance function(x,y,z) sampling over radius, use optional cached rtree - returns >9999 if no triangles within radius
// trianglesDistance_signed(tris, radius, rtree?, bvh?) // signed distance function(x,y,z) sampling over radius, use optional cached rtree and bvh tree - returns abs() > 9999 if no triangles within radius
// trianglesDistanceStable(pt, triangles, trisRTree, eps=0.1, S=1000) unsigned, sampling over 3 narrow bounding boxes along the XYZ axis 
// trianglesDistanceBruteForce(pt, triangles) //brute force / naive approach, testing each triangle
// trianglesIntersectingAABB(tris, aabb, rtree?) // get set of triangles whose bounding boxes intersect the aabb
// triangles2RTree(tris) //get rtree for triangles 
// triangles2BvhTree(tris) //get bvh for triangles 
// trianglesFlatten(tris) //flatten tris into 1-d float32 array

// funcs using a kd-tree based on loading triangle verts [note that searching using the vertices of the triangles rather than 'bounding volumes' may introduce edge cases that the r-tree does not]
// trianglesDistance_KdTree(tris, maxDist=Infinity, existingTree) //unsigned dist using a kd tree
// triangles2KdTree(tris) //convert tris to static-kdtree

// stochastic kd tree generates random pts on the surface of the mesh rather than just using the triangle verts 
// trianglesDistance_KdTree_stochastic(tris, radius=Infinity, _existingStochasticTreeDescriptor=null, nPtsSampler=tris.length*5)
// triangles2KdTreeStochastic(tris, nPtsToSample, skipAddVerts = false) //result may be less 'biased' if we dont add the triangle verts themselves

var tdf = require('triangles-distance-fast'); 
var fs = require("fs");
var stl = require('stl');
var path = require('path');

//load tris from stl file
var triangles = stl.toObject(fs.readFileSync(path.join(__dirname,'Bitey_Reconstructed_5k.stl'))).facets.map(function(f){return f.verts});

//make distance function
var df = tdf.trianglesDistance(triangles,20.0);

//use it
var dist = df(10,10,10);
console.log(dist); //5.114792068071368

//returns >9999 if nothing found within the radius [or < -9999 if using signed distance inside mesh]
```


[![stonks](https://i.imgur.com/UpDxbfe.png)](https://www.npmjs.com/~stonkpunk)

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