1   /* Copyright 2002-2024 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.frames.encounter;
18  
19  import org.hipparchus.Field;
20  import org.hipparchus.geometry.euclidean.threed.FieldRotation;
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.geometry.euclidean.twod.FieldVector2D;
25  import org.hipparchus.geometry.euclidean.twod.Vector2D;
26  import org.hipparchus.linear.BlockFieldMatrix;
27  import org.hipparchus.linear.BlockRealMatrix;
28  import org.hipparchus.linear.FieldMatrix;
29  import org.hipparchus.linear.RealMatrix;
30  import org.hipparchus.util.Binary64;
31  import org.hipparchus.util.Binary64Field;
32  import org.hipparchus.util.FastMath;
33  import org.hipparchus.util.MathUtils;
34  import org.hipparchus.util.SinCos;
35  import org.junit.jupiter.api.Assertions;
36  import org.junit.jupiter.api.DisplayName;
37  import org.junit.jupiter.api.Test;
38  import org.mockito.Mockito;
39  import org.orekit.TestUtils;
40  import org.orekit.time.AbsoluteDate;
41  import org.orekit.time.FieldAbsoluteDate;
42  import org.orekit.utils.FieldPVCoordinates;
43  import org.orekit.utils.PVCoordinates;
44  
45  class DefaultEncounterLOFTest {
46  
47      @Test
48      @DisplayName("Test rotationFromInertial default method")
49      void testReturnExpectedRotation() {
50          // Given
51          final AbsoluteDate arbritraryEpoch = AbsoluteDate.ARBITRARY_EPOCH;
52          final PVCoordinates originPV = new PVCoordinates(new Vector3D(6378000. + 400000., 0., 0.),
53                                                           new Vector3D(0., 7668.63, 0.));
54  
55          final PVCoordinates otherPV = new PVCoordinates(new Vector3D(6378000. + 400000 + 1., 0., 0.),
56                                                          new Vector3D(0., 0., 7668.63));
57  
58          final EncounterLOF encounterFrame = new DefaultEncounterLOF(otherPV);
59  
60          // When
61          final Rotation   computedRotation       = encounterFrame.rotationFromInertial(arbritraryEpoch, originPV);
62          final RealMatrix computedRotationMatrix = new BlockRealMatrix(computedRotation.getMatrix());
63  
64          // Then
65          final SinCos sinCos = FastMath.sinCos(MathUtils.SEMI_PI / 2);
66          final double sin    = sinCos.sin();
67          final double cos    = sinCos.cos();
68  
69          final RealMatrix expectedRotationMatrix = new BlockRealMatrix(
70                  new double[][] { { 1, 0, 0 }, { 0, cos, sin }, { 0, -sin, cos } });
71  
72          TestUtils.validateRealMatrix(expectedRotationMatrix, computedRotationMatrix, 1e-15);
73  
74      }
75  
76      @Test
77      @DisplayName("Test rotationFromInertial (field version) default method")
78      void testReturnExpectedFieldRotation() {
79          // Given
80          final Binary64Field     field           = Binary64Field.getInstance();
81          final FieldAbsoluteDate<Binary64> arbritraryEpoch = new FieldAbsoluteDate<>(field, AbsoluteDate.ARBITRARY_EPOCH);
82          final FieldPVCoordinates<Binary64> originPV = new FieldPVCoordinates<>(field, new PVCoordinates(
83                  new Vector3D(6378000. + 400000., 0., 0.), new Vector3D(0., 7668.63, 0.)));
84  
85          final PVCoordinates otherPV = new PVCoordinates(
86                  new Vector3D(6378000. + 400000 + 1., 0., 0.), new Vector3D(0., 0., 7668.63));
87  
88          final EncounterLOF encounterFrame = new DefaultEncounterLOF(otherPV);
89  
90          // When
91          final FieldRotation<Binary64> computedRotation       = encounterFrame.rotationFromInertial(field, arbritraryEpoch, originPV);
92          final FieldMatrix<Binary64>   computedRotationMatrix = new BlockFieldMatrix<>(computedRotation.getMatrix());
93  
94          // Then
95          final SinCos sinCos = FastMath.sinCos(MathUtils.SEMI_PI / 2);
96          final double sin    = sinCos.sin();
97          final double cos    = sinCos.cos();
98  
99          final RealMatrix expectedRotationMatrix = new BlockRealMatrix(
100                 new double[][] { { 1, 0, 0 }, { 0, cos, sin }, { 0, -sin, cos } });
101 
102         TestUtils.validateFieldMatrix(expectedRotationMatrix, computedRotationMatrix, 1e-15);
103     }
104 
105     @Test
106     @DisplayName("Test isQuasiInertial method")
107     void testReturnTrue() {
108         // Given
109         final PVCoordinates otherMock = Mockito.mock(PVCoordinates.class);
110 
111         final EncounterLOF encounterLOF = new DefaultEncounterLOF(otherMock);
112 
113         // When
114         final boolean returnedInertialFlag = encounterLOF.isQuasiInertial();
115 
116         // Then
117         Assertions.assertTrue(returnedInertialFlag);
118 
119     }
120 
121     @Test
122     @DisplayName("Test projectOntoCollisionPlane")
123     void testReturnExpectedProjectedMatrix() {
124         // Given
125         final PVCoordinates otherPVMock = Mockito.mock(PVCoordinates.class);
126 
127         final RealMatrix matrix = new BlockRealMatrix(new double[][] {
128                 { 1, 2, 3 },
129                 { 4, 5, 6 },
130                 { 7, 8, 9 }
131         });
132 
133         final FieldMatrix<Binary64> fieldMatrix = new BlockFieldMatrix<>(new Binary64[][] {
134                 { new Binary64(1), new Binary64(2), new Binary64(3) },
135                 { new Binary64(4), new Binary64(5), new Binary64(6) },
136                 { new Binary64(7), new Binary64(8), new Binary64(9) }
137         });
138 
139         final AbstractEncounterLOF encounterFrame = new DefaultEncounterLOF(otherPVMock);
140 
141         // When
142         final RealMatrix            computedProjectedMatrix      = encounterFrame.projectOntoCollisionPlane(matrix);
143         final FieldMatrix<Binary64> computedProjectedFieldMatrix = encounterFrame.projectOntoCollisionPlane(fieldMatrix);
144 
145         // Then
146         final RealMatrix expectedProjectedMatrix = new BlockRealMatrix(new double[][] {
147                 { 1, 2 },
148                 { 4, 5 }
149         });
150 
151         TestUtils.validateRealMatrix(expectedProjectedMatrix, computedProjectedMatrix, 1e-15);
152         TestUtils.validateFieldMatrix(expectedProjectedMatrix, computedProjectedFieldMatrix, 1e-15);
153 
154     }
155 
156     @Test
157     @DisplayName("Test projectOntoCollisionPlane")
158     void testReturnExpectedProjectedVector() {
159         // Given
160         final PVCoordinates otherPVMock = Mockito.mock(PVCoordinates.class);
161 
162         final Vector3D vector = new Vector3D(1, 2, 3);
163         final FieldVector3D<Binary64> fieldVector = new FieldVector3D<>(new Binary64(1),
164                                                                         new Binary64(2),
165                                                                         new Binary64(3));
166 
167         final EncounterLOF encounterFrame = new DefaultEncounterLOF(otherPVMock);
168 
169         // When
170         final Vector2D                computedProjectedVector      = encounterFrame.projectOntoCollisionPlane(vector);
171         final FieldVector2D<Binary64> computedProjectedFieldVector = encounterFrame.projectOntoCollisionPlane(fieldVector);
172 
173         // Then
174         final Vector2D expectedProjectedVector = new Vector2D(1, 2);
175 
176         TestUtils.validateVector2D(expectedProjectedVector, computedProjectedVector, 1e-15);
177         TestUtils.validateFieldVector2D(expectedProjectedVector, computedProjectedFieldVector, 1e-15);
178 
179     }
180 
181     @Test
182     @DisplayName("Test getAxisNormalToCollisionPlane")
183     void testReturnExpectedAxisNormalToCollisionPlane() {
184         // Given
185         final Field<Binary64> field = Binary64Field.getInstance();
186 
187         final PVCoordinates otherPV = Mockito.mock(PVCoordinates.class);
188 
189         final EncounterLOF encounterFrame = new DefaultEncounterLOF(otherPV);
190 
191         // When
192         final Vector3D                gottenAxis      = encounterFrame.getAxisNormalToCollisionPlane();
193         final FieldVector3D<Binary64> gottenFieldAxis = encounterFrame.getAxisNormalToCollisionPlane(field);
194 
195         // Then
196         final Vector3D expectedAxis = new Vector3D(0, 0, 1);
197 
198         TestUtils.validateVector3D(expectedAxis, gottenAxis, 1e-15);
199         TestUtils.validateFieldVector3D(expectedAxis, gottenFieldAxis, 1e-15);
200     }
201 
202 }