@woosh/meep-engine
Version:
Pure JavaScript game engine. Fully featured and production ready.
49 lines (42 loc) • 1.5 kB
JavaScript
/**
* Non-robust
*
* Return a positive value if the point pd lies below the
* plane passing through pa, pb, and pc; "below" is defined so
* that pa, pb, and pc appear in counterclockwise order when
* viewed from above the plane. Returns a negative value if
* pd lies above the plane. Returns zero if the points are
* coplanar. The result is also a rough approximation of six
* times the signed volume of the tetrahedron defined by the
* four points.
*
* NOTE: ported from http://www.cs.cmu.edu/afs/cs/project/quake/public/code/predicates.c
*
* @param {number[]} points
* @param {number} a index of point a
* @param {number} b index of point b
* @param {number} c index of point c
* @param {number} d index of point d
* @returns {number}
*/
export function orient3d_fast(points, a, b, c, d) {
const a3 = a * 3;
const d3 = d * 3;
const b3 = b * 3;
const c3 = c * 3;
const d_x = points[d3];
const d_y = points[d3 + 1];
const d_z = points[d3 + 2];
const adx = points[a3] - d_x;
const ady = points[a3 + 1] - d_y;
const adz = points[a3 + 2] - d_z;
const bdx = points[b3] - d_x;
const bdy = points[b3 + 1] - d_y;
const bdz = points[b3 + 2] - d_z;
const cdx = points[c3] - d_x;
const cdy = points[c3 + 1] - d_y;
const cdz = points[c3 + 2] - d_z;
return adx * (bdy * cdz - bdz * cdy)
+ bdx * (cdy * adz - cdz * ady)
+ cdx * (ady * bdz - adz * bdy);
}