PerspectiveOffCenterFrustum.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. import Cartesian2 from './Cartesian2.js';
  2. import Cartesian3 from './Cartesian3.js';
  3. import Cartesian4 from './Cartesian4.js';
  4. import CullingVolume from './CullingVolume.js';
  5. import defaultValue from './defaultValue.js';
  6. import defined from './defined.js';
  7. import defineProperties from './defineProperties.js';
  8. import deprecationWarning from './deprecationWarning.js';
  9. import DeveloperError from './DeveloperError.js';
  10. import CesiumMath from './Math.js';
  11. import Matrix4 from './Matrix4.js';
  12. /**
  13. * The viewing frustum is defined by 6 planes.
  14. * Each plane is represented by a {@link Cartesian4} object, where the x, y, and z components
  15. * define the unit vector normal to the plane, and the w component is the distance of the
  16. * plane from the origin/camera position.
  17. *
  18. * @alias PerspectiveOffCenterFrustum
  19. * @constructor
  20. *
  21. * @param {Object} [options] An object with the following properties:
  22. * @param {Number} [options.left] The left clipping plane distance.
  23. * @param {Number} [options.right] The right clipping plane distance.
  24. * @param {Number} [options.top] The top clipping plane distance.
  25. * @param {Number} [options.bottom] The bottom clipping plane distance.
  26. * @param {Number} [options.near=1.0] The near clipping plane distance.
  27. * @param {Number} [options.far=500000000.0] The far clipping plane distance.
  28. *
  29. * @example
  30. * var frustum = new Cesium.PerspectiveOffCenterFrustum({
  31. * left : -1.0,
  32. * right : 1.0,
  33. * top : 1.0,
  34. * bottom : -1.0,
  35. * near : 1.0,
  36. * far : 100.0
  37. * });
  38. *
  39. * @see PerspectiveFrustum
  40. */
  41. function PerspectiveOffCenterFrustum(options) {
  42. options = defaultValue(options, defaultValue.EMPTY_OBJECT);
  43. /**
  44. * Defines the left clipping plane.
  45. * @type {Number}
  46. * @default undefined
  47. */
  48. this.left = options.left;
  49. this._left = undefined;
  50. /**
  51. * Defines the right clipping plane.
  52. * @type {Number}
  53. * @default undefined
  54. */
  55. this.right = options.right;
  56. this._right = undefined;
  57. /**
  58. * Defines the top clipping plane.
  59. * @type {Number}
  60. * @default undefined
  61. */
  62. this.top = options.top;
  63. this._top = undefined;
  64. /**
  65. * Defines the bottom clipping plane.
  66. * @type {Number}
  67. * @default undefined
  68. */
  69. this.bottom = options.bottom;
  70. this._bottom = undefined;
  71. /**
  72. * The distance of the near plane.
  73. * @type {Number}
  74. * @default 1.0
  75. */
  76. this.near = defaultValue(options.near, 1.0);
  77. this._near = this.near;
  78. /**
  79. * The distance of the far plane.
  80. * @type {Number}
  81. * @default 500000000.0
  82. */
  83. this.far = defaultValue(options.far, 500000000.0);
  84. this._far = this.far;
  85. this._cullingVolume = new CullingVolume();
  86. this._perspectiveMatrix = new Matrix4();
  87. this._infinitePerspective = new Matrix4();
  88. }
  89. function update(frustum) {
  90. //>>includeStart('debug', pragmas.debug);
  91. if (!defined(frustum.right) || !defined(frustum.left) ||
  92. !defined(frustum.top) || !defined(frustum.bottom) ||
  93. !defined(frustum.near) || !defined(frustum.far)) {
  94. throw new DeveloperError('right, left, top, bottom, near, or far parameters are not set.');
  95. }
  96. //>>includeEnd('debug');
  97. var t = frustum.top;
  98. var b = frustum.bottom;
  99. var r = frustum.right;
  100. var l = frustum.left;
  101. var n = frustum.near;
  102. var f = frustum.far;
  103. if (t !== frustum._top || b !== frustum._bottom ||
  104. l !== frustum._left || r !== frustum._right ||
  105. n !== frustum._near || f !== frustum._far) {
  106. //>>includeStart('debug', pragmas.debug);
  107. if (frustum.near <= 0 || frustum.near > frustum.far) {
  108. throw new DeveloperError('near must be greater than zero and less than far.');
  109. }
  110. //>>includeEnd('debug');
  111. frustum._left = l;
  112. frustum._right = r;
  113. frustum._top = t;
  114. frustum._bottom = b;
  115. frustum._near = n;
  116. frustum._far = f;
  117. frustum._perspectiveMatrix = Matrix4.computePerspectiveOffCenter(l, r, b, t, n, f, frustum._perspectiveMatrix);
  118. frustum._infinitePerspective = Matrix4.computeInfinitePerspectiveOffCenter(l, r, b, t, n, frustum._infinitePerspective);
  119. }
  120. }
  121. defineProperties(PerspectiveOffCenterFrustum.prototype, {
  122. /**
  123. * Gets the perspective projection matrix computed from the view frustum.
  124. * @memberof PerspectiveOffCenterFrustum.prototype
  125. * @type {Matrix4}
  126. * @readonly
  127. *
  128. * @see PerspectiveOffCenterFrustum#infiniteProjectionMatrix
  129. */
  130. projectionMatrix : {
  131. get : function() {
  132. update(this);
  133. return this._perspectiveMatrix;
  134. }
  135. },
  136. /**
  137. * Gets the perspective projection matrix computed from the view frustum with an infinite far plane.
  138. * @memberof PerspectiveOffCenterFrustum.prototype
  139. * @type {Matrix4}
  140. * @readonly
  141. *
  142. * @see PerspectiveOffCenterFrustum#projectionMatrix
  143. */
  144. infiniteProjectionMatrix : {
  145. get : function() {
  146. update(this);
  147. return this._infinitePerspective;
  148. }
  149. }
  150. });
  151. var getPlanesRight = new Cartesian3();
  152. var getPlanesNearCenter = new Cartesian3();
  153. var getPlanesFarCenter = new Cartesian3();
  154. var getPlanesNormal = new Cartesian3();
  155. /**
  156. * Creates a culling volume for this frustum.
  157. *
  158. * @param {Cartesian3} position The eye position.
  159. * @param {Cartesian3} direction The view direction.
  160. * @param {Cartesian3} up The up direction.
  161. * @returns {CullingVolume} A culling volume at the given position and orientation.
  162. *
  163. * @example
  164. * // Check if a bounding volume intersects the frustum.
  165. * var cullingVolume = frustum.computeCullingVolume(cameraPosition, cameraDirection, cameraUp);
  166. * var intersect = cullingVolume.computeVisibility(boundingVolume);
  167. */
  168. PerspectiveOffCenterFrustum.prototype.computeCullingVolume = function(position, direction, up) {
  169. //>>includeStart('debug', pragmas.debug);
  170. if (!defined(position)) {
  171. throw new DeveloperError('position is required.');
  172. }
  173. if (!defined(direction)) {
  174. throw new DeveloperError('direction is required.');
  175. }
  176. if (!defined(up)) {
  177. throw new DeveloperError('up is required.');
  178. }
  179. //>>includeEnd('debug');
  180. var planes = this._cullingVolume.planes;
  181. var t = this.top;
  182. var b = this.bottom;
  183. var r = this.right;
  184. var l = this.left;
  185. var n = this.near;
  186. var f = this.far;
  187. var right = Cartesian3.cross(direction, up, getPlanesRight);
  188. var nearCenter = getPlanesNearCenter;
  189. Cartesian3.multiplyByScalar(direction, n, nearCenter);
  190. Cartesian3.add(position, nearCenter, nearCenter);
  191. var farCenter = getPlanesFarCenter;
  192. Cartesian3.multiplyByScalar(direction, f, farCenter);
  193. Cartesian3.add(position, farCenter, farCenter);
  194. var normal = getPlanesNormal;
  195. //Left plane computation
  196. Cartesian3.multiplyByScalar(right, l, normal);
  197. Cartesian3.add(nearCenter, normal, normal);
  198. Cartesian3.subtract(normal, position, normal);
  199. Cartesian3.normalize(normal, normal);
  200. Cartesian3.cross(normal, up, normal);
  201. Cartesian3.normalize(normal, normal);
  202. var plane = planes[0];
  203. if (!defined(plane)) {
  204. plane = planes[0] = new Cartesian4();
  205. }
  206. plane.x = normal.x;
  207. plane.y = normal.y;
  208. plane.z = normal.z;
  209. plane.w = -Cartesian3.dot(normal, position);
  210. //Right plane computation
  211. Cartesian3.multiplyByScalar(right, r, normal);
  212. Cartesian3.add(nearCenter, normal, normal);
  213. Cartesian3.subtract(normal, position, normal);
  214. Cartesian3.cross(up, normal, normal);
  215. Cartesian3.normalize(normal, normal);
  216. plane = planes[1];
  217. if (!defined(plane)) {
  218. plane = planes[1] = new Cartesian4();
  219. }
  220. plane.x = normal.x;
  221. plane.y = normal.y;
  222. plane.z = normal.z;
  223. plane.w = -Cartesian3.dot(normal, position);
  224. //Bottom plane computation
  225. Cartesian3.multiplyByScalar(up, b, normal);
  226. Cartesian3.add(nearCenter, normal, normal);
  227. Cartesian3.subtract(normal, position, normal);
  228. Cartesian3.cross(right, normal, normal);
  229. Cartesian3.normalize(normal, normal);
  230. plane = planes[2];
  231. if (!defined(plane)) {
  232. plane = planes[2] = new Cartesian4();
  233. }
  234. plane.x = normal.x;
  235. plane.y = normal.y;
  236. plane.z = normal.z;
  237. plane.w = -Cartesian3.dot(normal, position);
  238. //Top plane computation
  239. Cartesian3.multiplyByScalar(up, t, normal);
  240. Cartesian3.add(nearCenter, normal, normal);
  241. Cartesian3.subtract(normal, position, normal);
  242. Cartesian3.cross(normal, right, normal);
  243. Cartesian3.normalize(normal, normal);
  244. plane = planes[3];
  245. if (!defined(plane)) {
  246. plane = planes[3] = new Cartesian4();
  247. }
  248. plane.x = normal.x;
  249. plane.y = normal.y;
  250. plane.z = normal.z;
  251. plane.w = -Cartesian3.dot(normal, position);
  252. //Near plane computation
  253. plane = planes[4];
  254. if (!defined(plane)) {
  255. plane = planes[4] = new Cartesian4();
  256. }
  257. plane.x = direction.x;
  258. plane.y = direction.y;
  259. plane.z = direction.z;
  260. plane.w = -Cartesian3.dot(direction, nearCenter);
  261. //Far plane computation
  262. Cartesian3.negate(direction, normal);
  263. plane = planes[5];
  264. if (!defined(plane)) {
  265. plane = planes[5] = new Cartesian4();
  266. }
  267. plane.x = normal.x;
  268. plane.y = normal.y;
  269. plane.z = normal.z;
  270. plane.w = -Cartesian3.dot(normal, farCenter);
  271. return this._cullingVolume;
  272. };
  273. /**
  274. * Returns the pixel's width and height in meters.
  275. *
  276. * @param {Number} drawingBufferWidth The width of the drawing buffer.
  277. * @param {Number} drawingBufferHeight The height of the drawing buffer.
  278. * @param {Number} distance The distance to the near plane in meters.
  279. * @param {Number} pixelRatio The scaling factor from pixel space to coordinate space.
  280. * @param {Cartesian2} result The object onto which to store the result.
  281. * @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.
  282. *
  283. * @exception {DeveloperError} drawingBufferWidth must be greater than zero.
  284. * @exception {DeveloperError} drawingBufferHeight must be greater than zero.
  285. * @exception {DeveloperError} pixelRatio must be greater than zero.
  286. *
  287. * @example
  288. * // Example 1
  289. * // Get the width and height of a pixel.
  290. * var pixelSize = camera.frustum.getPixelDimensions(scene.drawingBufferWidth, scene.drawingBufferHeight, 1.0, scene.pixelRatio, new Cesium.Cartesian2());
  291. *
  292. * @example
  293. * // Example 2
  294. * // Get the width and height of a pixel if the near plane was set to 'distance'.
  295. * // For example, get the size of a pixel of an image on a billboard.
  296. * var position = camera.position;
  297. * var direction = camera.direction;
  298. * var toCenter = Cesium.Cartesian3.subtract(primitive.boundingVolume.center, position, new Cesium.Cartesian3()); // vector from camera to a primitive
  299. * var toCenterProj = Cesium.Cartesian3.multiplyByScalar(direction, Cesium.Cartesian3.dot(direction, toCenter), new Cesium.Cartesian3()); // project vector onto camera direction vector
  300. * var distance = Cesium.Cartesian3.magnitude(toCenterProj);
  301. * var pixelSize = camera.frustum.getPixelDimensions(scene.drawingBufferWidth, scene.drawingBufferHeight, distance, scene.pixelRatio, new Cesium.Cartesian2());
  302. */
  303. PerspectiveOffCenterFrustum.prototype.getPixelDimensions = function(drawingBufferWidth, drawingBufferHeight, distance, pixelRatio, result) {
  304. update(this);
  305. if (pixelRatio instanceof Cartesian2) {
  306. result = pixelRatio;
  307. pixelRatio = 1.0;
  308. 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.');
  309. }
  310. //>>includeStart('debug', pragmas.debug);
  311. if (!defined(drawingBufferWidth) || !defined(drawingBufferHeight)) {
  312. throw new DeveloperError('Both drawingBufferWidth and drawingBufferHeight are required.');
  313. }
  314. if (drawingBufferWidth <= 0) {
  315. throw new DeveloperError('drawingBufferWidth must be greater than zero.');
  316. }
  317. if (drawingBufferHeight <= 0) {
  318. throw new DeveloperError('drawingBufferHeight must be greater than zero.');
  319. }
  320. if (!defined(distance)) {
  321. throw new DeveloperError('distance is required.');
  322. }
  323. if (!defined(pixelRatio)) {
  324. throw new DeveloperError('pixelRatio is required');
  325. }
  326. if (pixelRatio <= 0) {
  327. throw new DeveloperError('pixelRatio must be greater than zero.');
  328. }
  329. if (!defined(result)) {
  330. throw new DeveloperError('A result object is required.');
  331. }
  332. //>>includeEnd('debug');
  333. var inverseNear = 1.0 / this.near;
  334. var tanTheta = this.top * inverseNear;
  335. var pixelHeight = 2.0 * pixelRatio * distance * tanTheta / drawingBufferHeight;
  336. tanTheta = this.right * inverseNear;
  337. var pixelWidth = 2.0 * pixelRatio * distance * tanTheta / drawingBufferWidth;
  338. result.x = pixelWidth;
  339. result.y = pixelHeight;
  340. return result;
  341. };
  342. /**
  343. * Returns a duplicate of a PerspectiveOffCenterFrustum instance.
  344. *
  345. * @param {PerspectiveOffCenterFrustum} [result] The object onto which to store the result.
  346. * @returns {PerspectiveOffCenterFrustum} The modified result parameter or a new PerspectiveFrustum instance if one was not provided.
  347. */
  348. PerspectiveOffCenterFrustum.prototype.clone = function(result) {
  349. if (!defined(result)) {
  350. result = new PerspectiveOffCenterFrustum();
  351. }
  352. result.right = this.right;
  353. result.left = this.left;
  354. result.top = this.top;
  355. result.bottom = this.bottom;
  356. result.near = this.near;
  357. result.far = this.far;
  358. // force update of clone to compute matrices
  359. result._left = undefined;
  360. result._right = undefined;
  361. result._top = undefined;
  362. result._bottom = undefined;
  363. result._near = undefined;
  364. result._far = undefined;
  365. return result;
  366. };
  367. /**
  368. * Compares the provided PerspectiveOffCenterFrustum componentwise and returns
  369. * <code>true</code> if they are equal, <code>false</code> otherwise.
  370. *
  371. * @param {PerspectiveOffCenterFrustum} [other] The right hand side PerspectiveOffCenterFrustum.
  372. * @returns {Boolean} <code>true</code> if they are equal, <code>false</code> otherwise.
  373. */
  374. PerspectiveOffCenterFrustum.prototype.equals = function(other) {
  375. return (defined(other) && other instanceof PerspectiveOffCenterFrustum &&
  376. this.right === other.right &&
  377. this.left === other.left &&
  378. this.top === other.top &&
  379. this.bottom === other.bottom &&
  380. this.near === other.near &&
  381. this.far === other.far);
  382. };
  383. /**
  384. * Compares the provided PerspectiveOffCenterFrustum componentwise and returns
  385. * <code>true</code> if they pass an absolute or relative tolerance test,
  386. * <code>false</code> otherwise.
  387. *
  388. * @param {PerspectiveOffCenterFrustum} other The right hand side PerspectiveOffCenterFrustum.
  389. * @param {Number} relativeEpsilon The relative epsilon tolerance to use for equality testing.
  390. * @param {Number} [absoluteEpsilon=relativeEpsilon] The absolute epsilon tolerance to use for equality testing.
  391. * @returns {Boolean} <code>true</code> if this and other are within the provided epsilon, <code>false</code> otherwise.
  392. */
  393. PerspectiveOffCenterFrustum.prototype.equalsEpsilon = function(other, relativeEpsilon, absoluteEpsilon) {
  394. return (other === this) ||
  395. (defined(other) &&
  396. other instanceof PerspectiveOffCenterFrustum &&
  397. CesiumMath.equalsEpsilon(this.right, other.right, relativeEpsilon, absoluteEpsilon) &&
  398. CesiumMath.equalsEpsilon(this.left, other.left, relativeEpsilon, absoluteEpsilon) &&
  399. CesiumMath.equalsEpsilon(this.top, other.top, relativeEpsilon, absoluteEpsilon) &&
  400. CesiumMath.equalsEpsilon(this.bottom, other.bottom, relativeEpsilon, absoluteEpsilon) &&
  401. CesiumMath.equalsEpsilon(this.near, other.near, relativeEpsilon, absoluteEpsilon) &&
  402. CesiumMath.equalsEpsilon(this.far, other.far, relativeEpsilon, absoluteEpsilon));
  403. };
  404. export default PerspectiveOffCenterFrustum;