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.apache.commons.math3.exception.MathArithmeticException;
22  import org.apache.commons.math3.geometry.euclidean.threed.Vector3D;
23  import org.apache.commons.math3.geometry.euclidean.twod.Vector2D;
24  import org.apache.commons.math3.util.FastMath;
25  import org.apache.commons.math3.util.MathArrays;
26  import org.apache.commons.math3.util.Precision;
27  import org.orekit.errors.OrekitException;
28  import org.orekit.errors.OrekitMessages;
29  import org.orekit.frames.Frame;
30  
31  /**
32   * Modeling of a general three-axes ellipsoid. <p>
33   * @since 7.0
34   * @author Luc Maisonobe
35   */
36  public class Ellipsoid implements Serializable {
37  
38      /** Serializable UID. */
39      private static final long serialVersionUID = 20140924L;
40  
41      /** Frame at the ellipsoid center, aligned with principal axes. */
42      private final Frame frame;
43  
44      /** First semi-axis length. */
45      private final double a;
46  
47      /** Second semi-axis length. */
48      private final double b;
49  
50      /** Third semi-axis length. */
51      private final double c;
52  
53      /** Simple constructor.
54       * @param frame at the ellipsoid center, aligned with principal axes
55       * @param a first semi-axis length
56       * @param b second semi-axis length
57       * @param c third semi-axis length
58       */
59      public Ellipsoid(final Frame frame, final double a, final double b, final double c) {
60          this.frame = frame;
61          this.a     = a;
62          this.b     = b;
63          this.c     = c;
64      }
65  
66      /** Get the length of the first semi-axis.
67       * @return length of the first semi-axis (m)
68       */
69      public double getA() {
70          return a;
71      }
72  
73      /** Get the length of the second semi-axis.
74       * @return length of the second semi-axis (m)
75       */
76      public double getB() {
77          return b;
78      }
79  
80      /** Get the length of the third semi-axis.
81       * @return length of the third semi-axis (m)
82       */
83      public double getC() {
84          return c;
85      }
86  
87      /** Get the ellipsoid central frame.
88       * @return ellipsoid central frame
89       */
90      public Frame getFrame() {
91          return frame;
92      }
93  
94      /** Check if a point is inside the ellipsoid.
95       * @param point point to check, in the ellipsoid frame
96       * @return true if the point is inside the ellipsoid
97       * (or exactly on ellipsoid surface)
98       * @since 7.1
99       */
100     public boolean isInside(final Vector3D point) {
101         final double scaledX = point.getX() / a;
102         final double scaledY = point.getY() / b;
103         final double scaledZ = point.getZ() / c;
104         return scaledX * scaledX + scaledY * scaledY + scaledZ * scaledZ <= 1.0;
105     }
106 
107     /** Compute the 2D ellipse at the intersection of the 3D ellipsoid and a plane.
108      * @param planePoint point belonging to the plane, in the ellipsoid frame
109      * @param planeNormal normal of the plane, in the ellipsoid frame
110      * @return plane section or null if there are no intersections
111      */
112     public Ellipse getPlaneSection(final Vector3D planePoint, final Vector3D planeNormal) {
113 
114         // we define the points Q in the plane using two free variables τ and υ as:
115         // Q = P + τ u + υ v
116         // where u and v are two unit vectors belonging to the plane
117         // Q belongs to the 3D ellipsoid so:
118         // (xQ / a)² + (yQ / b)² + (zQ / c)² = 1
119         // combining both equations, we get:
120         //   (xP² + 2 xP (τ xU + υ xV) + (τ xU + υ xV)²) / a²
121         // + (yP² + 2 yP (τ yU + υ yV) + (τ yU + υ yV)²) / b²
122         // + (zP² + 2 zP (τ zU + υ zV) + (τ zU + υ zV)²) / c²
123         // = 1
124         // which can be rewritten:
125         // α τ² + β υ² + 2 γ τυ + 2 δ τ + 2 ε υ + ζ = 0
126         // with
127         // α =  xU²  / a² +  yU²  / b² +  zU²  / c² > 0
128         // β =  xV²  / a² +  yV²  / b² +  zV²  / c² > 0
129         // γ = xU xV / a² + yU yV / b² + zU zV / c²
130         // δ = xP xU / a² + yP yU / b² + zP zU / c²
131         // ε = xP xV / a² + yP yV / b² + zP zV / c²
132         // ζ =  xP²  / a² +  yP²  / b² +  zP²  / c² - 1
133         // this is the equation of a conic (here an ellipse)
134         // Of course, we note that if the point P belongs to the ellipsoid
135         // then ζ = 0 and the equation holds at point P since τ = 0 and υ = 0
136         final Vector3D u     = planeNormal.orthogonal();
137         final Vector3D v     = Vector3D.crossProduct(planeNormal, u).normalize();
138         final double xUOa    = u.getX() / a;
139         final double yUOb    = u.getY() / b;
140         final double zUOc    = u.getZ() / c;
141         final double xVOa    = v.getX() / a;
142         final double yVOb    = v.getY() / b;
143         final double zVOc    = v.getZ() / c;
144         final double xPOa    = planePoint.getX() / a;
145         final double yPOb    = planePoint.getY() / b;
146         final double zPOc    = planePoint.getZ() / c;
147         final double alpha   = xUOa * xUOa + yUOb * yUOb + zUOc * zUOc;
148         final double beta    = xVOa * xVOa + yVOb * yVOb + zVOc * zVOc;
149         final double gamma   = MathArrays.linearCombination(xUOa, xVOa, yUOb, yVOb, zUOc, zVOc);
150         final double delta   = MathArrays.linearCombination(xPOa, xUOa, yPOb, yUOb, zPOc, zUOc);
151         final double epsilon = MathArrays.linearCombination(xPOa, xVOa, yPOb, yVOb, zPOc, zVOc);
152         final double zeta    = MathArrays.linearCombination(xPOa, xPOa, yPOb, yPOb, zPOc, zPOc, 1, -1);
153 
154         // reduce the general equation α τ² + β υ² + 2 γ τυ + 2 δ τ + 2 ε υ + ζ = 0
155         // to canonical form (λ/l)² + (μ/m)² = 1
156         // using a coordinates change
157         //       τ = τC + λ cosθ - μ sinθ
158         //       υ = υC + λ sinθ + μ cosθ
159         // or equivalently
160         //       λ =   (τ - τC) cosθ + (υ - υC) sinθ
161         //       μ = - (τ - τC) sinθ + (υ - υC) cosθ
162         // τC and υC are the coordinates of the 2D ellipse center with respect to P
163         // 2l and 2m and are the axes lengths (major or minor depending on which one is greatest)
164         // θ is the angle of the 2D ellipse axis corresponding to axis with length 2l
165 
166         // choose θ in order to cancel the coupling term in λμ
167         // expanding the general equation, we get: A λ² + B μ² + C λμ + D λ + E μ + F = 0
168         // with C = 2[(β - α) cosθ sinθ + γ (cos²θ - sin²θ)]
169         // hence the term is cancelled when θ = arctan(t), with γ t² + (α - β) t - γ = 0
170         // As the solutions of the quadratic equation obey t₁t₂ = -1, they correspond to
171         // angles θ in quadrature to each other. Selecting one solution or the other simply
172         // exchanges the principal axes. As we don't care about which axis we want as the
173         // first one, we select an arbitrary solution
174         final double tanTheta;
175         if (FastMath.abs(gamma) < Precision.SAFE_MIN) {
176             tanTheta = 0.0;
177         } else {
178             final double bMA = beta - alpha;
179             tanTheta = (bMA >= 0) ?
180                        (-2 * gamma / (bMA + FastMath.sqrt(bMA * bMA + 4 * gamma * gamma))) :
181                        (-2 * gamma / (bMA - FastMath.sqrt(bMA * bMA + 4 * gamma * gamma)));
182         }
183         final double tan2   = tanTheta * tanTheta;
184         final double cos2   = 1 / (1 + tan2);
185         final double sin2   = tan2 * cos2;
186         final double cosSin = tanTheta * cos2;
187         final double cos    = FastMath.sqrt(cos2);
188         final double sin    = tanTheta * cos;
189 
190         // choose τC and υC in order to cancel the linear terms in λ and μ
191         // expanding the general equation, we get: A λ² + B μ² + C λμ + D λ + E μ + F = 0
192         // with D = 2[ (α τC + γ υC + δ) cosθ + (γ τC + β υC + ε) sinθ]
193         //      E = 2[-(α τC + γ υC + δ) sinθ + (γ τC + β υC + ε) cosθ]
194         // θ can be eliminated by combining the equations
195         // D cosθ - E sinθ = 2[α τC + γ υC + δ]
196         // E cosθ + D sinθ = 2[γ τC + β υC + ε]
197         // hence the terms D and E are both cancelled (regardless of θ) when
198         //     τC = (β δ - γ ε) / (γ² - α β)
199         //     υC = (α ε - γ δ) / (γ² - α β)
200         final double denom = MathArrays.linearCombination(gamma, gamma,   -alpha, beta);
201         final double tauC  = MathArrays.linearCombination(beta,  delta,   -gamma, epsilon) / denom;
202         final double nuC   = MathArrays.linearCombination(alpha, epsilon, -gamma, delta)   / denom;
203 
204         // compute l and m
205         // expanding the general equation, we get: A λ² + B μ² + C λμ + D λ + E μ + F = 0
206         // with A = α cos²θ + β sin²θ + 2 γ cosθ sinθ
207         //      B = α sin²θ + β cos²θ - 2 γ cosθ sinθ
208         //      F = α τC² + β υC² + 2 γ τC υC + 2 δ τC + 2 ε υC + ζ
209         // hence we compute directly l = √(-F/A) and m = √(-F/B)
210         final double twogcs = 2 * gamma * cosSin;
211         final double bigA   = alpha * cos2 + beta * sin2 + twogcs;
212         final double bigB   = alpha * sin2 + beta * cos2 - twogcs;
213         final double bigF   = (alpha * tauC + 2 * (gamma * nuC + delta)) * tauC +
214                               (beta * nuC + 2 * epsilon) * nuC + zeta;
215         final double l      = FastMath.sqrt(-bigF / bigA);
216         final double m      = FastMath.sqrt(-bigF / bigB);
217         if (Double.isNaN(l + m)) {
218             // the plane does not intersect the ellipsoid
219             return null;
220         }
221 
222         if (l > m) {
223             return new Ellipse(new Vector3D(1, planePoint, tauC, u, nuC, v),
224                                new Vector3D( cos, u, sin, v),
225                                new Vector3D(-sin, u, cos, v),
226                                l, m, frame);
227         } else {
228             return new Ellipse(new Vector3D(1, planePoint, tauC, u, nuC, v),
229                                new Vector3D(sin, u, -cos, v),
230                                new Vector3D(cos, u,  sin, v),
231                                m, l, frame);
232         }
233 
234     }
235 
236     /** Find a point on ellipsoid limb, as seen by an external observer.
237      * @param observer observer position in ellipsoid frame
238      * @param outside point outside ellipsoid in ellipsoid frame, defining the phase around limb
239      * @return point on ellipsoid limb
240      * @exception OrekitException if the observer is inside the ellipsoid
241      * @exception MathArithmeticException if ellipsoid center, observer and outside
242      * points are aligned
243      * @since 7.1
244      */
245     public Vector3D pointOnLimb(final Vector3D observer, final Vector3D outside)
246         throws OrekitException, MathArithmeticException {
247 
248         // there is no limb if we are inside the ellipsoid
249         if (isInside(observer)) {
250             throw new OrekitException(OrekitMessages.POINT_INSIDE_ELLIPSOID);
251         }
252         // cut the ellipsoid, to find an elliptical plane section
253         final Vector3D normal  = Vector3D.crossProduct(observer, outside);
254         final Ellipse  section = getPlaneSection(Vector3D.ZERO, normal);
255         final double   a2      = section.getA() * section.getA();
256         final double   b2      = section.getB() * section.getB();
257 
258         // the point on limb is tangential to the ellipse
259         // if T(xt, yt) is an ellipse point at which the tangent is drawn
260         // if O(xo, yo) is a point outside of the ellipse belonging to the tangent at T,
261         // then the two following equations holds:
262         //  a² yt²   + b² xt²   = a² b²  (T belongs to the ellipse)
263         //  a² yt yo + b² xt xo = a² b²  (TP is tangent to the ellipse)
264         // using the second equation to eliminate yt from the first equation, we get
265         // b² (a² - xt xo)² + a² xt² yo² = a⁴ yo²
266         // (a² yo² + b² xo²) xt² - 2 a² b² xo xt + a⁴ (b² - yo²) = 0
267         // which can easily be solved for xt
268         final Vector2D observer2D = section.toPlane(observer);
269         final double   xo         = observer2D.getX();
270         final double   yo         = observer2D.getY();
271         final double   xo2        = xo * xo;
272         final double   yo2        = yo * yo;
273         final double   alpha      = a2 * yo2 + b2 * xo2;
274         final double   beta       = a2 * b2 * xo;
275         final double   gamma      = a2 * a2 * (b2 - yo2);
276         // we know there are two solutions as we already checked the point is outside ellipsoid
277         final double   sqrt       = FastMath.sqrt(beta * beta - alpha * gamma);
278         final double   xt1;
279         final double   xt2;
280         if (beta > 0) {
281             final double s = beta + sqrt;
282             xt1 = s / alpha;
283             xt2 = gamma / s;
284         } else {
285             final double s = beta - sqrt;
286             xt1 = gamma / s;
287             xt2 = s / alpha;
288         }
289 
290         // we return the limb point in the direction of the outside point
291         final Vector3D t1 = section.toSpace(new Vector2D(xt1, b2 * (a2 - xt1 * xo) / (a2 * yo)));
292         final Vector3D t2 = section.toSpace(new Vector2D(xt2, b2 * (a2 - xt2 * xo) / (a2 * yo)));
293         return Vector3D.distance(t1, outside) <= Vector3D.distance(t2, outside) ? t1 : t2;
294 
295     }
296 
297 }