IntersectionTests-ea138127.js 57 KB

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