1   /* Copyright 2022-2026 Romain Serra
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.CalculusFieldElement;
20  import org.hipparchus.Field;
21  import org.hipparchus.geometry.euclidean.threed.FieldRotation;
22  import org.hipparchus.geometry.euclidean.threed.FieldVector3D;
23  import org.hipparchus.geometry.euclidean.threed.Rotation;
24  import org.hipparchus.geometry.euclidean.threed.Vector3D;
25  import org.orekit.bodies.BodyShape;
26  import org.orekit.bodies.FieldGeodeticPoint;
27  import org.orekit.bodies.GeodeticPoint;
28  import org.orekit.time.AbsoluteDate;
29  import org.orekit.time.FieldAbsoluteDate;
30  
31  /**
32   * Topocentric frame transform provider (towards parent body-fixed, body-centered).
33   * @author Romain Serra
34   * @since 14.0
35   */
36  public class TopocentricTransformProvider implements TransformProvider {
37  
38      /** Geodetic point. */
39      private final GeodeticPoint geodeticPoint;
40  
41      /** Body shape where point is attached. */
42      private final BodyShape bodyShape;
43  
44      /**
45       * Constructor.
46       * @param geodeticPoint geodetic point
47       * @param bodyShape body shape
48       */
49      public TopocentricTransformProvider(final GeodeticPoint geodeticPoint, final BodyShape bodyShape) {
50          this.geodeticPoint = geodeticPoint;
51          this.bodyShape = bodyShape;
52      }
53  
54      /**
55       * Getter for the body shape.
56       * @return shape
57       */
58      public BodyShape getBodyShape() {
59          return bodyShape;
60      }
61  
62      @Override
63      public StaticTransform getStaticTransform(final AbsoluteDate date) {
64          return StaticTransform.of(date, buildTranslation(), buildRotation(geodeticPoint));
65      }
66  
67      @Override
68      public <T extends CalculusFieldElement<T>> FieldStaticTransform<T> getStaticTransform(final FieldAbsoluteDate<T> date) {
69          final Field<T> field = date.getField();
70          final FieldGeodeticPoint<T> fieldGeodeticPoint = new FieldGeodeticPoint<>(field, geodeticPoint);
71          return FieldStaticTransform.of(date, buildFieldTranslation(bodyShape, fieldGeodeticPoint),
72                  buildFieldRotation(fieldGeodeticPoint));
73      }
74  
75      @Override
76      public Transform getTransform(final AbsoluteDate date) {
77          return new Transform(date, buildTranslation(), buildRotation(geodeticPoint));
78      }
79  
80      private Vector3D buildTranslation() {
81          return bodyShape.transform(geodeticPoint).negate();
82      }
83  
84      @Override
85      public <T extends CalculusFieldElement<T>> FieldTransform<T> getTransform(final FieldAbsoluteDate<T> date) {
86          final Field<T> field = date.getField();
87          final FieldGeodeticPoint<T> fieldGeodeticPoint = new FieldGeodeticPoint<>(field, geodeticPoint);
88          return getTransform(bodyShape, date, fieldGeodeticPoint);
89      }
90  
91      /**
92       * Method building a Field transform from body shape and point.
93       * @param shape body shape
94       * @param date date
95       * @param fieldGeodeticPoint geodetic point
96       * @return transform
97       * @param <T> field type
98       */
99      public static  <T extends CalculusFieldElement<T>> FieldTransform<T> getTransform(final BodyShape shape,
100                                                                                       final FieldAbsoluteDate<T> date,
101                                                                                       final FieldGeodeticPoint<T> fieldGeodeticPoint) {
102         return new FieldTransform<>(date, buildFieldTranslation(shape, fieldGeodeticPoint),
103                 buildFieldRotation(fieldGeodeticPoint));
104     }
105 
106     private static <T extends CalculusFieldElement<T>> FieldVector3D<T> buildFieldTranslation(final BodyShape shape,
107                                                                                               final FieldGeodeticPoint<T> fieldGeodeticPoint) {
108         return shape.transform(fieldGeodeticPoint).negate();
109     }
110 
111     private static Rotation buildRotation(final GeodeticPoint point) {
112         return new Rotation(point.getEast(), point.getZenith(), Vector3D.PLUS_I, Vector3D.PLUS_K);
113     }
114 
115     private static <W extends CalculusFieldElement<W>> FieldRotation<W> buildFieldRotation(final FieldGeodeticPoint<W> fieldGeodeticPoint) {
116         final Field<W> field = fieldGeodeticPoint.getAltitude().getField();
117         return new FieldRotation<>(fieldGeodeticPoint.getEast(), fieldGeodeticPoint.getZenith(),
118                 FieldVector3D.getPlusI(field), FieldVector3D.getPlusK(field));
119     }
120 }