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