IntersectionTests-afc38163.js 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841
  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/master/LICENSE.md for full licensing details.
  22. */
  23. define(['exports', './when-54c2dc71', './Check-6c0211bc', './Math-1124a290', './Cartesian2-33d2657c', './Transforms-8be64844'], function (exports, when, Check, _Math, Cartesian2, Transforms) { 'use strict';
  24. /**
  25. * Defines functions for 2nd order polynomial functions of one variable with only real coefficients.
  26. *
  27. * @namespace QuadraticRealPolynomial
  28. */
  29. var 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 Check.DeveloperError("a is a required number.");
  42. }
  43. if (typeof b !== "number") {
  44. throw new Check.DeveloperError("b is a required number.");
  45. }
  46. if (typeof c !== "number") {
  47. throw new Check.DeveloperError("c is a required number.");
  48. }
  49. //>>includeEnd('debug');
  50. var discriminant = b * b - 4.0 * a * c;
  51. return discriminant;
  52. };
  53. function addWithCancellationCheck(left, right, tolerance) {
  54. var difference = left + right;
  55. if (
  56. _Math.CesiumMath.sign(left) !== _Math.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 Check.DeveloperError("a is a required number.");
  75. }
  76. if (typeof b !== "number") {
  77. throw new Check.DeveloperError("b is a required number.");
  78. }
  79. if (typeof c !== "number") {
  80. throw new Check.DeveloperError("c is a required number.");
  81. }
  82. //>>includeEnd('debug');
  83. var 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. var cMagnitude = Math.abs(c);
  97. var aMagnitude = Math.abs(a);
  98. if (
  99. cMagnitude < aMagnitude &&
  100. cMagnitude / aMagnitude < _Math.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 < _Math.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. var 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. var b2 = b * b;
  132. var four_ac = 4.0 * a * c;
  133. var radicand = addWithCancellationCheck(b2, -four_ac, _Math.CesiumMath.EPSILON14);
  134. if (radicand < 0.0) {
  135. // Both roots are complex.
  136. return [];
  137. }
  138. var q =
  139. -0.5 *
  140. addWithCancellationCheck(
  141. b,
  142. _Math.CesiumMath.sign(b) * Math.sqrt(radicand),
  143. _Math.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. var 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 Check.DeveloperError("a is a required number.");
  169. }
  170. if (typeof b !== "number") {
  171. throw new Check.DeveloperError("b is a required number.");
  172. }
  173. if (typeof c !== "number") {
  174. throw new Check.DeveloperError("c is a required number.");
  175. }
  176. if (typeof d !== "number") {
  177. throw new Check.DeveloperError("d is a required number.");
  178. }
  179. //>>includeEnd('debug');
  180. var a2 = a * a;
  181. var b2 = b * b;
  182. var c2 = c * c;
  183. var d2 = d * d;
  184. var 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. var A = a;
  193. var B = b / 3.0;
  194. var C = c / 3.0;
  195. var D = d;
  196. var AC = A * C;
  197. var BD = B * D;
  198. var B2 = B * B;
  199. var C2 = C * C;
  200. var delta1 = A * C - B2;
  201. var delta2 = A * D - B * C;
  202. var delta3 = B * D - C2;
  203. var discriminant = 4.0 * delta1 * delta3 - delta2 * delta2;
  204. var temp;
  205. var temp1;
  206. if (discriminant < 0.0) {
  207. var ABar;
  208. var CBar;
  209. var 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. var s = DBar < 0.0 ? -1.0 : 1.0; // This is not Math.Sign()!
  220. var temp0 = -s * Math.abs(ABar) * Math.sqrt(-discriminant);
  221. temp1 = -DBar + temp0;
  222. var x = temp1 / 2.0;
  223. var p = x < 0.0 ? -Math.pow(-x, 1.0 / 3.0) : Math.pow(x, 1.0 / 3.0);
  224. var 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. var CBarA = delta1;
  232. var DBarA = -2.0 * B * delta1 + A * delta2;
  233. var CBarD = delta3;
  234. var DBarD = -D * delta2 + 2.0 * C * delta3;
  235. var squareRootOfDiscriminant = Math.sqrt(discriminant);
  236. var halfSquareRootOf3 = Math.sqrt(3.0) / 2.0;
  237. var theta = Math.abs(Math.atan2(A * squareRootOfDiscriminant, -DBarA) / 3.0);
  238. temp = 2.0 * Math.sqrt(-CBarA);
  239. var cosine = Math.cos(theta);
  240. temp1 = temp * cosine;
  241. var temp3 = temp * (-cosine / 2.0 - halfSquareRootOf3 * Math.sin(theta));
  242. var numeratorLarge = temp1 + temp3 > 2.0 * B ? temp1 - B : temp3 - B;
  243. var denominatorLarge = A;
  244. var 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. var numeratorSmall = -D;
  251. var denominatorSmall = temp1 + temp3 < 2.0 * C ? temp1 + C : temp3 + C;
  252. var root3 = numeratorSmall / denominatorSmall;
  253. var E = denominatorLarge * denominatorSmall;
  254. var F =
  255. -numeratorLarge * denominatorSmall - denominatorLarge * numeratorSmall;
  256. var G = numeratorLarge * numeratorSmall;
  257. var 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 Check.DeveloperError("a is a required number.");
  288. }
  289. if (typeof b !== "number") {
  290. throw new Check.DeveloperError("b is a required number.");
  291. }
  292. if (typeof c !== "number") {
  293. throw new Check.DeveloperError("c is a required number.");
  294. }
  295. if (typeof d !== "number") {
  296. throw new Check.DeveloperError("d is a required number.");
  297. }
  298. //>>includeEnd('debug');
  299. var roots;
  300. var 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. var 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. var 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 Check.DeveloperError("a is a required number.");
  372. }
  373. if (typeof b !== "number") {
  374. throw new Check.DeveloperError("b is a required number.");
  375. }
  376. if (typeof c !== "number") {
  377. throw new Check.DeveloperError("c is a required number.");
  378. }
  379. if (typeof d !== "number") {
  380. throw new Check.DeveloperError("d is a required number.");
  381. }
  382. if (typeof e !== "number") {
  383. throw new Check.DeveloperError("e is a required number.");
  384. }
  385. //>>includeEnd('debug');
  386. var a2 = a * a;
  387. var a3 = a2 * a;
  388. var b2 = b * b;
  389. var b3 = b2 * b;
  390. var c2 = c * c;
  391. var c3 = c2 * c;
  392. var d2 = d * d;
  393. var d3 = d2 * d;
  394. var e2 = e * e;
  395. var e3 = e2 * e;
  396. var 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. var a3Squared = a3 * a3;
  419. var p = a2 - (3.0 * a3Squared) / 8.0;
  420. var q = a1 - (a2 * a3) / 2.0 + (a3Squared * a3) / 8.0;
  421. var 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. var 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. var temp = -a3 / 4.0;
  435. // Use the largest positive root.
  436. var hSquared = cubicRoots[cubicRoots.length - 1];
  437. if (Math.abs(hSquared) < _Math.CesiumMath.EPSILON14) {
  438. // y^4 + p y^2 + r = 0.
  439. var roots = QuadraticRealPolynomial.computeRealRoots(1.0, p, r);
  440. if (roots.length === 2) {
  441. var root0 = roots[0];
  442. var root1 = roots[1];
  443. var y;
  444. if (root0 >= 0.0 && root1 >= 0.0) {
  445. var y0 = Math.sqrt(root0);
  446. var 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. var h = Math.sqrt(hSquared);
  459. var m = (p + hSquared - q / h) / 2.0;
  460. var 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. var roots1 = QuadraticRealPolynomial.computeRealRoots(1.0, h, m);
  463. var 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. var a1Squared = a1 * a1;
  497. var a2Squared = a2 * a2;
  498. var a3Squared = a3 * a3;
  499. var p = -2.0 * a2;
  500. var q = a1 * a3 + a2Squared - 4.0 * a0;
  501. var r = a3Squared * a0 - a1 * a2 * a3 + a1Squared;
  502. var cubicRoots = CubicRealPolynomial.computeRealRoots(1.0, p, q, r);
  503. if (cubicRoots.length > 0) {
  504. // Use the most positive root
  505. var y = cubicRoots[0];
  506. var temp = a2 - y;
  507. var tempSquared = temp * temp;
  508. var g1 = a3 / 2.0;
  509. var h1 = temp / 2.0;
  510. var m = tempSquared - 4.0 * a0;
  511. var mError = tempSquared + 4.0 * Math.abs(a0);
  512. var n = a3Squared - 4.0 * y;
  513. var nError = a3Squared + 4.0 * Math.abs(y);
  514. var g2;
  515. var h2;
  516. if (y < 0.0 || m * nError < n * mError) {
  517. var squareRootOfN = Math.sqrt(n);
  518. g2 = squareRootOfN / 2.0;
  519. h2 = squareRootOfN === 0.0 ? 0.0 : (a3 * h1 - a1) / squareRootOfN;
  520. } else {
  521. var squareRootOfM = Math.sqrt(m);
  522. g2 = squareRootOfM === 0.0 ? 0.0 : (a3 * h1 - a1) / squareRootOfM;
  523. h2 = squareRootOfM / 2.0;
  524. }
  525. var G;
  526. var g;
  527. if (g1 === 0.0 && g2 === 0.0) {
  528. G = 0.0;
  529. g = 0.0;
  530. } else if (_Math.CesiumMath.sign(g1) === _Math.CesiumMath.sign(g2)) {
  531. G = g1 + g2;
  532. g = y / G;
  533. } else {
  534. g = g1 - g2;
  535. G = y / g;
  536. }
  537. var H;
  538. var h;
  539. if (h1 === 0.0 && h2 === 0.0) {
  540. H = 0.0;
  541. h = 0.0;
  542. } else if (_Math.CesiumMath.sign(h1) === _Math.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. var roots1 = QuadraticRealPolynomial.computeRealRoots(1.0, G, H);
  551. var 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 Check.DeveloperError("a is a required number.");
  589. }
  590. if (typeof b !== "number") {
  591. throw new Check.DeveloperError("b is a required number.");
  592. }
  593. if (typeof c !== "number") {
  594. throw new Check.DeveloperError("c is a required number.");
  595. }
  596. if (typeof d !== "number") {
  597. throw new Check.DeveloperError("d is a required number.");
  598. }
  599. if (typeof e !== "number") {
  600. throw new Check.DeveloperError("e is a required number.");
  601. }
  602. //>>includeEnd('debug');
  603. if (Math.abs(a) < _Math.CesiumMath.EPSILON15) {
  604. return CubicRealPolynomial.computeRealRoots(b, c, d, e);
  605. }
  606. var a3 = b / a;
  607. var a2 = c / a;
  608. var a1 = d / a;
  609. var a0 = e / a;
  610. var 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 = Cartesian2.Cartesian3.clone(when.defaultValue(direction, Cartesian2.Cartesian3.ZERO));
  661. if (!Cartesian2.Cartesian3.equals(direction, Cartesian2.Cartesian3.ZERO)) {
  662. Cartesian2.Cartesian3.normalize(direction, direction);
  663. }
  664. /**
  665. * The origin of the ray.
  666. * @type {Cartesian3}
  667. * @default {@link Cartesian3.ZERO}
  668. */
  669. this.origin = Cartesian2.Cartesian3.clone(when.defaultValue(origin, Cartesian2.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 (!when.defined(ray)) {
  685. return undefined;
  686. }
  687. if (!when.defined(result)) {
  688. return new Ray(ray.origin, ray.direction);
  689. }
  690. result.origin = Cartesian2.Cartesian3.clone(ray.origin);
  691. result.direction = Cartesian2.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. * var intersection = Cesium.IntersectionTests.rayEllipsoid(ray, ellipsoid);
  706. * var point = Cesium.Ray.getPoint(ray, intersection.start);
  707. */
  708. Ray.getPoint = function (ray, t, result) {
  709. //>>includeStart('debug', pragmas.debug);
  710. Check.Check.typeOf.object("ray", ray);
  711. Check.Check.typeOf.number("t", t);
  712. //>>includeEnd('debug');
  713. if (!when.defined(result)) {
  714. result = new Cartesian2.Cartesian3();
  715. }
  716. result = Cartesian2.Cartesian3.multiplyByScalar(ray.direction, t, result);
  717. return Cartesian2.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. var 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 (!when.defined(ray)) {
  736. throw new Check.DeveloperError("ray is required.");
  737. }
  738. if (!when.defined(plane)) {
  739. throw new Check.DeveloperError("plane is required.");
  740. }
  741. //>>includeEnd('debug');
  742. if (!when.defined(result)) {
  743. result = new Cartesian2.Cartesian3();
  744. }
  745. var origin = ray.origin;
  746. var direction = ray.direction;
  747. var normal = plane.normal;
  748. var denominator = Cartesian2.Cartesian3.dot(normal, direction);
  749. if (Math.abs(denominator) < _Math.CesiumMath.EPSILON15) {
  750. // Ray is parallel to plane. The ray may be in the polygon's plane.
  751. return undefined;
  752. }
  753. var t = (-plane.distance - Cartesian2.Cartesian3.dot(normal, origin)) / denominator;
  754. if (t < 0) {
  755. return undefined;
  756. }
  757. result = Cartesian2.Cartesian3.multiplyByScalar(direction, t, result);
  758. return Cartesian2.Cartesian3.add(origin, result, result);
  759. };
  760. var scratchEdge0 = new Cartesian2.Cartesian3();
  761. var scratchEdge1 = new Cartesian2.Cartesian3();
  762. var scratchPVec = new Cartesian2.Cartesian3();
  763. var scratchTVec = new Cartesian2.Cartesian3();
  764. var scratchQVec = new Cartesian2.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 (!when.defined(ray)) {
  790. throw new Check.DeveloperError("ray is required.");
  791. }
  792. if (!when.defined(p0)) {
  793. throw new Check.DeveloperError("p0 is required.");
  794. }
  795. if (!when.defined(p1)) {
  796. throw new Check.DeveloperError("p1 is required.");
  797. }
  798. if (!when.defined(p2)) {
  799. throw new Check.DeveloperError("p2 is required.");
  800. }
  801. //>>includeEnd('debug');
  802. cullBackFaces = when.defaultValue(cullBackFaces, false);
  803. var origin = ray.origin;
  804. var direction = ray.direction;
  805. var edge0 = Cartesian2.Cartesian3.subtract(p1, p0, scratchEdge0);
  806. var edge1 = Cartesian2.Cartesian3.subtract(p2, p0, scratchEdge1);
  807. var p = Cartesian2.Cartesian3.cross(direction, edge1, scratchPVec);
  808. var det = Cartesian2.Cartesian3.dot(edge0, p);
  809. var tvec;
  810. var q;
  811. var u;
  812. var v;
  813. var t;
  814. if (cullBackFaces) {
  815. if (det < _Math.CesiumMath.EPSILON6) {
  816. return undefined;
  817. }
  818. tvec = Cartesian2.Cartesian3.subtract(origin, p0, scratchTVec);
  819. u = Cartesian2.Cartesian3.dot(tvec, p);
  820. if (u < 0.0 || u > det) {
  821. return undefined;
  822. }
  823. q = Cartesian2.Cartesian3.cross(tvec, edge0, scratchQVec);
  824. v = Cartesian2.Cartesian3.dot(direction, q);
  825. if (v < 0.0 || u + v > det) {
  826. return undefined;
  827. }
  828. t = Cartesian2.Cartesian3.dot(edge1, q) / det;
  829. } else {
  830. if (Math.abs(det) < _Math.CesiumMath.EPSILON6) {
  831. return undefined;
  832. }
  833. var invDet = 1.0 / det;
  834. tvec = Cartesian2.Cartesian3.subtract(origin, p0, scratchTVec);
  835. u = Cartesian2.Cartesian3.dot(tvec, p) * invDet;
  836. if (u < 0.0 || u > 1.0) {
  837. return undefined;
  838. }
  839. q = Cartesian2.Cartesian3.cross(tvec, edge0, scratchQVec);
  840. v = Cartesian2.Cartesian3.dot(direction, q) * invDet;
  841. if (v < 0.0 || u + v > 1.0) {
  842. return undefined;
  843. }
  844. t = Cartesian2.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. var t = IntersectionTests.rayTriangleParametric(
  874. ray,
  875. p0,
  876. p1,
  877. p2,
  878. cullBackFaces
  879. );
  880. if (!when.defined(t) || t < 0.0) {
  881. return undefined;
  882. }
  883. if (!when.defined(result)) {
  884. result = new Cartesian2.Cartesian3();
  885. }
  886. Cartesian2.Cartesian3.multiplyByScalar(ray.direction, t, result);
  887. return Cartesian2.Cartesian3.add(ray.origin, result, result);
  888. };
  889. var 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 (!when.defined(v0)) {
  915. throw new Check.DeveloperError("v0 is required.");
  916. }
  917. if (!when.defined(v1)) {
  918. throw new Check.DeveloperError("v1 is required.");
  919. }
  920. if (!when.defined(p0)) {
  921. throw new Check.DeveloperError("p0 is required.");
  922. }
  923. if (!when.defined(p1)) {
  924. throw new Check.DeveloperError("p1 is required.");
  925. }
  926. if (!when.defined(p2)) {
  927. throw new Check.DeveloperError("p2 is required.");
  928. }
  929. //>>includeEnd('debug');
  930. var ray = scratchLineSegmentTriangleRay;
  931. Cartesian2.Cartesian3.clone(v0, ray.origin);
  932. Cartesian2.Cartesian3.subtract(v1, v0, ray.direction);
  933. Cartesian2.Cartesian3.normalize(ray.direction, ray.direction);
  934. var t = IntersectionTests.rayTriangleParametric(
  935. ray,
  936. p0,
  937. p1,
  938. p2,
  939. cullBackFaces
  940. );
  941. if (!when.defined(t) || t < 0.0 || t > Cartesian2.Cartesian3.distance(v0, v1)) {
  942. return undefined;
  943. }
  944. if (!when.defined(result)) {
  945. result = new Cartesian2.Cartesian3();
  946. }
  947. Cartesian2.Cartesian3.multiplyByScalar(ray.direction, t, result);
  948. return Cartesian2.Cartesian3.add(ray.origin, result, result);
  949. };
  950. function solveQuadratic(a, b, c, result) {
  951. var det = b * b - 4.0 * a * c;
  952. if (det < 0.0) {
  953. return undefined;
  954. } else if (det > 0.0) {
  955. var denom = 1.0 / (2.0 * a);
  956. var disc = Math.sqrt(det);
  957. var root0 = (-b + disc) * denom;
  958. var 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. var 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. var raySphereRoots = {
  976. root0: 0.0,
  977. root1: 0.0,
  978. };
  979. function raySphere(ray, sphere, result) {
  980. if (!when.defined(result)) {
  981. result = new Transforms.Interval();
  982. }
  983. var origin = ray.origin;
  984. var direction = ray.direction;
  985. var center = sphere.center;
  986. var radiusSquared = sphere.radius * sphere.radius;
  987. var diff = Cartesian2.Cartesian3.subtract(origin, center, scratchPVec);
  988. var a = Cartesian2.Cartesian3.dot(direction, direction);
  989. var b = 2.0 * Cartesian2.Cartesian3.dot(direction, diff);
  990. var c = Cartesian2.Cartesian3.magnitudeSquared(diff) - radiusSquared;
  991. var roots = solveQuadratic(a, b, c, raySphereRoots);
  992. if (!when.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 (!when.defined(ray)) {
  1011. throw new Check.DeveloperError("ray is required.");
  1012. }
  1013. if (!when.defined(sphere)) {
  1014. throw new Check.DeveloperError("sphere is required.");
  1015. }
  1016. //>>includeEnd('debug');
  1017. result = raySphere(ray, sphere, result);
  1018. if (!when.defined(result) || result.stop < 0.0) {
  1019. return undefined;
  1020. }
  1021. result.start = Math.max(result.start, 0.0);
  1022. return result;
  1023. };
  1024. var 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 (!when.defined(p0)) {
  1038. throw new Check.DeveloperError("p0 is required.");
  1039. }
  1040. if (!when.defined(p1)) {
  1041. throw new Check.DeveloperError("p1 is required.");
  1042. }
  1043. if (!when.defined(sphere)) {
  1044. throw new Check.DeveloperError("sphere is required.");
  1045. }
  1046. //>>includeEnd('debug');
  1047. var ray = scratchLineSegmentRay;
  1048. Cartesian2.Cartesian3.clone(p0, ray.origin);
  1049. var direction = Cartesian2.Cartesian3.subtract(p1, p0, ray.direction);
  1050. var maxT = Cartesian2.Cartesian3.magnitude(direction);
  1051. Cartesian2.Cartesian3.normalize(direction, direction);
  1052. result = raySphere(ray, sphere, result);
  1053. if (!when.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. var scratchQ = new Cartesian2.Cartesian3();
  1061. var scratchW = new Cartesian2.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 (!when.defined(ray)) {
  1072. throw new Check.DeveloperError("ray is required.");
  1073. }
  1074. if (!when.defined(ellipsoid)) {
  1075. throw new Check.DeveloperError("ellipsoid is required.");
  1076. }
  1077. //>>includeEnd('debug');
  1078. var inverseRadii = ellipsoid.oneOverRadii;
  1079. var q = Cartesian2.Cartesian3.multiplyComponents(inverseRadii, ray.origin, scratchQ);
  1080. var w = Cartesian2.Cartesian3.multiplyComponents(inverseRadii, ray.direction, scratchW);
  1081. var q2 = Cartesian2.Cartesian3.magnitudeSquared(q);
  1082. var qw = Cartesian2.Cartesian3.dot(q, w);
  1083. var difference, w2, product, discriminant, temp;
  1084. if (q2 > 1.0) {
  1085. // Outside ellipsoid.
  1086. if (qw >= 0.0) {
  1087. // Looking outward or tangent (0 intersections).
  1088. return undefined;
  1089. }
  1090. // qw < 0.0.
  1091. var qw2 = qw * qw;
  1092. difference = q2 - 1.0; // Positively valued.
  1093. w2 = Cartesian2.Cartesian3.magnitudeSquared(w);
  1094. product = w2 * difference;
  1095. if (qw2 < product) {
  1096. // Imaginary roots (0 intersections).
  1097. return undefined;
  1098. } else if (qw2 > product) {
  1099. // Distinct roots (2 intersections).
  1100. discriminant = qw * qw - product;
  1101. temp = -qw + Math.sqrt(discriminant); // Avoid cancellation.
  1102. var root0 = temp / w2;
  1103. var root1 = difference / temp;
  1104. if (root0 < root1) {
  1105. return new Transforms.Interval(root0, root1);
  1106. }
  1107. return {
  1108. start: root1,
  1109. stop: root0,
  1110. };
  1111. }
  1112. // qw2 == product. Repeated roots (2 intersections).
  1113. var root = Math.sqrt(difference / w2);
  1114. return new Transforms.Interval(root, root);
  1115. } else if (q2 < 1.0) {
  1116. // Inside ellipsoid (2 intersections).
  1117. difference = q2 - 1.0; // Negatively valued.
  1118. w2 = Cartesian2.Cartesian3.magnitudeSquared(w);
  1119. product = w2 * difference; // Negatively valued.
  1120. discriminant = qw * qw - product;
  1121. temp = -qw + Math.sqrt(discriminant); // Positively valued.
  1122. return new Transforms.Interval(0.0, temp / w2);
  1123. }
  1124. // q2 == 1.0. On ellipsoid.
  1125. if (qw < 0.0) {
  1126. // Looking inward.
  1127. w2 = Cartesian2.Cartesian3.magnitudeSquared(w);
  1128. return new Transforms.Interval(0.0, -qw / w2);
  1129. }
  1130. // qw >= 0.0. Looking outward or tangent.
  1131. return undefined;
  1132. };
  1133. function addWithCancellationCheck$1(left, right, tolerance) {
  1134. var difference = left + right;
  1135. if (
  1136. _Math.CesiumMath.sign(left) !== _Math.CesiumMath.sign(right) &&
  1137. Math.abs(difference / Math.max(Math.abs(left), Math.abs(right))) < tolerance
  1138. ) {
  1139. return 0.0;
  1140. }
  1141. return difference;
  1142. }
  1143. function quadraticVectorExpression(A, b, c, x, w) {
  1144. var xSquared = x * x;
  1145. var wSquared = w * w;
  1146. var l2 = (A[Transforms.Matrix3.COLUMN1ROW1] - A[Transforms.Matrix3.COLUMN2ROW2]) * wSquared;
  1147. var l1 =
  1148. w *
  1149. (x *
  1150. addWithCancellationCheck$1(
  1151. A[Transforms.Matrix3.COLUMN1ROW0],
  1152. A[Transforms.Matrix3.COLUMN0ROW1],
  1153. _Math.CesiumMath.EPSILON15
  1154. ) +
  1155. b.y);
  1156. var l0 =
  1157. A[Transforms.Matrix3.COLUMN0ROW0] * xSquared +
  1158. A[Transforms.Matrix3.COLUMN2ROW2] * wSquared +
  1159. x * b.x +
  1160. c;
  1161. var r1 =
  1162. wSquared *
  1163. addWithCancellationCheck$1(
  1164. A[Transforms.Matrix3.COLUMN2ROW1],
  1165. A[Transforms.Matrix3.COLUMN1ROW2],
  1166. _Math.CesiumMath.EPSILON15
  1167. );
  1168. var r0 =
  1169. w *
  1170. (x *
  1171. addWithCancellationCheck$1(A[Transforms.Matrix3.COLUMN2ROW0], A[Transforms.Matrix3.COLUMN0ROW2]) +
  1172. b.z);
  1173. var cosines;
  1174. var solutions = [];
  1175. if (r0 === 0.0 && r1 === 0.0) {
  1176. cosines = QuadraticRealPolynomial.computeRealRoots(l2, l1, l0);
  1177. if (cosines.length === 0) {
  1178. return solutions;
  1179. }
  1180. var cosine0 = cosines[0];
  1181. var sine0 = Math.sqrt(Math.max(1.0 - cosine0 * cosine0, 0.0));
  1182. solutions.push(new Cartesian2.Cartesian3(x, w * cosine0, w * -sine0));
  1183. solutions.push(new Cartesian2.Cartesian3(x, w * cosine0, w * sine0));
  1184. if (cosines.length === 2) {
  1185. var cosine1 = cosines[1];
  1186. var sine1 = Math.sqrt(Math.max(1.0 - cosine1 * cosine1, 0.0));
  1187. solutions.push(new Cartesian2.Cartesian3(x, w * cosine1, w * -sine1));
  1188. solutions.push(new Cartesian2.Cartesian3(x, w * cosine1, w * sine1));
  1189. }
  1190. return solutions;
  1191. }
  1192. var r0Squared = r0 * r0;
  1193. var r1Squared = r1 * r1;
  1194. var l2Squared = l2 * l2;
  1195. var r0r1 = r0 * r1;
  1196. var c4 = l2Squared + r1Squared;
  1197. var c3 = 2.0 * (l1 * l2 + r0r1);
  1198. var c2 = 2.0 * l0 * l2 + l1 * l1 - r1Squared + r0Squared;
  1199. var c1 = 2.0 * (l0 * l1 - r0r1);
  1200. var c0 = l0 * l0 - r0Squared;
  1201. if (c4 === 0.0 && c3 === 0.0 && c2 === 0.0 && c1 === 0.0) {
  1202. return solutions;
  1203. }
  1204. cosines = QuarticRealPolynomial.computeRealRoots(c4, c3, c2, c1, c0);
  1205. var length = cosines.length;
  1206. if (length === 0) {
  1207. return solutions;
  1208. }
  1209. for (var i = 0; i < length; ++i) {
  1210. var cosine = cosines[i];
  1211. var cosineSquared = cosine * cosine;
  1212. var sineSquared = Math.max(1.0 - cosineSquared, 0.0);
  1213. var sine = Math.sqrt(sineSquared);
  1214. //var left = l2 * cosineSquared + l1 * cosine + l0;
  1215. var left;
  1216. if (_Math.CesiumMath.sign(l2) === _Math.CesiumMath.sign(l0)) {
  1217. left = addWithCancellationCheck$1(
  1218. l2 * cosineSquared + l0,
  1219. l1 * cosine,
  1220. _Math.CesiumMath.EPSILON12
  1221. );
  1222. } else if (_Math.CesiumMath.sign(l0) === _Math.CesiumMath.sign(l1 * cosine)) {
  1223. left = addWithCancellationCheck$1(
  1224. l2 * cosineSquared,
  1225. l1 * cosine + l0,
  1226. _Math.CesiumMath.EPSILON12
  1227. );
  1228. } else {
  1229. left = addWithCancellationCheck$1(
  1230. l2 * cosineSquared + l1 * cosine,
  1231. l0,
  1232. _Math.CesiumMath.EPSILON12
  1233. );
  1234. }
  1235. var right = addWithCancellationCheck$1(r1 * cosine, r0, _Math.CesiumMath.EPSILON15);
  1236. var product = left * right;
  1237. if (product < 0.0) {
  1238. solutions.push(new Cartesian2.Cartesian3(x, w * cosine, w * sine));
  1239. } else if (product > 0.0) {
  1240. solutions.push(new Cartesian2.Cartesian3(x, w * cosine, w * -sine));
  1241. } else if (sine !== 0.0) {
  1242. solutions.push(new Cartesian2.Cartesian3(x, w * cosine, w * -sine));
  1243. solutions.push(new Cartesian2.Cartesian3(x, w * cosine, w * sine));
  1244. ++i;
  1245. } else {
  1246. solutions.push(new Cartesian2.Cartesian3(x, w * cosine, w * sine));
  1247. }
  1248. }
  1249. return solutions;
  1250. }
  1251. var firstAxisScratch = new Cartesian2.Cartesian3();
  1252. var secondAxisScratch = new Cartesian2.Cartesian3();
  1253. var thirdAxisScratch = new Cartesian2.Cartesian3();
  1254. var referenceScratch = new Cartesian2.Cartesian3();
  1255. var bCart = new Cartesian2.Cartesian3();
  1256. var bScratch = new Transforms.Matrix3();
  1257. var btScratch = new Transforms.Matrix3();
  1258. var diScratch = new Transforms.Matrix3();
  1259. var dScratch = new Transforms.Matrix3();
  1260. var cScratch = new Transforms.Matrix3();
  1261. var tempMatrix = new Transforms.Matrix3();
  1262. var aScratch = new Transforms.Matrix3();
  1263. var sScratch = new Cartesian2.Cartesian3();
  1264. var closestScratch = new Cartesian2.Cartesian3();
  1265. var surfPointScratch = new Cartesian2.Cartographic();
  1266. /**
  1267. * Provides the point along the ray which is nearest to the ellipsoid.
  1268. *
  1269. * @param {Ray} ray The ray.
  1270. * @param {Ellipsoid} ellipsoid The ellipsoid.
  1271. * @returns {Cartesian3} The nearest planetodetic point on the ray.
  1272. */
  1273. IntersectionTests.grazingAltitudeLocation = function (ray, ellipsoid) {
  1274. //>>includeStart('debug', pragmas.debug);
  1275. if (!when.defined(ray)) {
  1276. throw new Check.DeveloperError("ray is required.");
  1277. }
  1278. if (!when.defined(ellipsoid)) {
  1279. throw new Check.DeveloperError("ellipsoid is required.");
  1280. }
  1281. //>>includeEnd('debug');
  1282. var position = ray.origin;
  1283. var direction = ray.direction;
  1284. if (!Cartesian2.Cartesian3.equals(position, Cartesian2.Cartesian3.ZERO)) {
  1285. var normal = ellipsoid.geodeticSurfaceNormal(position, firstAxisScratch);
  1286. if (Cartesian2.Cartesian3.dot(direction, normal) >= 0.0) {
  1287. // The location provided is the closest point in altitude
  1288. return position;
  1289. }
  1290. }
  1291. var intersects = when.defined(this.rayEllipsoid(ray, ellipsoid));
  1292. // Compute the scaled direction vector.
  1293. var f = ellipsoid.transformPositionToScaledSpace(direction, firstAxisScratch);
  1294. // Constructs a basis from the unit scaled direction vector. Construct its rotation and transpose.
  1295. var firstAxis = Cartesian2.Cartesian3.normalize(f, f);
  1296. var reference = Cartesian2.Cartesian3.mostOrthogonalAxis(f, referenceScratch);
  1297. var secondAxis = Cartesian2.Cartesian3.normalize(
  1298. Cartesian2.Cartesian3.cross(reference, firstAxis, secondAxisScratch),
  1299. secondAxisScratch
  1300. );
  1301. var thirdAxis = Cartesian2.Cartesian3.normalize(
  1302. Cartesian2.Cartesian3.cross(firstAxis, secondAxis, thirdAxisScratch),
  1303. thirdAxisScratch
  1304. );
  1305. var B = bScratch;
  1306. B[0] = firstAxis.x;
  1307. B[1] = firstAxis.y;
  1308. B[2] = firstAxis.z;
  1309. B[3] = secondAxis.x;
  1310. B[4] = secondAxis.y;
  1311. B[5] = secondAxis.z;
  1312. B[6] = thirdAxis.x;
  1313. B[7] = thirdAxis.y;
  1314. B[8] = thirdAxis.z;
  1315. var B_T = Transforms.Matrix3.transpose(B, btScratch);
  1316. // Get the scaling matrix and its inverse.
  1317. var D_I = Transforms.Matrix3.fromScale(ellipsoid.radii, diScratch);
  1318. var D = Transforms.Matrix3.fromScale(ellipsoid.oneOverRadii, dScratch);
  1319. var C = cScratch;
  1320. C[0] = 0.0;
  1321. C[1] = -direction.z;
  1322. C[2] = direction.y;
  1323. C[3] = direction.z;
  1324. C[4] = 0.0;
  1325. C[5] = -direction.x;
  1326. C[6] = -direction.y;
  1327. C[7] = direction.x;
  1328. C[8] = 0.0;
  1329. var temp = Transforms.Matrix3.multiply(
  1330. Transforms.Matrix3.multiply(B_T, D, tempMatrix),
  1331. C,
  1332. tempMatrix
  1333. );
  1334. var A = Transforms.Matrix3.multiply(Transforms.Matrix3.multiply(temp, D_I, aScratch), B, aScratch);
  1335. var b = Transforms.Matrix3.multiplyByVector(temp, position, bCart);
  1336. // Solve for the solutions to the expression in standard form:
  1337. var solutions = quadraticVectorExpression(
  1338. A,
  1339. Cartesian2.Cartesian3.negate(b, firstAxisScratch),
  1340. 0.0,
  1341. 0.0,
  1342. 1.0
  1343. );
  1344. var s;
  1345. var altitude;
  1346. var length = solutions.length;
  1347. if (length > 0) {
  1348. var closest = Cartesian2.Cartesian3.clone(Cartesian2.Cartesian3.ZERO, closestScratch);
  1349. var maximumValue = Number.NEGATIVE_INFINITY;
  1350. for (var i = 0; i < length; ++i) {
  1351. s = Transforms.Matrix3.multiplyByVector(
  1352. D_I,
  1353. Transforms.Matrix3.multiplyByVector(B, solutions[i], sScratch),
  1354. sScratch
  1355. );
  1356. var v = Cartesian2.Cartesian3.normalize(
  1357. Cartesian2.Cartesian3.subtract(s, position, referenceScratch),
  1358. referenceScratch
  1359. );
  1360. var dotProduct = Cartesian2.Cartesian3.dot(v, direction);
  1361. if (dotProduct > maximumValue) {
  1362. maximumValue = dotProduct;
  1363. closest = Cartesian2.Cartesian3.clone(s, closest);
  1364. }
  1365. }
  1366. var surfacePoint = ellipsoid.cartesianToCartographic(
  1367. closest,
  1368. surfPointScratch
  1369. );
  1370. maximumValue = _Math.CesiumMath.clamp(maximumValue, 0.0, 1.0);
  1371. altitude =
  1372. Cartesian2.Cartesian3.magnitude(
  1373. Cartesian2.Cartesian3.subtract(closest, position, referenceScratch)
  1374. ) * Math.sqrt(1.0 - maximumValue * maximumValue);
  1375. altitude = intersects ? -altitude : altitude;
  1376. surfacePoint.height = altitude;
  1377. return ellipsoid.cartographicToCartesian(surfacePoint, new Cartesian2.Cartesian3());
  1378. }
  1379. return undefined;
  1380. };
  1381. var lineSegmentPlaneDifference = new Cartesian2.Cartesian3();
  1382. /**
  1383. * Computes the intersection of a line segment and a plane.
  1384. *
  1385. * @param {Cartesian3} endPoint0 An end point of the line segment.
  1386. * @param {Cartesian3} endPoint1 The other end point of the line segment.
  1387. * @param {Plane} plane The plane.
  1388. * @param {Cartesian3} [result] The object onto which to store the result.
  1389. * @returns {Cartesian3} The intersection point or undefined if there is no intersection.
  1390. *
  1391. * @example
  1392. * var origin = Cesium.Cartesian3.fromDegrees(-75.59777, 40.03883);
  1393. * var normal = ellipsoid.geodeticSurfaceNormal(origin);
  1394. * var plane = Cesium.Plane.fromPointNormal(origin, normal);
  1395. *
  1396. * var p0 = new Cesium.Cartesian3(...);
  1397. * var p1 = new Cesium.Cartesian3(...);
  1398. *
  1399. * // find the intersection of the line segment from p0 to p1 and the tangent plane at origin.
  1400. * var intersection = Cesium.IntersectionTests.lineSegmentPlane(p0, p1, plane);
  1401. */
  1402. IntersectionTests.lineSegmentPlane = function (
  1403. endPoint0,
  1404. endPoint1,
  1405. plane,
  1406. result
  1407. ) {
  1408. //>>includeStart('debug', pragmas.debug);
  1409. if (!when.defined(endPoint0)) {
  1410. throw new Check.DeveloperError("endPoint0 is required.");
  1411. }
  1412. if (!when.defined(endPoint1)) {
  1413. throw new Check.DeveloperError("endPoint1 is required.");
  1414. }
  1415. if (!when.defined(plane)) {
  1416. throw new Check.DeveloperError("plane is required.");
  1417. }
  1418. //>>includeEnd('debug');
  1419. if (!when.defined(result)) {
  1420. result = new Cartesian2.Cartesian3();
  1421. }
  1422. var difference = Cartesian2.Cartesian3.subtract(
  1423. endPoint1,
  1424. endPoint0,
  1425. lineSegmentPlaneDifference
  1426. );
  1427. var normal = plane.normal;
  1428. var nDotDiff = Cartesian2.Cartesian3.dot(normal, difference);
  1429. // check if the segment and plane are parallel
  1430. if (Math.abs(nDotDiff) < _Math.CesiumMath.EPSILON6) {
  1431. return undefined;
  1432. }
  1433. var nDotP0 = Cartesian2.Cartesian3.dot(normal, endPoint0);
  1434. var t = -(plane.distance + nDotP0) / nDotDiff;
  1435. // intersection only if t is in [0, 1]
  1436. if (t < 0.0 || t > 1.0) {
  1437. return undefined;
  1438. }
  1439. // intersection is endPoint0 + t * (endPoint1 - endPoint0)
  1440. Cartesian2.Cartesian3.multiplyByScalar(difference, t, result);
  1441. Cartesian2.Cartesian3.add(endPoint0, result, result);
  1442. return result;
  1443. };
  1444. /**
  1445. * Computes the intersection of a triangle and a plane
  1446. *
  1447. * @param {Cartesian3} p0 First point of the triangle
  1448. * @param {Cartesian3} p1 Second point of the triangle
  1449. * @param {Cartesian3} p2 Third point of the triangle
  1450. * @param {Plane} plane Intersection plane
  1451. * @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)
  1452. *
  1453. * @example
  1454. * var origin = Cesium.Cartesian3.fromDegrees(-75.59777, 40.03883);
  1455. * var normal = ellipsoid.geodeticSurfaceNormal(origin);
  1456. * var plane = Cesium.Plane.fromPointNormal(origin, normal);
  1457. *
  1458. * var p0 = new Cesium.Cartesian3(...);
  1459. * var p1 = new Cesium.Cartesian3(...);
  1460. * var p2 = new Cesium.Cartesian3(...);
  1461. *
  1462. * // convert the triangle composed of points (p0, p1, p2) to three triangles that don't cross the plane
  1463. * var triangles = Cesium.IntersectionTests.trianglePlaneIntersection(p0, p1, p2, plane);
  1464. */
  1465. IntersectionTests.trianglePlaneIntersection = function (p0, p1, p2, plane) {
  1466. //>>includeStart('debug', pragmas.debug);
  1467. if (!when.defined(p0) || !when.defined(p1) || !when.defined(p2) || !when.defined(plane)) {
  1468. throw new Check.DeveloperError("p0, p1, p2, and plane are required.");
  1469. }
  1470. //>>includeEnd('debug');
  1471. var planeNormal = plane.normal;
  1472. var planeD = plane.distance;
  1473. var p0Behind = Cartesian2.Cartesian3.dot(planeNormal, p0) + planeD < 0.0;
  1474. var p1Behind = Cartesian2.Cartesian3.dot(planeNormal, p1) + planeD < 0.0;
  1475. var p2Behind = Cartesian2.Cartesian3.dot(planeNormal, p2) + planeD < 0.0;
  1476. // Given these dots products, the calls to lineSegmentPlaneIntersection
  1477. // always have defined results.
  1478. var numBehind = 0;
  1479. numBehind += p0Behind ? 1 : 0;
  1480. numBehind += p1Behind ? 1 : 0;
  1481. numBehind += p2Behind ? 1 : 0;
  1482. var u1, u2;
  1483. if (numBehind === 1 || numBehind === 2) {
  1484. u1 = new Cartesian2.Cartesian3();
  1485. u2 = new Cartesian2.Cartesian3();
  1486. }
  1487. if (numBehind === 1) {
  1488. if (p0Behind) {
  1489. IntersectionTests.lineSegmentPlane(p0, p1, plane, u1);
  1490. IntersectionTests.lineSegmentPlane(p0, p2, plane, u2);
  1491. return {
  1492. positions: [p0, p1, p2, u1, u2],
  1493. indices: [
  1494. // Behind
  1495. 0,
  1496. 3,
  1497. 4,
  1498. // In front
  1499. 1,
  1500. 2,
  1501. 4,
  1502. 1,
  1503. 4,
  1504. 3,
  1505. ],
  1506. };
  1507. } else if (p1Behind) {
  1508. IntersectionTests.lineSegmentPlane(p1, p2, plane, u1);
  1509. IntersectionTests.lineSegmentPlane(p1, p0, plane, u2);
  1510. return {
  1511. positions: [p0, p1, p2, u1, u2],
  1512. indices: [
  1513. // Behind
  1514. 1,
  1515. 3,
  1516. 4,
  1517. // In front
  1518. 2,
  1519. 0,
  1520. 4,
  1521. 2,
  1522. 4,
  1523. 3,
  1524. ],
  1525. };
  1526. } else if (p2Behind) {
  1527. IntersectionTests.lineSegmentPlane(p2, p0, plane, u1);
  1528. IntersectionTests.lineSegmentPlane(p2, p1, plane, u2);
  1529. return {
  1530. positions: [p0, p1, p2, u1, u2],
  1531. indices: [
  1532. // Behind
  1533. 2,
  1534. 3,
  1535. 4,
  1536. // In front
  1537. 0,
  1538. 1,
  1539. 4,
  1540. 0,
  1541. 4,
  1542. 3,
  1543. ],
  1544. };
  1545. }
  1546. } else if (numBehind === 2) {
  1547. if (!p0Behind) {
  1548. IntersectionTests.lineSegmentPlane(p1, p0, plane, u1);
  1549. IntersectionTests.lineSegmentPlane(p2, p0, plane, u2);
  1550. return {
  1551. positions: [p0, p1, p2, u1, u2],
  1552. indices: [
  1553. // Behind
  1554. 1,
  1555. 2,
  1556. 4,
  1557. 1,
  1558. 4,
  1559. 3,
  1560. // In front
  1561. 0,
  1562. 3,
  1563. 4,
  1564. ],
  1565. };
  1566. } else if (!p1Behind) {
  1567. IntersectionTests.lineSegmentPlane(p2, p1, plane, u1);
  1568. IntersectionTests.lineSegmentPlane(p0, p1, plane, u2);
  1569. return {
  1570. positions: [p0, p1, p2, u1, u2],
  1571. indices: [
  1572. // Behind
  1573. 2,
  1574. 0,
  1575. 4,
  1576. 2,
  1577. 4,
  1578. 3,
  1579. // In front
  1580. 1,
  1581. 3,
  1582. 4,
  1583. ],
  1584. };
  1585. } else if (!p2Behind) {
  1586. IntersectionTests.lineSegmentPlane(p0, p2, plane, u1);
  1587. IntersectionTests.lineSegmentPlane(p1, p2, plane, u2);
  1588. return {
  1589. positions: [p0, p1, p2, u1, u2],
  1590. indices: [
  1591. // Behind
  1592. 0,
  1593. 1,
  1594. 4,
  1595. 0,
  1596. 4,
  1597. 3,
  1598. // In front
  1599. 2,
  1600. 3,
  1601. 4,
  1602. ],
  1603. };
  1604. }
  1605. }
  1606. // if numBehind is 3, the triangle is completely behind the plane;
  1607. // otherwise, it is completely in front (numBehind is 0).
  1608. return undefined;
  1609. };
  1610. exports.IntersectionTests = IntersectionTests;
  1611. exports.Ray = Ray;
  1612. });
  1613. //# sourceMappingURL=IntersectionTests-afc38163.js.map