Ellipse.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.bodies;

  18. import org.hipparchus.geometry.euclidean.threed.Vector3D;
  19. import org.hipparchus.geometry.euclidean.twod.Vector2D;
  20. import org.hipparchus.util.FastMath;
  21. import org.hipparchus.util.MathArrays;
  22. import org.hipparchus.util.SinCos;
  23. import org.orekit.frames.Frame;
  24. import org.orekit.utils.TimeStampedPVCoordinates;

  25. /**
  26.  * Model of a 2D ellipse in 3D space.
  27.  * <p>
  28.  * These ellipses are mainly created as plane sections of general 3D ellipsoids,
  29.  * but can be used for other purposes.
  30.  * </p>
  31.  * <p>
  32.  * Instances of this class are guaranteed to be immutable.
  33.  * </p>
  34.  * @see Ellipsoid#getPlaneSection(Vector3D, Vector3D)
  35.  * @since 7.0
  36.  * @author Luc Maisonobe
  37.  */
  38. public class Ellipse {

  39.     /** Convergence limit. */
  40.     private static final double ANGULAR_THRESHOLD = 1.0e-12;

  41.     /** Center of the 2D ellipse. */
  42.     private final Vector3D center;

  43.     /** Unit vector along the major axis. */
  44.     private final Vector3D u;

  45.     /** Unit vector along the minor axis. */
  46.     private final Vector3D v;

  47.     /** Semi major axis. */
  48.     private final double a;

  49.     /** Semi minor axis. */
  50.     private final double b;

  51.     /** Frame in which the ellipse is defined. */
  52.     private final Frame frame;

  53.     /** Semi major axis radius power 2. */
  54.     private final double a2;

  55.     /** Semi minor axis power 2. */
  56.     private final double b2;

  57.     /** Eccentricity power 2. */
  58.     private final double e2;

  59.     /** 1 minus flatness. */
  60.     private final double g;

  61.     /** g * g. */
  62.     private final double g2;

  63.     /** Evolute factor along major axis. */
  64.     private final double evoluteFactorX;

  65.     /** Evolute factor along minor axis. */
  66.     private final double evoluteFactorY;

  67.     /** Simple constructor.
  68.      * @param center center of the 2D ellipse
  69.      * @param u unit vector along the major axis
  70.      * @param v unit vector along the minor axis
  71.      * @param a semi major axis
  72.      * @param b semi minor axis
  73.      * @param frame frame in which the ellipse is defined
  74.      */
  75.     public Ellipse(final Vector3D center, final Vector3D u,
  76.                    final Vector3D v, final double a, final double b,
  77.                    final Frame frame) {
  78.         this.center = center;
  79.         this.u      = u;
  80.         this.v      = v;
  81.         this.a      = a;
  82.         this.b      = b;
  83.         this.frame  = frame;
  84.         this.a2     = a * a;
  85.         this.g      = b / a;
  86.         this.g2     = g * g;
  87.         this.e2     = 1 - g2;
  88.         this.b2     = b * b;
  89.         this.evoluteFactorX = (a2 - b2) / (a2 * a2);
  90.         this.evoluteFactorY = (b2 - a2) / (b2 * b2);
  91.     }

  92.     /** Get the center of the 2D ellipse.
  93.      * @return center of the 2D ellipse
  94.      */
  95.     public Vector3D getCenter() {
  96.         return center;
  97.     }

  98.     /** Get the unit vector along the major axis.
  99.      * @return unit vector along the major axis
  100.      */
  101.     public Vector3D getU() {
  102.         return u;
  103.     }

  104.     /** Get the unit vector along the minor axis.
  105.      * @return unit vector along the minor axis
  106.      */
  107.     public Vector3D getV() {
  108.         return v;
  109.     }

  110.     /** Get the semi major axis.
  111.      * @return semi major axis
  112.      */
  113.     public double getA() {
  114.         return a;
  115.     }

  116.     /** Get the semi minor axis.
  117.      * @return semi minor axis
  118.      */
  119.     public double getB() {
  120.         return b;
  121.     }

  122.     /** Get the defining frame.
  123.      * @return defining frame
  124.      */
  125.     public Frame getFrame() {
  126.         return frame;
  127.     }

  128.     /** Get a point of the 2D ellipse.
  129.      * @param theta angular parameter on the ellipse (really the eccentric anomaly)
  130.      * @return ellipse point at theta, in underlying ellipsoid frame
  131.      */
  132.     public Vector3D pointAt(final double theta) {
  133.         final SinCos scTheta = FastMath.sinCos(theta);
  134.         return toSpace(new Vector2D(a * scTheta.cos(), b * scTheta.sin()));
  135.     }

  136.     /** Create a point from its ellipse-relative coordinates.
  137.      * @param p point defined with respect to ellipse
  138.      * @return point defined with respect to 3D frame
  139.      * @see #toPlane(Vector3D)
  140.      */
  141.     public Vector3D toSpace(final Vector2D p) {
  142.         return new Vector3D(1, center, p.getX(), u, p.getY(), v);
  143.     }

  144.     /** Project a point to the ellipse plane.
  145.      * @param p point defined with respect to 3D frame
  146.      * @return point defined with respect to ellipse
  147.      * @see #toSpace(Vector2D)
  148.      */
  149.     public Vector2D toPlane(final Vector3D p) {
  150.         final Vector3D delta = p.subtract(center);
  151.         return new Vector2D(Vector3D.dotProduct(delta, u), Vector3D.dotProduct(delta, v));
  152.     }

  153.     /** Find the closest ellipse point.
  154.      * @param p point in the ellipse plane to project on the ellipse itself
  155.      * @return closest point belonging to 2D meridian ellipse
  156.      */
  157.     public Vector2D projectToEllipse(final Vector2D p) {

  158.         final double x = FastMath.abs(p.getX());
  159.         final double y = p.getY();

  160.         if (x <= ANGULAR_THRESHOLD * FastMath.abs(y)) {
  161.             // the point is almost on the minor axis, approximate the ellipse with
  162.             // the osculating circle whose center is at evolute cusp along minor axis
  163.             final double osculatingRadius = a2 / b;
  164.             final double evoluteCuspZ     = FastMath.copySign(a * e2 / g, -y);
  165.             final double deltaZ           = y - evoluteCuspZ;
  166.             final double ratio            = osculatingRadius / FastMath.hypot(deltaZ, x);
  167.             return new Vector2D(FastMath.copySign(ratio * x, p.getX()),
  168.                                 evoluteCuspZ + ratio * deltaZ);
  169.         }

  170.         if (FastMath.abs(y) <= ANGULAR_THRESHOLD * x) {
  171.             // the point is almost on the major axis

  172.             final double osculatingRadius = b2 / a;
  173.             final double evoluteCuspR     = a * e2;
  174.             final double deltaR           = x - evoluteCuspR;
  175.             if (deltaR >= 0) {
  176.                 // the point is outside of the ellipse evolute, approximate the ellipse
  177.                 // with the osculating circle whose center is at evolute cusp along major axis
  178.                 final double ratio = osculatingRadius / FastMath.hypot(y, deltaR);
  179.                 return new Vector2D(FastMath.copySign(evoluteCuspR + ratio * deltaR, p.getX()),
  180.                                     ratio * y);
  181.             }

  182.             // the point is on the part of the major axis within ellipse evolute
  183.             // we can compute the closest ellipse point analytically
  184.             final double rEllipse = x / e2;
  185.             return new Vector2D(FastMath.copySign(rEllipse, p.getX()),
  186.                                 FastMath.copySign(g * FastMath.sqrt(a2 - rEllipse * rEllipse), y));

  187.         } else {

  188.             // initial point at evolute cusp along major axis
  189.             double omegaX = a * e2;
  190.             double omegaY = 0.0;

  191.             double projectedX = x;
  192.             double projectedY = y;
  193.             double deltaX     = Double.POSITIVE_INFINITY;
  194.             double deltaY     = Double.POSITIVE_INFINITY;
  195.             int count = 0;
  196.             final double threshold = ANGULAR_THRESHOLD * ANGULAR_THRESHOLD * a2;
  197.             while ((deltaX * deltaX + deltaY * deltaY) > threshold && count++ < 100) { // this loop usually converges in 3 iterations

  198.                 // find point at the intersection of ellipse and line going from query point to evolute point
  199.                 final double dx         = x - omegaX;
  200.                 final double dy         = y - omegaY;
  201.                 final double alpha      = b2 * dx * dx + a2 * dy * dy;
  202.                 final double betaPrime  = b2 * omegaX * dx + a2 * omegaY * dy;
  203.                 final double gamma      = b2 * omegaX * omegaX + a2 * omegaY * omegaY - a2 * b2;
  204.                 final double deltaPrime = MathArrays.linearCombination(betaPrime, betaPrime, -alpha, gamma);
  205.                 final double ratio      = (betaPrime <= 0) ?
  206.                                           (FastMath.sqrt(deltaPrime) - betaPrime) / alpha :
  207.                                           -gamma / (FastMath.sqrt(deltaPrime) + betaPrime);
  208.                 final double previousX  = projectedX;
  209.                 final double previousY  = projectedY;
  210.                 projectedX = omegaX + ratio * dx;
  211.                 projectedY = omegaY + ratio * dy;

  212.                 // find new evolute point
  213.                 omegaX     = evoluteFactorX * projectedX * projectedX * projectedX;
  214.                 omegaY     = evoluteFactorY * projectedY * projectedY * projectedY;

  215.                 // compute convergence parameters
  216.                 deltaX     = projectedX - previousX;
  217.                 deltaY     = projectedY - previousY;

  218.             }
  219.             return new Vector2D(FastMath.copySign(projectedX, p.getX()), projectedY);
  220.         }
  221.     }

  222.     /** Project position-velocity-acceleration on an ellipse.
  223.      * @param pv position-velocity-acceleration to project, in the reference frame
  224.      * @return projected position-velocity-acceleration
  225.      */
  226.     public TimeStampedPVCoordinates projectToEllipse(final TimeStampedPVCoordinates pv) {

  227.         // find the closest point in the meridian plane
  228.         final Vector2D p2D = toPlane(pv.getPosition());
  229.         final Vector2D e2D = projectToEllipse(p2D);

  230.         // tangent to the ellipse
  231.         final double fx = -a2 * e2D.getY();
  232.         final double fy =  b2 * e2D.getX();
  233.         final double f2 = fx * fx + fy * fy;
  234.         final double f  = FastMath.sqrt(f2);
  235.         final Vector2D tangent = new Vector2D(fx / f, fy / f);

  236.         // normal to the ellipse (towards interior)
  237.         final Vector2D normal = new Vector2D(-tangent.getY(), tangent.getX());

  238.         // center of curvature
  239.         final double x2     = e2D.getX() * e2D.getX();
  240.         final double y2     = e2D.getY() * e2D.getY();
  241.         final double eX     = evoluteFactorX * x2;
  242.         final double eY     = evoluteFactorY * y2;
  243.         final double omegaX = eX * e2D.getX();
  244.         final double omegaY = eY * e2D.getY();

  245.         // velocity projection ratio
  246.         final double rho                = FastMath.hypot(e2D.getX() - omegaX, e2D.getY() - omegaY);
  247.         final double d                  = FastMath.hypot(p2D.getX() - omegaX, p2D.getY() - omegaY);
  248.         final double projectionRatio    = rho / d;

  249.         // tangential velocity
  250.         final Vector2D pDot2D           = new Vector2D(Vector3D.dotProduct(pv.getVelocity(), u),
  251.                                                        Vector3D.dotProduct(pv.getVelocity(), v));
  252.         final double   pDotTangent      = pDot2D.dotProduct(tangent);
  253.         final double   pDotNormal       = pDot2D.dotProduct(normal);
  254.         final double   eDotTangent      = projectionRatio * pDotTangent;
  255.         final Vector2D eDot2D           = new Vector2D(eDotTangent, tangent);
  256.         final Vector2D tangentDot       = new Vector2D(a2 * b2 * (e2D.getX() * eDot2D.getY() - e2D.getY() * eDot2D.getX()) / f2,
  257.                                                        normal);

  258.         // velocity of the center of curvature in the meridian plane
  259.         final double omegaXDot          = 3 * eX * eDotTangent * tangent.getX();
  260.         final double omegaYDot          = 3 * eY * eDotTangent * tangent.getY();

  261.         // derivative of the projection ratio
  262.         final double voz                = omegaXDot * tangent.getY() - omegaYDot * tangent.getX();
  263.         final double vsz                = -pDotNormal;
  264.         final double projectionRatioDot = ((rho - d) * voz - rho * vsz) / (d * d);

  265.         // acceleration
  266.         final Vector2D pDotDot2D        = new Vector2D(Vector3D.dotProduct(pv.getAcceleration(), u),
  267.                                                        Vector3D.dotProduct(pv.getAcceleration(), v));
  268.         final double   pDotDotTangent   = pDotDot2D.dotProduct(tangent);
  269.         final double   pDotTangentDot   = pDot2D.dotProduct(tangentDot);
  270.         final double   eDotDotTangent   = projectionRatio    * (pDotDotTangent + pDotTangentDot) +
  271.                                           projectionRatioDot * pDotTangent;
  272.         final Vector2D eDotDot2D        = new Vector2D(eDotDotTangent, tangent, eDotTangent, tangentDot);

  273.         // back to 3D
  274.         final Vector3D e3D       = toSpace(e2D);
  275.         final Vector3D eDot3D    = new Vector3D(eDot2D.getX(),    u, eDot2D.getY(),    v);
  276.         final Vector3D eDotDot3D = new Vector3D(eDotDot2D.getX(), u, eDotDot2D.getY(), v);

  277.         return new TimeStampedPVCoordinates(pv.getDate(), e3D, eDot3D, eDotDot3D);

  278.     }

  279.     /** Find the center of curvature (point on the evolute) at the nadir of a point.
  280.      * @param point point in the ellipse plane
  281.      * @return center of curvature of the ellipse directly at point nadir
  282.      * @since 7.1
  283.      */
  284.     public Vector2D getCenterOfCurvature(final Vector2D point) {
  285.         final Vector2D projected = projectToEllipse(point);
  286.         return new Vector2D(evoluteFactorX * projected.getX() * projected.getX() * projected.getX(),
  287.                             evoluteFactorY * projected.getY() * projected.getY() * projected.getY());
  288.     }

  289. }