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