{ RCSid $Id: ambpos.cal,v 2.4 2014/09/03 14:05:55 greg Exp $ }
{
	Compute patterns for ambient position markers
}
odd(n) : .5*n - floor(.5*n) - .25;
posangle(a) : if(-a, a + 2*PI, a) / DEGREE;
{ Relative hit point }
rpx = Px - arg(1);
rpy = Py - arg(2);
rpz = Pz - arg(3);

{ Calculation of directional gradient pattern }
ndx = arg(4); ndy = arg(5); ndz = arg(6);
dgx = arg(7); dgy = arg(8); dgz = arg(9);
rdx = ndy*rpz - ndz*rpy;
rdy = ndz*rpx - ndx*rpz;
rdz = ndx*rpy - ndy*rpx;
dgrad = (rdx*dgx + rdy*dgy + rdz*dgz)/sqrt(rpx*rpx + rpy*rpy + rpz*rpz);
dirfunc = bound(0, 1 + dgrad, 2);

{ Calculation of positional gradient pattern }
pgx = arg(4); pgy = arg(5); pgz = arg(6);
posfunc = bound(0, 1 + (pgx*rpx + pgy*rpy + pgz*rpz), 2);
				{ corral flags and associated pattern }
ux = arg(4); uy = arg(5); uz = arg(6);
cflags = arg(7);
vx = Ny*uz - Nz*uy;
vy = Nz*ux - Nx*uz;
vz = Nx*uy - Ny*ux;
cflg(i) = if(odd(floor( cflags / 2^i )), 1, -1);
corralled = cflg(floor(32/360*posangle(
		atan2(rpx*vx+rpy*vy+rpz*vz, rpx*ux+rpy*uy+rpz*uz) )));

{ Calculation of ellipse stencil for base }
usx = arg(4); usy = arg(5); usz = arg(6);
vsx = arg(7); vsy = arg(8); vsz = arg(9);
ellipstencil = if(1 - sq(usx*rpx + usy*rpy + usz*rpz) - sq(vsx*rpx + vsy*rpy + vsz*rpz),
			1, 0);
