UNPKG

@cesium/engine

Version:

CesiumJS is a JavaScript library for creating 3D globes and 2D maps in a web browser without a plugin.

1,544 lines (1,425 loc) 103 kB
// @ts-check import Cartesian3 from "./Cartesian3.js"; import Cartesian4 from "./Cartesian4.js"; import Check from "./Check.js"; import Frozen from "./Frozen.js"; import defined from "./defined.js"; import DeveloperError from "./DeveloperError.js"; import CesiumMath from "./Math.js"; import Matrix3 from "./Matrix3.js"; import RuntimeError from "./RuntimeError.js"; /** @import Quaternion from "./Quaternion.js"; */ /** @import TranslationRotationScale from "./TranslationRotationScale.js"; */ /** @import Camera from "../Scene/Camera.js"; */ /** * @typedef {object} Viewport * @property {number} [x] * @property {number} [y] * @property {number} [width] * @property {number} [height] */ /** * A 4x4 matrix, indexable as a column-major order array. * Constructor parameters are in row-major order for code readability. * * @implements {ArrayLike<number>} * * @see Matrix4.fromArray * @see Matrix4.fromColumnMajorArray * @see Matrix4.fromRowMajorArray * @see Matrix4.fromRotationTranslation * @see Matrix4.fromTranslationQuaternionRotationScale * @see Matrix4.fromTranslationRotationScale * @see Matrix4.fromTranslation * @see Matrix4.fromScale * @see Matrix4.fromUniformScale * @see Matrix4.fromRotation * @see Matrix4.fromCamera * @see Matrix4.computePerspectiveFieldOfView * @see Matrix4.computeOrthographicOffCenter * @see Matrix4.computePerspectiveOffCenter * @see Matrix4.computeInfinitePerspectiveOffCenter * @see Matrix4.computeViewportTransformation * @see Matrix4.computeView * @see Matrix2 * @see Matrix3 * @see Packable */ // @ts-expect-error TODO(tsd-jsdoc): Requires index signature support. class Matrix4 { /** * @param {number} [column0Row0=0.0] The value for column 0, row 0. * @param {number} [column1Row0=0.0] The value for column 1, row 0. * @param {number} [column2Row0=0.0] The value for column 2, row 0. * @param {number} [column3Row0=0.0] The value for column 3, row 0. * @param {number} [column0Row1=0.0] The value for column 0, row 1. * @param {number} [column1Row1=0.0] The value for column 1, row 1. * @param {number} [column2Row1=0.0] The value for column 2, row 1. * @param {number} [column3Row1=0.0] The value for column 3, row 1. * @param {number} [column0Row2=0.0] The value for column 0, row 2. * @param {number} [column1Row2=0.0] The value for column 1, row 2. * @param {number} [column2Row2=0.0] The value for column 2, row 2. * @param {number} [column3Row2=0.0] The value for column 3, row 2. * @param {number} [column0Row3=0.0] The value for column 0, row 3. * @param {number} [column1Row3=0.0] The value for column 1, row 3. * @param {number} [column2Row3=0.0] The value for column 2, row 3. * @param {number} [column3Row3=0.0] The value for column 3, row 3. */ constructor( column0Row0, column1Row0, column2Row0, column3Row0, column0Row1, column1Row1, column2Row1, column3Row1, column0Row2, column1Row2, column2Row2, column3Row2, column0Row3, column1Row3, column2Row3, column3Row3, ) { this[0] = column0Row0 ?? 0.0; this[1] = column0Row1 ?? 0.0; this[2] = column0Row2 ?? 0.0; this[3] = column0Row3 ?? 0.0; this[4] = column1Row0 ?? 0.0; this[5] = column1Row1 ?? 0.0; this[6] = column1Row2 ?? 0.0; this[7] = column1Row3 ?? 0.0; this[8] = column2Row0 ?? 0.0; this[9] = column2Row1 ?? 0.0; this[10] = column2Row2 ?? 0.0; this[11] = column2Row3 ?? 0.0; this[12] = column3Row0 ?? 0.0; this[13] = column3Row1 ?? 0.0; this[14] = column3Row2 ?? 0.0; this[15] = column3Row3 ?? 0.0; } /** * Stores the provided instance into the provided array. * * @param {Matrix4} value The value to pack. * @param {number[]} array The array to pack into. * @param {number} [startingIndex=0] The index into the array at which to start packing the elements. * * @returns {number[]} The array that was packed into */ static pack(value, array, startingIndex) { //>>includeStart('debug', pragmas.debug); Check.typeOf.object("value", value); Check.defined("array", array); //>>includeEnd('debug'); startingIndex = startingIndex ?? 0; array[startingIndex++] = value[0]; array[startingIndex++] = value[1]; array[startingIndex++] = value[2]; array[startingIndex++] = value[3]; array[startingIndex++] = value[4]; array[startingIndex++] = value[5]; array[startingIndex++] = value[6]; array[startingIndex++] = value[7]; array[startingIndex++] = value[8]; array[startingIndex++] = value[9]; array[startingIndex++] = value[10]; array[startingIndex++] = value[11]; array[startingIndex++] = value[12]; array[startingIndex++] = value[13]; array[startingIndex++] = value[14]; array[startingIndex] = value[15]; return array; } /** * Retrieves an instance from a packed array. * * @param {number[]} array The packed array. * @param {number} [startingIndex=0] The starting index of the element to be unpacked. * @param {Matrix4} [result] The object into which to store the result. * @returns {Matrix4} The modified result parameter or a new Matrix4 instance if one was not provided. */ static unpack(array, startingIndex, result) { //>>includeStart('debug', pragmas.debug); Check.defined("array", array); //>>includeEnd('debug'); startingIndex = startingIndex ?? 0; if (!defined(result)) { result = new Matrix4(); } result[0] = array[startingIndex++]; result[1] = array[startingIndex++]; result[2] = array[startingIndex++]; result[3] = array[startingIndex++]; result[4] = array[startingIndex++]; result[5] = array[startingIndex++]; result[6] = array[startingIndex++]; result[7] = array[startingIndex++]; result[8] = array[startingIndex++]; result[9] = array[startingIndex++]; result[10] = array[startingIndex++]; result[11] = array[startingIndex++]; result[12] = array[startingIndex++]; result[13] = array[startingIndex++]; result[14] = array[startingIndex++]; result[15] = array[startingIndex]; return result; } /** * Flattens an array of Matrix4s into an array of components. The components * are stored in column-major order. * * @param {Matrix4[]} array The array of matrices to pack. * @param {number[]} [result] The array onto which to store the result. If this is a typed array, it must have array.length * 16 components, else a {@link DeveloperError} will be thrown. If it is a regular array, it will be resized to have (array.length * 16) elements. * @returns {number[]} The packed array. */ static packArray(array, result) { //>>includeStart('debug', pragmas.debug); Check.defined("array", array); //>>includeEnd('debug'); const length = array.length; const resultLength = length * 16; if (!defined(result)) { result = new Array(resultLength); // @ts-expect-error TODO(tsd-jsdoc): Requires conditional TypedArray types. } else if (!Array.isArray(result) && result.length !== resultLength) { //>>includeStart('debug', pragmas.debug); throw new DeveloperError( "If result is a typed array, it must have exactly array.length * 16 elements", ); //>>includeEnd('debug'); } else if (result.length !== resultLength) { /** @type {number[]} */ (result).length = resultLength; } for (let i = 0; i < length; ++i) { Matrix4.pack(array[i], result, i * 16); } return result; } /** * Unpacks an array of column-major matrix components into an array of Matrix4s. * * @param {number[]} array The array of components to unpack. * @param {Matrix4[]} [result] The array onto which to store the result. * @returns {Matrix4[]} The unpacked array. */ static unpackArray(array, result) { //>>includeStart('debug', pragmas.debug); Check.defined("array", array); Check.typeOf.number.greaterThanOrEquals("array.length", array.length, 16); if (array.length % 16 !== 0) { throw new DeveloperError("array length must be a multiple of 16."); } //>>includeEnd('debug'); const length = array.length; if (!defined(result)) { result = new Array(length / 16); } else { result.length = length / 16; } for (let i = 0; i < length; i += 16) { const index = i / 16; result[index] = Matrix4.unpack(array, i, result[index]); } return result; } /** * Duplicates a Matrix4 instance. * * @param {Matrix4} matrix The matrix to duplicate. * @param {Matrix4} [result] The object onto which to store the result. * @returns {Matrix4} The modified result parameter or a new Matrix4 instance if one was not provided. (Returns undefined if matrix is undefined) */ static clone(matrix, result) { if (!defined(matrix)) { return undefined; } if (!defined(result)) { return new Matrix4( matrix[0], matrix[4], matrix[8], matrix[12], matrix[1], matrix[5], matrix[9], matrix[13], matrix[2], matrix[6], matrix[10], matrix[14], matrix[3], matrix[7], matrix[11], matrix[15], ); } result[0] = matrix[0]; result[1] = matrix[1]; result[2] = matrix[2]; result[3] = matrix[3]; result[4] = matrix[4]; result[5] = matrix[5]; result[6] = matrix[6]; result[7] = matrix[7]; result[8] = matrix[8]; result[9] = matrix[9]; result[10] = matrix[10]; result[11] = matrix[11]; result[12] = matrix[12]; result[13] = matrix[13]; result[14] = matrix[14]; result[15] = matrix[15]; return result; } /** * Computes a Matrix4 instance from a column-major order array. * * @param {number[]} values The column-major order array. * @param {Matrix4} [result] The object in which the result will be stored, if undefined a new instance will be created. * @returns {Matrix4} The modified result parameter, or a new Matrix4 instance if one was not provided. */ static fromColumnMajorArray(values, result) { //>>includeStart('debug', pragmas.debug); Check.defined("values", values); //>>includeEnd('debug'); // @ts-expect-error TODO(tsd-jsdoc): Requires index signature support. return Matrix4.clone(values, result); } /** * Computes a Matrix4 instance from a row-major order array. * The resulting matrix will be in column-major order. * * @param {number[]} values The row-major order array. * @param {Matrix4} [result] The object in which the result will be stored, if undefined a new instance will be created. * @returns {Matrix4} The modified result parameter, or a new Matrix4 instance if one was not provided. */ static fromRowMajorArray(values, result) { //>>includeStart('debug', pragmas.debug); Check.defined("values", values); //>>includeEnd('debug'); if (!defined(result)) { return new Matrix4( values[0], values[1], values[2], values[3], values[4], values[5], values[6], values[7], values[8], values[9], values[10], values[11], values[12], values[13], values[14], values[15], ); } result[0] = values[0]; result[1] = values[4]; result[2] = values[8]; result[3] = values[12]; result[4] = values[1]; result[5] = values[5]; result[6] = values[9]; result[7] = values[13]; result[8] = values[2]; result[9] = values[6]; result[10] = values[10]; result[11] = values[14]; result[12] = values[3]; result[13] = values[7]; result[14] = values[11]; result[15] = values[15]; return result; } /** * Computes a Matrix4 instance from a Matrix3 representing the rotation * and a Cartesian3 representing the translation. * * @param {Matrix3} rotation The upper left portion of the matrix representing the rotation. * @param {Cartesian3} [translation=Cartesian3.ZERO] The upper right portion of the matrix representing the translation. * @param {Matrix4} [result] The object in which the result will be stored, if undefined a new instance will be created. * @returns {Matrix4} The modified result parameter, or a new Matrix4 instance if one was not provided. */ static fromRotationTranslation(rotation, translation, result) { //>>includeStart('debug', pragmas.debug); Check.typeOf.object("rotation", rotation); //>>includeEnd('debug'); translation = translation ?? Cartesian3.ZERO; if (!defined(result)) { return new Matrix4( rotation[0], rotation[3], rotation[6], translation.x, rotation[1], rotation[4], rotation[7], translation.y, rotation[2], rotation[5], rotation[8], translation.z, 0.0, 0.0, 0.0, 1.0, ); } result[0] = rotation[0]; result[1] = rotation[1]; result[2] = rotation[2]; result[3] = 0.0; result[4] = rotation[3]; result[5] = rotation[4]; result[6] = rotation[5]; result[7] = 0.0; result[8] = rotation[6]; result[9] = rotation[7]; result[10] = rotation[8]; result[11] = 0.0; result[12] = translation.x; result[13] = translation.y; result[14] = translation.z; result[15] = 1.0; return result; } /** * Computes a Matrix4 instance from a translation, rotation, and scale (TRS) * representation with the rotation represented as a quaternion. * * @param {Cartesian3} translation The translation transformation. * @param {Quaternion} rotation The rotation transformation. * @param {Cartesian3} scale The non-uniform scale transformation. * @param {Matrix4} [result] The object in which the result will be stored, if undefined a new instance will be created. * @returns {Matrix4} The modified result parameter, or a new Matrix4 instance if one was not provided. * * @example * const result = Cesium.Matrix4.fromTranslationQuaternionRotationScale( * new Cesium.Cartesian3(1.0, 2.0, 3.0), // translation * Cesium.Quaternion.IDENTITY, // rotation * new Cesium.Cartesian3(7.0, 8.0, 9.0), // scale * result); */ static fromTranslationQuaternionRotationScale( translation, rotation, scale, result, ) { //>>includeStart('debug', pragmas.debug); Check.typeOf.object("translation", translation); Check.typeOf.object("rotation", rotation); Check.typeOf.object("scale", scale); //>>includeEnd('debug'); if (!defined(result)) { result = new Matrix4(); } const scaleX = scale.x; const scaleY = scale.y; const scaleZ = scale.z; const x2 = rotation.x * rotation.x; const xy = rotation.x * rotation.y; const xz = rotation.x * rotation.z; const xw = rotation.x * rotation.w; const y2 = rotation.y * rotation.y; const yz = rotation.y * rotation.z; const yw = rotation.y * rotation.w; const z2 = rotation.z * rotation.z; const zw = rotation.z * rotation.w; const w2 = rotation.w * rotation.w; const m00 = x2 - y2 - z2 + w2; const m01 = 2.0 * (xy - zw); const m02 = 2.0 * (xz + yw); const m10 = 2.0 * (xy + zw); const m11 = -x2 + y2 - z2 + w2; const m12 = 2.0 * (yz - xw); const m20 = 2.0 * (xz - yw); const m21 = 2.0 * (yz + xw); const m22 = -x2 - y2 + z2 + w2; result[0] = m00 * scaleX; result[1] = m10 * scaleX; result[2] = m20 * scaleX; result[3] = 0.0; result[4] = m01 * scaleY; result[5] = m11 * scaleY; result[6] = m21 * scaleY; result[7] = 0.0; result[8] = m02 * scaleZ; result[9] = m12 * scaleZ; result[10] = m22 * scaleZ; result[11] = 0.0; result[12] = translation.x; result[13] = translation.y; result[14] = translation.z; result[15] = 1.0; return result; } /** * Creates a Matrix4 instance from a {@link TranslationRotationScale} instance. * * @param {TranslationRotationScale} translationRotationScale The instance. * @param {Matrix4} [result] The object in which the result will be stored, if undefined a new instance will be created. * @returns {Matrix4} The modified result parameter, or a new Matrix4 instance if one was not provided. */ static fromTranslationRotationScale(translationRotationScale, result) { //>>includeStart('debug', pragmas.debug); Check.typeOf.object("translationRotationScale", translationRotationScale); //>>includeEnd('debug'); return Matrix4.fromTranslationQuaternionRotationScale( translationRotationScale.translation, translationRotationScale.rotation, translationRotationScale.scale, result, ); } /** * Creates a Matrix4 instance from a Cartesian3 representing the translation. * * @param {Cartesian3} translation The upper right portion of the matrix representing the translation. * @param {Matrix4} [result] The object in which the result will be stored, if undefined a new instance will be created. * @returns {Matrix4} The modified result parameter, or a new Matrix4 instance if one was not provided. * * @see Matrix4.multiplyByTranslation */ static fromTranslation(translation, result) { //>>includeStart('debug', pragmas.debug); Check.typeOf.object("translation", translation); //>>includeEnd('debug'); return Matrix4.fromRotationTranslation( Matrix3.IDENTITY, translation, result, ); } /** * Computes a Matrix4 instance representing a non-uniform scale. * * @param {Cartesian3} scale The x, y, and z scale factors. * @param {Matrix4} [result] The object in which the result will be stored, if undefined a new instance will be created. * @returns {Matrix4} The modified result parameter, or a new Matrix4 instance if one was not provided. * * @example * // Creates * // [7.0, 0.0, 0.0, 0.0] * // [0.0, 8.0, 0.0, 0.0] * // [0.0, 0.0, 9.0, 0.0] * // [0.0, 0.0, 0.0, 1.0] * const m = Cesium.Matrix4.fromScale(new Cesium.Cartesian3(7.0, 8.0, 9.0)); */ static fromScale(scale, result) { //>>includeStart('debug', pragmas.debug); Check.typeOf.object("scale", scale); //>>includeEnd('debug'); if (!defined(result)) { return new Matrix4( scale.x, 0.0, 0.0, 0.0, 0.0, scale.y, 0.0, 0.0, 0.0, 0.0, scale.z, 0.0, 0.0, 0.0, 0.0, 1.0, ); } result[0] = scale.x; result[1] = 0.0; result[2] = 0.0; result[3] = 0.0; result[4] = 0.0; result[5] = scale.y; result[6] = 0.0; result[7] = 0.0; result[8] = 0.0; result[9] = 0.0; result[10] = scale.z; result[11] = 0.0; result[12] = 0.0; result[13] = 0.0; result[14] = 0.0; result[15] = 1.0; return result; } /** * Computes a Matrix4 instance representing a uniform scale. * * @param {number} scale The uniform scale factor. * @param {Matrix4} [result] The object in which the result will be stored, if undefined a new instance will be created. * @returns {Matrix4} The modified result parameter, or a new Matrix4 instance if one was not provided. * * @example * // Creates * // [2.0, 0.0, 0.0, 0.0] * // [0.0, 2.0, 0.0, 0.0] * // [0.0, 0.0, 2.0, 0.0] * // [0.0, 0.0, 0.0, 1.0] * const m = Cesium.Matrix4.fromUniformScale(2.0); */ static fromUniformScale(scale, result) { //>>includeStart('debug', pragmas.debug); Check.typeOf.number("scale", scale); //>>includeEnd('debug'); if (!defined(result)) { return new Matrix4( scale, 0.0, 0.0, 0.0, 0.0, scale, 0.0, 0.0, 0.0, 0.0, scale, 0.0, 0.0, 0.0, 0.0, 1.0, ); } result[0] = scale; result[1] = 0.0; result[2] = 0.0; result[3] = 0.0; result[4] = 0.0; result[5] = scale; result[6] = 0.0; result[7] = 0.0; result[8] = 0.0; result[9] = 0.0; result[10] = scale; result[11] = 0.0; result[12] = 0.0; result[13] = 0.0; result[14] = 0.0; result[15] = 1.0; return result; } /** * Creates a rotation matrix. * * @param {Matrix3} rotation The rotation matrix. * @param {Matrix4} [result] The object in which the result will be stored, if undefined a new instance will be created. * @returns {Matrix4} The modified result parameter, or a new Matrix4 instance if one was not provided. */ static fromRotation(rotation, result) { //>>includeStart('debug', pragmas.debug); Check.typeOf.object("rotation", rotation); //>>includeEnd('debug'); if (!defined(result)) { result = new Matrix4(); } result[0] = rotation[0]; result[1] = rotation[1]; result[2] = rotation[2]; result[3] = 0.0; result[4] = rotation[3]; result[5] = rotation[4]; result[6] = rotation[5]; result[7] = 0.0; result[8] = rotation[6]; result[9] = rotation[7]; result[10] = rotation[8]; result[11] = 0.0; result[12] = 0.0; result[13] = 0.0; result[14] = 0.0; result[15] = 1.0; return result; } /** * Computes a Matrix4 instance from a Camera. * * @param {Camera} camera The camera to use. * @param {Matrix4} [result] The object in which the result will be stored, if undefined a new instance will be created. * @returns {Matrix4} The modified result parameter, or a new Matrix4 instance if one was not provided. */ static fromCamera(camera, result) { //>>includeStart('debug', pragmas.debug); Check.typeOf.object("camera", camera); //>>includeEnd('debug'); const position = camera.position; const direction = camera.direction; const up = camera.up; //>>includeStart('debug', pragmas.debug); Check.typeOf.object("camera.position", position); Check.typeOf.object("camera.direction", direction); Check.typeOf.object("camera.up", up); //>>includeEnd('debug'); Cartesian3.normalize(direction, fromCameraF); Cartesian3.normalize( Cartesian3.cross(fromCameraF, up, fromCameraR), fromCameraR, ); Cartesian3.normalize( Cartesian3.cross(fromCameraR, fromCameraF, fromCameraU), fromCameraU, ); const sX = fromCameraR.x; const sY = fromCameraR.y; const sZ = fromCameraR.z; const fX = fromCameraF.x; const fY = fromCameraF.y; const fZ = fromCameraF.z; const uX = fromCameraU.x; const uY = fromCameraU.y; const uZ = fromCameraU.z; const positionX = position.x; const positionY = position.y; const positionZ = position.z; const t0 = sX * -positionX + sY * -positionY + sZ * -positionZ; const t1 = uX * -positionX + uY * -positionY + uZ * -positionZ; const t2 = fX * positionX + fY * positionY + fZ * positionZ; // The code below this comment is an optimized // version of the commented lines. // Rather that create two matrices and then multiply, // we just bake in the multiplcation as part of creation. // const rotation = new Matrix4( // sX, sY, sZ, 0.0, // uX, uY, uZ, 0.0, // -fX, -fY, -fZ, 0.0, // 0.0, 0.0, 0.0, 1.0); // const translation = new Matrix4( // 1.0, 0.0, 0.0, -position.x, // 0.0, 1.0, 0.0, -position.y, // 0.0, 0.0, 1.0, -position.z, // 0.0, 0.0, 0.0, 1.0); // return rotation.multiply(translation); if (!defined(result)) { return new Matrix4( sX, sY, sZ, t0, uX, uY, uZ, t1, -fX, -fY, -fZ, t2, 0.0, 0.0, 0.0, 1.0, ); } result[0] = sX; result[1] = uX; result[2] = -fX; result[3] = 0.0; result[4] = sY; result[5] = uY; result[6] = -fY; result[7] = 0.0; result[8] = sZ; result[9] = uZ; result[10] = -fZ; result[11] = 0.0; result[12] = t0; result[13] = t1; result[14] = t2; result[15] = 1.0; return result; } /** * Computes a Matrix4 instance representing a perspective transformation matrix. * * @param {number} fovY The field of view along the Y axis in radians. * @param {number} aspectRatio The aspect ratio. * @param {number} near The distance to the near plane in meters. * @param {number} far The distance to the far plane in meters. * @param {Matrix4} result The object in which the result will be stored. * @returns {Matrix4} The modified result parameter. * * @exception {DeveloperError} fovY must be in (0, PI]. * @exception {DeveloperError} aspectRatio must be greater than zero. * @exception {DeveloperError} near must be greater than zero. * @exception {DeveloperError} far must be greater than zero. */ static computePerspectiveFieldOfView(fovY, aspectRatio, near, far, result) { //>>includeStart('debug', pragmas.debug); Check.typeOf.number.greaterThan("fovY", fovY, 0.0); Check.typeOf.number.lessThan("fovY", fovY, Math.PI); Check.typeOf.number.greaterThan("near", near, 0.0); Check.typeOf.number.greaterThan("far", far, 0.0); Check.typeOf.object("result", result); //>>includeEnd('debug'); const bottom = Math.tan(fovY * 0.5); const column1Row1 = 1.0 / bottom; const column0Row0 = column1Row1 / aspectRatio; const column2Row2 = (far + near) / (near - far); const column3Row2 = (2.0 * far * near) / (near - far); result[0] = column0Row0; result[1] = 0.0; result[2] = 0.0; result[3] = 0.0; result[4] = 0.0; result[5] = column1Row1; result[6] = 0.0; result[7] = 0.0; result[8] = 0.0; result[9] = 0.0; result[10] = column2Row2; result[11] = -1.0; result[12] = 0.0; result[13] = 0.0; result[14] = column3Row2; result[15] = 0.0; return result; } /** * Computes a Matrix4 instance representing an orthographic transformation matrix. * * @param {number} left The number of meters to the left of the camera that will be in view. * @param {number} right The number of meters to the right of the camera that will be in view. * @param {number} bottom The number of meters below of the camera that will be in view. * @param {number} top The number of meters above of the camera that will be in view. * @param {number} near The distance to the near plane in meters. * @param {number} far The distance to the far plane in meters. * @param {Matrix4} result The object in which the result will be stored. * @returns {Matrix4} The modified result parameter. */ static computeOrthographicOffCenter( left, right, bottom, top, near, far, result, ) { //>>includeStart('debug', pragmas.debug); Check.typeOf.number("left", left); Check.typeOf.number("right", right); Check.typeOf.number("bottom", bottom); Check.typeOf.number("top", top); Check.typeOf.number("near", near); Check.typeOf.number("far", far); Check.typeOf.object("result", result); //>>includeEnd('debug'); let a = 1.0 / (right - left); let b = 1.0 / (top - bottom); let c = 1.0 / (far - near); const tx = -(right + left) * a; const ty = -(top + bottom) * b; const tz = -(far + near) * c; a *= 2.0; b *= 2.0; c *= -2.0; result[0] = a; result[1] = 0.0; result[2] = 0.0; result[3] = 0.0; result[4] = 0.0; result[5] = b; result[6] = 0.0; result[7] = 0.0; result[8] = 0.0; result[9] = 0.0; result[10] = c; result[11] = 0.0; result[12] = tx; result[13] = ty; result[14] = tz; result[15] = 1.0; return result; } /** * Computes a Matrix4 instance representing an off center perspective transformation. * * @param {number} left The number of meters to the left of the camera that will be in view. * @param {number} right The number of meters to the right of the camera that will be in view. * @param {number} bottom The number of meters below the camera that will be in view. * @param {number} top The number of meters above the camera that will be in view. * @param {number} near The distance to the near plane in meters. * @param {number} far The distance to the far plane in meters. * @param {Matrix4} result The object in which the result will be stored. * @returns {Matrix4} The modified result parameter. */ static computePerspectiveOffCenter( left, right, bottom, top, near, far, result, ) { //>>includeStart('debug', pragmas.debug); Check.typeOf.number("left", left); Check.typeOf.number("right", right); Check.typeOf.number("bottom", bottom); Check.typeOf.number("top", top); Check.typeOf.number("near", near); Check.typeOf.number("far", far); Check.typeOf.object("result", result); //>>includeEnd('debug'); const column0Row0 = (2.0 * near) / (right - left); const column1Row1 = (2.0 * near) / (top - bottom); const column2Row0 = (right + left) / (right - left); const column2Row1 = (top + bottom) / (top - bottom); const column2Row2 = -(far + near) / (far - near); const column2Row3 = -1.0; const column3Row2 = (-2.0 * far * near) / (far - near); result[0] = column0Row0; result[1] = 0.0; result[2] = 0.0; result[3] = 0.0; result[4] = 0.0; result[5] = column1Row1; result[6] = 0.0; result[7] = 0.0; result[8] = column2Row0; result[9] = column2Row1; result[10] = column2Row2; result[11] = column2Row3; result[12] = 0.0; result[13] = 0.0; result[14] = column3Row2; result[15] = 0.0; return result; } /** * Computes a Matrix4 instance representing an infinite off center perspective transformation. * * @param {number} left The number of meters to the left of the camera that will be in view. * @param {number} right The number of meters to the right of the camera that will be in view. * @param {number} bottom The number of meters below of the camera that will be in view. * @param {number} top The number of meters above of the camera that will be in view. * @param {number} near The distance to the near plane in meters. * @param {Matrix4} result The object in which the result will be stored. * @returns {Matrix4} The modified result parameter. */ static computeInfinitePerspectiveOffCenter( left, right, bottom, top, near, result, ) { //>>includeStart('debug', pragmas.debug); Check.typeOf.number("left", left); Check.typeOf.number("right", right); Check.typeOf.number("bottom", bottom); Check.typeOf.number("top", top); Check.typeOf.number("near", near); Check.typeOf.object("result", result); //>>includeEnd('debug'); const column0Row0 = (2.0 * near) / (right - left); const column1Row1 = (2.0 * near) / (top - bottom); const column2Row0 = (right + left) / (right - left); const column2Row1 = (top + bottom) / (top - bottom); const column2Row2 = -1.0; const column2Row3 = -1.0; const column3Row2 = -2.0 * near; result[0] = column0Row0; result[1] = 0.0; result[2] = 0.0; result[3] = 0.0; result[4] = 0.0; result[5] = column1Row1; result[6] = 0.0; result[7] = 0.0; result[8] = column2Row0; result[9] = column2Row1; result[10] = column2Row2; result[11] = column2Row3; result[12] = 0.0; result[13] = 0.0; result[14] = column3Row2; result[15] = 0.0; return result; } /** * Computes a Matrix4 instance that transforms from normalized device coordinates to window coordinates. * * @param {Viewport} [viewport = { x : 0.0, y : 0.0, width : 0.0, height : 0.0 }] The viewport's corners as shown in Example 1. * @param {number} [nearDepthRange=0.0] The near plane distance in window coordinates. * @param {number} [farDepthRange=1.0] The far plane distance in window coordinates. * @param {Matrix4} [result] The object in which the result will be stored. * @returns {Matrix4} The modified result parameter. * * @example * // Create viewport transformation using an explicit viewport and depth range. * const m = Cesium.Matrix4.computeViewportTransformation({ * x : 0.0, * y : 0.0, * width : 1024.0, * height : 768.0 * }, 0.0, 1.0, new Cesium.Matrix4()); */ static computeViewportTransformation( viewport, nearDepthRange, farDepthRange, result, ) { if (!defined(result)) { result = new Matrix4(); } viewport = viewport ?? Frozen.EMPTY_OBJECT; const x = viewport.x ?? 0.0; const y = viewport.y ?? 0.0; const width = viewport.width ?? 0.0; const height = viewport.height ?? 0.0; nearDepthRange = nearDepthRange ?? 0.0; farDepthRange = farDepthRange ?? 1.0; const halfWidth = width * 0.5; const halfHeight = height * 0.5; const halfDepth = (farDepthRange - nearDepthRange) * 0.5; const column0Row0 = halfWidth; const column1Row1 = halfHeight; const column2Row2 = halfDepth; const column3Row0 = x + halfWidth; const column3Row1 = y + halfHeight; const column3Row2 = nearDepthRange + halfDepth; const column3Row3 = 1.0; result[0] = column0Row0; result[1] = 0.0; result[2] = 0.0; result[3] = 0.0; result[4] = 0.0; result[5] = column1Row1; result[6] = 0.0; result[7] = 0.0; result[8] = 0.0; result[9] = 0.0; result[10] = column2Row2; result[11] = 0.0; result[12] = column3Row0; result[13] = column3Row1; result[14] = column3Row2; result[15] = column3Row3; return result; } /** * Computes a Matrix4 instance that transforms from world space to view space. * * @param {Cartesian3} position The position of the camera. * @param {Cartesian3} direction The forward direction. * @param {Cartesian3} up The up direction. * @param {Cartesian3} right The right direction. * @param {Matrix4} result The object in which the result will be stored. * @returns {Matrix4} The modified result parameter. */ static computeView(position, direction, up, right, result) { //>>includeStart('debug', pragmas.debug); Check.typeOf.object("position", position); Check.typeOf.object("direction", direction); Check.typeOf.object("up", up); Check.typeOf.object("right", right); Check.typeOf.object("result", result); //>>includeEnd('debug'); result[0] = right.x; result[1] = up.x; result[2] = -direction.x; result[3] = 0.0; result[4] = right.y; result[5] = up.y; result[6] = -direction.y; result[7] = 0.0; result[8] = right.z; result[9] = up.z; result[10] = -direction.z; result[11] = 0.0; result[12] = -Cartesian3.dot(right, position); result[13] = -Cartesian3.dot(up, position); result[14] = Cartesian3.dot(direction, position); result[15] = 1.0; return result; } /** * Computes an Array from the provided Matrix4 instance. * The array will be in column-major order. * * @param {Matrix4} matrix The matrix to use.. * @param {number[]} [result] The Array onto which to store the result. * @returns {number[]} The modified Array parameter or a new Array instance if one was not provided. * * @example * //create an array from an instance of Matrix4 * // m = [10.0, 14.0, 18.0, 22.0] * // [11.0, 15.0, 19.0, 23.0] * // [12.0, 16.0, 20.0, 24.0] * // [13.0, 17.0, 21.0, 25.0] * const a = Cesium.Matrix4.toArray(m); * * // m remains the same * //creates a = [10.0, 11.0, 12.0, 13.0, 14.0, 15.0, 16.0, 17.0, 18.0, 19.0, 20.0, 21.0, 22.0, 23.0, 24.0, 25.0] */ static toArray(matrix, result) { //>>includeStart('debug', pragmas.debug); Check.typeOf.object("matrix", matrix); //>>includeEnd('debug'); if (!defined(result)) { return [ matrix[0], matrix[1], matrix[2], matrix[3], matrix[4], matrix[5], matrix[6], matrix[7], matrix[8], matrix[9], matrix[10], matrix[11], matrix[12], matrix[13], matrix[14], matrix[15], ]; } result[0] = matrix[0]; result[1] = matrix[1]; result[2] = matrix[2]; result[3] = matrix[3]; result[4] = matrix[4]; result[5] = matrix[5]; result[6] = matrix[6]; result[7] = matrix[7]; result[8] = matrix[8]; result[9] = matrix[9]; result[10] = matrix[10]; result[11] = matrix[11]; result[12] = matrix[12]; result[13] = matrix[13]; result[14] = matrix[14]; result[15] = matrix[15]; return result; } /** * Computes the array index of the element at the provided row and column. * * @param {number} row The zero-based index of the row. * @param {number} column The zero-based index of the column. * @returns {number} The index of the element at the provided row and column. * * @exception {DeveloperError} row must be 0, 1, 2, or 3. * @exception {DeveloperError} column must be 0, 1, 2, or 3. * * @example * const myMatrix = new Cesium.Matrix4(); * const column1Row0Index = Cesium.Matrix4.getElementIndex(1, 0); * const column1Row0 = myMatrix[column1Row0Index]; * myMatrix[column1Row0Index] = 10.0; */ static getElementIndex(column, row) { //>>includeStart('debug', pragmas.debug); Check.typeOf.number.greaterThanOrEquals("row", row, 0); Check.typeOf.number.lessThanOrEquals("row", row, 3); Check.typeOf.number.greaterThanOrEquals("column", column, 0); Check.typeOf.number.lessThanOrEquals("column", column, 3); //>>includeEnd('debug'); return column * 4 + row; } /** * Retrieves a copy of the matrix column at the provided index as a Cartesian4 instance. * * @param {Matrix4} matrix The matrix to use. * @param {number} index The zero-based index of the column to retrieve. * @param {Cartesian4} result The object onto which to store the result. * @returns {Cartesian4} The modified result parameter. * * @exception {DeveloperError} index must be 0, 1, 2, or 3. * * @example * //returns a Cartesian4 instance with values from the specified column * // m = [10.0, 11.0, 12.0, 13.0] * // [14.0, 15.0, 16.0, 17.0] * // [18.0, 19.0, 20.0, 21.0] * // [22.0, 23.0, 24.0, 25.0] * * //Example 1: Creates an instance of Cartesian * const a = Cesium.Matrix4.getColumn(m, 2, new Cesium.Cartesian4()); * * @example * //Example 2: Sets values for Cartesian instance * const a = new Cesium.Cartesian4(); * Cesium.Matrix4.getColumn(m, 2, a); * * // a.x = 12.0; a.y = 16.0; a.z = 20.0; a.w = 24.0; */ static getColumn(matrix, index, result) { //>>includeStart('debug', pragmas.debug); Check.typeOf.object("matrix", matrix); Check.typeOf.number.greaterThanOrEquals("index", index, 0); Check.typeOf.number.lessThanOrEquals("index", index, 3); Check.typeOf.object("result", result); //>>includeEnd('debug'); const startIndex = index * 4; // @ts-expect-error TODO(tsd-jsdoc): Requires index signature support. const x = matrix[startIndex]; // @ts-expect-error TODO(tsd-jsdoc): Requires index signature support. const y = matrix[startIndex + 1]; // @ts-expect-error TODO(tsd-jsdoc): Requires index signature support. const z = matrix[startIndex + 2]; // @ts-expect-error TODO(tsd-jsdoc): Requires index signature support. const w = matrix[startIndex + 3]; result.x = x; result.y = y; result.z = z; result.w = w; return result; } /** * Computes a new matrix that replaces the specified column in the provided matrix with the provided Cartesian4 instance. * * @param {Matrix4} matrix The matrix to use. * @param {number} index The zero-based index of the column to set. * @param {Cartesian4} cartesian The Cartesian whose values will be assigned to the specified column. * @param {Matrix4} result The object onto which to store the result. * @returns {Matrix4} The modified result parameter. * * @exception {DeveloperError} index must be 0, 1, 2, or 3. * * @example * //creates a new Matrix4 instance with new column values from the Cartesian4 instance * // m = [10.0, 11.0, 12.0, 13.0] * // [14.0, 15.0, 16.0, 17.0] * // [18.0, 19.0, 20.0, 21.0] * // [22.0, 23.0, 24.0, 25.0] * * const a = Cesium.Matrix4.setColumn(m, 2, new Cesium.Cartesian4(99.0, 98.0, 97.0, 96.0), new Cesium.Matrix4()); * * // m remains the same * // a = [10.0, 11.0, 99.0, 13.0] * // [14.0, 15.0, 98.0, 17.0] * // [18.0, 19.0, 97.0, 21.0] * // [22.0, 23.0, 96.0, 25.0] */ static setColumn(matrix, index, cartesian, result) { //>>includeStart('debug', pragmas.debug); Check.typeOf.object("matrix", matrix); Check.typeOf.number.greaterThanOrEquals("index", index, 0); Check.typeOf.number.lessThanOrEquals("index", index, 3); Check.typeOf.object("cartesian", cartesian); Check.typeOf.object("result", result); //>>includeEnd('debug'); result = Matrix4.clone(matrix, result); const startIndex = index * 4; // @ts-expect-error TODO(tsd-jsdoc): Requires index signature support. result[startIndex] = cartesian.x; // @ts-expect-error TODO(tsd-jsdoc): Requires index signature support. result[startIndex + 1] = cartesian.y; // @ts-expect-error TODO(tsd-jsdoc): Requires index signature support. result[startIndex + 2] = cartesian.z; // @ts-expect-error TODO(tsd-jsdoc): Requires index signature support. result[startIndex + 3] = cartesian.w; return result; } /** * Retrieves a copy of the matrix row at the provided index as a Cartesian4 instance. * * @param {Matrix4} matrix The matrix to use. * @param {number} index The zero-based index of the row to retrieve. * @param {Cartesian4} result The object onto which to store the result. * @returns {Cartesian4} The modified result parameter. * * @exception {DeveloperError} index must be 0, 1, 2, or 3. * * @example * //returns a Cartesian4 instance with values from the specified column * // m = [10.0, 11.0, 12.0, 13.0] * // [14.0, 15.0, 16.0, 17.0] * // [18.0, 19.0, 20.0, 21.0] * // [22.0, 23.0, 24.0, 25.0] * * //Example 1: Returns an instance of Cartesian * const a = Cesium.Matrix4.getRow(m, 2, new Cesium.Cartesian4()); * * @example * //Example 2: Sets values for a Cartesian instance * const a = new Cesium.Cartesian4(); * Cesium.Matrix4.getRow(m, 2, a); * * // a.x = 18.0; a.y = 19.0; a.z = 20.0; a.w = 21.0; */ static getRow(matrix, index, result) { //>>includeStart('debug', pragmas.debug); Check.typeOf.object("matrix", matrix); Check.typeOf.number.greaterThanOrEquals("index", index, 0); Check.typeOf.number.lessThanOrEquals("index", index, 3); Check.typeOf.object("result", result); //>>includeEnd('debug'); // @ts-expect-error TODO(tsd-jsdoc): Requires index signature support. const x = matrix[index]; // @ts-expect-error TODO(tsd-jsdoc): Requires index signature support. const y = matrix[index + 4]; // @ts-expect-error TODO(tsd-jsdoc): Requires index signature support. const z = matrix[index + 8]; // @ts-expect-error TODO(tsd-jsdoc): Requires index signature support. const w = matrix[index + 12]; result.x = x; result.y = y; result.z = z; result.w = w; return result; } /** * Computes a new matrix that replaces the specified row in the provided matrix with the provided Cartesian4 instance. * * @param {Matrix4} matrix The matrix to use. * @param {number} index The zero-based index of the row to set. * @param {Cartesian4} cartesian The Cartesian whose values will be assigned to the specified row. * @param {Matrix4} result The object onto which to store the result. * @returns {Matrix4} The modified result parameter. * * @exception {DeveloperError} index must be 0, 1, 2, or 3. * * @example * //create a new Matrix4 instance with new row values from the Cartesian4 instance * // m = [10.0, 11.0, 12.0, 13.0] * // [14.0, 15.0, 16.0, 17.0] * // [18.0, 19.0, 20.0, 21.0] * // [22.0, 23.0, 24.0, 25.0] * * const a = Cesium.Matrix4.setRow(m, 2, new Cesium.Cartesian4(99.0, 98.0, 97.0, 96.0), new Cesium.Matrix4()); * * // m remains the same * // a = [10.0, 11.0, 12.0, 13.0] * // [14.0, 15.0, 16.0, 17.0] * // [99.0, 98.0, 97.0, 96.0] * // [22.0, 23.0, 24.0, 25.0] */ static setRow(matrix, index, cartesian, result) { //>>includeStart('debug', pragmas.debug); Check.typeOf.object("matrix", matrix); Check.typeOf.number.greaterThanOrEquals("index", index, 0); Check.typeOf.number.lessThanOrEquals("index", index, 3); Check.typeOf.object("cartesian", cartesian); Check.typeOf.object("result", result); //>>includeEnd('debug'); result = Matrix4.clone(matrix, result); // @ts-expect-error TODO(tsd-jsdoc): Requires index signature support. result[index] = cartesian.x; // @ts-expect-error TODO(tsd-jsdoc): Requires index signature support. result[index + 4] = cartesian.y; // @ts-expect-error TODO(tsd-jsdoc): Requires index signature support. result[index + 8] = cartesian.z; // @ts-expect-error TODO(tsd-jsdoc): Requires index signature support. result[index + 12] = cartesian.w; return result; } /** * Computes a new matrix that replaces the translation in the rightmost column of the provided * matrix with the provided translation. This assumes the matrix is an affine transformation. * * @param {Matrix4} matrix The matrix to use. * @param {Cartesian3} translation The translation that replaces the translation of the provided matrix. * @param {Matrix4} result The object onto which to store the result. * @returns {Matrix4} The modified result parameter. */ static setTranslation(matrix, translation, result) { //>>includeStart('debug', pragmas.debug); Check.typeOf.object("matrix", matrix); Check.typeOf.object("translation", translation); Check.typeOf.object("result", result); //>>includeEnd('debug'); result[0] = matrix[0]; result[1] = matrix[1]; result[2] = matrix[2]; result[3] = matrix[3]; result[4] = matrix[4]; result[5] = matrix[5]; result[6] = matrix[6]; result[7] = matrix[7]; result[8] = matrix[8]; result[9] = matrix[9]; result[10] = matrix[10]; result[11] = matrix[11]; result[12] = translation.x; result[13] = translation.y; result[14] = translation.z; result[15] = matrix[15]; return result; } /** * Computes a new matrix that replaces the scale with the provided scale. * This assumes the matrix is an affine transformation. * * @param {Matrix4} matrix The matrix to use. * @param {Cartesian3} scale The scale that replaces the scale of the provided matrix. * @param {Matrix4} result The object onto which to store the result. * @returns {Matrix4} The modified result parameter. * * @see Matrix4.setUniformScale * @see Matrix4.fromScale * @see Matrix4.fromUniformScale * @see Matrix4.multiplyByScale * @see Matrix4.multiplyByUniformScale * @see Matrix4.getScale */ static setScale(matrix, scale, result) { //>>includeStart('debug', pragmas.debug); Check.typeOf.object("matrix", matrix); Check.typeOf.object("scale", scale); Check.typeOf.object("result", result); //>>includeEnd('debug'); const existingScale = Matrix4.getScale(matrix, scaleScratch1); const scaleRatioX = scale.x / existingScale.x; const scaleRatioY = scale.y / existingScale.y; const scaleRatioZ = scale.z / existingScale.z; result[0] = matrix[0] * scaleRatioX; result[1] = matrix[1] * scaleRatioX; result[2] = matrix[2] * scaleRatioX; result[3] = matrix[3]; result[4] = matrix[4] * scaleRatioY; result[5] = matrix[5] * scaleRatioY; result[6] = matrix[6] * scaleRatioY; result[7] = matrix[7]; result[8] = matrix[8] * scaleRatioZ; result[9] = matrix[9] * scaleRatioZ; result[10] = matrix[10] * scaleRatioZ; result[11] = matrix[11]; result[12] = matrix[12]; result[13] = matrix[13]; result[14] = matrix[14]; result[15] = matrix[15]; return result; } /** * Computes a new matrix that replaces the scale with the provided uniform scale. * This assumes the matrix is an affine transformation. * * @param {Matrix4} matrix The matrix to use. * @param {number} scale The uniform scale that replaces the scale of the provided matrix. * @param {Matrix4} result The object onto which to store the result. * @returns {Matrix4} The modified result parameter. * * @see Matrix4.setScale * @see Matrix4.fromScale * @see Matrix4.fromUniformScale * @see Matrix4.multiplyByScale * @see Matrix4.multiplyByUniformScale * @see Matrix4.getScale */ static setUniformScale(matrix, scale, result) { //>>includeStart('debug', pragmas.debug); Check.typeOf.object("matrix", matrix); Check.typeOf.number("scale", scale); Check.typeOf.object("result", result); //>>includeEnd('debug'); const existingScale = Matrix4.getScale(matrix, scaleScratch2); const scaleRat