You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

OrthographicOffCenterFrustum.js 15KB

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