1   /* Copyright 2002-2026 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;
18  
19  import java.util.HashMap;
20  import java.util.Map;
21  
22  import org.hipparchus.CalculusFieldElement;
23  import org.hipparchus.Field;
24  import org.hipparchus.geometry.euclidean.threed.FieldRotation;
25  import org.hipparchus.geometry.euclidean.threed.Rotation;
26  import org.hipparchus.geometry.euclidean.threed.RotationConvention;
27  import org.hipparchus.geometry.euclidean.threed.RotationOrder;
28  import org.hipparchus.geometry.euclidean.threed.Vector3D;
29  import org.orekit.time.AbsoluteDate;
30  import org.orekit.time.FieldAbsoluteDate;
31  import org.orekit.time.TimeScalarFunction;
32  import org.orekit.time.TimeScales;
33  import org.orekit.time.TimeVectorFunction;
34  import org.orekit.utils.IERSConventions;
35  
36  /** Mean Equator, Mean Equinox Frame.
37   * <p>This frame handles precession effects according to to selected IERS conventions.</p>
38   * <p>Its parent frame is the GCRF frame.
39   * <p>It is sometimes called Mean of Date (MoD) frame.
40   * @author Pascal Parraud
41   */
42  class MODProvider implements FieldBasedTransformProvider {
43  
44      /** Function computing the precession angles. */
45      private final TimeVectorFunction precessionFunction;
46  
47      /** Constant rotation between ecliptic and equator poles at J2000.0. */
48      private final Rotation r4;
49  
50      /** Constant rotations between ecliptic and equator poles at J2000.0. */
51      private final Map<Field<? extends CalculusFieldElement<?>>, FieldRotation<? extends CalculusFieldElement<?>>> fieldR4;
52  
53      /** Simple constructor.
54       * @param conventions IERS conventions to apply
55       * @param timeScales used to define this frame.
56       */
57      MODProvider(final IERSConventions conventions, final TimeScales timeScales) {
58          this.precessionFunction = conventions.getPrecessionFunction(timeScales);
59          final TimeScalarFunction epsilonAFunction =
60                  conventions.getMeanObliquityFunction(timeScales);
61          final AbsoluteDate date0 = conventions.getNutationReferenceEpoch(timeScales);
62          final double epsilon0 = epsilonAFunction.value(date0);
63          r4 = new Rotation(Vector3D.PLUS_I, epsilon0, RotationConvention.FRAME_TRANSFORM);
64          fieldR4 = new HashMap<>();
65      }
66  
67      /** Compute the complete precession rotation.
68       * @param date current date
69       * @param <T> type of the field elements
70       * @return complete precession rotation
71       */
72      public <T extends CalculusFieldElement<T>> FieldRotation<T> getRotation(final FieldAbsoluteDate<T> date) {
73  
74          // compute the precession angles phiA, omegaA, chiA
75          final T[] angles = precessionFunction.value(date);
76  
77          // complete precession
78          return getR4(date.getField()).compose(
79                          new FieldRotation<>(RotationOrder.ZXZ, RotationConvention.FRAME_TRANSFORM,
80                                              angles[0].negate(), angles[1].negate(), angles[2]),
81                          RotationConvention.FRAME_TRANSFORM);
82  
83      }
84  
85      /** Get the constant rotation converted to the specified field.
86       * @param field field to which the elements belong
87       * @param <T> type of the field elements
88       * @return constant rotation converted to the specified field
89       */
90      @SuppressWarnings("unchecked")
91      private <T extends CalculusFieldElement<T>> FieldRotation<T> getR4(final Field<T> field) {
92          synchronized (fieldR4) {
93              return (FieldRotation<T>) fieldR4.computeIfAbsent(field, f -> new FieldRotation<>(field, r4));
94          }
95      }
96  
97  }