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 * 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.apache.commons.math3.geometry.spherical.oned; 18 19 import org.apache.commons.math3.geometry.Point; 20 import org.apache.commons.math3.geometry.Space; 21 import org.apache.commons.math3.geometry.euclidean.twod.Vector2D; 22 import org.apache.commons.math3.util.FastMath; 23 import org.apache.commons.math3.util.MathUtils; 24 25 /** This class represents a point on the 1-sphere. 26 * <p>Instances of this class are guaranteed to be immutable.</p> 27 * @since 3.3 28 */ 29 public class S1Point implements Point<Sphere1D> { 30 31 // CHECKSTYLE: stop ConstantName 32 /** A vector with all coordinates set to NaN. */ 33 public static final S1Point NaN = new S1Point(Double.NaN, Vector2D.NaN); 34 // CHECKSTYLE: resume ConstantName 35 36 /** Serializable UID. */ 37 private static final long serialVersionUID = 20131218L; 38 39 /** Azimuthal angle \( \alpha \). */ 40 private final double alpha; 41 42 /** Corresponding 2D normalized vector. */ 43 private final Vector2D vector; 44 45 /** Simple constructor. 46 * Build a vector from its coordinates 47 * @param alpha azimuthal angle \( \alpha \) 48 * @see #getAlpha() 49 */ S1Point(final double alpha)50 public S1Point(final double alpha) { 51 this(MathUtils.normalizeAngle(alpha, FastMath.PI), 52 new Vector2D(FastMath.cos(alpha), FastMath.sin(alpha))); 53 } 54 55 /** Build a point from its internal components. 56 * @param alpha azimuthal angle \( \alpha \) 57 * @param vector corresponding vector 58 */ S1Point(final double alpha, final Vector2D vector)59 private S1Point(final double alpha, final Vector2D vector) { 60 this.alpha = alpha; 61 this.vector = vector; 62 } 63 64 /** Get the azimuthal angle \( \alpha \). 65 * @return azimuthal angle \( \alpha \) 66 * @see #S1Point(double) 67 */ getAlpha()68 public double getAlpha() { 69 return alpha; 70 } 71 72 /** Get the corresponding normalized vector in the 2D euclidean space. 73 * @return normalized vector 74 */ getVector()75 public Vector2D getVector() { 76 return vector; 77 } 78 79 /** {@inheritDoc} */ getSpace()80 public Space getSpace() { 81 return Sphere1D.getInstance(); 82 } 83 84 /** {@inheritDoc} */ isNaN()85 public boolean isNaN() { 86 return Double.isNaN(alpha); 87 } 88 89 /** {@inheritDoc} */ distance(final Point<Sphere1D> point)90 public double distance(final Point<Sphere1D> point) { 91 return distance(this, (S1Point) point); 92 } 93 94 /** Compute the distance (angular separation) between two points. 95 * @param p1 first vector 96 * @param p2 second vector 97 * @return the angular separation between p1 and p2 98 */ distance(S1Point p1, S1Point p2)99 public static double distance(S1Point p1, S1Point p2) { 100 return Vector2D.angle(p1.vector, p2.vector); 101 } 102 103 /** 104 * Test for the equality of two points on the 2-sphere. 105 * <p> 106 * If all coordinates of two points are exactly the same, and none are 107 * <code>Double.NaN</code>, the two points are considered to be equal. 108 * </p> 109 * <p> 110 * <code>NaN</code> coordinates are considered to affect globally the vector 111 * and be equals to each other - i.e, if either (or all) coordinates of the 112 * 2D vector are equal to <code>Double.NaN</code>, the 2D vector is equal to 113 * {@link #NaN}. 114 * </p> 115 * 116 * @param other Object to test for equality to this 117 * @return true if two points on the 2-sphere objects are equal, false if 118 * object is null, not an instance of S2Point, or 119 * not equal to this S2Point instance 120 * 121 */ 122 @Override equals(Object other)123 public boolean equals(Object other) { 124 125 if (this == other) { 126 return true; 127 } 128 129 if (other instanceof S1Point) { 130 final S1Point rhs = (S1Point) other; 131 if (rhs.isNaN()) { 132 return this.isNaN(); 133 } 134 135 return alpha == rhs.alpha; 136 } 137 138 return false; 139 140 } 141 142 /** 143 * Get a hashCode for the 2D vector. 144 * <p> 145 * All NaN values have the same hash code.</p> 146 * 147 * @return a hash code value for this object 148 */ 149 @Override hashCode()150 public int hashCode() { 151 if (isNaN()) { 152 return 542; 153 } 154 return 1759 * MathUtils.hash(alpha); 155 } 156 157 } 158