1   /* Copyright 2022-2025 Luc Maisonobe
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.attitudes;
18  
19  import org.hipparchus.CalculusFieldElement;
20  import org.hipparchus.Field;
21  import org.hipparchus.analysis.differentiation.FieldUnivariateDerivative2;
22  import org.hipparchus.analysis.differentiation.UnivariateDerivative2;
23  import org.hipparchus.geometry.euclidean.threed.FieldVector3D;
24  import org.hipparchus.geometry.euclidean.threed.Vector3D;
25  import org.orekit.bodies.OneAxisEllipsoid;
26  import org.orekit.frames.FieldTransform;
27  import org.orekit.frames.FieldStaticTransform;
28  import org.orekit.frames.Frame;
29  import org.orekit.frames.StaticTransform;
30  import org.orekit.frames.Transform;
31  import org.orekit.utils.ExtendedPositionProvider;
32  import org.orekit.utils.FieldPVCoordinates;
33  import org.orekit.utils.PVCoordinates;
34  import org.orekit.utils.TimeStampedFieldPVCoordinates;
35  import org.orekit.utils.TimeStampedPVCoordinates;
36  
37  import java.util.HashMap;
38  import java.util.Map;
39  
40  /**
41   * Ground point target for {@link AlignedAndConstrained}.
42   * @author Luc Maisonobe
43   * @since 12.2
44   */
45  public class GroundPointTarget implements TargetProvider
46  {
47  
48      /** Location of the target in Earth frame. */
49      private final PVCoordinates location;
50  
51      /** Cached field-based locations. */
52      private final transient Map<Field<? extends CalculusFieldElement<?>>, FieldPVCoordinates<?>>
53          cachedLocations;
54  
55      /** Simple constructor.
56       * @param location location of the target in Earth frame
57       */
58      public GroundPointTarget(final Vector3D location)
59      {
60          this.location        = new PVCoordinates(location, Vector3D.ZERO, Vector3D.ZERO);
61          this.cachedLocations = new HashMap<>();
62      }
63  
64      /** {@inheritDoc} */
65      @Override
66      public FieldVector3D<UnivariateDerivative2> getDerivative2TargetDirection(final ExtendedPositionProvider sun,
67                                                                                final OneAxisEllipsoid earth,
68                                                                                final TimeStampedPVCoordinates pv,
69                                                                                final Frame frame) {
70          final Transform earthToInert = earth.getFrame().getTransformTo(frame, pv.getDate());
71          return new PVCoordinates(pv, earthToInert.transformPVCoordinates(location)).
72                 toUnivariateDerivative2Vector().
73                 normalize();
74      }
75  
76      /** {@inheritDoc} */
77      @Override
78      public Vector3D getTargetDirection(final ExtendedPositionProvider sun, final OneAxisEllipsoid earth,
79                                              final TimeStampedPVCoordinates pv, final Frame frame) {
80          final StaticTransform earthToInert = earth.getFrame().getStaticTransformTo(frame, pv.getDate());
81          return earthToInert.transformPosition(location.getPosition()).subtract(pv.getPosition()).normalize();
82      }
83  
84      /** {@inheritDoc} */
85      @Override
86      public <T extends CalculusFieldElement<T>> FieldVector3D<FieldUnivariateDerivative2<T>> getDerivative2TargetDirection(final ExtendedPositionProvider sun,
87                                                                                                                            final OneAxisEllipsoid earth,
88                                                                                                                            final TimeStampedFieldPVCoordinates<T> pv,
89                                                                                                                            final Frame frame) {
90          final Field<T> field = pv.getDate().getField();
91  
92          // get the target location for specified field
93          @SuppressWarnings("unchecked")
94          final FieldPVCoordinates<T> l =
95              (FieldPVCoordinates<T>) cachedLocations.computeIfAbsent(field, f -> {
96                  final T zero = field.getZero();
97                  return new FieldPVCoordinates<>(new FieldVector3D<>(zero.newInstance(location.getPosition().getX()),
98                                                                      zero.newInstance(location.getPosition().getY()),
99                                                                      zero.newInstance(location.getPosition().getZ())),
100                                                 FieldVector3D.getZero(field),
101                                                 FieldVector3D.getZero(field));
102             });
103 
104         final FieldTransform<T> earthToInert = earth.getFrame().getTransformTo(frame, pv.getDate());
105         return new FieldPVCoordinates<>(pv, earthToInert.transformPVCoordinates(l)).
106                toUnivariateDerivative2Vector().
107                normalize();
108 
109     }
110 
111     /** {@inheritDoc} */
112     @Override
113     public <T extends CalculusFieldElement<T>> FieldVector3D<T> getTargetDirection(final ExtendedPositionProvider sun,
114                                                                                    final OneAxisEllipsoid earth,
115                                                                                    final TimeStampedFieldPVCoordinates<T> pv,
116                                                                                    final Frame frame) {
117         final FieldStaticTransform<T> earthToInert = earth.getFrame().getStaticTransformTo(frame, pv.getDate());
118         return earthToInert.transformPosition(location.getPosition()).subtract(pv.getPosition()).normalize();
119     }
120 }