All files / engine/Source/Core Cartographic.js

98.57% Statements 69/70
94.73% Branches 36/38
100% Functions 12/12
98.57% Lines 69/70

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305                                              43462x             43462x             43462x                         1x   24056x 24055x     24054x   24054x 281x     23773x 23773x 23773x 23773x                           1x   295x 294x     293x 293x   293x     1x 1x 1x     1x         1x         1x                     1x 838x     838x     838x         838x               837x       837x         837x   837x   837x 837x   837x   837x 415x   422x 422x 422x 422x                       1x   2328x     2328x                               1x 211654x 1x   211653x 17x           211636x 211636x 211636x 211636x                     1x 82x                                       1x 142x   142x                               1x               1x 13x                   1x 82x                       1x 36x               1x 129x      
import Cartesian3 from "./Cartesian3.js";
import Check from "./Check.js";
import defined from "./defined.js";
import CesiumMath from "./Math.js";
import scaleToGeodeticSurface from "./scaleToGeodeticSurface.js";
 
/**
 * A position defined by longitude, latitude, and height.
 * @alias Cartographic
 * @constructor
 *
 * @param {number} [longitude=0.0] The longitude, in radians.
 * @param {number} [latitude=0.0] The latitude, in radians.
 * @param {number} [height=0.0] The height, in meters, above the ellipsoid.
 *
 * @see Ellipsoid
 */
function Cartographic(longitude, latitude, height) {
  /**
   * The longitude, in radians.
   * @type {number}
   * @default 0.0
   */
  this.longitude = longitude ?? 0.0;
 
  /**
   * The latitude, in radians.
   * @type {number}
   * @default 0.0
   */
  this.latitude = latitude ?? 0.0;
 
  /**
   * The height, in meters, above the ellipsoid.
   * @type {number}
   * @default 0.0
   */
  this.height = height ?? 0.0;
}
 
/**
 * Creates a new Cartographic instance from longitude and latitude
 * specified in radians.
 *
 * @param {number} longitude The longitude, in radians.
 * @param {number} latitude The latitude, in radians.
 * @param {number} [height=0.0] The height, in meters, above the ellipsoid.
 * @param {Cartographic} [result] The object onto which to store the result.
 * @returns {Cartographic} The modified result parameter or a new Cartographic instance if one was not provided.
 */
Cartographic.fromRadians = function (longitude, latitude, height, result) {
  //>>includeStart('debug', pragmas.debug);
  Check.typeOf.number("longitude", longitude);
  Check.typeOf.number("latitude", latitude);
  //>>includeEnd('debug');
 
  height = height ?? 0.0;
 
  if (!defined(result)) {
    return new Cartographic(longitude, latitude, height);
  }
 
  result.longitude = longitude;
  result.latitude = latitude;
  result.height = height;
  return result;
};
 
/**
 * Creates a new Cartographic instance from longitude and latitude
 * specified in degrees.  The values in the resulting object will
 * be in radians.
 *
 * @param {number} longitude The longitude, in degrees.
 * @param {number} latitude The latitude, in degrees.
 * @param {number} [height=0.0] The height, in meters, above the ellipsoid.
 * @param {Cartographic} [result] The object onto which to store the result.
 * @returns {Cartographic} The modified result parameter or a new Cartographic instance if one was not provided.
 */
Cartographic.fromDegrees = function (longitude, latitude, height, result) {
  //>>includeStart('debug', pragmas.debug);
  Check.typeOf.number("longitude", longitude);
  Check.typeOf.number("latitude", latitude);
  //>>includeEnd('debug');
 
  longitude = CesiumMath.toRadians(longitude);
  latitude = CesiumMath.toRadians(latitude);
 
  return Cartographic.fromRadians(longitude, latitude, height, result);
};
 
const cartesianToCartographicN = new Cartesian3();
const cartesianToCartographicP = new Cartesian3();
const cartesianToCartographicH = new Cartesian3();
 
