Plane-ac6a1d3e.js 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /* This file is automatically rebuilt by the Cesium build process. */
  2. define(['exports', './when-e6985d2a', './Check-24cae389', './Math-392d0035', './Cartesian2-a5d6dde9', './Transforms-81680c41'], function (exports, when, Check, _Math, Cartesian2, Transforms) { '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. * var 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. Check.Check.typeOf.object("normal", normal);
  31. if (
  32. !_Math.CesiumMath.equalsEpsilon(
  33. Cartesian2.Cartesian3.magnitude(normal),
  34. 1.0,
  35. _Math.CesiumMath.EPSILON6
  36. )
  37. ) {
  38. throw new Check.DeveloperError("normal must be normalized.");
  39. }
  40. Check.Check.typeOf.number("distance", distance);
  41. //>>includeEnd('debug');
  42. /**
  43. * The plane's normal.
  44. *
  45. * @type {Cartesian3}
  46. */
  47. this.normal = Cartesian2.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. * var point = Cesium.Cartesian3.fromDegrees(-72.0, 40.0);
  69. * var normal = ellipsoid.geodeticSurfaceNormal(point);
  70. * var 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. Check.Check.typeOf.object("point", point);
  77. Check.Check.typeOf.object("normal", normal);
  78. if (
  79. !_Math.CesiumMath.equalsEpsilon(
  80. Cartesian2.Cartesian3.magnitude(normal),
  81. 1.0,
  82. _Math.CesiumMath.EPSILON6
  83. )
  84. ) {
  85. throw new Check.DeveloperError("normal must be normalized.");
  86. }
  87. //>>includeEnd('debug');
  88. var distance = -Cartesian2.Cartesian3.dot(normal, point);
  89. if (!when.defined(result)) {
  90. return new Plane(normal, distance);
  91. }
  92. Cartesian2.Cartesian3.clone(normal, result.normal);
  93. result.distance = distance;
  94. return result;
  95. };
  96. var scratchNormal = new Cartesian2.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. Check.Check.typeOf.object("coefficients", coefficients);
  109. //>>includeEnd('debug');
  110. var normal = Cartesian2.Cartesian3.fromCartesian4(coefficients, scratchNormal);
  111. var distance = coefficients.w;
  112. //>>includeStart('debug', pragmas.debug);
  113. if (
  114. !_Math.CesiumMath.equalsEpsilon(
  115. Cartesian2.Cartesian3.magnitude(normal),
  116. 1.0,
  117. _Math.CesiumMath.EPSILON6
  118. )
  119. ) {
  120. throw new Check.DeveloperError("normal must be normalized.");
  121. }
  122. //>>includeEnd('debug');
  123. if (!when.defined(result)) {
  124. return new Plane(normal, distance);
  125. }
  126. Cartesian2.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. Check.Check.typeOf.object("plane", plane);
  144. Check.Check.typeOf.object("point", point);
  145. //>>includeEnd('debug');
  146. return Cartesian2.Cartesian3.dot(plane.normal, point) + plane.distance;
  147. };
  148. var scratchCartesian = new Cartesian2.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. Check.Check.typeOf.object("plane", plane);
  159. Check.Check.typeOf.object("point", point);
  160. //>>includeEnd('debug');
  161. if (!when.defined(result)) {
  162. result = new Cartesian2.Cartesian3();
  163. }
  164. // projectedPoint = point - (normal.point + scale) * normal
  165. var pointDistance = Plane.getPointDistance(plane, point);
  166. var scaledNormal = Cartesian2.Cartesian3.multiplyByScalar(
  167. plane.normal,
  168. pointDistance,
  169. scratchCartesian
  170. );
  171. return Cartesian2.Cartesian3.subtract(point, scaledNormal, result);
  172. };
  173. var scratchPosition = new Cartesian2.Cartesian3();
  174. /**
  175. * Transforms the plane by the given transformation matrix.
  176. *
  177. * @param {Plane} plane The plane.
  178. * @param {Matrix4} transform The transformation matrix.
  179. * @param {Plane} [result] The object into which to store the result.
  180. * @returns {Plane} The plane transformed by the given transformation matrix.
  181. */
  182. Plane.transform = function (plane, transform, result) {
  183. //>>includeStart('debug', pragmas.debug);
  184. Check.Check.typeOf.object("plane", plane);
  185. Check.Check.typeOf.object("transform", transform);
  186. //>>includeEnd('debug');
  187. Transforms.Matrix4.multiplyByPointAsVector(transform, plane.normal, scratchNormal);
  188. Cartesian2.Cartesian3.normalize(scratchNormal, scratchNormal);
  189. Cartesian2.Cartesian3.multiplyByScalar(plane.normal, -plane.distance, scratchPosition);
  190. Transforms.Matrix4.multiplyByPoint(transform, scratchPosition, scratchPosition);
  191. return Plane.fromPointNormal(scratchPosition, scratchNormal, result);
  192. };
  193. /**
  194. * Duplicates a Plane instance.
  195. *
  196. * @param {Plane} plane The plane to duplicate.
  197. * @param {Plane} [result] The object onto which to store the result.
  198. * @returns {Plane} The modified result parameter or a new Plane instance if one was not provided.
  199. */
  200. Plane.clone = function (plane, result) {
  201. //>>includeStart('debug', pragmas.debug);
  202. Check.Check.typeOf.object("plane", plane);
  203. //>>includeEnd('debug');
  204. if (!when.defined(result)) {
  205. return new Plane(plane.normal, plane.distance);
  206. }
  207. Cartesian2.Cartesian3.clone(plane.normal, result.normal);
  208. result.distance = plane.distance;
  209. return result;
  210. };
  211. /**
  212. * Compares the provided Planes by normal and distance and returns
  213. * <code>true</code> if they are equal, <code>false</code> otherwise.
  214. *
  215. * @param {Plane} left The first plane.
  216. * @param {Plane} right The second plane.
  217. * @returns {Boolean} <code>true</code> if left and right are equal, <code>false</code> otherwise.
  218. */
  219. Plane.equals = function (left, right) {
  220. //>>includeStart('debug', pragmas.debug);
  221. Check.Check.typeOf.object("left", left);
  222. Check.Check.typeOf.object("right", right);
  223. //>>includeEnd('debug');
  224. return (
  225. left.distance === right.distance &&
  226. Cartesian2.Cartesian3.equals(left.normal, right.normal)
  227. );
  228. };
  229. /**
  230. * A constant initialized to the XY plane passing through the origin, with normal in positive Z.
  231. *
  232. * @type {Plane}
  233. * @constant
  234. */
  235. Plane.ORIGIN_XY_PLANE = Object.freeze(new Plane(Cartesian2.Cartesian3.UNIT_Z, 0.0));
  236. /**
  237. * A constant initialized to the YZ plane passing through the origin, with normal in positive X.
  238. *
  239. * @type {Plane}
  240. * @constant
  241. */
  242. Plane.ORIGIN_YZ_PLANE = Object.freeze(new Plane(Cartesian2.Cartesian3.UNIT_X, 0.0));
  243. /**
  244. * A constant initialized to the ZX plane passing through the origin, with normal in positive Y.
  245. *
  246. * @type {Plane}
  247. * @constant
  248. */
  249. Plane.ORIGIN_ZX_PLANE = Object.freeze(new Plane(Cartesian2.Cartesian3.UNIT_Y, 0.0));
  250. exports.Plane = Plane;
  251. });