1   /* Copyright 2002-2021 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.frames;
18  
19  import org.hipparchus.Field;
20  import org.hipparchus.CalculusFieldElement;
21  import org.hipparchus.analysis.differentiation.FieldUnivariateDerivative2;
22  import org.hipparchus.analysis.differentiation.UnivariateDerivative2;
23  import org.hipparchus.geometry.euclidean.threed.FieldRotation;
24  import org.hipparchus.geometry.euclidean.threed.FieldVector3D;
25  import org.hipparchus.geometry.euclidean.threed.RotationConvention;
26  import org.hipparchus.geometry.euclidean.threed.RotationOrder;
27  import org.hipparchus.geometry.euclidean.threed.Vector3D;
28  import org.orekit.bodies.CelestialBody;
29  import org.orekit.time.AbsoluteDate;
30  import org.orekit.time.FieldAbsoluteDate;
31  import org.orekit.utils.FieldPVCoordinates;
32  
33  /** Transform provider for the rotating frame of the CR3BP System.
34   * @author Vincent Mouraux
35   * @since 10.2
36   */
37  class CR3BPRotatingTransformProvider implements TransformProvider {
38  
39      /** Serializable UID.*/
40      private static final long serialVersionUID = 20190519L;
41  
42      /** Frame for results. Always defined as primaryBody's inertially oriented frame.*/
43      private final Frame frame;
44  
45      /** Celestial body with smaller mass, m2.*/
46      private final CelestialBody secondaryBody;
47  
48      /** Mass ratio of the system.*/
49      private final double mu;
50  
51  
52      /** Simple constructor.
53       * @param mu System mass ratio
54       * @param primaryBody Primary body.
55       * @param secondaryBody Secondary body.
56       */
57      CR3BPRotatingTransformProvider(final double mu, final CelestialBody primaryBody, final CelestialBody secondaryBody) {
58          this.secondaryBody = secondaryBody;
59          this.frame = primaryBody.getInertiallyOrientedFrame();
60          this.mu = mu;
61      }
62  
63      /** {@inheritDoc} */
64      @Override
65      public Transform getTransform(final AbsoluteDate date) {
66          final FieldPVCoordinates<UnivariateDerivative2> pv21        = secondaryBody.getPVCoordinates(date, frame).toUnivariateDerivative2PV();
67          final Field<UnivariateDerivative2>              field       = pv21.getPosition().getX().getField();
68          final FieldVector3D<UnivariateDerivative2>      translation = FieldVector3D.getPlusI(field).scalarMultiply(pv21.getPosition().getNorm().multiply(mu)).negate();
69  
70          final FieldRotation<UnivariateDerivative2> rotation = new FieldRotation<>(pv21.getPosition(), pv21.getMomentum(),
71                                                                                    FieldVector3D.getPlusI(field),
72                                                                                    FieldVector3D.getPlusK(field));
73  
74          final UnivariateDerivative2[] rotationRates = rotation.getAngles(RotationOrder.XYZ, RotationConvention.FRAME_TRANSFORM);
75          final Vector3D rotationRate = new Vector3D(rotationRates[0].getPartialDerivative(1),   rotationRates[1].getPartialDerivative(1),   rotationRates[2].getPartialDerivative(1));
76          final Vector3D rotationAcc  = new Vector3D(rotationRates[0].getPartialDerivative(2),   rotationRates[1].getPartialDerivative(2),   rotationRates[2].getPartialDerivative(2));
77          final Vector3D velocity     = new Vector3D(translation.getX().getPartialDerivative(1), translation.getY().getPartialDerivative(1), translation.getZ().getPartialDerivative(1));
78          final Vector3D acceleration = new Vector3D(translation.getX().getPartialDerivative(2), translation.getY().getPartialDerivative(2), translation.getZ().getPartialDerivative(2));
79  
80          final Transform transform1 = new Transform(date, translation.toVector3D(), velocity, acceleration);
81          final Transform transform2 = new Transform(date, rotation.toRotation(), rotationRate, rotationAcc);
82          return new Transform(date, transform2, transform1);
83      }
84  
85      /** {@inheritDoc} */
86      @Override
87      public <T extends CalculusFieldElement<T>> FieldTransform<T> getTransform(final FieldAbsoluteDate<T> date) {
88          final FieldPVCoordinates<T> pv21 = secondaryBody.getPVCoordinates(date, frame);
89          final Field<T>              field = pv21.getPosition().getX().getField();
90  
91          final FieldVector3D<T> translationField = FieldVector3D.getPlusI(field).scalarMultiply(pv21.getPosition().getNorm().multiply(mu)).negate();
92          final FieldRotation<T> rotationField = new FieldRotation<>(pv21.getPosition(), pv21.getMomentum(),
93                                                                     FieldVector3D.getPlusI(field),
94                                                                     FieldVector3D.getPlusK(field));
95  
96          final FieldPVCoordinates<FieldUnivariateDerivative2<T>> pv21FDS        = secondaryBody.getPVCoordinates(date, frame).toUnivariateDerivative2PV();
97          final Field<FieldUnivariateDerivative2<T>>              fieldUD        = pv21FDS.getPosition().getX().getField();
98          final FieldVector3D<FieldUnivariateDerivative2<T>>      translationFDS = FieldVector3D.getPlusI(fieldUD).scalarMultiply(pv21FDS.getPosition().getNorm().multiply(mu)).negate();
99  
100         final FieldRotation<FieldUnivariateDerivative2<T>> rotationFDS = new FieldRotation<>(pv21FDS.getPosition(), pv21FDS.getMomentum(),
101                                                                                              FieldVector3D.getPlusI(fieldUD),
102                                                                                              FieldVector3D.getPlusK(fieldUD));
103         final FieldUnivariateDerivative2<T>[] rotationRates = rotationFDS.getAngles(RotationOrder.XYZ, RotationConvention.FRAME_TRANSFORM);
104         final FieldVector3D<T> rotationRate = new FieldVector3D<>(rotationRates[0].getPartialDerivative(1),      rotationRates[1].getPartialDerivative(1),      rotationRates[2].getPartialDerivative(1));
105         final FieldVector3D<T> rotationAcc  = new FieldVector3D<>(rotationRates[0].getPartialDerivative(2),      rotationRates[1].getPartialDerivative(2),      rotationRates[2].getPartialDerivative(2));
106         final FieldVector3D<T> velocity     = new FieldVector3D<>(translationFDS.getX().getPartialDerivative(1), translationFDS.getY().getPartialDerivative(1), translationFDS.getZ().getPartialDerivative(1));
107         final FieldVector3D<T> acceleration = new FieldVector3D<>(translationFDS.getX().getPartialDerivative(2), translationFDS.getY().getPartialDerivative(2), translationFDS.getZ().getPartialDerivative(2));
108 
109         final FieldTransform<T> transform1 = new FieldTransform<>(date, translationField, velocity, acceleration);
110         final FieldTransform<T> transform2 = new FieldTransform<>(date, rotationField, rotationRate, rotationAcc);
111         return new FieldTransform<>(date, transform2, transform1);
112     }
113 }