OrthographicFrustum.js 10 KB

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