Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

PerspectiveFrustum.js 15KB

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