// To avoid circular dependencies, these are set by Ellipsoid when Ellipsoid.default is set.
Cartographic._ellipsoidOneOverRadii = new Cartesian3(
  1.0 / 6378137.0,
  1.0 / 6378137.0,
  1.0 / 6356752.3142451793,
);
Cartographic._ellipsoidOneOverRadiiSquared = new Cartesian3(
  1.0 / (6378137.0 * 6378137.0),
  1.0 / (6378137.0 * 6378137.0),
  1.0 / (6356752.3142451793 * 6356752.3142451793),
);
Cartographic._ellipsoidCenterToleranceSquared = CesiumMath.EPSILON1;
 
/**
 * Creates a new Cartographic instance from a Cartesian position. The values in the
 * resulting object will be in radians.
 *
 * @param {Cartesian3} cartesian The Cartesian position to convert to cartographic representation.
 * @param {Ellipsoid} [ellipsoid=Ellipsoid.default] The ellipsoid on which the position lies.
 * @param {Cartographic} [result] The object onto which to store the result.
 * @returns {Cartographic} The modified result parameter, new Cartographic instance if none was provided, or undefined if the cartesian is at the center of the ellipsoid.
 */
Cartographic.fromCartesian = function (cartesian, ellipsoid, result) {
  const oneOverRadii = defined(ellipsoid)
    ? ellipsoid.oneOverRadii
    : Cartographic._ellipsoidOneOverRadii;
  const oneOverRadiiSquared = defined(ellipsoid)
    ? ellipsoid.oneOverRadiiSquared
    : Cartographic._ellipsoidOneOverRadiiSquared;
  const centerToleranceSquared = defined(ellipsoid)
    ? ellipsoid._centerToleranceSquared
    : Cartographic._ellipsoidCenterToleranceSquared;
 
  //`cartesian is required.` is thrown from scaleToGeodeticSurface
  const p = scaleToGeodeticSurface(
    cartesian,
    oneOverRadii,
    oneOverRadiiSquared,
    centerToleranceSquared,
    cartesianToCartographicP,
  );
 
  Iif (!defined(p)) {
    return undefined;
  }
 
  let n = Cartesian3.multiplyComponents(
    p,
    oneOverRadiiSquared,
    cartesianToCartographicN,
  );
  n = Cartesian3.normalize(n, n);
 
  const h = Cartesian3.subtract(cartesian, p, cartesianToCartographicH);
 
  const longitude = Math.atan2(n.y, n.x);
  const latitude = Math.asin(n.z);
  const height =
    CesiumMath.sign(Cartesian3.dot(h, cartesian)) * Cartesian3.magnitude(h);
 
  if (!defined(result)) {
    return new Cartographic(longitude, latitude, height);
  }
  result.longitude = longitude;
  result.latitude = latitude;
  result.height = height;
  return result;
};
 
/**
 * Creates a new Cartesian3 instance from a Cartographic input. The values in the inputted
 * object should be in radians.
 *
 * @param {Cartographic} cartographic Input to be converted into a Cartesian3 output.
 * @param {Ellipsoid} [ellipsoid=Ellipsoid.default] The ellipsoid on which the position lies.
 * @param {Cartesian3} [result] The object onto which to store the result.
 * @returns {Cartesian3} The position
 */
Cartographic.toCartesian = function (cartographic, ellipsoid, result) {
  //>>includeStart('debug', pragmas.debug);
  Check.defined("cartographic", cartographic);
  //>>includeEnd('debug');
 
  return Cartesian3.fromRadians(
    cartographic.longitude,
    cartographic.latitude,
    cartographic.height,
    ellipsoid,
    result,
  );
};
 
/**
 * Duplicates a Cartographic instance.
 *
 * @param {Cartographic} cartographic The cartographic to duplicate.
 * @param {Cartographic} [result] The object onto which to store the result.
 * @returns {Cartographic} The modified result parameter or a new Cartographic instance if one was not provided. (Returns undefined if cartographic is undefined)
 */
