View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF 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    *      https://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  
18  /*
19   * This is not the original file distributed by the Apache Software Foundation
20   * It has been modified by the Hipparchus project
21   */
22  package org.hipparchus.geometry.spherical.twod;
23  
24  import org.hipparchus.exception.MathIllegalArgumentException;
25  import org.hipparchus.exception.MathRuntimeException;
26  import org.hipparchus.geometry.Point;
27  import org.hipparchus.geometry.Space;
28  import org.hipparchus.geometry.euclidean.threed.Vector3D;
29  import org.hipparchus.util.FastMath;
30  import org.hipparchus.util.MathUtils;
31  import org.hipparchus.util.SinCos;
32  
33  /** This class represents a point on the 2-sphere.
34   * <p>
35   * We use the mathematical convention to use the azimuthal angle \( \theta \)
36   * in the x-y plane as the first coordinate, and the polar angle \( \varphi \)
37   * as the second coordinate (see <a
38   * href="http://mathworld.wolfram.com/SphericalCoordinates.html">Spherical
39   * Coordinates</a> in MathWorld).
40   * </p>
41   * <p>Instances of this class are guaranteed to be immutable.</p>
42   */
43  public class S2Point implements Point<Sphere2D, S2Point> {
44  
45      /** +I (coordinates: \( \theta = 0, \varphi = \pi/2 \)). */
46      public static final S2Point PLUS_I = new S2Point(0, MathUtils.SEMI_PI, Vector3D.PLUS_I);
47  
48      /** +J (coordinates: \( \theta = \pi/2, \varphi = \pi/2 \))). */
49      public static final S2Point PLUS_J = new S2Point(MathUtils.SEMI_PI, MathUtils.SEMI_PI, Vector3D.PLUS_J);
50  
51      /** +K (coordinates: \( \theta = any angle, \varphi = 0 \)). */
52      public static final S2Point PLUS_K = new S2Point(0, 0, Vector3D.PLUS_K);
53  
54      /** -I (coordinates: \( \theta = \pi, \varphi = \pi/2 \)). */
55      public static final S2Point MINUS_I = new S2Point(FastMath.PI, MathUtils.SEMI_PI, Vector3D.MINUS_I);
56  
57      /** -J (coordinates: \( \theta = 3\pi/2, \varphi = \pi/2 \)). */
58      public static final S2Point MINUS_J = new S2Point(1.5 * FastMath.PI, MathUtils.SEMI_PI, Vector3D.MINUS_J);
59  
60      /** -K (coordinates: \( \theta = any angle, \varphi = \pi \)). */
61      public static final S2Point MINUS_K = new S2Point(0, FastMath.PI, Vector3D.MINUS_K);
62  
63      // CHECKSTYLE: stop ConstantName
64      /** A vector with all coordinates set to NaN. */
65      public static final S2Point NaN = new S2Point(Double.NaN, Double.NaN, Vector3D.NaN);
66      // CHECKSTYLE: resume ConstantName
67  
68      /** Serializable UID. */
69      private static final long serialVersionUID = 20131218L;
70  
71      /** Azimuthal angle \( \theta \) in the x-y plane. */
72      private final double theta;
73  
74      /** Polar angle \( \varphi \). */
75      private final double phi;
76  
77      /** Corresponding 3D normalized vector. */
78      private final Vector3D vector;
79  
80      /** Simple constructor.
81       * Build a vector from its spherical coordinates
82       * @param theta azimuthal angle \( \theta \) in the x-y plane
83       * @param phi polar angle \( \varphi \)
84       * @see #getTheta()
85       * @see #getPhi()
86       * @exception MathIllegalArgumentException if \( \varphi \) is not in the [\( 0; \pi \)] range
87       */
88      public S2Point(final double theta, final double phi)
89          throws MathIllegalArgumentException {
90          this(theta, phi, vector(theta, phi));
91      }
92  
93      /** Simple constructor.
94       * Build a vector from its underlying 3D vector
95       * @param vector 3D vector
96       * @exception MathRuntimeException if vector norm is zero
97       */
98      public S2Point(final Vector3D vector) throws MathRuntimeException {
99          this(FastMath.atan2(vector.getY(), vector.getX()), Vector3D.angle(Vector3D.PLUS_K, vector),
100              vector.normalize());
101     }
102 
103     /** Build a point from its internal components.
104      * @param theta azimuthal angle \( \theta \) in the x-y plane
105      * @param phi polar angle \( \varphi \)
106      * @param vector corresponding vector
107      */
108     private S2Point(final double theta, final double phi, final Vector3D vector) {
109         this.theta  = theta;
110         this.phi    = phi;
111         this.vector = vector;
112     }
113 
114     /** Build the normalized vector corresponding to spherical coordinates.
115      * @param theta azimuthal angle \( \theta \) in the x-y plane
116      * @param phi polar angle \( \varphi \)
117      * @return normalized vector
118      * @exception MathIllegalArgumentException if \( \varphi \) is not in the [\( 0; \pi \)] range
119      */
120     private static Vector3D vector(final double theta, final double phi)
121        throws MathIllegalArgumentException {
122 
123         MathUtils.checkRangeInclusive(phi, 0, FastMath.PI);
124 
125         final SinCos scTheta = FastMath.sinCos(theta);
126         final SinCos scPhi   = FastMath.sinCos(phi);
127 
128         return new Vector3D(scTheta.cos() * scPhi.sin(), scTheta.sin() * scPhi.sin(), scPhi.cos());
129 
130     }
131 
132     /** Get the azimuthal angle \( \theta \) in the x-y plane.
133      * @return azimuthal angle \( \theta \) in the x-y plane
134      * @see #S2Point(double, double)
135      */
136     public double getTheta() {
137         return theta;
138     }
139 
140     /** Get the polar angle \( \varphi \).
141      * @return polar angle \( \varphi \)
142      * @see #S2Point(double, double)
143      */
144     public double getPhi() {
145         return phi;
146     }
147 
148     /** Get the corresponding normalized vector in the 3D euclidean space.
149      * @return normalized vector
150      */
151     public Vector3D getVector() {
152         return vector;
153     }
154 
155     /** {@inheritDoc} */
156     @Override
157     public Space getSpace() {
158         return Sphere2D.getInstance();
159     }
160 
161     /** {@inheritDoc} */
162     @Override
163     public boolean isNaN() {
164         return Double.isNaN(theta) || Double.isNaN(phi);
165     }
166 
167     /** Get the opposite of the instance.
168      * @return a new vector which is opposite to the instance
169      */
170     public S2Point negate() {
171         return new S2Point(FastMath.PI + theta, FastMath.PI - phi, vector.negate());
172     }
173 
174     /** {@inheritDoc} */
175     @Override
176     public double distance(final S2Point point) {
177         return distance(this, point);
178     }
179 
180     /** Compute the distance (angular separation) between two points.
181      * @param p1 first vector
182      * @param p2 second vector
183      * @return the angular separation between p1 and p2
184      */
185     public static double distance(S2Point p1, S2Point p2) {
186         return Vector3D.angle(p1.vector, p2.vector);
187     }
188 
189     /** {@inheritDoc} */
190     @Override
191     public S2Point moveTowards(final S2Point other, final double ratio) {
192         final double alpha = Vector3D.angle(vector, other.vector);
193         if (alpha == 0) {
194             // special case to avoid division by zero in normalization below
195             return this;
196         }
197         else {
198             final double sA = (FastMath.sin((1 - ratio) * alpha));
199             final double sB = FastMath.sin(ratio * alpha);
200             return new S2Point(new Vector3D(sA, vector, sB, other.vector));
201         }
202     }
203 
204     /**
205      * Test for the equality of two points on the 2-sphere.
206      * <p>
207      * If all coordinates of two points are exactly the same, and none are
208      * {@code Double.NaN}, the two points are considered to be equal.
209      * </p>
210      * <p>
211      * {@code NaN} coordinates are considered to affect globally the point
212      * and be equals to each other - i.e, if either (or all) coordinates of the
213      * point are equal to {@code Double.NaN}, the point is equal to
214      * {@link #NaN}.
215      * </p>
216      *
217      * @param other Object to test for equality to this
218      * @return true if two points on the 2-sphere objects are equal, false if
219      *         object is null, not an instance of S2Point, or
220      *         not equal to this S2Point instance
221      *
222      */
223     @Override
224     public boolean equals(Object other) {
225 
226         if (this == other) {
227             return true;
228         }
229 
230         if (other instanceof S2Point) {
231             final S2Point rhs = (S2Point) other;
232             return theta == rhs.theta && phi == rhs.phi || isNaN() && rhs.isNaN();
233         }
234 
235         return false;
236 
237     }
238 
239     /**
240      * Test for the equality of two points on the 2-sphere.
241      * <p>
242      * If all coordinates of two points are exactly the same, and none are
243      * {@code Double.NaN}, the two points are considered to be equal.
244      * </p>
245      * <p>
246      * In compliance with IEEE754 handling, if any coordinates of any of the
247      * two points are {@code NaN}, then the points are considered different.
248      * This implies that {@link #NaN S2Point.NaN}.equals({@link #NaN S2Point.NaN})
249      * returns {@code false} despite the instance is checked against itself.
250      * </p>
251      *
252      * @param other Object to test for equality to this
253      * @return true if two points objects are equal, false if
254      *         object is null, not an instance of S2Point, or
255      *         not equal to this S2Point instance
256      * @since 2.1
257      */
258     public boolean equalsIeee754(Object other) {
259 
260         if (this == other && !isNaN()) {
261             return true;
262         }
263 
264         if (other instanceof S2Point) {
265             final S2Point rhs = (S2Point) other;
266             return phi == rhs.phi && theta == rhs.theta;
267         }
268 
269         return false;
270 
271     }
272 
273     /**
274      * Get a hashCode for the point.
275      * <p>
276      * All NaN values have the same hash code.</p>
277      *
278      * @return a hash code value for this object
279      */
280     @Override
281     public int hashCode() {
282         if (isNaN()) {
283             return 542;
284         }
285         return 134 * (37 * MathUtils.hash(theta) +  MathUtils.hash(phi));
286     }
287 
288     @Override
289     public String toString() {
290         return "S2Point{" +
291                 "theta=" + theta +
292                 ", phi=" + phi +
293                 '}';
294     }
295 
296 }