IntersectionTests-a25e058d.js 57 KB

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