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.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   * @author Bryan Cazabonne
30   * @since 10.2
31   */
32  public class ApmQuaternion extends CommentsContainer {
33  
34      /** Endpoints (i.e. frames A, B and their relationship). */
35      private final AttitudeEndpoints endpoints;
36  
37      /** Quaternion. */
38      private double[] q;
39  
40      /** Quaternion derivative. */
41      private double[] qDot;
42  
43      /** Simple constructor.
44       */
45      public ApmQuaternion() {
46          endpoints = new AttitudeEndpoints();
47          q         = new double[4];
48          qDot      = new double[4];
49          Arrays.fill(q,    Double.NaN);
50          Arrays.fill(qDot, Double.NaN);
51      }
52  
53      /** {@inheritDoc} */
54      @Override
55      public void validate(final double version) {
56          super.validate(version);
57          if (version < 2.0) {
58              endpoints.checkMandatoryEntriesExceptExternalFrame(version,
59                                                                 ApmQuaternionKey.Q_FRAME_A,
60                                                                 ApmQuaternionKey.Q_FRAME_B,
61                                                                 ApmQuaternionKey.Q_DIR);
62              endpoints.checkExternalFrame(ApmQuaternionKey.Q_FRAME_A, ApmQuaternionKey.Q_FRAME_B);
63          } else {
64              endpoints.checkMandatoryEntriesExceptExternalFrame(version,
65                                                                 ApmQuaternionKey.REF_FRAME_A,
66                                                                 ApmQuaternionKey.REF_FRAME_B,
67                                                                 ApmQuaternionKey.Q_DIR);
68              endpoints.checkExternalFrame(ApmQuaternionKey.REF_FRAME_A, ApmQuaternionKey.REF_FRAME_B);
69          }
70          if (Double.isNaN(q[0] + q[1] + q[2] + q[3])) {
71              throw new OrekitException(OrekitMessages.UNINITIALIZED_VALUE_FOR_KEY, "Q{C|1|2|3}");
72          }
73      }
74  
75      /** Get the endpoints (i.e. frames A, B and their relationship).
76       * @return endpoints
77       */
78      public AttitudeEndpoints getEndpoints() {
79          return endpoints;
80      }
81  
82      /**
83       * Get the quaternion.
84       * @return quaternion
85       */
86      public Quaternion getQuaternion() {
87          return new Quaternion(q[0], q[1], q[2], q[3]);
88      }
89  
90      /**
91       * Set quaternion component.
92       * @param index component index (0 is scalar part)
93       * @param value quaternion component
94       */
95      public void setQ(final int index, final double value) {
96          refuseFurtherComments();
97          this.q[index] = value;
98      }
99  
100     /**
101      * Get the quaternion derivative.
102      * @return quaternion derivative
103      */
104     public Quaternion getQuaternionDot() {
105         return new Quaternion(qDot[0], qDot[1], qDot[2], qDot[3]);
106     }
107 
108     /**
109      * Set quaternion derivative component.
110      * @param index component index (0 is scalar part)
111      * @param derivative quaternion derivative component
112      */
113     public void setQDot(final int index, final double derivative) {
114         refuseFurtherComments();
115         this.qDot[index] = derivative;
116     }
117 
118     /** Check if the logical block includes rates.
119      * @return true if logical block includes rates
120      */
121     public boolean hasRates() {
122         return !Double.isNaN(qDot[0] + qDot[1] + qDot[2] + qDot[3]);
123     }
124 
125 }