function [hdisp,vdisp] = ApproxDisparity(eyes,disparitycoordsystem,objectlocationcoords,coord1,coord2,coord3)
% Returns approximate expressions for an objects horizontal and vertical
% disparity in the coordinate system specified by 'disparitycoordsystem'.
% Possible values are 'planar', 'azimuth-longitude/elevation-longitude', 'azimuth-longitude/elevation-latitude',
% 'azimuth-latitude/elevation-longitude' and 'azimuth-latitude/elevation-latitude'.
% The variable 'objectlocationcoords' explains how the object location ./
% visual direction is specified. Possible values are 
% 1) 'space, gaze', in which case position is specified by the object's spatial location in gaze-centered coordinates, U,W,S.
% 2) 'retina, S', in which case position is specified by the object's retinal location in the coordinates specified by 'disparitycoordsystem', plus its distance along the cylocpean gaze direction, S.

global L R CYC DELTA HALFDELTA ALL

Tccos = cos(eyes(CYC).T);
Tcsin = sin(eyes(CYC).T);
Hc = eyes(CYC).H;
Hccos = cos(eyes(CYC).H);
Hcsin = sin(eyes(CYC).H);
Vccos = cos(eyes(CYC).V);
Vcsin = sin(eyes(CYC).V);
HD = eyes(DELTA).H;
VD = eyes(DELTA).V;
TD = eyes(DELTA).T;
I = eyes(ALL).I;

if strcmp(disparitycoordsystem,'planar')
    if strcmp(objectlocationcoords,'space, gaze')
        U = coord1;        W = coord2;        S = coord3;
        hdisp = (U./S.*Hcsin-Hccos.*Tccos).*I./S + ((U.^2./S.^2+1).*Tccos-U.*W./S.^2.*Tcsin).*HD - (W./S.*Hcsin+U.*W./S.^2.*Hccos.*Tccos+(U.^2./S.^2+1).*Hccos.*Tcsin).*VD - W./S.*TD;
        vdisp = (W./S.*Hcsin+Tcsin.*Hccos).*I./S + (-(W.^2./S.^2+1).*Tcsin+U.*W./S.^2.*Tccos).*HD + (U./S.*Hcsin-U.*W./S.^2.*Hccos.*Tcsin-(W.^2./S.^2+1).*Hccos.*Tccos).*VD + U./S.*TD;;
    elseif strcmp(objectlocationcoords,'retina, S')
        xc = coord1;        yc = coord2;        S = coord3;
        hdisp = -(xc.*Hcsin+Hccos.*Tccos).*I./S + ((xc.^2+1).*Tccos-xc.*yc.*Tcsin).*HD + (yc.*Hcsin-xc.*yc.*Hccos.*Tccos-(xc.^2+1).*Hccos.*Tcsin).*VD + yc.*TD;
        vdisp = -(yc.*Hcsin-Tcsin.*Hccos).*I./S + (xc.*yc.*Tccos-(yc.^2+1).*Tcsin).*HD - (xc.*Hcsin+(yc.^2+1).*Hccos.*Tccos+xc.*yc.*Hccos.*Tcsin).*VD - xc.*TD;
    end
end

if strcmp(disparitycoordsystem,'azimuth-longitude/elevation-longitude')
    if strcmp(objectlocationcoords,'space, gaze')
        U = coord1;        W = coord2;        S = coord3;
        hdisp = 1./(S.^2+U.^2).* ( I.*(U.*Hcsin-S.*Hccos.*Tccos) + ( (S.^2+U.^2).*Tccos - W.*U.*Tcsin).*HD - ((S.^2+U.^2).* Tcsin.*Hccos  + U.*W.*Tccos.*Hccos + W.*S.*Hcsin ).*VD  - W.*S.*TD);
        vdisp = 1./(S.^2+W.^2).* ( (W.*Hcsin+S.*Tcsin.*Hccos).*I + (U.*W.*Tccos-(W.^2+S.^2).*Tcsin).*HD - (-U.*S.*Hcsin+U.*W.*Hccos.*Tcsin+(W.^2+S.^2).*Hccos.*Tccos).*VD + U.*S.*TD );
    elseif strcmp(objectlocationcoords,'retina, S')
        ac = coord1;        ec = coord2;        S = coord3;
        hdisp =  -I./S.*cos(ac).*(cos(ac).*Hccos.*Tccos+sin(ac).*Hcsin) + ( Tccos - 0.5.*sin(2.*ac).*tan(ec).*Tcsin).*HD - (Tcsin.*Hccos  + 0.5.*sin(2.*ac).*tan(ec).*Tccos.*Hccos - cos(ac).^2.*tan(ec).*Hcsin ).*VD  + cos(ac).^2.*tan(ec).*TD;
        vdisp =  cos(ec).^2.*(Tcsin.*Hccos-tan(ec).*Hcsin).*I./S + (cos(ec).*tan(ac).*sin(ec).*Tccos-Tcsin).*HD - (cos(ec).^2.*tan(ac).*Hcsin+cos(ec).*tan(ac).*sin(ec).*Hccos.*Tcsin+Hccos.*Tccos).*VD -cos(ec).^2.*tan(ac).*TD ;
    end
