1   /* Copyright 2002-2024 CS GROUP
2    * Licensed to CS GROUP (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  
19  import java.io.Serializable;
20  import java.text.NumberFormat;
21  
22  import org.hipparchus.geometry.euclidean.threed.Vector3D;
23  import org.hipparchus.util.CompositeFormat;
24  import org.hipparchus.util.FastMath;
25  import org.hipparchus.util.MathUtils;
26  import org.hipparchus.util.SinCos;
27  
28  /** Point location relative to a 2D body surface.
29   * <p>Instance of this class are guaranteed to be immutable.</p>
30   * @see BodyShape
31   * @see FieldGeodeticPoint
32   * @author Luc Maisonobe
33   */
34  public class GeodeticPoint implements Serializable {
35  
36      /** North pole.
37       * @since 10.0
38       */
39      public static final GeodeticPoint NORTH_POLE = new GeodeticPoint(+0.5 * FastMath.PI, 0.0, 0.0);
40  
41      /** South pole.
42       * @since 10.0
43       */
44      public static final GeodeticPoint SOUTH_POLE = new GeodeticPoint(-0.5 * FastMath.PI, 0.0, 0.0);
45  
46      /** Serializable UID. */
47      private static final long serialVersionUID = 7862466825590075399L;
48  
49      /** Latitude of the point (rad). */
50      private final double latitude;
51  
52      /** Longitude of the point (rad). */
53      private final double longitude;
54  
55      /** Altitude of the point (m). */
56      private final double altitude;
57  
58      /** Zenith direction. */
59      private transient Vector3D zenith;
60  
61      /** Nadir direction. */
62      private transient Vector3D nadir;
63  
64      /** North direction. */
65      private transient Vector3D north;
66  
67      /** South direction. */
68      private transient Vector3D south;
69  
70      /** East direction. */
71      private transient Vector3D east;
72  
73      /** West direction. */
74      private transient Vector3D west;
75  
76      /**
77       * Build a new instance. The angular coordinates will be normalized so that
78       * the latitude is between ±π/2 and the longitude is between ±π.
79       *
80       * @param latitude latitude of the point (rad)
81       * @param longitude longitude of the point (rad)
82       * @param altitude altitude of the point (m)
83       */
84      public GeodeticPoint(final double latitude, final double longitude,
85                           final double altitude) {
86          double lat = MathUtils.normalizeAngle(latitude, FastMath.PI / 2);
87          double lon = MathUtils.normalizeAngle(longitude, 0);
88          if (lat > FastMath.PI / 2.0) {
89              // latitude is beyond the pole -> add 180 to longitude
90              lat = FastMath.PI - lat;
91              lon = MathUtils.normalizeAngle(longitude + FastMath.PI, 0);
92          }
93          this.latitude  = lat;
94          this.longitude = lon;
95          this.altitude  = altitude;
96      }
97  
98      /** Get the latitude.
99       * @return latitude, an angular value in the range [-π/2, π/2]
100      */
101     public double getLatitude() {
102         return latitude;
103     }
104 
105     /** Get the longitude.
106      * @return longitude, an angular value in the range [-π, π]
107      */
108     public double getLongitude() {
109         return longitude;
110     }
111 
112     /** Get the altitude.
113      * @return altitude
114      */
115     public double getAltitude() {
116         return altitude;
117     }
118 
119     /** Get the direction above the point, expressed in parent shape frame.
120      * <p>The zenith direction is defined as the normal to local horizontal plane.</p>
121      * @return unit vector in the zenith direction
122      * @see #getNadir()
123      */
124     public Vector3D getZenith() {
125         if (zenith == null) {
126             final SinCos scLat = FastMath.sinCos(latitude);
127             final SinCos scLon = FastMath.sinCos(longitude);
128             zenith = new Vector3D(scLon.cos() * scLat.cos(), scLon.sin() * scLat.cos(), scLat.sin());
129         }
130         return zenith;
131     }
132 
133     /** Get the direction below the point, expressed in parent shape frame.
134      * <p>The nadir direction is the opposite of zenith direction.</p>
135      * @return unit vector in the nadir direction
136      * @see #getZenith()
137      */
138     public Vector3D getNadir() {
139         if (nadir == null) {
140             nadir = getZenith().negate();
141         }
142         return nadir;
143     }
144 
145     /** Get the direction to the north of point, expressed in parent shape frame.
146      * <p>The north direction is defined in the horizontal plane
147      * (normal to zenith direction) and following the local meridian.</p>
148      * @return unit vector in the north direction
149      * @see #getSouth()
150      */
151     public Vector3D getNorth() {
152         if (north == null) {
153             final SinCos scLat = FastMath.sinCos(latitude);
154             final SinCos scLon = FastMath.sinCos(longitude);
155             north = new Vector3D(-scLon.cos() * scLat.sin(), -scLon.sin() * scLat.sin(), scLat.cos());
156         }
157         return north;
158     }
159 
160     /** Get the direction to the south of point, expressed in parent shape frame.
161      * <p>The south direction is the opposite of north direction.</p>
162      * @return unit vector in the south direction
163      * @see #getNorth()
164      */
165     public Vector3D getSouth() {
166         if (south == null) {
167             south = getNorth().negate();
168         }
169         return south;
170     }
171 
172     /** Get the direction to the east of point, expressed in parent shape frame.
173      * <p>The east direction is defined in the horizontal plane
174      * in order to complete direct triangle (east, north, zenith).</p>
175      * @return unit vector in the east direction
176      * @see #getWest()
177      */
178     public Vector3D getEast() {
179         if (east == null) {
180             final SinCos scLon = FastMath.sinCos(longitude);
181             east = new Vector3D(-scLon.sin(), scLon.cos(), 0);
182         }
183         return east;
184     }
185 
186     /** Get the direction to the west of point, expressed in parent shape frame.
187      * <p>The west direction is the opposite of east direction.</p>
188      * @return unit vector in the west direction
189      * @see #getEast()
190      */
191     public Vector3D getWest() {
192         if (west == null) {
193             west = getEast().negate();
194         }
195         return west;
196     }
197 
198     @Override
199     public boolean equals(final Object object) {
200         if (object instanceof GeodeticPoint) {
201             final GeodeticPoint other = (GeodeticPoint) object;
202             return this.getLatitude() == other.getLatitude() &&
203                    this.getLongitude() == other.getLongitude() &&
204                    this.getAltitude() == other.getAltitude();
205         }
206         return false;
207     }
208 
209     @Override
210     public int hashCode() {
211         return Double.valueOf(this.getLatitude()).hashCode() ^
212                Double.valueOf(this.getLongitude()).hashCode() ^
213                Double.valueOf(this.getAltitude()).hashCode();
214     }
215 
216     @Override
217     public String toString() {
218         final NumberFormat format = CompositeFormat.getDefaultNumberFormat();
219         return "{lat: " +
220                format.format(FastMath.toDegrees(this.getLatitude())) +
221                " deg, lon: " +
222                format.format(FastMath.toDegrees(this.getLongitude())) +
223                " deg, alt: " +
224                format.format(this.getAltitude()) +
225                "}";
226     }
227 }