{ RCSid $Id: rayinit.cal,v 2.17 2018/01/18 19:43:43 greg Exp $ }
{
	Initialization file for Radiance.

	The following are predefined:

	Dx, Dy, Dz			- ray direction
	Nx, Ny, Nz			- surface normal
	Px, Py, Pz			- intersection point
	T				- distance from start
	Ts				- single ray (shadow) distance
	Rdot				- ray dot product
	S				- world scale
	Tx, Ty, Tz			- world origin
	Ix, Iy, Iz			- world i unit vector
	Jx, Jy, Jz			- world j unit vector
	Kx, Ky, Kz			- world k unit vector
	arg(n)				- real arguments, arg(0) is count

	For mesh objects, the following are available:

	Lu, Lv				- local (u,v) coordinates

	For *func & *data materials, the following are also available:

 	NxP, NyP, NzP			- perturbed surface normal
 	RdotP				- perturbed ray dot product
 	CrP, CgP, CbP			- perturbed material color

	For prism1 and prism2 types, the following are available:

	DxA, DyA, DzA			- direction to target light source

	Library functions:

	if(a, b, c)			- if a positive, return b, else c

	select(N, a1, a2, ..)		- return aN

	sqrt(x)				- square root function

	sin(x), cos(x), tan(x),
	asin(x), acos(x),
	atan(x), atan2(y,x)		- standard trig functions (radians)

	floor(x), ceil(x)		- g.l.b. & l.u.b.

	exp(x), log(x), log10(x)	- exponent and log functions

	erf(z), erfc(z)			- error functions

	rand(x)				- pseudo-random function (0 to 1)

	noise3(x,y,z), noise3x(x,y,z),
	noise3y(x,y,z), noise3z(x,y,z)	- noise function with gradient (-1 to 1)

	fnoise3(x,y,z)			- fractal noise function (-1 to 1)
}

			{ Backward compatibility }
AC = arg(0);
A1 = arg(1); A2 = arg(2); A3 = arg(3); A4 = arg(4); A5 = arg(5);
A6 = arg(6); A7 = arg(7); A8 = arg(8); A9 = arg(9); A10 = arg(10);

noise3a(x,y,z) : noise3x(x,y,z);
noise3b(x,y,z) : noise3y(x,y,z);
noise3c(x,y,z) : noise3z(x,y,z);

			{ Forward compatibility (?) }
D(i) = select(i, Dx, Dy, Dz);
N(i) = select(i, Nx, Ny, Nz);
P(i) = select(i, Px, Py, Pz);
noise3d(i,x,y,z) : select(i, noise3x(x,y,z), noise3y(x,y,z), noise3z(x,y,z));

			{ More robust versions of library functions }
bound(a,x,b) : if(a-x, a, if(x-b, b, x));
Acos(x) : acos(bound(-1,x,1));
Asin(x) : asin(bound(-1,x,1));
Atan2(y,x) : if(x*x+y*y, atan2(y,x), 0);
Exp(x) : if(-x-100, 0, exp(x));
Sqrt(x) : if(x, sqrt(x), 0);

			{ Useful constants }
PI : 3.14159265358979323846;
DEGREE : PI/180;
FTINY : 1e-7;

			{ Useful functions }
and(a,b) : if( a, b, a );
or(a,b) : if( a, a, b );
not(a) : if( a, -1, 1 );
xor(a,b) : if( a, not(b), b );
abs(x) : if( x, x, -x );
sgn(x) : if( x, 1, if(-x, -1, 0) );
sq(x) : x*x;
max(a,b) : if( a-b, a, b );
min(a,b) : if( a-b, b, a );
inside(a,x,b) : and(x-a,b-x);
frac(x) : x - floor(x);
mod(n,d) : n - floor(n/d)*d;
tri(n,d) : abs( d - mod(n-d,2*d) );
linterp(t,p0,p1) : (1-t)*p0 + t*p1;

noop(v) : v;
clip(v) : bound(0,v,1);
noneg(v) : if(v,v,0);
red(r,g,b) : if(r,r,0);
green(r,g,b) : if(g,g,0);
blue(r,g,b) : if(b,b,0);
grey(r,g,b) : noneg(.265074126*r + .670114631*g + .064811243*b);
clip_r(r,g,b) : bound(0,r,1);
clip_g(r,g,b) : bound(0,g,1);
clip_b(r,g,b) : bound(0,b,1);
clipgrey(r,g,b) : min(grey(r,g,b),1);

dot(v1,v2) : v1(1)*v2(1) + v1(2)*v2(2) + v1(3)*v2(3);
cross(i,v1,v2) : select(i,	v1(2)*v2(3) - v1(3)*v2(2),
				v1(3)*v2(1) - v1(1)*v2(3),
				v1(1)*v2(2) - v1(2)*v2(1));

fade(near_val,far_val,dist) : far_val +
		if (16-dist, (near_val-far_val)/(1+dist*dist), 0);

hermite(p0,p1,r0,r1,t) :	p0 * ((2*t-3)*t*t+1) +
				p1 * (-2*t+3)*t*t +
				r0 * (((t-2)*t+1)*t) +
				r1 * ((t-1)*t*t);

bezier(p1, p2, p3, p4, t) : 	p1 * (1+t*(-3+t*(3-t))) +
				p2 * 3*t*(1+t*(-2+t)) +
				p3 * 3*t*t*(1-t) +
				p4 * t*t*t ;

bspline(pp, p0, p1, pn, t) :	pp * (1/6+t*(-.5+t*(.5-1/6*t))) +
				p0 * (2/3+t*t*(-1+.5*t)) +
				p1 * (1/6+t*(.5+t*(.5-.5*t))) +
				pn * (1/6*t*t*t) ;

turbulence(x,y,z,s) : if( s-1.01, 0, abs(noise3(x/s,y/s,z/s)*s) +
						turbulence(x,y,z,2*s) );
turbulencex(x,y,z,s) : if( s-1.01, 0,
			sgn(noise3(x/s,y/s,z/s))*noise3x(x/s,y/s,z/s) +
			turbulencex(x,y,z,2*s) );
turbulencey(x,y,z,s) : if( s-1.01, 0,
			sgn(noise3(x/s,y/s,z/s))*noise3y(x/s,y/s,z/s) +
			turbulencey(x,y,z,2*s) );
turbulencez(x,y,z,s) : if( s-1.01, 0,
			sgn(noise3(x/s,y/s,z/s))*noise3z(x/s,y/s,z/s) +
			turbulencez(x,y,z,2*s) );

			{ Normal distribution from uniform range (0,1) }

un2`P.(t) : t - (2.515517+t*(.802853+t*.010328))/
		(1+t*(1.432788+t*(.189269+t*.001308))) ;
un1`P.(p) : un2`P.(sqrt(-2*log(p))) ;

unif2norm(p) : if( .5-p, -un1`P.(p), un1`P.(1-p) ) ;

nrand(x) = unif2norm(rand(x));

			{ Local (u,v) coordinates for planar surfaces }
crosslen`P. = Nx*Nx + Ny*Ny;
			{ U is distance from projected Z-axis }
U = if( crosslen`P. - FTINY,
		(Py*Nx - Px*Ny)/crosslen`P.,
		Px);
			{ V is defined so that N = U x V }
V = if( crosslen`P. - FTINY,
		Pz - Nz*(Px*Nx + Py*Ny)/crosslen`P.,
		Py);