end
if strcmp(disparitycoordsystem,'azimuth-longitude/elevation-latitude')
    if strcmp(objectlocationcoords,'space, gaze')
        U = coord1;        W = coord2;        S = coord3;
        hdisp = 1./(S.^2+U.^2).* ( I.*(U.*Hcsin-S.*Hccos.*Tccos) + ( (S.^2+U.^2).*Tccos - W.*U.*Tcsin).*HD - ((S.^2+U.^2).* Tcsin.*Hccos  + U.*W.*Tccos.*Hccos + W.*S.*Hcsin ).*VD  - W.*S.*TD);
        vdisp = I.*(Hccos.*Tccos.*U.*W+Hcsin.*W.*S+Tcsin.*Hccos.*(U.^2+S.^2))./(U.^2+W.^2+S.^2)./sqrt(U.^2+S.^2) - (S.*Tcsin.*HD+S.*Hccos.*Tccos.*VD-U.*Hcsin.*VD-U.*TD)./sqrt(U.^2+S.^2);
    elseif strcmp(objectlocationcoords,'retina, S')
        ac = coord1;        kc = coord2;        S = coord3;
        hdisp =  -I./S.*cos(ac).*(cos(ac).*Hccos.*Tccos+sin(ac).*Hcsin) + ( Tccos - sin(ac).*tan(kc).*Tcsin).*HD - (Tcsin.*Hccos  + sin(ac).*tan(kc).*Tccos.*Hccos - cos(ac).*tan(kc).*Hcsin ).*VD  + cos(ac).*tan(kc).*TD;
        vdisp = I./S.*cos(ac).*cos(kc).*(Tccos.*Hccos.*sin(ac).*sin(kc) - Hcsin.*cos(ac).*sin(kc) + Tcsin.*Hccos.*cos(kc)) - Tcsin.*HD.*cos(ac) - (Hccos.*Tccos.*cos(ac)+sin(ac).*Hcsin).*VD - sin(ac).*TD;
    end
end
if strcmp(disparitycoordsystem,'azimuth-latitude/elevation-longitude')
    if strcmp(objectlocationcoords,'space, gaze')
        U = coord1;        W = coord2;        S = coord3;
        hdisp = -I.*(Tcsin.*Hccos.*U.*W-Hcsin.*U.*S+Hccos.*Tccos.*(W.^2+S.^2))./(U.^2+W.^2+S.^2)./sqrt(W.^2+S.^2) - (-S.*Tccos.*HD+S.*Hccos.*Tcsin.*VD+W.*Hcsin.*VD+W.*TD)./sqrt(W.^2+S.^2);
        vdisp = 1./(S.^2+W.^2).* ( (W.*Hcsin+S.*Tcsin.*Hccos).*I + (U.*W.*Tccos-(W.^2+S.^2).*Tcsin).*HD - (-U.*S.*Hcsin+U.*W.*Hccos.*Tcsin+(W.^2+S.^2).*Hccos.*Tccos).*VD + U.*S.*TD );
    elseif strcmp(objectlocationcoords,'retina, S')
        bc = coord1;        ec = coord2;        S = coord3;
        U = -S.*tan(bc)./cos(ec); W = -S.*tan(ec); %sqrt(W.^2+S.^2) = S./cos(ec)
        hdisp = -I./S.*cos(ec).*cos(bc).*(sin(ec).*sin(bc).*Tcsin.*Hccos + sin(bc).*cos(ec).*Hcsin + cos(bc).*Hccos.*Tccos) + cos(ec).*Tccos.*HD - cos(ec).*Hccos.*Tcsin.*VD + sin(ec).*Hcsin.*VD + sin(ec).*TD;
        vdisp =  cos(ec).^2.*(Tcsin.*Hccos-tan(ec).*Hcsin).*I./S + (tan(bc).*sin(ec).*Tccos-Tcsin).*HD - (cos(ec).*tan(bc).*Hcsin+tan(bc).*sin(ec).*Hccos.*Tcsin+Hccos.*Tccos).*VD -cos(ec).*tan(bc).*TD ;
    end
end
if strcmp(disparitycoordsystem,'azimuth-latitude/elevation-latitude')
    if strcmp(objectlocationcoords,'space, gaze')
        U = coord1;        W = coord2;        S = coord3;
        hdisp = -I.*(Tcsin.*Hccos.*U.*W-Hcsin.*U.*S+Tccos.*Hccos.*(W.^2+S.^2))./(U.^2+W.^2+S.^2)./sqrt(W.^2+S.^2) - (-S.*Tccos.*HD+S.*Hccos.*Tcsin.*VD+W.*Hcsin.*VD+W.*TD)./sqrt(W.^2+S.^2);
        vdisp = I.*(Hccos.*Tccos.*U.*W+Hcsin.*W.*S+Tcsin.*Hccos.*(U.^2+S.^2))./(U.^2+W.^2+S.^2)./sqrt(U.^2+S.^2) - (S.*Tcsin.*HD+S.*Hccos.*Tccos.*VD-U.*Hcsin.*VD-U.*TD)./sqrt(U.^2+S.^2);
    elseif strcmp(objectlocationcoords,'retina, S')
        bc = coord1;        kc = coord2;        S = coord3;
        'haven''t done these'
    end
end






