1   /* Copyright 2002-2020 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  
19  import java.io.Serializable;
20  
21  import org.hipparchus.geometry.euclidean.threed.Vector3D;
22  import org.hipparchus.geometry.euclidean.twod.Vector2D;
23  import org.hipparchus.util.FastMath;
24  import org.hipparchus.util.MathArrays;
25  import org.orekit.frames.Frame;
26  import org.orekit.utils.TimeStampedPVCoordinates;
27  
28  /**
29   * Model of a 2D ellipse in 3D space.
30   * <p>
31   * These ellipses are mainly created as plane sections of general 3D ellipsoids,
32   * but can be used for other purposes.
33   * </p>
34   * <p>
35   * Instances of this class are guaranteed to be immutable.
36   * </p>
37   * @see Ellipsoid#getPlaneSection(Vector3D, Vector3D)
38   * @since 7.0
39   * @author Luc Maisonobe
40   */
41  public class Ellipse implements Serializable {
42  
43      /** Serializable UID. */
44      private static final long serialVersionUID = 20140925L;
45  
46      /** Convergence limit. */
47      private static final double ANGULAR_THRESHOLD = 1.0e-12;
48  
49      /** Center of the 2D ellipse. */
50      private final Vector3D center;
51  
52      /** Unit vector along the major axis. */
53      private final Vector3D u;
54  
55      /** Unit vector along the minor axis. */
56      private final Vector3D v;
57  
58      /** Semi major axis. */
59      private final double a;
60  
61      /** Semi minor axis. */
62      private final double b;
63  
64      /** Frame in which the ellipse is defined. */
65      private final Frame frame;
66  
67      /** Semi major axis radius power 2. */
68      private final double a2;
69  
70      /** Semi minor axis power 2. */
71      private final double b2;
72  
73      /** Eccentricity power 2. */
74      private final double e2;
75  
76      /** 1 minus flatness. */
77      private final double g;
78  
79      /** g * g. */
80      private final double g2;
81  
82      /** Evolute factor along major axis. */
83      private final double evoluteFactorX;
84  
85      /** Evolute factor along minor axis. */
86      private final double evoluteFactorY;
87  
88      /** Simple constructor.
89       * @param center center of the 2D ellipse
90       * @param u unit vector along the major axis
91       * @param v unit vector along the minor axis
92       * @param a semi major axis
93       * @param b semi minor axis
94       * @param frame frame in which the ellipse is defined
95       */
96      public Ellipse(final Vector3D center, final Vector3D u,
97                     final Vector3D v, final double a, final double b,
98                     final Frame frame) {
99          this.center = center;
100         this.u      = u;
101         this.v      = v;
102         this.a      = a;
103         this.b      = b;
104         this.frame  = frame;
105         this.a2     = a * a;
106         this.g      = b / a;
107         this.g2     = g * g;
108         this.e2     = 1 - g2;
109         this.b2     = b * b;
110         this.evoluteFactorX = (a2 - b2) / (a2 * a2);
111         this.evoluteFactorY = (b2 - a2) / (b2 * b2);
112     }
113 
114     /** Get the center of the 2D ellipse.
115      * @return center of the 2D ellipse
116      */
117     public Vector3D getCenter() {
118         return center;
119     }
120 
121     /** Get the unit vector along the major axis.
122      * @return unit vector along the major axis
123      */
124     public Vector3D getU() {
125         return u;
126     }
127 
128     /** Get the unit vector along the minor axis.
129      * @return unit vector along the minor axis
130      */
131     public Vector3D getV() {
132         return v;
133     }
134 
135     /** Get the semi major axis.
136      * @return semi major axis
137      */
138     public double getA() {
139         return a;
140     }
141 
142     /** Get the semi minor axis.
143      * @return semi minor axis
144      */
145     public double getB() {
146         return b;
147     }
148 
149     /** Get the defining frame.
150      * @return defining frame
151      */
152     public Frame getFrame() {
153         return frame;
154     }
155 
156     /** Get a point of the 2D ellipse.
157      * @param theta angular parameter on the ellipse (really the eccentric anomaly)
158      * @return ellipse point at theta, in underlying ellipsoid frame
159      */
160     public Vector3D pointAt(final double theta) {
161         return toSpace(new Vector2D(a * FastMath.cos(theta), b * FastMath.sin(theta)));
162     }
163 
164     /** Create a point from its ellipse-relative coordinates.
165      * @param p point defined with respect to ellipse
166      * @return point defined with respect to 3D frame
167      * @see #toPlane(Vector3D)
168      */
169     public Vector3D toSpace(final Vector2D p) {
170         return new Vector3D(1, center, p.getX(), u, p.getY(), v);
171     }
172 
173     /** Project a point to the ellipse plane.
174      * @param p point defined with respect to 3D frame
175      * @return point defined with respect to ellipse
176      * @see #toSpace(Vector2D)
177      */
178     public Vector2D toPlane(final Vector3D p) {
179         final Vector3D delta = p.subtract(center);
180         return new Vector2D(Vector3D.dotProduct(delta, u), Vector3D.dotProduct(delta, v));
181     }
182 
183     /** Find the closest ellipse point.
184      * @param p point in the ellipse plane to project on the ellipse itself
185      * @return closest point belonging to 2D meridian ellipse
186      */
187     public Vector2D projectToEllipse(final Vector2D p) {
188 
189         final double x = FastMath.abs(p.getX());
190         final double y = p.getY();
191 
192         if (x <= ANGULAR_THRESHOLD * FastMath.abs(y)) {
193             // the point is almost on the minor axis, approximate the ellipse with
194             // the osculating circle whose center is at evolute cusp along minor axis
195             final double osculatingRadius = a2 / b;
196             final double evoluteCuspZ     = FastMath.copySign(a * e2 / g, -y);
197             final double deltaZ           = y - evoluteCuspZ;
198             final double ratio            = osculatingRadius / FastMath.hypot(deltaZ, x);
199             return new Vector2D(FastMath.copySign(ratio * x, p.getX()),
200                                 evoluteCuspZ + ratio * deltaZ);
201         }
202 
203         if (FastMath.abs(y) <= ANGULAR_THRESHOLD * x) {
204             // the point is almost on the major axis
205 
206             final double osculatingRadius = b2 / a;
207             final double evoluteCuspR     = a * e2;
208             final double deltaR           = x - evoluteCuspR;
209             if (deltaR >= 0) {
210                 // the point is outside of the ellipse evolute, approximate the ellipse
211                 // with the osculating circle whose center is at evolute cusp along major axis
212                 final double ratio = osculatingRadius / FastMath.hypot(y, deltaR);
213                 return new Vector2D(FastMath.copySign(evoluteCuspR + ratio * deltaR, p.getX()),
214                                     ratio * y);
215             }
216 
217             // the point is on the part of the major axis within ellipse evolute
218             // we can compute the closest ellipse point analytically
219             final double rEllipse = x / e2;
220             return new Vector2D(FastMath.copySign(rEllipse, p.getX()),
221                                 FastMath.copySign(g * FastMath.sqrt(a2 - rEllipse * rEllipse), y));
222 
223         } else {
224 
225             // initial point at evolute cusp along major axis
226             double omegaX = a * e2;
227             double omegaY = 0.0;
228 
229             double projectedX = x;
230             double projectedY = y;
231             double deltaX     = Double.POSITIVE_INFINITY;
232             double deltaY     = Double.POSITIVE_INFINITY;
233             int count = 0;
234             final double threshold = ANGULAR_THRESHOLD * ANGULAR_THRESHOLD * a2;
235             while ((deltaX * deltaX + deltaY * deltaY) > threshold && count++ < 100) { // this loop usually converges in 3 iterations
236 
237                 // find point at the intersection of ellipse and line going from query point to evolute point
238                 final double dx         = x - omegaX;
239                 final double dy         = y - omegaY;
240                 final double alpha      = b2 * dx * dx + a2 * dy * dy;
241                 final double betaPrime  = b2 * omegaX * dx + a2 * omegaY * dy;
242                 final double gamma      = b2 * omegaX * omegaX + a2 * omegaY * omegaY - a2 * b2;
243                 final double deltaPrime = MathArrays.linearCombination(betaPrime, betaPrime, -alpha, gamma);
244                 final double ratio      = (betaPrime <= 0) ?
245                                           (FastMath.sqrt(deltaPrime) - betaPrime) / alpha :
246                                           -gamma / (FastMath.sqrt(deltaPrime) + betaPrime);
247                 final double previousX  = projectedX;
248                 final double previousY  = projectedY;
249                 projectedX = omegaX + ratio * dx;
250                 projectedY = omegaY + ratio * dy;
251 
252                 // find new evolute point
253                 omegaX     = evoluteFactorX * projectedX * projectedX * projectedX;
254                 omegaY     = evoluteFactorY * projectedY * projectedY * projectedY;
255 
256                 // compute convergence parameters
257                 deltaX     = projectedX - previousX;
258                 deltaY     = projectedY - previousY;
259 
260             }
261             return new Vector2D(FastMath.copySign(projectedX, p.getX()), projectedY);
262         }
263     }
264 
265     /** Project position-velocity-acceleration on an ellipse.
266      * @param pv position-velocity-acceleration to project, in the reference frame
267      * @return projected position-velocity-acceleration
268      */
269     public TimeStampedPVCoordinatesVCoordinates">TimeStampedPVCoordinates projectToEllipse(final TimeStampedPVCoordinates pv) {
270 
271         // find the closest point in the meridian plane
272         final Vector2D p2D = toPlane(pv.getPosition());
273         final Vector2D e2D = projectToEllipse(p2D);
274 
275         // tangent to the ellipse
276         final double fx = -a2 * e2D.getY();
277         final double fy =  b2 * e2D.getX();
278         final double f2 = fx * fx + fy * fy;
279         final double f  = FastMath.sqrt(f2);
280         final Vector2D tangent = new Vector2D(fx / f, fy / f);
281 
282         // normal to the ellipse (towards interior)
283         final Vector2D normal = new Vector2D(-tangent.getY(), tangent.getX());
284 
285         // center of curvature
286         final double x2     = e2D.getX() * e2D.getX();
287         final double y2     = e2D.getY() * e2D.getY();
288         final double eX     = evoluteFactorX * x2;
289         final double eY     = evoluteFactorY * y2;
290         final double omegaX = eX * e2D.getX();
291         final double omegaY = eY * e2D.getY();
292 
293         // velocity projection ratio
294         final double rho                = FastMath.hypot(e2D.getX() - omegaX, e2D.getY() - omegaY);
295         final double d                  = FastMath.hypot(p2D.getX() - omegaX, p2D.getY() - omegaY);
296         final double projectionRatio    = rho / d;
297 
298         // tangential velocity
299         final Vector2D pDot2D           = new Vector2D(Vector3D.dotProduct(pv.getVelocity(), u),
300                                                        Vector3D.dotProduct(pv.getVelocity(), v));
301         final double   pDotTangent      = pDot2D.dotProduct(tangent);
302         final double   pDotNormal       = pDot2D.dotProduct(normal);
303         final double   eDotTangent      = projectionRatio * pDotTangent;
304         final Vector2D eDot2D           = new Vector2D(eDotTangent, tangent);
305         final Vector2D tangentDot       = new Vector2D(a2 * b2 * (e2D.getX() * eDot2D.getY() - e2D.getY() * eDot2D.getX()) / f2,
306                                                        normal);
307 
308         // velocity of the center of curvature in the meridian plane
309         final double omegaXDot          = 3 * eX * eDotTangent * tangent.getX();
310         final double omegaYDot          = 3 * eY * eDotTangent * tangent.getY();
311 
312         // derivative of the projection ratio
313         final double voz                = omegaXDot * tangent.getY() - omegaYDot * tangent.getX();
314         final double vsz                = -pDotNormal;
315         final double projectionRatioDot = ((rho - d) * voz - rho * vsz) / (d * d);
316 
317         // acceleration
318         final Vector2D pDotDot2D        = new Vector2D(Vector3D.dotProduct(pv.getAcceleration(), u),
319                                                        Vector3D.dotProduct(pv.getAcceleration(), v));
320         final double   pDotDotTangent   = pDotDot2D.dotProduct(tangent);
321         final double   pDotTangentDot   = pDot2D.dotProduct(tangentDot);
322         final double   eDotDotTangent   = projectionRatio    * (pDotDotTangent + pDotTangentDot) +
323                                           projectionRatioDot * pDotTangent;
324         final Vector2D eDotDot2D        = new Vector2D(eDotDotTangent, tangent, eDotTangent, tangentDot);
325 
326         // back to 3D
327         final Vector3D e3D       = toSpace(e2D);
328         final Vector3D eDot3D    = new Vector3D(eDot2D.getX(),    u, eDot2D.getY(),    v);
329         final Vector3D eDotDot3D = new Vector3D(eDotDot2D.getX(), u, eDotDot2D.getY(), v);
330 
331         return new TimeStampedPVCoordinates(pv.getDate(), e3D, eDot3D, eDotDot3D);
332 
333     }
334 
335     /** Find the center of curvature (point on the evolute) at the nadir of a point.
336      * @param point point in the ellipse plane
337      * @return center of curvature of the ellipse directly at point nadir
338      * @since 7.1
339      */
340     public Vector2D getCenterOfCurvature(final Vector2D point) {
341         final Vector2D projected = projectToEllipse(point);
342         return new Vector2D(evoluteFactorX * projected.getX() * projected.getX() * projected.getX(),
343                             evoluteFactorY * projected.getY() * projected.getY() * projected.getY());
344     }
345 
346 }