IntersectionTests-88c49b2e.js 56 KB

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