1   /* Contributed in the public domain.
2    * Licensed to CS GROUP (CS) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * CS 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    *   http://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  package org.orekit.bodies;
18  
19  import org.hipparchus.CalculusFieldElement;
20  import org.hipparchus.complex.Complex;
21  import org.hipparchus.complex.ComplexField;
22  import org.hipparchus.geometry.euclidean.threed.Vector3D;
23  import org.hipparchus.util.Binary64;
24  import org.hipparchus.util.Binary64Field;
25  import org.hipparchus.util.FastMath;
26  import org.junit.jupiter.api.Assertions;
27  import org.junit.jupiter.api.Test;
28  
29  /**
30   * Unit tests for {@link FieldGeodeticPoint}.
31   *
32   * @author Evan Ward
33   *
34   */
35  public class FieldGeodeticPointTest {
36  
37      /**
38       * check {@link FieldGeodeticPoint#FieldGeodeticPoint(CalculusFieldElement, CalculusFieldElement, CalculusFieldElement)} angle
39       * normalization.
40       */
41      @Test
42      void testGeodeticPointAngleNormalization() {
43          // action
44          FieldGeodeticPoint<Binary64> point =
45                  new FieldGeodeticPoint<>(new Binary64(FastMath.toRadians(135)),
46                                           new Binary64(FastMath.toRadians(90 - 360)),
47                                           new Binary64(0));
48  
49          // verify
50          Assertions.assertEquals(FastMath.toRadians(45), point.getLatitude().getReal(), 1.0e-15);
51          Assertions.assertEquals(FastMath.toRadians(-90), point.getLongitude().getReal(), 1.0e-15);
52  
53          Assertions.assertEquals(0, Vector3D.distance(point.getEast().toVector3D(),   Vector3D.PLUS_I), 1.0e-15);
54          Assertions.assertEquals(0, Vector3D.distance(point.getNorth().toVector3D(),  new Vector3D( 0.50 * FastMath.PI,  0.25 * FastMath.PI)), 1.0e-15);
55          Assertions.assertEquals(0, Vector3D.distance(point.getWest().toVector3D(),   Vector3D.MINUS_I), 1.0e-15);
56          Assertions.assertEquals(0, Vector3D.distance(point.getSouth().toVector3D(),  new Vector3D(-0.50 * FastMath.PI, -0.25 * FastMath.PI)), 1.0e-15);
57          Assertions.assertEquals(0, Vector3D.distance(point.getZenith().toVector3D(), new Vector3D(-0.50 * FastMath.PI,  0.25 * FastMath.PI)), 1.0e-15);
58          Assertions.assertEquals(0, Vector3D.distance(point.getNadir().toVector3D(),  new Vector3D( 0.50 * FastMath.PI, -0.25 * FastMath.PI)), 1.0e-15);
59  
60      }
61  
62      /**
63       * check {@link FieldGeodeticPoint#FieldGeodeticPoint(CalculusFieldElement, CalculusFieldElement, CalculusFieldElement)} for
64       * several different angles.
65       */
66      @Test
67      void testGeodeticPoint() {
68          // setup
69          // the input and expected results
70          final double pi = FastMath.PI;
71          double[][] points = {
72                  // Input lat, Input lon; expected lat, expected lon
73                  // first quadrant
74                  { pi / 6, pi / 6, pi / 6, pi / 6 },
75                  // second quadrant
76                  { 4 * pi / 6, 4 * pi / 6, pi / 3, -pi / 3 },
77                  // third quadrant
78                  { 7 * pi / 6, 7 * pi / 6, -pi / 6, pi / 6 },
79                  // fourth quadrant
80                  { -pi / 6, -pi / 6, -pi / 6, -pi / 6 },
81                  { -4 * pi / 6, -4 * pi / 6, -pi / 3, pi / 3 },
82                  { -pi / 6, -4 * pi / 3, -pi / 6, 2 * pi / 3 } };
83  
84          for (double[] point : points) {
85              // action
86              FieldGeodeticPoint<Binary64> gp =
87                      new FieldGeodeticPoint<>(new Binary64(point[0]),
88                                               new Binary64(point[1]),
89                                               Binary64.ZERO);
90              Assertions.assertEquals(0, gp.getEast().crossProduct(gp.getNorth()).distance(gp.getZenith()).getReal(), 1.0e-15);
91              Assertions.assertEquals(0, gp.getNorth().crossProduct(gp.getWest()).distance(gp.getZenith()).getReal(), 1.0e-15);
92              Assertions.assertEquals(0, gp.getSouth().crossProduct(gp.getWest()).distance(gp.getNadir()).getReal(), 1.0e-15);
93              Assertions.assertEquals(0, gp.getEast().crossProduct(gp.getSouth()).distance(gp.getNadir()).getReal(), 1.0e-15);
94              Assertions.assertEquals(0, gp.getZenith().crossProduct(gp.getSouth()).distance(gp.getEast()).getReal(), 1.0e-15);
95              Assertions.assertEquals(0, gp.getNadir().crossProduct(gp.getWest()).distance(gp.getNorth()).getReal(), 1.0e-15);
96  
97              // verify to within 5 ulps
98              Assertions.assertEquals(point[2], gp.getLatitude().getReal(), 5 * FastMath.ulp(point[2]));
99              Assertions.assertEquals(point[3], gp.getLongitude().getReal(), 5 * FastMath.ulp(point[3]));
100         }
101     }
102 
103     /**
104      * check {@link FieldGeodeticPoint#equals(Object)}.
105      */
106     @Test
107     void testEquals() {
108         // setup
109         FieldGeodeticPoint<Binary64> point =
110                 new FieldGeodeticPoint<>(new Binary64(1),
111                                          new Binary64(2),
112                                          new Binary64(3));
113 
114         // actions + verify
115         Assertions.assertEquals(point, new FieldGeodeticPoint<>(Binary64Field.getInstance(),
116                                                                 new GeodeticPoint(1, 2, 3)));
117         Assertions.assertNotEquals(point, new FieldGeodeticPoint<>(new Binary64(0), new Binary64(2), new Binary64(3)));
118         Assertions.assertNotEquals(point, new FieldGeodeticPoint<>(new Binary64(1), new Binary64(0), new Binary64(3)));
119         Assertions.assertNotEquals(point,new FieldGeodeticPoint<>(new Binary64(1), new Binary64(2), new Binary64(0)));
120         Assertions.assertNotEquals(point, new Object());
121         Assertions.assertEquals(point.hashCode(),
122                                 new FieldGeodeticPoint<>(new Binary64(1),
123                                                          new Binary64(2),
124                                                          new Binary64(3)).hashCode());
125         Assertions.assertNotEquals(point.hashCode(),
126                                    new FieldGeodeticPoint<>(new Binary64(1),
127                                                             new Binary64(FastMath.nextUp(2)),
128                                                             new Binary64(3)).hashCode());
129     }
130 
131     /**
132      * check {@link FieldGeodeticPoint#toString()}.
133      */
134     @Test
135     void testToString() {
136         // setup
137         FieldGeodeticPoint<Binary64> point =
138                 new FieldGeodeticPoint<>(new Binary64(FastMath.toRadians(30)),
139                                          new Binary64(FastMath.toRadians(60)),
140                                          new Binary64(90));
141 
142         // action
143         String actual = point.toString();
144 
145         // verify
146         Assertions.assertEquals("{lat: 30 deg, lon: 60 deg, alt: 90}", actual);
147     }
148 
149     @Test
150     void testToGeodeticPoint() {
151         // GIVEN
152         final GeodeticPoint geodeticPoint = new GeodeticPoint(1., 2., 3.);
153         final ComplexField field = ComplexField.getInstance();
154         final FieldGeodeticPoint<Complex> fieldGeodeticPoint = new FieldGeodeticPoint<>(field, geodeticPoint);
155         // WHEN
156         final GeodeticPoint actualPoint = fieldGeodeticPoint.toGeodeticPoint();
157         // THEN
158         Assertions.assertEquals(geodeticPoint.getAltitude(), actualPoint.getAltitude());
159         Assertions.assertEquals(geodeticPoint.getLatitude(), actualPoint.getLatitude());
160         Assertions.assertEquals(geodeticPoint.getLongitude(), actualPoint.getLongitude());
161     }
162 
163 }