/* InDirectCalc v1.8.1 Ver.:OK (c) 01/01 - Ch Iossif */
/*
   Name:         INDIRectCALCulator
   Version:      1.8.1
   Author:       Ch Iossif <chiossif@yahoo.com>
   Date:         26/06/09 10:12
   Modified:     01/01/01 12:00
   Description:  Indirect Georeference Calculations on SAR images
   Usage:        $InDirectCalc
   Uses:         THESE additional inline parameters
   Compile line: $gcc -o InDirectCalc InDirectCalc.c -lm

   Copyright (C) January 2001 Ch Iossif <chiossif@yahoo.com>

   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
#include <stdio.h>
#include <math.h>

#define ACCURACY 1e-4
#define sqr(x) ((x)*(x))

int main(void) {
    double c, f0, R, pR, t, pt, fd, pfd, a, fa, b;
    double xp, yp, zp, dxp, dyp, dzp, t0, tt, dt, x, y, z, dx, dy, dz;
    double ax3, ax2, ax1, ax0, ay3, ay2, ay1, ay0, az3, az2, az1, az0;

    c=299792458.0L;
    f0=5300000000.0L;   /* THIS additional inline parameter */

    while (scanf("%lf %lf %lf",&xp,&yp,&zp)==3) {
        dxp=0.0L;
        dyp=0.0L;
        dzp=0.0L;

        t0=75034.113L;      /* THIS additional inline parameter */
        tt=75049.910L;      /* THIS additional inline parameter */
        dt=(tt-t0)/26538;   /* THIS additional inline parameter */

        ax3=6.40920893811625E-04;   /* THIS additional inline parameter */
        ax2=-1.47433972859755E+02;  /* THIS additional inline parameter */
        ax1=1.12961565545807E+07;   /* THIS additional inline parameter */
        ax0=-2.88278590824000E+11;  /* THIS additional inline parameter */

        ay3=7.20679197560248E-04;   /* THIS additional inline parameter */
        ay2=-1.62935738059576E+02;  /* THIS additional inline parameter */
        ay1=1.22758499271622E+07;   /* THIS additional inline parameter */
        ay0=-3.08209847907000E+11;  /* THIS additional inline parameter */

        az3=-9.86904082215467E-04;  /* THIS additional inline parameter */
        az2=2.19815028768032E+02;   /* THIS additional inline parameter */
        az1=-1.63122323725281E+07;  /* THIS additional inline parameter */
        az0=4.03312145034500E+11;   /* THIS additional inline parameter */

        fd=-1.0;
        t=t0;
        do {
            pfd=fd;
            pt=t;
            pR=R;
            t+=dt;
            x=ax3*t*t*t+ax2*t*t+ax1*t+ax0;
            y=ay3*t*t*t+ay2*t*t+ay1*t+ay0;
            z=az3*t*t*t+az2*t*t+az1*t+az0;
            dx=3.0L*ax3*t*t+2.0L*ax2*t+ax1;
            dy=3.0L*ay3*t*t+2.0L*ay2*t+ay1;
            dz=3.0L*az3*t*t+2.0L*az2*t+az1;
            R=sqrt(sqr(x-xp)+sqr(y-yp)+sqr(z-zp));
            fd=2.0L*f0/c/R*(dx*(x-xp)+dy*(y-yp)+dz*(z-zp));
        } while (pfd*fd>0);

        a=pt;
        fa=pfd;
        b=t;
        do {
            t=(a+b)/2;
            x=ax3*t*t*t+ax2*t*t+ax1*t+ax0;
            y=ay3*t*t*t+ay2*t*t+ay1*t+ay0;
            z=az3*t*t*t+az2*t*t+az1*t+az0;
            dx=3.0L*ax3*t*t+2.0L*ax2*t+ax1;
            dy=3.0L*ay3*t*t+2.0L*ay2*t+ay1;
            dz=3.0L*az3*t*t+2.0L*az2*t+az1;

            R=sqrt(sqr(x-xp)+sqr(y-yp)+sqr(z-zp));
            fd=2.0L*f0/c/R*(dx*(x-xp)+dy*(y-yp)+dz*(z-zp));
            if (fd*fa>0) {
                fa=fd;
                a=t;
            } else
                b=t;
        } while (fabs(fd)>ACCURACY);
        printf("%12.3lf %12.3lf %12.3lf  --- > %14.8lf - %14.8lf - %+20.14le\n",xp,yp,zp,t,R,fd);
    }

    printf("\n\nInDirectCalc by Christos Iossifides (c) 2001 NaTUReS Lab.\n");
    return 0;
}

