1   /* Copyright 2002-2022 CS GROUP
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.models.earth.tessellation;
18  
19  import java.io.IOException;
20  
21  import org.hipparchus.geometry.euclidean.threed.Vector3D;
22  import org.hipparchus.geometry.partitioning.Region.Location;
23  import org.hipparchus.geometry.spherical.twod.S2Point;
24  import org.hipparchus.geometry.spherical.twod.SphericalPolygonsSet;
25  import org.hipparchus.util.FastMath;
26  import org.junit.After;
27  import org.junit.Assert;
28  import org.junit.Before;
29  import org.junit.Test;
30  import org.orekit.Utils;
31  import org.orekit.bodies.GeodeticPoint;
32  import org.orekit.bodies.OneAxisEllipsoid;
33  import org.orekit.frames.FramesFactory;
34  import org.orekit.utils.Constants;
35  import org.orekit.utils.IERSConventions;
36  
37  public class DivertedSingularityAimingTest {
38  
39      @Test
40      public void testSingularityOutside() {
41          Assert.assertEquals(1, aiming.getSingularPoints().size());
42          final GeodeticPoint singularity = aiming.getSingularPoints().get(0);
43          Assert.assertEquals(Location.OUTSIDE, aoi.checkPoint(toS2Point(singularity)));
44      }
45  
46      @Test
47      public void testAroundSingularity() throws IOException {
48  
49          GeodeticPoint singularityGP = aiming.getSingularPoints().get(0);
50          Vector3D singularity = earth.transform(singularityGP);
51          Assert.assertEquals(GeodeticPoint.SOUTH_POLE.getLatitude(), singularityGP.getLatitude(), 1.0e-10);
52  
53          // in a small disk (less than 1cm radius) around singularity, aiming direction changes a lot
54          double lat = GeodeticPoint.SOUTH_POLE.getLatitude() + 1.0e-9;
55          GeodeticPoint gp000  = new GeodeticPoint(lat, 0.0, 0.0);
56          Vector3D      p000   = earth.transform(gp000);
57          Vector3D      dir000 = aiming.alongTileDirection(p000, gp000);
58          GeodeticPoint gp090  = new GeodeticPoint(lat, 0.5 * FastMath.PI, 0.0);
59          Vector3D      p090   = earth.transform(gp090);
60          Vector3D      dir090 = aiming.alongTileDirection(p090, gp090);
61          Assert.assertEquals(0.0064, Vector3D.distance(singularity, p000), 1.0e-4);
62          Assert.assertEquals(0.0064, Vector3D.distance(singularity, p090), 1.0e-4);
63          Assert.assertEquals(FastMath.PI, Vector3D.angle(dir000, dir090), 5.0e-7);
64  
65      }
66  
67      @Test
68      public void testOppositeSingularity() throws IOException {
69  
70          GeodeticPoint singularityGP = aiming.getSingularPoints().get(0);
71          Vector3D singularity = earth.transform(singularityGP);
72          Vector3D opposite    = singularity.negate();
73          GeodeticPoint oppositeGP = earth.transform(opposite, earth.getBodyFrame(), null);
74          Assert.assertEquals(GeodeticPoint.NORTH_POLE.getLatitude(), oppositeGP.getLatitude(), 1.0e-10);
75  
76          // around opposite of singularity, aiming direction is almost constant
77          // (as we use dipole field to model aiming direction, there is only one singularity)
78          Vector3D refDir = aiming.alongTileDirection(opposite, oppositeGP);
79          double lat = GeodeticPoint.NORTH_POLE.getLatitude() - 1.0e-9;
80          for (double lon = 0; lon < 2 * FastMath.PI; lon += 0.001) {
81              GeodeticPoint gp = new GeodeticPoint(lat, lon, 0.0);
82              Vector3D      p  = earth.transform(gp);
83              Vector3D      dir = aiming.alongTileDirection(p, gp);
84              Assert.assertEquals(0.0064, Vector3D.distance(opposite, p), 1.0e-4);
85              Assert.assertEquals(0.0, Vector3D.angle(refDir, dir), 1.1e-9);
86          }
87  
88      }
89  
90      private S2Point toS2Point(final GeodeticPoint point) {
91          return new S2Point(point.getLongitude(), 0.5 * FastMath.PI - point.getLatitude());
92      }
93  
94      @Before
95      public void setUp() {
96          Utils.setDataRoot("regular-data");
97          earth = new OneAxisEllipsoid(Constants.WGS84_EARTH_EQUATORIAL_RADIUS,
98                                       Constants.WGS84_EARTH_FLATTENING,
99                                       FramesFactory.getITRF(IERSConventions.IERS_2010, true));
100         aoi = new SphericalPolygonsSet(1.e-9, new S2Point[] {
101             new S2Point(FastMath.toRadians(-120.0), FastMath.toRadians(5.0)),
102             new S2Point(FastMath.toRadians(   0.0), FastMath.toRadians(5.0)),
103             new S2Point(FastMath.toRadians( 120.0), FastMath.toRadians(5.0))
104         });
105         aiming = new DivertedSingularityAiming(aoi);
106     }
107 
108     @After
109     public void tearDown() {
110         earth  = null;
111         aoi    = null;
112         aiming = null;
113     }
114 
115     private OneAxisEllipsoid earth;
116     private SphericalPolygonsSet aoi;
117     private TileAiming aiming;
118 
119 }