IntersectionTests-f17c84f0.js 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818
  1. /* This file is automatically rebuilt by the Cesium build process. */
  2. define(['exports', './when-e6985d2a', './Check-24cae389', './Math-392d0035', './Cartesian2-a5d6dde9', './Transforms-81680c41'], function (exports, when, Check, _Math, Cartesian2, Transforms) { 'use strict';
  3. /**
  4. * Defines functions for 2nd order polynomial functions of one variable with only real coefficients.
  5. *
  6. * @namespace QuadraticRealPolynomial
  7. */
  8. var 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 Check.DeveloperError("a is a required number.");
  21. }
  22. if (typeof b !== "number") {
  23. throw new Check.DeveloperError("b is a required number.");
  24. }
  25. if (typeof c !== "number") {
  26. throw new Check.DeveloperError("c is a required number.");
  27. }
  28. //>>includeEnd('debug');
  29. var discriminant = b * b - 4.0 * a * c;
  30. return discriminant;
  31. };
  32. function addWithCancellationCheck(left, right, tolerance) {
  33. var difference = left + right;
  34. if (
  35. _Math.CesiumMath.sign(left) !== _Math.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 Check.DeveloperError("a is a required number.");
  54. }
  55. if (typeof b !== "number") {
  56. throw new Check.DeveloperError("b is a required number.");
  57. }
  58. if (typeof c !== "number") {
  59. throw new Check.DeveloperError("c is a required number.");
  60. }
  61. //>>includeEnd('debug');
  62. var 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. var cMagnitude = Math.abs(c);
  76. var aMagnitude = Math.abs(a);
  77. if (
  78. cMagnitude < aMagnitude &&
  79. cMagnitude / aMagnitude < _Math.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 < _Math.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. var 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. var b2 = b * b;
  111. var four_ac = 4.0 * a * c;
  112. var radicand = addWithCancellationCheck(b2, -four_ac, _Math.CesiumMath.EPSILON14);
  113. if (radicand < 0.0) {
  114. // Both roots are complex.
  115. return [];
  116. }
  117. var q =
  118. -0.5 *
  119. addWithCancellationCheck(
  120. b,
  121. _Math.CesiumMath.sign(b) * Math.sqrt(radicand),
  122. _Math.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. var 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. var a2 = a * a;
  160. var b2 = b * b;
  161. var c2 = c * c;
  162. var d2 = d * d;
  163. var 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. var A = a;
  172. var B = b / 3.0;
  173. var C = c / 3.0;
  174. var D = d;
  175. var AC = A * C;
  176. var BD = B * D;
  177. var B2 = B * B;
  178. var C2 = C * C;
  179. var delta1 = A * C - B2;
  180. var delta2 = A * D - B * C;
  181. var delta3 = B * D - C2;
  182. var discriminant = 4.0 * delta1 * delta3 - delta2 * delta2;
  183. var temp;
  184. var temp1;
  185. if (discriminant < 0.0) {
  186. var ABar;
  187. var CBar;
  188. var 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. var s = DBar < 0.0 ? -1.0 : 1.0; // This is not Math.Sign()!
  199. var temp0 = -s * Math.abs(ABar) * Math.sqrt(-discriminant);
  200. temp1 = -DBar + temp0;
  201. var x = temp1 / 2.0;
  202. var p = x < 0.0 ? -Math.pow(-x, 1.0 / 3.0) : Math.pow(x, 1.0 / 3.0);
  203. var 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. var CBarA = delta1;
  211. var DBarA = -2.0 * B * delta1 + A * delta2;
  212. var CBarD = delta3;
  213. var DBarD = -D * delta2 + 2.0 * C * delta3;
  214. var squareRootOfDiscriminant = Math.sqrt(discriminant);
  215. var halfSquareRootOf3 = Math.sqrt(3.0) / 2.0;
  216. var theta = Math.abs(Math.atan2(A * squareRootOfDiscriminant, -DBarA) / 3.0);
  217. temp = 2.0 * Math.sqrt(-CBarA);
  218. var cosine = Math.cos(theta);
  219. temp1 = temp * cosine;
  220. var temp3 = temp * (-cosine / 2.0 - halfSquareRootOf3 * Math.sin(theta));
  221. var numeratorLarge = temp1 + temp3 > 2.0 * B ? temp1 - B : temp3 - B;
  222. var denominatorLarge = A;
  223. var 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. var numeratorSmall = -D;
  230. var denominatorSmall = temp1 + temp3 < 2.0 * C ? temp1 + C : temp3 + C;
  231. var root3 = numeratorSmall / denominatorSmall;
  232. var E = denominatorLarge * denominatorSmall;
  233. var F =
  234. -numeratorLarge * denominatorSmall - denominatorLarge * numeratorSmall;
  235. var G = numeratorLarge * numeratorSmall;
  236. var 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. var roots;
  279. var 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. var 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. var 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 Check.DeveloperError("a is a required number.");
  351. }
  352. if (typeof b !== "number") {
  353. throw new Check.DeveloperError("b is a required number.");
  354. }
  355. if (typeof c !== "number") {
  356. throw new Check.DeveloperError("c is a required number.");
  357. }
  358. if (typeof d !== "number") {
  359. throw new Check.DeveloperError("d is a required number.");
  360. }
  361. if (typeof e !== "number") {
  362. throw new Check.DeveloperError("e is a required number.");
  363. }
  364. //>>includeEnd('debug');
  365. var a2 = a * a;
  366. var a3 = a2 * a;
  367. var b2 = b * b;
  368. var b3 = b2 * b;
  369. var c2 = c * c;
  370. var c3 = c2 * c;
  371. var d2 = d * d;
  372. var d3 = d2 * d;
  373. var e2 = e * e;
  374. var e3 = e2 * e;
  375. var 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. var a3Squared = a3 * a3;
  398. var p = a2 - (3.0 * a3Squared) / 8.0;
  399. var q = a1 - (a2 * a3) / 2.0 + (a3Squared * a3) / 8.0;
  400. var 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. var 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. var temp = -a3 / 4.0;
  414. // Use the largest positive root.
  415. var hSquared = cubicRoots[cubicRoots.length - 1];
  416. if (Math.abs(hSquared) < _Math.CesiumMath.EPSILON14) {
  417. // y^4 + p y^2 + r = 0.
  418. var roots = QuadraticRealPolynomial.computeRealRoots(1.0, p, r);
  419. if (roots.length === 2) {
  420. var root0 = roots[0];
  421. var root1 = roots[1];
  422. var y;
  423. if (root0 >= 0.0 && root1 >= 0.0) {
  424. var y0 = Math.sqrt(root0);
  425. var 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. var h = Math.sqrt(hSquared);
  438. var m = (p + hSquared - q / h) / 2.0;
  439. var 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. var roots1 = QuadraticRealPolynomial.computeRealRoots(1.0, h, m);
  442. var 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. var a1Squared = a1 * a1;
  476. var a2Squared = a2 * a2;
  477. var a3Squared = a3 * a3;
  478. var p = -2.0 * a2;
  479. var q = a1 * a3 + a2Squared - 4.0 * a0;
  480. var r = a3Squared * a0 - a1 * a2 * a3 + a1Squared;
  481. var cubicRoots = CubicRealPolynomial.computeRealRoots(1.0, p, q, r);
  482. if (cubicRoots.length > 0) {
  483. // Use the most positive root
  484. var y = cubicRoots[0];
  485. var temp = a2 - y;
  486. var tempSquared = temp * temp;
  487. var g1 = a3 / 2.0;
  488. var h1 = temp / 2.0;
  489. var m = tempSquared - 4.0 * a0;
  490. var mError = tempSquared + 4.0 * Math.abs(a0);
  491. var n = a3Squared - 4.0 * y;
  492. var nError = a3Squared + 4.0 * Math.abs(y);
  493. var g2;
  494. var h2;
  495. if (y < 0.0 || m * nError < n * mError) {
  496. var squareRootOfN = Math.sqrt(n);
  497. g2 = squareRootOfN / 2.0;
  498. h2 = squareRootOfN === 0.0 ? 0.0 : (a3 * h1 - a1) / squareRootOfN;
  499. } else {
  500. var squareRootOfM = Math.sqrt(m);
  501. g2 = squareRootOfM === 0.0 ? 0.0 : (a3 * h1 - a1) / squareRootOfM;
  502. h2 = squareRootOfM / 2.0;
  503. }
  504. var G;
  505. var g;
  506. if (g1 === 0.0 && g2 === 0.0) {
  507. G = 0.0;
  508. g = 0.0;
  509. } else if (_Math.CesiumMath.sign(g1) === _Math.CesiumMath.sign(g2)) {
  510. G = g1 + g2;
  511. g = y / G;
  512. } else {
  513. g = g1 - g2;
  514. G = y / g;
  515. }
  516. var H;
  517. var h;
  518. if (h1 === 0.0 && h2 === 0.0) {
  519. H = 0.0;
  520. h = 0.0;
  521. } else if (_Math.CesiumMath.sign(h1) === _Math.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. var roots1 = QuadraticRealPolynomial.computeRealRoots(1.0, G, H);
  530. var 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 Check.DeveloperError("a is a required number.");
  568. }
  569. if (typeof b !== "number") {
  570. throw new Check.DeveloperError("b is a required number.");
  571. }
  572. if (typeof c !== "number") {
  573. throw new Check.DeveloperError("c is a required number.");
  574. }
  575. if (typeof d !== "number") {
  576. throw new Check.DeveloperError("d is a required number.");
  577. }
  578. if (typeof e !== "number") {
  579. throw new Check.DeveloperError("e is a required number.");
  580. }
  581. //>>includeEnd('debug');
  582. if (Math.abs(a) < _Math.CesiumMath.EPSILON15) {
  583. return CubicRealPolynomial.computeRealRoots(b, c, d, e);
  584. }
  585. var a3 = b / a;
  586. var a2 = c / a;
  587. var a1 = d / a;
  588. var a0 = e / a;
  589. var 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 = Cartesian2.Cartesian3.clone(when.defaultValue(direction, Cartesian2.Cartesian3.ZERO));
  640. if (!Cartesian2.Cartesian3.equals(direction, Cartesian2.Cartesian3.ZERO)) {
  641. Cartesian2.Cartesian3.normalize(direction, direction);
  642. }
  643. /**
  644. * The origin of the ray.
  645. * @type {Cartesian3}
  646. * @default {@link Cartesian3.ZERO}
  647. */
  648. this.origin = Cartesian2.Cartesian3.clone(when.defaultValue(origin, Cartesian2.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 (!when.defined(ray)) {
  664. return undefined;
  665. }
  666. if (!when.defined(result)) {
  667. return new Ray(ray.origin, ray.direction);
  668. }
  669. result.origin = Cartesian2.Cartesian3.clone(ray.origin);
  670. result.direction = Cartesian2.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. * var intersection = Cesium.IntersectionTests.rayEllipsoid(ray, ellipsoid);
  685. * var point = Cesium.Ray.getPoint(ray, intersection.start);
  686. */
  687. Ray.getPoint = function (ray, t, result) {
  688. //>>includeStart('debug', pragmas.debug);
  689. Check.Check.typeOf.object("ray", ray);
  690. Check.Check.typeOf.number("t", t);
  691. //>>includeEnd('debug');
  692. if (!when.defined(result)) {
  693. result = new Cartesian2.Cartesian3();
  694. }
  695. result = Cartesian2.Cartesian3.multiplyByScalar(ray.direction, t, result);
  696. return Cartesian2.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. var 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 (!when.defined(ray)) {
  715. throw new Check.DeveloperError("ray is required.");
  716. }
  717. if (!when.defined(plane)) {
  718. throw new Check.DeveloperError("plane is required.");
  719. }
  720. //>>includeEnd('debug');
  721. if (!when.defined(result)) {
  722. result = new Cartesian2.Cartesian3();
  723. }
  724. var origin = ray.origin;
  725. var direction = ray.direction;
  726. var normal = plane.normal;
  727. var denominator = Cartesian2.Cartesian3.dot(normal, direction);
  728. if (Math.abs(denominator) < _Math.CesiumMath.EPSILON15) {
  729. // Ray is parallel to plane. The ray may be in the polygon's plane.
  730. return undefined;
  731. }
  732. var t = (-plane.distance - Cartesian2.Cartesian3.dot(normal, origin)) / denominator;
  733. if (t < 0) {
  734. return undefined;
  735. }
  736. result = Cartesian2.Cartesian3.multiplyByScalar(direction, t, result);
  737. return Cartesian2.Cartesian3.add(origin, result, result);
  738. };
  739. var scratchEdge0 = new Cartesian2.Cartesian3();
  740. var scratchEdge1 = new Cartesian2.Cartesian3();
  741. var scratchPVec = new Cartesian2.Cartesian3();
  742. var scratchTVec = new Cartesian2.Cartesian3();
  743. var scratchQVec = new Cartesian2.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 (!when.defined(ray)) {
  769. throw new Check.DeveloperError("ray is required.");
  770. }
  771. if (!when.defined(p0)) {
  772. throw new Check.DeveloperError("p0 is required.");
  773. }
  774. if (!when.defined(p1)) {
  775. throw new Check.DeveloperError("p1 is required.");
  776. }
  777. if (!when.defined(p2)) {
  778. throw new Check.DeveloperError("p2 is required.");
  779. }
  780. //>>includeEnd('debug');
  781. cullBackFaces = when.defaultValue(cullBackFaces, false);
  782. var origin = ray.origin;
  783. var direction = ray.direction;
  784. var edge0 = Cartesian2.Cartesian3.subtract(p1, p0, scratchEdge0);
  785. var edge1 = Cartesian2.Cartesian3.subtract(p2, p0, scratchEdge1);
  786. var p = Cartesian2.Cartesian3.cross(direction, edge1, scratchPVec);
  787. var det = Cartesian2.Cartesian3.dot(edge0, p);
  788. var tvec;
  789. var q;
  790. var u;
  791. var v;
  792. var t;
  793. if (cullBackFaces) {
  794. if (det < _Math.CesiumMath.EPSILON6) {
  795. return undefined;
  796. }
  797. tvec = Cartesian2.Cartesian3.subtract(origin, p0, scratchTVec);
  798. u = Cartesian2.Cartesian3.dot(tvec, p);
  799. if (u < 0.0 || u > det) {
  800. return undefined;
  801. }
  802. q = Cartesian2.Cartesian3.cross(tvec, edge0, scratchQVec);
  803. v = Cartesian2.Cartesian3.dot(direction, q);
  804. if (v < 0.0 || u + v > det) {
  805. return undefined;
  806. }
  807. t = Cartesian2.Cartesian3.dot(edge1, q) / det;
  808. } else {
  809. if (Math.abs(det) < _Math.CesiumMath.EPSILON6) {
  810. return undefined;
  811. }
  812. var invDet = 1.0 / det;
  813. tvec = Cartesian2.Cartesian3.subtract(origin, p0, scratchTVec);
  814. u = Cartesian2.Cartesian3.dot(tvec, p) * invDet;
  815. if (u < 0.0 || u > 1.0) {
  816. return undefined;
  817. }
  818. q = Cartesian2.Cartesian3.cross(tvec, edge0, scratchQVec);
  819. v = Cartesian2.Cartesian3.dot(direction, q) * invDet;
  820. if (v < 0.0 || u + v > 1.0) {
  821. return undefined;
  822. }
  823. t = Cartesian2.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. var t = IntersectionTests.rayTriangleParametric(
  853. ray,
  854. p0,
  855. p1,
  856. p2,
  857. cullBackFaces
  858. );
  859. if (!when.defined(t) || t < 0.0) {
  860. return undefined;
  861. }
  862. if (!when.defined(result)) {
  863. result = new Cartesian2.Cartesian3();
  864. }
  865. Cartesian2.Cartesian3.multiplyByScalar(ray.direction, t, result);
  866. return Cartesian2.Cartesian3.add(ray.origin, result, result);
  867. };
  868. var 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 (!when.defined(v0)) {
  894. throw new Check.DeveloperError("v0 is required.");
  895. }
  896. if (!when.defined(v1)) {
  897. throw new Check.DeveloperError("v1 is required.");
  898. }
  899. if (!when.defined(p0)) {
  900. throw new Check.DeveloperError("p0 is required.");
  901. }
  902. if (!when.defined(p1)) {
  903. throw new Check.DeveloperError("p1 is required.");
  904. }
  905. if (!when.defined(p2)) {
  906. throw new Check.DeveloperError("p2 is required.");
  907. }
  908. //>>includeEnd('debug');
  909. var ray = scratchLineSegmentTriangleRay;
  910. Cartesian2.Cartesian3.clone(v0, ray.origin);
  911. Cartesian2.Cartesian3.subtract(v1, v0, ray.direction);
  912. Cartesian2.Cartesian3.normalize(ray.direction, ray.direction);
  913. var t = IntersectionTests.rayTriangleParametric(
  914. ray,
  915. p0,
  916. p1,
  917. p2,
  918. cullBackFaces
  919. );
  920. if (!when.defined(t) || t < 0.0 || t > Cartesian2.Cartesian3.distance(v0, v1)) {
  921. return undefined;
  922. }
  923. if (!when.defined(result)) {
  924. result = new Cartesian2.Cartesian3();
  925. }
  926. Cartesian2.Cartesian3.multiplyByScalar(ray.direction, t, result);
  927. return Cartesian2.Cartesian3.add(ray.origin, result, result);
  928. };
  929. function solveQuadratic(a, b, c, result) {
  930. var det = b * b - 4.0 * a * c;
  931. if (det < 0.0) {
  932. return undefined;
  933. } else if (det > 0.0) {
  934. var denom = 1.0 / (2.0 * a);
  935. var disc = Math.sqrt(det);
  936. var root0 = (-b + disc) * denom;
  937. var 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. var 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. var raySphereRoots = {
  955. root0: 0.0,
  956. root1: 0.0,
  957. };
  958. function raySphere(ray, sphere, result) {
  959. if (!when.defined(result)) {
  960. result = new Transforms.Interval();
  961. }
  962. var origin = ray.origin;
  963. var direction = ray.direction;
  964. var center = sphere.center;
  965. var radiusSquared = sphere.radius * sphere.radius;
  966. var diff = Cartesian2.Cartesian3.subtract(origin, center, scratchPVec);
  967. var a = Cartesian2.Cartesian3.dot(direction, direction);
  968. var b = 2.0 * Cartesian2.Cartesian3.dot(direction, diff);
  969. var c = Cartesian2.Cartesian3.magnitudeSquared(diff) - radiusSquared;
  970. var roots = solveQuadratic(a, b, c, raySphereRoots);
  971. if (!when.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 (!when.defined(ray)) {
  990. throw new Check.DeveloperError("ray is required.");
  991. }
  992. if (!when.defined(sphere)) {
  993. throw new Check.DeveloperError("sphere is required.");
  994. }
  995. //>>includeEnd('debug');
  996. result = raySphere(ray, sphere, result);
  997. if (!when.defined(result) || result.stop < 0.0) {
  998. return undefined;
  999. }
  1000. result.start = Math.max(result.start, 0.0);
  1001. return result;
  1002. };
  1003. var 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 (!when.defined(p0)) {
  1017. throw new Check.DeveloperError("p0 is required.");
  1018. }
  1019. if (!when.defined(p1)) {
  1020. throw new Check.DeveloperError("p1 is required.");
  1021. }
  1022. if (!when.defined(sphere)) {
  1023. throw new Check.DeveloperError("sphere is required.");
  1024. }
  1025. //>>includeEnd('debug');
  1026. var ray = scratchLineSegmentRay;
  1027. Cartesian2.Cartesian3.clone(p0, ray.origin);
  1028. var direction = Cartesian2.Cartesian3.subtract(p1, p0, ray.direction);
  1029. var maxT = Cartesian2.Cartesian3.magnitude(direction);
  1030. Cartesian2.Cartesian3.normalize(direction, direction);
  1031. result = raySphere(ray, sphere, result);
  1032. if (!when.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. var scratchQ = new Cartesian2.Cartesian3();
  1040. var scratchW = new Cartesian2.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 (!when.defined(ray)) {
  1051. throw new Check.DeveloperError("ray is required.");
  1052. }
  1053. if (!when.defined(ellipsoid)) {
  1054. throw new Check.DeveloperError("ellipsoid is required.");
  1055. }
  1056. //>>includeEnd('debug');
  1057. var inverseRadii = ellipsoid.oneOverRadii;
  1058. var q = Cartesian2.Cartesian3.multiplyComponents(inverseRadii, ray.origin, scratchQ);
  1059. var w = Cartesian2.Cartesian3.multiplyComponents(inverseRadii, ray.direction, scratchW);
  1060. var q2 = Cartesian2.Cartesian3.magnitudeSquared(q);
  1061. var qw = Cartesian2.Cartesian3.dot(q, w);
  1062. var difference, w2, product, discriminant, temp;
  1063. if (q2 > 1.0) {
  1064. // Outside ellipsoid.
  1065. if (qw >= 0.0) {
  1066. // Looking outward or tangent (0 intersections).
  1067. return undefined;
  1068. }
  1069. // qw < 0.0.
  1070. var qw2 = qw * qw;
  1071. difference = q2 - 1.0; // Positively valued.
  1072. w2 = Cartesian2.Cartesian3.magnitudeSquared(w);
  1073. product = w2 * difference;
  1074. if (qw2 < product) {
  1075. // Imaginary roots (0 intersections).
  1076. return undefined;
  1077. } else if (qw2 > product) {
  1078. // Distinct roots (2 intersections).
  1079. discriminant = qw * qw - product;
  1080. temp = -qw + Math.sqrt(discriminant); // Avoid cancellation.
  1081. var root0 = temp / w2;
  1082. var root1 = difference / temp;
  1083. if (root0 < root1) {
  1084. return new Transforms.Interval(root0, root1);
  1085. }
  1086. return {
  1087. start: root1,
  1088. stop: root0,
  1089. };
  1090. }
  1091. // qw2 == product. Repeated roots (2 intersections).
  1092. var root = Math.sqrt(difference / w2);
  1093. return new Transforms.Interval(root, root);
  1094. } else if (q2 < 1.0) {
  1095. // Inside ellipsoid (2 intersections).
  1096. difference = q2 - 1.0; // Negatively valued.
  1097. w2 = Cartesian2.Cartesian3.magnitudeSquared(w);
  1098. product = w2 * difference; // Negatively valued.
  1099. discriminant = qw * qw - product;
  1100. temp = -qw + Math.sqrt(discriminant); // Positively valued.
  1101. return new Transforms.Interval(0.0, temp / w2);
  1102. }
  1103. // q2 == 1.0. On ellipsoid.
  1104. if (qw < 0.0) {
  1105. // Looking inward.
  1106. w2 = Cartesian2.Cartesian3.magnitudeSquared(w);
  1107. return new Transforms.Interval(0.0, -qw / w2);
  1108. }
  1109. // qw >= 0.0. Looking outward or tangent.
  1110. return undefined;
  1111. };
  1112. function addWithCancellationCheck$1(left, right, tolerance) {
  1113. var difference = left + right;
  1114. if (
  1115. _Math.CesiumMath.sign(left) !== _Math.CesiumMath.sign(right) &&
  1116. Math.abs(difference / Math.max(Math.abs(left), Math.abs(right))) < tolerance
  1117. ) {
  1118. return 0.0;
  1119. }
  1120. return difference;
  1121. }
  1122. function quadraticVectorExpression(A, b, c, x, w) {
  1123. var xSquared = x * x;
  1124. var wSquared = w * w;
  1125. var l2 = (A[Transforms.Matrix3.COLUMN1ROW1] - A[Transforms.Matrix3.COLUMN2ROW2]) * wSquared;
  1126. var l1 =
  1127. w *
  1128. (x *
  1129. addWithCancellationCheck$1(
  1130. A[Transforms.Matrix3.COLUMN1ROW0],
  1131. A[Transforms.Matrix3.COLUMN0ROW1],
  1132. _Math.CesiumMath.EPSILON15
  1133. ) +
  1134. b.y);
  1135. var l0 =
  1136. A[Transforms.Matrix3.COLUMN0ROW0] * xSquared +
  1137. A[Transforms.Matrix3.COLUMN2ROW2] * wSquared +
  1138. x * b.x +
  1139. c;
  1140. var r1 =
  1141. wSquared *
  1142. addWithCancellationCheck$1(
  1143. A[Transforms.Matrix3.COLUMN2ROW1],
  1144. A[Transforms.Matrix3.COLUMN1ROW2],
  1145. _Math.CesiumMath.EPSILON15
  1146. );
  1147. var r0 =
  1148. w *
  1149. (x *
  1150. addWithCancellationCheck$1(A[Transforms.Matrix3.COLUMN2ROW0], A[Transforms.Matrix3.COLUMN0ROW2]) +
  1151. b.z);
  1152. var cosines;
  1153. var solutions = [];
  1154. if (r0 === 0.0 && r1 === 0.0) {
  1155. cosines = QuadraticRealPolynomial.computeRealRoots(l2, l1, l0);
  1156. if (cosines.length === 0) {
  1157. return solutions;
  1158. }
  1159. var cosine0 = cosines[0];
  1160. var sine0 = Math.sqrt(Math.max(1.0 - cosine0 * cosine0, 0.0));
  1161. solutions.push(new Cartesian2.Cartesian3(x, w * cosine0, w * -sine0));
  1162. solutions.push(new Cartesian2.Cartesian3(x, w * cosine0, w * sine0));
  1163. if (cosines.length === 2) {
  1164. var cosine1 = cosines[1];
  1165. var sine1 = Math.sqrt(Math.max(1.0 - cosine1 * cosine1, 0.0));
  1166. solutions.push(new Cartesian2.Cartesian3(x, w * cosine1, w * -sine1));
  1167. solutions.push(new Cartesian2.Cartesian3(x, w * cosine1, w * sine1));
  1168. }
  1169. return solutions;
  1170. }
  1171. var r0Squared = r0 * r0;
  1172. var r1Squared = r1 * r1;
  1173. var l2Squared = l2 * l2;
  1174. var r0r1 = r0 * r1;
  1175. var c4 = l2Squared + r1Squared;
  1176. var c3 = 2.0 * (l1 * l2 + r0r1);
  1177. var c2 = 2.0 * l0 * l2 + l1 * l1 - r1Squared + r0Squared;
  1178. var c1 = 2.0 * (l0 * l1 - r0r1);
  1179. var c0 = l0 * l0 - r0Squared;
  1180. if (c4 === 0.0 && c3 === 0.0 && c2 === 0.0 && c1 === 0.0) {
  1181. return solutions;
  1182. }
  1183. cosines = QuarticRealPolynomial.computeRealRoots(c4, c3, c2, c1, c0);
  1184. var length = cosines.length;
  1185. if (length === 0) {
  1186. return solutions;
  1187. }
  1188. for (var i = 0; i < length; ++i) {
  1189. var cosine = cosines[i];
  1190. var cosineSquared = cosine * cosine;
  1191. var sineSquared = Math.max(1.0 - cosineSquared, 0.0);
  1192. var sine = Math.sqrt(sineSquared);
  1193. //var left = l2 * cosineSquared + l1 * cosine + l0;
  1194. var left;
  1195. if (_Math.CesiumMath.sign(l2) === _Math.CesiumMath.sign(l0)) {
  1196. left = addWithCancellationCheck$1(
  1197. l2 * cosineSquared + l0,
  1198. l1 * cosine,
  1199. _Math.CesiumMath.EPSILON12
  1200. );
  1201. } else if (_Math.CesiumMath.sign(l0) === _Math.CesiumMath.sign(l1 * cosine)) {
  1202. left = addWithCancellationCheck$1(
  1203. l2 * cosineSquared,
  1204. l1 * cosine + l0,
  1205. _Math.CesiumMath.EPSILON12
  1206. );
  1207. } else {
  1208. left = addWithCancellationCheck$1(
  1209. l2 * cosineSquared + l1 * cosine,
  1210. l0,
  1211. _Math.CesiumMath.EPSILON12
  1212. );
  1213. }
  1214. var right = addWithCancellationCheck$1(r1 * cosine, r0, _Math.CesiumMath.EPSILON15);
  1215. var product = left * right;
  1216. if (product < 0.0) {
  1217. solutions.push(new Cartesian2.Cartesian3(x, w * cosine, w * sine));
  1218. } else if (product > 0.0) {
  1219. solutions.push(new Cartesian2.Cartesian3(x, w * cosine, w * -sine));
  1220. } else if (sine !== 0.0) {
  1221. solutions.push(new Cartesian2.Cartesian3(x, w * cosine, w * -sine));
  1222. solutions.push(new Cartesian2.Cartesian3(x, w * cosine, w * sine));
  1223. ++i;
  1224. } else {
  1225. solutions.push(new Cartesian2.Cartesian3(x, w * cosine, w * sine));
  1226. }
  1227. }
  1228. return solutions;
  1229. }
  1230. var firstAxisScratch = new Cartesian2.Cartesian3();
  1231. var secondAxisScratch = new Cartesian2.Cartesian3();
  1232. var thirdAxisScratch = new Cartesian2.Cartesian3();
  1233. var referenceScratch = new Cartesian2.Cartesian3();
  1234. var bCart = new Cartesian2.Cartesian3();
  1235. var bScratch = new Transforms.Matrix3();
  1236. var btScratch = new Transforms.Matrix3();
  1237. var diScratch = new Transforms.Matrix3();
  1238. var dScratch = new Transforms.Matrix3();
  1239. var cScratch = new Transforms.Matrix3();
  1240. var tempMatrix = new Transforms.Matrix3();
  1241. var aScratch = new Transforms.Matrix3();
  1242. var sScratch = new Cartesian2.Cartesian3();
  1243. var closestScratch = new Cartesian2.Cartesian3();
  1244. var surfPointScratch = new Cartesian2.Cartographic();
  1245. /**
  1246. * Provides the point along the ray which is nearest to the ellipsoid.
  1247. *
  1248. * @param {Ray} ray The ray.
  1249. * @param {Ellipsoid} ellipsoid The ellipsoid.
  1250. * @returns {Cartesian3} The nearest planetodetic point on the ray.
  1251. */
  1252. IntersectionTests.grazingAltitudeLocation = function (ray, ellipsoid) {
  1253. //>>includeStart('debug', pragmas.debug);
  1254. if (!when.defined(ray)) {
  1255. throw new Check.DeveloperError("ray is required.");
  1256. }
  1257. if (!when.defined(ellipsoid)) {
  1258. throw new Check.DeveloperError("ellipsoid is required.");
  1259. }
  1260. //>>includeEnd('debug');
  1261. var position = ray.origin;
  1262. var direction = ray.direction;
  1263. if (!Cartesian2.Cartesian3.equals(position, Cartesian2.Cartesian3.ZERO)) {
  1264. var normal = ellipsoid.geodeticSurfaceNormal(position, firstAxisScratch);
  1265. if (Cartesian2.Cartesian3.dot(direction, normal) >= 0.0) {
  1266. // The location provided is the closest point in altitude
  1267. return position;
  1268. }
  1269. }
  1270. var intersects = when.defined(this.rayEllipsoid(ray, ellipsoid));
  1271. // Compute the scaled direction vector.
  1272. var f = ellipsoid.transformPositionToScaledSpace(direction, firstAxisScratch);
  1273. // Constructs a basis from the unit scaled direction vector. Construct its rotation and transpose.
  1274. var firstAxis = Cartesian2.Cartesian3.normalize(f, f);
  1275. var reference = Cartesian2.Cartesian3.mostOrthogonalAxis(f, referenceScratch);
  1276. var secondAxis = Cartesian2.Cartesian3.normalize(
  1277. Cartesian2.Cartesian3.cross(reference, firstAxis, secondAxisScratch),
  1278. secondAxisScratch
  1279. );
  1280. var thirdAxis = Cartesian2.Cartesian3.normalize(
  1281. Cartesian2.Cartesian3.cross(firstAxis, secondAxis, thirdAxisScratch),
  1282. thirdAxisScratch
  1283. );
  1284. var B = bScratch;
  1285. B[0] = firstAxis.x;
  1286. B[1] = firstAxis.y;
  1287. B[2] = firstAxis.z;
  1288. B[3] = secondAxis.x;
  1289. B[4] = secondAxis.y;
  1290. B[5] = secondAxis.z;
  1291. B[6] = thirdAxis.x;
  1292. B[7] = thirdAxis.y;
  1293. B[8] = thirdAxis.z;
  1294. var B_T = Transforms.Matrix3.transpose(B, btScratch);
  1295. // Get the scaling matrix and its inverse.
  1296. var D_I = Transforms.Matrix3.fromScale(ellipsoid.radii, diScratch);
  1297. var D = Transforms.Matrix3.fromScale(ellipsoid.oneOverRadii, dScratch);
  1298. var C = cScratch;
  1299. C[0] = 0.0;
  1300. C[1] = -direction.z;
  1301. C[2] = direction.y;
  1302. C[3] = direction.z;
  1303. C[4] = 0.0;
  1304. C[5] = -direction.x;
  1305. C[6] = -direction.y;
  1306. C[7] = direction.x;
  1307. C[8] = 0.0;
  1308. var temp = Transforms.Matrix3.multiply(
  1309. Transforms.Matrix3.multiply(B_T, D, tempMatrix),
  1310. C,
  1311. tempMatrix
  1312. );
  1313. var A = Transforms.Matrix3.multiply(Transforms.Matrix3.multiply(temp, D_I, aScratch), B, aScratch);
  1314. var b = Transforms.Matrix3.multiplyByVector(temp, position, bCart);
  1315. // Solve for the solutions to the expression in standard form:
  1316. var solutions = quadraticVectorExpression(
  1317. A,
  1318. Cartesian2.Cartesian3.negate(b, firstAxisScratch),
  1319. 0.0,
  1320. 0.0,
  1321. 1.0
  1322. );
  1323. var s;
  1324. var altitude;
  1325. var length = solutions.length;
  1326. if (length > 0) {
  1327. var closest = Cartesian2.Cartesian3.clone(Cartesian2.Cartesian3.ZERO, closestScratch);
  1328. var maximumValue = Number.NEGATIVE_INFINITY;
  1329. for (var i = 0; i < length; ++i) {
  1330. s = Transforms.Matrix3.multiplyByVector(
  1331. D_I,
  1332. Transforms.Matrix3.multiplyByVector(B, solutions[i], sScratch),
  1333. sScratch
  1334. );
  1335. var v = Cartesian2.Cartesian3.normalize(
  1336. Cartesian2.Cartesian3.subtract(s, position, referenceScratch),
  1337. referenceScratch
  1338. );
  1339. var dotProduct = Cartesian2.Cartesian3.dot(v, direction);
  1340. if (dotProduct > maximumValue) {
  1341. maximumValue = dotProduct;
  1342. closest = Cartesian2.Cartesian3.clone(s, closest);
  1343. }
  1344. }
  1345. var surfacePoint = ellipsoid.cartesianToCartographic(
  1346. closest,
  1347. surfPointScratch
  1348. );
  1349. maximumValue = _Math.CesiumMath.clamp(maximumValue, 0.0, 1.0);
  1350. altitude =
  1351. Cartesian2.Cartesian3.magnitude(
  1352. Cartesian2.Cartesian3.subtract(closest, position, referenceScratch)
  1353. ) * Math.sqrt(1.0 - maximumValue * maximumValue);
  1354. altitude = intersects ? -altitude : altitude;
  1355. surfacePoint.height = altitude;
  1356. return ellipsoid.cartographicToCartesian(surfacePoint, new Cartesian2.Cartesian3());
  1357. }
  1358. return undefined;
  1359. };
  1360. var lineSegmentPlaneDifference = new Cartesian2.Cartesian3();
  1361. /**
  1362. * Computes the intersection of a line segment and a plane.
  1363. *
  1364. * @param {Cartesian3} endPoint0 An end point of the line segment.
  1365. * @param {Cartesian3} endPoint1 The other end point of the line segment.
  1366. * @param {Plane} plane The plane.
  1367. * @param {Cartesian3} [result] The object onto which to store the result.
  1368. * @returns {Cartesian3} The intersection point or undefined if there is no intersection.
  1369. *
  1370. * @example
  1371. * var origin = Cesium.Cartesian3.fromDegrees(-75.59777, 40.03883);
  1372. * var normal = ellipsoid.geodeticSurfaceNormal(origin);
  1373. * var plane = Cesium.Plane.fromPointNormal(origin, normal);
  1374. *
  1375. * var p0 = new Cesium.Cartesian3(...);
  1376. * var p1 = new Cesium.Cartesian3(...);
  1377. *
  1378. * // find the intersection of the line segment from p0 to p1 and the tangent plane at origin.
  1379. * var intersection = Cesium.IntersectionTests.lineSegmentPlane(p0, p1, plane);
  1380. */
  1381. IntersectionTests.lineSegmentPlane = function (
  1382. endPoint0,
  1383. endPoint1,
  1384. plane,
  1385. result
  1386. ) {
  1387. //>>includeStart('debug', pragmas.debug);
  1388. if (!when.defined(endPoint0)) {
  1389. throw new Check.DeveloperError("endPoint0 is required.");
  1390. }
  1391. if (!when.defined(endPoint1)) {
  1392. throw new Check.DeveloperError("endPoint1 is required.");
  1393. }
  1394. if (!when.defined(plane)) {
  1395. throw new Check.DeveloperError("plane is required.");
  1396. }
  1397. //>>includeEnd('debug');
  1398. if (!when.defined(result)) {
  1399. result = new Cartesian2.Cartesian3();
  1400. }
  1401. var difference = Cartesian2.Cartesian3.subtract(
  1402. endPoint1,
  1403. endPoint0,
  1404. lineSegmentPlaneDifference
  1405. );
  1406. var normal = plane.normal;
  1407. var nDotDiff = Cartesian2.Cartesian3.dot(normal, difference);
  1408. // check if the segment and plane are parallel
  1409. if (Math.abs(nDotDiff) < _Math.CesiumMath.EPSILON6) {
  1410. return undefined;
  1411. }
  1412. var nDotP0 = Cartesian2.Cartesian3.dot(normal, endPoint0);
  1413. var t = -(plane.distance + nDotP0) / nDotDiff;
  1414. // intersection only if t is in [0, 1]
  1415. if (t < 0.0 || t > 1.0) {
  1416. return undefined;
  1417. }
  1418. // intersection is endPoint0 + t * (endPoint1 - endPoint0)
  1419. Cartesian2.Cartesian3.multiplyByScalar(difference, t, result);
  1420. Cartesian2.Cartesian3.add(endPoint0, result, result);
  1421. return result;
  1422. };
  1423. /**
  1424. * Computes the intersection of a triangle and a plane
  1425. *
  1426. * @param {Cartesian3} p0 First point of the triangle
  1427. * @param {Cartesian3} p1 Second point of the triangle
  1428. * @param {Cartesian3} p2 Third point of the triangle
  1429. * @param {Plane} plane Intersection plane
  1430. * @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)
  1431. *
  1432. * @example
  1433. * var origin = Cesium.Cartesian3.fromDegrees(-75.59777, 40.03883);
  1434. * var normal = ellipsoid.geodeticSurfaceNormal(origin);
  1435. * var plane = Cesium.Plane.fromPointNormal(origin, normal);
  1436. *
  1437. * var p0 = new Cesium.Cartesian3(...);
  1438. * var p1 = new Cesium.Cartesian3(...);
  1439. * var p2 = new Cesium.Cartesian3(...);
  1440. *
  1441. * // convert the triangle composed of points (p0, p1, p2) to three triangles that don't cross the plane
  1442. * var triangles = Cesium.IntersectionTests.trianglePlaneIntersection(p0, p1, p2, plane);
  1443. */
  1444. IntersectionTests.trianglePlaneIntersection = function (p0, p1, p2, plane) {
  1445. //>>includeStart('debug', pragmas.debug);
  1446. if (!when.defined(p0) || !when.defined(p1) || !when.defined(p2) || !when.defined(plane)) {
  1447. throw new Check.DeveloperError("p0, p1, p2, and plane are required.");
  1448. }
  1449. //>>includeEnd('debug');
  1450. var planeNormal = plane.normal;
  1451. var planeD = plane.distance;
  1452. var p0Behind = Cartesian2.Cartesian3.dot(planeNormal, p0) + planeD < 0.0;
  1453. var p1Behind = Cartesian2.Cartesian3.dot(planeNormal, p1) + planeD < 0.0;
  1454. var p2Behind = Cartesian2.Cartesian3.dot(planeNormal, p2) + planeD < 0.0;
  1455. // Given these dots products, the calls to lineSegmentPlaneIntersection
  1456. // always have defined results.
  1457. var numBehind = 0;
  1458. numBehind += p0Behind ? 1 : 0;
  1459. numBehind += p1Behind ? 1 : 0;
  1460. numBehind += p2Behind ? 1 : 0;
  1461. var u1, u2;
  1462. if (numBehind === 1 || numBehind === 2) {
  1463. u1 = new Cartesian2.Cartesian3();
  1464. u2 = new Cartesian2.Cartesian3();
  1465. }
  1466. if (numBehind === 1) {
  1467. if (p0Behind) {
  1468. IntersectionTests.lineSegmentPlane(p0, p1, plane, u1);
  1469. IntersectionTests.lineSegmentPlane(p0, p2, plane, u2);
  1470. return {
  1471. positions: [p0, p1, p2, u1, u2],
  1472. indices: [
  1473. // Behind
  1474. 0,
  1475. 3,
  1476. 4,
  1477. // In front
  1478. 1,
  1479. 2,
  1480. 4,
  1481. 1,
  1482. 4,
  1483. 3,
  1484. ],
  1485. };
  1486. } else if (p1Behind) {
  1487. IntersectionTests.lineSegmentPlane(p1, p2, plane, u1);
  1488. IntersectionTests.lineSegmentPlane(p1, p0, plane, u2);
  1489. return {
  1490. positions: [p0, p1, p2, u1, u2],
  1491. indices: [
  1492. // Behind
  1493. 1,
  1494. 3,
  1495. 4,
  1496. // In front
  1497. 2,
  1498. 0,
  1499. 4,
  1500. 2,
  1501. 4,
  1502. 3,
  1503. ],
  1504. };
  1505. } else if (p2Behind) {
  1506. IntersectionTests.lineSegmentPlane(p2, p0, plane, u1);
  1507. IntersectionTests.lineSegmentPlane(p2, p1, plane, u2);
  1508. return {
  1509. positions: [p0, p1, p2, u1, u2],
  1510. indices: [
  1511. // Behind
  1512. 2,
  1513. 3,
  1514. 4,
  1515. // In front
  1516. 0,
  1517. 1,
  1518. 4,
  1519. 0,
  1520. 4,
  1521. 3,
  1522. ],
  1523. };
  1524. }
  1525. } else if (numBehind === 2) {
  1526. if (!p0Behind) {
  1527. IntersectionTests.lineSegmentPlane(p1, p0, plane, u1);
  1528. IntersectionTests.lineSegmentPlane(p2, p0, plane, u2);
  1529. return {
  1530. positions: [p0, p1, p2, u1, u2],
  1531. indices: [
  1532. // Behind
  1533. 1,
  1534. 2,
  1535. 4,
  1536. 1,
  1537. 4,
  1538. 3,
  1539. // In front
  1540. 0,
  1541. 3,
  1542. 4,
  1543. ],
  1544. };
  1545. } else if (!p1Behind) {
  1546. IntersectionTests.lineSegmentPlane(p2, p1, plane, u1);
  1547. IntersectionTests.lineSegmentPlane(p0, p1, plane, u2);
  1548. return {
  1549. positions: [p0, p1, p2, u1, u2],
  1550. indices: [
  1551. // Behind
  1552. 2,
  1553. 0,
  1554. 4,
  1555. 2,
  1556. 4,
  1557. 3,
  1558. // In front
  1559. 1,
  1560. 3,
  1561. 4,
  1562. ],
  1563. };
  1564. } else if (!p2Behind) {
  1565. IntersectionTests.lineSegmentPlane(p0, p2, plane, u1);
  1566. IntersectionTests.lineSegmentPlane(p1, p2, plane, u2);
  1567. return {
  1568. positions: [p0, p1, p2, u1, u2],
  1569. indices: [
  1570. // Behind
  1571. 0,
  1572. 1,
  1573. 4,
  1574. 0,
  1575. 4,
  1576. 3,
  1577. // In front
  1578. 2,
  1579. 3,
  1580. 4,
  1581. ],
  1582. };
  1583. }
  1584. }
  1585. // if numBehind is 3, the triangle is completely behind the plane;
  1586. // otherwise, it is completely in front (numBehind is 0).
  1587. return undefined;
  1588. };
  1589. exports.IntersectionTests = IntersectionTests;
  1590. exports.Ray = Ray;
  1591. });