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.models.earth.tessellation;
18  
19  import java.util.Arrays;
20  import java.util.List;
21  
22  import org.hipparchus.analysis.differentiation.Gradient;
23  import org.hipparchus.analysis.interpolation.HermiteInterpolator;
24  import org.hipparchus.geometry.euclidean.threed.Rotation;
25  import org.hipparchus.geometry.euclidean.threed.Vector3D;
26  import org.hipparchus.util.FastMath;
27  import org.hipparchus.util.Pair;
28  import org.orekit.attitudes.FrameAlignedProvider;
29  import org.orekit.bodies.GeodeticPoint;
30  import org.orekit.bodies.OneAxisEllipsoid;
31  import org.orekit.orbits.Orbit;
32  import org.orekit.propagation.Propagator;
33  import org.orekit.propagation.analytical.KeplerianPropagator;
34  import org.orekit.propagation.events.LatitudeExtremumDetector;
35  import org.orekit.utils.TimeStampedPVCoordinates;
36  
37  /** Class used to orient tiles along an orbit track.
38   * @see ConstantAzimuthAiming
39   * @see DivertedSingularityAiming
40   * @author Luc Maisonobe
41   */
42  public class AlongTrackAiming implements TileAiming {
43  
44      /** Number of sampling steps for the half-track. */
45      private static final int SAMPLING_STEPS = 1000;
46  
47      /** Ground track over one half orbit. */
48      private final List<Pair<GeodeticPoint, TimeStampedPVCoordinates>> halfTrack;
49  
50      /** Indicator for orbit type. */
51      private final boolean retrogradeOrbit;
52  
53      /** Simple constructor.
54       * @param ellipsoid ellipsoid body on which the zone is defined
55       * @param orbit orbit along which tiles should be aligned
56       * @param isAscending indicator for zone tiling with respect to ascending
57       * or descending orbits
58       */
59      public AlongTrackAiming(final OneAxisEllipsoid ellipsoid, final Orbit orbit, final boolean isAscending) {
60          this.halfTrack       = findHalfTrack(orbit, ellipsoid, isAscending);
61          this.retrogradeOrbit = orbit.getPVCoordinates().getMomentum().getZ() < 0;
62      }
63  
64      /** {@inheritDoc} */
65      @Override
66      public List<GeodeticPoint> getSingularPoints() {
67          return Arrays.asList(GeodeticPoint.NORTH_POLE, GeodeticPoint.SOUTH_POLE);
68      }
69  
70      /** {@inheritDoc} */
71      @Override
72      public Vector3D alongTileDirection(final Vector3D point, final GeodeticPoint gp) {
73  
74          final double lStart = halfTrack.get(0).getFirst().getLatitude();
75          final double lEnd   = halfTrack.get(halfTrack.size() - 1).getFirst().getLatitude();
76  
77          // special handling for out of range latitudes
78          if (gp.getLatitude() < FastMath.min(lStart, lEnd) || gp.getLatitude() > FastMath.max(lStart, lEnd)) {
79              return retrogradeOrbit ? gp.getWest() : gp.getEast();
80          }
81  
82          // bracket the point in the half track sample
83          int    iInf = 0;
84          int    iSup = halfTrack.size() - 1;
85          while (iSup - iInf > 1) {
86              final int iMiddle = (iSup + iInf) / 2;
87              if (lStart < lEnd ^ halfTrack.get(iMiddle).getFirst().getLatitude() > gp.getLatitude()) {
88                  // the specified latitude is in the second half
89                  iInf = iMiddle;
90              } else {
91                  // the specified latitude is in the first half
92                  iSup = iMiddle;
93              }
94          }
95  
96          // ensure we can get points at iStart, iStart + 1, iStart + 2 and iStart + 3
97          final int iStart = FastMath.max(0, FastMath.min(iInf - 1, halfTrack.size() - 4));
98  
99          // interpolate ground sliding point at specified latitude
100         final HermiteInterpolator interpolator = new HermiteInterpolator();
101         for (int i = iStart; i < iStart + 4; ++i) {
102             final Vector3D position = halfTrack.get(i).getSecond().getPosition();
103             final Vector3D velocity = halfTrack.get(i).getSecond().getVelocity();
104             interpolator.addSamplePoint(halfTrack.get(i).getFirst().getLatitude(),
105                                         new double[] {
106                                             position.getX(), position.getY(), position.getZ(),
107                                             velocity.getX(), velocity.getY(), velocity.getZ()
108                                         });
109         }
110         final Gradient[] p  = interpolator.value(Gradient.variable(1, 0, gp.getLatitude()));
111 
112         // extract interpolated ground position/velocity
113         final Vector3D position = new Vector3D(p[0].getValue(),
114                                                p[1].getValue(),
115                                                p[2].getValue());
116         final Vector3D velocity = new Vector3D(p[3].getValue(),
117                                                p[4].getValue(),
118                                                p[5].getValue());
119 
120         // adjust longitude to match the specified one
121         final Rotation rotation      = new Rotation(Vector3D.PLUS_K, position, Vector3D.PLUS_K, point);
122         final Vector3D fixedVelocity = rotation.applyTo(velocity);
123 
124         // the tile direction is aligned with sliding point velocity
125         return fixedVelocity.normalize();
126 
127     }
128 
129     /** Find the ascending or descending part of an orbit track.
130      * @param orbit orbit along which tiles should be aligned
131      * @param ellipsoid ellipsoid over which track is sampled
132      * @param isAscending indicator for zone tiling with respect to ascending
133      * or descending orbits
134      * @return time stamped ground points on the selected half track
135      */
136     private static List<Pair<GeodeticPoint, TimeStampedPVCoordinates>> findHalfTrack(final Orbit orbit,
137                                                                                      final OneAxisEllipsoid ellipsoid,
138                                                                                      final boolean isAscending) {
139 
140         // find the span of the next half track
141         final Propagator propagator =
142                 new KeplerianPropagator(orbit, new FrameAlignedProvider(orbit.getFrame()));
143         final HalfTrackSpanHandler handler = new HalfTrackSpanHandler(isAscending);
144         final LatitudeExtremumDetector detector =
145                         new LatitudeExtremumDetector(0.25 * orbit.getKeplerianPeriod(), 1.0e-3, ellipsoid).
146                         withHandler(handler).
147                         withMaxIter(100);
148         propagator.addEventDetector(detector);
149         propagator.propagate(orbit.getDate().shiftedBy(3 * orbit.getKeplerianPeriod()));
150 
151         // sample the half track
152         propagator.clearEventsDetectors();
153         final HalfTrackSampler sampler = new HalfTrackSampler(ellipsoid);
154         propagator.setStepHandler(handler.getEnd().durationFrom(handler.getStart()) / SAMPLING_STEPS, sampler);
155         propagator.propagate(handler.getStart(), handler.getEnd());
156 
157         return sampler.getHalfTrack();
158 
159     }
160 
161 }