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.euclidean.threed.Rotation;
26  import org.hipparchus.geometry.euclidean.threed.RotationConvention;
27  import org.hipparchus.geometry.euclidean.threed.Vector3D;
28  import org.hipparchus.geometry.partitioning.Transform;
29  import org.hipparchus.geometry.spherical.oned.Arc;
30  import org.hipparchus.geometry.spherical.oned.LimitAngle;
31  import org.hipparchus.geometry.spherical.oned.S1Point;
32  import org.hipparchus.geometry.spherical.oned.Sphere1D;
33  import org.hipparchus.geometry.spherical.oned.SubLimitAngle;
34  import org.hipparchus.random.RandomGenerator;
35  import org.hipparchus.random.UnitSphereRandomVectorGenerator;
36  import org.hipparchus.random.Well1024a;
37  import org.hipparchus.util.FastMath;
38  import org.hipparchus.util.MathUtils;
39  import org.junit.jupiter.api.Test;
40  
41  import static org.junit.jupiter.api.Assertions.assertEquals;
42  import static org.junit.jupiter.api.Assertions.assertFalse;
43  import static org.junit.jupiter.api.Assertions.assertThrows;
44  import static org.junit.jupiter.api.Assertions.assertTrue;
45  
46  class CircleTest {
47  
48      @Test
49      void testEquator() {
50          Circle circle = new Circle(new Vector3D(0, 0, 1000), 1.0e-10).copySelf();
51          assertEquals(Vector3D.PLUS_K, circle.getPole());
52          assertEquals(1.0e-10, circle.getTolerance(), 1.0e-20);
53          circle.revertSelf();
54          assertEquals(Vector3D.MINUS_K, circle.getPole());
55          assertEquals(Vector3D.PLUS_K, circle.getReverse().getPole());
56          assertEquals(Vector3D.MINUS_K, circle.getPole());
57      }
58  
59      @Test
60      void testXY() {
61          Circle circle = new Circle(new S2Point(1.2, 2.5), new S2Point(-4.3, 0), 1.0e-10);
62          assertEquals(0.0, circle.getPointAt(0).distance(circle.getXAxis()), 1.0e-10);
63          assertEquals(0.0, circle.getPointAt(MathUtils.SEMI_PI).distance(circle.getYAxis()), 1.0e-10);
64          assertEquals(MathUtils.SEMI_PI, Vector3D.angle(circle.getXAxis(), circle.getYAxis()), 1.0e-10);
65          assertEquals(MathUtils.SEMI_PI, Vector3D.angle(circle.getXAxis(), circle.getPole()), 1.0e-10);
66          assertEquals(MathUtils.SEMI_PI, Vector3D.angle(circle.getPole(), circle.getYAxis()), 1.0e-10);
67          assertEquals(0.0,
68                              circle.getPole().distance(Vector3D.crossProduct(circle.getXAxis(), circle.getYAxis())),
69                              1.0e-10);
70      }
71  
72      @Test
73      void testReverse() {
74          Circle circle = new Circle(new S2Point(1.2, 2.5), new S2Point(-4.3, 0), 1.0e-10);
75          Circle reversed = circle.getReverse();
76          assertEquals(0.0, reversed.getPointAt(0).distance(reversed.getXAxis()), 1.0e-10);
77          assertEquals(0.0, reversed.getPointAt(MathUtils.SEMI_PI).distance(reversed.getYAxis()), 1.0e-10);
78          assertEquals(MathUtils.SEMI_PI, Vector3D.angle(reversed.getXAxis(), reversed.getYAxis()), 1.0e-10);
79          assertEquals(MathUtils.SEMI_PI, Vector3D.angle(reversed.getXAxis(), reversed.getPole()), 1.0e-10);
80          assertEquals(MathUtils.SEMI_PI, Vector3D.angle(reversed.getPole(), reversed.getYAxis()), 1.0e-10);
81          assertEquals(0.0,
82                              reversed.getPole().distance(Vector3D.crossProduct(reversed.getXAxis(), reversed.getYAxis())),
83                              1.0e-10);
84  
85          assertEquals(0, Vector3D.angle(circle.getXAxis(), reversed.getXAxis()), 1.0e-10);
86          assertEquals(FastMath.PI, Vector3D.angle(circle.getYAxis(), reversed.getYAxis()), 1.0e-10);
87          assertEquals(FastMath.PI, Vector3D.angle(circle.getPole(), reversed.getPole()), 1.0e-10);
88  
89          assertTrue(circle.sameOrientationAs(circle));
90          assertFalse(circle.sameOrientationAs(reversed));
91  
92      }
93  
94      @Test
95      void testPhase() {
96          Circle circle = new Circle(new S2Point(1.2, 2.5), new S2Point(-4.3, 0), 1.0e-10);
97          Vector3D p = new Vector3D(1, 2, -4);
98          Vector3D samePhase = circle.getPointAt(circle.getPhase(p));
99          assertEquals(0.0,
100                             Vector3D.angle(Vector3D.crossProduct(circle.getPole(), p),
101                                            Vector3D.crossProduct(circle.getPole(), samePhase)),
102                             1.0e-10);
103         assertEquals(MathUtils.SEMI_PI, Vector3D.angle(circle.getPole(), samePhase), 1.0e-10);
104         assertEquals(circle.getPhase(p), circle.getPhase(samePhase), 1.0e-10);
105         assertEquals(0.0, circle.getPhase(circle.getXAxis()), 1.0e-10);
106         assertEquals(MathUtils.SEMI_PI, circle.getPhase(circle.getYAxis()), 1.0e-10);
107 
108     }
109 
110     @Test
111     void testSubSpace() {
112         Circle circle = new Circle(new S2Point(1.2, 2.5), new S2Point(-4.3, 0), 1.0e-10);
113         assertEquals(0.0, circle.toSubSpace(new S2Point(circle.getXAxis())).getAlpha(), 1.0e-10);
114         assertEquals(MathUtils.SEMI_PI, circle.toSubSpace(new S2Point(circle.getYAxis())).getAlpha(), 1.0e-10);
115         Vector3D p = new Vector3D(1, 2, -4);
116         assertEquals(circle.getPhase(p), circle.toSubSpace(new S2Point(p)).getAlpha(), 1.0e-10);
117     }
118 
119     @Test
120     void testSpace() {
121         Circle circle = new Circle(new S2Point(1.2, 2.5), new S2Point(-4.3, 0), 1.0e-10);
122         for (double alpha = 0; alpha < MathUtils.TWO_PI; alpha += 0.1) {
123             Vector3D p = new Vector3D(FastMath.cos(alpha), circle.getXAxis(),
124                                       FastMath.sin(alpha), circle.getYAxis());
125             Vector3D q = circle.toSpace(new S1Point(alpha)).getVector();
126             assertEquals(0.0, p.distance(q), 1.0e-10);
127             assertEquals(MathUtils.SEMI_PI, Vector3D.angle(circle.getPole(), q), 1.0e-10);
128         }
129     }
130 
131     @Test
132     void testOffset() {
133         Circle circle = new Circle(Vector3D.PLUS_K, 1.0e-10);
134         assertEquals(0.0,                circle.getOffset(new S2Point(Vector3D.PLUS_I)),  1.0e-10);
135         assertEquals(0.0,                circle.getOffset(new S2Point(Vector3D.MINUS_I)), 1.0e-10);
136         assertEquals(0.0,                circle.getOffset(new S2Point(Vector3D.PLUS_J)),  1.0e-10);
137         assertEquals(0.0,                circle.getOffset(new S2Point(Vector3D.MINUS_J)), 1.0e-10);
138         assertEquals(-MathUtils.SEMI_PI, circle.getOffset(new S2Point(Vector3D.PLUS_K)),  1.0e-10);
139         assertEquals( MathUtils.SEMI_PI, circle.getOffset(new S2Point(Vector3D.MINUS_K)), 1.0e-10);
140 
141     }
142 
143     @Test
144     void testInsideArc() {
145         RandomGenerator random = new Well1024a(0xbfd34e92231bbcfeL);
146         UnitSphereRandomVectorGenerator sphRandom = new UnitSphereRandomVectorGenerator(3, random);
147         for (int i = 0; i < 100; ++i) {
148             Circle c1 = new Circle(new Vector3D(sphRandom.nextVector()), 1.0e-10);
149             Circle c2 = new Circle(new Vector3D(sphRandom.nextVector()), 1.0e-10);
150             checkArcIsInside(c1, c2);
151             checkArcIsInside(c2, c1);
152         }
153     }
154 
155     private void checkArcIsInside(final Circle arcCircle, final Circle otherCircle) {
156         Arc arc = arcCircle.getInsideArc(otherCircle);
157         assertEquals(FastMath.PI, arc.getSize(), 1.0e-10);
158         for (double alpha = arc.getInf(); alpha < arc.getSup(); alpha += 0.1) {
159             assertTrue(otherCircle.getOffset(arcCircle.getPointAt(alpha)) <= 2.0e-15);
160         }
161         for (double alpha = arc.getSup(); alpha < arc.getInf() + MathUtils.TWO_PI; alpha += 0.1) {
162             assertTrue(otherCircle.getOffset(arcCircle.getPointAt(alpha)) >= -2.0e-15);
163         }
164     }
165 
166     @Test
167     void testTransform() {
168         RandomGenerator random = new Well1024a(0x16992fc4294bf2f1L);
169         UnitSphereRandomVectorGenerator sphRandom = new UnitSphereRandomVectorGenerator(3, random);
170         for (int i = 0; i < 100; ++i) {
171 
172             Rotation r = new Rotation(new Vector3D(sphRandom.nextVector()),
173                                       FastMath.PI * random.nextDouble(),
174                                       RotationConvention.VECTOR_OPERATOR);
175             Transform<Sphere2D, S2Point, Circle, SubCircle, Sphere1D, S1Point, LimitAngle, SubLimitAngle> t = Circle.getTransform(r);
176 
177             S2Point  p = new S2Point(new Vector3D(sphRandom.nextVector()));
178             S2Point tp = t.apply(p);
179             assertEquals(0.0, r.applyTo(p.getVector()).distance(tp.getVector()), 1.0e-10);
180 
181             Circle  c = new Circle(new Vector3D(sphRandom.nextVector()), 1.0e-10);
182             Circle tc = t.apply(c);
183             assertEquals(0.0, r.applyTo(c.getPole()).distance(tc.getPole()),   1.0e-10);
184             assertEquals(0.0, r.applyTo(c.getXAxis()).distance(tc.getXAxis()), 1.0e-10);
185             assertEquals(0.0, r.applyTo(c.getYAxis()).distance(tc.getYAxis()), 1.0e-10);
186             assertEquals(c.getTolerance(), t.apply(c).getTolerance(), 1.0e-10);
187 
188             SubLimitAngle  sub = new LimitAngle(new S1Point(MathUtils.TWO_PI * random.nextDouble()),
189                                                 random.nextBoolean(), 1.0e-10).wholeHyperplane();
190             Vector3D psub = c.getPointAt(sub.getHyperplane().getLocation().getAlpha());
191             SubLimitAngle tsub = t.apply(sub, c, tc);
192             Vector3D ptsub = tc.getPointAt(tsub.getHyperplane().getLocation().getAlpha());
193             assertEquals(0.0, r.applyTo(psub).distance(ptsub), 1.0e-10);
194 
195         }
196     }
197 
198     @Test
199     void testTooSmallTolerance() {
200         assertThrows(MathIllegalArgumentException.class, () -> new Circle(Vector3D.PLUS_K, 0.9 * Sphere2D.SMALLEST_TOLERANCE));
201     }
202 
203     /** Check {@link Circle#getArc(S2Point, S2Point)}. */
204     @Test
205     void testGetArc() {
206         // setup
207         double tol = 1e-6;
208         Circle circle = new Circle(Vector3D.PLUS_K, tol);
209 
210         // action
211         Arc arc = circle.getArc(new S2Point(circle.getPointAt(0)), new S2Point(circle.getPointAt(1)));
212         // verify
213         assertEquals(0.5, arc.getBarycenter(), tol);
214         assertEquals(0, arc.getInf(), tol);
215         assertEquals(1, arc.getSup(), tol);
216         assertEquals(tol, arc.getTolerance(), 0);
217 
218         // action, crossing discontinuity
219         arc = circle.getArc(new S2Point(circle.getPointAt(3)), new S2Point(circle.getPointAt(-3)));
220         // verify
221         assertEquals(FastMath.PI, arc.getBarycenter(), tol);
222         assertEquals(3, arc.getInf(), tol);
223         assertEquals(2 * FastMath.PI - 3, arc.getSup(), tol);
224         assertEquals(tol, arc.getTolerance(), 0);
225     }
226 
227     @Test
228     public void testMove() {
229         double tol = 1e-6;
230         Circle circle = new Circle(Vector3D.PLUS_K, tol);
231         assertEquals( 0.0, circle.getOffset(S2Point.PLUS_I), 1.0e-10);
232         assertEquals( 0.1, circle.getOffset(circle.moveToOffset(S2Point.PLUS_I,  0.1)), 1.0e-10);
233         assertEquals(-1.2, circle.getOffset(circle.moveToOffset(S2Point.PLUS_I, -1.2)), 1.0e-10);
234     }
235 
236 }