EllipsoidalOccluder-5541419b.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /* This file is automatically rebuilt by the Cesium build process. */
  2. define(['exports', './defined-26bd4a03', './Check-da037458', './defaultValue-f2e68450', './Cartesian2-2a723276', './defineProperties-6f7a50f2', './Transforms-65aba0a4'], function (exports, defined, Check, defaultValue, Cartesian2, defineProperties, Transforms) { 'use strict';
  3. /**
  4. * Determine whether or not other objects are visible or hidden behind the visible horizon defined by
  5. * an {@link Ellipsoid} and a camera position. The ellipsoid is assumed to be located at the
  6. * origin of the coordinate system. This class uses the algorithm described in the
  7. * {@link https://cesium.com/blog/2013/04/25/Horizon-culling/|Horizon Culling} blog post.
  8. *
  9. * @alias EllipsoidalOccluder
  10. *
  11. * @param {Ellipsoid} ellipsoid The ellipsoid to use as an occluder.
  12. * @param {Cartesian3} [cameraPosition] The coordinate of the viewer/camera. If this parameter is not
  13. * specified, {@link EllipsoidalOccluder#cameraPosition} must be called before
  14. * testing visibility.
  15. *
  16. * @constructor
  17. *
  18. * @example
  19. * // Construct an ellipsoidal occluder with radii 1.0, 1.1, and 0.9.
  20. * var cameraPosition = new Cesium.Cartesian3(5.0, 6.0, 7.0);
  21. * var occluderEllipsoid = new Cesium.Ellipsoid(1.0, 1.1, 0.9);
  22. * var occluder = new Cesium.EllipsoidalOccluder(occluderEllipsoid, cameraPosition);
  23. *
  24. * @private
  25. */
  26. function EllipsoidalOccluder(ellipsoid, cameraPosition) {
  27. //>>includeStart('debug', pragmas.debug);
  28. Check.Check.typeOf.object('ellipsoid', ellipsoid);
  29. //>>includeEnd('debug');
  30. this._ellipsoid = ellipsoid;
  31. this._cameraPosition = new Cartesian2.Cartesian3();
  32. this._cameraPositionInScaledSpace = new Cartesian2.Cartesian3();
  33. this._distanceToLimbInScaledSpaceSquared = 0.0;
  34. // cameraPosition fills in the above values
  35. if (defined.defined(cameraPosition)) {
  36. this.cameraPosition = cameraPosition;
  37. }
  38. }
  39. defineProperties.defineProperties(EllipsoidalOccluder.prototype, {
  40. /**
  41. * Gets the occluding ellipsoid.
  42. * @memberof EllipsoidalOccluder.prototype
  43. * @type {Ellipsoid}
  44. */
  45. ellipsoid : {
  46. get: function() {
  47. return this._ellipsoid;
  48. }
  49. },
  50. /**
  51. * Gets or sets the position of the camera.
  52. * @memberof EllipsoidalOccluder.prototype
  53. * @type {Cartesian3}
  54. */
  55. cameraPosition : {
  56. get : function() {
  57. return this._cameraPosition;
  58. },
  59. set : function(cameraPosition) {
  60. // See https://cesium.com/blog/2013/04/25/Horizon-culling/
  61. var ellipsoid = this._ellipsoid;
  62. var cv = ellipsoid.transformPositionToScaledSpace(cameraPosition, this._cameraPositionInScaledSpace);
  63. var vhMagnitudeSquared = Cartesian2.Cartesian3.magnitudeSquared(cv) - 1.0;
  64. Cartesian2.Cartesian3.clone(cameraPosition, this._cameraPosition);
  65. this._cameraPositionInScaledSpace = cv;
  66. this._distanceToLimbInScaledSpaceSquared = vhMagnitudeSquared;
  67. }
  68. }
  69. });
  70. var scratchCartesian = new Cartesian2.Cartesian3();
  71. /**
  72. * Determines whether or not a point, the <code>occludee</code>, is hidden from view by the occluder.
  73. *
  74. * @param {Cartesian3} occludee The point to test for visibility.
  75. * @returns {Boolean} <code>true</code> if the occludee is visible; otherwise <code>false</code>.
  76. *
  77. * @example
  78. * var cameraPosition = new Cesium.Cartesian3(0, 0, 2.5);
  79. * var ellipsoid = new Cesium.Ellipsoid(1.0, 1.1, 0.9);
  80. * var occluder = new Cesium.EllipsoidalOccluder(ellipsoid, cameraPosition);
  81. * var point = new Cesium.Cartesian3(0, -3, -3);
  82. * occluder.isPointVisible(point); //returns true
  83. */
  84. EllipsoidalOccluder.prototype.isPointVisible = function(occludee) {
  85. var ellipsoid = this._ellipsoid;
  86. var occludeeScaledSpacePosition = ellipsoid.transformPositionToScaledSpace(occludee, scratchCartesian);
  87. return this.isScaledSpacePointVisible(occludeeScaledSpacePosition);
  88. };
  89. /**
  90. * Determines whether or not a point expressed in the ellipsoid scaled space, is hidden from view by the
  91. * occluder. To transform a Cartesian X, Y, Z position in the coordinate system aligned with the ellipsoid
  92. * into the scaled space, call {@link Ellipsoid#transformPositionToScaledSpace}.
  93. *
  94. * @param {Cartesian3} occludeeScaledSpacePosition The point to test for visibility, represented in the scaled space.
  95. * @returns {Boolean} <code>true</code> if the occludee is visible; otherwise <code>false</code>.
  96. *
  97. * @example
  98. * var cameraPosition = new Cesium.Cartesian3(0, 0, 2.5);
  99. * var ellipsoid = new Cesium.Ellipsoid(1.0, 1.1, 0.9);
  100. * var occluder = new Cesium.EllipsoidalOccluder(ellipsoid, cameraPosition);
  101. * var point = new Cesium.Cartesian3(0, -3, -3);
  102. * var scaledSpacePoint = ellipsoid.transformPositionToScaledSpace(point);
  103. * occluder.isScaledSpacePointVisible(scaledSpacePoint); //returns true
  104. */
  105. EllipsoidalOccluder.prototype.isScaledSpacePointVisible = function(occludeeScaledSpacePosition) {
  106. // See https://cesium.com/blog/2013/04/25/Horizon-culling/
  107. var cv = this._cameraPositionInScaledSpace;
  108. var vhMagnitudeSquared = this._distanceToLimbInScaledSpaceSquared;
  109. var vt = Cartesian2.Cartesian3.subtract(occludeeScaledSpacePosition, cv, scratchCartesian);
  110. var vtDotVc = -Cartesian2.Cartesian3.dot(vt, cv);
  111. // If vhMagnitudeSquared < 0 then we are below the surface of the ellipsoid and
  112. // in this case, set the culling plane to be on V.
  113. var isOccluded = vhMagnitudeSquared < 0 ? vtDotVc > 0 : (vtDotVc > vhMagnitudeSquared &&
  114. vtDotVc * vtDotVc / Cartesian2.Cartesian3.magnitudeSquared(vt) > vhMagnitudeSquared);
  115. return !isOccluded;
  116. };
  117. /**
  118. * Computes a point that can be used for horizon culling from a list of positions. If the point is below
  119. * the horizon, all of the positions are guaranteed to be below the horizon as well. The returned point
  120. * is expressed in the ellipsoid-scaled space and is suitable for use with
  121. * {@link EllipsoidalOccluder#isScaledSpacePointVisible}.
  122. *
  123. * @param {Cartesian3} directionToPoint The direction that the computed point will lie along.
  124. * A reasonable direction to use is the direction from the center of the ellipsoid to
  125. * the center of the bounding sphere computed from the positions. The direction need not
  126. * be normalized.
  127. * @param {Cartesian3[]} positions The positions from which to compute the horizon culling point. The positions
  128. * must be expressed in a reference frame centered at the ellipsoid and aligned with the
  129. * ellipsoid's axes.
  130. * @param {Cartesian3} [result] The instance on which to store the result instead of allocating a new instance.
  131. * @returns {Cartesian3} The computed horizon culling point, expressed in the ellipsoid-scaled space.
  132. */
  133. EllipsoidalOccluder.prototype.computeHorizonCullingPoint = function(directionToPoint, positions, result) {
  134. //>>includeStart('debug', pragmas.debug);
  135. Check.Check.typeOf.object('directionToPoint', directionToPoint);
  136. Check.Check.defined('positions', positions);
  137. //>>includeEnd('debug');
  138. if (!defined.defined(result)) {
  139. result = new Cartesian2.Cartesian3();
  140. }
  141. var ellipsoid = this._ellipsoid;
  142. var scaledSpaceDirectionToPoint = computeScaledSpaceDirectionToPoint(ellipsoid, directionToPoint);
  143. var resultMagnitude = 0.0;
  144. for (var i = 0, len = positions.length; i < len; ++i) {
  145. var position = positions[i];
  146. var candidateMagnitude = computeMagnitude(ellipsoid, position, scaledSpaceDirectionToPoint);
  147. resultMagnitude = Math.max(resultMagnitude, candidateMagnitude);
  148. }
  149. return magnitudeToPoint(scaledSpaceDirectionToPoint, resultMagnitude, result);
  150. };
  151. var positionScratch = new Cartesian2.Cartesian3();
  152. /**
  153. * Computes a point that can be used for horizon culling from a list of positions. If the point is below
  154. * the horizon, all of the positions are guaranteed to be below the horizon as well. The returned point
  155. * is expressed in the ellipsoid-scaled space and is suitable for use with
  156. * {@link EllipsoidalOccluder#isScaledSpacePointVisible}.
  157. *
  158. * @param {Cartesian3} directionToPoint The direction that the computed point will lie along.
  159. * A reasonable direction to use is the direction from the center of the ellipsoid to
  160. * the center of the bounding sphere computed from the positions. The direction need not
  161. * be normalized.
  162. * @param {Number[]} vertices The vertices from which to compute the horizon culling point. The positions
  163. * must be expressed in a reference frame centered at the ellipsoid and aligned with the
  164. * ellipsoid's axes.
  165. * @param {Number} [stride=3]
  166. * @param {Cartesian3} [center=Cartesian3.ZERO]
  167. * @param {Cartesian3} [result] The instance on which to store the result instead of allocating a new instance.
  168. * @returns {Cartesian3} The computed horizon culling point, expressed in the ellipsoid-scaled space.
  169. */
  170. EllipsoidalOccluder.prototype.computeHorizonCullingPointFromVertices = function(directionToPoint, vertices, stride, center, result) {
  171. //>>includeStart('debug', pragmas.debug);
  172. Check.Check.typeOf.object('directionToPoint', directionToPoint);
  173. Check.Check.defined('vertices', vertices);
  174. Check.Check.typeOf.number('stride', stride);
  175. //>>includeEnd('debug');
  176. if (!defined.defined(result)) {
  177. result = new Cartesian2.Cartesian3();
  178. }
  179. center = defaultValue.defaultValue(center, Cartesian2.Cartesian3.ZERO);
  180. var ellipsoid = this._ellipsoid;
  181. var scaledSpaceDirectionToPoint = computeScaledSpaceDirectionToPoint(ellipsoid, directionToPoint);
  182. var resultMagnitude = 0.0;
  183. for (var i = 0, len = vertices.length; i < len; i += stride) {
  184. positionScratch.x = vertices[i] + center.x;
  185. positionScratch.y = vertices[i + 1] + center.y;
  186. positionScratch.z = vertices[i + 2] + center.z;
  187. var candidateMagnitude = computeMagnitude(ellipsoid, positionScratch, scaledSpaceDirectionToPoint);
  188. resultMagnitude = Math.max(resultMagnitude, candidateMagnitude);
  189. }
  190. return magnitudeToPoint(scaledSpaceDirectionToPoint, resultMagnitude, result);
  191. };
  192. var subsampleScratch = [];
  193. /**
  194. * Computes a point that can be used for horizon culling of a rectangle. If the point is below
  195. * the horizon, the ellipsoid-conforming rectangle is guaranteed to be below the horizon as well.
  196. * The returned point is expressed in the ellipsoid-scaled space and is suitable for use with
  197. * {@link EllipsoidalOccluder#isScaledSpacePointVisible}.
  198. *
  199. * @param {Rectangle} rectangle The rectangle for which to compute the horizon culling point.
  200. * @param {Ellipsoid} ellipsoid The ellipsoid on which the rectangle is defined. This may be different from
  201. * the ellipsoid used by this instance for occlusion testing.
  202. * @param {Cartesian3} [result] The instance on which to store the result instead of allocating a new instance.
  203. * @returns {Cartesian3} The computed horizon culling point, expressed in the ellipsoid-scaled space.
  204. */
  205. EllipsoidalOccluder.prototype.computeHorizonCullingPointFromRectangle = function(rectangle, ellipsoid, result) {
  206. //>>includeStart('debug', pragmas.debug);
  207. Check.Check.typeOf.object('rectangle', rectangle);
  208. //>>includeEnd('debug');
  209. var positions = Cartesian2.Rectangle.subsample(rectangle, ellipsoid, 0.0, subsampleScratch);
  210. var bs = Transforms.BoundingSphere.fromPoints(positions);
  211. // If the bounding sphere center is too close to the center of the occluder, it doesn't make
  212. // sense to try to horizon cull it.
  213. if (Cartesian2.Cartesian3.magnitude(bs.center) < 0.1 * ellipsoid.minimumRadius) {
  214. return undefined;
  215. }
  216. return this.computeHorizonCullingPoint(bs.center, positions, result);
  217. };
  218. var scaledSpaceScratch = new Cartesian2.Cartesian3();
  219. var directionScratch = new Cartesian2.Cartesian3();
  220. function computeMagnitude(ellipsoid, position, scaledSpaceDirectionToPoint) {
  221. var scaledSpacePosition = ellipsoid.transformPositionToScaledSpace(position, scaledSpaceScratch);
  222. var magnitudeSquared = Cartesian2.Cartesian3.magnitudeSquared(scaledSpacePosition);
  223. var magnitude = Math.sqrt(magnitudeSquared);
  224. var direction = Cartesian2.Cartesian3.divideByScalar(scaledSpacePosition, magnitude, directionScratch);
  225. // For the purpose of this computation, points below the ellipsoid are consider to be on it instead.
  226. magnitudeSquared = Math.max(1.0, magnitudeSquared);
  227. magnitude = Math.max(1.0, magnitude);
  228. var cosAlpha = Cartesian2.Cartesian3.dot(direction, scaledSpaceDirectionToPoint);
  229. var sinAlpha = Cartesian2.Cartesian3.magnitude(Cartesian2.Cartesian3.cross(direction, scaledSpaceDirectionToPoint, direction));
  230. var cosBeta = 1.0 / magnitude;
  231. var sinBeta = Math.sqrt(magnitudeSquared - 1.0) * cosBeta;
  232. return 1.0 / (cosAlpha * cosBeta - sinAlpha * sinBeta);
  233. }
  234. function magnitudeToPoint(scaledSpaceDirectionToPoint, resultMagnitude, result) {
  235. // The horizon culling point is undefined if there were no positions from which to compute it,
  236. // the directionToPoint is pointing opposite all of the positions, or if we computed NaN or infinity.
  237. if (resultMagnitude <= 0.0 || resultMagnitude === 1.0 / 0.0 || resultMagnitude !== resultMagnitude) {
  238. return undefined;
  239. }
  240. return Cartesian2.Cartesian3.multiplyByScalar(scaledSpaceDirectionToPoint, resultMagnitude, result);
  241. }
  242. var directionToPointScratch = new Cartesian2.Cartesian3();
  243. function computeScaledSpaceDirectionToPoint(ellipsoid, directionToPoint) {
  244. if (Cartesian2.Cartesian3.equals(directionToPoint, Cartesian2.Cartesian3.ZERO)) {
  245. return directionToPoint;
  246. }
  247. ellipsoid.transformPositionToScaledSpace(directionToPoint, directionToPointScratch);
  248. return Cartesian2.Cartesian3.normalize(directionToPointScratch, directionToPointScratch);
  249. }
  250. exports.EllipsoidalOccluder = EllipsoidalOccluder;
  251. });