IntersectionTests-f96cd46d.js 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861
  1. /**
  2. * @license
  3. * Cesium - https://github.com/CesiumGS/cesium
  4. * Version 1.97
  5. *
  6. * Copyright 2011-2022 Cesium Contributors
  7. *
  8. * Licensed under the Apache License, Version 2.0 (the "License");
  9. * you may not use this file except in compliance with the License.
  10. * You may obtain a copy of the License at
  11. *
  12. * http://www.apache.org/licenses/LICENSE-2.0
  13. *
  14. * Unless required by applicable law or agreed to in writing, software
  15. * distributed under the License is distributed on an "AS IS" BASIS,
  16. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. * See the License for the specific language governing permissions and
  18. * limitations under the License.
  19. *
  20. * Columbus View (Pat. Pend.)
  21. *
  22. * Portions licensed separately.
  23. * See https://github.com/CesiumGS/cesium/blob/main/LICENSE.md for full licensing details.
  24. */
  25. define(['exports', './Matrix2-ab676047', './defaultValue-a6eb9f34', './RuntimeError-1088cc64', './Transforms-c78c4637', './ComponentDatatype-e06f4e16'], (function (exports, Matrix2, defaultValue, RuntimeError, Transforms, ComponentDatatype) { 'use strict';
  26. /**
  27. * Defines functions for 2nd order polynomial functions of one variable with only real coefficients.
  28. *
  29. * @namespace QuadraticRealPolynomial
  30. */
  31. const QuadraticRealPolynomial = {};
  32. /**
  33. * Provides the discriminant of the quadratic equation from the supplied coefficients.
  34. *
  35. * @param {Number} a The coefficient of the 2nd order monomial.
  36. * @param {Number} b The coefficient of the 1st order monomial.
  37. * @param {Number} c The coefficient of the 0th order monomial.
  38. * @returns {Number} The value of the discriminant.
  39. */
  40. QuadraticRealPolynomial.computeDiscriminant = function (a, b, c) {
  41. //>>includeStart('debug', pragmas.debug);
  42. if (typeof a !== "number") {
  43. throw new RuntimeError.DeveloperError("a is a required number.");
  44. }
  45. if (typeof b !== "number") {
  46. throw new RuntimeError.DeveloperError("b is a required number.");
  47. }
  48. if (typeof c !== "number") {
  49. throw new RuntimeError.DeveloperError("c is a required number.");
  50. }
  51. //>>includeEnd('debug');
  52. const discriminant = b * b - 4.0 * a * c;
  53. return discriminant;
  54. };
  55. function addWithCancellationCheck$1(left, right, tolerance) {
  56. const difference = left + right;
  57. if (
  58. ComponentDatatype.CesiumMath.sign(left) !== ComponentDatatype.CesiumMath.sign(right) &&
  59. Math.abs(difference / Math.max(Math.abs(left), Math.abs(right))) < tolerance
  60. ) {
  61. return 0.0;
  62. }
  63. return difference;
  64. }
  65. /**
  66. * Provides the real valued roots of the quadratic polynomial with the provided coefficients.
  67. *
  68. * @param {Number} a The coefficient of the 2nd order monomial.
  69. * @param {Number} b The coefficient of the 1st order monomial.
  70. * @param {Number} c The coefficient of the 0th order monomial.
  71. * @returns {Number[]} The real valued roots.
  72. */
  73. QuadraticRealPolynomial.computeRealRoots = function (a, b, c) {
  74. //>>includeStart('debug', pragmas.debug);
  75. if (typeof a !== "number") {
  76. throw new RuntimeError.DeveloperError("a is a required number.");
  77. }
  78. if (typeof b !== "number") {
  79. throw new RuntimeError.DeveloperError("b is a required number.");
  80. }
  81. if (typeof c !== "number") {
  82. throw new RuntimeError.DeveloperError("c is a required number.");
  83. }
  84. //>>includeEnd('debug');
  85. let ratio;
  86. if (a === 0.0) {
  87. if (b === 0.0) {
  88. // Constant function: c = 0.
  89. return [];
  90. }
  91. // Linear function: b * x + c = 0.
  92. return [-c / b];
  93. } else if (b === 0.0) {
  94. if (c === 0.0) {
  95. // 2nd order monomial: a * x^2 = 0.
  96. return [0.0, 0.0];
  97. }
  98. const cMagnitude = Math.abs(c);
  99. const aMagnitude = Math.abs(a);
  100. if (
  101. cMagnitude < aMagnitude &&
  102. cMagnitude / aMagnitude < ComponentDatatype.CesiumMath.EPSILON14
  103. ) {
  104. // c ~= 0.0.
  105. // 2nd order monomial: a * x^2 = 0.
  106. return [0.0, 0.0];
  107. } else if (
  108. cMagnitude > aMagnitude &&
  109. aMagnitude / cMagnitude < ComponentDatatype.CesiumMath.EPSILON14
  110. ) {
  111. // a ~= 0.0.
  112. // Constant function: c = 0.
  113. return [];
  114. }
  115. // a * x^2 + c = 0
  116. ratio = -c / a;
  117. if (ratio < 0.0) {
  118. // Both roots are complex.
  119. return [];
  120. }
  121. // Both roots are real.
  122. const root = Math.sqrt(ratio);
  123. return [-root, root];
  124. } else if (c === 0.0) {
  125. // a * x^2 + b * x = 0
  126. ratio = -b / a;
  127. if (ratio < 0.0) {
  128. return [ratio, 0.0];
  129. }
  130. return [0.0, ratio];
  131. }
  132. // a * x^2 + b * x + c = 0
  133. const b2 = b * b;
  134. const four_ac = 4.0 * a * c;
  135. const radicand = addWithCancellationCheck$1(b2, -four_ac, ComponentDatatype.CesiumMath.EPSILON14);
  136. if (radicand < 0.0) {
  137. // Both roots are complex.
  138. return [];
  139. }
  140. const q =
  141. -0.5 *
  142. addWithCancellationCheck$1(
  143. b,
  144. ComponentDatatype.CesiumMath.sign(b) * Math.sqrt(radicand),
  145. ComponentDatatype.CesiumMath.EPSILON14
  146. );
  147. if (b > 0.0) {
  148. return [q / a, c / q];
  149. }
  150. return [c / q, q / a];
  151. };
  152. var QuadraticRealPolynomial$1 = QuadraticRealPolynomial;
  153. /**
  154. * Defines functions for 3rd order polynomial functions of one variable with only real coefficients.
  155. *
  156. * @namespace CubicRealPolynomial
  157. */
  158. const CubicRealPolynomial = {};
  159. /**
  160. * Provides the discriminant of the cubic equation from the supplied coefficients.
  161. *
  162. * @param {Number} a The coefficient of the 3rd order monomial.
  163. * @param {Number} b The coefficient of the 2nd order monomial.
  164. * @param {Number} c The coefficient of the 1st order monomial.
  165. * @param {Number} d The coefficient of the 0th order monomial.
  166. * @returns {Number} The value of the discriminant.
  167. */
  168. CubicRealPolynomial.computeDiscriminant = function (a, b, c, d) {
  169. //>>includeStart('debug', pragmas.debug);
  170. if (typeof a !== "number") {
  171. throw new RuntimeError.DeveloperError("a is a required number.");
  172. }
  173. if (typeof b !== "number") {
  174. throw new RuntimeError.DeveloperError("b is a required number.");
  175. }
  176. if (typeof c !== "number") {
  177. throw new RuntimeError.DeveloperError("c is a required number.");
  178. }
  179. if (typeof d !== "number") {
  180. throw new RuntimeError.DeveloperError("d is a required number.");
  181. }
  182. //>>includeEnd('debug');
  183. const a2 = a * a;
  184. const b2 = b * b;
  185. const c2 = c * c;
  186. const d2 = d * d;
  187. const discriminant =
  188. 18.0 * a * b * c * d +
  189. b2 * c2 -
  190. 27.0 * a2 * d2 -
  191. 4.0 * (a * c2 * c + b2 * b * d);
  192. return discriminant;
  193. };
  194. function computeRealRoots(a, b, c, d) {
  195. const A = a;
  196. const B = b / 3.0;
  197. const C = c / 3.0;
  198. const D = d;
  199. const AC = A * C;
  200. const BD = B * D;
  201. const B2 = B * B;
  202. const C2 = C * C;
  203. const delta1 = A * C - B2;
  204. const delta2 = A * D - B * C;
  205. const delta3 = B * D - C2;
  206. const discriminant = 4.0 * delta1 * delta3 - delta2 * delta2;
  207. let temp;
  208. let temp1;
  209. if (discriminant < 0.0) {
  210. let ABar;
  211. let CBar;
  212. let DBar;
  213. if (B2 * BD >= AC * C2) {
  214. ABar = A;
  215. CBar = delta1;
  216. DBar = -2.0 * B * delta1 + A * delta2;
  217. } else {
  218. ABar = D;
  219. CBar = delta3;
  220. DBar = -D * delta2 + 2.0 * C * delta3;
  221. }
  222. const s = DBar < 0.0 ? -1.0 : 1.0; // This is not Math.Sign()!
  223. const temp0 = -s * Math.abs(ABar) * Math.sqrt(-discriminant);
  224. temp1 = -DBar + temp0;
  225. const x = temp1 / 2.0;
  226. const p = x < 0.0 ? -Math.pow(-x, 1.0 / 3.0) : Math.pow(x, 1.0 / 3.0);
  227. const q = temp1 === temp0 ? -p : -CBar / p;
  228. temp = CBar <= 0.0 ? p + q : -DBar / (p * p + q * q + CBar);
  229. if (B2 * BD >= AC * C2) {
  230. return [(temp - B) / A];
  231. }
  232. return [-D / (temp + C)];
  233. }
  234. const CBarA = delta1;
  235. const DBarA = -2.0 * B * delta1 + A * delta2;
  236. const CBarD = delta3;
  237. const DBarD = -D * delta2 + 2.0 * C * delta3;
  238. const squareRootOfDiscriminant = Math.sqrt(discriminant);
  239. const halfSquareRootOf3 = Math.sqrt(3.0) / 2.0;
  240. let theta = Math.abs(Math.atan2(A * squareRootOfDiscriminant, -DBarA) / 3.0);
  241. temp = 2.0 * Math.sqrt(-CBarA);
  242. let cosine = Math.cos(theta);
  243. temp1 = temp * cosine;
  244. let temp3 = temp * (-cosine / 2.0 - halfSquareRootOf3 * Math.sin(theta));
  245. const numeratorLarge = temp1 + temp3 > 2.0 * B ? temp1 - B : temp3 - B;
  246. const denominatorLarge = A;
  247. const root1 = numeratorLarge / denominatorLarge;
  248. theta = Math.abs(Math.atan2(D * squareRootOfDiscriminant, -DBarD) / 3.0);
  249. temp = 2.0 * Math.sqrt(-CBarD);
  250. cosine = Math.cos(theta);
  251. temp1 = temp * cosine;
  252. temp3 = temp * (-cosine / 2.0 - halfSquareRootOf3 * Math.sin(theta));
  253. const numeratorSmall = -D;
  254. const denominatorSmall = temp1 + temp3 < 2.0 * C ? temp1 + C : temp3 + C;
  255. const root3 = numeratorSmall / denominatorSmall;
  256. const E = denominatorLarge * denominatorSmall;
  257. const F =
  258. -numeratorLarge * denominatorSmall - denominatorLarge * numeratorSmall;
  259. const G = numeratorLarge * numeratorSmall;
  260. const root2 = (C * F - B * G) / (-B * F + C * E);
  261. if (root1 <= root2) {
  262. if (root1 <= root3) {
  263. if (root2 <= root3) {
  264. return [root1, root2, root3];
  265. }
  266. return [root1, root3, root2];
  267. }
  268. return [root3, root1, root2];
  269. }
  270. if (root1 <= root3) {
  271. return [root2, root1, root3];
  272. }
  273. if (root2 <= root3) {
  274. return [root2, root3, root1];
  275. }
  276. return [root3, root2, root1];
  277. }
  278. /**
  279. * Provides the real valued roots of the cubic polynomial with the provided coefficients.
  280. *
  281. * @param {Number} a The coefficient of the 3rd order monomial.
  282. * @param {Number} b The coefficient of the 2nd order monomial.
  283. * @param {Number} c The coefficient of the 1st order monomial.
  284. * @param {Number} d The coefficient of the 0th order monomial.
  285. * @returns {Number[]} The real valued roots.
  286. */
  287. CubicRealPolynomial.computeRealRoots = function (a, b, c, d) {
  288. //>>includeStart('debug', pragmas.debug);
  289. if (typeof a !== "number") {
  290. throw new RuntimeError.DeveloperError("a is a required number.");
  291. }
  292. if (typeof b !== "number") {
  293. throw new RuntimeError.DeveloperError("b is a required number.");
  294. }
  295. if (typeof c !== "number") {
  296. throw new RuntimeError.DeveloperError("c is a required number.");
  297. }
  298. if (typeof d !== "number") {
  299. throw new RuntimeError.DeveloperError("d is a required number.");
  300. }
  301. //>>includeEnd('debug');
  302. let roots;
  303. let ratio;
  304. if (a === 0.0) {
  305. // Quadratic function: b * x^2 + c * x + d = 0.
  306. return QuadraticRealPolynomial$1.computeRealRoots(b, c, d);
  307. } else if (b === 0.0) {
  308. if (c === 0.0) {
  309. if (d === 0.0) {
  310. // 3rd order monomial: a * x^3 = 0.
  311. return [0.0, 0.0, 0.0];
  312. }
  313. // a * x^3 + d = 0
  314. ratio = -d / a;
  315. const root =
  316. ratio < 0.0 ? -Math.pow(-ratio, 1.0 / 3.0) : Math.pow(ratio, 1.0 / 3.0);
  317. return [root, root, root];
  318. } else if (d === 0.0) {
  319. // x * (a * x^2 + c) = 0.
  320. roots = QuadraticRealPolynomial$1.computeRealRoots(a, 0, c);
  321. // Return the roots in ascending order.
  322. if (roots.Length === 0) {
  323. return [0.0];
  324. }
  325. return [roots[0], 0.0, roots[1]];
  326. }
  327. // Deflated cubic polynomial: a * x^3 + c * x + d= 0.
  328. return computeRealRoots(a, 0, c, d);
  329. } else if (c === 0.0) {
  330. if (d === 0.0) {
  331. // x^2 * (a * x + b) = 0.
  332. ratio = -b / a;
  333. if (ratio < 0.0) {
  334. return [ratio, 0.0, 0.0];
  335. }
  336. return [0.0, 0.0, ratio];
  337. }
  338. // a * x^3 + b * x^2 + d = 0.
  339. return computeRealRoots(a, b, 0, d);
  340. } else if (d === 0.0) {
  341. // x * (a * x^2 + b * x + c) = 0
  342. roots = QuadraticRealPolynomial$1.computeRealRoots(a, b, c);
  343. // Return the roots in ascending order.
  344. if (roots.length === 0) {
  345. return [0.0];
  346. } else if (roots[1] <= 0.0) {
  347. return [roots[0], roots[1], 0.0];
  348. } else if (roots[0] >= 0.0) {
  349. return [0.0, roots[0], roots[1]];
  350. }
  351. return [roots[0], 0.0, roots[1]];
  352. }
  353. return computeRealRoots(a, b, c, d);
  354. };
  355. var CubicRealPolynomial$1 = CubicRealPolynomial;
  356. /**
  357. * Defines functions for 4th order polynomial functions of one variable with only real coefficients.
  358. *
  359. * @namespace QuarticRealPolynomial
  360. */
  361. const QuarticRealPolynomial = {};
  362. /**
  363. * Provides the discriminant of the quartic equation from the supplied coefficients.
  364. *
  365. * @param {Number} a The coefficient of the 4th order monomial.
  366. * @param {Number} b The coefficient of the 3rd order monomial.
  367. * @param {Number} c The coefficient of the 2nd order monomial.
  368. * @param {Number} d The coefficient of the 1st order monomial.
  369. * @param {Number} e The coefficient of the 0th order monomial.
  370. * @returns {Number} The value of the discriminant.
  371. */
  372. QuarticRealPolynomial.computeDiscriminant = function (a, b, c, d, e) {
  373. //>>includeStart('debug', pragmas.debug);
  374. if (typeof a !== "number") {
  375. throw new RuntimeError.DeveloperError("a is a required number.");
  376. }
  377. if (typeof b !== "number") {
  378. throw new RuntimeError.DeveloperError("b is a required number.");
  379. }
  380. if (typeof c !== "number") {
  381. throw new RuntimeError.DeveloperError("c is a required number.");
  382. }
  383. if (typeof d !== "number") {
  384. throw new RuntimeError.DeveloperError("d is a required number.");
  385. }
  386. if (typeof e !== "number") {
  387. throw new RuntimeError.DeveloperError("e is a required number.");
  388. }
  389. //>>includeEnd('debug');
  390. const a2 = a * a;
  391. const a3 = a2 * a;
  392. const b2 = b * b;
  393. const b3 = b2 * b;
  394. const c2 = c * c;
  395. const c3 = c2 * c;
  396. const d2 = d * d;
  397. const d3 = d2 * d;
  398. const e2 = e * e;
  399. const e3 = e2 * e;
  400. const discriminant =
  401. b2 * c2 * d2 -
  402. 4.0 * b3 * d3 -
  403. 4.0 * a * c3 * d2 +
  404. 18 * a * b * c * d3 -
  405. 27.0 * a2 * d2 * d2 +
  406. 256.0 * a3 * e3 +
  407. e *
  408. (18.0 * b3 * c * d -
  409. 4.0 * b2 * c3 +
  410. 16.0 * a * c2 * c2 -
  411. 80.0 * a * b * c2 * d -
  412. 6.0 * a * b2 * d2 +
  413. 144.0 * a2 * c * d2) +
  414. e2 *
  415. (144.0 * a * b2 * c -
  416. 27.0 * b2 * b2 -
  417. 128.0 * a2 * c2 -
  418. 192.0 * a2 * b * d);
  419. return discriminant;
  420. };
  421. function original(a3, a2, a1, a0) {
  422. const a3Squared = a3 * a3;
  423. const p = a2 - (3.0 * a3Squared) / 8.0;
  424. const q = a1 - (a2 * a3) / 2.0 + (a3Squared * a3) / 8.0;
  425. const r =
  426. a0 -
  427. (a1 * a3) / 4.0 +
  428. (a2 * a3Squared) / 16.0 -
  429. (3.0 * a3Squared * a3Squared) / 256.0;
  430. // Find the roots of the cubic equations: h^6 + 2 p h^4 + (p^2 - 4 r) h^2 - q^2 = 0.
  431. const cubicRoots = CubicRealPolynomial$1.computeRealRoots(
  432. 1.0,
  433. 2.0 * p,
  434. p * p - 4.0 * r,
  435. -q * q
  436. );
  437. if (cubicRoots.length > 0) {
  438. const temp = -a3 / 4.0;
  439. // Use the largest positive root.
  440. const hSquared = cubicRoots[cubicRoots.length - 1];
  441. if (Math.abs(hSquared) < ComponentDatatype.CesiumMath.EPSILON14) {
  442. // y^4 + p y^2 + r = 0.
  443. const roots = QuadraticRealPolynomial$1.computeRealRoots(1.0, p, r);
  444. if (roots.length === 2) {
  445. const root0 = roots[0];
  446. const root1 = roots[1];
  447. let y;
  448. if (root0 >= 0.0 && root1 >= 0.0) {
  449. const y0 = Math.sqrt(root0);
  450. const y1 = Math.sqrt(root1);
  451. return [temp - y1, temp - y0, temp + y0, temp + y1];
  452. } else if (root0 >= 0.0 && root1 < 0.0) {
  453. y = Math.sqrt(root0);
  454. return [temp - y, temp + y];
  455. } else if (root0 < 0.0 && root1 >= 0.0) {
  456. y = Math.sqrt(root1);
  457. return [temp - y, temp + y];
  458. }
  459. }
  460. return [];
  461. } else if (hSquared > 0.0) {
  462. const h = Math.sqrt(hSquared);
  463. const m = (p + hSquared - q / h) / 2.0;
  464. const n = (p + hSquared + q / h) / 2.0;
  465. // Now solve the two quadratic factors: (y^2 + h y + m)(y^2 - h y + n);
  466. const roots1 = QuadraticRealPolynomial$1.computeRealRoots(1.0, h, m);
  467. const roots2 = QuadraticRealPolynomial$1.computeRealRoots(1.0, -h, n);
  468. if (roots1.length !== 0) {
  469. roots1[0] += temp;
  470. roots1[1] += temp;
  471. if (roots2.length !== 0) {
  472. roots2[0] += temp;
  473. roots2[1] += temp;
  474. if (roots1[1] <= roots2[0]) {
  475. return [roots1[0], roots1[1], roots2[0], roots2[1]];
  476. } else if (roots2[1] <= roots1[0]) {
  477. return [roots2[0], roots2[1], roots1[0], roots1[1]];
  478. } else if (roots1[0] >= roots2[0] && roots1[1] <= roots2[1]) {
  479. return [roots2[0], roots1[0], roots1[1], roots2[1]];
  480. } else if (roots2[0] >= roots1[0] && roots2[1] <= roots1[1]) {
  481. return [roots1[0], roots2[0], roots2[1], roots1[1]];
  482. } else if (roots1[0] > roots2[0] && roots1[0] < roots2[1]) {
  483. return [roots2[0], roots1[0], roots2[1], roots1[1]];
  484. }
  485. return [roots1[0], roots2[0], roots1[1], roots2[1]];
  486. }
  487. return roots1;
  488. }
  489. if (roots2.length !== 0) {
  490. roots2[0] += temp;
  491. roots2[1] += temp;
  492. return roots2;
  493. }
  494. return [];
  495. }
  496. }
  497. return [];
  498. }
  499. function neumark(a3, a2, a1, a0) {
  500. const a1Squared = a1 * a1;
  501. const a2Squared = a2 * a2;
  502. const a3Squared = a3 * a3;
  503. const p = -2.0 * a2;
  504. const q = a1 * a3 + a2Squared - 4.0 * a0;
  505. const r = a3Squared * a0 - a1 * a2 * a3 + a1Squared;
  506. const cubicRoots = CubicRealPolynomial$1.computeRealRoots(1.0, p, q, r);
  507. if (cubicRoots.length > 0) {
  508. // Use the most positive root
  509. const y = cubicRoots[0];
  510. const temp = a2 - y;
  511. const tempSquared = temp * temp;
  512. const g1 = a3 / 2.0;
  513. const h1 = temp / 2.0;
  514. const m = tempSquared - 4.0 * a0;
  515. const mError = tempSquared + 4.0 * Math.abs(a0);
  516. const n = a3Squared - 4.0 * y;
  517. const nError = a3Squared + 4.0 * Math.abs(y);
  518. let g2;
  519. let h2;
  520. if (y < 0.0 || m * nError < n * mError) {
  521. const squareRootOfN = Math.sqrt(n);
  522. g2 = squareRootOfN / 2.0;
  523. h2 = squareRootOfN === 0.0 ? 0.0 : (a3 * h1 - a1) / squareRootOfN;
  524. } else {
  525. const squareRootOfM = Math.sqrt(m);
  526. g2 = squareRootOfM === 0.0 ? 0.0 : (a3 * h1 - a1) / squareRootOfM;
  527. h2 = squareRootOfM / 2.0;
  528. }
  529. let G;
  530. let g;
  531. if (g1 === 0.0 && g2 === 0.0) {
  532. G = 0.0;
  533. g = 0.0;
  534. } else if (ComponentDatatype.CesiumMath.sign(g1) === ComponentDatatype.CesiumMath.sign(g2)) {
  535. G = g1 + g2;
  536. g = y / G;
  537. } else {
  538. g = g1 - g2;
  539. G = y / g;
  540. }
  541. let H;
  542. let h;
  543. if (h1 === 0.0 && h2 === 0.0) {
  544. H = 0.0;
  545. h = 0.0;
  546. } else if (ComponentDatatype.CesiumMath.sign(h1) === ComponentDatatype.CesiumMath.sign(h2)) {
  547. H = h1 + h2;
  548. h = a0 / H;
  549. } else {
  550. h = h1 - h2;
  551. H = a0 / h;
  552. }
  553. // Now solve the two quadratic factors: (y^2 + G y + H)(y^2 + g y + h);
  554. const roots1 = QuadraticRealPolynomial$1.computeRealRoots(1.0, G, H);
  555. const roots2 = QuadraticRealPolynomial$1.computeRealRoots(1.0, g, h);
  556. if (roots1.length !== 0) {
  557. if (roots2.length !== 0) {
  558. if (roots1[1] <= roots2[0]) {
  559. return [roots1[0], roots1[1], roots2[0], roots2[1]];
  560. } else if (roots2[1] <= roots1[0]) {
  561. return [roots2[0], roots2[1], roots1[0], roots1[1]];
  562. } else if (roots1[0] >= roots2[0] && roots1[1] <= roots2[1]) {
  563. return [roots2[0], roots1[0], roots1[1], roots2[1]];
  564. } else if (roots2[0] >= roots1[0] && roots2[1] <= roots1[1]) {
  565. return [roots1[0], roots2[0], roots2[1], roots1[1]];
  566. } else if (roots1[0] > roots2[0] && roots1[0] < roots2[1]) {
  567. return [roots2[0], roots1[0], roots2[1], roots1[1]];
  568. }
  569. return [roots1[0], roots2[0], roots1[1], roots2[1]];
  570. }
  571. return roots1;
  572. }
  573. if (roots2.length !== 0) {
  574. return roots2;
  575. }
  576. }
  577. return [];
  578. }
  579. /**
  580. * Provides the real valued roots of the quartic polynomial with the provided coefficients.
  581. *
  582. * @param {Number} a The coefficient of the 4th order monomial.
  583. * @param {Number} b The coefficient of the 3rd order monomial.
  584. * @param {Number} c The coefficient of the 2nd order monomial.
  585. * @param {Number} d The coefficient of the 1st order monomial.
  586. * @param {Number} e The coefficient of the 0th order monomial.
  587. * @returns {Number[]} The real valued roots.
  588. */
  589. QuarticRealPolynomial.computeRealRoots = function (a, b, c, d, e) {
  590. //>>includeStart('debug', pragmas.debug);
  591. if (typeof a !== "number") {
  592. throw new RuntimeError.DeveloperError("a is a required number.");
  593. }
  594. if (typeof b !== "number") {
  595. throw new RuntimeError.DeveloperError("b is a required number.");
  596. }
  597. if (typeof c !== "number") {
  598. throw new RuntimeError.DeveloperError("c is a required number.");
  599. }
  600. if (typeof d !== "number") {
  601. throw new RuntimeError.DeveloperError("d is a required number.");
  602. }
  603. if (typeof e !== "number") {
  604. throw new RuntimeError.DeveloperError("e is a required number.");
  605. }
  606. //>>includeEnd('debug');
  607. if (Math.abs(a) < ComponentDatatype.CesiumMath.EPSILON15) {
  608. return CubicRealPolynomial$1.computeRealRoots(b, c, d, e);
  609. }
  610. const a3 = b / a;
  611. const a2 = c / a;
  612. const a1 = d / a;
  613. const a0 = e / a;
  614. let k = a3 < 0.0 ? 1 : 0;
  615. k += a2 < 0.0 ? k + 1 : k;
  616. k += a1 < 0.0 ? k + 1 : k;
  617. k += a0 < 0.0 ? k + 1 : k;
  618. switch (k) {
  619. case 0:
  620. return original(a3, a2, a1, a0);
  621. case 1:
  622. return neumark(a3, a2, a1, a0);
  623. case 2:
  624. return neumark(a3, a2, a1, a0);
  625. case 3:
  626. return original(a3, a2, a1, a0);
  627. case 4:
  628. return original(a3, a2, a1, a0);
  629. case 5:
  630. return neumark(a3, a2, a1, a0);
  631. case 6:
  632. return original(a3, a2, a1, a0);
  633. case 7:
  634. return original(a3, a2, a1, a0);
  635. case 8:
  636. return neumark(a3, a2, a1, a0);
  637. case 9:
  638. return original(a3, a2, a1, a0);
  639. case 10:
  640. return original(a3, a2, a1, a0);
  641. case 11:
  642. return neumark(a3, a2, a1, a0);
  643. case 12:
  644. return original(a3, a2, a1, a0);
  645. case 13:
  646. return original(a3, a2, a1, a0);
  647. case 14:
  648. return original(a3, a2, a1, a0);
  649. case 15:
  650. return original(a3, a2, a1, a0);
  651. default:
  652. return undefined;
  653. }
  654. };
  655. var QuarticRealPolynomial$1 = QuarticRealPolynomial;
  656. /**
  657. * Represents a ray that extends infinitely from the provided origin in the provided direction.
  658. * @alias Ray
  659. * @constructor
  660. *
  661. * @param {Cartesian3} [origin=Cartesian3.ZERO] The origin of the ray.
  662. * @param {Cartesian3} [direction=Cartesian3.ZERO] The direction of the ray.
  663. */
  664. function Ray(origin, direction) {
  665. direction = Matrix2.Cartesian3.clone(defaultValue.defaultValue(direction, Matrix2.Cartesian3.ZERO));
  666. if (!Matrix2.Cartesian3.equals(direction, Matrix2.Cartesian3.ZERO)) {
  667. Matrix2.Cartesian3.normalize(direction, direction);
  668. }
  669. /**
  670. * The origin of the ray.
  671. * @type {Cartesian3}
  672. * @default {@link Cartesian3.ZERO}
  673. */
  674. this.origin = Matrix2.Cartesian3.clone(defaultValue.defaultValue(origin, Matrix2.Cartesian3.ZERO));
  675. /**
  676. * The direction of the ray.
  677. * @type {Cartesian3}
  678. */
  679. this.direction = direction;
  680. }
  681. /**
  682. * Duplicates a Ray instance.
  683. *
  684. * @param {Ray} ray The ray to duplicate.
  685. * @param {Ray} [result] The object onto which to store the result.
  686. * @returns {Ray} The modified result parameter or a new Ray instance if one was not provided. (Returns undefined if ray is undefined)
  687. */
  688. Ray.clone = function (ray, result) {
  689. if (!defaultValue.defined(ray)) {
  690. return undefined;
  691. }
  692. if (!defaultValue.defined(result)) {
  693. return new Ray(ray.origin, ray.direction);
  694. }
  695. result.origin = Matrix2.Cartesian3.clone(ray.origin);
  696. result.direction = Matrix2.Cartesian3.clone(ray.direction);
  697. return result;
  698. };
  699. /**
  700. * Computes the point along the ray given by r(t) = o + t*d,
  701. * where o is the origin of the ray and d is the direction.
  702. *
  703. * @param {Ray} ray The ray.
  704. * @param {Number} t A scalar value.
  705. * @param {Cartesian3} [result] The object in which the result will be stored.
  706. * @returns {Cartesian3} The modified result parameter, or a new instance if none was provided.
  707. *
  708. * @example
  709. * //Get the first intersection point of a ray and an ellipsoid.
  710. * const intersection = Cesium.IntersectionTests.rayEllipsoid(ray, ellipsoid);
  711. * const point = Cesium.Ray.getPoint(ray, intersection.start);
  712. */
  713. Ray.getPoint = function (ray, t, result) {
  714. //>>includeStart('debug', pragmas.debug);
  715. RuntimeError.Check.typeOf.object("ray", ray);
  716. RuntimeError.Check.typeOf.number("t", t);
  717. //>>includeEnd('debug');
  718. if (!defaultValue.defined(result)) {
  719. result = new Matrix2.Cartesian3();
  720. }
  721. result = Matrix2.Cartesian3.multiplyByScalar(ray.direction, t, result);
  722. return Matrix2.Cartesian3.add(ray.origin, result, result);
  723. };
  724. /**
  725. * Functions for computing the intersection between geometries such as rays, planes, triangles, and ellipsoids.
  726. *
  727. * @namespace IntersectionTests
  728. */
  729. const IntersectionTests = {};
  730. /**
  731. * Computes the intersection of a ray and a plane.
  732. *
  733. * @param {Ray} ray The ray.
  734. * @param {Plane} plane The plane.
  735. * @param {Cartesian3} [result] The object onto which to store the result.
  736. * @returns {Cartesian3} The intersection point or undefined if there is no intersections.
  737. */
  738. IntersectionTests.rayPlane = function (ray, plane, result) {
  739. //>>includeStart('debug', pragmas.debug);
  740. if (!defaultValue.defined(ray)) {
  741. throw new RuntimeError.DeveloperError("ray is required.");
  742. }
  743. if (!defaultValue.defined(plane)) {
  744. throw new RuntimeError.DeveloperError("plane is required.");
  745. }
  746. //>>includeEnd('debug');
  747. if (!defaultValue.defined(result)) {
  748. result = new Matrix2.Cartesian3();
  749. }
  750. const origin = ray.origin;
  751. const direction = ray.direction;
  752. const normal = plane.normal;
  753. const denominator = Matrix2.Cartesian3.dot(normal, direction);
  754. if (Math.abs(denominator) < ComponentDatatype.CesiumMath.EPSILON15) {
  755. // Ray is parallel to plane. The ray may be in the polygon's plane.
  756. return undefined;
  757. }
  758. const t = (-plane.distance - Matrix2.Cartesian3.dot(normal, origin)) / denominator;
  759. if (t < 0) {
  760. return undefined;
  761. }
  762. result = Matrix2.Cartesian3.multiplyByScalar(direction, t, result);
  763. return Matrix2.Cartesian3.add(origin, result, result);
  764. };
  765. const scratchEdge0 = new Matrix2.Cartesian3();
  766. const scratchEdge1 = new Matrix2.Cartesian3();
  767. const scratchPVec = new Matrix2.Cartesian3();
  768. const scratchTVec = new Matrix2.Cartesian3();
  769. const scratchQVec = new Matrix2.Cartesian3();
  770. /**
  771. * 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.
  772. *
  773. * Implements {@link https://cadxfem.org/inf/Fast%20MinimumStorage%20RayTriangle%20Intersection.pdf|
  774. * Fast Minimum Storage Ray/Triangle Intersection} by Tomas Moller and Ben Trumbore.
  775. *
  776. * @memberof IntersectionTests
  777. *
  778. * @param {Ray} ray The ray.
  779. * @param {Cartesian3} p0 The first vertex of the triangle.
  780. * @param {Cartesian3} p1 The second vertex of the triangle.
  781. * @param {Cartesian3} p2 The third vertex of the triangle.
  782. * @param {Boolean} [cullBackFaces=false] If <code>true</code>, will only compute an intersection with the front face of the triangle
  783. * and return undefined for intersections with the back face.
  784. * @returns {Number} The intersection as a parametric distance along the ray, or undefined if there is no intersection.
  785. */
  786. IntersectionTests.rayTriangleParametric = function (
  787. ray,
  788. p0,
  789. p1,
  790. p2,
  791. cullBackFaces
  792. ) {
  793. //>>includeStart('debug', pragmas.debug);
  794. if (!defaultValue.defined(ray)) {
  795. throw new RuntimeError.DeveloperError("ray is required.");
  796. }
  797. if (!defaultValue.defined(p0)) {
  798. throw new RuntimeError.DeveloperError("p0 is required.");
  799. }
  800. if (!defaultValue.defined(p1)) {
  801. throw new RuntimeError.DeveloperError("p1 is required.");
  802. }
  803. if (!defaultValue.defined(p2)) {
  804. throw new RuntimeError.DeveloperError("p2 is required.");
  805. }
  806. //>>includeEnd('debug');
  807. cullBackFaces = defaultValue.defaultValue(cullBackFaces, false);
  808. const origin = ray.origin;
  809. const direction = ray.direction;
  810. const edge0 = Matrix2.Cartesian3.subtract(p1, p0, scratchEdge0);
  811. const edge1 = Matrix2.Cartesian3.subtract(p2, p0, scratchEdge1);
  812. const p = Matrix2.Cartesian3.cross(direction, edge1, scratchPVec);
  813. const det = Matrix2.Cartesian3.dot(edge0, p);
  814. let tvec;
  815. let q;
  816. let u;
  817. let v;
  818. let t;
  819. if (cullBackFaces) {
  820. if (det < ComponentDatatype.CesiumMath.EPSILON6) {
  821. return undefined;
  822. }
  823. tvec = Matrix2.Cartesian3.subtract(origin, p0, scratchTVec);
  824. u = Matrix2.Cartesian3.dot(tvec, p);
  825. if (u < 0.0 || u > det) {
  826. return undefined;
  827. }
  828. q = Matrix2.Cartesian3.cross(tvec, edge0, scratchQVec);
  829. v = Matrix2.Cartesian3.dot(direction, q);
  830. if (v < 0.0 || u + v > det) {
  831. return undefined;
  832. }
  833. t = Matrix2.Cartesian3.dot(edge1, q) / det;
  834. } else {
  835. if (Math.abs(det) < ComponentDatatype.CesiumMath.EPSILON6) {
  836. return undefined;
  837. }
  838. const invDet = 1.0 / det;
  839. tvec = Matrix2.Cartesian3.subtract(origin, p0, scratchTVec);
  840. u = Matrix2.Cartesian3.dot(tvec, p) * invDet;
  841. if (u < 0.0 || u > 1.0) {
  842. return undefined;
  843. }
  844. q = Matrix2.Cartesian3.cross(tvec, edge0, scratchQVec);
  845. v = Matrix2.Cartesian3.dot(direction, q) * invDet;
  846. if (v < 0.0 || u + v > 1.0) {
  847. return undefined;
  848. }
  849. t = Matrix2.Cartesian3.dot(edge1, q) * invDet;
  850. }
  851. return t;
  852. };
  853. /**
  854. * Computes the intersection of a ray and a triangle as a Cartesian3 coordinate.
  855. *
  856. * Implements {@link https://cadxfem.org/inf/Fast%20MinimumStorage%20RayTriangle%20Intersection.pdf|
  857. * Fast Minimum Storage Ray/Triangle Intersection} by Tomas Moller and Ben Trumbore.
  858. *
  859. * @memberof IntersectionTests
  860. *
  861. * @param {Ray} ray The ray.
  862. * @param {Cartesian3} p0 The first vertex of the triangle.
  863. * @param {Cartesian3} p1 The second vertex of the triangle.
  864. * @param {Cartesian3} p2 The third vertex of the triangle.
  865. * @param {Boolean} [cullBackFaces=false] If <code>true</code>, will only compute an intersection with the front face of the triangle
  866. * and return undefined for intersections with the back face.
  867. * @param {Cartesian3} [result] The <code>Cartesian3</code> onto which to store the result.
  868. * @returns {Cartesian3} The intersection point or undefined if there is no intersections.
  869. */
  870. IntersectionTests.rayTriangle = function (
  871. ray,
  872. p0,
  873. p1,
  874. p2,
  875. cullBackFaces,
  876. result
  877. ) {
  878. const t = IntersectionTests.rayTriangleParametric(
  879. ray,
  880. p0,
  881. p1,
  882. p2,
  883. cullBackFaces
  884. );
  885. if (!defaultValue.defined(t) || t < 0.0) {
  886. return undefined;
  887. }
  888. if (!defaultValue.defined(result)) {
  889. result = new Matrix2.Cartesian3();
  890. }
  891. Matrix2.Cartesian3.multiplyByScalar(ray.direction, t, result);
  892. return Matrix2.Cartesian3.add(ray.origin, result, result);
  893. };
  894. const scratchLineSegmentTriangleRay = new Ray();
  895. /**
  896. * Computes the intersection of a line segment and a triangle.
  897. * @memberof IntersectionTests
  898. *
  899. * @param {Cartesian3} v0 The an end point of the line segment.
  900. * @param {Cartesian3} v1 The other end point of the line segment.
  901. * @param {Cartesian3} p0 The first vertex of the triangle.
  902. * @param {Cartesian3} p1 The second vertex of the triangle.
  903. * @param {Cartesian3} p2 The third vertex of the triangle.
  904. * @param {Boolean} [cullBackFaces=false] If <code>true</code>, will only compute an intersection with the front face of the triangle
  905. * and return undefined for intersections with the back face.
  906. * @param {Cartesian3} [result] The <code>Cartesian3</code> onto which to store the result.
  907. * @returns {Cartesian3} The intersection point or undefined if there is no intersections.
  908. */
  909. IntersectionTests.lineSegmentTriangle = function (
  910. v0,
  911. v1,
  912. p0,
  913. p1,
  914. p2,
  915. cullBackFaces,
  916. result
  917. ) {
  918. //>>includeStart('debug', pragmas.debug);
  919. if (!defaultValue.defined(v0)) {
  920. throw new RuntimeError.DeveloperError("v0 is required.");
  921. }
  922. if (!defaultValue.defined(v1)) {
  923. throw new RuntimeError.DeveloperError("v1 is required.");
  924. }
  925. if (!defaultValue.defined(p0)) {
  926. throw new RuntimeError.DeveloperError("p0 is required.");
  927. }
  928. if (!defaultValue.defined(p1)) {
  929. throw new RuntimeError.DeveloperError("p1 is required.");
  930. }
  931. if (!defaultValue.defined(p2)) {
  932. throw new RuntimeError.DeveloperError("p2 is required.");
  933. }
  934. //>>includeEnd('debug');
  935. const ray = scratchLineSegmentTriangleRay;
  936. Matrix2.Cartesian3.clone(v0, ray.origin);
  937. Matrix2.Cartesian3.subtract(v1, v0, ray.direction);
  938. Matrix2.Cartesian3.normalize(ray.direction, ray.direction);
  939. const t = IntersectionTests.rayTriangleParametric(
  940. ray,
  941. p0,
  942. p1,
  943. p2,
  944. cullBackFaces
  945. );
  946. if (!defaultValue.defined(t) || t < 0.0 || t > Matrix2.Cartesian3.distance(v0, v1)) {
  947. return undefined;
  948. }
  949. if (!defaultValue.defined(result)) {
  950. result = new Matrix2.Cartesian3();
  951. }
  952. Matrix2.Cartesian3.multiplyByScalar(ray.direction, t, result);
  953. return Matrix2.Cartesian3.add(ray.origin, result, result);
  954. };
  955. function solveQuadratic(a, b, c, result) {
  956. const det = b * b - 4.0 * a * c;
  957. if (det < 0.0) {
  958. return undefined;
  959. } else if (det > 0.0) {
  960. const denom = 1.0 / (2.0 * a);
  961. const disc = Math.sqrt(det);
  962. const root0 = (-b + disc) * denom;
  963. const root1 = (-b - disc) * denom;
  964. if (root0 < root1) {
  965. result.root0 = root0;
  966. result.root1 = root1;
  967. } else {
  968. result.root0 = root1;
  969. result.root1 = root0;
  970. }
  971. return result;
  972. }
  973. const root = -b / (2.0 * a);
  974. if (root === 0.0) {
  975. return undefined;
  976. }
  977. result.root0 = result.root1 = root;
  978. return result;
  979. }
  980. const raySphereRoots = {
  981. root0: 0.0,
  982. root1: 0.0,
  983. };
  984. function raySphere(ray, sphere, result) {
  985. if (!defaultValue.defined(result)) {
  986. result = new Transforms.Interval();
  987. }
  988. const origin = ray.origin;
  989. const direction = ray.direction;
  990. const center = sphere.center;
  991. const radiusSquared = sphere.radius * sphere.radius;
  992. const diff = Matrix2.Cartesian3.subtract(origin, center, scratchPVec);
  993. const a = Matrix2.Cartesian3.dot(direction, direction);
  994. const b = 2.0 * Matrix2.Cartesian3.dot(direction, diff);
  995. const c = Matrix2.Cartesian3.magnitudeSquared(diff) - radiusSquared;
  996. const roots = solveQuadratic(a, b, c, raySphereRoots);
  997. if (!defaultValue.defined(roots)) {
  998. return undefined;
  999. }
  1000. result.start = roots.root0;
  1001. result.stop = roots.root1;
  1002. return result;
  1003. }
  1004. /**
  1005. * Computes the intersection points of a ray with a sphere.
  1006. * @memberof IntersectionTests
  1007. *
  1008. * @param {Ray} ray The ray.
  1009. * @param {BoundingSphere} sphere The sphere.
  1010. * @param {Interval} [result] The result onto which to store the result.
  1011. * @returns {Interval} The interval containing scalar points along the ray or undefined if there are no intersections.
  1012. */
  1013. IntersectionTests.raySphere = function (ray, sphere, result) {
  1014. //>>includeStart('debug', pragmas.debug);
  1015. if (!defaultValue.defined(ray)) {
  1016. throw new RuntimeError.DeveloperError("ray is required.");
  1017. }
  1018. if (!defaultValue.defined(sphere)) {
  1019. throw new RuntimeError.DeveloperError("sphere is required.");
  1020. }
  1021. //>>includeEnd('debug');
  1022. result = raySphere(ray, sphere, result);
  1023. if (!defaultValue.defined(result) || result.stop < 0.0) {
  1024. return undefined;
  1025. }
  1026. result.start = Math.max(result.start, 0.0);
  1027. return result;
  1028. };
  1029. const scratchLineSegmentRay = new Ray();
  1030. /**
  1031. * Computes the intersection points of a line segment with a sphere.
  1032. * @memberof IntersectionTests
  1033. *
  1034. * @param {Cartesian3} p0 An end point of the line segment.
  1035. * @param {Cartesian3} p1 The other end point of the line segment.
  1036. * @param {BoundingSphere} sphere The sphere.
  1037. * @param {Interval} [result] The result onto which to store the result.
  1038. * @returns {Interval} The interval containing scalar points along the ray or undefined if there are no intersections.
  1039. */
  1040. IntersectionTests.lineSegmentSphere = function (p0, p1, sphere, result) {
  1041. //>>includeStart('debug', pragmas.debug);
  1042. if (!defaultValue.defined(p0)) {
  1043. throw new RuntimeError.DeveloperError("p0 is required.");
  1044. }
  1045. if (!defaultValue.defined(p1)) {
  1046. throw new RuntimeError.DeveloperError("p1 is required.");
  1047. }
  1048. if (!defaultValue.defined(sphere)) {
  1049. throw new RuntimeError.DeveloperError("sphere is required.");
  1050. }
  1051. //>>includeEnd('debug');
  1052. const ray = scratchLineSegmentRay;
  1053. Matrix2.Cartesian3.clone(p0, ray.origin);
  1054. const direction = Matrix2.Cartesian3.subtract(p1, p0, ray.direction);
  1055. const maxT = Matrix2.Cartesian3.magnitude(direction);
  1056. Matrix2.Cartesian3.normalize(direction, direction);
  1057. result = raySphere(ray, sphere, result);
  1058. if (!defaultValue.defined(result) || result.stop < 0.0 || result.start > maxT) {
  1059. return undefined;
  1060. }
  1061. result.start = Math.max(result.start, 0.0);
  1062. result.stop = Math.min(result.stop, maxT);
  1063. return result;
  1064. };
  1065. const scratchQ = new Matrix2.Cartesian3();
  1066. const scratchW = new Matrix2.Cartesian3();
  1067. /**
  1068. * Computes the intersection points of a ray with an ellipsoid.
  1069. *
  1070. * @param {Ray} ray The ray.
  1071. * @param {Ellipsoid} ellipsoid The ellipsoid.
  1072. * @returns {Interval} The interval containing scalar points along the ray or undefined if there are no intersections.
  1073. */
  1074. IntersectionTests.rayEllipsoid = function (ray, ellipsoid) {
  1075. //>>includeStart('debug', pragmas.debug);
  1076. if (!defaultValue.defined(ray)) {
  1077. throw new RuntimeError.DeveloperError("ray is required.");
  1078. }
  1079. if (!defaultValue.defined(ellipsoid)) {
  1080. throw new RuntimeError.DeveloperError("ellipsoid is required.");
  1081. }
  1082. //>>includeEnd('debug');
  1083. const inverseRadii = ellipsoid.oneOverRadii;
  1084. const q = Matrix2.Cartesian3.multiplyComponents(inverseRadii, ray.origin, scratchQ);
  1085. const w = Matrix2.Cartesian3.multiplyComponents(
  1086. inverseRadii,
  1087. ray.direction,
  1088. scratchW
  1089. );
  1090. const q2 = Matrix2.Cartesian3.magnitudeSquared(q);
  1091. const qw = Matrix2.Cartesian3.dot(q, w);
  1092. let difference, w2, product, discriminant, temp;
  1093. if (q2 > 1.0) {
  1094. // Outside ellipsoid.
  1095. if (qw >= 0.0) {
  1096. // Looking outward or tangent (0 intersections).
  1097. return undefined;
  1098. }
  1099. // qw < 0.0.
  1100. const qw2 = qw * qw;
  1101. difference = q2 - 1.0; // Positively valued.
  1102. w2 = Matrix2.Cartesian3.magnitudeSquared(w);
  1103. product = w2 * difference;
  1104. if (qw2 < product) {
  1105. // Imaginary roots (0 intersections).
  1106. return undefined;
  1107. } else if (qw2 > product) {
  1108. // Distinct roots (2 intersections).
  1109. discriminant = qw * qw - product;
  1110. temp = -qw + Math.sqrt(discriminant); // Avoid cancellation.
  1111. const root0 = temp / w2;
  1112. const root1 = difference / temp;
  1113. if (root0 < root1) {
  1114. return new Transforms.Interval(root0, root1);
  1115. }
  1116. return {
  1117. start: root1,
  1118. stop: root0,
  1119. };
  1120. }
  1121. // qw2 == product. Repeated roots (2 intersections).
  1122. const root = Math.sqrt(difference / w2);
  1123. return new Transforms.Interval(root, root);
  1124. } else if (q2 < 1.0) {
  1125. // Inside ellipsoid (2 intersections).
  1126. difference = q2 - 1.0; // Negatively valued.
  1127. w2 = Matrix2.Cartesian3.magnitudeSquared(w);
  1128. product = w2 * difference; // Negatively valued.
  1129. discriminant = qw * qw - product;
  1130. temp = -qw + Math.sqrt(discriminant); // Positively valued.
  1131. return new Transforms.Interval(0.0, temp / w2);
  1132. }
  1133. // q2 == 1.0. On ellipsoid.
  1134. if (qw < 0.0) {
  1135. // Looking inward.
  1136. w2 = Matrix2.Cartesian3.magnitudeSquared(w);
  1137. return new Transforms.Interval(0.0, -qw / w2);
  1138. }
  1139. // qw >= 0.0. Looking outward or tangent.
  1140. return undefined;
  1141. };
  1142. function addWithCancellationCheck(left, right, tolerance) {
  1143. const difference = left + right;
  1144. if (
  1145. ComponentDatatype.CesiumMath.sign(left) !== ComponentDatatype.CesiumMath.sign(right) &&
  1146. Math.abs(difference / Math.max(Math.abs(left), Math.abs(right))) < tolerance
  1147. ) {
  1148. return 0.0;
  1149. }
  1150. return difference;
  1151. }
  1152. function quadraticVectorExpression(A, b, c, x, w) {
  1153. const xSquared = x * x;
  1154. const wSquared = w * w;
  1155. const l2 = (A[Matrix2.Matrix3.COLUMN1ROW1] - A[Matrix2.Matrix3.COLUMN2ROW2]) * wSquared;
  1156. const l1 =
  1157. w *
  1158. (x *
  1159. addWithCancellationCheck(
  1160. A[Matrix2.Matrix3.COLUMN1ROW0],
  1161. A[Matrix2.Matrix3.COLUMN0ROW1],
  1162. ComponentDatatype.CesiumMath.EPSILON15
  1163. ) +
  1164. b.y);
  1165. const l0 =
  1166. A[Matrix2.Matrix3.COLUMN0ROW0] * xSquared +
  1167. A[Matrix2.Matrix3.COLUMN2ROW2] * wSquared +
  1168. x * b.x +
  1169. c;
  1170. const r1 =
  1171. wSquared *
  1172. addWithCancellationCheck(
  1173. A[Matrix2.Matrix3.COLUMN2ROW1],
  1174. A[Matrix2.Matrix3.COLUMN1ROW2],
  1175. ComponentDatatype.CesiumMath.EPSILON15
  1176. );
  1177. const r0 =
  1178. w *
  1179. (x *
  1180. addWithCancellationCheck(A[Matrix2.Matrix3.COLUMN2ROW0], A[Matrix2.Matrix3.COLUMN0ROW2]) +
  1181. b.z);
  1182. let cosines;
  1183. const solutions = [];
  1184. if (r0 === 0.0 && r1 === 0.0) {
  1185. cosines = QuadraticRealPolynomial$1.computeRealRoots(l2, l1, l0);
  1186. if (cosines.length === 0) {
  1187. return solutions;
  1188. }
  1189. const cosine0 = cosines[0];
  1190. const sine0 = Math.sqrt(Math.max(1.0 - cosine0 * cosine0, 0.0));
  1191. solutions.push(new Matrix2.Cartesian3(x, w * cosine0, w * -sine0));
  1192. solutions.push(new Matrix2.Cartesian3(x, w * cosine0, w * sine0));
  1193. if (cosines.length === 2) {
  1194. const cosine1 = cosines[1];
  1195. const sine1 = Math.sqrt(Math.max(1.0 - cosine1 * cosine1, 0.0));
  1196. solutions.push(new Matrix2.Cartesian3(x, w * cosine1, w * -sine1));
  1197. solutions.push(new Matrix2.Cartesian3(x, w * cosine1, w * sine1));
  1198. }
  1199. return solutions;
  1200. }
  1201. const r0Squared = r0 * r0;
  1202. const r1Squared = r1 * r1;
  1203. const l2Squared = l2 * l2;
  1204. const r0r1 = r0 * r1;
  1205. const c4 = l2Squared + r1Squared;
  1206. const c3 = 2.0 * (l1 * l2 + r0r1);
  1207. const c2 = 2.0 * l0 * l2 + l1 * l1 - r1Squared + r0Squared;
  1208. const c1 = 2.0 * (l0 * l1 - r0r1);
  1209. const c0 = l0 * l0 - r0Squared;
  1210. if (c4 === 0.0 && c3 === 0.0 && c2 === 0.0 && c1 === 0.0) {
  1211. return solutions;
  1212. }
  1213. cosines = QuarticRealPolynomial$1.computeRealRoots(c4, c3, c2, c1, c0);
  1214. const length = cosines.length;
  1215. if (length === 0) {
  1216. return solutions;
  1217. }
  1218. for (let i = 0; i < length; ++i) {
  1219. const cosine = cosines[i];
  1220. const cosineSquared = cosine * cosine;
  1221. const sineSquared = Math.max(1.0 - cosineSquared, 0.0);
  1222. const sine = Math.sqrt(sineSquared);
  1223. //const left = l2 * cosineSquared + l1 * cosine + l0;
  1224. let left;
  1225. if (ComponentDatatype.CesiumMath.sign(l2) === ComponentDatatype.CesiumMath.sign(l0)) {
  1226. left = addWithCancellationCheck(
  1227. l2 * cosineSquared + l0,
  1228. l1 * cosine,
  1229. ComponentDatatype.CesiumMath.EPSILON12
  1230. );
  1231. } else if (ComponentDatatype.CesiumMath.sign(l0) === ComponentDatatype.CesiumMath.sign(l1 * cosine)) {
  1232. left = addWithCancellationCheck(
  1233. l2 * cosineSquared,
  1234. l1 * cosine + l0,
  1235. ComponentDatatype.CesiumMath.EPSILON12
  1236. );
  1237. } else {
  1238. left = addWithCancellationCheck(
  1239. l2 * cosineSquared + l1 * cosine,
  1240. l0,
  1241. ComponentDatatype.CesiumMath.EPSILON12
  1242. );
  1243. }
  1244. const right = addWithCancellationCheck(
  1245. r1 * cosine,
  1246. r0,
  1247. ComponentDatatype.CesiumMath.EPSILON15
  1248. );
  1249. const product = left * right;
  1250. if (product < 0.0) {
  1251. solutions.push(new Matrix2.Cartesian3(x, w * cosine, w * sine));
  1252. } else if (product > 0.0) {
  1253. solutions.push(new Matrix2.Cartesian3(x, w * cosine, w * -sine));
  1254. } else if (sine !== 0.0) {
  1255. solutions.push(new Matrix2.Cartesian3(x, w * cosine, w * -sine));
  1256. solutions.push(new Matrix2.Cartesian3(x, w * cosine, w * sine));
  1257. ++i;
  1258. } else {
  1259. solutions.push(new Matrix2.Cartesian3(x, w * cosine, w * sine));
  1260. }
  1261. }
  1262. return solutions;
  1263. }
  1264. const firstAxisScratch = new Matrix2.Cartesian3();
  1265. const secondAxisScratch = new Matrix2.Cartesian3();
  1266. const thirdAxisScratch = new Matrix2.Cartesian3();
  1267. const referenceScratch = new Matrix2.Cartesian3();
  1268. const bCart = new Matrix2.Cartesian3();
  1269. const bScratch = new Matrix2.Matrix3();
  1270. const btScratch = new Matrix2.Matrix3();
  1271. const diScratch = new Matrix2.Matrix3();
  1272. const dScratch = new Matrix2.Matrix3();
  1273. const cScratch = new Matrix2.Matrix3();
  1274. const tempMatrix = new Matrix2.Matrix3();
  1275. const aScratch = new Matrix2.Matrix3();
  1276. const sScratch = new Matrix2.Cartesian3();
  1277. const closestScratch = new Matrix2.Cartesian3();
  1278. const surfPointScratch = new Matrix2.Cartographic();
  1279. /**
  1280. * Provides the point along the ray which is nearest to the ellipsoid.
  1281. *
  1282. * @param {Ray} ray The ray.
  1283. * @param {Ellipsoid} ellipsoid The ellipsoid.
  1284. * @returns {Cartesian3} The nearest planetodetic point on the ray.
  1285. */
  1286. IntersectionTests.grazingAltitudeLocation = function (ray, ellipsoid) {
  1287. //>>includeStart('debug', pragmas.debug);
  1288. if (!defaultValue.defined(ray)) {
  1289. throw new RuntimeError.DeveloperError("ray is required.");
  1290. }
  1291. if (!defaultValue.defined(ellipsoid)) {
  1292. throw new RuntimeError.DeveloperError("ellipsoid is required.");
  1293. }
  1294. //>>includeEnd('debug');
  1295. const position = ray.origin;
  1296. const direction = ray.direction;
  1297. if (!Matrix2.Cartesian3.equals(position, Matrix2.Cartesian3.ZERO)) {
  1298. const normal = ellipsoid.geodeticSurfaceNormal(position, firstAxisScratch);
  1299. if (Matrix2.Cartesian3.dot(direction, normal) >= 0.0) {
  1300. // The location provided is the closest point in altitude
  1301. return position;
  1302. }
  1303. }
  1304. const intersects = defaultValue.defined(this.rayEllipsoid(ray, ellipsoid));
  1305. // Compute the scaled direction vector.
  1306. const f = ellipsoid.transformPositionToScaledSpace(
  1307. direction,
  1308. firstAxisScratch
  1309. );
  1310. // Constructs a basis from the unit scaled direction vector. Construct its rotation and transpose.
  1311. const firstAxis = Matrix2.Cartesian3.normalize(f, f);
  1312. const reference = Matrix2.Cartesian3.mostOrthogonalAxis(f, referenceScratch);
  1313. const secondAxis = Matrix2.Cartesian3.normalize(
  1314. Matrix2.Cartesian3.cross(reference, firstAxis, secondAxisScratch),
  1315. secondAxisScratch
  1316. );
  1317. const thirdAxis = Matrix2.Cartesian3.normalize(
  1318. Matrix2.Cartesian3.cross(firstAxis, secondAxis, thirdAxisScratch),
  1319. thirdAxisScratch
  1320. );
  1321. const B = bScratch;
  1322. B[0] = firstAxis.x;
  1323. B[1] = firstAxis.y;
  1324. B[2] = firstAxis.z;
  1325. B[3] = secondAxis.x;
  1326. B[4] = secondAxis.y;
  1327. B[5] = secondAxis.z;
  1328. B[6] = thirdAxis.x;
  1329. B[7] = thirdAxis.y;
  1330. B[8] = thirdAxis.z;
  1331. const B_T = Matrix2.Matrix3.transpose(B, btScratch);
  1332. // Get the scaling matrix and its inverse.
  1333. const D_I = Matrix2.Matrix3.fromScale(ellipsoid.radii, diScratch);
  1334. const D = Matrix2.Matrix3.fromScale(ellipsoid.oneOverRadii, dScratch);
  1335. const C = cScratch;
  1336. C[0] = 0.0;
  1337. C[1] = -direction.z;
  1338. C[2] = direction.y;
  1339. C[3] = direction.z;
  1340. C[4] = 0.0;
  1341. C[5] = -direction.x;
  1342. C[6] = -direction.y;
  1343. C[7] = direction.x;
  1344. C[8] = 0.0;
  1345. const temp = Matrix2.Matrix3.multiply(
  1346. Matrix2.Matrix3.multiply(B_T, D, tempMatrix),
  1347. C,
  1348. tempMatrix
  1349. );
  1350. const A = Matrix2.Matrix3.multiply(
  1351. Matrix2.Matrix3.multiply(temp, D_I, aScratch),
  1352. B,
  1353. aScratch
  1354. );
  1355. const b = Matrix2.Matrix3.multiplyByVector(temp, position, bCart);
  1356. // Solve for the solutions to the expression in standard form:
  1357. const solutions = quadraticVectorExpression(
  1358. A,
  1359. Matrix2.Cartesian3.negate(b, firstAxisScratch),
  1360. 0.0,
  1361. 0.0,
  1362. 1.0
  1363. );
  1364. let s;
  1365. let altitude;
  1366. const length = solutions.length;
  1367. if (length > 0) {
  1368. let closest = Matrix2.Cartesian3.clone(Matrix2.Cartesian3.ZERO, closestScratch);
  1369. let maximumValue = Number.NEGATIVE_INFINITY;
  1370. for (let i = 0; i < length; ++i) {
  1371. s = Matrix2.Matrix3.multiplyByVector(
  1372. D_I,
  1373. Matrix2.Matrix3.multiplyByVector(B, solutions[i], sScratch),
  1374. sScratch
  1375. );
  1376. const v = Matrix2.Cartesian3.normalize(
  1377. Matrix2.Cartesian3.subtract(s, position, referenceScratch),
  1378. referenceScratch
  1379. );
  1380. const dotProduct = Matrix2.Cartesian3.dot(v, direction);
  1381. if (dotProduct > maximumValue) {
  1382. maximumValue = dotProduct;
  1383. closest = Matrix2.Cartesian3.clone(s, closest);
  1384. }
  1385. }
  1386. const surfacePoint = ellipsoid.cartesianToCartographic(
  1387. closest,
  1388. surfPointScratch
  1389. );
  1390. maximumValue = ComponentDatatype.CesiumMath.clamp(maximumValue, 0.0, 1.0);
  1391. altitude =
  1392. Matrix2.Cartesian3.magnitude(
  1393. Matrix2.Cartesian3.subtract(closest, position, referenceScratch)
  1394. ) * Math.sqrt(1.0 - maximumValue * maximumValue);
  1395. altitude = intersects ? -altitude : altitude;
  1396. surfacePoint.height = altitude;
  1397. return ellipsoid.cartographicToCartesian(surfacePoint, new Matrix2.Cartesian3());
  1398. }
  1399. return undefined;
  1400. };
  1401. const lineSegmentPlaneDifference = new Matrix2.Cartesian3();
  1402. /**
  1403. * Computes the intersection of a line segment and a plane.
  1404. *
  1405. * @param {Cartesian3} endPoint0 An end point of the line segment.
  1406. * @param {Cartesian3} endPoint1 The other end point of the line segment.
  1407. * @param {Plane} plane The plane.
  1408. * @param {Cartesian3} [result] The object onto which to store the result.
  1409. * @returns {Cartesian3} The intersection point or undefined if there is no intersection.
  1410. *
  1411. * @example
  1412. * const origin = Cesium.Cartesian3.fromDegrees(-75.59777, 40.03883);
  1413. * const normal = ellipsoid.geodeticSurfaceNormal(origin);
  1414. * const plane = Cesium.Plane.fromPointNormal(origin, normal);
  1415. *
  1416. * const p0 = new Cesium.Cartesian3(...);
  1417. * const p1 = new Cesium.Cartesian3(...);
  1418. *
  1419. * // find the intersection of the line segment from p0 to p1 and the tangent plane at origin.
  1420. * const intersection = Cesium.IntersectionTests.lineSegmentPlane(p0, p1, plane);
  1421. */
  1422. IntersectionTests.lineSegmentPlane = function (
  1423. endPoint0,
  1424. endPoint1,
  1425. plane,
  1426. result
  1427. ) {
  1428. //>>includeStart('debug', pragmas.debug);
  1429. if (!defaultValue.defined(endPoint0)) {
  1430. throw new RuntimeError.DeveloperError("endPoint0 is required.");
  1431. }
  1432. if (!defaultValue.defined(endPoint1)) {
  1433. throw new RuntimeError.DeveloperError("endPoint1 is required.");
  1434. }
  1435. if (!defaultValue.defined(plane)) {
  1436. throw new RuntimeError.DeveloperError("plane is required.");
  1437. }
  1438. //>>includeEnd('debug');
  1439. if (!defaultValue.defined(result)) {
  1440. result = new Matrix2.Cartesian3();
  1441. }
  1442. const difference = Matrix2.Cartesian3.subtract(
  1443. endPoint1,
  1444. endPoint0,
  1445. lineSegmentPlaneDifference
  1446. );
  1447. const normal = plane.normal;
  1448. const nDotDiff = Matrix2.Cartesian3.dot(normal, difference);
  1449. // check if the segment and plane are parallel
  1450. if (Math.abs(nDotDiff) < ComponentDatatype.CesiumMath.EPSILON6) {
  1451. return undefined;
  1452. }
  1453. const nDotP0 = Matrix2.Cartesian3.dot(normal, endPoint0);
  1454. const t = -(plane.distance + nDotP0) / nDotDiff;
  1455. // intersection only if t is in [0, 1]
  1456. if (t < 0.0 || t > 1.0) {
  1457. return undefined;
  1458. }
  1459. // intersection is endPoint0 + t * (endPoint1 - endPoint0)
  1460. Matrix2.Cartesian3.multiplyByScalar(difference, t, result);
  1461. Matrix2.Cartesian3.add(endPoint0, result, result);
  1462. return result;
  1463. };
  1464. /**
  1465. * Computes the intersection of a triangle and a plane
  1466. *
  1467. * @param {Cartesian3} p0 First point of the triangle
  1468. * @param {Cartesian3} p1 Second point of the triangle
  1469. * @param {Cartesian3} p2 Third point of the triangle
  1470. * @param {Plane} plane Intersection plane
  1471. * @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)
  1472. *
  1473. * @example
  1474. * const origin = Cesium.Cartesian3.fromDegrees(-75.59777, 40.03883);
  1475. * const normal = ellipsoid.geodeticSurfaceNormal(origin);
  1476. * const plane = Cesium.Plane.fromPointNormal(origin, normal);
  1477. *
  1478. * const p0 = new Cesium.Cartesian3(...);
  1479. * const p1 = new Cesium.Cartesian3(...);
  1480. * const p2 = new Cesium.Cartesian3(...);
  1481. *
  1482. * // convert the triangle composed of points (p0, p1, p2) to three triangles that don't cross the plane
  1483. * const triangles = Cesium.IntersectionTests.trianglePlaneIntersection(p0, p1, p2, plane);
  1484. */
  1485. IntersectionTests.trianglePlaneIntersection = function (p0, p1, p2, plane) {
  1486. //>>includeStart('debug', pragmas.debug);
  1487. if (!defaultValue.defined(p0) || !defaultValue.defined(p1) || !defaultValue.defined(p2) || !defaultValue.defined(plane)) {
  1488. throw new RuntimeError.DeveloperError("p0, p1, p2, and plane are required.");
  1489. }
  1490. //>>includeEnd('debug');
  1491. const planeNormal = plane.normal;
  1492. const planeD = plane.distance;
  1493. const p0Behind = Matrix2.Cartesian3.dot(planeNormal, p0) + planeD < 0.0;
  1494. const p1Behind = Matrix2.Cartesian3.dot(planeNormal, p1) + planeD < 0.0;
  1495. const p2Behind = Matrix2.Cartesian3.dot(planeNormal, p2) + planeD < 0.0;
  1496. // Given these dots products, the calls to lineSegmentPlaneIntersection
  1497. // always have defined results.
  1498. let numBehind = 0;
  1499. numBehind += p0Behind ? 1 : 0;
  1500. numBehind += p1Behind ? 1 : 0;
  1501. numBehind += p2Behind ? 1 : 0;
  1502. let u1, u2;
  1503. if (numBehind === 1 || numBehind === 2) {
  1504. u1 = new Matrix2.Cartesian3();
  1505. u2 = new Matrix2.Cartesian3();
  1506. }
  1507. if (numBehind === 1) {
  1508. if (p0Behind) {
  1509. IntersectionTests.lineSegmentPlane(p0, p1, plane, u1);
  1510. IntersectionTests.lineSegmentPlane(p0, p2, plane, u2);
  1511. return {
  1512. positions: [p0, p1, p2, u1, u2],
  1513. indices: [
  1514. // Behind
  1515. 0,
  1516. 3,
  1517. 4,
  1518. // In front
  1519. 1,
  1520. 2,
  1521. 4,
  1522. 1,
  1523. 4,
  1524. 3,
  1525. ],
  1526. };
  1527. } else if (p1Behind) {
  1528. IntersectionTests.lineSegmentPlane(p1, p2, plane, u1);
  1529. IntersectionTests.lineSegmentPlane(p1, p0, plane, u2);
  1530. return {
  1531. positions: [p0, p1, p2, u1, u2],
  1532. indices: [
  1533. // Behind
  1534. 1,
  1535. 3,
  1536. 4,
  1537. // In front
  1538. 2,
  1539. 0,
  1540. 4,
  1541. 2,
  1542. 4,
  1543. 3,
  1544. ],
  1545. };
  1546. } else if (p2Behind) {
  1547. IntersectionTests.lineSegmentPlane(p2, p0, plane, u1);
  1548. IntersectionTests.lineSegmentPlane(p2, p1, plane, u2);
  1549. return {
  1550. positions: [p0, p1, p2, u1, u2],
  1551. indices: [
  1552. // Behind
  1553. 2,
  1554. 3,
  1555. 4,
  1556. // In front
  1557. 0,
  1558. 1,
  1559. 4,
  1560. 0,
  1561. 4,
  1562. 3,
  1563. ],
  1564. };
  1565. }
  1566. } else if (numBehind === 2) {
  1567. if (!p0Behind) {
  1568. IntersectionTests.lineSegmentPlane(p1, p0, plane, u1);
  1569. IntersectionTests.lineSegmentPlane(p2, p0, plane, u2);
  1570. return {
  1571. positions: [p0, p1, p2, u1, u2],
  1572. indices: [
  1573. // Behind
  1574. 1,
  1575. 2,
  1576. 4,
  1577. 1,
  1578. 4,
  1579. 3,
  1580. // In front
  1581. 0,
  1582. 3,
  1583. 4,
  1584. ],
  1585. };
  1586. } else if (!p1Behind) {
  1587. IntersectionTests.lineSegmentPlane(p2, p1, plane, u1);
  1588. IntersectionTests.lineSegmentPlane(p0, p1, plane, u2);
  1589. return {
  1590. positions: [p0, p1, p2, u1, u2],
  1591. indices: [
  1592. // Behind
  1593. 2,
  1594. 0,
  1595. 4,
  1596. 2,
  1597. 4,
  1598. 3,
  1599. // In front
  1600. 1,
  1601. 3,
  1602. 4,
  1603. ],
  1604. };
  1605. } else if (!p2Behind) {
  1606. IntersectionTests.lineSegmentPlane(p0, p2, plane, u1);
  1607. IntersectionTests.lineSegmentPlane(p1, p2, plane, u2);
  1608. return {
  1609. positions: [p0, p1, p2, u1, u2],
  1610. indices: [
  1611. // Behind
  1612. 0,
  1613. 1,
  1614. 4,
  1615. 0,
  1616. 4,
  1617. 3,
  1618. // In front
  1619. 2,
  1620. 3,
  1621. 4,
  1622. ],
  1623. };
  1624. }
  1625. }
  1626. // if numBehind is 3, the triangle is completely behind the plane;
  1627. // otherwise, it is completely in front (numBehind is 0).
  1628. return undefined;
  1629. };
  1630. var IntersectionTests$1 = IntersectionTests;
  1631. exports.IntersectionTests = IntersectionTests$1;
  1632. exports.Ray = Ray;
  1633. }));