Plane-24f22488.js 11 KB

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