| 1 | # robust-predicates
|
|---|
| 2 |
|
|---|
| 3 | Fast robust predicates for computational geometry in JavaScript. Provides reliable 2D and 3D point orientation tests (`orient2d`, `orient3d`, `incircle`, `insphere`) that are not susceptible to floating point errors (without sacrificing performance). A modern port of [Jonathan R Shewchuk's C code](https://www.cs.cmu.edu/~quake/robust.html), an industry standard since 1996.
|
|---|
| 4 |
|
|---|
| 5 | <a href="https://observablehq.com/@mourner/non-robust-arithmetic-as-art"><img width="600" height="200" src="predicates.png" /></a>
|
|---|
| 6 |
|
|---|
| 7 | _Figure: non-robust vs robust `orient2d` test for points within a tiny range (2<sup>-42</sup>)._
|
|---|
| 8 |
|
|---|
| 9 | [](https://github.com/mourner/robust-predicates/actions)
|
|---|
| 10 | [](https://github.com/mourner/projects)
|
|---|
| 11 | [](https://unpkg.com/robust-predicates)
|
|---|
| 12 |
|
|---|
| 13 | ## [Demo](https://observablehq.com/@mourner/non-robust-arithmetic-as-art)
|
|---|
| 14 |
|
|---|
| 15 | ## API
|
|---|
| 16 |
|
|---|
| 17 | Note: unlike J. Shewchuk's original code, all the functions in this library assume `y` axis is oriented _downwards_ ↓, so the semantics are different.
|
|---|
| 18 |
|
|---|
| 19 | ### `orient2d(ax,ay, bx,by, cx,cy)`
|
|---|
| 20 |
|
|---|
| 21 | - Returns a *positive* value if the points `a`, `b`, and `c` occur in _counterclockwise_ order (`c` lies to the left of the directed line defined by points `a` and `b`).
|
|---|
| 22 | - Returns a *negative* value if they occur in _clockwise_ order (`c` lies to the right of the directed line `ab`).
|
|---|
| 23 | - Returns *zero* if they are _collinear_.
|
|---|
| 24 |
|
|---|
| 25 | The result is also an approximation of twice the signed area of the triangle defined by the three points.
|
|---|
| 26 |
|
|---|
| 27 | ### `incircle(ax,ay, bx,by, cx,cy, dx,dy)`
|
|---|
| 28 |
|
|---|
| 29 | - Returns a _positive_ value if the point `d` lies _outside_ the circle passing through `a`, `b`, and `c`.
|
|---|
| 30 | - Returns a _negative_ value if it lies _inside_.
|
|---|
| 31 | - Returns _zero_ if the four points are _cocircular_.
|
|---|
| 32 |
|
|---|
| 33 | The points `a`, `b`, and `c` must be in _counterclockwise_ order, or the sign of the result will be reversed.
|
|---|
| 34 |
|
|---|
| 35 | ### `orient3d(ax,ay,az, bx,by,bz, cx,cy,cz, dx,dy,dz)`
|
|---|
| 36 |
|
|---|
| 37 | - Returns a _positive_ value if the point `d` lies _above_ the plane passing through `a`, `b`, and `c`, meaning that `a`, `b`, and `c` appear in counterclockwise order when viewed from `d`.
|
|---|
| 38 | - Returns a _negative_ value if `d` lies _below_ the plane.
|
|---|
| 39 | - Returns _zero_ if the points are _coplanar_.
|
|---|
| 40 |
|
|---|
| 41 | The result is also an approximation of six times the signed volume of the tetrahedron defined by the four points.
|
|---|
| 42 |
|
|---|
| 43 | ### `insphere(ax,ay,az, bx,by,bz, cx,cy,cz, dx,dy,dz, ex,ey,ez)`
|
|---|
| 44 |
|
|---|
| 45 | - Returns a _positive_ value if the point `e` lies _outside_ the sphere passing through `a`, `b`, `c`, and `d`.
|
|---|
| 46 | - Returns a _negative_ value if it lies _inside_.
|
|---|
| 47 | - Returns _zero_ if the five points are _cospherical_.
|
|---|
| 48 |
|
|---|
| 49 | The points `a`, `b`, `c`, and `d` must be ordered so that they have a _positive orientation_
|
|---|
| 50 | (as defined by `orient3d`), or the sign of the result will be reversed.
|
|---|
| 51 |
|
|---|
| 52 | ### `orient2dfast`, `orient3dfast`, `incirclefast`, `inspherefast`
|
|---|
| 53 |
|
|---|
| 54 | Simple, approximate, non-robust versions of predicates above. Use when robustness isn't needed.
|
|---|
| 55 |
|
|---|
| 56 | ## Example
|
|---|
| 57 |
|
|---|
| 58 | ```js
|
|---|
| 59 | import {orient2d} from 'robust-predicates';
|
|---|
| 60 |
|
|---|
| 61 | const ccw = orient2d(ax, ay, bx, by, cx, cy) > 0;
|
|---|
| 62 | ````
|
|---|
| 63 |
|
|---|
| 64 | ## Install
|
|---|
| 65 |
|
|---|
| 66 | Install with `npm install robust-predicates` or `yarn add robust-predicates`, or use one of the browser builds:
|
|---|
| 67 |
|
|---|
| 68 | - [predicates.min.js](https://unpkg.com/robust-predicates/umd/predicates.min.js) (all predicates)
|
|---|
| 69 | - [orient2d.min.js](https://unpkg.com/robust-predicates/umd/orient2d.min.js) (`orient2d`, `orient2dfast`)
|
|---|
| 70 | - [orient3d.min.js](https://unpkg.com/robust-predicates/umd/orient3d.min.js) (`orient3d`, `orient3dfast`)
|
|---|
| 71 | - [incircle.min.js](https://unpkg.com/robust-predicates/umd/incircle.min.js) (`incircle`, `incirclefast`)
|
|---|
| 72 | - [insphere.min.js](https://unpkg.com/robust-predicates/umd/insphere.min.js) (`insphere`, `inspherefast`)
|
|---|
| 73 |
|
|---|
| 74 | ## Thanks
|
|---|
| 75 |
|
|---|
| 76 | This project is just a port — all the brilliant, hard work was done by [Jonathan Richard Shewchuk](https://people.eecs.berkeley.edu/~jrs/).
|
|---|
| 77 |
|
|---|
| 78 | The port was also inspired by [Mikola Lysenko](https://twitter.com/MikolaLysenko)'s excellent [Robust Arithmetic Notes](https://github.com/mikolalysenko/robust-arithmetic-notes) and related projects like [robust-orientation](https://github.com/mikolalysenko/robust-orientation) and [robust-in-sphere](https://github.com/mikolalysenko/robust-in-sphere).
|
|---|
| 79 |
|
|---|
| 80 | ## License
|
|---|
| 81 |
|
|---|
| 82 | Since the original code is in the public domain, this project follows the same choice. See [Unlicense](https://unlicense.org).
|
|---|