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.files.ccsds.ndm.adm.apm;
18  
19  import java.util.Arrays;
20  
21  import org.hipparchus.complex.Quaternion;
22  import org.orekit.errors.OrekitException;
23  import org.orekit.errors.OrekitMessages;
24  import org.orekit.files.ccsds.definitions.CcsdsFrameMapper;
25  import org.orekit.files.ccsds.ndm.adm.AttitudeEndpoints;
26  import org.orekit.files.ccsds.section.CommentsContainer;
27  import org.orekit.frames.Frame;
28  
29  /**
30   * Container for Attitude Parameter Message quaternion logical block.
31   * <p>
32   * Beware that the Orekit getters and setters all rely on SI units. The parsers
33   * and writers take care of converting these SI units into CCSDS mandatory units.
34   * The {@link org.orekit.utils.units.Unit Unit} class provides useful
35   * {@link org.orekit.utils.units.Unit#fromSI(double) fromSi} and
36   * {@link org.orekit.utils.units.Unit#toSI(double) toSI} methods in case the callers
37   * already use CCSDS units instead of the API SI units. The general-purpose
38   * {@link org.orekit.utils.units.Unit Unit} class (without an 's') and the
39   * CCSDS-specific {@link org.orekit.files.ccsds.definitions.Units Units} class
40   * (with an 's') also provide some predefined units. These predefined units and the
41   * {@link org.orekit.utils.units.Unit#fromSI(double) fromSi} and
42   * {@link org.orekit.utils.units.Unit#toSI(double) toSI} conversion methods are indeed
43   * what the parsers and writers use for the conversions.
44   * </p>
45   * @author Bryan Cazabonne
46   * @since 10.2
47   */
48  public class ApmQuaternion extends CommentsContainer {
49  
50      /** Endpoints (i.e. frames A, B and their relationship). */
51      private final AttitudeEndpoints endpoints;
52  
53      /** Quaternion. */
54      private final double[] q;
55  
56      /** Quaternion derivative. */
57      private final double[] qDot;
58  
59      /**
60       * Simple constructor.
61       *
62       * @param frameMapper for creating a {@link Frame}.
63       * @since 13.1.5
64       */
65      public ApmQuaternion(final CcsdsFrameMapper frameMapper) {
66          endpoints = new AttitudeEndpoints(frameMapper);
67          q         = new double[4];
68          qDot      = new double[4];
69          Arrays.fill(q,    Double.NaN);
70          Arrays.fill(qDot, Double.NaN);
71      }
72  
73      /** {@inheritDoc} */
74      @Override
75      public void validate(final double version) {
76          super.validate(version);
77          if (version < 2.0) {
78              endpoints.checkMandatoryEntriesExceptExternalFrame(version,
79                                                                 ApmQuaternionKey.Q_FRAME_A,
80                                                                 ApmQuaternionKey.Q_FRAME_B,
81                                                                 ApmQuaternionKey.Q_DIR);
82              endpoints.checkExternalFrame(ApmQuaternionKey.Q_FRAME_A, ApmQuaternionKey.Q_FRAME_B);
83          } else {
84              endpoints.checkMandatoryEntriesExceptExternalFrame(version,
85                                                                 ApmQuaternionKey.REF_FRAME_A,
86                                                                 ApmQuaternionKey.REF_FRAME_B,
87                                                                 ApmQuaternionKey.Q_DIR);
88              endpoints.checkExternalFrame(ApmQuaternionKey.REF_FRAME_A, ApmQuaternionKey.REF_FRAME_B);
89          }
90          if (Double.isNaN(q[0] + q[1] + q[2] + q[3])) {
91              throw new OrekitException(OrekitMessages.UNINITIALIZED_VALUE_FOR_KEY, "Q{C|1|2|3}");
92          }
93      }
94  
95      /** Get the endpoints (i.e. frames A, B and their relationship).
96       * @return endpoints
97       */
98      public AttitudeEndpoints getEndpoints() {
99          return endpoints;
100     }
101 
102     /**
103      * Get the quaternion.
104      * @return quaternion
105      */
106     public Quaternion getQuaternion() {
107         return new Quaternion(q[0], q[1], q[2], q[3]);
108     }
109 
110     /**
111      * Set quaternion component.
112      * @param index component index (0 is scalar part)
113      * @param value quaternion component
114      */
115     public void setQ(final int index, final double value) {
116         refuseFurtherComments();
117         this.q[index] = value;
118     }
119 
120     /**
121      * Get the quaternion derivative.
122      * @return quaternion derivative
123      */
124     public Quaternion getQuaternionDot() {
125         return new Quaternion(qDot[0], qDot[1], qDot[2], qDot[3]);
126     }
127 
128     /**
129      * Set quaternion derivative component.
130      * @param index component index (0 is scalar part)
131      * @param derivative quaternion derivative component
132      */
133     public void setQDot(final int index, final double derivative) {
134         refuseFurtherComments();
135         this.qDot[index] = derivative;
136     }
137 
138     /** Check if the logical block includes rates.
139      * @return true if logical block includes rates
140      */
141     public boolean hasRates() {
142         return !Double.isNaN(qDot[0] + qDot[1] + qDot[2] + qDot[3]);
143     }
144 
145 }