EllipsoidalOccluder.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  1. import BoundingSphere from "./BoundingSphere.js";
  2. import Cartesian3 from "./Cartesian3.js";
  3. import Check from "./Check.js";
  4. import defaultValue from "./defaultValue.js";
  5. import defined from "./defined.js";
  6. import Ellipsoid from "./Ellipsoid.js";
  7. import Rectangle from "./Rectangle.js";
  8. /**
  9. * Determine whether or not other objects are visible or hidden behind the visible horizon defined by
  10. * an {@link Ellipsoid} and a camera position. The ellipsoid is assumed to be located at the
  11. * origin of the coordinate system. This class uses the algorithm described in the
  12. * {@link https://cesium.com/blog/2013/04/25/Horizon-culling/|Horizon Culling} blog post.
  13. *
  14. * @alias EllipsoidalOccluder
  15. *
  16. * @param {Ellipsoid} ellipsoid The ellipsoid to use as an occluder.
  17. * @param {Cartesian3} [cameraPosition] The coordinate of the viewer/camera. If this parameter is not
  18. * specified, {@link EllipsoidalOccluder#cameraPosition} must be called before
  19. * testing visibility.
  20. *
  21. * @constructor
  22. *
  23. * @example
  24. * // Construct an ellipsoidal occluder with radii 1.0, 1.1, and 0.9.
  25. * const cameraPosition = new Cesium.Cartesian3(5.0, 6.0, 7.0);
  26. * const occluderEllipsoid = new Cesium.Ellipsoid(1.0, 1.1, 0.9);
  27. * const occluder = new Cesium.EllipsoidalOccluder(occluderEllipsoid, cameraPosition);
  28. *
  29. * @private
  30. */
  31. function EllipsoidalOccluder(ellipsoid, cameraPosition) {
  32. //>>includeStart('debug', pragmas.debug);
  33. Check.typeOf.object("ellipsoid", ellipsoid);
  34. //>>includeEnd('debug');
  35. this._ellipsoid = ellipsoid;
  36. this._cameraPosition = new Cartesian3();
  37. this._cameraPositionInScaledSpace = new Cartesian3();
  38. this._distanceToLimbInScaledSpaceSquared = 0.0;
  39. // cameraPosition fills in the above values
  40. if (defined(cameraPosition)) {
  41. this.cameraPosition = cameraPosition;
  42. }
  43. }
  44. Object.defineProperties(EllipsoidalOccluder.prototype, {
  45. /**
  46. * Gets the occluding ellipsoid.
  47. * @memberof EllipsoidalOccluder.prototype
  48. * @type {Ellipsoid}
  49. */
  50. ellipsoid: {
  51. get: function () {
  52. return this._ellipsoid;
  53. },
  54. },
  55. /**
  56. * Gets or sets the position of the camera.
  57. * @memberof EllipsoidalOccluder.prototype
  58. * @type {Cartesian3}
  59. */
  60. cameraPosition: {
  61. get: function () {
  62. return this._cameraPosition;
  63. },
  64. set: function (cameraPosition) {
  65. // See https://cesium.com/blog/2013/04/25/Horizon-culling/
  66. const ellipsoid = this._ellipsoid;
  67. const cv = ellipsoid.transformPositionToScaledSpace(
  68. cameraPosition,
  69. this._cameraPositionInScaledSpace
  70. );
  71. const vhMagnitudeSquared = Cartesian3.magnitudeSquared(cv) - 1.0;
  72. Cartesian3.clone(cameraPosition, this._cameraPosition);
  73. this._cameraPositionInScaledSpace = cv;
  74. this._distanceToLimbInScaledSpaceSquared = vhMagnitudeSquared;
  75. },
  76. },
  77. });
  78. const scratchCartesian = new Cartesian3();
  79. /**
  80. * Determines whether or not a point, the <code>occludee</code>, is hidden from view by the occluder.
  81. *
  82. * @param {Cartesian3} occludee The point to test for visibility.
  83. * @returns {boolean} <code>true</code> if the occludee is visible; otherwise <code>false</code>.
  84. *
  85. * @example
  86. * const cameraPosition = new Cesium.Cartesian3(0, 0, 2.5);
  87. * const ellipsoid = new Cesium.Ellipsoid(1.0, 1.1, 0.9);
  88. * const occluder = new Cesium.EllipsoidalOccluder(ellipsoid, cameraPosition);
  89. * const point = new Cesium.Cartesian3(0, -3, -3);
  90. * occluder.isPointVisible(point); //returns true
  91. */
  92. EllipsoidalOccluder.prototype.isPointVisible = function (occludee) {
  93. const ellipsoid = this._ellipsoid;
  94. const occludeeScaledSpacePosition = ellipsoid.transformPositionToScaledSpace(
  95. occludee,
  96. scratchCartesian
  97. );
  98. return isScaledSpacePointVisible(
  99. occludeeScaledSpacePosition,
  100. this._cameraPositionInScaledSpace,
  101. this._distanceToLimbInScaledSpaceSquared
  102. );
  103. };
  104. /**
  105. * Determines whether or not a point expressed in the ellipsoid scaled space, is hidden from view by the
  106. * occluder. To transform a Cartesian X, Y, Z position in the coordinate system aligned with the ellipsoid
  107. * into the scaled space, call {@link Ellipsoid#transformPositionToScaledSpace}.
  108. *
  109. * @param {Cartesian3} occludeeScaledSpacePosition The point to test for visibility, represented in the scaled space.
  110. * @returns {boolean} <code>true</code> if the occludee is visible; otherwise <code>false</code>.
  111. *
  112. * @example
  113. * const cameraPosition = new Cesium.Cartesian3(0, 0, 2.5);
  114. * const ellipsoid = new Cesium.Ellipsoid(1.0, 1.1, 0.9);
  115. * const occluder = new Cesium.EllipsoidalOccluder(ellipsoid, cameraPosition);
  116. * const point = new Cesium.Cartesian3(0, -3, -3);
  117. * const scaledSpacePoint = ellipsoid.transformPositionToScaledSpace(point);
  118. * occluder.isScaledSpacePointVisible(scaledSpacePoint); //returns true
  119. */
  120. EllipsoidalOccluder.prototype.isScaledSpacePointVisible = function (
  121. occludeeScaledSpacePosition
  122. ) {
  123. return isScaledSpacePointVisible(
  124. occludeeScaledSpacePosition,
  125. this._cameraPositionInScaledSpace,
  126. this._distanceToLimbInScaledSpaceSquared
  127. );
  128. };
  129. const scratchCameraPositionInScaledSpaceShrunk = new Cartesian3();
  130. /**
  131. * Similar to {@link EllipsoidalOccluder#isScaledSpacePointVisible} except tests against an
  132. * ellipsoid that has been shrunk by the minimum height when the minimum height is below
  133. * the ellipsoid. This is intended to be used with points generated by
  134. * {@link EllipsoidalOccluder#computeHorizonCullingPointPossiblyUnderEllipsoid} or
  135. * {@link EllipsoidalOccluder#computeHorizonCullingPointFromVerticesPossiblyUnderEllipsoid}.
  136. *
  137. * @param {Cartesian3} occludeeScaledSpacePosition The point to test for visibility, represented in the scaled space of the possibly-shrunk ellipsoid.
  138. * @returns {boolean} <code>true</code> if the occludee is visible; otherwise <code>false</code>.
  139. */
  140. EllipsoidalOccluder.prototype.isScaledSpacePointVisiblePossiblyUnderEllipsoid = function (
  141. occludeeScaledSpacePosition,
  142. minimumHeight
  143. ) {
  144. const ellipsoid = this._ellipsoid;
  145. let vhMagnitudeSquared;
  146. let cv;
  147. if (
  148. defined(minimumHeight) &&
  149. minimumHeight < 0.0 &&
  150. ellipsoid.minimumRadius > -minimumHeight
  151. ) {
  152. // This code is similar to the cameraPosition setter, but unrolled for performance because it will be called a lot.
  153. cv = scratchCameraPositionInScaledSpaceShrunk;
  154. cv.x = this._cameraPosition.x / (ellipsoid.radii.x + minimumHeight);
  155. cv.y = this._cameraPosition.y / (ellipsoid.radii.y + minimumHeight);
  156. cv.z = this._cameraPosition.z / (ellipsoid.radii.z + minimumHeight);
  157. vhMagnitudeSquared = cv.x * cv.x + cv.y * cv.y + cv.z * cv.z - 1.0;
  158. } else {
  159. cv = this._cameraPositionInScaledSpace;
  160. vhMagnitudeSquared = this._distanceToLimbInScaledSpaceSquared;
  161. }
  162. return isScaledSpacePointVisible(
  163. occludeeScaledSpacePosition,
  164. cv,
  165. vhMagnitudeSquared
  166. );
  167. };
  168. /**
  169. * Computes a point that can be used for horizon culling from a list of positions. If the point is below
  170. * the horizon, all of the positions are guaranteed to be below the horizon as well. The returned point
  171. * is expressed in the ellipsoid-scaled space and is suitable for use with
  172. * {@link EllipsoidalOccluder#isScaledSpacePointVisible}.
  173. *
  174. * @param {Cartesian3} directionToPoint The direction that the computed point will lie along.
  175. * A reasonable direction to use is the direction from the center of the ellipsoid to
  176. * the center of the bounding sphere computed from the positions. The direction need not
  177. * be normalized.
  178. * @param {Cartesian3[]} positions The positions from which to compute the horizon culling point. The positions
  179. * must be expressed in a reference frame centered at the ellipsoid and aligned with the
  180. * ellipsoid's axes.
  181. * @param {Cartesian3} [result] The instance on which to store the result instead of allocating a new instance.
  182. * @returns {Cartesian3} The computed horizon culling point, expressed in the ellipsoid-scaled space.
  183. */
  184. EllipsoidalOccluder.prototype.computeHorizonCullingPoint = function (
  185. directionToPoint,
  186. positions,
  187. result
  188. ) {
  189. return computeHorizonCullingPointFromPositions(
  190. this._ellipsoid,
  191. directionToPoint,
  192. positions,
  193. result
  194. );
  195. };
  196. const scratchEllipsoidShrunk = Ellipsoid.clone(Ellipsoid.UNIT_SPHERE);
  197. /**
  198. * Similar to {@link EllipsoidalOccluder#computeHorizonCullingPoint} except computes the culling
  199. * point relative to an ellipsoid that has been shrunk by the minimum height when the minimum height is below
  200. * the ellipsoid. The returned point is expressed in the possibly-shrunk ellipsoid-scaled space and is suitable
  201. * for use with {@link EllipsoidalOccluder#isScaledSpacePointVisiblePossiblyUnderEllipsoid}.
  202. *
  203. * @param {Cartesian3} directionToPoint The direction that the computed point will lie along.
  204. * A reasonable direction to use is the direction from the center of the ellipsoid to
  205. * the center of the bounding sphere computed from the positions. The direction need not
  206. * be normalized.
  207. * @param {Cartesian3[]} positions The positions from which to compute the horizon culling point. The positions
  208. * must be expressed in a reference frame centered at the ellipsoid and aligned with the
  209. * ellipsoid's axes.
  210. * @param {number} [minimumHeight] The minimum height of all positions. If this value is undefined, all positions are assumed to be above the ellipsoid.
  211. * @param {Cartesian3} [result] The instance on which to store the result instead of allocating a new instance.
  212. * @returns {Cartesian3} The computed horizon culling point, expressed in the possibly-shrunk ellipsoid-scaled space.
  213. */
  214. EllipsoidalOccluder.prototype.computeHorizonCullingPointPossiblyUnderEllipsoid = function (
  215. directionToPoint,
  216. positions,
  217. minimumHeight,
  218. result
  219. ) {
  220. const possiblyShrunkEllipsoid = getPossiblyShrunkEllipsoid(
  221. this._ellipsoid,
  222. minimumHeight,
  223. scratchEllipsoidShrunk
  224. );
  225. return computeHorizonCullingPointFromPositions(
  226. possiblyShrunkEllipsoid,
  227. directionToPoint,
  228. positions,
  229. result
  230. );
  231. };
  232. /**
  233. * Computes a point that can be used for horizon culling from a list of positions. If the point is below
  234. * the horizon, all of the positions are guaranteed to be below the horizon as well. The returned point
  235. * is expressed in the ellipsoid-scaled space and is suitable for use with
  236. * {@link EllipsoidalOccluder#isScaledSpacePointVisible}.
  237. *
  238. * @param {Cartesian3} directionToPoint The direction that the computed point will lie along.
  239. * A reasonable direction to use is the direction from the center of the ellipsoid to
  240. * the center of the bounding sphere computed from the positions. The direction need not
  241. * be normalized.
  242. * @param {number[]} vertices The vertices from which to compute the horizon culling point. The positions
  243. * must be expressed in a reference frame centered at the ellipsoid and aligned with the
  244. * ellipsoid's axes.
  245. * @param {number} [stride=3]
  246. * @param {Cartesian3} [center=Cartesian3.ZERO]
  247. * @param {Cartesian3} [result] The instance on which to store the result instead of allocating a new instance.
  248. * @returns {Cartesian3} The computed horizon culling point, expressed in the ellipsoid-scaled space.
  249. */
  250. EllipsoidalOccluder.prototype.computeHorizonCullingPointFromVertices = function (
  251. directionToPoint,
  252. vertices,
  253. stride,
  254. center,
  255. result
  256. ) {
  257. return computeHorizonCullingPointFromVertices(
  258. this._ellipsoid,
  259. directionToPoint,
  260. vertices,
  261. stride,
  262. center,
  263. result
  264. );
  265. };
  266. /**
  267. * Similar to {@link EllipsoidalOccluder#computeHorizonCullingPointFromVertices} except computes the culling
  268. * point relative to an ellipsoid that has been shrunk by the minimum height when the minimum height is below
  269. * the ellipsoid. The returned point is expressed in the possibly-shrunk ellipsoid-scaled space and is suitable
  270. * for use with {@link EllipsoidalOccluder#isScaledSpacePointVisiblePossiblyUnderEllipsoid}.
  271. *
  272. * @param {Cartesian3} directionToPoint The direction that the computed point will lie along.
  273. * A reasonable direction to use is the direction from the center of the ellipsoid to
  274. * the center of the bounding sphere computed from the positions. The direction need not
  275. * be normalized.
  276. * @param {number[]} vertices The vertices from which to compute the horizon culling point. The positions
  277. * must be expressed in a reference frame centered at the ellipsoid and aligned with the
  278. * ellipsoid's axes.
  279. * @param {number} [stride=3]
  280. * @param {Cartesian3} [center=Cartesian3.ZERO]
  281. * @param {number} [minimumHeight] The minimum height of all vertices. If this value is undefined, all vertices are assumed to be above the ellipsoid.
  282. * @param {Cartesian3} [result] The instance on which to store the result instead of allocating a new instance.
  283. * @returns {Cartesian3} The computed horizon culling point, expressed in the possibly-shrunk ellipsoid-scaled space.
  284. */
  285. EllipsoidalOccluder.prototype.computeHorizonCullingPointFromVerticesPossiblyUnderEllipsoid = function (
  286. directionToPoint,
  287. vertices,
  288. stride,
  289. center,
  290. minimumHeight,
  291. result
  292. ) {
  293. const possiblyShrunkEllipsoid = getPossiblyShrunkEllipsoid(
  294. this._ellipsoid,
  295. minimumHeight,
  296. scratchEllipsoidShrunk
  297. );
  298. return computeHorizonCullingPointFromVertices(
  299. possiblyShrunkEllipsoid,
  300. directionToPoint,
  301. vertices,
  302. stride,
  303. center,
  304. result
  305. );
  306. };
  307. const subsampleScratch = [];
  308. /**
  309. * Computes a point that can be used for horizon culling of a rectangle. If the point is below
  310. * the horizon, the ellipsoid-conforming rectangle is guaranteed to be below the horizon as well.
  311. * The returned point is expressed in the ellipsoid-scaled space and is suitable for use with
  312. * {@link EllipsoidalOccluder#isScaledSpacePointVisible}.
  313. *
  314. * @param {Rectangle} rectangle The rectangle for which to compute the horizon culling point.
  315. * @param {Ellipsoid} ellipsoid The ellipsoid on which the rectangle is defined. This may be different from
  316. * the ellipsoid used by this instance for occlusion testing.
  317. * @param {Cartesian3} [result] The instance on which to store the result instead of allocating a new instance.
  318. * @returns {Cartesian3} The computed horizon culling point, expressed in the ellipsoid-scaled space.
  319. */
  320. EllipsoidalOccluder.prototype.computeHorizonCullingPointFromRectangle = function (
  321. rectangle,
  322. ellipsoid,
  323. result
  324. ) {
  325. //>>includeStart('debug', pragmas.debug);
  326. Check.typeOf.object("rectangle", rectangle);
  327. //>>includeEnd('debug');
  328. const positions = Rectangle.subsample(
  329. rectangle,
  330. ellipsoid,
  331. 0.0,
  332. subsampleScratch
  333. );
  334. const bs = BoundingSphere.fromPoints(positions);
  335. // If the bounding sphere center is too close to the center of the occluder, it doesn't make
  336. // sense to try to horizon cull it.
  337. if (Cartesian3.magnitude(bs.center) < 0.1 * ellipsoid.minimumRadius) {
  338. return undefined;
  339. }
  340. return this.computeHorizonCullingPoint(bs.center, positions, result);
  341. };
  342. const scratchEllipsoidShrunkRadii = new Cartesian3();
  343. function getPossiblyShrunkEllipsoid(ellipsoid, minimumHeight, result) {
  344. if (
  345. defined(minimumHeight) &&
  346. minimumHeight < 0.0 &&
  347. ellipsoid.minimumRadius > -minimumHeight
  348. ) {
  349. const ellipsoidShrunkRadii = Cartesian3.fromElements(
  350. ellipsoid.radii.x + minimumHeight,
  351. ellipsoid.radii.y + minimumHeight,
  352. ellipsoid.radii.z + minimumHeight,
  353. scratchEllipsoidShrunkRadii
  354. );
  355. ellipsoid = Ellipsoid.fromCartesian3(ellipsoidShrunkRadii, result);
  356. }
  357. return ellipsoid;
  358. }
  359. function computeHorizonCullingPointFromPositions(
  360. ellipsoid,
  361. directionToPoint,
  362. positions,
  363. result
  364. ) {
  365. //>>includeStart('debug', pragmas.debug);
  366. Check.typeOf.object("directionToPoint", directionToPoint);
  367. Check.defined("positions", positions);
  368. //>>includeEnd('debug');
  369. if (!defined(result)) {
  370. result = new Cartesian3();
  371. }
  372. const scaledSpaceDirectionToPoint = computeScaledSpaceDirectionToPoint(
  373. ellipsoid,
  374. directionToPoint
  375. );
  376. let resultMagnitude = 0.0;
  377. for (let i = 0, len = positions.length; i < len; ++i) {
  378. const position = positions[i];
  379. const candidateMagnitude = computeMagnitude(
  380. ellipsoid,
  381. position,
  382. scaledSpaceDirectionToPoint
  383. );
  384. if (candidateMagnitude < 0.0) {
  385. // all points should face the same direction, but this one doesn't, so return undefined
  386. return undefined;
  387. }
  388. resultMagnitude = Math.max(resultMagnitude, candidateMagnitude);
  389. }
  390. return magnitudeToPoint(scaledSpaceDirectionToPoint, resultMagnitude, result);
  391. }
  392. const positionScratch = new Cartesian3();
  393. function computeHorizonCullingPointFromVertices(
  394. ellipsoid,
  395. directionToPoint,
  396. vertices,
  397. stride,
  398. center,
  399. result
  400. ) {
  401. //>>includeStart('debug', pragmas.debug);
  402. Check.typeOf.object("directionToPoint", directionToPoint);
  403. Check.defined("vertices", vertices);
  404. Check.typeOf.number("stride", stride);
  405. //>>includeEnd('debug');
  406. if (!defined(result)) {
  407. result = new Cartesian3();
  408. }
  409. stride = defaultValue(stride, 3);
  410. center = defaultValue(center, Cartesian3.ZERO);
  411. const scaledSpaceDirectionToPoint = computeScaledSpaceDirectionToPoint(
  412. ellipsoid,
  413. directionToPoint
  414. );
  415. let resultMagnitude = 0.0;
  416. for (let i = 0, len = vertices.length; i < len; i += stride) {
  417. positionScratch.x = vertices[i] + center.x;
  418. positionScratch.y = vertices[i + 1] + center.y;
  419. positionScratch.z = vertices[i + 2] + center.z;
  420. const candidateMagnitude = computeMagnitude(
  421. ellipsoid,
  422. positionScratch,
  423. scaledSpaceDirectionToPoint
  424. );
  425. if (candidateMagnitude < 0.0) {
  426. // all points should face the same direction, but this one doesn't, so return undefined
  427. return undefined;
  428. }
  429. resultMagnitude = Math.max(resultMagnitude, candidateMagnitude);
  430. }
  431. return magnitudeToPoint(scaledSpaceDirectionToPoint, resultMagnitude, result);
  432. }
  433. function isScaledSpacePointVisible(
  434. occludeeScaledSpacePosition,
  435. cameraPositionInScaledSpace,
  436. distanceToLimbInScaledSpaceSquared
  437. ) {
  438. // See https://cesium.com/blog/2013/04/25/Horizon-culling/
  439. const cv = cameraPositionInScaledSpace;
  440. const vhMagnitudeSquared = distanceToLimbInScaledSpaceSquared;
  441. const vt = Cartesian3.subtract(
  442. occludeeScaledSpacePosition,
  443. cv,
  444. scratchCartesian
  445. );
  446. const vtDotVc = -Cartesian3.dot(vt, cv);
  447. // If vhMagnitudeSquared < 0 then we are below the surface of the ellipsoid and
  448. // in this case, set the culling plane to be on V.
  449. const isOccluded =
  450. vhMagnitudeSquared < 0
  451. ? vtDotVc > 0
  452. : vtDotVc > vhMagnitudeSquared &&
  453. (vtDotVc * vtDotVc) / Cartesian3.magnitudeSquared(vt) >
  454. vhMagnitudeSquared;
  455. return !isOccluded;
  456. }
  457. const scaledSpaceScratch = new Cartesian3();
  458. const directionScratch = new Cartesian3();
  459. function computeMagnitude(ellipsoid, position, scaledSpaceDirectionToPoint) {
  460. const scaledSpacePosition = ellipsoid.transformPositionToScaledSpace(
  461. position,
  462. scaledSpaceScratch
  463. );
  464. let magnitudeSquared = Cartesian3.magnitudeSquared(scaledSpacePosition);
  465. let magnitude = Math.sqrt(magnitudeSquared);
  466. const direction = Cartesian3.divideByScalar(
  467. scaledSpacePosition,
  468. magnitude,
  469. directionScratch
  470. );
  471. // For the purpose of this computation, points below the ellipsoid are consider to be on it instead.
  472. magnitudeSquared = Math.max(1.0, magnitudeSquared);
  473. magnitude = Math.max(1.0, magnitude);
  474. const cosAlpha = Cartesian3.dot(direction, scaledSpaceDirectionToPoint);
  475. const sinAlpha = Cartesian3.magnitude(
  476. Cartesian3.cross(direction, scaledSpaceDirectionToPoint, direction)
  477. );
  478. const cosBeta = 1.0 / magnitude;
  479. const sinBeta = Math.sqrt(magnitudeSquared - 1.0) * cosBeta;
  480. return 1.0 / (cosAlpha * cosBeta - sinAlpha * sinBeta);
  481. }
  482. function magnitudeToPoint(
  483. scaledSpaceDirectionToPoint,
  484. resultMagnitude,
  485. result
  486. ) {
  487. // The horizon culling point is undefined if there were no positions from which to compute it,
  488. // the directionToPoint is pointing opposite all of the positions, or if we computed NaN or infinity.
  489. if (
  490. resultMagnitude <= 0.0 ||
  491. resultMagnitude === 1.0 / 0.0 ||
  492. resultMagnitude !== resultMagnitude
  493. ) {
  494. return undefined;
  495. }
  496. return Cartesian3.multiplyByScalar(
  497. scaledSpaceDirectionToPoint,
  498. resultMagnitude,
  499. result
  500. );
  501. }
  502. const directionToPointScratch = new Cartesian3();
  503. function computeScaledSpaceDirectionToPoint(ellipsoid, directionToPoint) {
  504. if (Cartesian3.equals(directionToPoint, Cartesian3.ZERO)) {
  505. return directionToPoint;
  506. }
  507. ellipsoid.transformPositionToScaledSpace(
  508. directionToPoint,
  509. directionToPointScratch
  510. );
  511. return Cartesian3.normalize(directionToPointScratch, directionToPointScratch);
  512. }
  513. export default EllipsoidalOccluder;