OrthographicFrustum.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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. * Gets the orthographic projection matrix computed from the view frustum.
  167. * @memberof OrthographicFrustum.prototype
  168. * @type {OrthographicOffCenterFrustum}
  169. * @readonly
  170. * @private
  171. */
  172. offCenterFrustum: {
  173. get: function () {
  174. update(this);
  175. return this._offCenterFrustum;
  176. },
  177. },
  178. });
  179. /**
  180. * Creates a culling volume for this frustum.
  181. *
  182. * @param {Cartesian3} position The eye position.
  183. * @param {Cartesian3} direction The view direction.
  184. * @param {Cartesian3} up The up direction.
  185. * @returns {CullingVolume} A culling volume at the given position and orientation.
  186. *
  187. * @example
  188. * // Check if a bounding volume intersects the frustum.
  189. * const cullingVolume = frustum.computeCullingVolume(cameraPosition, cameraDirection, cameraUp);
  190. * const intersect = cullingVolume.computeVisibility(boundingVolume);
  191. */
  192. OrthographicFrustum.prototype.computeCullingVolume = function (
  193. position,
  194. direction,
  195. up
  196. ) {
  197. update(this);
  198. return this._offCenterFrustum.computeCullingVolume(position, direction, up);
  199. };
  200. /**
  201. * Returns the pixel's width and height in meters.
  202. *
  203. * @param {number} drawingBufferWidth The width of the drawing buffer.
  204. * @param {number} drawingBufferHeight The height of the drawing buffer.
  205. * @param {number} distance The distance to the near plane in meters.
  206. * @param {number} pixelRatio The scaling factor from pixel space to coordinate space.
  207. * @param {Cartesian2} result The object onto which to store the result.
  208. * @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.
  209. *
  210. * @exception {DeveloperError} drawingBufferWidth must be greater than zero.
  211. * @exception {DeveloperError} drawingBufferHeight must be greater than zero.
  212. * @exception {DeveloperError} pixelRatio must be greater than zero.
  213. *
  214. * @example
  215. * // Example 1
  216. * // Get the width and height of a pixel.
  217. * const pixelSize = camera.frustum.getPixelDimensions(scene.drawingBufferWidth, scene.drawingBufferHeight, 0.0, scene.pixelRatio, new Cesium.Cartesian2());
  218. */
  219. OrthographicFrustum.prototype.getPixelDimensions = function (
  220. drawingBufferWidth,
  221. drawingBufferHeight,
  222. distance,
  223. pixelRatio,
  224. result
  225. ) {
  226. update(this);
  227. return this._offCenterFrustum.getPixelDimensions(
  228. drawingBufferWidth,
  229. drawingBufferHeight,
  230. distance,
  231. pixelRatio,
  232. result
  233. );
  234. };
  235. /**
  236. * Returns a duplicate of a OrthographicFrustum instance.
  237. *
  238. * @param {OrthographicFrustum} [result] The object onto which to store the result.
  239. * @returns {OrthographicFrustum} The modified result parameter or a new OrthographicFrustum instance if one was not provided.
  240. */
  241. OrthographicFrustum.prototype.clone = function (result) {
  242. if (!defined(result)) {
  243. result = new OrthographicFrustum();
  244. }
  245. result.aspectRatio = this.aspectRatio;
  246. result.width = this.width;
  247. result.near = this.near;
  248. result.far = this.far;
  249. // force update of clone to compute matrices
  250. result._aspectRatio = undefined;
  251. result._width = undefined;
  252. result._near = undefined;
  253. result._far = undefined;
  254. this._offCenterFrustum.clone(result._offCenterFrustum);
  255. return result;
  256. };
  257. /**
  258. * Compares the provided OrthographicFrustum componentwise and returns
  259. * <code>true</code> if they are equal, <code>false</code> otherwise.
  260. *
  261. * @param {OrthographicFrustum} [other] The right hand side OrthographicFrustum.
  262. * @returns {boolean} <code>true</code> if they are equal, <code>false</code> otherwise.
  263. */
  264. OrthographicFrustum.prototype.equals = function (other) {
  265. if (!defined(other) || !(other instanceof OrthographicFrustum)) {
  266. return false;
  267. }
  268. update(this);
  269. update(other);
  270. return (
  271. this.width === other.width &&
  272. this.aspectRatio === other.aspectRatio &&
  273. this._offCenterFrustum.equals(other._offCenterFrustum)
  274. );
  275. };
  276. /**
  277. * Compares the provided OrthographicFrustum componentwise and returns
  278. * <code>true</code> if they pass an absolute or relative tolerance test,
  279. * <code>false</code> otherwise.
  280. *
  281. * @param {OrthographicFrustum} other The right hand side OrthographicFrustum.
  282. * @param {number} relativeEpsilon The relative epsilon tolerance to use for equality testing.
  283. * @param {number} [absoluteEpsilon=relativeEpsilon] The absolute epsilon tolerance to use for equality testing.
  284. * @returns {boolean} <code>true</code> if this and other are within the provided epsilon, <code>false</code> otherwise.
  285. */
  286. OrthographicFrustum.prototype.equalsEpsilon = function (
  287. other,
  288. relativeEpsilon,
  289. absoluteEpsilon
  290. ) {
  291. if (!defined(other) || !(other instanceof OrthographicFrustum)) {
  292. return false;
  293. }
  294. update(this);
  295. update(other);
  296. return (
  297. CesiumMath.equalsEpsilon(
  298. this.width,
  299. other.width,
  300. relativeEpsilon,
  301. absoluteEpsilon
  302. ) &&
  303. CesiumMath.equalsEpsilon(
  304. this.aspectRatio,
  305. other.aspectRatio,
  306. relativeEpsilon,
  307. absoluteEpsilon
  308. ) &&
  309. this._offCenterFrustum.equalsEpsilon(
  310. other._offCenterFrustum,
  311. relativeEpsilon,
  312. absoluteEpsilon
  313. )
  314. );
  315. };
  316. export default OrthographicFrustum;