Plane-4c3d403b.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. define(['exports', './Matrix3-41c58dde', './Matrix2-e1298525', './Check-6ede7e26', './defaultValue-fe22d8c0', './Math-0a2ac845'], (function (exports, Matrix3, Matrix2, Check, defaultValue, Math) { 'use strict';
  2. /**
  3. * A plane in Hessian Normal Form defined by
  4. * <pre>
  5. * ax + by + cz + d = 0
  6. * </pre>
  7. * where (a, b, c) is the plane's <code>normal</code>, d is the signed
  8. * <code>distance</code> to the plane, and (x, y, z) is any point on
  9. * the plane.
  10. *
  11. * @alias Plane
  12. * @constructor
  13. *
  14. * @param {Cartesian3} normal The plane's normal (normalized).
  15. * @param {number} distance The shortest distance from the origin to the plane. The sign of
  16. * <code>distance</code> determines which side of the plane the origin
  17. * is on. If <code>distance</code> is positive, the origin is in the half-space
  18. * in the direction of the normal; if negative, the origin is in the half-space
  19. * opposite to the normal; if zero, the plane passes through the origin.
  20. *
  21. * @example
  22. * // The plane x=0
  23. * const plane = new Cesium.Plane(Cesium.Cartesian3.UNIT_X, 0.0);
  24. *
  25. * @exception {DeveloperError} Normal must be normalized
  26. */
  27. function Plane(normal, distance) {
  28. //>>includeStart('debug', pragmas.debug);
  29. Check.Check.typeOf.object("normal", normal);
  30. if (
  31. !Math.CesiumMath.equalsEpsilon(
  32. Matrix3.Cartesian3.magnitude(normal),
  33. 1.0,
  34. Math.CesiumMath.EPSILON6
  35. )
  36. ) {
  37. throw new Check.DeveloperError("normal must be normalized.");
  38. }
  39. Check.Check.typeOf.number("distance", distance);
  40. //>>includeEnd('debug');
  41. /**
  42. * The plane's normal.
  43. *
  44. * @type {Cartesian3}
  45. */
  46. this.normal = Matrix3.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. * const point = Cesium.Cartesian3.fromDegrees(-72.0, 40.0);
  68. * const normal = ellipsoid.geodeticSurfaceNormal(point);
  69. * const 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.Check.typeOf.object("point", point);
  76. Check.Check.typeOf.object("normal", normal);
  77. if (
  78. !Math.CesiumMath.equalsEpsilon(
  79. Matrix3.Cartesian3.magnitude(normal),
  80. 1.0,
  81. Math.CesiumMath.EPSILON6
  82. )
  83. ) {
  84. throw new Check.DeveloperError("normal must be normalized.");
  85. }
  86. //>>includeEnd('debug');
  87. const distance = -Matrix3.Cartesian3.dot(normal, point);
  88. if (!defaultValue.defined(result)) {
  89. return new Plane(normal, distance);
  90. }
  91. Matrix3.Cartesian3.clone(normal, result.normal);
  92. result.distance = distance;
  93. return result;
  94. };
  95. const scratchNormal = new Matrix3.Cartesian3();
  96. /**
  97. * Creates a plane from the general equation
  98. *
  99. * @param {Cartesian4} coefficients The plane's normal (normalized).
  100. * @param {Plane} [result] The object onto which to store the result.
  101. * @returns {Plane} A new plane instance or the modified result parameter.
  102. *
  103. * @exception {DeveloperError} Normal must be normalized
  104. */
  105. Plane.fromCartesian4 = function (coefficients, result) {
  106. //>>includeStart('debug', pragmas.debug);
  107. Check.Check.typeOf.object("coefficients", coefficients);
  108. //>>includeEnd('debug');
  109. const normal = Matrix3.Cartesian3.fromCartesian4(coefficients, scratchNormal);
  110. const distance = coefficients.w;
  111. //>>includeStart('debug', pragmas.debug);
  112. if (
  113. !Math.CesiumMath.equalsEpsilon(
  114. Matrix3.Cartesian3.magnitude(normal),
  115. 1.0,
  116. Math.CesiumMath.EPSILON6
  117. )
  118. ) {
  119. throw new Check.DeveloperError("normal must be normalized.");
  120. }
  121. //>>includeEnd('debug');
  122. if (!defaultValue.defined(result)) {
  123. return new Plane(normal, distance);
  124. }
  125. Matrix3.Cartesian3.clone(normal, result.normal);
  126. result.distance = distance;
  127. return result;
  128. };
  129. /**
  130. * Computes the signed shortest distance of a point to a plane.
  131. * The sign of the distance determines which side of the plane the point
  132. * is on. If the distance is positive, the point is in the half-space
  133. * in the direction of the normal; if negative, the point is in the half-space
  134. * opposite to the normal; if zero, the plane passes through the point.
  135. *
  136. * @param {Plane} plane The plane.
  137. * @param {Cartesian3} point The point.
  138. * @returns {number} The signed shortest distance of the point to the plane.
  139. */
  140. Plane.getPointDistance = function (plane, point) {
  141. //>>includeStart('debug', pragmas.debug);
  142. Check.Check.typeOf.object("plane", plane);
  143. Check.Check.typeOf.object("point", point);
  144. //>>includeEnd('debug');
  145. return Matrix3.Cartesian3.dot(plane.normal, point) + plane.distance;
  146. };
  147. const scratchCartesian = new Matrix3.Cartesian3();
  148. /**
  149. * Projects a point onto the plane.
  150. * @param {Plane} plane The plane to project the point onto
  151. * @param {Cartesian3} point The point to project onto the plane
  152. * @param {Cartesian3} [result] The result point. If undefined, a new Cartesian3 will be created.
  153. * @returns {Cartesian3} The modified result parameter or a new Cartesian3 instance if one was not provided.
  154. */
  155. Plane.projectPointOntoPlane = function (plane, point, result) {
  156. //>>includeStart('debug', pragmas.debug);
  157. Check.Check.typeOf.object("plane", plane);
  158. Check.Check.typeOf.object("point", point);
  159. //>>includeEnd('debug');
  160. if (!defaultValue.defined(result)) {
  161. result = new Matrix3.Cartesian3();
  162. }
  163. // projectedPoint = point - (normal.point + scale) * normal
  164. const pointDistance = Plane.getPointDistance(plane, point);
  165. const scaledNormal = Matrix3.Cartesian3.multiplyByScalar(
  166. plane.normal,
  167. pointDistance,
  168. scratchCartesian
  169. );
  170. return Matrix3.Cartesian3.subtract(point, scaledNormal, result);
  171. };
  172. const scratchInverseTranspose = new Matrix2.Matrix4();
  173. const scratchPlaneCartesian4 = new Matrix2.Cartesian4();
  174. const scratchTransformNormal = new Matrix3.Cartesian3();
  175. /**
  176. * Transforms the plane by the given transformation matrix.
  177. *
  178. * @param {Plane} plane The plane.
  179. * @param {Matrix4} transform The transformation matrix.
  180. * @param {Plane} [result] The object into which to store the result.
  181. * @returns {Plane} The plane transformed by the given transformation matrix.
  182. */
  183. Plane.transform = function (plane, transform, result) {
  184. //>>includeStart('debug', pragmas.debug);
  185. Check.Check.typeOf.object("plane", plane);
  186. Check.Check.typeOf.object("transform", transform);
  187. //>>includeEnd('debug');
  188. const normal = plane.normal;
  189. const distance = plane.distance;
  190. const inverseTranspose = Matrix2.Matrix4.inverseTranspose(
  191. transform,
  192. scratchInverseTranspose
  193. );
  194. let planeAsCartesian4 = Matrix2.Cartesian4.fromElements(
  195. normal.x,
  196. normal.y,
  197. normal.z,
  198. distance,
  199. scratchPlaneCartesian4
  200. );
  201. planeAsCartesian4 = Matrix2.Matrix4.multiplyByVector(
  202. inverseTranspose,
  203. planeAsCartesian4,
  204. planeAsCartesian4
  205. );
  206. // Convert the transformed plane to Hessian Normal Form
  207. const transformedNormal = Matrix3.Cartesian3.fromCartesian4(
  208. planeAsCartesian4,
  209. scratchTransformNormal
  210. );
  211. planeAsCartesian4 = Matrix2.Cartesian4.divideByScalar(
  212. planeAsCartesian4,
  213. Matrix3.Cartesian3.magnitude(transformedNormal),
  214. planeAsCartesian4
  215. );
  216. return Plane.fromCartesian4(planeAsCartesian4, result);
  217. };
  218. /**
  219. * Duplicates a Plane instance.
  220. *
  221. * @param {Plane} plane The plane to duplicate.
  222. * @param {Plane} [result] The object onto which to store the result.
  223. * @returns {Plane} The modified result parameter or a new Plane instance if one was not provided.
  224. */
  225. Plane.clone = function (plane, result) {
  226. //>>includeStart('debug', pragmas.debug);
  227. Check.Check.typeOf.object("plane", plane);
  228. //>>includeEnd('debug');
  229. if (!defaultValue.defined(result)) {
  230. return new Plane(plane.normal, plane.distance);
  231. }
  232. Matrix3.Cartesian3.clone(plane.normal, result.normal);
  233. result.distance = plane.distance;
  234. return result;
  235. };
  236. /**
  237. * Compares the provided Planes by normal and distance and returns
  238. * <code>true</code> if they are equal, <code>false</code> otherwise.
  239. *
  240. * @param {Plane} left The first plane.
  241. * @param {Plane} right The second plane.
  242. * @returns {boolean} <code>true</code> if left and right are equal, <code>false</code> otherwise.
  243. */
  244. Plane.equals = function (left, right) {
  245. //>>includeStart('debug', pragmas.debug);
  246. Check.Check.typeOf.object("left", left);
  247. Check.Check.typeOf.object("right", right);
  248. //>>includeEnd('debug');
  249. return (
  250. left.distance === right.distance &&
  251. Matrix3.Cartesian3.equals(left.normal, right.normal)
  252. );
  253. };
  254. /**
  255. * A constant initialized to the XY plane passing through the origin, with normal in positive Z.
  256. *
  257. * @type {Plane}
  258. * @constant
  259. */
  260. Plane.ORIGIN_XY_PLANE = Object.freeze(new Plane(Matrix3.Cartesian3.UNIT_Z, 0.0));
  261. /**
  262. * A constant initialized to the YZ plane passing through the origin, with normal in positive X.
  263. *
  264. * @type {Plane}
  265. * @constant
  266. */
  267. Plane.ORIGIN_YZ_PLANE = Object.freeze(new Plane(Matrix3.Cartesian3.UNIT_X, 0.0));
  268. /**
  269. * A constant initialized to the ZX plane passing through the origin, with normal in positive Y.
  270. *
  271. * @type {Plane}
  272. * @constant
  273. */
  274. Plane.ORIGIN_ZX_PLANE = Object.freeze(new Plane(Matrix3.Cartesian3.UNIT_Y, 0.0));
  275. exports.Plane = Plane;
  276. }));