All files / engine/Source/Core Stereographic.js

98.68% Statements 75/76
94.44% Branches 17/18
100% Functions 10/10
98.66% Lines 74/75

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                                      33x 33x 6x     33x 33x 6x       1x               940x                     1499x                     1499x                     472x 472x 472x 472x                     816x 816x 180x     816x         1x 1x               1x 468x 22x     468x 468x 468x 468x       468x 468x     1x 1x 1x                 1x   446x     446x 446x 446x 446x 140x 140x     446x 446x       446x         446x   446x         446x 446x 446x   446x 23x     423x 423x 423x                   1x   1x     1x 1x 1x       1x 2x   1x                   1x 382x 1x   381x 1x           380x 380x   380x                 1x   1x 1x   1x           1x                
import Cartesian2 from "./Cartesian2.js";
import Cartesian3 from "./Cartesian3.js";
import Cartographic from "./Cartographic.js";
import Check from "./Check.js";
import defined from "./defined.js";
import Ellipsoid from "./Ellipsoid.js";
import EllipsoidTangentPlane from "./EllipsoidTangentPlane.js";
import IntersectionTests from "./IntersectionTests.js";
import CesiumMath from "./Math.js";
import Ray from "./Ray.js";
 
/**
 * Represents a point in stereographic coordinates, which can be obtained by projecting a cartesian coordinate from one pole onto a tangent plane at the other pole.
 * The stereographic projection faithfully represents the relative directions of all great circles passing through its center point.
 * To faithfully represents angles everywhere, this is a conformal projection, which means points are projected onto an arbrary sphere.
 * @param {Cartesian2} [position] The steroegraphic coordinates.
 * @param {EllipseGeometry} [tangentPlane] The tangent plane onto which the point was projected.
 */
function Stereographic(position, tangentPlane) {
  this.position = position;
  if (!defined(this.position)) {
    this.position = new Cartesian2();
  }
 
  this.tangentPlane = tangentPlane;
  if (!defined(this.tangentPlane)) {
    this.tangentPlane = Stereographic.NORTH_POLE_TANGENT_PLANE;
  }
}
 
Object.defineProperties(Stereographic.prototype, {
  /**
   * Gets the ellipsoid.
   * @memberof Stereographic.prototype
   * @type {Ellipsoid}
   */
  ellipsoid: {
    get: function () {
      return this.tangentPlane.ellipsoid;
    },
  },
 
  /**
   * Gets the x coordinate
   * @memberof Stereographic.prototype
   * @type {number}
   */
  x: {
    get: function () {
      return this.position.x;
    },
  },
 
  /**
   * Gets the y coordinate
   * @memberof Stereographic.prototype
   * @type {number}
   */
  y: {
    get: function () {
      return this.position.y;
    },
  },
 
  /**
   * Computes the conformal latitude, or the ellipsoidal latitude projected onto an arbitrary sphere.
   * @memberof Stereographic.prototype
   * @type {number}
   */
  conformalLatitude: {
    get: function () {
      const r = Cartesian2.magnitude(this.position);
      const d = 2 * this.ellipsoid.maximumRadius;
      const sign = this.tangentPlane.plane.normal.z;
      return sign * (CesiumMath.PI_OVER_TWO - 2 * Math.atan2(r, d));
    },
  },
 
  /**
   * Computes the longitude
   * @memberof Stereographic.prototype
   * @type {number}
   */
  longitude: {
    get: function () {
      let longitude = CesiumMath.PI_OVER_TWO + Math.atan2(this.y, this.x);
      if (longitude > Math.PI) {
        longitude -= CesiumMath.TWO_PI;
      }
 
      return longitude;
    },
  },
});
 
const scratchCartographic = new Cartographic();
const scratchCartesian = new Cartesian3();
 
/**
 * Computes the latitude based on an ellipsoid.
 *
 * @param {Ellipsoid} [ellipsoid=Ellipsoid.default] The ellipsoid on which to compute the longitude.
 * @returns {number} The latitude
 */
Stereographic.prototype.getLatitude = function (ellipsoid) {
  if (!defined(ellipsoid)) {
    ellipsoid = Ellipsoid.default;
  }
 
  scratchCartographic.latitude = this.conformalLatitude;
  scratchCartographic.longitude = this.longitude;
  scratchCartographic.height = 0.0;
  const cartesian = this.ellipsoid.cartographicToCartesian(
    scratchCartographic,
    scratchCartesian,
  );
  ellipsoid.cartesianToCartographic(cartesian, scratchCartographic);
  return scratchCartographic.latitude;
};
 
