OrthographicFrustum.js 12 KB

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