IntersectionTests-5deed78b.js 56 KB

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