const scratchProjectPointOntoPlaneRay = new Ray();
const scratchProjectPointOntoPlaneRayDirection = new Cartesian3();
const scratchProjectPointOntoPlaneCartesian3 = new Cartesian3();
 
/**
 * Computes the projection of the provided 3D position onto the 2D polar plane, radially outward from the provided origin.
 *
 * @param {Cartesian3} cartesian The point to project.
 * @param {Stereographic} [result] The object onto which to store the result.
 * @returns {Sterographic} The modified result parameter or a new Sterographic instance if none was provided.
 */
Stereographic.fromCartesian = function (cartesian, result) {
  //>>includeStart('debug', pragmas.debug);
  Check.defined("cartesian", cartesian);
  //>>includeEnd('debug');
 
  const sign = CesiumMath.signNotZero(cartesian.z);
  let tangentPlane = Stereographic.NORTH_POLE_TANGENT_PLANE;
  let origin = Stereographic.SOUTH_POLE;
  if (sign < 0) {
    tangentPlane = Stereographic.SOUTH_POLE_TANGENT_PLANE;
    origin = Stereographic.NORTH_POLE;
  }
 
  const ray = scratchProjectPointOntoPlaneRay;
  ray.origin = tangentPlane.ellipsoid.scaleToGeocentricSurface(
    cartesian,
    ray.origin,
  );
  ray.direction = Cartesian3.subtract(
    ray.origin,
    origin,
    scratchProjectPointOntoPlaneRayDirection,
  );
  Cartesian3.normalize(ray.direction, ray.direction);
 
  const intersectionPoint = IntersectionTests.rayPlane(
    ray,
    tangentPlane.plane,
    scratchProjectPointOntoPlaneCartesian3,
  );
  const v = Cartesian3.subtract(intersectionPoint, origin, intersectionPoint);
  const x = Cartesian3.dot(tangentPlane.xAxis, v);
  const y = sign * Cartesian3.dot(tangentPlane.yAxis, v);
 
  if (!defined(result)) {
    return new Stereographic(new Cartesian2(x, y), tangentPlane);
  }
 
  result.position = new Cartesian2(x, y);
  result.tangentPlane = tangentPlane;
  return result;
};
 
/**
 * Computes the projection of the provided 3D positions onto the 2D polar plane, radially outward from the provided origin.
 *
 * @param {Cartesian3[]} cartesians The points to project.
 * @param {Stereographic[]} [result] The object onto which to store the result.
 * @returns {Sterographic[]} The modified result parameter or a new Sterographic instance if none was provided.
 */
Stereographic.fromCartesianArray = function (cartesians, result) {
  //>>includeStart('debug', pragmas.debug);
  Check.defined("cartesians", cartesians);
  //>>includeEnd('debug');
 
  const length = cartesians.length;
  if (!defined(result)) {
    result = new Array(length);
  } else E{
    result.length = length;
  }
  for (let i = 0; i < length; i++) {
    result[i] = Stereographic.fromCartesian(cartesians[i], result[i]);
  }
  return result;
};
 
/**
 * Duplicates a Stereographic instance.
 *
 * @param {Stereographic} stereographic The Stereographic to duplicate.
 * @param {Stereographic} [result] The object onto which to store the result.
 * @returns {Stereographic} The modified result parameter or a new Stereographic instance if one was not provided. (Returns undefined if stereographic is undefined)
 */
Stereographic.clone = function (stereographic, result) {
  if (!defined(stereographic)) {
    return undefined;
  }
  if (!defined(result)) {
    return new Stereographic(
      stereographic.position,
      stereographic.tangentPlane,
    );
  }
 
  result.position = stereographic.position;
  result.tangentPlane = stereographic.tangentPlane;
 
  return result;
};
 
/**
 * An Ellipsoid instance initialized to radii of (0.5, 0.5, 0.5).
 *
 * @type {Stereographic}
 * @constant
 */
Stereographic.HALF_UNIT_SPHERE = Object.freeze(new Ellipsoid(0.5, 0.5, 0.5));
 
Stereographic.NORTH_POLE = Object.freeze(new Cartesian3(0.0, 0.0, 0.5));
Stereographic.SOUTH_POLE = Object.freeze(new Cartesian3(0.0, 0.0, -0.5));
 
Stereographic.NORTH_POLE_TANGENT_PLANE = Object.freeze(
  new EllipsoidTangentPlane(
    Stereographic.NORTH_POLE,
    Stereographic.HALF_UNIT_SPHERE,
  ),
);
Stereographic.SOUTH_POLE_TANGENT_PLANE = Object.freeze(
  new EllipsoidTangentPlane(
    Stereographic.SOUTH_POLE,
    Stereographic.HALF_UNIT_SPHERE,
  ),
);
 
export default Stereographic;