You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

EllipsoidalOccluder.js 22KB

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