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