IodGooding.java

  1. /* Copyright 2002-2022 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.estimation.iod;

  18. import org.hipparchus.geometry.euclidean.threed.Vector3D;
  19. import org.hipparchus.util.FastMath;
  20. import org.orekit.estimation.measurements.AngularRaDec;
  21. import org.orekit.frames.Frame;
  22. import org.orekit.orbits.KeplerianOrbit;
  23. import org.orekit.time.AbsoluteDate;
  24. import org.orekit.utils.PVCoordinates;

  25. /**
  26.  * Gooding angles only initial orbit determination, assuming Keplerian motion.
  27.  * An orbit is determined from three angular observations.
  28.  *
  29.  * Reference:
  30.  *    Gooding, R.H., A New Procedure for Orbit Determination Based on Three Lines of Sight (Angles only),
  31.  *    Technical Report 93004, April 1993
  32.  *
  33.  * @author Joris Olympio
  34.  * @since 8.0
  35.  */
  36. public class IodGooding {

  37.     /** Gravitationnal constant. */
  38.     private final double mu;

  39.     /** Normalizing constant for distances. */
  40.     private double R;

  41.     /** Normalizing constant for velocities. */
  42.     private double V;

  43.     /** Normalizing constant for duration. */
  44.     private double T;

  45.     /** observer position 1. */
  46.     private Vector3D vObserverPosition1;

  47.     /** observer position 2. */
  48.     private Vector3D vObserverPosition2;

  49.     /** observer position 3. */
  50.     private Vector3D vObserverPosition3;

  51.     /** Date of the first observation. * */
  52.     private AbsoluteDate date1;

  53.     /** Radius of point 1 (X-R1). */
  54.     private double R1;
  55.     /** Radius of point 2 (X-R2). */
  56.     private double R2;
  57.     /** Radius of point 3 (X-R3). */
  58.     private double R3;

  59.     /** Range of point 1 (O1-R1). */
  60.     private double rho1;
  61.     /** Range of point 2 (O2-R2). */
  62.     private double rho2;
  63.     /** Range of point 3 (O3-R3). */
  64.     private double rho3;

  65.     /** working variable. */
  66.     private double D1;

  67.     /** working variable. */
  68.     private double D3;

  69.     /** factor for FD. */
  70.     private double facFiniteDiff;

  71.     /** Simple Lambert's problem solver. */
  72.     private IodLambert lambert;

  73.     /** Creator.
  74.      * @param mu  gravitational constant
  75.      */
  76.     public IodGooding(final double mu) {
  77.         this.mu = mu;

  78.         this.rho1 = 0;
  79.         this.rho2 = 0;
  80.         this.rho3 = 0;
  81.     }

  82.     /** Get the range for observation (1).
  83.      *
  84.      * @return the range for observation (1).
  85.      */
  86.     public double getRange1() {
  87.         return rho1 * R;
  88.     }

  89.     /** Get the range for observation (2).
  90.      *
  91.      * @return the range for observation (2).
  92.      */
  93.     public double getRange2() {
  94.         return rho2 * R;
  95.     }

  96.     /** Get the range for observation (3).
  97.      *
  98.      * @return the range for observation (3).
  99.      */
  100.     public double getRange3() {
  101.         return rho3 * R;
  102.     }

  103.     /** Orbit got from three angular observations.
  104.      * @param frame inertial frame for observer coordinates and orbit estimate
  105.      * @param raDec1 first angular observation
  106.      * @param raDec2 second angular observation
  107.      * @param raDec3 third angular observation
  108.      * @param rho1init initial guess of the range problem. range 1, in meters
  109.      * @param rho3init initial guess of the range problem. range 3, in meters
  110.      * @param nRev number of complete revolutions between observation 1 and 3
  111.      * @param direction true if posigrade (short way)
  112.      * @return an estimate of the Keplerian orbit at the central date
  113.      *         (i.e., date of the second angular observation)
  114.      * @since 11.0
  115.      */
  116.     public KeplerianOrbit estimate(final Frame frame, final AngularRaDec raDec1,
  117.                                    final AngularRaDec raDec2, final AngularRaDec raDec3,
  118.                                    final double rho1init, final double rho3init,
  119.                                    final int nRev, final boolean direction) {
  120.         return estimate(frame,
  121.                         stationPosition(frame, raDec1),
  122.                         stationPosition(frame, raDec2),
  123.                         stationPosition(frame, raDec3),
  124.                         lineOfSight(raDec1), raDec1.getDate(),
  125.                         lineOfSight(raDec2), raDec2.getDate(),
  126.                         lineOfSight(raDec3), raDec3.getDate(),
  127.                         rho1init, rho3init, nRev, direction);
  128.     }

  129.     /** Orbit got from three angular observations.
  130.      * @param frame inertial frame for observer coordinates and orbit estimate
  131.      * @param raDec1 first angular observation
  132.      * @param raDec2 second angular observation
  133.      * @param raDec3 third angular observation
  134.      * @param rho1init initial guess of the range problem. range 1, in meters
  135.      * @param rho3init initial guess of the range problem. range 3, in meters
  136.      * @return an estimate of the Keplerian orbit at the central date
  137.      *         (i.e., date of the second angular observation)
  138.      * @since 11.0
  139.      */
  140.     public KeplerianOrbit estimate(final Frame frame, final AngularRaDec raDec1,
  141.                                    final AngularRaDec raDec2, final AngularRaDec raDec3,
  142.                                    final double rho1init, final double rho3init) {
  143.         return estimate(frame, raDec1, raDec2, raDec3, rho1init, rho3init, 0, true);
  144.     }

  145.     /** Orbit got from Observed Three Lines of Sight (angles only).
  146.      * @param frame inertial frame for observer coordinates and orbit estimate
  147.      * @param O1 Observer position 1
  148.      * @param O2 Observer position 2
  149.      * @param O3 Observer position 3
  150.      * @param lineOfSight1 line of sight 1
  151.      * @param dateObs1 date of observation 1
  152.      * @param lineOfSight2 line of sight 2
  153.      * @param dateObs2 date of observation 2
  154.      * @param lineOfSight3 line of sight 3
  155.      * @param dateObs3 date of observation 3
  156.      * @param rho1init initial guess of the range problem. range 1, in meters
  157.      * @param rho3init initial guess of the range problem. range 3, in meters
  158.      * @param nRev number of complete revolutions between observation1  and 3
  159.      * @param direction true if posigrade (short way)
  160.      * @return an estimate of the Keplerian orbit at the central date
  161.      *         (i.e., date of the second angular observation)
  162.      */
  163.     public KeplerianOrbit estimate(final Frame frame, final Vector3D O1, final Vector3D O2, final Vector3D O3,
  164.                                    final Vector3D lineOfSight1, final AbsoluteDate dateObs1,
  165.                                    final Vector3D lineOfSight2, final AbsoluteDate dateObs2,
  166.                                    final Vector3D lineOfSight3, final AbsoluteDate dateObs3,
  167.                                    final double rho1init, final double rho3init, final int nRev,
  168.                                    final boolean direction) {

  169.         this.date1 = dateObs1;

  170.         // normalizing coefficients
  171.         R = FastMath.max(rho1init, rho3init);
  172.         V = FastMath.sqrt(mu / R);
  173.         T = R / V;

  174.         // Initialize Lambert's problem solver for non-dimensional units.
  175.         lambert = new IodLambert(1.);

  176.         this.vObserverPosition1 = O1.scalarMultiply(1. / R);
  177.         this.vObserverPosition2 = O2.scalarMultiply(1. / R);
  178.         this.vObserverPosition3 = O3.scalarMultiply(1. / R);

  179.         final int maxiter = 100; // maximum iter

  180.         // solve the range problem
  181.         solveRangeProblem(frame,
  182.                           rho1init / R, rho3init / R,
  183.                           dateObs3.durationFrom(dateObs1) / T, dateObs2.durationFrom(dateObs1) / T,
  184.                           nRev,
  185.                           direction,
  186.                           lineOfSight1, lineOfSight2, lineOfSight3,
  187.                           maxiter);

  188.         // use the Gibbs problem to get the orbit now that we have three position vectors.
  189.         final IodGibbs gibbs = new IodGibbs(mu);
  190.         final Vector3D p1 = vObserverPosition1.add(lineOfSight1.scalarMultiply(rho1)).scalarMultiply(R);
  191.         final Vector3D p2 = vObserverPosition2.add(lineOfSight2.scalarMultiply(rho2)).scalarMultiply(R);
  192.         final Vector3D p3 = vObserverPosition3.add(lineOfSight3.scalarMultiply(rho3)).scalarMultiply(R);
  193.         return gibbs.estimate(frame, p1, dateObs1, p2, dateObs2, p3, dateObs3);
  194.     }

  195.     /** Orbit got from Observed Three Lines of Sight (angles only).
  196.      * assuming there was less than an half revolution between start and final date
  197.      * @param frame inertial frame for observer coordinates and orbit estimate
  198.      * @param O1 Observer position 1
  199.      * @param O2 Observer position 2
  200.      * @param O3 Observer position 3
  201.      * @param lineOfSight1 line of sight 1
  202.      * @param dateObs1 date of observation 1
  203.      * @param lineOfSight2 line of sight 2
  204.      * @param dateObs2 date of observation 1
  205.      * @param lineOfSight3 line of sight 3
  206.      * @param dateObs3 date of observation 1
  207.      * @param rho1init initial guess of the range problem. range 1, in meters
  208.      * @param rho3init initial guess of the range problem. range 3, in meters
  209.      * @return an estimate of the Keplerian orbit at the central date
  210.      *         (i.e., date of the second angular observation)
  211.      */
  212.     public KeplerianOrbit estimate(final Frame frame, final Vector3D O1, final Vector3D O2, final Vector3D O3,
  213.                                    final Vector3D lineOfSight1, final AbsoluteDate dateObs1,
  214.                                    final Vector3D lineOfSight2, final AbsoluteDate dateObs2,
  215.                                    final Vector3D lineOfSight3, final AbsoluteDate dateObs3,
  216.                                    final double rho1init, final double rho3init) {

  217.         return this.estimate(frame, O1, O2, O3, lineOfSight1, dateObs1, lineOfSight2, dateObs2,
  218.                 lineOfSight3, dateObs3, rho1init, rho3init, 0, true);
  219.     }

  220.     /** Solve the range problem when three line of sight are given.
  221.      * @param frame frame to be used (orbit frame)
  222.      * @param rho1init   initial value for range R1, in meters
  223.      * @param rho3init   initial value for range R3, in meters
  224.      * @param T13   time of flight 1->3, in seconds
  225.      * @param T12   time of flight 1->2, in seconds
  226.      * @param nrev number of revolutions
  227.      * @param direction  posigrade (true) or retrograde
  228.      * @param lineOfSight1  line of sight 1
  229.      * @param lineOfSight2  line of sight 2
  230.      * @param lineOfSight3  line of sight 3
  231.      * @param maxIterations         max iter
  232.      * @return nothing
  233.      */
  234.     private boolean solveRangeProblem(final Frame frame,
  235.                                       final double rho1init, final double rho3init,
  236.                                       final double T13, final double T12,
  237.                                       final int nrev,
  238.                                       final boolean direction,
  239.                                       final Vector3D lineOfSight1,
  240.                                       final Vector3D lineOfSight2,
  241.                                       final Vector3D lineOfSight3,
  242.                                       final int maxIterations) {
  243.         final double ARBF = 1e-6;   // finite differences step
  244.         boolean withHalley = true;  // use Halley's method
  245.         final double cvtol = 1e-14; // convergence tolerance

  246.         rho1 = rho1init;
  247.         rho3 = rho3init;


  248.         int iter = 0;
  249.         double stoppingCriterion = 10 * cvtol;
  250.         while (iter < maxIterations && FastMath.abs(stoppingCriterion) > cvtol)  {
  251.             facFiniteDiff = ARBF;

  252.             // proposed in the original algorithm by Gooding.
  253.             // We change the threshold to maxIterations / 2.
  254.             if (iter >= (maxIterations / 2)) {
  255.                 withHalley = true;
  256.             }

  257.             // tentative position for R2
  258.             final Vector3D P2 = getPositionOnLoS2(frame,
  259.                                                   lineOfSight1, rho1,
  260.                                                   lineOfSight3, rho3,
  261.                                                   T13, T12, nrev, direction);

  262.             if (P2 == null) {
  263.                 modifyIterate(lineOfSight1, lineOfSight2, lineOfSight3);
  264.             } else {
  265.                 //
  266.                 R2 = P2.getNorm();

  267.                 // compute current line of sight for (2) and the associated range
  268.                 final Vector3D C = P2.subtract(vObserverPosition2);
  269.                 rho2 = C.getNorm();

  270.                 final double R10 = R1;
  271.                 final double R30 = R3;

  272.                 // indicate how close we are to the direction of sight for measurement (2)
  273.                 // for convergence test
  274.                 final double CR = lineOfSight2.dotProduct(C);

  275.                 // construct a basis and the function f and g.
  276.                 // Specifically, f is the P-coordinate and g the N-coordinate.
  277.                 // They should be zero when line of sight 2 and current direction for 2 from O2 are aligned.
  278.                 final Vector3D u = lineOfSight2.crossProduct(C);
  279.                 final Vector3D P = (u.crossProduct(lineOfSight2)).normalize();
  280.                 final Vector3D ENt = lineOfSight2.crossProduct(P);

  281.                 // if ENt is zero we have a solution!
  282.                 final double ENR = ENt.getNorm();
  283.                 if (ENR == 0.) {
  284.                     return true;
  285.                 }

  286.                 //Normalize EN
  287.                 final Vector3D EN = ENt.normalize();

  288.                 // Coordinate along 'F function'
  289.                 final double Fc = P.dotProduct(C);
  290.                 //Gc = EN.dotProduct(C);

  291.                 // Now get partials, by finite differences
  292.                 final double[] FD = new double[2];
  293.                 final double[] GD = new double[2];
  294.                 computeDerivatives(frame,
  295.                                    rho1, rho3,
  296.                                    R10, R30,
  297.                                    lineOfSight1, lineOfSight3,
  298.                                    P, EN,
  299.                                    Fc,
  300.                                    T13, T12,
  301.                                    withHalley,
  302.                                    nrev,
  303.                                    direction,
  304.                                    FD, GD);

  305.                 // terms of the Jacobian
  306.                 final double fr1 = FD[0];
  307.                 final double fr3 = FD[1];
  308.                 final double gr1 = GD[0];
  309.                 final double gr3 = GD[1];
  310.                 // determinant of the Jacobian matrix
  311.                 final double detj = fr1 * gr3 - fr3 * gr1;

  312.                 // compute the Newton step
  313.                 D3 = -gr3 * Fc / detj;
  314.                 D1 = gr1 * Fc / detj;

  315.                 // update current values of ranges
  316.                 rho1 = rho1 + D3;
  317.                 rho3 = rho3 + D1;

  318.                 // convergence tests
  319.                 final double den = FastMath.max(CR, R2);
  320.                 stoppingCriterion = Fc / den;
  321.             }

  322.             ++iter;
  323.         } // end while

  324.         return true;
  325.     }

  326.     /** Change the current iterate if Lambert's problem solver failed to find a solution.
  327.      *
  328.      * @param lineOfSight1 line of sight 1
  329.      * @param lineOfSight2 line of sight 2
  330.      * @param lineOfSight3 line of sight 3
  331.      */
  332.     private void modifyIterate(final Vector3D lineOfSight1,
  333.                                final Vector3D lineOfSight2,
  334.                                final Vector3D lineOfSight3) {
  335.         // This is a modifier proposed in the initial paper of Gooding.
  336.         // Try to avoid Lambert-fail, by common-perpendicular starters
  337.         final Vector3D R13 = vObserverPosition3.subtract(vObserverPosition1);
  338.         D1 = R13.dotProduct(lineOfSight1);
  339.         D3 = R13.dotProduct(lineOfSight3);
  340.         final double D2 = lineOfSight1.dotProduct(lineOfSight3);
  341.         final double D4 = 1. - D2 * D2;
  342.         rho1 = FastMath.max((D1 - D3 * D2) / D4, 0.);
  343.         rho3 = FastMath.max((D1 * D2 - D3) / D4, 0.);
  344.     }

  345.     /** Compute the derivatives by finite-differences for the range problem.
  346.      * Specifically, we are trying to solve the problem:
  347.      *      f(x, y) = 0
  348.      *      g(x, y) = 0
  349.      * So, in a Newton-Raphson process, we would need the derivatives:
  350.      *  fx, fy, gx, gy
  351.      * Enventually,
  352.      *    dx =-f*gy / D
  353.      *    dy = f*gx / D
  354.      * where D is the determinant of the Jacobian matrix.
  355.      *
  356.      * @param frame frame to be used (orbit frame)
  357.      * @param x    current range 1
  358.      * @param y    current range 3
  359.      * @param R10   current radius 1
  360.      * @param R30   current radius 3
  361.      * @param lineOfSight1  line of sight
  362.      * @param lineOfSight3  line of sight
  363.      * @param Pin   basis vector
  364.      * @param Ein   basis vector
  365.      * @param F     value of the f-function
  366.      * @param T13   time of flight 1->3, in seconds
  367.      * @param T12   time of flight 1->2, in seconds
  368.      * @param withHalley    use Halley iterative method
  369.      * @param nrev  number of revolutions
  370.      * @param direction direction of motion
  371.      * @param FD    derivatives of f wrt (rho1, rho3) by finite differences
  372.      * @param GD    derivatives of g wrt (rho1, rho3) by finite differences
  373.      */
  374.     private void computeDerivatives(final Frame frame,
  375.                                     final double x, final double y,
  376.                                     final double R10, final double R30,
  377.                                     final Vector3D lineOfSight1, final Vector3D lineOfSight3,
  378.                                     final Vector3D Pin,
  379.                                     final Vector3D Ein,
  380.                                     final double F,
  381.                                     final double T13, final double T12,
  382.                                     final boolean withHalley,
  383.                                     final int nrev,
  384.                                     final boolean direction,
  385.                                     final double[] FD, final double[] GD) {

  386.         final Vector3D P = Pin.normalize();
  387.         final Vector3D EN = Ein.normalize();

  388.         // Now get partials, by finite differences
  389.         // steps for the differentiation
  390.         final double dx = facFiniteDiff * x;
  391.         final double dy = facFiniteDiff * y;

  392.         final Vector3D Cm1 = getPositionOnLoS2 (frame,
  393.                                                 lineOfSight1, x - dx,
  394.                                                 lineOfSight3, y,
  395.                                                 T13, T12, nrev, direction).subtract(vObserverPosition2);

  396.         final double Fm1 = P.dotProduct(Cm1);
  397.         final double Gm1 = EN.dotProduct(Cm1);

  398.         final Vector3D Cp1 = getPositionOnLoS2 (frame,
  399.                                                 lineOfSight1, x + dx,
  400.                                                 lineOfSight3, y,
  401.                                                 T13, T12, nrev, direction).subtract(vObserverPosition2);

  402.         final double Fp1  = P.dotProduct(Cp1);
  403.         final double Gp1 = EN.dotProduct(Cp1);

  404.         // derivatives df/drho1 and dg/drho1
  405.         final double Fx = (Fp1 - Fm1) / (2. * dx);
  406.         final double Gx = (Gp1 - Gm1) / (2. * dx);

  407.         final Vector3D Cm3 = getPositionOnLoS2 (frame,
  408.                                                 lineOfSight1, x,
  409.                                                 lineOfSight3, y - dy,
  410.                                                 T13, T12, nrev, direction).subtract(vObserverPosition2);

  411.         final double Fm3 = P.dotProduct(Cm3);
  412.         final double Gm3 = EN.dotProduct(Cm3);

  413.         final Vector3D Cp3 = getPositionOnLoS2 (frame,
  414.                                                 lineOfSight1, x,
  415.                                                 lineOfSight3, y + dy,
  416.                                                 T13, T12, nrev, direction).subtract(vObserverPosition2);

  417.         final double Fp3 = P.dotProduct(Cp3);
  418.         final double Gp3 = EN.dotProduct(Cp3);

  419.         // derivatives df/drho3 and dg/drho3
  420.         final double Fy = (Fp3 - Fm3) / (2. * dy);
  421.         final double Gy = (Gp3 - Gm3) / (2. * dy);
  422.         final double detJac = Fx * Gy - Fy * Gx;

  423.         // Coefficients for the classical Newton-Raphson iterative method
  424.         FD[0] = Fx;
  425.         FD[1] = Fy;
  426.         GD[0] = Gx;
  427.         GD[1] = Gy;

  428.         // Modified Newton-Raphson process, with Halley's method to have cubic convergence.
  429.         // This requires computing second order derivatives.
  430.         if (withHalley) {
  431.             //
  432.             final double hrho1Sq = dx * dx;
  433.             final double hrho3Sq = dy * dy;

  434.             // Second order derivatives: d^2f / drho1^2 and d^2g / drho3^2
  435.             final double Fxx = (Fp1 + Fm1 - 2 * F) / hrho1Sq;
  436.             final double Gxx = (Gp1 + Gm1 - 2 * F) / hrho1Sq;
  437.             final double Fyy = (Fp3 + Fp3 - 2 * F) / hrho3Sq;
  438.             final double Gyy = (Gm3 + Gm3 - 2 * F) / hrho3Sq;

  439.             final Vector3D Cp13 = getPositionOnLoS2 (frame,
  440.                                                      lineOfSight1, x + dx,
  441.                                                      lineOfSight3, y + dy,
  442.                                                      T13, T12, nrev, direction).subtract(vObserverPosition2);

  443.             // f function value at (x1+dx1, x3+dx3)
  444.             final double Fp13 = P.dotProduct(Cp13);
  445.             // g function value at (x1+dx1, x3+dx3)
  446.             final double Gp13 = EN.dotProduct(Cp13);

  447.             final Vector3D Cm13 = getPositionOnLoS2 (frame,
  448.                                                      lineOfSight1, x - dx,
  449.                                                      lineOfSight3, y - dy,
  450.                                                      T13, T12, nrev, direction).subtract(vObserverPosition2);

  451.             // f function value at (x1-dx1, x3-dx3)
  452.             final double Fm13 = P.dotProduct(Cm13);
  453.             // g function value at (x1-dx1, x3-dx3)
  454.             final double Gm13 = EN.dotProduct(Cm13);

  455.             // Second order derivatives: d^2f / drho1drho3 and d^2g / drho1drho3
  456.             //double Fxy = Fp13 / (dx * dy) - (Fx / dy + Fy / dx) -
  457.             //                0.5 * (Fxx * dx / dy + Fyy * dy / dx);
  458.             //double Gxy = Gp13 / (dx * dy) - (Gx / dy + Gy / dx) -
  459.             //                0.5 * (Gxx * dx / dy + Gyy * dy / dx);
  460.             final double Fxy = (Fp13 + Fm13) / (2 * dx * dy) - 0.5 * (Fxx * dx / dy + Fyy * dy / dx) - F / (dx * dy);
  461.             final double Gxy = (Gp13 + Gm13) / (2 * dx * dy) - 0.5 * (Gxx * dx / dy + Gyy * dy / dx) - F / (dx * dy);

  462.             // delta Newton Raphson, 1st order step
  463.             final double dx3NR = -Gy * F / detJac;
  464.             final double dx1NR = Gx * F / detJac;

  465.             // terms of the Jacobian, considering the development, after linearization
  466.             // of the second order Taylor expansion around the Newton Raphson iterate:
  467.             // (fx + 1/2 * fxx * dx* + 1/2 * fxy * dy*) * dx
  468.             //      + (fy + 1/2 * fyy * dy* + 1/2 * fxy * dx*) * dy
  469.             // where: dx* and dy* would be the step of the Newton raphson process.
  470.             final double FxH = Fx + 0.5 * (Fxx * dx3NR + Fxy * dx1NR);
  471.             final double FyH = Fy + 0.5 * (Fxy * dx3NR + Fxx * dx1NR);
  472.             final double GxH = Gx + 0.5 * (Gxx * dx3NR + Gxy * dx1NR);
  473.             final double GyH = Gy + 0.5 * (Gxy * dx3NR + Gyy * dx1NR);

  474.             // New Halley's method "Jacobian"
  475.             FD[0] = FxH;
  476.             FD[1] = FyH;
  477.             GD[0] = GxH;
  478.             GD[1] = GyH;
  479.         }
  480.     }

  481.     /** Calculate the position along sight-line.
  482.      * @param frame frame to be used (orbit frame)
  483.      * @param E1 line of sight 1
  484.      * @param RO1 distance along E1
  485.      * @param E3 line of sight 3
  486.      * @param RO3 distance along E3
  487.      * @param T12   time of flight
  488.      * @param T13   time of flight
  489.      * @param nRev number of revolutions
  490.      * @param posigrade direction of motion
  491.      * @return (R2-O2)
  492.      */
  493.     private Vector3D getPositionOnLoS2(final Frame frame,
  494.                                        final Vector3D E1, final double RO1,
  495.                                        final Vector3D E3, final double RO3,
  496.                                        final double T13, final double T12,
  497.                                        final int nRev, final boolean posigrade) {
  498.         final Vector3D P1 = vObserverPosition1.add(E1.scalarMultiply(RO1));
  499.         R1 = P1.getNorm();

  500.         final Vector3D P3 = vObserverPosition3.add(E3.scalarMultiply(RO3));
  501.         R3 = P3.getNorm();

  502.         final Vector3D P13 = P1.crossProduct(P3);

  503.         // sweep angle
  504.         // (Fails only if either R1 or R2 is zero)
  505.         double TH = FastMath.atan2(P13.getNorm(), P1.dotProduct(P3));

  506.         // compute the number of revolutions
  507.         if (!posigrade) {
  508.             TH = 2 * FastMath.PI - TH;
  509.         }

  510.         // Solve the Lambert's problem to get the velocities at endpoints
  511.         final double[] V1 = new double[2];
  512.         // work with non-dimensional units (MU=1)
  513.         final boolean exitflag = lambert.solveLambertPb(R1, R3, TH, T13, nRev, V1);

  514.         if (exitflag) {
  515.             // basis vectors
  516.             final Vector3D Pn = P1.crossProduct(P3);
  517.             final Vector3D Pt = Pn.crossProduct(P1);

  518.             // tangential velocity norm
  519.             double RT = Pt.getNorm();
  520.             if (!posigrade) {
  521.                 RT = -RT;
  522.             }

  523.             // velocity vector at P1
  524.             final Vector3D Vel1 = new  Vector3D(V1[0] / R1, P1, V1[1] / RT, Pt);

  525.             // estimate the position at the second observation time
  526.             // propagate (P1, V1) during TAU + T12 to get (P2, V2)
  527.             final Vector3D P2 = propagatePV(frame, P1, Vel1, T12);

  528.             return P2;
  529.         }

  530.         return null;
  531.     }

  532.     /** Propagate a solution (Kepler).
  533.      * @param frame frame to be used (orbit frame)
  534.      * @param P1    initial position vector
  535.      * @param V1    initial velocity vector
  536.      * @param tau   propagation time
  537.      * @return final position vector
  538.      */
  539.     private Vector3D propagatePV(final Frame frame, final Vector3D P1, final Vector3D V1, final double tau) {
  540.         final PVCoordinates pv1 = new PVCoordinates(P1, V1);
  541.         // create a Keplerian orbit. Assume MU = 1.
  542.         final KeplerianOrbit orbit = new KeplerianOrbit(pv1, frame, date1, 1.);
  543.         return orbit.shiftedBy(tau).getPVCoordinates().getPosition();
  544.     }

  545.     /**
  546.      * Calculates the line of sight vector.
  547.      * @param alpha right ascension angle, in radians
  548.      * @param delta declination angle, in radians
  549.      * @return the line of sight vector
  550.      * @since 11.0
  551.      */
  552.     public static Vector3D lineOfSight(final double alpha, final double delta) {
  553.         return new Vector3D(FastMath.cos(delta) * FastMath.cos(alpha),
  554.                             FastMath.cos(delta) * FastMath.sin(alpha),
  555.                             FastMath.sin(delta));
  556.     }

  557.     /**
  558.      * Calculate the line of sight vector from an AngularRaDec measurement.
  559.      * @param raDec measurement
  560.      * @return the line of sight vector
  561.      * @since 11.0
  562.      */
  563.     public static Vector3D lineOfSight(final AngularRaDec raDec) {

  564.         // Observed values
  565.         final double[] observed = raDec.getObservedValue();

  566.         // Return
  567.         return lineOfSight(observed[0], observed[1]);

  568.     }

  569.     /**
  570.      * Get the station position from the right ascension / declination measurement.
  571.      * @param frame inertial frame for station posiiton and orbit estimate
  572.      * @param raDec measurement
  573.      * @return the station position
  574.      */
  575.     private static Vector3D stationPosition(final Frame frame, final AngularRaDec raDec) {
  576.         return raDec.getStation().getBaseFrame().getPVCoordinates(raDec.getDate(), frame).getPosition();
  577.     }

  578. }