Plane-76b84425.js 11 KB

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