ApmQuaternion.java

  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. import java.util.Arrays;

  19. import org.hipparchus.complex.Quaternion;
  20. import org.orekit.errors.OrekitException;
  21. import org.orekit.errors.OrekitMessages;
  22. import org.orekit.files.ccsds.ndm.adm.AttitudeEndpoints;
  23. import org.orekit.files.ccsds.section.CommentsContainer;

  24. /**
  25.  * Container for Attitude Parameter Message quaternion logical block.
  26.  * <p>
  27.  * Beware that the Orekit getters and setters all rely on SI units. The parsers
  28.  * and writers take care of converting these SI units into CCSDS mandatory units.
  29.  * The {@link org.orekit.utils.units.Unit Unit} class provides useful
  30.  * {@link org.orekit.utils.units.Unit#fromSI(double) fromSi} and
  31.  * {@link org.orekit.utils.units.Unit#toSI(double) toSI} methods in case the callers
  32.  * already use CCSDS units instead of the API SI units. The general-purpose
  33.  * {@link org.orekit.utils.units.Unit Unit} class (without an 's') and the
  34.  * CCSDS-specific {@link org.orekit.files.ccsds.definitions.Units Units} class
  35.  * (with an 's') also provide some predefined units. These predefined units and the
  36.  * {@link org.orekit.utils.units.Unit#fromSI(double) fromSi} and
  37.  * {@link org.orekit.utils.units.Unit#toSI(double) toSI} conversion methods are indeed
  38.  * what the parsers and writers use for the conversions.
  39.  * </p>
  40.  * @author Bryan Cazabonne
  41.  * @since 10.2
  42.  */
  43. public class ApmQuaternion extends CommentsContainer {

  44.     /** Endpoints (i.e. frames A, B and their relationship). */
  45.     private final AttitudeEndpoints endpoints;

  46.     /** Quaternion. */
  47.     private final double[] q;

  48.     /** Quaternion derivative. */
  49.     private final double[] qDot;

  50.     /** Simple constructor.
  51.      */
  52.     public ApmQuaternion() {
  53.         endpoints = new AttitudeEndpoints();
  54.         q         = new double[4];
  55.         qDot      = new double[4];
  56.         Arrays.fill(q,    Double.NaN);
  57.         Arrays.fill(qDot, Double.NaN);
  58.     }

  59.     /** {@inheritDoc} */
  60.     @Override
  61.     public void validate(final double version) {
  62.         super.validate(version);
  63.         if (version < 2.0) {
  64.             endpoints.checkMandatoryEntriesExceptExternalFrame(version,
  65.                                                                ApmQuaternionKey.Q_FRAME_A,
  66.                                                                ApmQuaternionKey.Q_FRAME_B,
  67.                                                                ApmQuaternionKey.Q_DIR);
  68.             endpoints.checkExternalFrame(ApmQuaternionKey.Q_FRAME_A, ApmQuaternionKey.Q_FRAME_B);
  69.         } else {
  70.             endpoints.checkMandatoryEntriesExceptExternalFrame(version,
  71.                                                                ApmQuaternionKey.REF_FRAME_A,
  72.                                                                ApmQuaternionKey.REF_FRAME_B,
  73.                                                                ApmQuaternionKey.Q_DIR);
  74.             endpoints.checkExternalFrame(ApmQuaternionKey.REF_FRAME_A, ApmQuaternionKey.REF_FRAME_B);
  75.         }
  76.         if (Double.isNaN(q[0] + q[1] + q[2] + q[3])) {
  77.             throw new OrekitException(OrekitMessages.UNINITIALIZED_VALUE_FOR_KEY, "Q{C|1|2|3}");
  78.         }
  79.     }

  80.     /** Get the endpoints (i.e. frames A, B and their relationship).
  81.      * @return endpoints
  82.      */
  83.     public AttitudeEndpoints getEndpoints() {
  84.         return endpoints;
  85.     }

  86.     /**
  87.      * Get the quaternion.
  88.      * @return quaternion
  89.      */
  90.     public Quaternion getQuaternion() {
  91.         return new Quaternion(q[0], q[1], q[2], q[3]);
  92.     }

  93.     /**
  94.      * Set quaternion component.
  95.      * @param index component index (0 is scalar part)
  96.      * @param value quaternion component
  97.      */
  98.     public void setQ(final int index, final double value) {
  99.         refuseFurtherComments();
  100.         this.q[index] = value;
  101.     }

  102.     /**
  103.      * Get the quaternion derivative.
  104.      * @return quaternion derivative
  105.      */
  106.     public Quaternion getQuaternionDot() {
  107.         return new Quaternion(qDot[0], qDot[1], qDot[2], qDot[3]);
  108.     }

  109.     /**
  110.      * Set quaternion derivative component.
  111.      * @param index component index (0 is scalar part)
  112.      * @param derivative quaternion derivative component
  113.      */
  114.     public void setQDot(final int index, final double derivative) {
  115.         refuseFurtherComments();
  116.         this.qDot[index] = derivative;
  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. }