TopocentricFrame.java

  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.frames;

  18. import org.hipparchus.Field;
  19. import org.hipparchus.CalculusFieldElement;
  20. import org.hipparchus.analysis.UnivariateFunction;
  21. import org.hipparchus.analysis.solvers.BracketingNthOrderBrentSolver;
  22. import org.hipparchus.analysis.solvers.UnivariateSolver;
  23. import org.hipparchus.exception.MathRuntimeException;
  24. import org.hipparchus.geometry.euclidean.threed.FieldVector3D;
  25. import org.hipparchus.geometry.euclidean.threed.Rotation;
  26. import org.hipparchus.geometry.euclidean.threed.Vector3D;
  27. import org.hipparchus.util.FastMath;
  28. import org.hipparchus.util.FieldSinCos;
  29. import org.hipparchus.util.MathUtils;
  30. import org.hipparchus.util.SinCos;
  31. import org.orekit.bodies.BodyShape;
  32. import org.orekit.bodies.FieldGeodeticPoint;
  33. import org.orekit.bodies.GeodeticPoint;
  34. import org.orekit.errors.OrekitException;
  35. import org.orekit.time.AbsoluteDate;
  36. import org.orekit.time.FieldAbsoluteDate;
  37. import org.orekit.utils.Constants;
  38. import org.orekit.utils.FieldPVCoordinates;
  39. import org.orekit.utils.FieldTrackingCoordinates;
  40. import org.orekit.utils.PVCoordinates;
  41. import org.orekit.utils.PVCoordinatesProvider;
  42. import org.orekit.utils.TimeStampedPVCoordinates;
  43. import org.orekit.utils.TrackingCoordinates;


  44. /** Topocentric frame.
  45.  * <p>Frame associated to a position near the surface of a body shape.</p>
  46.  * <p>
  47.  * The origin of the frame is at the defining {@link GeodeticPoint geodetic point}
  48.  * location, and the right-handed canonical trihedra is:
  49.  * </p>
  50.  * <ul>
  51.  *   <li>X axis in the local horizontal plane (normal to zenith direction) and
  52.  *   following the local parallel towards East</li>
  53.  *   <li>Y axis in the horizontal plane (normal to zenith direction) and
  54.  *   following the local meridian towards North</li>
  55.  *   <li>Z axis towards Zenith direction</li>
  56.  * </ul>
  57.  * @author V&eacute;ronique Pommier-Maurussane
  58.  */
  59. public class TopocentricFrame extends Frame implements PVCoordinatesProvider {

  60.     /** Body shape on which the local point is defined. */
  61.     private final BodyShape parentShape;

  62.     /** Geodetic point where the topocentric frame is defined. */
  63.     private final GeodeticPoint point;

  64.     /** Cartesian point where the topocentric frame is defined.
  65.      * @since 12.0
  66.      */
  67.     private final Vector3D cartesianPoint;

  68.     /** Simple constructor.
  69.      * @param parentShape body shape on which the local point is defined
  70.      * @param point local surface point where topocentric frame is defined
  71.      * @param name the string representation
  72.      */
  73.     public TopocentricFrame(final BodyShape parentShape, final GeodeticPoint point,
  74.                             final String name) {

  75.         super(parentShape.getBodyFrame(),
  76.                 new Transform(AbsoluteDate.ARBITRARY_EPOCH,
  77.                         new Transform(AbsoluteDate.ARBITRARY_EPOCH,
  78.                                 parentShape.transform(point).negate()),
  79.                         new Transform(AbsoluteDate.ARBITRARY_EPOCH,
  80.                                 new Rotation(point.getEast(), point.getZenith(),
  81.                                         Vector3D.PLUS_I, Vector3D.PLUS_K),
  82.                                 Vector3D.ZERO)),
  83.                 name, false);
  84.         this.parentShape    = parentShape;
  85.         this.point          = point;
  86.         this.cartesianPoint = getTransformProvider().
  87.                 getStaticTransform(AbsoluteDate.ARBITRARY_EPOCH).
  88.                 getInverse().
  89.                 transformPosition(Vector3D.ZERO);
  90.     }

  91.     /** Get the body shape on which the local point is defined.
  92.      * @return body shape on which the local point is defined
  93.      */
  94.     public BodyShape getParentShape() {
  95.         return parentShape;
  96.     }

  97.     /** Get the surface point defining the origin of the frame.
  98.      * @return surface point defining the origin of the frame
  99.      */
  100.     public GeodeticPoint getPoint() {
  101.         return point;
  102.     }

  103.     /** Get the surface point defining the origin of the frame.
  104.      * @return surface point defining the origin of the frame in body frame
  105.      * @since 12.0
  106.      */
  107.     public Vector3D getCartesianPoint() {
  108.         return cartesianPoint;
  109.     }

  110.     /** Get the surface point defining the origin of the frame.
  111.      * @param <T> type of the elements
  112.      * @param field of the elements
  113.      * @return surface point defining the origin of the frame
  114.      * @since 9.3
  115.      */
  116.     public <T extends CalculusFieldElement<T>> FieldGeodeticPoint<T> getPoint(final Field<T> field) {
  117.         final T zero = field.getZero();
  118.         return new FieldGeodeticPoint<>(zero.newInstance(point.getLatitude()),
  119.                 zero.newInstance(point.getLongitude()),
  120.                 zero.newInstance(point.getAltitude()));
  121.     }

  122.     /** Get the zenith direction of topocentric frame, expressed in parent shape frame.
  123.      * <p>The zenith direction is defined as the normal to local horizontal plane.</p>
  124.      * @return unit vector in the zenith direction
  125.      * @see #getNadir()
  126.      */
  127.     public Vector3D getZenith() {
  128.         return point.getZenith();
  129.     }

  130.     /** Get the nadir direction of topocentric frame, expressed in parent shape frame.
  131.      * <p>The nadir direction is the opposite of zenith direction.</p>
  132.      * @return unit vector in the nadir direction
  133.      * @see #getZenith()
  134.      */
  135.     public Vector3D getNadir() {
  136.         return point.getNadir();
  137.     }

  138.     /** Get the north direction of topocentric frame, expressed in parent shape frame.
  139.      * <p>The north direction is defined in the horizontal plane
  140.      * (normal to zenith direction) and following the local meridian.</p>
  141.      * @return unit vector in the north direction
  142.      * @see #getSouth()
  143.      */
  144.     public Vector3D getNorth() {
  145.         return point.getNorth();
  146.     }

  147.     /** Get the south direction of topocentric frame, expressed in parent shape frame.
  148.      * <p>The south direction is the opposite of north direction.</p>
  149.      * @return unit vector in the south direction
  150.      * @see #getNorth()
  151.      */
  152.     public Vector3D getSouth() {
  153.         return point.getSouth();
  154.     }

  155.     /** Get the east direction of topocentric frame, expressed in parent shape frame.
  156.      * <p>The east direction is defined in the horizontal plane
  157.      * in order to complete direct triangle (east, north, zenith).</p>
  158.      * @return unit vector in the east direction
  159.      * @see #getWest()
  160.      */
  161.     public Vector3D getEast() {
  162.         return point.getEast();
  163.     }

  164.     /** Get the west direction of topocentric frame, expressed in parent shape frame.
  165.      * <p>The west direction is the opposite of east direction.</p>
  166.      * @return unit vector in the west direction
  167.      * @see #getEast()
  168.      */
  169.     public Vector3D getWest() {
  170.         return point.getWest();
  171.     }

  172.     /** Get the tracking coordinates of a point with regards to the local point.
  173.      * @param extPoint point for which elevation shall be computed
  174.      * @param frame frame in which the point is defined
  175.      * @param date computation date
  176.      * @return tracking coordinates of the point
  177.      * @since 12.0
  178.      */
  179.     public TrackingCoordinates getTrackingCoordinates(final Vector3D extPoint, final Frame frame,
  180.                                                       final AbsoluteDate date) {

  181.         // transform given point from given frame to topocentric frame
  182.         final Vector3D extPointTopo = transformPoint(extPoint, frame, date);

  183.         final double azimuth = computeAzimuthFromTopoPoint(extPointTopo);

  184.         return new TrackingCoordinates(azimuth, extPointTopo.getDelta(), extPointTopo.getNorm());

  185.     }

  186.     /** Get the tracking coordinates of a point with regards to the local point.
  187.      * @param <T> type of the field elements
  188.      * @param extPoint point for which elevation shall be computed
  189.      * @param frame frame in which the point is defined
  190.      * @param date computation date
  191.      * @return tracking coordinates of the point
  192.      * @since 12.0
  193.      */
  194.     public <T extends CalculusFieldElement<T>> FieldTrackingCoordinates<T> getTrackingCoordinates(final FieldVector3D<T> extPoint,
  195.                                                                                                   final Frame frame,
  196.                                                                                                   final FieldAbsoluteDate<T> date) {

  197.         // Transform given point from given frame to topocentric frame
  198.         final FieldVector3D<T> extPointTopo = transformPoint(extPoint, frame, date);

  199.         final T azimuth = computeAzimuthFromTopoPoint(extPointTopo);

  200.         return new FieldTrackingCoordinates<>(azimuth, extPointTopo.getDelta(), extPointTopo.getNorm());

  201.     }

  202.     /** Get the elevation of a point with regards to the local point.
  203.      * <p>The elevation is the angle between the local horizontal and
  204.      * the direction from local point to given point.</p>
  205.      * @param extPoint point for which elevation shall be computed
  206.      * @param frame frame in which the point is defined
  207.      * @param date computation date
  208.      * @return elevation of the point
  209.      */
  210.     public double getElevation(final Vector3D extPoint, final Frame frame,
  211.                                final AbsoluteDate date) {

  212.         // Transform given point from given frame to topocentric frame
  213.         final Vector3D extPointTopo = transformPoint(extPoint, frame, date);

  214.         return extPointTopo.getDelta();
  215.     }

  216.     /** Get the elevation of a point with regards to the local point.
  217.      * <p>The elevation is the angle between the local horizontal and
  218.      * the direction from local point to given point.</p>
  219.      * @param <T> type of the elements
  220.      * @param extPoint point for which elevation shall be computed
  221.      * @param frame frame in which the point is defined
  222.      * @param date computation date
  223.      * @return elevation of the point
  224.      * @since 9.3
  225.      */
  226.     public <T extends CalculusFieldElement<T>> T getElevation(final FieldVector3D<T> extPoint, final Frame frame,
  227.                                                               final FieldAbsoluteDate<T> date) {

  228.         // Transform given point from given frame to topocentric frame
  229.         final FieldVector3D<T> extPointTopo = transformPoint(extPoint, frame, date);

  230.         return extPointTopo.getDelta();
  231.     }

  232.     /** Get the azimuth of a point with regards to the topocentric frame center point.
  233.      * <p>The azimuth is the angle between the North direction at local point and
  234.      * the projection in local horizontal plane of the direction from local point
  235.      * to given point. Azimuth angles are counted clockwise, i.e positive towards the East.</p>
  236.      * @param extPoint point for which elevation shall be computed
  237.      * @param frame frame in which the point is defined
  238.      * @param date computation date
  239.      * @return azimuth of the point
  240.      */
  241.     public double getAzimuth(final Vector3D extPoint, final Frame frame,
  242.                              final AbsoluteDate date) {

  243.         // Transform given point from given frame to topocentric frame
  244.         final Vector3D extPointTopo = transformPoint(extPoint, frame, date);

  245.         return computeAzimuthFromTopoPoint(extPointTopo);

  246.     }

  247.     /** Get the azimuth of a point with regards to the topocentric frame center point.
  248.      * <p>The azimuth is the angle between the North direction at local point and
  249.      * the projection in local horizontal plane of the direction from local point
  250.      * to given point. Azimuth angles are counted clockwise, i.e positive towards the East.</p>
  251.      * @param <T> type of the elements
  252.      * @param extPoint point for which elevation shall be computed
  253.      * @param frame frame in which the point is defined
  254.      * @param date computation date
  255.      * @return azimuth of the point
  256.      * @since 9.3
  257.      */
  258.     public <T extends CalculusFieldElement<T>> T getAzimuth(final FieldVector3D<T> extPoint, final Frame frame,
  259.                                                             final FieldAbsoluteDate<T> date) {

  260.         // Transform given point from given frame to topocentric frame
  261.         final FieldVector3D<T> extPointTopo = transformPoint(extPoint, frame, date);

  262.         return computeAzimuthFromTopoPoint(extPointTopo);

  263.     }

  264.     /** Get the range of a point with regards to the topocentric frame center point.
  265.      * @param extPoint point for which range shall be computed
  266.      * @param frame frame in which the point is defined
  267.      * @param date computation date
  268.      * @return range (distance) of the point
  269.      */
  270.     public double getRange(final Vector3D extPoint, final Frame frame,
  271.                            final AbsoluteDate date) {

  272.         // Transform given point from given frame to topocentric frame
  273.         final Vector3D extPointTopo = transformPoint(extPoint, frame, date);

  274.         return extPointTopo.getNorm();

  275.     }

  276.     /** Get the range of a point with regards to the topocentric frame center point.
  277.      * @param <T> type of the elements
  278.      * @param extPoint point for which range shall be computed
  279.      * @param frame frame in which the point is defined
  280.      * @param date computation date
  281.      * @return range (distance) of the point
  282.      * @since 9.3
  283.      */
  284.     public <T extends CalculusFieldElement<T>> T getRange(final FieldVector3D<T> extPoint, final Frame frame,
  285.                                                           final FieldAbsoluteDate<T> date) {

  286.         // Transform given point from given frame to topocentric frame
  287.         final FieldVector3D<T> extPointTopo = transformPoint(extPoint, frame, date);

  288.         return extPointTopo.getNorm();

  289.     }

  290.     /** Get the range rate of a point with regards to the topocentric frame center point.
  291.      * @param extPV point/velocity for which range rate shall be computed
  292.      * @param frame frame in which the point is defined
  293.      * @param date computation date
  294.      * @return range rate of the point (positive if point departs from frame)
  295.      */
  296.     public double getRangeRate(final PVCoordinates extPV, final Frame frame,
  297.                                final AbsoluteDate date) {

  298.         // Transform given point from given frame to topocentric frame
  299.         final KinematicTransform t = frame.getKinematicTransformTo(this, date);
  300.         final PVCoordinates extPVTopo = t.transformOnlyPV(extPV);

  301.         // Compute range rate (doppler) : relative rate along the line of sight
  302.         return Vector3D.dotProduct(extPVTopo.getPosition(), extPVTopo.getVelocity()) /
  303.                 extPVTopo.getPosition().getNorm();

  304.     }

  305.     /** Get the range rate of a point with regards to the topocentric frame center point.
  306.      * @param <T> type of the elements
  307.      * @param extPV point/velocity for which range rate shall be computed
  308.      * @param frame frame in which the point is defined
  309.      * @param date computation date
  310.      * @return range rate of the point (positive if point departs from frame)
  311.      * @since 9.3
  312.      */
  313.     public <T extends CalculusFieldElement<T>> T getRangeRate(final FieldPVCoordinates<T> extPV, final Frame frame,
  314.                                                               final FieldAbsoluteDate<T> date) {

  315.         // Transform given point from given frame to topocentric frame
  316.         final FieldKinematicTransform<T> t = frame.getKinematicTransformTo(this, date);
  317.         final FieldPVCoordinates<T> extPVTopo = t.transformOnlyPV(extPV);

  318.         // Compute range rate (doppler) : relative rate along the line of sight
  319.         return FieldVector3D.dotProduct(extPVTopo.getPosition(), extPVTopo.getVelocity()).divide(
  320.                 extPVTopo.getPosition().getNorm());

  321.     }

  322.     /**
  323.      * Compute the limit visibility point for a satellite in a given direction.
  324.      * <p>
  325.      * This method can be used to compute visibility circles around ground stations
  326.      * for example, using a simple loop on azimuth, with either a fixed elevation
  327.      * or an elevation that depends on azimuth to take ground masks into account.
  328.      * </p>
  329.      * @param radius satellite distance to Earth center
  330.      * @param azimuth pointing azimuth from station
  331.      * @param elevation pointing elevation from station
  332.      * @return limit visibility point for the satellite
  333.      */
  334.     public GeodeticPoint computeLimitVisibilityPoint(final double radius,
  335.                                                      final double azimuth, final double elevation) {
  336.         try {
  337.             // convergence threshold on point position: 1mm
  338.             final double deltaP = 0.001;
  339.             final UnivariateSolver solver =
  340.                     new BracketingNthOrderBrentSolver(deltaP / Constants.WGS84_EARTH_EQUATORIAL_RADIUS,
  341.                             deltaP, deltaP, 5);

  342.             // find the distance such that a point in the specified direction and at the solved-for
  343.             // distance is exactly at the specified radius
  344.             final double distance = solver.solve(1000, new UnivariateFunction() {
  345.                 /** {@inheritDoc} */
  346.                 public double value(final double x) {
  347.                     final GeodeticPoint gp = pointAtDistance(azimuth, elevation, x);
  348.                     return parentShape.transform(gp).getNorm() - radius;
  349.                 }
  350.             }, 0, 2 * radius);

  351.             // return the limit point
  352.             return pointAtDistance(azimuth, elevation, distance);

  353.         } catch (MathRuntimeException mrte) {
  354.             throw new OrekitException(mrte);
  355.         }
  356.     }

  357.     /** Compute the point observed from the station at some specified distance.
  358.      * @param azimuth pointing azimuth from station
  359.      * @param elevation pointing elevation from station
  360.      * @param distance distance to station
  361.      * @return observed point
  362.      */
  363.     public GeodeticPoint pointAtDistance(final double azimuth, final double elevation,
  364.                                          final double distance) {
  365.         final SinCos scAz  = FastMath.sinCos(azimuth);
  366.         final SinCos scEl  = FastMath.sinCos(elevation);
  367.         final Vector3D  observed = new Vector3D(distance * scEl.cos() * scAz.sin(),
  368.                 distance * scEl.cos() * scAz.cos(),
  369.                 distance * scEl.sin());
  370.         return parentShape.transform(observed, this, AbsoluteDate.ARBITRARY_EPOCH);
  371.     }

  372.     /** {@inheritDoc} */
  373.     @Override
  374.     public Vector3D getPosition(final AbsoluteDate date, final Frame frame) {
  375.         return getStaticTransformTo(frame, date).transformPosition(Vector3D.ZERO);
  376.     }

  377.     /** Get the {@link PVCoordinates} of the topocentric frame origin in the selected frame.
  378.      * @param date current date
  379.      * @param frame the frame where to define the position
  380.      * @return position/velocity of the topocentric frame origin (m and m/s)
  381.      */
  382.     public TimeStampedPVCoordinates getPVCoordinates(final AbsoluteDate date, final Frame frame) {
  383.         return getTransformTo(frame, date).transformPVCoordinates(new TimeStampedPVCoordinates(date,
  384.                 Vector3D.ZERO,
  385.                 Vector3D.ZERO,
  386.                 Vector3D.ZERO));
  387.     }

  388.     /** Get the topocentric position from {@link TrackingCoordinates}.
  389.      * @param coords The coordinates that are to be converted.
  390.      * @return The topocentric coordinates.
  391.      * @since 12.1
  392.      */
  393.     public static Vector3D getTopocentricPosition(final TrackingCoordinates coords) {
  394.         return getTopocentricPosition(coords.getAzimuth(), coords.getElevation(), coords.getRange());
  395.     }

  396.     /** Get the topocentric position from {@link FieldTrackingCoordinates}.
  397.      * @param coords The coordinates that are to be converted.
  398.      * @param <T> Type of the field coordinates.
  399.      * @return The topocentric coordinates.
  400.      * @since 12.1
  401.      */
  402.     public static <T extends CalculusFieldElement<T>> FieldVector3D<T> getTopocentricPosition(final FieldTrackingCoordinates<T> coords) {
  403.         return getTopocentricPosition(coords.getAzimuth(), coords.getElevation(), coords.getRange());
  404.     }

  405.     /**
  406.      * Gets the topocentric position from a set of az/el/ra coordinates.
  407.      * @param azimuth the angle of rotation around the vertical axis, going East.
  408.      * @param elevation the elevation angle from the local horizon.
  409.      * @param range the distance from the goedetic position.
  410.      * @return the topocentric position.
  411.      * @since 12.1
  412.      */
  413.     private static Vector3D getTopocentricPosition(final double azimuth, final double elevation, final double range) {
  414.         final SinCos sinCosAz = FastMath.sinCos(azimuth);
  415.         final SinCos sinCosEL = FastMath.sinCos(elevation);
  416.         return new Vector3D(range * sinCosEL.cos() * sinCosAz.sin(), range * sinCosEL.cos() * sinCosAz.cos(), range * sinCosEL.sin());
  417.     }

  418.     /**
  419.      * Gets the topocentric position from a set of az/el/ra coordinates.
  420.      * @param azimuth the angle of rotation around the vertical axis, going East.
  421.      * @param elevation the elevation angle from the local horizon.
  422.      * @param range the distance from the geodetic position.
  423.      * @return the topocentric position.
  424.      * @param <T> the type of the az/el/ra coordinates.
  425.      * @since 12.1
  426.      */
  427.     private static <T extends CalculusFieldElement<T>> FieldVector3D<T> getTopocentricPosition(final T azimuth, final T elevation, final T range) {
  428.         final FieldSinCos<T> sinCosAz = FastMath.sinCos(azimuth);
  429.         final FieldSinCos<T> sinCosEl = FastMath.sinCos(elevation);
  430.         return new FieldVector3D<>(
  431.                 range.multiply(sinCosEl.cos()).multiply(sinCosAz.sin()),
  432.                 range.multiply(sinCosEl.cos()).multiply(sinCosAz.cos()),
  433.                 range.multiply(sinCosEl.sin())
  434.         );
  435.     }

  436.     /** Transform point in topocentric frame.
  437.      * @param extPoint point
  438.      * @param date current date
  439.      * @param frame the frame where to define the position
  440.      * @return transformed point in topocentric frame
  441.      */
  442.     private Vector3D transformPoint(final Vector3D extPoint, final Frame frame, final AbsoluteDate date) {
  443.         final StaticTransform t = frame.getStaticTransformTo(this, date);
  444.         return t.transformPosition(extPoint);
  445.     }

  446.     /** Transform point in topocentric frame.
  447.      * @param <T> type of the field elements
  448.      * @param extPoint point
  449.      * @param date current date
  450.      * @param frame the frame where to define the position
  451.      * @return transformed point in topocentric frame
  452.      */
  453.     private <T extends CalculusFieldElement<T>> FieldVector3D<T> transformPoint(final FieldVector3D<T> extPoint,
  454.                                                                                 final Frame frame,
  455.                                                                                 final FieldAbsoluteDate<T> date) {
  456.         final FieldStaticTransform<T> t = frame.getStaticTransformTo(this, date);
  457.         return t.transformPosition(extPoint);
  458.     }

  459.     /** Compute azimuth from topocentric point.
  460.      * @param extPointTopo topocentric point
  461.      * @return azimuth
  462.      */
  463.     private double computeAzimuthFromTopoPoint(final Vector3D extPointTopo) {
  464.         final double azimuth = FastMath.atan2(extPointTopo.getX(), extPointTopo.getY());
  465.         if (azimuth < 0.0) {
  466.             return azimuth + MathUtils.TWO_PI;
  467.         } else {
  468.             return azimuth;
  469.         }
  470.     }

  471.     /** Compute azimuth from topocentric point.
  472.      * @param <T> type of the field elements
  473.      * @param extPointTopo topocentric point
  474.      * @return azimuth
  475.      */
  476.     private <T extends CalculusFieldElement<T>> T computeAzimuthFromTopoPoint(final FieldVector3D<T> extPointTopo) {
  477.         final T azimuth = FastMath.atan2(extPointTopo.getX(), extPointTopo.getY());
  478.         if (azimuth.getReal() < 0.0) {
  479.             return azimuth.add(MathUtils.TWO_PI);
  480.         } else {
  481.             return azimuth;
  482.         }
  483.     }

  484. }