/** * A 3D Cartesian point. * @alias Cartesian3 * @constructor * * @param {number} [x=0.0] The X component. * @param {number} [y=0.0] The Y component. * @param {number} [z=0.0] The Z component. * * @see Cartesian2 * @see Cartesian4 * @see Packable */ function Cartesian3(x, y, z) { /** * The X component. * @type {number} * @default 0.0 */ this.x = defaultValue(x, 0.0); /** * The Y component. * @type {number} * @default 0.0 */ this.y = defaultValue(y, 0.0); /** * The Z component. * @type {number} * @default 0.0 */ this.z = defaultValue(z, 0.0); } /** * Computes the provided Cartesian's squared magnitude. * * @param {Cartesian3} cartesian The Cartesian instance whose squared magnitude is to be computed. * @returns {number} The squared magnitude. */ Cartesian3.magnitudeSquared = function (cartesian) { return ( cartesian.x * cartesian.x + cartesian.y * cartesian.y + cartesian.z * cartesian.z ); }; /** * Computes the Cartesian's magnitude (length). * * @param {Cartesian3} cartesian The Cartesian instance whose magnitude is to be computed. * @returns {number} The magnitude. */ Cartesian3.magnitude = function (cartesian) { return Math.sqrt(Cartesian3.magnitudeSquared(cartesian)); }; const distanceScratch = new Cartesian3(); /** * Computes the distance between two points. * * @param {Cartesian3} left The first point to compute the distance from. * @param {Cartesian3} right The second point to compute the distance to. * @returns {number} The distance between two points. * * @example * // Returns 1.0 * const d = Cesium.Cartesian3.distance(new Cesium.Cartesian3(1.0, 0.0, 0.0), new Cesium.Cartesian3(2.0, 0.0, 0.0)); */ Cartesian3.distance = function (left, right) { Cartesian3.subtract(left, right, distanceScratch); return Cartesian3.magnitude(distanceScratch); }; /** * Computes the normalized form of the supplied Cartesian. * * @param {Cartesian3} cartesian The Cartesian to be normalized. * @param {Cartesian3} result The object onto which to store the result. * @returns {Cartesian3} The modified result parameter. */ Cartesian3.normalize = function (cartesian, result) { const magnitude = Cartesian3.magnitude(cartesian); result.x = cartesian.x / magnitude; result.y = cartesian.y / magnitude; result.z = cartesian.z / magnitude; return result; }; /** * Computes the dot (scalar) product of two Cartesians. * * @param {Cartesian3} left The first Cartesian. * @param {Cartesian3} right The second Cartesian. * @returns {number} The dot product. */ Cartesian3.dot = function (left, right) { return left.x * right.x + left.y * right.y + left.z * right.z; }; /** * Computes the componentwise sum of two Cartesians. * * @param {Cartesian3} left The first Cartesian. * @param {Cartesian3} right The second Cartesian. * @param {Cartesian3} result The object onto which to store the result. * @returns {Cartesian3} The modified result parameter. */ Cartesian3.add = function (left, right, result) { result.x = left.x + right.x; result.y = left.y + right.y; result.z = left.z + right.z; return result; }; /** * Computes the componentwise difference of two Cartesians. * * @param {Cartesian3} left The first Cartesian. * @param {Cartesian3} right The second Cartesian. * @param {Cartesian3} result The object onto which to store the result. * @returns {Cartesian3} The modified result parameter. */ Cartesian3.subtract = function (left, right, result) { result.x = left.x - right.x; result.y = left.y - right.y; result.z = left.z - right.z; return result; }; /** * Multiplies the provided Cartesian componentwise by the provided scalar. * * @param {Cartesian3} cartesian The Cartesian to be scaled. * @param {number} scalar The scalar to multiply with. * @param {Cartesian3} result The object onto which to store the result. * @returns {Cartesian3} The modified result parameter. */ Cartesian3.multiplyByScalar = function (cartesian, scalar, result) { result.x = cartesian.x * scalar; result.y = cartesian.y * scalar; result.z = cartesian.z * scalar; return result; }; /** * Computes the cross (outer) product of two Cartesians. * * @param {Cartesian3} left The first Cartesian. * @param {Cartesian3} right The second Cartesian. * @param {Cartesian3} result The object onto which to store the result. * @returns {Cartesian3} The cross product. */ Cartesian3.cross = function (left, right, result) { const leftX = left.x; const leftY = left.y; const leftZ = left.z; const rightX = right.x; const rightY = right.y; const rightZ = right.z; const x = leftY * rightZ - leftZ * rightY; const y = leftZ * rightX - leftX * rightZ; const z = leftX * rightY - leftY * rightX; result.x = x; result.y = y; result.z = z; return result; }; function defaultValue(a, b) { if (a !== undefined && a !== null) { return a; } return b; }