EllipsoidalOccluder.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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 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. * var cameraPosition = new Cesium.Cartesian3(5.0, 6.0, 7.0);
  26. * var occluderEllipsoid = new Cesium.Ellipsoid(1.0, 1.1, 0.9);
  27. * var 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. 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. var ellipsoid = this._ellipsoid;
  67. var cv = ellipsoid.transformPositionToScaledSpace(cameraPosition, this._cameraPositionInScaledSpace);
  68. var vhMagnitudeSquared = Cartesian3.magnitudeSquared(cv) - 1.0;
  69. Cartesian3.clone(cameraPosition, this._cameraPosition);
  70. this._cameraPositionInScaledSpace = cv;
  71. this._distanceToLimbInScaledSpaceSquared = vhMagnitudeSquared;
  72. }
  73. }
  74. });
  75. var scratchCartesian = new Cartesian3();
  76. /**
  77. * Determines whether or not a point, the <code>occludee</code>, is hidden from view by the occluder.
  78. *
  79. * @param {Cartesian3} occludee The point to test for visibility.
  80. * @returns {Boolean} <code>true</code> if the occludee is visible; otherwise <code>false</code>.
  81. *
  82. * @example
  83. * var cameraPosition = new Cesium.Cartesian3(0, 0, 2.5);
  84. * var ellipsoid = new Cesium.Ellipsoid(1.0, 1.1, 0.9);
  85. * var occluder = new Cesium.EllipsoidalOccluder(ellipsoid, cameraPosition);
  86. * var point = new Cesium.Cartesian3(0, -3, -3);
  87. * occluder.isPointVisible(point); //returns true
  88. */
  89. EllipsoidalOccluder.prototype.isPointVisible = function(occludee) {
  90. var ellipsoid = this._ellipsoid;
  91. var occludeeScaledSpacePosition = ellipsoid.transformPositionToScaledSpace(occludee, scratchCartesian);
  92. return this.isScaledSpacePointVisible(occludeeScaledSpacePosition);
  93. };
  94. /**
  95. * Determines whether or not a point expressed in the ellipsoid scaled space, is hidden from view by the
  96. * occluder. To transform a Cartesian X, Y, Z position in the coordinate system aligned with the ellipsoid
  97. * into the scaled space, call {@link Ellipsoid#transformPositionToScaledSpace}.
  98. *
  99. * @param {Cartesian3} occludeeScaledSpacePosition The point to test for visibility, represented in the scaled space.
  100. * @returns {Boolean} <code>true</code> if the occludee is visible; otherwise <code>false</code>.
  101. *
  102. * @example
  103. * var cameraPosition = new Cesium.Cartesian3(0, 0, 2.5);
  104. * var ellipsoid = new Cesium.Ellipsoid(1.0, 1.1, 0.9);
  105. * var occluder = new Cesium.EllipsoidalOccluder(ellipsoid, cameraPosition);
  106. * var point = new Cesium.Cartesian3(0, -3, -3);
  107. * var scaledSpacePoint = ellipsoid.transformPositionToScaledSpace(point);
  108. * occluder.isScaledSpacePointVisible(scaledSpacePoint); //returns true
  109. */
  110. EllipsoidalOccluder.prototype.isScaledSpacePointVisible = function(occludeeScaledSpacePosition) {
  111. // See https://cesium.com/blog/2013/04/25/Horizon-culling/
  112. var cv = this._cameraPositionInScaledSpace;
  113. var vhMagnitudeSquared = this._distanceToLimbInScaledSpaceSquared;
  114. var vt = Cartesian3.subtract(occludeeScaledSpacePosition, cv, scratchCartesian);
  115. var vtDotVc = -Cartesian3.dot(vt, cv);
  116. // If vhMagnitudeSquared < 0 then we are below the surface of the ellipsoid and
  117. // in this case, set the culling plane to be on V.
  118. var isOccluded = vhMagnitudeSquared < 0 ? vtDotVc > 0 : (vtDotVc > vhMagnitudeSquared &&
  119. vtDotVc * vtDotVc / Cartesian3.magnitudeSquared(vt) > vhMagnitudeSquared);
  120. return !isOccluded;
  121. };
  122. /**
  123. * Computes a point that can be used for horizon culling from a list of positions. If the point is below
  124. * the horizon, all of the positions are guaranteed to be below the horizon as well. The returned point
  125. * is expressed in the ellipsoid-scaled space and is suitable for use with
  126. * {@link EllipsoidalOccluder#isScaledSpacePointVisible}.
  127. *
  128. * @param {Cartesian3} directionToPoint The direction that the computed point will lie along.
  129. * A reasonable direction to use is the direction from the center of the ellipsoid to
  130. * the center of the bounding sphere computed from the positions. The direction need not
  131. * be normalized.
  132. * @param {Cartesian3[]} positions The positions from which to compute the horizon culling point. The positions
  133. * must be expressed in a reference frame centered at the ellipsoid and aligned with the
  134. * ellipsoid's axes.
  135. * @param {Cartesian3} [result] The instance on which to store the result instead of allocating a new instance.
  136. * @returns {Cartesian3} The computed horizon culling point, expressed in the ellipsoid-scaled space.
  137. */
  138. EllipsoidalOccluder.prototype.computeHorizonCullingPoint = function(directionToPoint, positions, result) {
  139. //>>includeStart('debug', pragmas.debug);
  140. Check.typeOf.object('directionToPoint', directionToPoint);
  141. Check.defined('positions', positions);
  142. //>>includeEnd('debug');
  143. if (!defined(result)) {
  144. result = new Cartesian3();
  145. }
  146. var ellipsoid = this._ellipsoid;
  147. var scaledSpaceDirectionToPoint = computeScaledSpaceDirectionToPoint(ellipsoid, directionToPoint);
  148. var resultMagnitude = 0.0;
  149. for (var i = 0, len = positions.length; i < len; ++i) {
  150. var position = positions[i];
  151. var candidateMagnitude = computeMagnitude(ellipsoid, position, scaledSpaceDirectionToPoint);
  152. resultMagnitude = Math.max(resultMagnitude, candidateMagnitude);
  153. }
  154. return magnitudeToPoint(scaledSpaceDirectionToPoint, resultMagnitude, result);
  155. };
  156. var positionScratch = new Cartesian3();
  157. /**
  158. * Computes a point that can be used for horizon culling from a list of positions. If the point is below
  159. * the horizon, all of the positions are guaranteed to be below the horizon as well. The returned point
  160. * is expressed in the ellipsoid-scaled space and is suitable for use with
  161. * {@link EllipsoidalOccluder#isScaledSpacePointVisible}.
  162. *
  163. * @param {Cartesian3} directionToPoint The direction that the computed point will lie along.
  164. * A reasonable direction to use is the direction from the center of the ellipsoid to
  165. * the center of the bounding sphere computed from the positions. The direction need not
  166. * be normalized.
  167. * @param {Number[]} vertices The vertices from which to compute the horizon culling point. The positions
  168. * must be expressed in a reference frame centered at the ellipsoid and aligned with the
  169. * ellipsoid's axes.
  170. * @param {Number} [stride=3]
  171. * @param {Cartesian3} [center=Cartesian3.ZERO]
  172. * @param {Cartesian3} [result] The instance on which to store the result instead of allocating a new instance.
  173. * @returns {Cartesian3} The computed horizon culling point, expressed in the ellipsoid-scaled space.
  174. */
  175. EllipsoidalOccluder.prototype.computeHorizonCullingPointFromVertices = function(directionToPoint, vertices, stride, center, result) {
  176. //>>includeStart('debug', pragmas.debug);
  177. Check.typeOf.object('directionToPoint', directionToPoint);
  178. Check.defined('vertices', vertices);
  179. Check.typeOf.number('stride', stride);
  180. //>>includeEnd('debug');
  181. if (!defined(result)) {
  182. result = new Cartesian3();
  183. }
  184. center = defaultValue(center, Cartesian3.ZERO);
  185. var ellipsoid = this._ellipsoid;
  186. var scaledSpaceDirectionToPoint = computeScaledSpaceDirectionToPoint(ellipsoid, directionToPoint);
  187. var resultMagnitude = 0.0;
  188. for (var i = 0, len = vertices.length; i < len; i += stride) {
  189. positionScratch.x = vertices[i] + center.x;
  190. positionScratch.y = vertices[i + 1] + center.y;
  191. positionScratch.z = vertices[i + 2] + center.z;
  192. var candidateMagnitude = computeMagnitude(ellipsoid, positionScratch, scaledSpaceDirectionToPoint);
  193. resultMagnitude = Math.max(resultMagnitude, candidateMagnitude);
  194. }
  195. return magnitudeToPoint(scaledSpaceDirectionToPoint, resultMagnitude, result);
  196. };
  197. var subsampleScratch = [];
  198. /**
  199. * Computes a point that can be used for horizon culling of a rectangle. If the point is below
  200. * the horizon, the ellipsoid-conforming rectangle is guaranteed to be below the horizon as well.
  201. * The returned point is expressed in the ellipsoid-scaled space and is suitable for use with
  202. * {@link EllipsoidalOccluder#isScaledSpacePointVisible}.
  203. *
  204. * @param {Rectangle} rectangle The rectangle for which to compute the horizon culling point.
  205. * @param {Ellipsoid} ellipsoid The ellipsoid on which the rectangle is defined. This may be different from
  206. * the ellipsoid used by this instance for occlusion testing.
  207. * @param {Cartesian3} [result] The instance on which to store the result instead of allocating a new instance.
  208. * @returns {Cartesian3} The computed horizon culling point, expressed in the ellipsoid-scaled space.
  209. */
  210. EllipsoidalOccluder.prototype.computeHorizonCullingPointFromRectangle = function(rectangle, ellipsoid, result) {
  211. //>>includeStart('debug', pragmas.debug);
  212. Check.typeOf.object('rectangle', rectangle);
  213. //>>includeEnd('debug');
  214. var positions = Rectangle.subsample(rectangle, ellipsoid, 0.0, subsampleScratch);
  215. var bs = BoundingSphere.fromPoints(positions);
  216. // If the bounding sphere center is too close to the center of the occluder, it doesn't make
  217. // sense to try to horizon cull it.
  218. if (Cartesian3.magnitude(bs.center) < 0.1 * ellipsoid.minimumRadius) {
  219. return undefined;
  220. }
  221. return this.computeHorizonCullingPoint(bs.center, positions, result);
  222. };
  223. var scaledSpaceScratch = new Cartesian3();
  224. var directionScratch = new Cartesian3();
  225. function computeMagnitude(ellipsoid, position, scaledSpaceDirectionToPoint) {
  226. var scaledSpacePosition = ellipsoid.transformPositionToScaledSpace(position, scaledSpaceScratch);
  227. var magnitudeSquared = Cartesian3.magnitudeSquared(scaledSpacePosition);
  228. var magnitude = Math.sqrt(magnitudeSquared);
  229. var direction = Cartesian3.divideByScalar(scaledSpacePosition, magnitude, directionScratch);
  230. // For the purpose of this computation, points below the ellipsoid are consider to be on it instead.
  231. magnitudeSquared = Math.max(1.0, magnitudeSquared);
  232. magnitude = Math.max(1.0, magnitude);
  233. var cosAlpha = Cartesian3.dot(direction, scaledSpaceDirectionToPoint);
  234. var sinAlpha = Cartesian3.magnitude(Cartesian3.cross(direction, scaledSpaceDirectionToPoint, direction));
  235. var cosBeta = 1.0 / magnitude;
  236. var sinBeta = Math.sqrt(magnitudeSquared - 1.0) * cosBeta;
  237. return 1.0 / (cosAlpha * cosBeta - sinAlpha * sinBeta);
  238. }
  239. function magnitudeToPoint(scaledSpaceDirectionToPoint, resultMagnitude, result) {
  240. // The horizon culling point is undefined if there were no positions from which to compute it,
  241. // the directionToPoint is pointing opposite all of the positions, or if we computed NaN or infinity.
  242. if (resultMagnitude <= 0.0 || resultMagnitude === 1.0 / 0.0 || resultMagnitude !== resultMagnitude) {
  243. return undefined;
  244. }
  245. return Cartesian3.multiplyByScalar(scaledSpaceDirectionToPoint, resultMagnitude, result);
  246. }
  247. var directionToPointScratch = new Cartesian3();
  248. function computeScaledSpaceDirectionToPoint(ellipsoid, directionToPoint) {
  249. if (Cartesian3.equals(directionToPoint, Cartesian3.ZERO)) {
  250. return directionToPoint;
  251. }
  252. ellipsoid.transformPositionToScaledSpace(directionToPoint, directionToPointScratch);
  253. return Cartesian3.normalize(directionToPointScratch, directionToPointScratch);
  254. }
  255. export default EllipsoidalOccluder;