Plane-8390418f.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. /**
  2. * Cesium - https://github.com/AnalyticalGraphicsInc/cesium
  3. *
  4. * Copyright 2011-2017 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/AnalyticalGraphicsInc/cesium/blob/master/LICENSE.md for full licensing details.
  22. */
  23. define(['exports', './when-8d13db60', './Check-70bec281', './Math-61ede240', './Cartographic-fe4be337', './BoundingSphere-775c5788'], function (exports, when, Check, _Math, Cartographic, BoundingSphere) { '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. * var 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. Check.Check.typeOf.object('normal', normal);
  52. if (!_Math.CesiumMath.equalsEpsilon(Cartographic.Cartesian3.magnitude(normal), 1.0, _Math.CesiumMath.EPSILON6)) {
  53. throw new Check.DeveloperError('normal must be normalized.');
  54. }
  55. Check.Check.typeOf.number('distance', distance);
  56. //>>includeEnd('debug');
  57. /**
  58. * The plane's normal.
  59. *
  60. * @type {Cartesian3}
  61. */
  62. this.normal = Cartographic.Cartesian3.clone(normal);
  63. /**
  64. * The shortest distance from the origin to the plane. The sign of
  65. * <code>distance</code> determines which side of the plane the origin
  66. * is on. If <code>distance</code> is positive, the origin is in the half-space
  67. * in the direction of the normal; if negative, the origin is in the half-space
  68. * opposite to the normal; if zero, the plane passes through the origin.
  69. *
  70. * @type {Number}
  71. */
  72. this.distance = distance;
  73. }
  74. /**
  75. * Creates a plane from a normal and a point on the plane.
  76. *
  77. * @param {Cartesian3} point The point on the plane.
  78. * @param {Cartesian3} normal The plane's normal (normalized).
  79. * @param {Plane} [result] The object onto which to store the result.
  80. * @returns {Plane} A new plane instance or the modified result parameter.
  81. *
  82. * @example
  83. * var point = Cesium.Cartesian3.fromDegrees(-72.0, 40.0);
  84. * var normal = ellipsoid.geodeticSurfaceNormal(point);
  85. * var tangentPlane = Cesium.Plane.fromPointNormal(point, normal);
  86. *
  87. * @exception {DeveloperError} Normal must be normalized
  88. */
  89. Plane.fromPointNormal = function(point, normal, result) {
  90. //>>includeStart('debug', pragmas.debug);
  91. Check.Check.typeOf.object('point', point);
  92. Check.Check.typeOf.object('normal', normal);
  93. if (!_Math.CesiumMath.equalsEpsilon(Cartographic.Cartesian3.magnitude(normal), 1.0, _Math.CesiumMath.EPSILON6)) {
  94. throw new Check.DeveloperError('normal must be normalized.');
  95. }
  96. //>>includeEnd('debug');
  97. var distance = -Cartographic.Cartesian3.dot(normal, point);
  98. if (!when.defined(result)) {
  99. return new Plane(normal, distance);
  100. }
  101. Cartographic.Cartesian3.clone(normal, result.normal);
  102. result.distance = distance;
  103. return result;
  104. };
  105. var scratchNormal = new Cartographic.Cartesian3();
  106. /**
  107. * Creates a plane from the general equation
  108. *
  109. * @param {Cartesian4} coefficients The plane's normal (normalized).
  110. * @param {Plane} [result] The object onto which to store the result.
  111. * @returns {Plane} A new plane instance or the modified result parameter.
  112. *
  113. * @exception {DeveloperError} Normal must be normalized
  114. */
  115. Plane.fromCartesian4 = function(coefficients, result) {
  116. //>>includeStart('debug', pragmas.debug);
  117. Check.Check.typeOf.object('coefficients', coefficients);
  118. //>>includeEnd('debug');
  119. var normal = Cartographic.Cartesian3.fromCartesian4(coefficients, scratchNormal);
  120. var distance = coefficients.w;
  121. //>>includeStart('debug', pragmas.debug);
  122. if (!_Math.CesiumMath.equalsEpsilon(Cartographic.Cartesian3.magnitude(normal), 1.0, _Math.CesiumMath.EPSILON6)) {
  123. throw new Check.DeveloperError('normal must be normalized.');
  124. }
  125. //>>includeEnd('debug');
  126. if (!when.defined(result)) {
  127. return new Plane(normal, distance);
  128. }
  129. Cartographic.Cartesian3.clone(normal, result.normal);
  130. result.distance = distance;
  131. return result;
  132. };
  133. /**
  134. * Computes the signed shortest distance of a point to a plane.
  135. * The sign of the distance determines which side of the plane the point
  136. * is on. If the distance is positive, the point is in the half-space
  137. * in the direction of the normal; if negative, the point is in the half-space
  138. * opposite to the normal; if zero, the plane passes through the point.
  139. *
  140. * @param {Plane} plane The plane.
  141. * @param {Cartesian3} point The point.
  142. * @returns {Number} The signed shortest distance of the point to the plane.
  143. */
  144. Plane.getPointDistance = function(plane, point) {
  145. //>>includeStart('debug', pragmas.debug);
  146. Check.Check.typeOf.object('plane', plane);
  147. Check.Check.typeOf.object('point', point);
  148. //>>includeEnd('debug');
  149. return Cartographic.Cartesian3.dot(plane.normal, point) + plane.distance;
  150. };
  151. var scratchCartesian = new Cartographic.Cartesian3();
  152. /**
  153. * Projects a point onto the plane.
  154. * @param {Plane} plane The plane to project the point onto
  155. * @param {Cartesian3} point The point to project onto the plane
  156. * @param {Cartesian3} [result] The result point. If undefined, a new Cartesian3 will be created.
  157. * @returns {Cartesian3} The modified result parameter or a new Cartesian3 instance if one was not provided.
  158. */
  159. Plane.projectPointOntoPlane = function(plane, point, result) {
  160. //>>includeStart('debug', pragmas.debug);
  161. Check.Check.typeOf.object('plane', plane);
  162. Check.Check.typeOf.object('point', point);
  163. //>>includeEnd('debug');
  164. if (!when.defined(result)) {
  165. result = new Cartographic.Cartesian3();
  166. }
  167. // projectedPoint = point - (normal.point + scale) * normal
  168. var pointDistance = Plane.getPointDistance(plane, point);
  169. var scaledNormal = Cartographic.Cartesian3.multiplyByScalar(plane.normal, pointDistance, scratchCartesian);
  170. return Cartographic.Cartesian3.subtract(point, scaledNormal, result);
  171. };
  172. var scratchPosition = new Cartographic.Cartesian3();
  173. /**
  174. * Transforms the plane by the given transformation matrix.
  175. *
  176. * @param {Plane} plane The plane.
  177. * @param {Matrix4} transform The transformation matrix.
  178. * @param {Plane} [result] The object into which to store the result.
  179. * @returns {Plane} The plane transformed by the given transformation matrix.
  180. */
  181. Plane.transform = function(plane, transform, result) {
  182. //>>includeStart('debug', pragmas.debug);
  183. Check.Check.typeOf.object('plane', plane);
  184. Check.Check.typeOf.object('transform', transform);
  185. //>>includeEnd('debug');
  186. BoundingSphere.Matrix4.multiplyByPointAsVector(transform, plane.normal, scratchNormal);
  187. Cartographic.Cartesian3.normalize(scratchNormal, scratchNormal);
  188. Cartographic.Cartesian3.multiplyByScalar(plane.normal, -plane.distance, scratchPosition);
  189. BoundingSphere.Matrix4.multiplyByPoint(transform, scratchPosition, scratchPosition);
  190. return Plane.fromPointNormal(scratchPosition, scratchNormal, result);
  191. };
  192. /**
  193. * Duplicates a Plane instance.
  194. *
  195. * @param {Plane} plane The plane to duplicate.
  196. * @param {Plane} [result] The object onto which to store the result.
  197. * @returns {Plane} The modified result parameter or a new Plane instance if one was not provided.
  198. */
  199. Plane.clone = function(plane, result) {
  200. //>>includeStart('debug', pragmas.debug);
  201. Check.Check.typeOf.object('plane', plane);
  202. //>>includeEnd('debug');
  203. if (!when.defined(result)) {
  204. return new Plane(plane.normal, plane.distance);
  205. }
  206. Cartographic.Cartesian3.clone(plane.normal, result.normal);
  207. result.distance = plane.distance;
  208. return result;
  209. };
  210. /**
  211. * Compares the provided Planes by normal and distance and returns
  212. * <code>true</code> if they are equal, <code>false</code> otherwise.
  213. *
  214. * @param {Plane} left The first plane.
  215. * @param {Plane} right The second plane.
  216. * @returns {Boolean} <code>true</code> if left and right are equal, <code>false</code> otherwise.
  217. */
  218. Plane.equals = function(left, right) {
  219. //>>includeStart('debug', pragmas.debug);
  220. Check.Check.typeOf.object('left', left);
  221. Check.Check.typeOf.object('right', right);
  222. //>>includeEnd('debug');
  223. return (left.distance === right.distance) && Cartographic.Cartesian3.equals(left.normal, right.normal);
  224. };
  225. /**
  226. * A constant initialized to the XY plane passing through the origin, with normal in positive Z.
  227. *
  228. * @type {Plane}
  229. * @constant
  230. */
  231. Plane.ORIGIN_XY_PLANE = Object.freeze(new Plane(Cartographic.Cartesian3.UNIT_Z, 0.0));
  232. /**
  233. * A constant initialized to the YZ plane passing through the origin, with normal in positive X.
  234. *
  235. * @type {Plane}
  236. * @constant
  237. */
  238. Plane.ORIGIN_YZ_PLANE = Object.freeze(new Plane(Cartographic.Cartesian3.UNIT_X, 0.0));
  239. /**
  240. * A constant initialized to the ZX plane passing through the origin, with normal in positive Y.
  241. *
  242. * @type {Plane}
  243. * @constant
  244. */
  245. Plane.ORIGIN_ZX_PLANE = Object.freeze(new Plane(Cartographic.Cartesian3.UNIT_Y, 0.0));
  246. exports.Plane = Plane;
  247. });