Plane-e20fba8c.js 10 KB

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