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.utils;
18  
19  import org.hipparchus.analysis.differentiation.Derivative;
20  import org.hipparchus.geometry.euclidean.threed.FieldRotation;
21  import org.hipparchus.geometry.euclidean.threed.Rotation;
22  import org.hipparchus.geometry.euclidean.threed.RotationConvention;
23  import org.hipparchus.geometry.euclidean.threed.Vector3D;
24  import org.orekit.time.AbsoluteDate;
25  import org.orekit.time.TimeStamped;
26  
27  /** {@link TimeStamped time-stamped} version of {@link AngularCoordinates}.
28   * <p>Instances of this class are guaranteed to be immutable.</p>
29   * @author Luc Maisonobe
30   * @since 7.0
31   */
32  public class TimeStampedAngularCoordinates extends AngularCoordinates implements TimeStamped {
33  
34      /** Serializable UID. */
35      private static final long serialVersionUID = 20140723L;
36  
37      /** The date. */
38      private final AbsoluteDate date;
39  
40      /** Builds a rotation/rotation rate pair.
41       * @param date coordinates date
42       * @param rotation rotation
43       * @param rotationRate rotation rate Ω (rad/s)
44       * @param rotationAcceleration rotation acceleration dΩ/dt (rad²/s²)
45       */
46      public TimeStampedAngularCoordinates(final AbsoluteDate date,
47                                           final Rotation rotation,
48                                           final Vector3D rotationRate,
49                                           final Vector3D rotationAcceleration) {
50          super(rotation, rotationRate, rotationAcceleration);
51          this.date = date;
52      }
53  
54      /** Build the rotation that transforms a pair of pv coordinates into another pair.
55  
56       * <p><em>WARNING</em>! This method requires much more stringent assumptions on
57       * its parameters than the similar {@link Rotation#Rotation(Vector3D, Vector3D,
58       * Vector3D, Vector3D) constructor} from the {@link Rotation Rotation} class.
59       * As far as the Rotation constructor is concerned, the {@code v₂} vector from
60       * the second pair can be slightly misaligned. The Rotation constructor will
61       * compensate for this misalignment and create a rotation that ensure {@code
62       * v₁ = r(u₁)} and {@code v₂ ∈ plane (r(u₁), r(u₂))}. <em>THIS IS NOT
63       * TRUE ANYMORE IN THIS CLASS</em>! As derivatives are involved and must be
64       * preserved, this constructor works <em>only</em> if the two pairs are fully
65       * consistent, i.e. if a rotation exists that fulfill all the requirements: {@code
66       * v₁ = r(u₁)}, {@code v₂ = r(u₂)}, {@code dv₁/dt = dr(u₁)/dt}, {@code dv₂/dt
67       * = dr(u₂)/dt}, {@code d²v₁/dt² = d²r(u₁)/dt²}, {@code d²v₂/dt² = d²r(u₂)/dt²}.</p>
68  
69       * @param date coordinates date
70       * @param u1 first vector of the origin pair
71       * @param u2 second vector of the origin pair
72       * @param v1 desired image of u1 by the rotation
73       * @param v2 desired image of u2 by the rotation
74       * @param tolerance relative tolerance factor used to check singularities
75       */
76      public TimeStampedAngularCoordinates(final AbsoluteDate date,
77                                           final PVCoordinates u1, final PVCoordinates u2,
78                                           final PVCoordinates v1, final PVCoordinates v2,
79                                           final double tolerance) {
80          super(u1, u2, v1, v2, tolerance);
81          this.date = date;
82      }
83  
84      /** Build one of the rotations that transform one pv coordinates into another one.
85  
86       * <p>Except for a possible scale factor, if the instance were
87       * applied to the vector u it will produce the vector v. There is an
88       * infinite number of such rotations, this constructor choose the
89       * one with the smallest associated angle (i.e. the one whose axis
90       * is orthogonal to the (u, v) plane). If u and v are collinear, an
91       * arbitrary rotation axis is chosen.</p>
92  
93       * @param date coordinates date
94       * @param u origin vector
95       * @param v desired image of u by the rotation
96       */
97      public TimeStampedAngularCoordinates(final AbsoluteDate date,
98                                           final PVCoordinates u, final PVCoordinates v) {
99          super(u, v);
100         this.date = date;
101     }
102 
103     /** Builds a TimeStampedAngularCoordinates from  a {@link FieldRotation}&lt;{@link Derivative}&gt;.
104      * <p>
105      * The rotation components must have time as their only derivation parameter and
106      * have consistent derivation orders.
107      * </p>
108      * @param date coordinates date
109      * @param r rotation with time-derivatives embedded within the coordinates
110      * @param <U> type of the derivative
111      */
112     public <U extends Derivative<U>>TimeStampedAngularCoordinates(final AbsoluteDate date,
113                                                                   final FieldRotation<U> r) {
114         super(r);
115         this.date = date;
116     }
117 
118     /** {@inheritDoc} */
119     public AbsoluteDate getDate() {
120         return date;
121     }
122 
123     /** Revert a rotation/rotation rate pair.
124      * Build a pair which reverse the effect of another pair.
125      * @return a new pair whose effect is the reverse of the effect
126      * of the instance
127      */
128     public TimeStampedAngularCoordinates revert() {
129         return new TimeStampedAngularCoordinates(date,
130                                                  getRotation().revert(),
131                                                  getRotation().applyInverseTo(getRotationRate().negate()),
132                                                  getRotation().applyInverseTo(getRotationAcceleration().negate()));
133     }
134 
135     /** Get a time-shifted state.
136      * <p>
137      * The state can be slightly shifted to close dates. This shift is based on
138      * a simple linear model. It is <em>not</em> intended as a replacement for
139      * proper attitude propagation but should be sufficient for either small
140      * time shifts or coarse accuracy.
141      * </p>
142      * @param dt time shift in seconds
143      * @return a new state, shifted with respect to the instance (which is immutable)
144      */
145     public TimeStampedAngularCoordinates shiftedBy(final double dt) {
146         final AngularCoordinates sac = super.shiftedBy(dt);
147         return new TimeStampedAngularCoordinates(date.shiftedBy(dt),
148                                                  sac.getRotation(), sac.getRotationRate(), sac.getRotationAcceleration());
149 
150     }
151 
152     /** Add an offset from the instance.
153      * <p>
154      * We consider here that the offset rotation is applied first and the
155      * instance is applied afterward. Note that angular coordinates do <em>not</em>
156      * commute under this operation, i.e. {@code a.addOffset(b)} and {@code
157      * b.addOffset(a)} lead to <em>different</em> results in most cases.
158      * </p>
159      * <p>
160      * The two methods {@link #addOffset(AngularCoordinates) addOffset} and
161      * {@link #subtractOffset(AngularCoordinates) subtractOffset} are designed
162      * so that round trip applications are possible. This means that both {@code
163      * ac1.subtractOffset(ac2).addOffset(ac2)} and {@code
164      * ac1.addOffset(ac2).subtractOffset(ac2)} return angular coordinates equal to ac1.
165      * </p>
166      * @param offset offset to subtract
167      * @return new instance, with offset subtracted
168      * @see #subtractOffset(AngularCoordinates)
169      */
170     @Override
171     public TimeStampedAngularCoordinates addOffset(final AngularCoordinates offset) {
172         final Vector3D rOmega    = getRotation().applyTo(offset.getRotationRate());
173         final Vector3D rOmegaDot = getRotation().applyTo(offset.getRotationAcceleration());
174         return new TimeStampedAngularCoordinates(date,
175                                                  getRotation().compose(offset.getRotation(), RotationConvention.VECTOR_OPERATOR),
176                                                  getRotationRate().add(rOmega),
177                                                  new Vector3D( 1.0, getRotationAcceleration(),
178                                                                1.0, rOmegaDot,
179                                                               -1.0, Vector3D.crossProduct(getRotationRate(), rOmega)));
180     }
181 
182     /** Subtract an offset from the instance.
183      * <p>
184      * We consider here that the offset rotation is applied first and the
185      * instance is applied afterward. Note that angular coordinates do <em>not</em>
186      * commute under this operation, i.e. {@code a.subtractOffset(b)} and {@code
187      * b.subtractOffset(a)} lead to <em>different</em> results in most cases.
188      * </p>
189      * <p>
190      * The two methods {@link #addOffset(AngularCoordinates) addOffset} and
191      * {@link #subtractOffset(AngularCoordinates) subtractOffset} are designed
192      * so that round trip applications are possible. This means that both {@code
193      * ac1.subtractOffset(ac2).addOffset(ac2)} and {@code
194      * ac1.addOffset(ac2).subtractOffset(ac2)} return angular coordinates equal to ac1.
195      * </p>
196      * @param offset offset to subtract
197      * @return new instance, with offset subtracted
198      * @see #addOffset(AngularCoordinates)
199      */
200     @Override
201     public TimeStampedAngularCoordinates subtractOffset(final AngularCoordinates offset) {
202         return addOffset(offset.revert());
203     }
204 
205 }