1   /* Copyright 2002-2016 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  
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             final double k = FastMath.hypot(x / a, y / b);
225             double projectedX = x / k;
226             double projectedY = y / k;
227             double deltaX = Double.POSITIVE_INFINITY;
228             double deltaY = Double.POSITIVE_INFINITY;
229             int count = 0;
230             final double threshold = ANGULAR_THRESHOLD * ANGULAR_THRESHOLD * a2;
231             while ((deltaX * deltaX + deltaY * deltaY) > threshold && count++ < 100) { // this loop usually converges in 3 iterations
232                 final double omegaX     = evoluteFactorX * projectedX * projectedX * projectedX;
233                 final double omegaY     = evoluteFactorY * projectedY * projectedY * projectedY;
234                 final double dx         = x - omegaX;
235                 final double dy         = y - omegaY;
236                 final double alpha      = b2 * dx * dx + a2 * dy * dy;
237                 final double beta       = b2 * omegaX * dx + a2 * omegaY * dy;
238                 final double gamma      = b2 * omegaX * omegaX + a2 * omegaY * omegaY - a2 * b2;
239                 final double deltaPrime = MathArrays.linearCombination(beta, beta, -alpha, gamma);
240                 final double ratio      = (beta <= 0) ?
241                                           (FastMath.sqrt(deltaPrime) - beta) / alpha :
242                                           -gamma / (FastMath.sqrt(deltaPrime) + beta);
243                 final double previousX  = projectedX;
244                 final double previousY  = projectedY;
245                 projectedX = omegaX + ratio * dx;
246                 projectedY = omegaY + ratio * dy;
247                 deltaX     = projectedX - previousX;
248                 deltaY     = projectedY - previousY;
249             }
250             return new Vector2D(FastMath.copySign(projectedX, p.getX()), projectedY);
251         }
252     }
253 
254     /** Project position-velocity-acceleration on an ellipse.
255      * @param pv position-velocity-acceleration to project, in the reference frame
256      * @return projected position-velocity-acceleration
257      */
258     public TimeStampedPVCoordinates projectToEllipse(final TimeStampedPVCoordinates pv) {
259 
260         // find the closest point in the meridian plane
261         final Vector2D p2D = toPlane(pv.getPosition());
262         final Vector2D e2D = projectToEllipse(p2D);
263 
264         // tangent to the ellipse
265         final double fx = -a2 * e2D.getY();
266         final double fy =  b2 * e2D.getX();
267         final double f2 = fx * fx + fy * fy;
268         final double f  = FastMath.sqrt(f2);
269         final Vector2D tangent = new Vector2D(fx / f, fy / f);
270 
271         // normal to the ellipse (towards interior)
272         final Vector2D normal = new Vector2D(-tangent.getY(), tangent.getX());
273 
274         // center of curvature
275         final double x2     = e2D.getX() * e2D.getX();
276         final double y2     = e2D.getY() * e2D.getY();
277         final double eX     = evoluteFactorX * x2;
278         final double eY     = evoluteFactorY * y2;
279         final double omegaX = eX * e2D.getX();
280         final double omegaY = eY * e2D.getY();
281 
282         // velocity projection ratio
283         final double rho                = FastMath.hypot(e2D.getX() - omegaX, e2D.getY() - omegaY);
284         final double d                  = FastMath.hypot(p2D.getX() - omegaX, p2D.getY() - omegaY);
285         final double projectionRatio    = rho / d;
286 
287         // tangential velocity
288         final Vector2D pDot2D           = new Vector2D(Vector3D.dotProduct(pv.getVelocity(), u),
289                                                        Vector3D.dotProduct(pv.getVelocity(), v));
290         final double   pDotTangent      = pDot2D.dotProduct(tangent);
291         final double   pDotNormal       = pDot2D.dotProduct(normal);
292         final double   eDotTangent      = projectionRatio * pDotTangent;
293         final Vector2D eDot2D           = new Vector2D(eDotTangent, tangent);
294         final Vector2D tangentDot       = new Vector2D(a2 * b2 * (e2D.getX() * eDot2D.getY() - e2D.getY() * eDot2D.getX()) / f2,
295                                                        normal);
296 
297         // velocity of the center of curvature in the meridian plane
298         final double omegaXDot          = 3 * eX * eDotTangent * tangent.getX();
299         final double omegaYDot          = 3 * eY * eDotTangent * tangent.getY();
300 
301         // derivative of the projection ratio
302         final double voz                = omegaXDot * tangent.getY() - omegaYDot * tangent.getX();
303         final double vsz                = -pDotNormal;
304         final double projectionRatioDot = ((rho - d) * voz - rho * vsz) / (d * d);
305 
306         // acceleration
307         final Vector2D pDotDot2D        = new Vector2D(Vector3D.dotProduct(pv.getAcceleration(), u),
308                                                        Vector3D.dotProduct(pv.getAcceleration(), v));
309         final double   pDotDotTangent   = pDotDot2D.dotProduct(tangent);
310         final double   pDotTangentDot   = pDot2D.dotProduct(tangentDot);
311         final double   eDotDotTangent   = projectionRatio    * (pDotDotTangent + pDotTangentDot) +
312                                           projectionRatioDot * pDotTangent;
313         final Vector2D eDotDot2D        = new Vector2D(eDotDotTangent, tangent, eDotTangent, tangentDot);
314 
315         // back to 3D
316         final Vector3D e3D       = toSpace(e2D);
317         final Vector3D eDot3D    = new Vector3D(eDot2D.getX(),    u, eDot2D.getY(),    v);
318         final Vector3D eDotDot3D = new Vector3D(eDotDot2D.getX(), u, eDotDot2D.getY(), v);
319 
320         return new TimeStampedPVCoordinates(pv.getDate(), e3D, eDot3D, eDotDot3D);
321 
322     }
323 
324     /** Find the center of curvature (point on the evolute) at the nadir of a point.
325      * @param point point in the ellipse plane
326      * @return center of curvature of the ellipse directly at point nadir
327      * @since 7.1
328      */
329     public Vector2D getCenterOfCurvature(final Vector2D point) {
330         final Vector2D projected = projectToEllipse(point);
331         return new Vector2D(evoluteFactorX * projected.getX() * projected.getX() * projected.getX(),
332                             evoluteFactorY * projected.getY() * projected.getY() * projected.getY());
333     }
334 
335 }