1   /* Copyright 2022-2025 Romain Serra
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  
18  package org.orekit.utils;
19  
20  import org.hipparchus.geometry.euclidean.threed.Vector3D;
21  import org.hipparchus.linear.MatrixUtils;
22  import org.hipparchus.linear.RealMatrix;
23  import org.junit.jupiter.api.Assertions;
24  import org.junit.jupiter.api.Test;
25  import org.junit.jupiter.params.ParameterizedTest;
26  import org.junit.jupiter.params.provider.EnumSource;
27  import org.orekit.frames.*;
28  import org.orekit.orbits.CartesianOrbit;
29  import org.orekit.orbits.OrbitType;
30  import org.orekit.orbits.PositionAngleType;
31  import org.orekit.propagation.StateCovariance;
32  import org.orekit.time.AbsoluteDate;
33  
34  class CartesianCovarianceUtilsTest {
35  
36      @Test
37      void testConvertToLofType() {
38          // GIVEN
39          final Frame inputFrame = FramesFactory.getEME2000();
40          final Frame outputFrame = FramesFactory.getGCRF();
41          final RealMatrix matrix = buildSomeMatrix();
42          final AbsoluteDate date = AbsoluteDate.ARBITRARY_EPOCH;
43          // WHEN
44          final RealMatrix actualMatrix = CartesianCovarianceUtils.changeReferenceFrame(inputFrame, matrix, date,
45                  outputFrame);
46          // THEN
47          final Transform transform = inputFrame.getTransformTo(outputFrame, date);
48          final RealMatrix jacobianMatrix = MatrixUtils.createRealMatrix(transform.getPVJacobian());
49          final RealMatrix expectedMatrix = jacobianMatrix.multiply(matrix).multiplyTransposed(jacobianMatrix);
50          Assertions.assertEquals(0., expectedMatrix.subtract(actualMatrix).getNorm1(), 1e-15);
51      }
52  
53      @ParameterizedTest
54      @EnumSource(value = LOFType.class, names = {"QSW_INERTIAL", "TNW_INERTIAL", "LVLH_INERTIAL", "NTW_INERTIAL"})
55      void testConvertToLofType(final LOFType lofType) {
56          // GIVEN
57          final Vector3D position = new Vector3D(1.0, 2.0, 3.0);
58          final Vector3D velocity = new Vector3D(4., -0.5);
59          final RealMatrix matrix = buildSomeMatrix();
60          // WHEN
61          final RealMatrix actualMatrix = CartesianCovarianceUtils.convertToLofType(position, velocity, matrix, lofType);
62          // THEN
63          final StateCovariance covariance = new StateCovariance(matrix, AbsoluteDate.ARBITRARY_EPOCH,
64                  FramesFactory.getGCRF(), OrbitType.CARTESIAN, null);
65          final CartesianOrbit orbit = new CartesianOrbit(new PVCoordinates(position, velocity), covariance.getFrame(),
66                  covariance.getDate(), 1.);
67          final RealMatrix expectedMatrix = covariance.changeCovarianceFrame(orbit, lofType).getMatrix();
68          Assertions.assertEquals(0, expectedMatrix.subtract(actualMatrix).getNorm1(), 1e-15);
69      }
70  
71      @ParameterizedTest
72      @EnumSource(LOFType.class)
73      void testConvertFromLofType(final LOFType lofType) {
74          // GIVEN
75          final Vector3D position = new Vector3D(1.0, 2.0, 3.0);
76          final Vector3D velocity = new Vector3D(4., -0.5);
77          final RealMatrix expectedMatrix = buildSomeMatrix();
78          // WHEN
79          final RealMatrix matrix = CartesianCovarianceUtils.convertToLofType(position, velocity, expectedMatrix, lofType);
80          // THEN
81          final RealMatrix actualMatrix = CartesianCovarianceUtils.convertFromLofType(lofType, matrix, position, velocity);
82          Assertions.assertEquals(0., expectedMatrix.subtract(actualMatrix).getNorm1(), 1e-14);
83      }
84  
85      private static RealMatrix buildSomeMatrix() {
86          final RealMatrix matrix = MatrixUtils.createRealIdentityMatrix(6);
87          final double entry = 0.4;
88          final int index2 = 2;
89          final int index3 = 3;
90          matrix.setEntry(index3, index3, entry);
91          matrix.setEntry(index2, index2, entry);
92          final int index4 = 4;
93          final int index5 = 5;
94          final double otherEntry = -0.2;
95          matrix.setEntry(index4, index5, otherEntry);
96          matrix.setEntry(index5, index4, otherEntry);
97          return matrix;
98      }
99  
100 }