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> { 44 45 /** +I (coordinates: \( \theta = 0, \varphi = \pi/2 \)). */ 46 public static final S2Point PLUS_I = new S2Point(0, 0.5 * FastMath.PI, Vector3D.PLUS_I); 47 48 /** +J (coordinates: \( \theta = \pi/2, \varphi = \pi/2 \))). */ 49 public static final S2Point PLUS_J = new S2Point(0.5 * FastMath.PI, 0.5 * FastMath.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, 0.5 * FastMath.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, 0.5 * FastMath.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 Point<Sphere2D> point) { 177 return distance(this, (S2Point) 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 /** 190 * Test for the equality of two points on the 2-sphere. 191 * <p> 192 * If all coordinates of two points are exactly the same, and none are 193 * {@code Double.NaN}, the two points are considered to be equal. 194 * </p> 195 * <p> 196 * {@code NaN} coordinates are considered to affect globally the point 197 * and be equals to each other - i.e, if either (or all) coordinates of the 198 * point are equal to {@code Double.NaN}, the point is equal to 199 * {@link #NaN}. 200 * </p> 201 * 202 * @param other Object to test for equality to this 203 * @return true if two points on the 2-sphere objects are equal, false if 204 * object is null, not an instance of S2Point, or 205 * not equal to this S2Point instance 206 * 207 */ 208 @Override 209 public boolean equals(Object other) { 210 211 if (this == other) { 212 return true; 213 } 214 215 if (other instanceof S2Point) { 216 final S2Point rhs = (S2Point) other; 217 return theta == rhs.theta && phi == rhs.phi || isNaN() && rhs.isNaN(); 218 } 219 220 return false; 221 222 } 223 224 /** 225 * Test for the equality of two points on the 2-sphere. 226 * <p> 227 * If all coordinates of two points are exactly the same, and none are 228 * {@code Double.NaN}, the two points are considered to be equal. 229 * </p> 230 * <p> 231 * In compliance with IEEE754 handling, if any coordinates of any of the 232 * two points are {@code NaN}, then the points are considered different. 233 * This implies that {@link #NaN S2Point.NaN}.equals({@link #NaN S2Point.NaN}) 234 * returns {@code false} despite the instance is checked against itself. 235 * </p> 236 * 237 * @param other Object to test for equality to this 238 * @return true if two points objects are equal, false if 239 * object is null, not an instance of S2Point, or 240 * not equal to this S2Point instance 241 * @since 2.1 242 */ 243 public boolean equalsIeee754(Object other) { 244 245 if (this == other && !isNaN()) { 246 return true; 247 } 248 249 if (other instanceof S2Point) { 250 final S2Point rhs = (S2Point) other; 251 return phi == rhs.phi && theta == rhs.theta; 252 } 253 254 return false; 255 256 } 257 258 /** 259 * Get a hashCode for the point. 260 * <p> 261 * All NaN values have the same hash code.</p> 262 * 263 * @return a hash code value for this object 264 */ 265 @Override 266 public int hashCode() { 267 if (isNaN()) { 268 return 542; 269 } 270 return 134 * (37 * MathUtils.hash(theta) + MathUtils.hash(phi)); 271 } 272 273 @Override 274 public String toString() { 275 return "S2Point{" + 276 "theta=" + theta + 277 ", phi=" + phi + 278 '}'; 279 } 280 281 }