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.

OrthographicFrustum.js 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. import Check from './Check.js';
  2. import defaultValue from './defaultValue.js';
  3. import defined from './defined.js';
  4. import defineProperties from './defineProperties.js';
  5. import DeveloperError from './DeveloperError.js';
  6. import CesiumMath from './Math.js';
  7. import OrthographicOffCenterFrustum from './OrthographicOffCenterFrustum.js';
  8. /**
  9. * The viewing frustum is defined by 6 planes.
  10. * Each plane is represented by a {@link Cartesian4} object, where the x, y, and z components
  11. * define the unit vector normal to the plane, and the w component is the distance of the
  12. * plane from the origin/camera position.
  13. *
  14. * @alias OrthographicFrustum
  15. * @constructor
  16. *
  17. * @param {Object} [options] An object with the following properties:
  18. * @param {Number} [options.width] The width of the frustum in meters.
  19. * @param {Number} [options.aspectRatio] The aspect ratio of the frustum's width to it's height.
  20. * @param {Number} [options.near=1.0] The distance of the near plane.
  21. * @param {Number} [options.far=500000000.0] The distance of the far plane.
  22. *
  23. * @example
  24. * var maxRadii = ellipsoid.maximumRadius;
  25. *
  26. * var frustum = new Cesium.OrthographicFrustum();
  27. * frustum.near = 0.01 * maxRadii;
  28. * frustum.far = 50.0 * maxRadii;
  29. */
  30. function OrthographicFrustum(options) {
  31. options = defaultValue(options, defaultValue.EMPTY_OBJECT);
  32. this._offCenterFrustum = new OrthographicOffCenterFrustum();
  33. /**
  34. * The horizontal width of the frustum in meters.
  35. * @type {Number}
  36. * @default undefined
  37. */
  38. this.width = options.width;
  39. this._width = undefined;
  40. /**
  41. * The aspect ratio of the frustum's width to it's height.
  42. * @type {Number}
  43. * @default undefined
  44. */
  45. this.aspectRatio = options.aspectRatio;
  46. this._aspectRatio = undefined;
  47. /**
  48. * The distance of the near plane.
  49. * @type {Number}
  50. * @default 1.0
  51. */
  52. this.near = defaultValue(options.near, 1.0);
  53. this._near = this.near;
  54. /**
  55. * The distance of the far plane.
  56. * @type {Number}
  57. * @default 500000000.0;
  58. */
  59. this.far = defaultValue(options.far, 500000000.0);
  60. this._far = this.far;
  61. }
  62. /**
  63. * The number of elements used to pack the object into an array.
  64. * @type {Number}
  65. */
  66. OrthographicFrustum.packedLength = 4;
  67. /**
  68. * Stores the provided instance into the provided array.
  69. *
  70. * @param {OrthographicFrustum} value The value to pack.
  71. * @param {Number[]} array The array to pack into.
  72. * @param {Number} [startingIndex=0] The index into the array at which to start packing the elements.
  73. *
  74. * @returns {Number[]} The array that was packed into
  75. */
  76. OrthographicFrustum.pack = function(value, array, startingIndex) {
  77. //>>includeStart('debug', pragmas.debug);
  78. Check.typeOf.object('value', value);
  79. Check.defined('array', array);
  80. //>>includeEnd('debug');
  81. startingIndex = defaultValue(startingIndex, 0);
  82. array[startingIndex++] = value.width;
  83. array[startingIndex++] = value.aspectRatio;
  84. array[startingIndex++] = value.near;
  85. array[startingIndex] = value.far;
  86. return array;
  87. };
  88. /**
  89. * Retrieves an instance from a packed array.
  90. *
  91. * @param {Number[]} array The packed array.
  92. * @param {Number} [startingIndex=0] The starting index of the element to be unpacked.
  93. * @param {OrthographicFrustum} [result] The object into which to store the result.
  94. * @returns {OrthographicFrustum} The modified result parameter or a new OrthographicFrustum instance if one was not provided.
  95. */
  96. OrthographicFrustum.unpack = function(array, startingIndex, result) {
  97. //>>includeStart('debug', pragmas.debug);
  98. Check.defined('array', array);
  99. //>>includeEnd('debug');
  100. startingIndex = defaultValue(startingIndex, 0);
  101. if (!defined(result)) {
  102. result = new OrthographicFrustum();
  103. }
  104. result.width = array[startingIndex++];
  105. result.aspectRatio = array[startingIndex++];
  106. result.near = array[startingIndex++];
  107. result.far = array[startingIndex];
  108. return result;
  109. };
  110. function update(frustum) {
  111. //>>includeStart('debug', pragmas.debug);
  112. if (!defined(frustum.width) || !defined(frustum.aspectRatio) || !defined(frustum.near) || !defined(frustum.far)) {
  113. throw new DeveloperError('width, aspectRatio, near, or far parameters are not set.');
  114. }
  115. //>>includeEnd('debug');
  116. var f = frustum._offCenterFrustum;
  117. if (frustum.width !== frustum._width || frustum.aspectRatio !== frustum._aspectRatio ||
  118. frustum.near !== frustum._near || frustum.far !== frustum._far) {
  119. //>>includeStart('debug', pragmas.debug);
  120. if (frustum.aspectRatio < 0) {
  121. throw new DeveloperError('aspectRatio must be positive.');
  122. }
  123. if (frustum.near < 0 || frustum.near > frustum.far) {
  124. throw new DeveloperError('near must be greater than zero and less than far.');
  125. }
  126. //>>includeEnd('debug');
  127. frustum._aspectRatio = frustum.aspectRatio;
  128. frustum._width = frustum.width;
  129. frustum._near = frustum.near;
  130. frustum._far = frustum.far;
  131. var ratio = 1.0 / frustum.aspectRatio;
  132. f.right = frustum.width * 0.5;
  133. f.left = -f.right;
  134. f.top = ratio * f.right;
  135. f.bottom = -f.top;
  136. f.near = frustum.near;
  137. f.far = frustum.far;
  138. }
  139. }
  140. defineProperties(OrthographicFrustum.prototype, {
  141. /**
  142. * Gets the orthographic projection matrix computed from the view frustum.
  143. * @memberof OrthographicFrustum.prototype
  144. * @type {Matrix4}
  145. * @readonly
  146. */
  147. projectionMatrix : {
  148. get : function() {
  149. update(this);
  150. return this._offCenterFrustum.projectionMatrix;
  151. }
  152. }
  153. });
  154. /**
  155. * Creates a culling volume for this frustum.
  156. *
  157. * @param {Cartesian3} position The eye position.
  158. * @param {Cartesian3} direction The view direction.
  159. * @param {Cartesian3} up The up direction.
  160. * @returns {CullingVolume} A culling volume at the given position and orientation.
  161. *
  162. * @example
  163. * // Check if a bounding volume intersects the frustum.
  164. * var cullingVolume = frustum.computeCullingVolume(cameraPosition, cameraDirection, cameraUp);
  165. * var intersect = cullingVolume.computeVisibility(boundingVolume);
  166. */
  167. OrthographicFrustum.prototype.computeCullingVolume = function(position, direction, up) {
  168. update(this);
  169. return this._offCenterFrustum.computeCullingVolume(position, direction, up);
  170. };
  171. /**
  172. * Returns the pixel's width and height in meters.
  173. *
  174. * @param {Number} drawingBufferWidth The width of the drawing buffer.
  175. * @param {Number} drawingBufferHeight The height of the drawing buffer.
  176. * @param {Number} distance The distance to the near plane in meters.
  177. * @param {Number} pixelRatio The scaling factor from pixel space to coordinate space.
  178. * @param {Cartesian2} result The object onto which to store the result.
  179. * @returns {Cartesian2} The modified result parameter or a new instance of {@link Cartesian2} with the pixel's width and height in the x and y properties, respectively.
  180. *
  181. * @exception {DeveloperError} drawingBufferWidth must be greater than zero.
  182. * @exception {DeveloperError} drawingBufferHeight must be greater than zero.
  183. * @exception {DeveloperError} pixelRatio must be greater than zero.
  184. *
  185. * @example
  186. * // Example 1
  187. * // Get the width and height of a pixel.
  188. * var pixelSize = camera.frustum.getPixelDimensions(scene.drawingBufferWidth, scene.drawingBufferHeight, 0.0, scene.pixelRatio, new Cesium.Cartesian2());
  189. */
  190. OrthographicFrustum.prototype.getPixelDimensions = function(drawingBufferWidth, drawingBufferHeight, distance, pixelRatio, result) {
  191. update(this);
  192. return this._offCenterFrustum.getPixelDimensions(drawingBufferWidth, drawingBufferHeight, distance, pixelRatio, result);
  193. };
  194. /**
  195. * Returns a duplicate of a OrthographicFrustum instance.
  196. *
  197. * @param {OrthographicFrustum} [result] The object onto which to store the result.
  198. * @returns {OrthographicFrustum} The modified result parameter or a new OrthographicFrustum instance if one was not provided.
  199. */
  200. OrthographicFrustum.prototype.clone = function(result) {
  201. if (!defined(result)) {
  202. result = new OrthographicFrustum();
  203. }
  204. result.aspectRatio = this.aspectRatio;
  205. result.width = this.width;
  206. result.near = this.near;
  207. result.far = this.far;
  208. // force update of clone to compute matrices
  209. result._aspectRatio = undefined;
  210. result._width = undefined;
  211. result._near = undefined;
  212. result._far = undefined;
  213. this._offCenterFrustum.clone(result._offCenterFrustum);
  214. return result;
  215. };
  216. /**
  217. * Compares the provided OrthographicFrustum componentwise and returns
  218. * <code>true</code> if they are equal, <code>false</code> otherwise.
  219. *
  220. * @param {OrthographicFrustum} [other] The right hand side OrthographicFrustum.
  221. * @returns {Boolean} <code>true</code> if they are equal, <code>false</code> otherwise.
  222. */
  223. OrthographicFrustum.prototype.equals = function(other) {
  224. if (!defined(other) || !(other instanceof OrthographicFrustum)) {
  225. return false;
  226. }
  227. update(this);
  228. update(other);
  229. return (this.width === other.width &&
  230. this.aspectRatio === other.aspectRatio &&
  231. this._offCenterFrustum.equals(other._offCenterFrustum));
  232. };
  233. /**
  234. * Compares the provided OrthographicFrustum componentwise and returns
  235. * <code>true</code> if they pass an absolute or relative tolerance test,
  236. * <code>false</code> otherwise.
  237. *
  238. * @param {OrthographicFrustum} other The right hand side OrthographicFrustum.
  239. * @param {Number} relativeEpsilon The relative epsilon tolerance to use for equality testing.
  240. * @param {Number} [absoluteEpsilon=relativeEpsilon] The absolute epsilon tolerance to use for equality testing.
  241. * @returns {Boolean} <code>true</code> if this and other are within the provided epsilon, <code>false</code> otherwise.
  242. */
  243. OrthographicFrustum.prototype.equalsEpsilon = function(other, relativeEpsilon, absoluteEpsilon) {
  244. if (!defined(other) || !(other instanceof OrthographicFrustum)) {
  245. return false;
  246. }
  247. update(this);
  248. update(other);
  249. return (CesiumMath.equalsEpsilon(this.width, other.width, relativeEpsilon, absoluteEpsilon) &&
  250. CesiumMath.equalsEpsilon(this.aspectRatio, other.aspectRatio, relativeEpsilon, absoluteEpsilon) &&
  251. this._offCenterFrustum.equalsEpsilon(other._offCenterFrustum, relativeEpsilon, absoluteEpsilon));
  252. };
  253. export default OrthographicFrustum;