Plane.js 9.3 KB

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