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.geometry.spherical.oned.Sphere1D;
26  import org.hipparchus.random.RandomGenerator;
27  import org.hipparchus.random.Well1024a;
28  import org.hipparchus.util.FastMath;
29  import org.hipparchus.util.MathUtils;
30  import org.junit.jupiter.api.Test;
31  
32  import static org.junit.jupiter.api.Assertions.assertEquals;
33  import static org.junit.jupiter.api.Assertions.assertFalse;
34  import static org.junit.jupiter.api.Assertions.assertInstanceOf;
35  import static org.junit.jupiter.api.Assertions.assertNotEquals;
36  import static org.junit.jupiter.api.Assertions.assertNotSame;
37  import static org.junit.jupiter.api.Assertions.assertThrows;
38  import static org.junit.jupiter.api.Assertions.assertTrue;
39  
40  class S2PointTest {
41  
42  
43      @Test
44      void testS2Point() {
45          for (int k = -2; k < 3; ++k) {
46              S2Point p = new S2Point(1.0 + k * MathUtils.TWO_PI, 1.4);
47              assertEquals(1.0 + k * MathUtils.TWO_PI, p.getTheta(), 1.0e-10);
48              assertEquals(1.4, p.getPhi(), 1.0e-10);
49              assertEquals(FastMath.cos(1.0) * FastMath.sin(1.4), p.getVector().getX(), 1.0e-10);
50              assertEquals(FastMath.sin(1.0) * FastMath.sin(1.4), p.getVector().getY(), 1.0e-10);
51              assertEquals(FastMath.cos(1.4), p.getVector().getZ(), 1.0e-10);
52              assertFalse(p.isNaN());
53          }
54      }
55  
56      @Test
57      void testNegativePolarAngle() {
58          assertThrows(MathIllegalArgumentException.class, () -> new S2Point(1.0, -1.0));
59      }
60  
61      @Test
62      void testTooLargePolarAngle() {
63          assertThrows(MathIllegalArgumentException.class, () -> new S2Point(1.0, 3.5));
64      }
65  
66      @Test
67      void testNaN() {
68          assertTrue(S2Point.NaN.isNaN());
69          assertEquals(S2Point.NaN, new S2Point(Double.NaN, 1.0));
70          assertNotEquals(S2Point.NaN, new S2Point(1.0, 1.3));
71      }
72  
73      @SuppressWarnings("unlikely-arg-type")
74      @Test
75      void testEquals() {
76          S2Point a = new S2Point(1.0, 1.0);
77          S2Point b = new S2Point(1.0, 1.0);
78          assertEquals(a.hashCode(), b.hashCode());
79          assertNotSame(a, b);
80          assertEquals(a, b);
81          assertEquals(a, a);
82          assertNotEquals('a', a);
83          assertEquals(S2Point.NaN, S2Point.NaN);
84          assertEquals(S2Point.NaN, new S2Point(Double.NaN, 0.0));
85          assertEquals(S2Point.NaN, new S2Point(0.0, Double.NaN));
86      }
87  
88      @Test
89      void testEqualsIeee754() {
90          S2Point a = new S2Point(1.0, 1.0);
91          S2Point b = new S2Point(1.0, 1.0);
92          assertEquals(a.hashCode(), b.hashCode());
93          assertNotSame(a, b);
94          assertTrue(a.equalsIeee754(b));
95          assertTrue(a.equalsIeee754(a));
96          assertFalse(a.equalsIeee754('a'));
97          assertFalse(S2Point.NaN.equalsIeee754(S2Point.NaN));
98          assertFalse(S2Point.NaN.equalsIeee754(new S2Point(Double.NaN, 0.0)));
99          assertFalse(S2Point.NaN.equalsIeee754(new S2Point(0.0, Double.NaN)));
100     }
101 
102     @Test
103     void testDistance() {
104         S2Point a = new S2Point(1.0, MathUtils.SEMI_PI);
105         S2Point b = new S2Point(a.getTheta() + MathUtils.SEMI_PI, a.getPhi());
106         assertEquals(MathUtils.SEMI_PI, a.distance(b), 1.0e-10);
107         assertEquals(FastMath.PI, a.distance(a.negate()), 1.0e-10);
108         assertEquals(MathUtils.SEMI_PI, S2Point.MINUS_I.distance(S2Point.MINUS_K), 1.0e-10);
109         assertEquals(0.0, new S2Point(1.0, 0).distance(new S2Point(2.0, 0)), 1.0e-10);
110     }
111 
112     @Test
113     void testNegate() {
114         RandomGenerator generator = new Well1024a(0x79d1bc2e0999d238L);
115         for (int i = 0; i < 100000; ++i) {
116             S2Point p = new S2Point(MathUtils.TWO_PI * generator.nextDouble(),
117                                     FastMath.PI * generator.nextDouble());
118             S2Point np = new S2Point(p.negate().getTheta(), p.negate().getPhi());
119             assertEquals(FastMath.PI, p.distance(np), 1.4e-15);
120         }
121     }
122 
123     @Test
124     void testSpace() {
125         S2Point a = new S2Point(1.0, 1.0);
126         assertInstanceOf(Sphere2D.class, a.getSpace());
127         assertEquals(2, a.getSpace().getDimension());
128         assertInstanceOf(Sphere1D.class, a.getSpace().getSubSpace());
129     }
130 
131     @Test
132     void testMoveTowards() {
133         final S2Point s1 = new S2Point(2.0, 0.5);
134         final S2Point s2 = new S2Point(4.0, 2.5);
135         for (double r = 0.0; r <= 1.0; r += FastMath.scalb(1.0, -10)) {
136             // motion should be linear according to angles
137             assertEquals(r * s1.distance(s2), s1.distance(s1.moveTowards(s2, r)), 1.0e-14);
138         }
139     }
140 
141     @Test
142     void testMoveTowardsSpecialCase() {
143         final S2Point s = new S2Point(2.0, 0.5);
144         for (double r = 0.0; r <= 1.0; r += FastMath.scalb(1.0, -10)) {
145             // motion should be linear according to angles
146             assertEquals(0.0, s.distance(s.moveTowards(s, r)), 1.0e-20);
147         }
148     }
149 
150 }