OrthographicOffCenterFrustum.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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 OrthographicOffCenterFrustum
  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 maxRadii = ellipsoid.maximumRadius;
  31. *
  32. * var frustum = new Cesium.OrthographicOffCenterFrustum();
  33. * frustum.right = maxRadii * Cesium.Math.PI;
  34. * frustum.left = -c.frustum.right;
  35. * frustum.top = c.frustum.right * (canvas.clientHeight / canvas.clientWidth);
  36. * frustum.bottom = -c.frustum.top;
  37. * frustum.near = 0.01 * maxRadii;
  38. * frustum.far = 50.0 * maxRadii;
  39. */
  40. function OrthographicOffCenterFrustum(options) {
  41. options = defaultValue(options, defaultValue.EMPTY_OBJECT);
  42. /**
  43. * The left clipping plane.
  44. * @type {Number}
  45. * @default undefined
  46. */
  47. this.left = options.left;
  48. this._left = undefined;
  49. /**
  50. * The right clipping plane.
  51. * @type {Number}
  52. * @default undefined
  53. */
  54. this.right = options.right;
  55. this._right = undefined;
  56. /**
  57. * The top clipping plane.
  58. * @type {Number}
  59. * @default undefined
  60. */
  61. this.top = options.top;
  62. this._top = undefined;
  63. /**
  64. * The bottom clipping plane.
  65. * @type {Number}
  66. * @default undefined
  67. */
  68. this.bottom = options.bottom;
  69. this._bottom = undefined;
  70. /**
  71. * The distance of the near plane.
  72. * @type {Number}
  73. * @default 1.0
  74. */
  75. this.near = defaultValue(options.near, 1.0);
  76. this._near = this.near;
  77. /**
  78. * The distance of the far plane.
  79. * @type {Number}
  80. * @default 500000000.0;
  81. */
  82. this.far = defaultValue(options.far, 500000000.0);
  83. this._far = this.far;
  84. this._cullingVolume = new CullingVolume();
  85. this._orthographicMatrix = new Matrix4();
  86. }
  87. function update(frustum) {
  88. //>>includeStart('debug', pragmas.debug);
  89. if (!defined(frustum.right) || !defined(frustum.left) ||
  90. !defined(frustum.top) || !defined(frustum.bottom) ||
  91. !defined(frustum.near) || !defined(frustum.far)) {
  92. throw new DeveloperError('right, left, top, bottom, near, or far parameters are not set.');
  93. }
  94. //>>includeEnd('debug');
  95. if (frustum.top !== frustum._top || frustum.bottom !== frustum._bottom ||
  96. frustum.left !== frustum._left || frustum.right !== frustum._right ||
  97. frustum.near !== frustum._near || frustum.far !== frustum._far) {
  98. //>>includeStart('debug', pragmas.debug);
  99. if (frustum.left > frustum.right) {
  100. throw new DeveloperError('right must be greater than left.');
  101. }
  102. if (frustum.bottom > frustum.top) {
  103. throw new DeveloperError('top must be greater than bottom.');
  104. }
  105. if (frustum.near <= 0 || frustum.near > frustum.far) {
  106. throw new DeveloperError('near must be greater than zero and less than far.');
  107. }
  108. //>>includeEnd('debug');
  109. frustum._left = frustum.left;
  110. frustum._right = frustum.right;
  111. frustum._top = frustum.top;
  112. frustum._bottom = frustum.bottom;
  113. frustum._near = frustum.near;
  114. frustum._far = frustum.far;
  115. frustum._orthographicMatrix = Matrix4.computeOrthographicOffCenter(frustum.left, frustum.right, frustum.bottom, frustum.top, frustum.near, frustum.far, frustum._orthographicMatrix);
  116. }
  117. }
  118. defineProperties(OrthographicOffCenterFrustum.prototype, {
  119. /**
  120. * Gets the orthographic projection matrix computed from the view frustum.
  121. * @memberof OrthographicOffCenterFrustum.prototype
  122. * @type {Matrix4}
  123. * @readonly
  124. */
  125. projectionMatrix : {
  126. get : function() {
  127. update(this);
  128. return this._orthographicMatrix;
  129. }
  130. }
  131. });
  132. var getPlanesRight = new Cartesian3();
  133. var getPlanesNearCenter = new Cartesian3();
  134. var getPlanesPoint = new Cartesian3();
  135. var negateScratch = new Cartesian3();
  136. /**
  137. * Creates a culling volume for this frustum.
  138. *
  139. * @param {Cartesian3} position The eye position.
  140. * @param {Cartesian3} direction The view direction.
  141. * @param {Cartesian3} up The up direction.
  142. * @returns {CullingVolume} A culling volume at the given position and orientation.
  143. *
  144. * @example
  145. * // Check if a bounding volume intersects the frustum.
  146. * var cullingVolume = frustum.computeCullingVolume(cameraPosition, cameraDirection, cameraUp);
  147. * var intersect = cullingVolume.computeVisibility(boundingVolume);
  148. */
  149. OrthographicOffCenterFrustum.prototype.computeCullingVolume = function(position, direction, up) {
  150. //>>includeStart('debug', pragmas.debug);
  151. if (!defined(position)) {
  152. throw new DeveloperError('position is required.');
  153. }
  154. if (!defined(direction)) {
  155. throw new DeveloperError('direction is required.');
  156. }
  157. if (!defined(up)) {
  158. throw new DeveloperError('up is required.');
  159. }
  160. //>>includeEnd('debug');
  161. var planes = this._cullingVolume.planes;
  162. var t = this.top;
  163. var b = this.bottom;
  164. var r = this.right;
  165. var l = this.left;
  166. var n = this.near;
  167. var f = this.far;
  168. var right = Cartesian3.cross(direction, up, getPlanesRight);
  169. Cartesian3.normalize(right, right);
  170. var nearCenter = getPlanesNearCenter;
  171. Cartesian3.multiplyByScalar(direction, n, nearCenter);
  172. Cartesian3.add(position, nearCenter, nearCenter);
  173. var point = getPlanesPoint;
  174. // Left plane
  175. Cartesian3.multiplyByScalar(right, l, point);
  176. Cartesian3.add(nearCenter, point, point);
  177. var plane = planes[0];
  178. if (!defined(plane)) {
  179. plane = planes[0] = new Cartesian4();
  180. }
  181. plane.x = right.x;
  182. plane.y = right.y;
  183. plane.z = right.z;
  184. plane.w = -Cartesian3.dot(right, point);
  185. // Right plane
  186. Cartesian3.multiplyByScalar(right, r, point);
  187. Cartesian3.add(nearCenter, point, point);
  188. plane = planes[1];
  189. if (!defined(plane)) {
  190. plane = planes[1] = new Cartesian4();
  191. }
  192. plane.x = -right.x;
  193. plane.y = -right.y;
  194. plane.z = -right.z;
  195. plane.w = -Cartesian3.dot(Cartesian3.negate(right, negateScratch), point);
  196. // Bottom plane
  197. Cartesian3.multiplyByScalar(up, b, point);
  198. Cartesian3.add(nearCenter, point, point);
  199. plane = planes[2];
  200. if (!defined(plane)) {
  201. plane = planes[2] = new Cartesian4();
  202. }
  203. plane.x = up.x;
  204. plane.y = up.y;
  205. plane.z = up.z;
  206. plane.w = -Cartesian3.dot(up, point);
  207. // Top plane
  208. Cartesian3.multiplyByScalar(up, t, point);
  209. Cartesian3.add(nearCenter, point, point);
  210. plane = planes[3];
  211. if (!defined(plane)) {
  212. plane = planes[3] = new Cartesian4();
  213. }
  214. plane.x = -up.x;
  215. plane.y = -up.y;
  216. plane.z = -up.z;
  217. plane.w = -Cartesian3.dot(Cartesian3.negate(up, negateScratch), point);
  218. // Near plane
  219. plane = planes[4];
  220. if (!defined(plane)) {
  221. plane = planes[4] = new Cartesian4();
  222. }
  223. plane.x = direction.x;
  224. plane.y = direction.y;
  225. plane.z = direction.z;
  226. plane.w = -Cartesian3.dot(direction, nearCenter);
  227. // Far plane
  228. Cartesian3.multiplyByScalar(direction, f, point);
  229. Cartesian3.add(position, point, point);
  230. plane = planes[5];
  231. if (!defined(plane)) {
  232. plane = planes[5] = new Cartesian4();
  233. }
  234. plane.x = -direction.x;
  235. plane.y = -direction.y;
  236. plane.z = -direction.z;
  237. plane.w = -Cartesian3.dot(Cartesian3.negate(direction, negateScratch), point);
  238. return this._cullingVolume;
  239. };
  240. /**
  241. * Returns the pixel's width and height in meters.
  242. *
  243. * @param {Number} drawingBufferWidth The width of the drawing buffer.
  244. * @param {Number} drawingBufferHeight The height of the drawing buffer.
  245. * @param {Number} distance The distance to the near plane in meters.
  246. * @param {Number} pixelRatio The scaling factor from pixel space to coordinate space.
  247. * @param {Cartesian2} result The object onto which to store the result.
  248. * @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.
  249. *
  250. * @exception {DeveloperError} drawingBufferWidth must be greater than zero.
  251. * @exception {DeveloperError} drawingBufferHeight must be greater than zero.
  252. * @exception {DeveloperError} pixelRatio must be greater than zero.
  253. *
  254. * @example
  255. * // Example 1
  256. * // Get the width and height of a pixel.
  257. * var pixelSize = camera.frustum.getPixelDimensions(scene.drawingBufferWidth, scene.drawingBufferHeight, 0.0, scene.pixelRatio, new Cesium.Cartesian2());
  258. */
  259. OrthographicOffCenterFrustum.prototype.getPixelDimensions = function(drawingBufferWidth, drawingBufferHeight, distance, pixelRatio, result) {
  260. update(this);
  261. if (pixelRatio instanceof Cartesian2) {
  262. result = pixelRatio;
  263. pixelRatio = 1.0;
  264. 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.');
  265. }
  266. //>>includeStart('debug', pragmas.debug);
  267. if (!defined(drawingBufferWidth) || !defined(drawingBufferHeight)) {
  268. throw new DeveloperError('Both drawingBufferWidth and drawingBufferHeight are required.');
  269. }
  270. if (drawingBufferWidth <= 0) {
  271. throw new DeveloperError('drawingBufferWidth must be greater than zero.');
  272. }
  273. if (drawingBufferHeight <= 0) {
  274. throw new DeveloperError('drawingBufferHeight must be greater than zero.');
  275. }
  276. if (!defined(distance)) {
  277. throw new DeveloperError('distance is required.');
  278. }
  279. if (!defined(pixelRatio)) {
  280. throw new DeveloperError('pixelRatio is required.');
  281. }
  282. if (pixelRatio <= 0) {
  283. throw new DeveloperError('pixelRatio must be greater than zero.');
  284. }
  285. if (!defined(result)) {
  286. throw new DeveloperError('A result object is required.');
  287. }
  288. //>>includeEnd('debug');
  289. var frustumWidth = this.right - this.left;
  290. var frustumHeight = this.top - this.bottom;
  291. var pixelWidth = pixelRatio * frustumWidth / drawingBufferWidth;
  292. var pixelHeight = pixelRatio * frustumHeight / drawingBufferHeight;
  293. result.x = pixelWidth;
  294. result.y = pixelHeight;
  295. return result;
  296. };
  297. /**
  298. * Returns a duplicate of a OrthographicOffCenterFrustum instance.
  299. *
  300. * @param {OrthographicOffCenterFrustum} [result] The object onto which to store the result.
  301. * @returns {OrthographicOffCenterFrustum} The modified result parameter or a new OrthographicOffCenterFrustum instance if one was not provided.
  302. */
  303. OrthographicOffCenterFrustum.prototype.clone = function(result) {
  304. if (!defined(result)) {
  305. result = new OrthographicOffCenterFrustum();
  306. }
  307. result.left = this.left;
  308. result.right = this.right;
  309. result.top = this.top;
  310. result.bottom = this.bottom;
  311. result.near = this.near;
  312. result.far = this.far;
  313. // force update of clone to compute matrices
  314. result._left = undefined;
  315. result._right = undefined;
  316. result._top = undefined;
  317. result._bottom = undefined;
  318. result._near = undefined;
  319. result._far = undefined;
  320. return result;
  321. };
  322. /**
  323. * Compares the provided OrthographicOffCenterFrustum componentwise and returns
  324. * <code>true</code> if they are equal, <code>false</code> otherwise.
  325. *
  326. * @param {OrthographicOffCenterFrustum} [other] The right hand side OrthographicOffCenterFrustum.
  327. * @returns {Boolean} <code>true</code> if they are equal, <code>false</code> otherwise.
  328. */
  329. OrthographicOffCenterFrustum.prototype.equals = function(other) {
  330. return (defined(other) && other instanceof OrthographicOffCenterFrustum &&
  331. this.right === other.right &&
  332. this.left === other.left &&
  333. this.top === other.top &&
  334. this.bottom === other.bottom &&
  335. this.near === other.near &&
  336. this.far === other.far);
  337. };
  338. /**
  339. * Compares the provided OrthographicOffCenterFrustum componentwise and returns
  340. * <code>true</code> if they pass an absolute or relative tolerance test,
  341. * <code>false</code> otherwise.
  342. *
  343. * @param {OrthographicOffCenterFrustum} other The right hand side OrthographicOffCenterFrustum.
  344. * @param {Number} relativeEpsilon The relative epsilon tolerance to use for equality testing.
  345. * @param {Number} [absoluteEpsilon=relativeEpsilon] The absolute epsilon tolerance to use for equality testing.
  346. * @returns {Boolean} <code>true</code> if this and other are within the provided epsilon, <code>false</code> otherwise.
  347. */
  348. OrthographicOffCenterFrustum.prototype.equalsEpsilon = function(other, relativeEpsilon, absoluteEpsilon) {
  349. return (other === this) ||
  350. (defined(other) &&
  351. other instanceof OrthographicOffCenterFrustum &&
  352. CesiumMath.equalsEpsilon(this.right, other.right, relativeEpsilon, absoluteEpsilon) &&
  353. CesiumMath.equalsEpsilon(this.left, other.left, relativeEpsilon, absoluteEpsilon) &&
  354. CesiumMath.equalsEpsilon(this.top, other.top, relativeEpsilon, absoluteEpsilon) &&
  355. CesiumMath.equalsEpsilon(this.bottom, other.bottom, relativeEpsilon, absoluteEpsilon) &&
  356. CesiumMath.equalsEpsilon(this.near, other.near, relativeEpsilon, absoluteEpsilon) &&
  357. CesiumMath.equalsEpsilon(this.far, other.far, relativeEpsilon, absoluteEpsilon));
  358. };
  359. export default OrthographicOffCenterFrustum;