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.Assert;
31  import org.junit.Test;
32  
33  public class S2PointTest {
34  
35  
36      @Test
37      public void testS2Point() {
38          for (int k = -2; k < 3; ++k) {
39              S2Point p = new S2Point(1.0 + k * MathUtils.TWO_PI, 1.4);
40              Assert.assertEquals(1.0 + k * MathUtils.TWO_PI, p.getTheta(), 1.0e-10);
41              Assert.assertEquals(1.4, p.getPhi(), 1.0e-10);
42              Assert.assertEquals(FastMath.cos(1.0) * FastMath.sin(1.4), p.getVector().getX(), 1.0e-10);
43              Assert.assertEquals(FastMath.sin(1.0) * FastMath.sin(1.4), p.getVector().getY(), 1.0e-10);
44              Assert.assertEquals(FastMath.cos(1.4), p.getVector().getZ(), 1.0e-10);
45              Assert.assertFalse(p.isNaN());
46          }
47      }
48  
49      @Test(expected=MathIllegalArgumentException.class)
50      public void testNegativePolarAngle() {
51          new S2Point(1.0, -1.0);
52      }
53  
54      @Test(expected=MathIllegalArgumentException.class)
55      public void testTooLargePolarAngle() {
56          new S2Point(1.0, 3.5);
57      }
58  
59      @Test
60      public void testNaN() {
61          Assert.assertTrue(S2Point.NaN.isNaN());
62          Assert.assertTrue(S2Point.NaN.equals(new S2Point(Double.NaN, 1.0)));
63          Assert.assertFalse(new S2Point(1.0, 1.3).equals(S2Point.NaN));
64      }
65  
66      @SuppressWarnings("unlikely-arg-type")
67      @Test
68      public void testEquals() {
69          S2Point a = new S2Point(1.0, 1.0);
70          S2Point b = new S2Point(1.0, 1.0);
71          Assert.assertEquals(a.hashCode(), b.hashCode());
72          Assert.assertFalse(a == b);
73          Assert.assertTrue(a.equals(b));
74          Assert.assertTrue(a.equals(a));
75          Assert.assertFalse(a.equals('a'));
76          Assert.assertTrue(S2Point.NaN.equals(S2Point.NaN));
77          Assert.assertTrue(S2Point.NaN.equals(new S2Point(Double.NaN, 0.0)));
78          Assert.assertTrue(S2Point.NaN.equals(new S2Point(0.0, Double.NaN)));
79      }
80  
81      @Test
82      public void testEqualsIeee754() {
83          S2Point a = new S2Point(1.0, 1.0);
84          S2Point b = new S2Point(1.0, 1.0);
85          Assert.assertEquals(a.hashCode(), b.hashCode());
86          Assert.assertFalse(a == b);
87          Assert.assertTrue(a.equalsIeee754(b));
88          Assert.assertTrue(a.equalsIeee754(a));
89          Assert.assertFalse(a.equalsIeee754('a'));
90          Assert.assertFalse(S2Point.NaN.equalsIeee754(S2Point.NaN));
91          Assert.assertFalse(S2Point.NaN.equalsIeee754(new S2Point(Double.NaN, 0.0)));
92          Assert.assertFalse(S2Point.NaN.equalsIeee754(new S2Point(0.0, Double.NaN)));
93      }
94  
95      @Test
96      public void testDistance() {
97          S2Point a = new S2Point(1.0, 0.5 * FastMath.PI);
98          S2Point b = new S2Point(a.getTheta() + 0.5 * FastMath.PI, a.getPhi());
99          Assert.assertEquals(0.5 * FastMath.PI, a.distance(b), 1.0e-10);
100         Assert.assertEquals(FastMath.PI, a.distance(a.negate()), 1.0e-10);
101         Assert.assertEquals(0.5 * FastMath.PI, S2Point.MINUS_I.distance(S2Point.MINUS_K), 1.0e-10);
102         Assert.assertEquals(0.0, new S2Point(1.0, 0).distance(new S2Point(2.0, 0)), 1.0e-10);
103     }
104 
105     @Test
106     public void testNegate() {
107         RandomGenerator generator = new Well1024a(0x79d1bc2e0999d238l);
108         for (int i = 0; i < 100000; ++i) {
109             S2Point p = new S2Point(MathUtils.TWO_PI * generator.nextDouble(),
110                                     FastMath.PI * generator.nextDouble());
111             S2Point np = new S2Point(p.negate().getTheta(), p.negate().getPhi());
112             Assert.assertEquals(FastMath.PI, p.distance(np), 1.4e-15);
113         }
114     }
115 
116     @Test
117     public void testSpace() {
118         S2Point a = new S2Point(1.0, 1.0);
119         Assert.assertTrue(a.getSpace() instanceof Sphere2D);
120         Assert.assertEquals(2, a.getSpace().getDimension());
121         Assert.assertTrue(a.getSpace().getSubSpace() instanceof Sphere1D);
122     }
123 
124 }