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.files.ccsds.ndm.adm.apm;
18  
19  import org.orekit.files.ccsds.definitions.Units;
20  import org.orekit.files.ccsds.utils.ContextBinding;
21  import org.orekit.files.ccsds.utils.lexical.ParseToken;
22  import org.orekit.files.ccsds.utils.lexical.TokenType;
23  import org.orekit.utils.units.Unit;
24  
25  /** Keys for {@link ApmData APM Euler angles} entries.
26   * @author Bryan Cazabonne
27   * @since 10.2
28   */
29  public enum EulerKey {
30  
31      /** Rotation angles wrapping element in XML files (ADM V1 only). */
32      rotationAngles((token, context, container) -> true),
33  
34      /** Rotation rates wrapping element in XML files (ADM V1 only). */
35      rotationRates((token, context, container) -> true),
36  
37      /** Comment entry. */
38      COMMENT((token, context, container) ->
39              token.getType() == TokenType.ENTRY ? container.addComment(token.getContentAsNormalizedString()) : true),
40  
41      /** First reference frame entry (only for ADM V1). */
42      EULER_FRAME_A((token, context, container) -> token.processAsFrame(container.getEndpoints()::setFrameA, context, true, true, true)),
43  
44      /** First reference frame entry.
45       * @since 12.0
46       */
47      REF_FRAME_A((token, context, container) -> token.processAsFrame(container.getEndpoints()::setFrameA, context, true, true, true)),
48  
49      /** Second reference frame entry (only for ADM V1). */
50      EULER_FRAME_B((token, context, container) -> {
51          if (token.getType() == TokenType.ENTRY) {
52              container.checkNotNull(container.getEndpoints().getFrameA(), EULER_FRAME_A.name());
53              final boolean aIsSpaceraftBody = container.getEndpoints().getFrameA().asSpacecraftBodyFrame() != null;
54              return token.processAsFrame(container.getEndpoints()::setFrameB, context,
55                                          aIsSpaceraftBody, aIsSpaceraftBody, !aIsSpaceraftBody);
56          }
57          return true;
58      }),
59  
60      /** Second reference frame entry.
61       * @since 12.0
62       */
63      REF_FRAME_B((token, context, container) -> {
64          if (token.getType() == TokenType.ENTRY) {
65              container.checkNotNull(container.getEndpoints().getFrameA(), EULER_FRAME_A.name());
66              final boolean aIsSpaceraftBody = container.getEndpoints().getFrameA().asSpacecraftBodyFrame() != null;
67              return token.processAsFrame(container.getEndpoints()::setFrameB, context,
68                                          aIsSpaceraftBody, aIsSpaceraftBody, !aIsSpaceraftBody);
69          }
70          return true;
71      }),
72  
73      /** Rotation direction entry (ADM V1 only). */
74      EULER_DIR((token, context, container) -> {
75          if (token.getType() == TokenType.ENTRY) {
76              container.getEndpoints().setA2b(token.getContentAsUppercaseCharacter() == 'A');
77          }
78          return true;
79      }),
80  
81      /** Rotation sequence entry. */
82      EULER_ROT_SEQ((token, context, container) -> token.processAsRotationOrder(container::setEulerRotSeq)),
83  
84      /** Reference frame for rate entry (ADM V1 only). */
85      RATE_FRAME((token, context, container) -> {
86          if (token.getType() == TokenType.ENTRY) {
87              final String content = token.getContentAsUppercaseString();
88              final char   suffix  = content.charAt(content.length() - 1);
89              container.setRateFrameIsA(suffix == 'A');
90          }
91          return true;
92      }),
93  
94      /** X body rotation angle entry (ADM V1 only). */
95      X_ANGLE((token, context, container) -> token.processAsLabeledDouble('X', Unit.DEGREE, context.getParsedUnitsBehavior(),
96                                                                          container::setLabeledRotationAngle)),
97  
98      /** Y body rotation angle entry (ADM V1 only). */
99      Y_ANGLE((token, context, container) -> token.processAsLabeledDouble('Y', Unit.DEGREE, context.getParsedUnitsBehavior(),
100                                                                         container::setLabeledRotationAngle)),
101 
102     /** Z body rotation angle entry (ADM V1 only). */
103     Z_ANGLE((token, context, container) -> token.processAsLabeledDouble('Z', Unit.DEGREE, context.getParsedUnitsBehavior(),
104                                                                         container::setLabeledRotationAngle)),
105 
106     /** X body rotation rate entry (ADM V1 only). */
107     X_RATE((token, context, container) -> token.processAsLabeledDouble('X', Units.DEG_PER_S, context.getParsedUnitsBehavior(),
108                                                                        container::setLabeledRotationRate)),
109 
110     /** Y body rotation rate entry (ADM V1 only). */
111     Y_RATE((token, context, container) -> token.processAsLabeledDouble('Y', Units.DEG_PER_S, context.getParsedUnitsBehavior(),
112                                                                        container::setLabeledRotationRate)),
113 
114     /** Z body rotation rate entry (ADM V1 only). */
115     Z_RATE((token, context, container) -> token.processAsLabeledDouble('Z', Units.DEG_PER_S, context.getParsedUnitsBehavior(),
116                                                                        container::setLabeledRotationRate)),
117 
118     /** First body rotation angle entry.
119      * @since 12.0
120      */
121     ANGLE_1((token, context, container) -> token.processAsIndexedDouble(0, Unit.DEGREE, context.getParsedUnitsBehavior(),
122                                                                         container::setIndexedRotationAngle)),
123 
124     /** Second body rotation angle entry.
125      * @since 12.0
126      */
127     ANGLE_2((token, context, container) -> token.processAsIndexedDouble(1, Unit.DEGREE, context.getParsedUnitsBehavior(),
128                                                                         container::setIndexedRotationAngle)),
129 
130     /** Third body rotation angle entry.
131      * @since 12.0
132      */
133     ANGLE_3((token, context, container) -> token.processAsIndexedDouble(2, Unit.DEGREE, context.getParsedUnitsBehavior(),
134                                                                         container::setIndexedRotationAngle)),
135 
136     /** First body rotation rate entry.
137      * @since 12.0
138      */
139     ANGLE_1_DOT((token, context, container) -> token.processAsIndexedDouble(0, Units.DEG_PER_S, context.getParsedUnitsBehavior(),
140                                                                             container::setIndexedRotationRate)),
141 
142     /** Second body rotation rate entry.
143      * @since 12.0
144      */
145     ANGLE_2_DOT((token, context, container) -> token.processAsIndexedDouble(1, Units.DEG_PER_S, context.getParsedUnitsBehavior(),
146                                                                             container::setIndexedRotationRate)),
147 
148     /** Third body rotation rate entry.
149      * @since 12.0
150      */
151     ANGLE_3_DOT((token, context, container) -> token.processAsIndexedDouble(2, Units.DEG_PER_S, context.getParsedUnitsBehavior(),
152                                                                             container::setIndexedRotationRate));
153 
154     /** Processing method. */
155     private final transient TokenProcessor processor;
156 
157     /** Simple constructor.
158      * @param processor processing method
159      */
160     EulerKey(final TokenProcessor processor) {
161         this.processor = processor;
162     }
163 
164     /** Process one token.
165      * @param token token to process
166      * @param context context binding
167      * @param container container to fill
168      * @return true of token was accepted
169      */
170     public boolean process(final ParseToken token, final ContextBinding context, final Euler container) {
171         return processor.process(token, context, container);
172     }
173 
174     /** Interface for processing one token. */
175     interface TokenProcessor {
176         /** Process one token.
177          * @param token token to process
178          * @param context context binding
179          * @param container container to fill
180          * @return true of token was accepted
181          */
182         boolean process(ParseToken token, ContextBinding context, Euler container);
183     }
184 
185 }