Plane.js 8.7 KB

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