GeodeticPoint.java

  1. /* Copyright 2002-2018 CS Systèmes d'Information
  2.  * Licensed to CS Systèmes d'Information (CS) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * CS licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *   http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.orekit.bodies;

  18. import java.io.Serializable;
  19. import java.text.NumberFormat;

  20. import org.hipparchus.geometry.euclidean.threed.Vector3D;
  21. import org.hipparchus.util.CompositeFormat;
  22. import org.hipparchus.util.FastMath;
  23. import org.hipparchus.util.MathUtils;

  24. /** Point location relative to a 2D body surface.
  25.  * <p>Instance of this class are guaranteed to be immutable.</p>
  26.  * @see BodyShape
  27.  * @see FieldGeodeticPoint
  28.  * @author Luc Maisonobe
  29.  */
  30. public class GeodeticPoint implements Serializable {

  31.     /** Serializable UID. */
  32.     private static final long serialVersionUID = 7862466825590075399L;

  33.     /** Latitude of the point (rad). */
  34.     private final double latitude;

  35.     /** Longitude of the point (rad). */
  36.     private final double longitude;

  37.     /** Altitude of the point (m). */
  38.     private final double altitude;

  39.     /** Zenith direction. */
  40.     private transient Vector3D zenith;

  41.     /** Nadir direction. */
  42.     private transient Vector3D nadir;

  43.     /** North direction. */
  44.     private transient Vector3D north;

  45.     /** South direction. */
  46.     private transient Vector3D south;

  47.     /** East direction. */
  48.     private transient Vector3D east;

  49.     /** West direction. */
  50.     private transient Vector3D west;

  51.     /**
  52.      * Build a new instance. The angular coordinates will be normalized so that
  53.      * the latitude is between ±π/2 and the longitude is between ±π.
  54.      *
  55.      * @param latitude latitude of the point
  56.      * @param longitude longitude of the point
  57.      * @param altitude altitude of the point
  58.      */
  59.     public GeodeticPoint(final double latitude, final double longitude,
  60.                          final double altitude) {
  61.         double lat = MathUtils.normalizeAngle(latitude, FastMath.PI / 2);
  62.         double lon = MathUtils.normalizeAngle(longitude, 0);
  63.         if (lat > FastMath.PI / 2.0) {
  64.             // latitude is beyond the pole -> add 180 to longitude
  65.             lat = FastMath.PI - lat;
  66.             lon = MathUtils.normalizeAngle(longitude + FastMath.PI, 0);
  67.         }
  68.         this.latitude  = lat;
  69.         this.longitude = lon;
  70.         this.altitude  = altitude;
  71.     }

  72.     /** Get the latitude.
  73.      * @return latitude, an angular value in the range [-π/2, π/2]
  74.      */
  75.     public double getLatitude() {
  76.         return latitude;
  77.     }

  78.     /** Get the longitude.
  79.      * @return longitude, an angular value in the range [-π, π]
  80.      */
  81.     public double getLongitude() {
  82.         return longitude;
  83.     }

  84.     /** Get the altitude.
  85.      * @return altitude
  86.      */
  87.     public double getAltitude() {
  88.         return altitude;
  89.     }

  90.     /** Get the direction above the point, expressed in parent shape frame.
  91.      * <p>The zenith direction is defined as the normal to local horizontal plane.</p>
  92.      * @return unit vector in the zenith direction
  93.      * @see #getNadir()
  94.      */
  95.     public Vector3D getZenith() {
  96.         if (zenith == null) {
  97.             final double cosLat = FastMath.cos(latitude);
  98.             final double sinLat = FastMath.sin(latitude);
  99.             final double cosLon = FastMath.cos(longitude);
  100.             final double sinLon = FastMath.sin(longitude);
  101.             zenith = new Vector3D(cosLon * cosLat, sinLon * cosLat, sinLat);
  102.         }
  103.         return zenith;
  104.     }

  105.     /** Get the direction below the point, expressed in parent shape frame.
  106.      * <p>The nadir direction is the opposite of zenith direction.</p>
  107.      * @return unit vector in the nadir direction
  108.      * @see #getZenith()
  109.      */
  110.     public Vector3D getNadir() {
  111.         if (nadir == null) {
  112.             nadir = getZenith().negate();
  113.         }
  114.         return nadir;
  115.     }

  116.     /** Get the direction to the north of point, expressed in parent shape frame.
  117.      * <p>The north direction is defined in the horizontal plane
  118.      * (normal to zenith direction) and following the local meridian.</p>
  119.      * @return unit vector in the north direction
  120.      * @see #getSouth()
  121.      */
  122.     public Vector3D getNorth() {
  123.         if (north == null) {
  124.             final double cosLat = FastMath.cos(latitude);
  125.             final double sinLat = FastMath.sin(latitude);
  126.             final double cosLon = FastMath.cos(longitude);
  127.             final double sinLon = FastMath.sin(longitude);
  128.             north = new Vector3D(-cosLon * sinLat, -sinLon * sinLat, cosLat);
  129.         }
  130.         return north;
  131.     }

  132.     /** Get the direction to the south of point, expressed in parent shape frame.
  133.      * <p>The south direction is the opposite of north direction.</p>
  134.      * @return unit vector in the south direction
  135.      * @see #getNorth()
  136.      */
  137.     public Vector3D getSouth() {
  138.         if (south == null) {
  139.             south = getNorth().negate();
  140.         }
  141.         return south;
  142.     }

  143.     /** Get the direction to the east of point, expressed in parent shape frame.
  144.      * <p>The east direction is defined in the horizontal plane
  145.      * in order to complete direct triangle (east, north, zenith).</p>
  146.      * @return unit vector in the east direction
  147.      * @see #getWest()
  148.      */
  149.     public Vector3D getEast() {
  150.         if (east == null) {
  151.             east = new Vector3D(-FastMath.sin(longitude), FastMath.cos(longitude), 0);
  152.         }
  153.         return east;
  154.     }

  155.     /** Get the direction to the west of point, expressed in parent shape frame.
  156.      * <p>The west direction is the opposite of east direction.</p>
  157.      * @return unit vector in the west direction
  158.      * @see #getEast()
  159.      */
  160.     public Vector3D getWest() {
  161.         if (west == null) {
  162.             west = getEast().negate();
  163.         }
  164.         return west;
  165.     }

  166.     @Override
  167.     public boolean equals(final Object object) {
  168.         if (object instanceof GeodeticPoint) {
  169.             final GeodeticPoint other = (GeodeticPoint) object;
  170.             return this.getLatitude() == other.getLatitude() &&
  171.                    this.getLongitude() == other.getLongitude() &&
  172.                    this.getAltitude() == other.getAltitude();
  173.         }
  174.         return false;
  175.     }

  176.     @Override
  177.     public int hashCode() {
  178.         return new Double(this.getLatitude()).hashCode() ^
  179.                new Double(this.getLongitude()).hashCode() ^
  180.                new Double(this.getAltitude()).hashCode();
  181.     }

  182.     @Override
  183.     public String toString() {
  184.         final NumberFormat format = CompositeFormat.getDefaultNumberFormat();
  185.         return "{lat: " +
  186.                format.format(FastMath.toDegrees(this.getLatitude())) +
  187.                " deg, lon: " +
  188.                format.format(FastMath.toDegrees(this.getLongitude())) +
  189.                " deg, alt: " +
  190.                format.format(this.getAltitude()) +
  191.                "}";
  192.     }
  193. }