1   /* Copyright 2022-2025 Luc Maisonobe
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.utils;
18  
19  import org.hipparchus.CalculusFieldElement;
20  import org.hipparchus.Field;
21  import org.hipparchus.geometry.euclidean.threed.FieldVector3D;
22  import org.hipparchus.geometry.euclidean.threed.Rotation;
23  import org.hipparchus.geometry.euclidean.threed.Vector3D;
24  import org.hipparchus.util.Binary64;
25  import org.hipparchus.util.Binary64Field;
26  import org.hipparchus.util.FastMath;
27  import org.junit.jupiter.api.Assertions;
28  import org.junit.jupiter.api.BeforeEach;
29  import org.junit.jupiter.api.Test;
30  import org.orekit.Utils;
31  import org.orekit.bodies.CelestialBody;
32  import org.orekit.bodies.CelestialBodyFactory;
33  import org.orekit.frames.*;
34  import org.orekit.time.AbsoluteDate;
35  import org.orekit.time.FieldAbsoluteDate;
36  import org.orekit.time.TimeScalesFactory;
37  
38  public class ExtendedPositionProviderAdapterTest {
39  
40      @Test
41      public void testDouble() {
42  
43          final Frame         eme2000   = FramesFactory.getEME2000();
44          final CelestialBody moon      = CelestialBodyFactory.getMoon();
45          final Frame         moonFrame = new ExtendedPositionProviderAdapter(eme2000, moon, "moon-frame");
46  
47          final AbsoluteDate t0 = new AbsoluteDate("2000-01-22T13:30:00", TimeScalesFactory.getUTC());
48          double maxP = 0;
49          double maxV = 0;
50          double maxA = 0;
51          double maxR = 0;
52          for (double dt = 0; dt < Constants.JULIAN_DAY; dt += 60.0) {
53              final AbsoluteDate t = t0.shiftedBy(dt);
54              final TimeStampedPVCoordinates pv = moon.getPVCoordinates(t, moonFrame);
55              maxP = FastMath.max(maxP, pv.getPosition().getNorm());
56              maxV = FastMath.max(maxV, pv.getVelocity().getNorm());
57              maxA = FastMath.max(maxA, pv.getAcceleration().getNorm());
58              maxR = FastMath.max(maxR, moonFrame.getTransformTo(eme2000, t).getRotation().getAngle());
59          }
60          Assertions.assertEquals(0.0, maxP, 5.0e-7);
61          Assertions.assertEquals(0.0, maxV, 1.1e-12);
62          Assertions.assertEquals(0.0, maxA, 2.8e-18);
63          Assertions.assertEquals(0.0, maxR, 1.0e-30);
64  
65      }
66  
67      @Test
68      public void testField() {
69          doTestField(Binary64Field.getInstance());
70      }
71  
72      private <T extends CalculusFieldElement<T>> void doTestField(final Field<T> field) {
73  
74          final Frame         eme2000   = FramesFactory.getEME2000();
75          final CelestialBody moon      = CelestialBodyFactory.getMoon();
76          final Frame         moonFrame = new ExtendedPositionProviderAdapter(eme2000, moon, "moon-frame");
77  
78          final FieldAbsoluteDate<T> t0 = new FieldAbsoluteDate<>(field,
79                                                                  new AbsoluteDate("2000-01-22T13:30:00",
80                                                                                   TimeScalesFactory.getUTC()));
81          T maxP = field.getZero();
82          T maxV = field.getZero();
83          T maxA = field.getZero();
84          T maxR = field.getZero();
85          for (double dt = 0; dt < Constants.JULIAN_DAY; dt += 60.0) {
86              final FieldAbsoluteDate<T> t = t0.shiftedBy(dt);
87              final TimeStampedFieldPVCoordinates<T> pv = moon.getPVCoordinates(t, moonFrame);
88              maxP = FastMath.max(maxP, pv.getPosition().getNorm());
89              maxV = FastMath.max(maxV, pv.getVelocity().getNorm());
90              maxA = FastMath.max(maxA, pv.getAcceleration().getNorm());
91              maxR = FastMath.max(maxR, moonFrame.getTransformTo(eme2000, t).getRotation().getAngle());
92          }
93          Assertions.assertEquals(0.0, maxP.getReal(), 5.0e-7);
94          Assertions.assertEquals(0.0, maxV.getReal(), 1.1e-12);
95          Assertions.assertEquals(0.0, maxA.getReal(), 2.8e-18);
96          Assertions.assertEquals(0.0, maxR.getReal(), 1.0e-30);
97  
98      }
99  
100     @Test
101     void testGetStaticTransform() {
102         // GIVEN
103         final TestProvider provider = new TestProvider();
104         final Frame inputFrame = FramesFactory.getGCRF();
105         final AbsoluteDate date = AbsoluteDate.ARBITRARY_EPOCH;
106         final ExtendedPositionProviderAdapter adapter = new ExtendedPositionProviderAdapter(inputFrame, provider, "");
107         final Frame frame = FramesFactory.getEME2000();
108         // WHEN
109         final StaticTransform staticTransform = adapter.getStaticTransformTo(frame, date);
110         // THEN
111         final Transform transform = adapter.getTransformTo(frame, date);
112         Assertions.assertEquals(transform.getTranslation(), staticTransform.getTranslation());
113         Assertions.assertEquals(0., Rotation.distance(transform.getRotation(), staticTransform.getRotation()));
114     }
115 
116     @Test
117     void testGetStaticTransformField() {
118         // GIVEN
119         final TestProvider provider = new TestProvider();
120         final Frame inputFrame = FramesFactory.getGCRF();
121         final AbsoluteDate date = AbsoluteDate.ARBITRARY_EPOCH;
122         final ExtendedPositionProviderAdapter adapter = new ExtendedPositionProviderAdapter(inputFrame, provider, "");
123         final Frame frame = FramesFactory.getEME2000();
124         final Binary64Field field = Binary64Field.getInstance();
125         final FieldAbsoluteDate<Binary64> fieldAbsoluteDate = new FieldAbsoluteDate<>(field, date);
126         // WHEN
127         final FieldStaticTransform<Binary64> staticTransform = adapter.getStaticTransformTo(frame, fieldAbsoluteDate);
128         // THEN
129         final FieldTransform<Binary64> transform = adapter.getTransformTo(frame, fieldAbsoluteDate);
130         Assertions.assertEquals(transform.getTranslation(), staticTransform.getTranslation());
131         Assertions.assertEquals(0., Rotation.distance(transform.getRotation().toRotation(),
132                 staticTransform.getRotation().toRotation()));
133     }
134 
135     @BeforeEach
136     public void setUp() {
137         Utils.setDataRoot("regular-data");
138     }
139 
140     private static class TestProvider implements ExtendedPositionProvider {
141 
142         @Override
143         public <T extends CalculusFieldElement<T>> FieldVector3D<T> getPosition(FieldAbsoluteDate<T> date, Frame frame) {
144             return new FieldVector3D<>(date.getField(), Vector3D.MINUS_I);
145         }
146     }
147 
148 }