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