IntersectionTests.js 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032
  1. import Cartesian3 from "./Cartesian3.js";
  2. import Cartographic from "./Cartographic.js";
  3. import defaultValue from "./defaultValue.js";
  4. import defined from "./defined.js";
  5. import DeveloperError from "./DeveloperError.js";
  6. import Interval from "./Interval.js";
  7. import CesiumMath from "./Math.js";
  8. import Matrix3 from "./Matrix3.js";
  9. import QuadraticRealPolynomial from "./QuadraticRealPolynomial.js";
  10. import QuarticRealPolynomial from "./QuarticRealPolynomial.js";
  11. import Ray from "./Ray.js";
  12. /**
  13. * Functions for computing the intersection between geometries such as rays, planes, triangles, and ellipsoids.
  14. *
  15. * @namespace IntersectionTests
  16. */
  17. var IntersectionTests = {};
  18. /**
  19. * Computes the intersection of a ray and a plane.
  20. *
  21. * @param {Ray} ray The ray.
  22. * @param {Plane} plane The plane.
  23. * @param {Cartesian3} [result] The object onto which to store the result.
  24. * @returns {Cartesian3} The intersection point or undefined if there is no intersections.
  25. */
  26. IntersectionTests.rayPlane = function (ray, plane, result) {
  27. //>>includeStart('debug', pragmas.debug);
  28. if (!defined(ray)) {
  29. throw new DeveloperError("ray is required.");
  30. }
  31. if (!defined(plane)) {
  32. throw new DeveloperError("plane is required.");
  33. }
  34. //>>includeEnd('debug');
  35. if (!defined(result)) {
  36. result = new Cartesian3();
  37. }
  38. var origin = ray.origin;
  39. var direction = ray.direction;
  40. var normal = plane.normal;
  41. var denominator = Cartesian3.dot(normal, direction);
  42. if (Math.abs(denominator) < CesiumMath.EPSILON15) {
  43. // Ray is parallel to plane. The ray may be in the polygon's plane.
  44. return undefined;
  45. }
  46. var t = (-plane.distance - Cartesian3.dot(normal, origin)) / denominator;
  47. if (t < 0) {
  48. return undefined;
  49. }
  50. result = Cartesian3.multiplyByScalar(direction, t, result);
  51. return Cartesian3.add(origin, result, result);
  52. };
  53. var scratchEdge0 = new Cartesian3();
  54. var scratchEdge1 = new Cartesian3();
  55. var scratchPVec = new Cartesian3();
  56. var scratchTVec = new Cartesian3();
  57. var scratchQVec = new Cartesian3();
  58. /**
  59. * Computes the intersection of a ray and a triangle as a parametric distance along the input ray. The result is negative when the triangle is behind the ray.
  60. *
  61. * Implements {@link https://cadxfem.org/inf/Fast%20MinimumStorage%20RayTriangle%20Intersection.pdf|
  62. * Fast Minimum Storage Ray/Triangle Intersection} by Tomas Moller and Ben Trumbore.
  63. *
  64. * @memberof IntersectionTests
  65. *
  66. * @param {Ray} ray The ray.
  67. * @param {Cartesian3} p0 The first vertex of the triangle.
  68. * @param {Cartesian3} p1 The second vertex of the triangle.
  69. * @param {Cartesian3} p2 The third vertex of the triangle.
  70. * @param {Boolean} [cullBackFaces=false] If <code>true</code>, will only compute an intersection with the front face of the triangle
  71. * and return undefined for intersections with the back face.
  72. * @returns {Number} The intersection as a parametric distance along the ray, or undefined if there is no intersection.
  73. */
  74. IntersectionTests.rayTriangleParametric = function (
  75. ray,
  76. p0,
  77. p1,
  78. p2,
  79. cullBackFaces
  80. ) {
  81. //>>includeStart('debug', pragmas.debug);
  82. if (!defined(ray)) {
  83. throw new DeveloperError("ray is required.");
  84. }
  85. if (!defined(p0)) {
  86. throw new DeveloperError("p0 is required.");
  87. }
  88. if (!defined(p1)) {
  89. throw new DeveloperError("p1 is required.");
  90. }
  91. if (!defined(p2)) {
  92. throw new DeveloperError("p2 is required.");
  93. }
  94. //>>includeEnd('debug');
  95. cullBackFaces = defaultValue(cullBackFaces, false);
  96. var origin = ray.origin;
  97. var direction = ray.direction;
  98. var edge0 = Cartesian3.subtract(p1, p0, scratchEdge0);
  99. var edge1 = Cartesian3.subtract(p2, p0, scratchEdge1);
  100. var p = Cartesian3.cross(direction, edge1, scratchPVec);
  101. var det = Cartesian3.dot(edge0, p);
  102. var tvec;
  103. var q;
  104. var u;
  105. var v;
  106. var t;
  107. if (cullBackFaces) {
  108. if (det < CesiumMath.EPSILON6) {
  109. return undefined;
  110. }
  111. tvec = Cartesian3.subtract(origin, p0, scratchTVec);
  112. u = Cartesian3.dot(tvec, p);
  113. if (u < 0.0 || u > det) {
  114. return undefined;
  115. }
  116. q = Cartesian3.cross(tvec, edge0, scratchQVec);
  117. v = Cartesian3.dot(direction, q);
  118. if (v < 0.0 || u + v > det) {
  119. return undefined;
  120. }
  121. t = Cartesian3.dot(edge1, q) / det;
  122. } else {
  123. if (Math.abs(det) < CesiumMath.EPSILON6) {
  124. return undefined;
  125. }
  126. var invDet = 1.0 / det;
  127. tvec = Cartesian3.subtract(origin, p0, scratchTVec);
  128. u = Cartesian3.dot(tvec, p) * invDet;
  129. if (u < 0.0 || u > 1.0) {
  130. return undefined;
  131. }
  132. q = Cartesian3.cross(tvec, edge0, scratchQVec);
  133. v = Cartesian3.dot(direction, q) * invDet;
  134. if (v < 0.0 || u + v > 1.0) {
  135. return undefined;
  136. }
  137. t = Cartesian3.dot(edge1, q) * invDet;
  138. }
  139. return t;
  140. };
  141. /**
  142. * Computes the intersection of a ray and a triangle as a Cartesian3 coordinate.
  143. *
  144. * Implements {@link https://cadxfem.org/inf/Fast%20MinimumStorage%20RayTriangle%20Intersection.pdf|
  145. * Fast Minimum Storage Ray/Triangle Intersection} by Tomas Moller and Ben Trumbore.
  146. *
  147. * @memberof IntersectionTests
  148. *
  149. * @param {Ray} ray The ray.
  150. * @param {Cartesian3} p0 The first vertex of the triangle.
  151. * @param {Cartesian3} p1 The second vertex of the triangle.
  152. * @param {Cartesian3} p2 The third vertex of the triangle.
  153. * @param {Boolean} [cullBackFaces=false] If <code>true</code>, will only compute an intersection with the front face of the triangle
  154. * and return undefined for intersections with the back face.
  155. * @param {Cartesian3} [result] The <code>Cartesian3</code> onto which to store the result.
  156. * @returns {Cartesian3} The intersection point or undefined if there is no intersections.
  157. */
  158. IntersectionTests.rayTriangle = function (
  159. ray,
  160. p0,
  161. p1,
  162. p2,
  163. cullBackFaces,
  164. result
  165. ) {
  166. var t = IntersectionTests.rayTriangleParametric(
  167. ray,
  168. p0,
  169. p1,
  170. p2,
  171. cullBackFaces
  172. );
  173. if (!defined(t) || t < 0.0) {
  174. return undefined;
  175. }
  176. if (!defined(result)) {
  177. result = new Cartesian3();
  178. }
  179. Cartesian3.multiplyByScalar(ray.direction, t, result);
  180. return Cartesian3.add(ray.origin, result, result);
  181. };
  182. var scratchLineSegmentTriangleRay = new Ray();
  183. /**
  184. * Computes the intersection of a line segment and a triangle.
  185. * @memberof IntersectionTests
  186. *
  187. * @param {Cartesian3} v0 The an end point of the line segment.
  188. * @param {Cartesian3} v1 The other end point of the line segment.
  189. * @param {Cartesian3} p0 The first vertex of the triangle.
  190. * @param {Cartesian3} p1 The second vertex of the triangle.
  191. * @param {Cartesian3} p2 The third vertex of the triangle.
  192. * @param {Boolean} [cullBackFaces=false] If <code>true</code>, will only compute an intersection with the front face of the triangle
  193. * and return undefined for intersections with the back face.
  194. * @param {Cartesian3} [result] The <code>Cartesian3</code> onto which to store the result.
  195. * @returns {Cartesian3} The intersection point or undefined if there is no intersections.
  196. */
  197. IntersectionTests.lineSegmentTriangle = function (
  198. v0,
  199. v1,
  200. p0,
  201. p1,
  202. p2,
  203. cullBackFaces,
  204. result
  205. ) {
  206. //>>includeStart('debug', pragmas.debug);
  207. if (!defined(v0)) {
  208. throw new DeveloperError("v0 is required.");
  209. }
  210. if (!defined(v1)) {
  211. throw new DeveloperError("v1 is required.");
  212. }
  213. if (!defined(p0)) {
  214. throw new DeveloperError("p0 is required.");
  215. }
  216. if (!defined(p1)) {
  217. throw new DeveloperError("p1 is required.");
  218. }
  219. if (!defined(p2)) {
  220. throw new DeveloperError("p2 is required.");
  221. }
  222. //>>includeEnd('debug');
  223. var ray = scratchLineSegmentTriangleRay;
  224. Cartesian3.clone(v0, ray.origin);
  225. Cartesian3.subtract(v1, v0, ray.direction);
  226. Cartesian3.normalize(ray.direction, ray.direction);
  227. var t = IntersectionTests.rayTriangleParametric(
  228. ray,
  229. p0,
  230. p1,
  231. p2,
  232. cullBackFaces
  233. );
  234. if (!defined(t) || t < 0.0 || t > Cartesian3.distance(v0, v1)) {
  235. return undefined;
  236. }
  237. if (!defined(result)) {
  238. result = new Cartesian3();
  239. }
  240. Cartesian3.multiplyByScalar(ray.direction, t, result);
  241. return Cartesian3.add(ray.origin, result, result);
  242. };
  243. function solveQuadratic(a, b, c, result) {
  244. var det = b * b - 4.0 * a * c;
  245. if (det < 0.0) {
  246. return undefined;
  247. } else if (det > 0.0) {
  248. var denom = 1.0 / (2.0 * a);
  249. var disc = Math.sqrt(det);
  250. var root0 = (-b + disc) * denom;
  251. var root1 = (-b - disc) * denom;
  252. if (root0 < root1) {
  253. result.root0 = root0;
  254. result.root1 = root1;
  255. } else {
  256. result.root0 = root1;
  257. result.root1 = root0;
  258. }
  259. return result;
  260. }
  261. var root = -b / (2.0 * a);
  262. if (root === 0.0) {
  263. return undefined;
  264. }
  265. result.root0 = result.root1 = root;
  266. return result;
  267. }
  268. var raySphereRoots = {
  269. root0: 0.0,
  270. root1: 0.0,
  271. };
  272. function raySphere(ray, sphere, result) {
  273. if (!defined(result)) {
  274. result = new Interval();
  275. }
  276. var origin = ray.origin;
  277. var direction = ray.direction;
  278. var center = sphere.center;
  279. var radiusSquared = sphere.radius * sphere.radius;
  280. var diff = Cartesian3.subtract(origin, center, scratchPVec);
  281. var a = Cartesian3.dot(direction, direction);
  282. var b = 2.0 * Cartesian3.dot(direction, diff);
  283. var c = Cartesian3.magnitudeSquared(diff) - radiusSquared;
  284. var roots = solveQuadratic(a, b, c, raySphereRoots);
  285. if (!defined(roots)) {
  286. return undefined;
  287. }
  288. result.start = roots.root0;
  289. result.stop = roots.root1;
  290. return result;
  291. }
  292. /**
  293. * Computes the intersection points of a ray with a sphere.
  294. * @memberof IntersectionTests
  295. *
  296. * @param {Ray} ray The ray.
  297. * @param {BoundingSphere} sphere The sphere.
  298. * @param {Interval} [result] The result onto which to store the result.
  299. * @returns {Interval} The interval containing scalar points along the ray or undefined if there are no intersections.
  300. */
  301. IntersectionTests.raySphere = function (ray, sphere, result) {
  302. //>>includeStart('debug', pragmas.debug);
  303. if (!defined(ray)) {
  304. throw new DeveloperError("ray is required.");
  305. }
  306. if (!defined(sphere)) {
  307. throw new DeveloperError("sphere is required.");
  308. }
  309. //>>includeEnd('debug');
  310. result = raySphere(ray, sphere, result);
  311. if (!defined(result) || result.stop < 0.0) {
  312. return undefined;
  313. }
  314. result.start = Math.max(result.start, 0.0);
  315. return result;
  316. };
  317. var scratchLineSegmentRay = new Ray();
  318. /**
  319. * Computes the intersection points of a line segment with a sphere.
  320. * @memberof IntersectionTests
  321. *
  322. * @param {Cartesian3} p0 An end point of the line segment.
  323. * @param {Cartesian3} p1 The other end point of the line segment.
  324. * @param {BoundingSphere} sphere The sphere.
  325. * @param {Interval} [result] The result onto which to store the result.
  326. * @returns {Interval} The interval containing scalar points along the ray or undefined if there are no intersections.
  327. */
  328. IntersectionTests.lineSegmentSphere = function (p0, p1, sphere, result) {
  329. //>>includeStart('debug', pragmas.debug);
  330. if (!defined(p0)) {
  331. throw new DeveloperError("p0 is required.");
  332. }
  333. if (!defined(p1)) {
  334. throw new DeveloperError("p1 is required.");
  335. }
  336. if (!defined(sphere)) {
  337. throw new DeveloperError("sphere is required.");
  338. }
  339. //>>includeEnd('debug');
  340. var ray = scratchLineSegmentRay;
  341. Cartesian3.clone(p0, ray.origin);
  342. var direction = Cartesian3.subtract(p1, p0, ray.direction);
  343. var maxT = Cartesian3.magnitude(direction);
  344. Cartesian3.normalize(direction, direction);
  345. result = raySphere(ray, sphere, result);
  346. if (!defined(result) || result.stop < 0.0 || result.start > maxT) {
  347. return undefined;
  348. }
  349. result.start = Math.max(result.start, 0.0);
  350. result.stop = Math.min(result.stop, maxT);
  351. return result;
  352. };
  353. var scratchQ = new Cartesian3();
  354. var scratchW = new Cartesian3();
  355. /**
  356. * Computes the intersection points of a ray with an ellipsoid.
  357. *
  358. * @param {Ray} ray The ray.
  359. * @param {Ellipsoid} ellipsoid The ellipsoid.
  360. * @returns {Interval} The interval containing scalar points along the ray or undefined if there are no intersections.
  361. */
  362. IntersectionTests.rayEllipsoid = function (ray, ellipsoid) {
  363. //>>includeStart('debug', pragmas.debug);
  364. if (!defined(ray)) {
  365. throw new DeveloperError("ray is required.");
  366. }
  367. if (!defined(ellipsoid)) {
  368. throw new DeveloperError("ellipsoid is required.");
  369. }
  370. //>>includeEnd('debug');
  371. var inverseRadii = ellipsoid.oneOverRadii;
  372. var q = Cartesian3.multiplyComponents(inverseRadii, ray.origin, scratchQ);
  373. var w = Cartesian3.multiplyComponents(inverseRadii, ray.direction, scratchW);
  374. var q2 = Cartesian3.magnitudeSquared(q);
  375. var qw = Cartesian3.dot(q, w);
  376. var difference, w2, product, discriminant, temp;
  377. if (q2 > 1.0) {
  378. // Outside ellipsoid.
  379. if (qw >= 0.0) {
  380. // Looking outward or tangent (0 intersections).
  381. return undefined;
  382. }
  383. // qw < 0.0.
  384. var qw2 = qw * qw;
  385. difference = q2 - 1.0; // Positively valued.
  386. w2 = Cartesian3.magnitudeSquared(w);
  387. product = w2 * difference;
  388. if (qw2 < product) {
  389. // Imaginary roots (0 intersections).
  390. return undefined;
  391. } else if (qw2 > product) {
  392. // Distinct roots (2 intersections).
  393. discriminant = qw * qw - product;
  394. temp = -qw + Math.sqrt(discriminant); // Avoid cancellation.
  395. var root0 = temp / w2;
  396. var root1 = difference / temp;
  397. if (root0 < root1) {
  398. return new Interval(root0, root1);
  399. }
  400. return {
  401. start: root1,
  402. stop: root0,
  403. };
  404. }
  405. // qw2 == product. Repeated roots (2 intersections).
  406. var root = Math.sqrt(difference / w2);
  407. return new Interval(root, root);
  408. } else if (q2 < 1.0) {
  409. // Inside ellipsoid (2 intersections).
  410. difference = q2 - 1.0; // Negatively valued.
  411. w2 = Cartesian3.magnitudeSquared(w);
  412. product = w2 * difference; // Negatively valued.
  413. discriminant = qw * qw - product;
  414. temp = -qw + Math.sqrt(discriminant); // Positively valued.
  415. return new Interval(0.0, temp / w2);
  416. }
  417. // q2 == 1.0. On ellipsoid.
  418. if (qw < 0.0) {
  419. // Looking inward.
  420. w2 = Cartesian3.magnitudeSquared(w);
  421. return new Interval(0.0, -qw / w2);
  422. }
  423. // qw >= 0.0. Looking outward or tangent.
  424. return undefined;
  425. };
  426. function addWithCancellationCheck(left, right, tolerance) {
  427. var difference = left + right;
  428. if (
  429. CesiumMath.sign(left) !== CesiumMath.sign(right) &&
  430. Math.abs(difference / Math.max(Math.abs(left), Math.abs(right))) < tolerance
  431. ) {
  432. return 0.0;
  433. }
  434. return difference;
  435. }
  436. function quadraticVectorExpression(A, b, c, x, w) {
  437. var xSquared = x * x;
  438. var wSquared = w * w;
  439. var l2 = (A[Matrix3.COLUMN1ROW1] - A[Matrix3.COLUMN2ROW2]) * wSquared;
  440. var l1 =
  441. w *
  442. (x *
  443. addWithCancellationCheck(
  444. A[Matrix3.COLUMN1ROW0],
  445. A[Matrix3.COLUMN0ROW1],
  446. CesiumMath.EPSILON15
  447. ) +
  448. b.y);
  449. var l0 =
  450. A[Matrix3.COLUMN0ROW0] * xSquared +
  451. A[Matrix3.COLUMN2ROW2] * wSquared +
  452. x * b.x +
  453. c;
  454. var r1 =
  455. wSquared *
  456. addWithCancellationCheck(
  457. A[Matrix3.COLUMN2ROW1],
  458. A[Matrix3.COLUMN1ROW2],
  459. CesiumMath.EPSILON15
  460. );
  461. var r0 =
  462. w *
  463. (x *
  464. addWithCancellationCheck(A[Matrix3.COLUMN2ROW0], A[Matrix3.COLUMN0ROW2]) +
  465. b.z);
  466. var cosines;
  467. var solutions = [];
  468. if (r0 === 0.0 && r1 === 0.0) {
  469. cosines = QuadraticRealPolynomial.computeRealRoots(l2, l1, l0);
  470. if (cosines.length === 0) {
  471. return solutions;
  472. }
  473. var cosine0 = cosines[0];
  474. var sine0 = Math.sqrt(Math.max(1.0 - cosine0 * cosine0, 0.0));
  475. solutions.push(new Cartesian3(x, w * cosine0, w * -sine0));
  476. solutions.push(new Cartesian3(x, w * cosine0, w * sine0));
  477. if (cosines.length === 2) {
  478. var cosine1 = cosines[1];
  479. var sine1 = Math.sqrt(Math.max(1.0 - cosine1 * cosine1, 0.0));
  480. solutions.push(new Cartesian3(x, w * cosine1, w * -sine1));
  481. solutions.push(new Cartesian3(x, w * cosine1, w * sine1));
  482. }
  483. return solutions;
  484. }
  485. var r0Squared = r0 * r0;
  486. var r1Squared = r1 * r1;
  487. var l2Squared = l2 * l2;
  488. var r0r1 = r0 * r1;
  489. var c4 = l2Squared + r1Squared;
  490. var c3 = 2.0 * (l1 * l2 + r0r1);
  491. var c2 = 2.0 * l0 * l2 + l1 * l1 - r1Squared + r0Squared;
  492. var c1 = 2.0 * (l0 * l1 - r0r1);
  493. var c0 = l0 * l0 - r0Squared;
  494. if (c4 === 0.0 && c3 === 0.0 && c2 === 0.0 && c1 === 0.0) {
  495. return solutions;
  496. }
  497. cosines = QuarticRealPolynomial.computeRealRoots(c4, c3, c2, c1, c0);
  498. var length = cosines.length;
  499. if (length === 0) {
  500. return solutions;
  501. }
  502. for (var i = 0; i < length; ++i) {
  503. var cosine = cosines[i];
  504. var cosineSquared = cosine * cosine;
  505. var sineSquared = Math.max(1.0 - cosineSquared, 0.0);
  506. var sine = Math.sqrt(sineSquared);
  507. //var left = l2 * cosineSquared + l1 * cosine + l0;
  508. var left;
  509. if (CesiumMath.sign(l2) === CesiumMath.sign(l0)) {
  510. left = addWithCancellationCheck(
  511. l2 * cosineSquared + l0,
  512. l1 * cosine,
  513. CesiumMath.EPSILON12
  514. );
  515. } else if (CesiumMath.sign(l0) === CesiumMath.sign(l1 * cosine)) {
  516. left = addWithCancellationCheck(
  517. l2 * cosineSquared,
  518. l1 * cosine + l0,
  519. CesiumMath.EPSILON12
  520. );
  521. } else {
  522. left = addWithCancellationCheck(
  523. l2 * cosineSquared + l1 * cosine,
  524. l0,
  525. CesiumMath.EPSILON12
  526. );
  527. }
  528. var right = addWithCancellationCheck(r1 * cosine, r0, CesiumMath.EPSILON15);
  529. var product = left * right;
  530. if (product < 0.0) {
  531. solutions.push(new Cartesian3(x, w * cosine, w * sine));
  532. } else if (product > 0.0) {
  533. solutions.push(new Cartesian3(x, w * cosine, w * -sine));
  534. } else if (sine !== 0.0) {
  535. solutions.push(new Cartesian3(x, w * cosine, w * -sine));
  536. solutions.push(new Cartesian3(x, w * cosine, w * sine));
  537. ++i;
  538. } else {
  539. solutions.push(new Cartesian3(x, w * cosine, w * sine));
  540. }
  541. }
  542. return solutions;
  543. }
  544. var firstAxisScratch = new Cartesian3();
  545. var secondAxisScratch = new Cartesian3();
  546. var thirdAxisScratch = new Cartesian3();
  547. var referenceScratch = new Cartesian3();
  548. var bCart = new Cartesian3();
  549. var bScratch = new Matrix3();
  550. var btScratch = new Matrix3();
  551. var diScratch = new Matrix3();
  552. var dScratch = new Matrix3();
  553. var cScratch = new Matrix3();
  554. var tempMatrix = new Matrix3();
  555. var aScratch = new Matrix3();
  556. var sScratch = new Cartesian3();
  557. var closestScratch = new Cartesian3();
  558. var surfPointScratch = new Cartographic();
  559. /**
  560. * Provides the point along the ray which is nearest to the ellipsoid.
  561. *
  562. * @param {Ray} ray The ray.
  563. * @param {Ellipsoid} ellipsoid The ellipsoid.
  564. * @returns {Cartesian3} The nearest planetodetic point on the ray.
  565. */
  566. IntersectionTests.grazingAltitudeLocation = function (ray, ellipsoid) {
  567. //>>includeStart('debug', pragmas.debug);
  568. if (!defined(ray)) {
  569. throw new DeveloperError("ray is required.");
  570. }
  571. if (!defined(ellipsoid)) {
  572. throw new DeveloperError("ellipsoid is required.");
  573. }
  574. //>>includeEnd('debug');
  575. var position = ray.origin;
  576. var direction = ray.direction;
  577. if (!Cartesian3.equals(position, Cartesian3.ZERO)) {
  578. var normal = ellipsoid.geodeticSurfaceNormal(position, firstAxisScratch);
  579. if (Cartesian3.dot(direction, normal) >= 0.0) {
  580. // The location provided is the closest point in altitude
  581. return position;
  582. }
  583. }
  584. var intersects = defined(this.rayEllipsoid(ray, ellipsoid));
  585. // Compute the scaled direction vector.
  586. var f = ellipsoid.transformPositionToScaledSpace(direction, firstAxisScratch);
  587. // Constructs a basis from the unit scaled direction vector. Construct its rotation and transpose.
  588. var firstAxis = Cartesian3.normalize(f, f);
  589. var reference = Cartesian3.mostOrthogonalAxis(f, referenceScratch);
  590. var secondAxis = Cartesian3.normalize(
  591. Cartesian3.cross(reference, firstAxis, secondAxisScratch),
  592. secondAxisScratch
  593. );
  594. var thirdAxis = Cartesian3.normalize(
  595. Cartesian3.cross(firstAxis, secondAxis, thirdAxisScratch),
  596. thirdAxisScratch
  597. );
  598. var B = bScratch;
  599. B[0] = firstAxis.x;
  600. B[1] = firstAxis.y;
  601. B[2] = firstAxis.z;
  602. B[3] = secondAxis.x;
  603. B[4] = secondAxis.y;
  604. B[5] = secondAxis.z;
  605. B[6] = thirdAxis.x;
  606. B[7] = thirdAxis.y;
  607. B[8] = thirdAxis.z;
  608. var B_T = Matrix3.transpose(B, btScratch);
  609. // Get the scaling matrix and its inverse.
  610. var D_I = Matrix3.fromScale(ellipsoid.radii, diScratch);
  611. var D = Matrix3.fromScale(ellipsoid.oneOverRadii, dScratch);
  612. var C = cScratch;
  613. C[0] = 0.0;
  614. C[1] = -direction.z;
  615. C[2] = direction.y;
  616. C[3] = direction.z;
  617. C[4] = 0.0;
  618. C[5] = -direction.x;
  619. C[6] = -direction.y;
  620. C[7] = direction.x;
  621. C[8] = 0.0;
  622. var temp = Matrix3.multiply(
  623. Matrix3.multiply(B_T, D, tempMatrix),
  624. C,
  625. tempMatrix
  626. );
  627. var A = Matrix3.multiply(Matrix3.multiply(temp, D_I, aScratch), B, aScratch);
  628. var b = Matrix3.multiplyByVector(temp, position, bCart);
  629. // Solve for the solutions to the expression in standard form:
  630. var solutions = quadraticVectorExpression(
  631. A,
  632. Cartesian3.negate(b, firstAxisScratch),
  633. 0.0,
  634. 0.0,
  635. 1.0
  636. );
  637. var s;
  638. var altitude;
  639. var length = solutions.length;
  640. if (length > 0) {
  641. var closest = Cartesian3.clone(Cartesian3.ZERO, closestScratch);
  642. var maximumValue = Number.NEGATIVE_INFINITY;
  643. for (var i = 0; i < length; ++i) {
  644. s = Matrix3.multiplyByVector(
  645. D_I,
  646. Matrix3.multiplyByVector(B, solutions[i], sScratch),
  647. sScratch
  648. );
  649. var v = Cartesian3.normalize(
  650. Cartesian3.subtract(s, position, referenceScratch),
  651. referenceScratch
  652. );
  653. var dotProduct = Cartesian3.dot(v, direction);
  654. if (dotProduct > maximumValue) {
  655. maximumValue = dotProduct;
  656. closest = Cartesian3.clone(s, closest);
  657. }
  658. }
  659. var surfacePoint = ellipsoid.cartesianToCartographic(
  660. closest,
  661. surfPointScratch
  662. );
  663. maximumValue = CesiumMath.clamp(maximumValue, 0.0, 1.0);
  664. altitude =
  665. Cartesian3.magnitude(
  666. Cartesian3.subtract(closest, position, referenceScratch)
  667. ) * Math.sqrt(1.0 - maximumValue * maximumValue);
  668. altitude = intersects ? -altitude : altitude;
  669. surfacePoint.height = altitude;
  670. return ellipsoid.cartographicToCartesian(surfacePoint, new Cartesian3());
  671. }
  672. return undefined;
  673. };
  674. var lineSegmentPlaneDifference = new Cartesian3();
  675. /**
  676. * Computes the intersection of a line segment and a plane.
  677. *
  678. * @param {Cartesian3} endPoint0 An end point of the line segment.
  679. * @param {Cartesian3} endPoint1 The other end point of the line segment.
  680. * @param {Plane} plane The plane.
  681. * @param {Cartesian3} [result] The object onto which to store the result.
  682. * @returns {Cartesian3} The intersection point or undefined if there is no intersection.
  683. *
  684. * @example
  685. * var origin = Cesium.Cartesian3.fromDegrees(-75.59777, 40.03883);
  686. * var normal = ellipsoid.geodeticSurfaceNormal(origin);
  687. * var plane = Cesium.Plane.fromPointNormal(origin, normal);
  688. *
  689. * var p0 = new Cesium.Cartesian3(...);
  690. * var p1 = new Cesium.Cartesian3(...);
  691. *
  692. * // find the intersection of the line segment from p0 to p1 and the tangent plane at origin.
  693. * var intersection = Cesium.IntersectionTests.lineSegmentPlane(p0, p1, plane);
  694. */
  695. IntersectionTests.lineSegmentPlane = function (
  696. endPoint0,
  697. endPoint1,
  698. plane,
  699. result
  700. ) {
  701. //>>includeStart('debug', pragmas.debug);
  702. if (!defined(endPoint0)) {
  703. throw new DeveloperError("endPoint0 is required.");
  704. }
  705. if (!defined(endPoint1)) {
  706. throw new DeveloperError("endPoint1 is required.");
  707. }
  708. if (!defined(plane)) {
  709. throw new DeveloperError("plane is required.");
  710. }
  711. //>>includeEnd('debug');
  712. if (!defined(result)) {
  713. result = new Cartesian3();
  714. }
  715. var difference = Cartesian3.subtract(
  716. endPoint1,
  717. endPoint0,
  718. lineSegmentPlaneDifference
  719. );
  720. var normal = plane.normal;
  721. var nDotDiff = Cartesian3.dot(normal, difference);
  722. // check if the segment and plane are parallel
  723. if (Math.abs(nDotDiff) < CesiumMath.EPSILON6) {
  724. return undefined;
  725. }
  726. var nDotP0 = Cartesian3.dot(normal, endPoint0);
  727. var t = -(plane.distance + nDotP0) / nDotDiff;
  728. // intersection only if t is in [0, 1]
  729. if (t < 0.0 || t > 1.0) {
  730. return undefined;
  731. }
  732. // intersection is endPoint0 + t * (endPoint1 - endPoint0)
  733. Cartesian3.multiplyByScalar(difference, t, result);
  734. Cartesian3.add(endPoint0, result, result);
  735. return result;
  736. };
  737. /**
  738. * Computes the intersection of a triangle and a plane
  739. *
  740. * @param {Cartesian3} p0 First point of the triangle
  741. * @param {Cartesian3} p1 Second point of the triangle
  742. * @param {Cartesian3} p2 Third point of the triangle
  743. * @param {Plane} plane Intersection plane
  744. * @returns {Object} An object with properties <code>positions</code> and <code>indices</code>, which are arrays that represent three triangles that do not cross the plane. (Undefined if no intersection exists)
  745. *
  746. * @example
  747. * var origin = Cesium.Cartesian3.fromDegrees(-75.59777, 40.03883);
  748. * var normal = ellipsoid.geodeticSurfaceNormal(origin);
  749. * var plane = Cesium.Plane.fromPointNormal(origin, normal);
  750. *
  751. * var p0 = new Cesium.Cartesian3(...);
  752. * var p1 = new Cesium.Cartesian3(...);
  753. * var p2 = new Cesium.Cartesian3(...);
  754. *
  755. * // convert the triangle composed of points (p0, p1, p2) to three triangles that don't cross the plane
  756. * var triangles = Cesium.IntersectionTests.trianglePlaneIntersection(p0, p1, p2, plane);
  757. */
  758. IntersectionTests.trianglePlaneIntersection = function (p0, p1, p2, plane) {
  759. //>>includeStart('debug', pragmas.debug);
  760. if (!defined(p0) || !defined(p1) || !defined(p2) || !defined(plane)) {
  761. throw new DeveloperError("p0, p1, p2, and plane are required.");
  762. }
  763. //>>includeEnd('debug');
  764. var planeNormal = plane.normal;
  765. var planeD = plane.distance;
  766. var p0Behind = Cartesian3.dot(planeNormal, p0) + planeD < 0.0;
  767. var p1Behind = Cartesian3.dot(planeNormal, p1) + planeD < 0.0;
  768. var p2Behind = Cartesian3.dot(planeNormal, p2) + planeD < 0.0;
  769. // Given these dots products, the calls to lineSegmentPlaneIntersection
  770. // always have defined results.
  771. var numBehind = 0;
  772. numBehind += p0Behind ? 1 : 0;
  773. numBehind += p1Behind ? 1 : 0;
  774. numBehind += p2Behind ? 1 : 0;
  775. var u1, u2;
  776. if (numBehind === 1 || numBehind === 2) {
  777. u1 = new Cartesian3();
  778. u2 = new Cartesian3();
  779. }
  780. if (numBehind === 1) {
  781. if (p0Behind) {
  782. IntersectionTests.lineSegmentPlane(p0, p1, plane, u1);
  783. IntersectionTests.lineSegmentPlane(p0, p2, plane, u2);
  784. return {
  785. positions: [p0, p1, p2, u1, u2],
  786. indices: [
  787. // Behind
  788. 0,
  789. 3,
  790. 4,
  791. // In front
  792. 1,
  793. 2,
  794. 4,
  795. 1,
  796. 4,
  797. 3,
  798. ],
  799. };
  800. } else if (p1Behind) {
  801. IntersectionTests.lineSegmentPlane(p1, p2, plane, u1);
  802. IntersectionTests.lineSegmentPlane(p1, p0, plane, u2);
  803. return {
  804. positions: [p0, p1, p2, u1, u2],
  805. indices: [
  806. // Behind
  807. 1,
  808. 3,
  809. 4,
  810. // In front
  811. 2,
  812. 0,
  813. 4,
  814. 2,
  815. 4,
  816. 3,
  817. ],
  818. };
  819. } else if (p2Behind) {
  820. IntersectionTests.lineSegmentPlane(p2, p0, plane, u1);
  821. IntersectionTests.lineSegmentPlane(p2, p1, plane, u2);
  822. return {
  823. positions: [p0, p1, p2, u1, u2],
  824. indices: [
  825. // Behind
  826. 2,
  827. 3,
  828. 4,
  829. // In front
  830. 0,
  831. 1,
  832. 4,
  833. 0,
  834. 4,
  835. 3,
  836. ],
  837. };
  838. }
  839. } else if (numBehind === 2) {
  840. if (!p0Behind) {
  841. IntersectionTests.lineSegmentPlane(p1, p0, plane, u1);
  842. IntersectionTests.lineSegmentPlane(p2, p0, plane, u2);
  843. return {
  844. positions: [p0, p1, p2, u1, u2],
  845. indices: [
  846. // Behind
  847. 1,
  848. 2,
  849. 4,
  850. 1,
  851. 4,
  852. 3,
  853. // In front
  854. 0,
  855. 3,
  856. 4,
  857. ],
  858. };
  859. } else if (!p1Behind) {
  860. IntersectionTests.lineSegmentPlane(p2, p1, plane, u1);
  861. IntersectionTests.lineSegmentPlane(p0, p1, plane, u2);
  862. return {
  863. positions: [p0, p1, p2, u1, u2],
  864. indices: [
  865. // Behind
  866. 2,
  867. 0,
  868. 4,
  869. 2,
  870. 4,
  871. 3,
  872. // In front
  873. 1,
  874. 3,
  875. 4,
  876. ],
  877. };
  878. } else if (!p2Behind) {
  879. IntersectionTests.lineSegmentPlane(p0, p2, plane, u1);
  880. IntersectionTests.lineSegmentPlane(p1, p2, plane, u2);
  881. return {
  882. positions: [p0, p1, p2, u1, u2],
  883. indices: [
  884. // Behind
  885. 0,
  886. 1,
  887. 4,
  888. 0,
  889. 4,
  890. 3,
  891. // In front
  892. 2,
  893. 3,
  894. 4,
  895. ],
  896. };
  897. }
  898. }
  899. // if numBehind is 3, the triangle is completely behind the plane;
  900. // otherwise, it is completely in front (numBehind is 0).
  901. return undefined;
  902. };
  903. export default IntersectionTests;