Cartographic.clone = function (cartographic, result) {
  if (!defined(cartographic)) {
    return undefined;
  }
  if (!defined(result)) {
    return new Cartographic(
      cartographic.longitude,
      cartographic.latitude,
      cartographic.height,
    );
  }
  result.longitude = cartographic.longitude;
  result.latitude = cartographic.latitude;
  result.height = cartographic.height;
  return result;
};
 
/**
 * Compares the provided cartographics componentwise and returns
 * <code>true</code> if they are equal, <code>false</code> otherwise.
 *
 * @param {Cartographic} [left] The first cartographic.
 * @param {Cartographic} [right] The second cartographic.
 * @returns {boolean} <code>true</code> if left and right are equal, <code>false</code> otherwise.
 */
Cartographic.equals = function (left, right) {
  return (
    left === right ||
    (defined(left) &&
      defined(right) &&
      left.longitude === right.longitude &&
      left.latitude === right.latitude &&
      left.height === right.height)
  );
};
 
/**
 * Compares the provided cartographics componentwise and returns
 * <code>true</code> if they are within the provided epsilon,
 * <code>false</code> otherwise.
 *
 * @param {Cartographic} [left] The first cartographic.
 * @param {Cartographic} [right] The second cartographic.
 * @param {number} [epsilon=0] The epsilon to use for equality testing.
 * @returns {boolean} <code>true</code> if left and right are within the provided epsilon, <code>false</code> otherwise.
 */
Cartographic.equalsEpsilon = function (left, right, epsilon) {
  epsilon = epsilon ?? 0;
 
  return (
    left === right ||
    (defined(left) &&
      defined(right) &&
      Math.abs(left.longitude - right.longitude) <= epsilon &&
      Math.abs(left.latitude - right.latitude) <= epsilon &&
      Math.abs(left.height - right.height) <= epsilon)
  );
};
 
/**
 * An immutable Cartographic instance initialized to (0.0, 0.0, 0.0).
 *
 * @type {Cartographic}
 * @constant
 */
Cartographic.ZERO = Object.freeze(new Cartographic(0.0, 0.0, 0.0));
 
/**
 * Duplicates this instance.
 *
 * @param {Cartographic} [result] The object onto which to store the result.
 * @returns {Cartographic} The modified result parameter or a new Cartographic instance if one was not provided.
 */
Cartographic.prototype.clone = function (result) {
  return Cartographic.clone(this, result);
};
 
/**
 * Compares the provided against this cartographic componentwise and returns
 * <code>true</code> if they are equal, <code>false</code> otherwise.
 *
 * @param {Cartographic} [right] The second cartographic.
 * @returns {boolean} <code>true</code> if left and right are equal, <code>false</code> otherwise.
 */
Cartographic.prototype.equals = function (right) {
  return Cartographic.equals(this, right);
};
 
/**
 * Compares the provided against this cartographic componentwise and returns
 * <code>true</code> if they are within the provided epsilon,
 * <code>false</code> otherwise.
 *
 * @param {Cartographic} [right] The second cartographic.
 * @param {number} [epsilon=0] The epsilon to use for equality testing.
 * @returns {boolean} <code>true</code> if left and right are within the provided epsilon, <code>false</code> otherwise.
 */
Cartographic.prototype.equalsEpsilon = function (right, epsilon) {
  return Cartographic.equalsEpsilon(this, right, epsilon);
};
 
/**
 * Creates a string representing this cartographic in the format '(longitude, latitude, height)'.
 *
 * @returns {string} A string representing the provided cartographic in the format '(longitude, latitude, height)'.
 */
Cartographic.prototype.toString = function () {
  return `(${this.longitude}, ${this.latitude}, ${this.height})`;
};
export default Cartographic;