PerspectiveFrustum.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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 PerspectiveOffCenterFrustum from './PerspectiveOffCenterFrustum.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 PerspectiveFrustum
  17. * @constructor
  18. *
  19. * @param {Object} [options] An object with the following properties:
  20. * @param {Number} [options.fov] The angle of the field of view (FOV), in radians.
  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. * @param {Number} [options.xOffset=0.0] The offset in the x direction.
  25. * @param {Number} [options.yOffset=0.0] The offset in the y direction.
  26. *
  27. * @example
  28. * var frustum = new Cesium.PerspectiveFrustum({
  29. * fov : Cesium.Math.PI_OVER_THREE,
  30. * aspectRatio : canvas.clientWidth / canvas.clientHeight
  31. * near : 1.0,
  32. * far : 1000.0
  33. * });
  34. *
  35. * @see PerspectiveOffCenterFrustum
  36. */
  37. function PerspectiveFrustum(options) {
  38. options = defaultValue(options, defaultValue.EMPTY_OBJECT);
  39. this._offCenterFrustum = new PerspectiveOffCenterFrustum();
  40. /**
  41. * The angle of the field of view (FOV), in radians. This angle will be used
  42. * as the horizontal FOV if the width is greater than the height, otherwise
  43. * it will be the vertical FOV.
  44. * @type {Number}
  45. * @default undefined
  46. */
  47. this.fov = options.fov;
  48. this._fov = undefined;
  49. this._fovy = undefined;
  50. this._sseDenominator = undefined;
  51. /**
  52. * The aspect ratio of the frustum's width to it's height.
  53. * @type {Number}
  54. * @default undefined
  55. */
  56. this.aspectRatio = options.aspectRatio;
  57. this._aspectRatio = undefined;
  58. /**
  59. * The distance of the near plane.
  60. * @type {Number}
  61. * @default 1.0
  62. */
  63. this.near = defaultValue(options.near, 1.0);
  64. this._near = this.near;
  65. /**
  66. * The distance of the far plane.
  67. * @type {Number}
  68. * @default 500000000.0
  69. */
  70. this.far = defaultValue(options.far, 500000000.0);
  71. this._far = this.far;
  72. /**
  73. * Offsets the frustum in the x direction.
  74. * @type {Number}
  75. * @default 0.0
  76. */
  77. this.xOffset = defaultValue(options.xOffset, 0.0);
  78. this._xOffset = this.xOffset;
  79. /**
  80. * Offsets the frustum in the y direction.
  81. * @type {Number}
  82. * @default 0.0
  83. */
  84. this.yOffset = defaultValue(options.yOffset, 0.0);
  85. this._yOffset = this.yOffset;
  86. }
  87. /**
  88. * The number of elements used to pack the object into an array.
  89. * @type {Number}
  90. */
  91. PerspectiveFrustum.packedLength = 6;
  92. /**
  93. * Stores the provided instance into the provided array.
  94. *
  95. * @param {PerspectiveFrustum} value The value to pack.
  96. * @param {Number[]} array The array to pack into.
  97. * @param {Number} [startingIndex=0] The index into the array at which to start packing the elements.
  98. *
  99. * @returns {Number[]} The array that was packed into
  100. */
  101. PerspectiveFrustum.pack = function(value, array, startingIndex) {
  102. //>>includeStart('debug', pragmas.debug);
  103. Check.typeOf.object('value', value);
  104. Check.defined('array', array);
  105. //>>includeEnd('debug');
  106. startingIndex = defaultValue(startingIndex, 0);
  107. array[startingIndex++] = value.fov;
  108. array[startingIndex++] = value.aspectRatio;
  109. array[startingIndex++] = value.near;
  110. array[startingIndex++] = value.far;
  111. array[startingIndex++] = value.xOffset;
  112. array[startingIndex] = value.yOffset;
  113. return array;
  114. };
  115. /**
  116. * Retrieves an instance from a packed array.
  117. *
  118. * @param {Number[]} array The packed array.
  119. * @param {Number} [startingIndex=0] The starting index of the element to be unpacked.
  120. * @param {PerspectiveFrustum} [result] The object into which to store the result.
  121. * @returns {PerspectiveFrustum} The modified result parameter or a new PerspectiveFrustum instance if one was not provided.
  122. */
  123. PerspectiveFrustum.unpack = function(array, startingIndex, result) {
  124. //>>includeStart('debug', pragmas.debug);
  125. Check.defined('array', array);
  126. //>>includeEnd('debug');
  127. startingIndex = defaultValue(startingIndex, 0);
  128. if (!defined(result)) {
  129. result = new PerspectiveFrustum();
  130. }
  131. result.fov = array[startingIndex++];
  132. result.aspectRatio = array[startingIndex++];
  133. result.near = array[startingIndex++];
  134. result.far = array[startingIndex++];
  135. result.xOffset = array[startingIndex++];
  136. result.yOffset = array[startingIndex];
  137. return result;
  138. };
  139. function update(frustum) {
  140. //>>includeStart('debug', pragmas.debug);
  141. if (!defined(frustum.fov) || !defined(frustum.aspectRatio) || !defined(frustum.near) || !defined(frustum.far)) {
  142. throw new DeveloperError('fov, aspectRatio, near, or far parameters are not set.');
  143. }
  144. //>>includeEnd('debug');
  145. var f = frustum._offCenterFrustum;
  146. if (frustum.fov !== frustum._fov || frustum.aspectRatio !== frustum._aspectRatio ||
  147. frustum.near !== frustum._near || frustum.far !== frustum._far ||
  148. frustum.xOffset !== frustum._xOffset || frustum.yOffset !== frustum._yOffset) {
  149. //>>includeStart('debug', pragmas.debug);
  150. if (frustum.fov < 0 || frustum.fov >= Math.PI) {
  151. throw new DeveloperError('fov must be in the range [0, PI).');
  152. }
  153. if (frustum.aspectRatio < 0) {
  154. throw new DeveloperError('aspectRatio must be positive.');
  155. }
  156. if (frustum.near < 0 || frustum.near > frustum.far) {
  157. throw new DeveloperError('near must be greater than zero and less than far.');
  158. }
  159. //>>includeEnd('debug');
  160. frustum._aspectRatio = frustum.aspectRatio;
  161. frustum._fov = frustum.fov;
  162. frustum._fovy = (frustum.aspectRatio <= 1) ? frustum.fov : Math.atan(Math.tan(frustum.fov * 0.5) / frustum.aspectRatio) * 2.0;
  163. frustum._near = frustum.near;
  164. frustum._far = frustum.far;
  165. frustum._sseDenominator = 2.0 * Math.tan(0.5 * frustum._fovy);
  166. frustum._xOffset = frustum.xOffset;
  167. frustum._yOffset = frustum.yOffset;
  168. f.top = frustum.near * Math.tan(0.5 * frustum._fovy);
  169. f.bottom = -f.top;
  170. f.right = frustum.aspectRatio * f.top;
  171. f.left = -f.right;
  172. f.near = frustum.near;
  173. f.far = frustum.far;
  174. f.right += frustum.xOffset;
  175. f.left += frustum.xOffset;
  176. f.top += frustum.yOffset;
  177. f.bottom += frustum.yOffset;
  178. }
  179. }
  180. defineProperties(PerspectiveFrustum.prototype, {
  181. /**
  182. * Gets the perspective projection matrix computed from the view frustum.
  183. * @memberof PerspectiveFrustum.prototype
  184. * @type {Matrix4}
  185. * @readonly
  186. *
  187. * @see PerspectiveFrustum#infiniteProjectionMatrix
  188. */
  189. projectionMatrix : {
  190. get : function() {
  191. update(this);
  192. return this._offCenterFrustum.projectionMatrix;
  193. }
  194. },
  195. /**
  196. * The perspective projection matrix computed from the view frustum with an infinite far plane.
  197. * @memberof PerspectiveFrustum.prototype
  198. * @type {Matrix4}
  199. * @readonly
  200. *
  201. * @see PerspectiveFrustum#projectionMatrix
  202. */
  203. infiniteProjectionMatrix : {
  204. get : function() {
  205. update(this);
  206. return this._offCenterFrustum.infiniteProjectionMatrix;
  207. }
  208. },
  209. /**
  210. * Gets the angle of the vertical field of view, in radians.
  211. * @memberof PerspectiveFrustum.prototype
  212. * @type {Number}
  213. * @readonly
  214. * @default undefined
  215. */
  216. fovy : {
  217. get : function() {
  218. update(this);
  219. return this._fovy;
  220. }
  221. },
  222. /**
  223. * @readonly
  224. * @private
  225. */
  226. sseDenominator : {
  227. get : function () {
  228. update(this);
  229. return this._sseDenominator;
  230. }
  231. }
  232. });
  233. /**
  234. * Creates a culling volume for this frustum.
  235. *
  236. * @param {Cartesian3} position The eye position.
  237. * @param {Cartesian3} direction The view direction.
  238. * @param {Cartesian3} up The up direction.
  239. * @returns {CullingVolume} A culling volume at the given position and orientation.
  240. *
  241. * @example
  242. * // Check if a bounding volume intersects the frustum.
  243. * var cullingVolume = frustum.computeCullingVolume(cameraPosition, cameraDirection, cameraUp);
  244. * var intersect = cullingVolume.computeVisibility(boundingVolume);
  245. */
  246. PerspectiveFrustum.prototype.computeCullingVolume = function(position, direction, up) {
  247. update(this);
  248. return this._offCenterFrustum.computeCullingVolume(position, direction, up);
  249. };
  250. /**
  251. * Returns the pixel's width and height in meters.
  252. *
  253. * @param {Number} drawingBufferWidth The width of the drawing buffer.
  254. * @param {Number} drawingBufferHeight The height of the drawing buffer.
  255. * @param {Number} distance The distance to the near plane in meters.
  256. * @param {Number} pixelRatio The scaling factor from pixel space to coordinate space.
  257. * @param {Cartesian2} result The object onto which to store the result.
  258. * @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.
  259. *
  260. * @exception {DeveloperError} drawingBufferWidth must be greater than zero.
  261. * @exception {DeveloperError} drawingBufferHeight must be greater than zero.
  262. * @exception {DeveloperError} pixelRatio must be greater than zero.
  263. *
  264. * @example
  265. * // Example 1
  266. * // Get the width and height of a pixel.
  267. * var pixelSize = camera.frustum.getPixelDimensions(scene.drawingBufferWidth, scene.drawingBufferHeight, 1.0, scene.pixelRatio, new Cesium.Cartesian2());
  268. *
  269. * @example
  270. * // Example 2
  271. * // Get the width and height of a pixel if the near plane was set to 'distance'.
  272. * // For example, get the size of a pixel of an image on a billboard.
  273. * var position = camera.position;
  274. * var direction = camera.direction;
  275. * var toCenter = Cesium.Cartesian3.subtract(primitive.boundingVolume.center, position, new Cesium.Cartesian3()); // vector from camera to a primitive
  276. * var toCenterProj = Cesium.Cartesian3.multiplyByScalar(direction, Cesium.Cartesian3.dot(direction, toCenter), new Cesium.Cartesian3()); // project vector onto camera direction vector
  277. * var distance = Cesium.Cartesian3.magnitude(toCenterProj);
  278. * var pixelSize = camera.frustum.getPixelDimensions(scene.drawingBufferWidth, scene.drawingBufferHeight, distance, scene.pixelRatio, new Cesium.Cartesian2());
  279. */
  280. PerspectiveFrustum.prototype.getPixelDimensions = function(drawingBufferWidth, drawingBufferHeight, distance, pixelRatio, result) {
  281. update(this);
  282. if (pixelRatio instanceof Cartesian2) {
  283. result = pixelRatio;
  284. pixelRatio = 1.0;
  285. 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.');
  286. }
  287. return this._offCenterFrustum.getPixelDimensions(drawingBufferWidth, drawingBufferHeight, distance, pixelRatio, result);
  288. };
  289. /**
  290. * Returns a duplicate of a PerspectiveFrustum instance.
  291. *
  292. * @param {PerspectiveFrustum} [result] The object onto which to store the result.
  293. * @returns {PerspectiveFrustum} The modified result parameter or a new PerspectiveFrustum instance if one was not provided.
  294. */
  295. PerspectiveFrustum.prototype.clone = function(result) {
  296. if (!defined(result)) {
  297. result = new PerspectiveFrustum();
  298. }
  299. result.aspectRatio = this.aspectRatio;
  300. result.fov = this.fov;
  301. result.near = this.near;
  302. result.far = this.far;
  303. // force update of clone to compute matrices
  304. result._aspectRatio = undefined;
  305. result._fov = undefined;
  306. result._near = undefined;
  307. result._far = undefined;
  308. this._offCenterFrustum.clone(result._offCenterFrustum);
  309. return result;
  310. };
  311. /**
  312. * Compares the provided PerspectiveFrustum componentwise and returns
  313. * <code>true</code> if they are equal, <code>false</code> otherwise.
  314. *
  315. * @param {PerspectiveFrustum} [other] The right hand side PerspectiveFrustum.
  316. * @returns {Boolean} <code>true</code> if they are equal, <code>false</code> otherwise.
  317. */
  318. PerspectiveFrustum.prototype.equals = function(other) {
  319. if (!defined(other) || !(other instanceof PerspectiveFrustum)) {
  320. return false;
  321. }
  322. update(this);
  323. update(other);
  324. return (this.fov === other.fov &&
  325. this.aspectRatio === other.aspectRatio &&
  326. this._offCenterFrustum.equals(other._offCenterFrustum));
  327. };
  328. /**
  329. * Compares the provided PerspectiveFrustum componentwise and returns
  330. * <code>true</code> if they pass an absolute or relative tolerance test,
  331. * <code>false</code> otherwise.
  332. *
  333. * @param {PerspectiveFrustum} other The right hand side PerspectiveFrustum.
  334. * @param {Number} relativeEpsilon The relative epsilon tolerance to use for equality testing.
  335. * @param {Number} [absoluteEpsilon=relativeEpsilon] The absolute epsilon tolerance to use for equality testing.
  336. * @returns {Boolean} <code>true</code> if this and other are within the provided epsilon, <code>false</code> otherwise.
  337. */
  338. PerspectiveFrustum.prototype.equalsEpsilon = function(other, relativeEpsilon, absoluteEpsilon) {
  339. if (!defined(other) || !(other instanceof PerspectiveFrustum)) {
  340. return false;
  341. }
  342. update(this);
  343. update(other);
  344. return (CesiumMath.equalsEpsilon(this.fov, other.fov, relativeEpsilon, absoluteEpsilon) &&
  345. CesiumMath.equalsEpsilon(this.aspectRatio, other.aspectRatio, relativeEpsilon, absoluteEpsilon) &&
  346. this._offCenterFrustum.equalsEpsilon(other._offCenterFrustum, relativeEpsilon, absoluteEpsilon));
  347. };
  348. export default PerspectiveFrustum;