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;
18  
19  import java.io.Serializable;
20  import java.util.HashMap;
21  import java.util.Map;
22  
23  import org.hipparchus.CalculusFieldElement;
24  import org.hipparchus.Field;
25  import org.hipparchus.geometry.euclidean.threed.FieldRotation;
26  import org.hipparchus.geometry.euclidean.threed.Rotation;
27  import org.hipparchus.geometry.euclidean.threed.RotationConvention;
28  import org.hipparchus.geometry.euclidean.threed.RotationOrder;
29  import org.hipparchus.geometry.euclidean.threed.Vector3D;
30  import org.orekit.annotation.DefaultDataContext;
31  import org.orekit.data.DataContext;
32  import org.orekit.errors.OrekitException;
33  import org.orekit.errors.OrekitInternalError;
34  import org.orekit.time.AbsoluteDate;
35  import org.orekit.time.FieldAbsoluteDate;
36  import org.orekit.time.TimeScalarFunction;
37  import org.orekit.time.TimeScales;
38  import org.orekit.time.TimeVectorFunction;
39  import org.orekit.utils.IERSConventions;
40  
41  /** Mean Equator, Mean Equinox Frame.
42   * <p>This frame handles precession effects according to to selected IERS conventions.</p>
43   * <p>Its parent frame is the GCRF frame.
44   * <p>It is sometimes called Mean of Date (MoD) frame.
45   * @author Pascal Parraud
46   */
47  class MODProvider implements TransformProvider {
48  
49      /** Serializable UID. */
50      private static final long serialVersionUID = 20130920L;
51  
52      /** Conventions. */
53      private final IERSConventions conventions;
54  
55      /** Function computing the precession angles. */
56      private final transient TimeVectorFunction precessionFunction;
57  
58      /** Constant rotation between ecliptic and equator poles at J2000.0. */
59      private final Rotation r4;
60  
61      /** Constant rotations between ecliptic and equator poles at J2000.0. */
62      private final transient Map<Field<? extends CalculusFieldElement<?>>, FieldRotation<? extends CalculusFieldElement<?>>> fieldR4;
63  
64      /** Simple constructor.
65       * @param conventions IERS conventions to apply
66       * @param timeScales used to define this frame.
67       */
68      MODProvider(final IERSConventions conventions, final TimeScales timeScales) {
69          this.conventions        = conventions;
70          this.precessionFunction = conventions.getPrecessionFunction(timeScales);
71          final TimeScalarFunction epsilonAFunction =
72                  conventions.getMeanObliquityFunction(timeScales);
73          final AbsoluteDate date0 = conventions.getNutationReferenceEpoch(timeScales);
74          final double epsilon0 = epsilonAFunction.value(date0);
75          r4 = new Rotation(Vector3D.PLUS_I, epsilon0, RotationConvention.FRAME_TRANSFORM);
76          fieldR4 = new HashMap<>();
77      }
78  
79      /** {@inheritDoc} */
80      @Override
81      public Transform getTransform(final AbsoluteDate date) {
82  
83          // compute the precession angles phiA, omegaA, chiA
84          final double[] angles = precessionFunction.value(date);
85  
86          // complete precession
87          final Rotation precession = r4.compose(new Rotation(RotationOrder.ZXZ, RotationConvention.FRAME_TRANSFORM,
88                                                              -angles[0], -angles[1], angles[2]),
89                                                 RotationConvention.FRAME_TRANSFORM);
90  
91          // set up the transform from parent GCRF
92          return new Transform(date, precession);
93  
94      }
95  
96      /** {@inheritDoc} */
97      @SuppressWarnings("unchecked")
98      @Override
99      public <T extends CalculusFieldElement<T>> FieldTransform<T> getTransform(final FieldAbsoluteDate<T> date) {
100 
101         // compute the precession angles phiA, omegaA, chiA
102         final T[] angles = precessionFunction.value(date);
103 
104         final FieldRotation<T> fR4;
105         synchronized (fieldR4) {
106             fR4 = (FieldRotation<T>) fieldR4.computeIfAbsent(date.getField(),
107                                                              f -> new FieldRotation<>((Field<T>) f, r4));
108         }
109 
110         // complete precession
111         final FieldRotation<T> precession = fR4.compose(new FieldRotation<>(RotationOrder.ZXZ, RotationConvention.FRAME_TRANSFORM,
112                                                                             angles[0].negate(),
113                                                                             angles[1].negate(),
114                                                                             angles[2]),
115                                                         RotationConvention.FRAME_TRANSFORM);
116 
117         // set up the transform from parent GCRF
118         return new FieldTransform<>(date, precession);
119 
120     }
121 
122     /** Replace the instance with a data transfer object for serialization.
123      * <p>
124      * This intermediate class serializes only the frame key.
125      * </p>
126      * @return data transfer object that will be serialized
127      */
128     @DefaultDataContext
129     private Object writeReplace() {
130         return new DataTransferObject(conventions);
131     }
132 
133     /** Internal class used only for serialization. */
134     @DefaultDataContext
135     private static class DataTransferObject implements Serializable {
136 
137         /** Serializable UID. */
138         private static final long serialVersionUID = 20131209L;
139 
140         /** Conventions. */
141         private final IERSConventions conventions;
142 
143         /** Simple constructor.
144          * @param conventions IERSConventions conventions
145          */
146         DataTransferObject(final IERSConventions conventions) {
147             this.conventions = conventions;
148         }
149 
150         /** Replace the deserialized data transfer object with a {@link MODProvider}.
151          * @return replacement {@link MODProvider}
152          */
153         private Object readResolve() {
154             try {
155                 // retrieve a managed frame
156                 return new MODProvider(conventions,
157                         DataContext.getDefault().getTimeScales());
158             } catch (OrekitException oe) {
159                 throw new OrekitInternalError(oe);
160             }
161         }
162 
163     }
164 
165 }