1   /* Copyright 2002-2025 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 TransformProvider {
43  
44      /** Conventions. */
45      private final IERSConventions conventions;
46  
47      /** Function computing the precession angles. */
48      private final TimeVectorFunction precessionFunction;
49  
50      /** Constant rotation between ecliptic and equator poles at J2000.0. */
51      private final Rotation r4;
52  
53      /** Constant rotations between ecliptic and equator poles at J2000.0. */
54      private final Map<Field<? extends CalculusFieldElement<?>>, FieldRotation<? extends CalculusFieldElement<?>>> fieldR4;
55  
56      /** Simple constructor.
57       * @param conventions IERS conventions to apply
58       * @param timeScales used to define this frame.
59       */
60      MODProvider(final IERSConventions conventions, final TimeScales timeScales) {
61          this.conventions        = conventions;
62          this.precessionFunction = conventions.getPrecessionFunction(timeScales);
63          final TimeScalarFunction epsilonAFunction =
64                  conventions.getMeanObliquityFunction(timeScales);
65          final AbsoluteDate date0 = conventions.getNutationReferenceEpoch(timeScales);
66          final double epsilon0 = epsilonAFunction.value(date0);
67          r4 = new Rotation(Vector3D.PLUS_I, epsilon0, RotationConvention.FRAME_TRANSFORM);
68          fieldR4 = new HashMap<>();
69      }
70  
71      /** {@inheritDoc} */
72      @Override
73      public Transform getTransform(final AbsoluteDate date) {
74  
75          // compute the precession angles phiA, omegaA, chiA
76          final double[] angles = precessionFunction.value(date);
77  
78          // complete precession
79          final Rotation precession = r4.compose(new Rotation(RotationOrder.ZXZ, RotationConvention.FRAME_TRANSFORM,
80                                                              -angles[0], -angles[1], angles[2]),
81                                                 RotationConvention.FRAME_TRANSFORM);
82  
83          // set up the transform from parent GCRF
84          return new Transform(date, precession);
85  
86      }
87  
88      /** {@inheritDoc} */
89      @SuppressWarnings("unchecked")
90      @Override
91      public <T extends CalculusFieldElement<T>> FieldTransform<T> getTransform(final FieldAbsoluteDate<T> date) {
92  
93          // compute the precession angles phiA, omegaA, chiA
94          final T[] angles = precessionFunction.value(date);
95  
96          final FieldRotation<T> fR4;
97          synchronized (fieldR4) {
98              fR4 = (FieldRotation<T>) fieldR4.computeIfAbsent(date.getField(),
99                                                               f -> new FieldRotation<>((Field<T>) f, r4));
100         }
101 
102         // complete precession
103         final FieldRotation<T> precession = fR4.compose(new FieldRotation<>(RotationOrder.ZXZ, RotationConvention.FRAME_TRANSFORM,
104                                                                             angles[0].negate(),
105                                                                             angles[1].negate(),
106                                                                             angles[2]),
107                                                         RotationConvention.FRAME_TRANSFORM);
108 
109         // set up the transform from parent GCRF
110         return new FieldTransform<>(date, precession);
111 
112     }
113 
114 }