<?php
class Sourcemap_Proj_Transform_Ortho extends Sourcemap_Proj_Transform {
/*******************************************************************************
NAME                             ORTHOGRAPHIC 

PURPOSE:	Transforms input longitude and latitude to Easting and
		Northing for the Orthographic projection.  The
		longitude and latitude must be in radians.  The Easting
		and Northing values will be returned in meters.

PROGRAMMER              DATE
----------              ----
T. Mittan		Mar, 1993

ALGORITHM REFERENCES

1.  Snyder, John P., "Map Projections--A Working Manual", U.S. Geological
    Survey Professional Paper 1395 (Supersedes USGS Bulletin 1532), United
    State Government Printing Office, Washington D.C., 1987.

2.  Snyder, John P. and Voxland, Philip M., "An Album of Map Projections",
    U.S. Geological Survey Professional Paper 1453 , United State Government
    Printing Office, Washington D.C., 1989.
*******************************************************************************/

  /* Initialize the Orthographic projection
    -------------------------------------*/
    public function init() {
        //double temp;			/* temporary variable		*/

        /* Place parameters in static storage for common use
           -------------------------------------------------*/;
        $this->sin_p14 = sin($this->lat0);
        $this->cos_p14 = cos($this->lat0);	
    }


    # Orthographic forward equations--mapping lat,long to x,y
    public function forward($p) {
#        $sinphi, $cosphi;	/* sin and cos value				*/
#        $dlon;		/* delta longitude value			*/
#        $coslon;		/* cos of longitude				*/
#        $ksp;		/* scale factor					*/
#        $g;		
        $lon = $p->x;
        $lat = $p->y;	
        /* Forward equations
           -----------------*/
        $dlon = Sourcemap_Proj::adjust_lon($lon - $this->long0);

        $sinphi = sin($lat);
        $cosphi = cos($lat);	

        $coslon = cos($dlon);
        $g = $this->sin_p14 * $sinphi + $this->cos_p14 * $cosphi * $coslon;
        $ksp = 1.0;
        if(($g > 0) || (abs($g) <= Sourcemap_Proj::EPSLN)) {
            $x = $this->a * $ksp * $cosphi * sin($dlon);
            $y = $this->y0 + $this->a * $ksp * ($this->cos_p14 * $sinphi - $this->sin_p14 * $cosphi * $coslon);
        } else {
            throw new Exception("Point error.");
        }
        $p->x = $x;
        $p->y = $y;
        return $p;
    }

    public function inverse($p) {
#        $rh;		/* height above ellipsoid			*/
#        $z;		/* angle					*/
#        $sinz, $cosz;	/* sin of z and cos of z			*/
#        $temp;
#        $con;
#        $lon, $lat;
        /* Inverse equations
           -----------------*/
        $p->x -= $this->x0;
        $p->y -= $this->y0;
        $rh = sqrt($p->x * $p->x + $p->y * $p->y);
        if ($rh > $this->a + .0000001) {
            throw new Exception("orthoInvDataError");
        }
        $z = Sourcemap_Proj::asinz($rh / $this->a);

        $sinz = sin($z);
        $cosz = cos($z);

        $lon = $this->long0;
        if(abs($rh) <= Sourcemap_Proj::EPSLN) {
            $lat = $this->lat0; 
        }
        $lat = Sourcemap_Proj::asinz($cosz * $this->sin_p14 + ($p->y * $sinz * $this->cos_p14) / $rh);
        $con = abs($lat0) - Sourcemap_Proj::HALF_PI;
        if(abs($con) <= Sourcemap_Proj::EPSLN) {
            if($this->lat0 >= 0) {
                $lon = Sourcemap_Proj::adjust_lon($this->long0 + atan2($p->x, -$p->y));
            } else {
                $lon = Sourcemap_Proj::adjust_lon($this->long0 - atan2(-$p->x, $p->y));
            }
        }
        $con = $cosz - $this->sin_p14 * sin($lat);
        if((abs($con) >= Sourcemap_Proj::EPSLN) || (abs($x) >= Sourcemap_Proj::EPSLN)) {
            $lon = Sourcemap_Proj::adjust_lon($this->long0 + atan2(($p->x * $sinz * $this->cos_p14), ($con * $rh)));
        }
        $p->x = $lon;
        $p->y = $lat;
        return $p;
    }
}
