Plane.js 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. import Cartesian3 from './Cartesian3.js';
  2. import Check from './Check.js';
  3. import defined from './defined.js';
  4. import DeveloperError from './DeveloperError.js';
  5. import freezeObject from './freezeObject.js';
  6. import CesiumMath from './Math.js';
  7. import Matrix4 from './Matrix4.js';
  8. /**
  9. * A plane in Hessian Normal Form defined by
  10. * <pre>
  11. * ax + by + cz + d = 0
  12. * </pre>
  13. * where (a, b, c) is the plane's <code>normal</code>, d is the signed
  14. * <code>distance</code> to the plane, and (x, y, z) is any point on
  15. * the plane.
  16. *
  17. * @alias Plane
  18. * @constructor
  19. *
  20. * @param {Cartesian3} normal The plane's normal (normalized).
  21. * @param {Number} distance The shortest distance from the origin to the plane. The sign of
  22. * <code>distance</code> determines which side of the plane the origin
  23. * is on. If <code>distance</code> is positive, the origin is in the half-space
  24. * in the direction of the normal; if negative, the origin is in the half-space
  25. * opposite to the normal; if zero, the plane passes through the origin.
  26. *
  27. * @example
  28. * // The plane x=0
  29. * var plane = new Cesium.Plane(Cesium.Cartesian3.UNIT_X, 0.0);
  30. *
  31. * @exception {DeveloperError} Normal must be normalized
  32. */
  33. function Plane(normal, distance) {
  34. //>>includeStart('debug', pragmas.debug);
  35. Check.typeOf.object('normal', normal);
  36. if (!CesiumMath.equalsEpsilon(Cartesian3.magnitude(normal), 1.0, CesiumMath.EPSILON6)) {
  37. throw new DeveloperError('normal must be normalized.');
  38. }
  39. Check.typeOf.number('distance', distance);
  40. //>>includeEnd('debug');
  41. /**
  42. * The plane's normal.
  43. *
  44. * @type {Cartesian3}
  45. */
  46. this.normal = Cartesian3.clone(normal);
  47. /**
  48. * The shortest distance from the origin to the plane. The sign of
  49. * <code>distance</code> determines which side of the plane the origin
  50. * is on. If <code>distance</code> is positive, the origin is in the half-space
  51. * in the direction of the normal; if negative, the origin is in the half-space
  52. * opposite to the normal; if zero, the plane passes through the origin.
  53. *
  54. * @type {Number}
  55. */
  56. this.distance = distance;
  57. }
  58. /**
  59. * Creates a plane from a normal and a point on the plane.
  60. *
  61. * @param {Cartesian3} point The point on the plane.
  62. * @param {Cartesian3} normal The plane's normal (normalized).
  63. * @param {Plane} [result] The object onto which to store the result.
  64. * @returns {Plane} A new plane instance or the modified result parameter.
  65. *
  66. * @example
  67. * var point = Cesium.Cartesian3.fromDegrees(-72.0, 40.0);
  68. * var normal = ellipsoid.geodeticSurfaceNormal(point);
  69. * var tangentPlane = Cesium.Plane.fromPointNormal(point, normal);
  70. *
  71. * @exception {DeveloperError} Normal must be normalized
  72. */
  73. Plane.fromPointNormal = function(point, normal, result) {
  74. //>>includeStart('debug', pragmas.debug);
  75. Check.typeOf.object('point', point);
  76. Check.typeOf.object('normal', normal);
  77. if (!CesiumMath.equalsEpsilon(Cartesian3.magnitude(normal), 1.0, CesiumMath.EPSILON6)) {
  78. throw new DeveloperError('normal must be normalized.');
  79. }
  80. //>>includeEnd('debug');
  81. var distance = -Cartesian3.dot(normal, point);
  82. if (!defined(result)) {
  83. return new Plane(normal, distance);
  84. }
  85. Cartesian3.clone(normal, result.normal);
  86. result.distance = distance;
  87. return result;
  88. };
  89. var scratchNormal = new Cartesian3();
  90. /**
  91. * Creates a plane from the general equation
  92. *
  93. * @param {Cartesian4} coefficients The plane's normal (normalized).
  94. * @param {Plane} [result] The object onto which to store the result.
  95. * @returns {Plane} A new plane instance or the modified result parameter.
  96. *
  97. * @exception {DeveloperError} Normal must be normalized
  98. */
  99. Plane.fromCartesian4 = function(coefficients, result) {
  100. //>>includeStart('debug', pragmas.debug);
  101. Check.typeOf.object('coefficients', coefficients);
  102. //>>includeEnd('debug');
  103. var normal = Cartesian3.fromCartesian4(coefficients, scratchNormal);
  104. var distance = coefficients.w;
  105. //>>includeStart('debug', pragmas.debug);
  106. if (!CesiumMath.equalsEpsilon(Cartesian3.magnitude(normal), 1.0, CesiumMath.EPSILON6)) {
  107. throw new DeveloperError('normal must be normalized.');
  108. }
  109. //>>includeEnd('debug');
  110. if (!defined(result)) {
  111. return new Plane(normal, distance);
  112. }
  113. Cartesian3.clone(normal, result.normal);
  114. result.distance = distance;
  115. return result;
  116. };
  117. /**
  118. * Computes the signed shortest distance of a point to a plane.
  119. * The sign of the distance determines which side of the plane the point
  120. * is on. If the distance is positive, the point is in the half-space
  121. * in the direction of the normal; if negative, the point is in the half-space
  122. * opposite to the normal; if zero, the plane passes through the point.
  123. *
  124. * @param {Plane} plane The plane.
  125. * @param {Cartesian3} point The point.
  126. * @returns {Number} The signed shortest distance of the point to the plane.
  127. */
  128. Plane.getPointDistance = function(plane, point) {
  129. //>>includeStart('debug', pragmas.debug);
  130. Check.typeOf.object('plane', plane);
  131. Check.typeOf.object('point', point);
  132. //>>includeEnd('debug');
  133. return Cartesian3.dot(plane.normal, point) + plane.distance;
  134. };
  135. var scratchCartesian = new Cartesian3();
  136. /**
  137. * Projects a point onto the plane.
  138. * @param {Plane} plane The plane to project the point onto
  139. * @param {Cartesian3} point The point to project onto the plane
  140. * @param {Cartesian3} [result] The result point. If undefined, a new Cartesian3 will be created.
  141. * @returns {Cartesian3} The modified result parameter or a new Cartesian3 instance if one was not provided.
  142. */
  143. Plane.projectPointOntoPlane = function(plane, point, result) {
  144. //>>includeStart('debug', pragmas.debug);
  145. Check.typeOf.object('plane', plane);
  146. Check.typeOf.object('point', point);
  147. //>>includeEnd('debug');
  148. if (!defined(result)) {
  149. result = new Cartesian3();
  150. }
  151. // projectedPoint = point - (normal.point + scale) * normal
  152. var pointDistance = Plane.getPointDistance(plane, point);
  153. var scaledNormal = Cartesian3.multiplyByScalar(plane.normal, pointDistance, scratchCartesian);
  154. return Cartesian3.subtract(point, scaledNormal, result);
  155. };
  156. var scratchPosition = new Cartesian3();
  157. /**
  158. * Transforms the plane by the given transformation matrix.
  159. *
  160. * @param {Plane} plane The plane.
  161. * @param {Matrix4} transform The transformation matrix.
  162. * @param {Plane} [result] The object into which to store the result.
  163. * @returns {Plane} The plane transformed by the given transformation matrix.
  164. */
  165. Plane.transform = function(plane, transform, result) {
  166. //>>includeStart('debug', pragmas.debug);
  167. Check.typeOf.object('plane', plane);
  168. Check.typeOf.object('transform', transform);
  169. //>>includeEnd('debug');
  170. Matrix4.multiplyByPointAsVector(transform, plane.normal, scratchNormal);
  171. Cartesian3.normalize(scratchNormal, scratchNormal);
  172. Cartesian3.multiplyByScalar(plane.normal, -plane.distance, scratchPosition);
  173. Matrix4.multiplyByPoint(transform, scratchPosition, scratchPosition);
  174. return Plane.fromPointNormal(scratchPosition, scratchNormal, result);
  175. };
  176. /**
  177. * Duplicates a Plane instance.
  178. *
  179. * @param {Plane} plane The plane to duplicate.
  180. * @param {Plane} [result] The object onto which to store the result.
  181. * @returns {Plane} The modified result parameter or a new Plane instance if one was not provided.
  182. */
  183. Plane.clone = function(plane, result) {
  184. //>>includeStart('debug', pragmas.debug);
  185. Check.typeOf.object('plane', plane);
  186. //>>includeEnd('debug');
  187. if (!defined(result)) {
  188. return new Plane(plane.normal, plane.distance);
  189. }
  190. Cartesian3.clone(plane.normal, result.normal);
  191. result.distance = plane.distance;
  192. return result;
  193. };
  194. /**
  195. * Compares the provided Planes by normal and distance and returns
  196. * <code>true</code> if they are equal, <code>false</code> otherwise.
  197. *
  198. * @param {Plane} left The first plane.
  199. * @param {Plane} right The second plane.
  200. * @returns {Boolean} <code>true</code> if left and right are equal, <code>false</code> otherwise.
  201. */
  202. Plane.equals = function(left, right) {
  203. //>>includeStart('debug', pragmas.debug);
  204. Check.typeOf.object('left', left);
  205. Check.typeOf.object('right', right);
  206. //>>includeEnd('debug');
  207. return (left.distance === right.distance) && Cartesian3.equals(left.normal, right.normal);
  208. };
  209. /**
  210. * A constant initialized to the XY plane passing through the origin, with normal in positive Z.
  211. *
  212. * @type {Plane}
  213. * @constant
  214. */
  215. Plane.ORIGIN_XY_PLANE = freezeObject(new Plane(Cartesian3.UNIT_Z, 0.0));
  216. /**
  217. * A constant initialized to the YZ plane passing through the origin, with normal in positive X.
  218. *
  219. * @type {Plane}
  220. * @constant
  221. */
  222. Plane.ORIGIN_YZ_PLANE = freezeObject(new Plane(Cartesian3.UNIT_X, 0.0));
  223. /**
  224. * A constant initialized to the ZX plane passing through the origin, with normal in positive Y.
  225. *
  226. * @type {Plane}
  227. * @constant
  228. */
  229. Plane.ORIGIN_ZX_PLANE = freezeObject(new Plane(Cartesian3.UNIT_Y, 0.0));
  230. export default Plane;