UNPKG

cornerstone-math

Version:

Math and computation geometry functionality for cornerstone

291 lines (169 loc) 6.51 kB
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>plane.js - Documentation</title> <script src="scripts/prettify/prettify.js"></script> <script src="scripts/prettify/lang-css.js"></script> <!--[if lt IE 9]> <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script> <![endif]--> <link type="text/css" rel="stylesheet" href="styles/prettify.css"> <link type="text/css" rel="stylesheet" href="styles/jsdoc.css"> </head> <body> <input type="checkbox" id="nav-trigger" class="nav-trigger" /> <label for="nav-trigger" class="navicon-button x"> <div class="navicon"></div> </label> <label for="nav-trigger" class="overlay"></label> <nav> <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="Line3.html">Line3</a></li><li><a href="Plane.html">Plane</a></li></ul><h3>Global</h3><ul><li><a href="global.html#approximatelyEquals">approximatelyEquals</a></li><li><a href="global.html#findClosestPoint">findClosestPoint</a></li></ul> </nav> <div id="main"> <h1 class="page-title">plane.js</h1> <section> <article> <pre class="prettyprint source linenums"><code>import Vector3 from './vector3.js'; // Copied from Three.JS /** * @author bhouston / http://exocortex.com */ class Plane { constructor (normal, constant) { this.normal = (normal !== undefined) ? normal : new Vector3(1, 0, 0); this.constant = (constant !== undefined) ? constant : 0; } set (normal, constant) { this.normal.copy(normal); this.constant = constant; return this; } setComponents (x, y, z, w) { this.normal.set(x, y, z); this.constant = w; return this; } setFromNormalAndCoplanarPoint (normal, point) { this.normal.copy(normal); // Must be this.normal, not normal, as this.normal is normalized this.constant = -point.dot(this.normal); return this; } copy (plane) { this.normal.copy(plane.normal); this.constant = plane.constant; return this; } normalize () { // Note: will lead to a divide by zero if the plane is invalid. const inverseNormalLength = 1.0 / this.normal.length(); this.normal.multiplyScalar(inverseNormalLength); this.constant *= inverseNormalLength; return this; } negate () { this.constant *= -1; this.normal.negate(); return this; } distanceToPoint (point) { return this.normal.dot(point) + this.constant; } distanceToSphere (sphere) { return this.distanceToPoint(sphere.center) - sphere.radius; } projectPoint (point, optionalTarget) { return this.orthoPoint(point, optionalTarget).sub(point).negate(); } orthoPoint (point, optionalTarget) { const perpendicularMagnitude = this.distanceToPoint(point); const result = optionalTarget || new Vector3(); return result.copy(this.normal).multiplyScalar(perpendicularMagnitude); } isIntersectionLine (line) { // Note: this tests if a line intersects the plane, not whether it (or its end-points) are coplanar with it. const startSign = this.distanceToPoint(line.start); const endSign = this.distanceToPoint(line.end); return (startSign &lt; 0 &amp;&amp; endSign > 0) || (endSign &lt; 0 &amp;&amp; startSign > 0); } intersectPlane (targetPlane) { // Returns the intersection line between two planes const direction = this.normal.clone().cross(targetPlane.normal); const origin = new Vector3(); const intersectionData = { origin, direction }; // If the planes are parallel, return an empty vector for the intersection line if (this.normal.clone().cross(targetPlane.normal).length &lt; 1e-10) { intersectionData.direction = new Vector3(); return intersectionData; } const h1 = this.constant; const h2 = targetPlane.constant; const n1dotn2 = this.normal.clone().dot(targetPlane.normal); const c1 = -(h1 - h2 * n1dotn2) / (1 - n1dotn2 * n1dotn2); const c2 = -(h2 - h1 * n1dotn2) / (1 - n1dotn2 * n1dotn2); intersectionData.origin = this.normal.clone().multiplyScalar(c1).add(targetPlane.normal.clone().multiplyScalar(c2)); return intersectionData; } coplanarPoint (optionalTarget) { const result = optionalTarget || new Vector3(); return result.copy(this.normal).multiplyScalar(-this.constant); } translate (offset) { this.constant = this.constant - offset.dot(this.normal); return this; } equals (plane) { return plane.normal.equals(this.normal) &amp;&amp; (plane.constant === this.constant); } clone () { return new Plane().copy(this); } } Plane.prototype.setFromCoplanarPoints = (function () { const v1 = new Vector3(); const v2 = new Vector3(); return function (a, b, c) { const normal = v1.subVectors(c, b).cross(v2.subVectors(a, b)).normalize(); // Q: should an error be thrown if normal is zero (e.g. degenerate plane)? this.setFromNormalAndCoplanarPoint(normal, a); return this; }; })(); Plane.prototype.intersectLine = (function () { const v1 = new Vector3(); return function (line, optionalTarget) { const result = optionalTarget || new Vector3(); const direction = line.delta(v1); const denominator = this.normal.dot(direction); if (denominator === 0) { // Line is coplanar, return origin if (this.distanceToPoint(line.start) === 0) { return result.copy(line.start); } // Unsure if this is the correct method to handle this case. return undefined; } const t = -(line.start.dot(this.normal) + this.constant) / denominator; if (t &lt; 0 || t > 1) { return undefined; } return result.copy(direction).multiplyScalar(t).add(line.start); }; })(); export default Plane; </code></pre> </article> </section> </div> <br class="clear"> <footer> Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.6.6</a> using the <a href="https://github.com/clenemt/docdash">docdash</a> theme. </footer> <script>prettyPrint();</script> <script src="scripts/linenumber.js"></script> </body> </html>