001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.math3.geometry.spherical.oned;
018
019import org.apache.commons.math3.geometry.Point;
020import org.apache.commons.math3.geometry.Space;
021import org.apache.commons.math3.geometry.euclidean.twod.Vector2D;
022import org.apache.commons.math3.util.FastMath;
023import org.apache.commons.math3.util.MathUtils;
024
025/** This class represents a point on the 1-sphere.
026 * <p>Instances of this class are guaranteed to be immutable.</p>
027 * @since 3.3
028 */
029public class S1Point implements Point<Sphere1D> {
030
031   // CHECKSTYLE: stop ConstantName
032    /** A vector with all coordinates set to NaN. */
033    public static final S1Point NaN = new S1Point(Double.NaN, Vector2D.NaN);
034    // CHECKSTYLE: resume ConstantName
035
036    /** Serializable UID. */
037    private static final long serialVersionUID = 20131218L;
038
039    /** Azimuthal angle \( \alpha \). */
040    private final double alpha;
041
042    /** Corresponding 2D normalized vector. */
043    private final Vector2D vector;
044
045    /** Simple constructor.
046     * Build a vector from its coordinates
047     * @param alpha azimuthal angle \( \alpha \)
048     * @see #getAlpha()
049     */
050    public S1Point(final double alpha) {
051        this(MathUtils.normalizeAngle(alpha, FastMath.PI),
052             new Vector2D(FastMath.cos(alpha), FastMath.sin(alpha)));
053    }
054
055    /** Build a point from its internal components.
056     * @param alpha azimuthal angle \( \alpha \)
057     * @param vector corresponding vector
058     */
059    private S1Point(final double alpha, final Vector2D vector) {
060        this.alpha  = alpha;
061        this.vector = vector;
062    }
063
064    /** Get the azimuthal angle \( \alpha \).
065     * @return azimuthal angle \( \alpha \)
066     * @see #S1Point(double)
067     */
068    public double getAlpha() {
069        return alpha;
070    }
071
072    /** Get the corresponding normalized vector in the 2D euclidean space.
073     * @return normalized vector
074     */
075    public Vector2D getVector() {
076        return vector;
077    }
078
079    /** {@inheritDoc} */
080    public Space getSpace() {
081        return Sphere1D.getInstance();
082    }
083
084    /** {@inheritDoc} */
085    public boolean isNaN() {
086        return Double.isNaN(alpha);
087    }
088
089    /** {@inheritDoc} */
090    public double distance(final Point<Sphere1D> point) {
091        return distance(this, (S1Point) point);
092    }
093
094    /** Compute the distance (angular separation) between two points.
095     * @param p1 first vector
096     * @param p2 second vector
097     * @return the angular separation between p1 and p2
098     */
099    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
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
150    public int hashCode() {
151        if (isNaN()) {
152            return 542;
153        }
154        return 1759 * MathUtils.hash(alpha);
155    }
156
157}