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