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