OrthographicOffCenterFrustum.js 13 KB

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