Ellipse.java

  1. /* Copyright 2002-2018 CS Systèmes d'Information
  2.  * Licensed to CS Systèmes d'Information (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 java.io.Serializable;

  19. import org.hipparchus.geometry.euclidean.threed.Vector3D;
  20. import org.hipparchus.geometry.euclidean.twod.Vector2D;
  21. import org.hipparchus.util.FastMath;
  22. import org.hipparchus.util.MathArrays;
  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 implements Serializable {

  39.     /** Serializable UID. */
  40.     private static final long serialVersionUID = 20140925L;

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

  43.     /** Center of the 2D ellipse. */
  44.     private final Vector3D center;

  45.     /** Unit vector along the major axis. */
  46.     private final Vector3D u;

  47.     /** Unit vector along the minor axis. */
  48.     private final Vector3D v;

  49.     /** Semi major axis. */
  50.     private final double a;

  51.     /** Semi minor axis. */
  52.     private final double b;

  53.     /** Frame in which the ellipse is defined. */
  54.     private final Frame frame;

  55.     /** Semi major axis radius power 2. */
  56.     private final double a2;

  57.     /** Semi minor axis power 2. */
  58.     private final double b2;

  59.     /** Eccentricity power 2. */
  60.     private final double e2;

  61.     /** 1 minus flatness. */
  62.     private final double g;

  63.     /** g * g. */
  64.     private final double g2;

  65.     /** Evolute factor along major axis. */
  66.     private final double evoluteFactorX;

  67.     /** Evolute factor along minor axis. */
  68.     private final double evoluteFactorY;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  188.         } else {
  189.             final double k = FastMath.hypot(x / a, y / b);
  190.             double projectedX = x / k;
  191.             double projectedY = y / k;
  192.             double deltaX = Double.POSITIVE_INFINITY;
  193.             double deltaY = Double.POSITIVE_INFINITY;
  194.             int count = 0;
  195.             final double threshold = ANGULAR_THRESHOLD * ANGULAR_THRESHOLD * a2;
  196.             while ((deltaX * deltaX + deltaY * deltaY) > threshold && count++ < 100) { // this loop usually converges in 3 iterations
  197.                 final double omegaX     = evoluteFactorX * projectedX * projectedX * projectedX;
  198.                 final double omegaY     = evoluteFactorY * projectedY * projectedY * projectedY;
  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 beta       = b2 * omegaX * dx + a2 * omegaY * dy;
  203.                 final double gamma      = b2 * omegaX * omegaX + a2 * omegaY * omegaY - a2 * b2;
  204.                 final double deltaPrime = MathArrays.linearCombination(beta, beta, -alpha, gamma);
  205.                 final double ratio      = (beta <= 0) ?
  206.                                           (FastMath.sqrt(deltaPrime) - beta) / alpha :
  207.                                           -gamma / (FastMath.sqrt(deltaPrime) + beta);
  208.                 final double previousX  = projectedX;
  209.                 final double previousY  = projectedY;
  210.                 projectedX = omegaX + ratio * dx;
  211.                 projectedY = omegaY + ratio * dy;
  212.                 deltaX     = projectedX - previousX;
  213.                 deltaY     = projectedY - previousY;
  214.             }
  215.             return new Vector2D(FastMath.copySign(projectedX, p.getX()), projectedY);
  216.         }
  217.     }

  218.     /** Project position-velocity-acceleration on an ellipse.
  219.      * @param pv position-velocity-acceleration to project, in the reference frame
  220.      * @return projected position-velocity-acceleration
  221.      */
  222.     public TimeStampedPVCoordinates projectToEllipse(final TimeStampedPVCoordinates pv) {

  223.         // find the closest point in the meridian plane
  224.         final Vector2D p2D = toPlane(pv.getPosition());
  225.         final Vector2D e2D = projectToEllipse(p2D);

  226.         // tangent to the ellipse
  227.         final double fx = -a2 * e2D.getY();
  228.         final double fy =  b2 * e2D.getX();
  229.         final double f2 = fx * fx + fy * fy;
  230.         final double f  = FastMath.sqrt(f2);
  231.         final Vector2D tangent = new Vector2D(fx / f, fy / f);

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

  234.         // center of curvature
  235.         final double x2     = e2D.getX() * e2D.getX();
  236.         final double y2     = e2D.getY() * e2D.getY();
  237.         final double eX     = evoluteFactorX * x2;
  238.         final double eY     = evoluteFactorY * y2;
  239.         final double omegaX = eX * e2D.getX();
  240.         final double omegaY = eY * e2D.getY();

  241.         // velocity projection ratio
  242.         final double rho                = FastMath.hypot(e2D.getX() - omegaX, e2D.getY() - omegaY);
  243.         final double d                  = FastMath.hypot(p2D.getX() - omegaX, p2D.getY() - omegaY);
  244.         final double projectionRatio    = rho / d;

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

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

  257.         // derivative of the projection ratio
  258.         final double voz                = omegaXDot * tangent.getY() - omegaYDot * tangent.getX();
  259.         final double vsz                = -pDotNormal;
  260.         final double projectionRatioDot = ((rho - d) * voz - rho * vsz) / (d * d);

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

  269.         // back to 3D
  270.         final Vector3D e3D       = toSpace(e2D);
  271.         final Vector3D eDot3D    = new Vector3D(eDot2D.getX(),    u, eDot2D.getY(),    v);
  272.         final Vector3D eDotDot3D = new Vector3D(eDotDot2D.getX(), u, eDotDot2D.getY(), v);

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

  274.     }

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

  285. }