{
	He-Torrance Reflectance Model (Siggraph 1991)

	Complete spectral version (8/17/95)

	The primitive for this function should look something like:

	void BRTDfunc name
	10
		s`r s`g s`b
		0 0 0
		dd`r dd`g dd`b
		He3.cal
	0
	17	amb`r amb`g amb`b
		amb`r amb`g amb`b
		0 0 0
		sigma0 tau
		n_real`r n_imag`r
		n_real`g n_imag`g
		n_real`b n_imag`b
}

			{ Constants }
lambda`r : .67;			{ red wavelength (microns) }
lambda`g : .55;			{ green wavelength (microns) }
lambda`b : .43;			{ blue wavelength (microns) }
z0err : .0001;			{ accepted error in value of z0 }
Dsumlim : .000001;		{ last term of D summation }
Dsummax : 200;			{ maximum terms in D summation }

			{ Parameters }
sigma0 = arg(10);		{ surface height deviation (microns) }
tau = arg(11);			{ correlation distance (microns) }
n_real`r = arg(12);		{ red real part of index of refraction }
n_imag`r = arg(13);		{ red imaginary part of index of refraction }
n_real`g = arg(14);		{ green real part of index of refraction }
n_imag`g = arg(15);		{ green imaginary part of index of refraction }
n_real`b = arg(16);		{ blue real part of index of refraction }
n_imag`b = arg(17);		{ blue imaginary part of index of refraction }
			{ Derived parameters }
n_k`r = n_imag`r/n_real`r;
n_k`g = n_imag`g/n_real`g;
n_k`b = n_imag`b/n_real`b;

			{ Repeated formulas }
cotexp(t) = tau/sigma0/2/tan(t);
shadowf2(et,erfcet) = (1-.5*erfcet) /
		((Exp(-sq(et))/sqrt(PI)/et - erfcet)/2 + 1);
shadowf1(t) = or(FTINY-sigma0, .01-abs(t));
shadowf0(t) = abs(t) - (PI/2-.0001);
shadowf(t) = if(shadowf0(t), 0, if(shadowf1(t), 1,
		shadowf2(cotexp(t), erfc(cotexp(t)))));
K(t) = if(abs(t)-FTINY, tan(t) * erfc(cotexp(t)), 0);
fuvA`r(ct) = sq(n_real`r)*(1-sq(n_k`r)) - (1-sq(ct));
fuvB`r(ct) = sqrt(sq(fuvA`r(ct)) + 4*sq(sq(n_real`r)*n_k`r));
fu2`r(ct) = (fuvA`r(ct) + fuvB`r(ct))/2;
fv2`r(ct) = (-fuvA`r(ct) + fuvB`r(ct))/2;
fperp2`r(ct) = (sq(ct-sqrt(fu2`r(ct))) + fv2`r(ct)) /
		(sq(ct+sqrt(fu2`r(ct))) + fv2`r(ct));
fpara2`r(ct) = (sq(sq(n_real`r)*(1-sq(n_k`r))*ct - sqrt(fu2`r(ct))) +
		sq(2*sq(n_real`r)*n_k`r*ct - sqrt(fv2`r(ct)))) /
		(sq(sq(n_real`r)*(1-sq(n_k`r))*ct + sqrt(fu2`r(ct))) +
		sq(2*sq(n_real`r)*n_k`r*ct + sqrt(fv2`r(ct))));
fresnel2`r(ct) = (fperp2`r(ct) + fpara2`r(ct))/2;
fuvA`g(ct) = sq(n_real`g)*(1-sq(n_k`g)) - (1-sq(ct));
fuvB`g(ct) = sqrt(sq(fuvA`g(ct)) + 4*sq(sq(n_real`g)*n_k`g));
fu2`g(ct) = (fuvA`g(ct) + fuvB`g(ct))/2;
fv2`g(ct) = (-fuvA`g(ct) + fuvB`g(ct))/2;
fperp2`g(ct) = (sq(ct-sqrt(fu2`g(ct))) + fv2`g(ct)) /
		(sq(ct+sqrt(fu2`g(ct))) + fv2`g(ct));
fpara2`g(ct) = (sq(sq(n_real`g)*(1-sq(n_k`g))*ct - sqrt(fu2`g(ct))) +
		sq(2*sq(n_real`g)*n_k`g*ct - sqrt(fv2`g(ct)))) /
		(sq(sq(n_real`g)*(1-sq(n_k`g))*ct + sqrt(fu2`g(ct))) +
		sq(2*sq(n_real`g)*n_k`g*ct + sqrt(fv2`g(ct))));
fresnel2`g(ct) = (fperp2`g(ct) + fpara2`g(ct))/2;
fuvA`b(ct) = sq(n_real`b)*(1-sq(n_k`b)) - (1-sq(ct));
fuvB`b(ct) = sqrt(sq(fuvA`b(ct)) + 4*sq(sq(n_real`b)*n_k`b));
fu2`b(ct) = (fuvA`b(ct) + fuvB`b(ct))/2;
fv2`b(ct) = (-fuvA`b(ct) + fuvB`b(ct))/2;
fperp2`b(ct) = (sq(ct-sqrt(fu2`b(ct))) + fv2`b(ct)) /
		(sq(ct+sqrt(fu2`b(ct))) + fv2`b(ct));
fpara2`b(ct) = (sq(sq(n_real`b)*(1-sq(n_k`b))*ct - sqrt(fu2`b(ct))) +
		sq(2*sq(n_real`b)*n_k`b*ct - sqrt(fv2`b(ct)))) /
		(sq(sq(n_real`b)*(1-sq(n_k`b))*ct + sqrt(fu2`b(ct))) +
		sq(2*sq(n_real`b)*n_k`b*ct + sqrt(fv2`b(ct))));
fresnel2`b(ct) = (fperp2`b(ct) + fpara2`b(ct))/2;

			{ Formulas dependent only on reflected direction }
theta_r = acos(RdotP);
shadowf_r = shadowf(theta_r);
K_r = K(theta_r);
srx = Dy*NzP - Dz*NyP; sry = Dz*NxP - Dx*NzP; srz = Dx*NyP - Dy*NxP;
srn2 = sq(srx) + sq(sry) + sq(srz);
prx = sry*Dz - srz*Dy;
pry = srz*Dx - srx*Dz;
prz = srx*Dy - sry*Dx;
s`r = fresnel2`r(RdotP)*Exp(-g`r(RdotP))*sq(shadowf_r);
s`g = fresnel2`g(RdotP)*Exp(-g`g(RdotP))*sq(shadowf_r);
s`b = fresnel2`b(RdotP)*Exp(-g`b(RdotP))*sq(shadowf_r);

			{ Formulas dependent on incident direction }
		{ z0 }
z0d(Ki,z) = -(Ki+K_r)/(4*sigma0)*z*Exp(-sq(z/sigma0)/2) - sqrt(PI/2);
z0lim(x) = if(x, max(x,z0err), min(x,-z0err));
z0off(Ki,z) = (sigma0/4*(Ki+K_r)*Exp(-sq(z/sigma0)/2)-sqrt(PI/2)*z)/
		z0lim(z0d(Ki,z));
z0root(Ki, x0, x1, i) = if(i,
			if(z0err-abs(x1-x0),
				x1,
				z0root(Ki,x1,x1-z0off(Ki,x1),i-1)),
			0);
z0(ti) = z0root(K(ti), .1, -z0off(K(ti),.1), 100);
		{ sigma }
sigma(ti) = if( FTINY-sigma0, sigma0,
		sigma0/sqrt(1+sq(z0(ti)/sigma0)) );
		{ g }
g`r(cti) = sq(2*PI/lambda`r*sigma(Acos(cti))*(cti+RdotP));
g`g(cti) = sq(2*PI/lambda`g*sigma(Acos(cti))*(cti+RdotP));
g`b(cti) = sq(2*PI/lambda`b*sigma(Acos(cti))*(cti+RdotP));
		{ |F|^2 }
fresnel2dd`r(kix,kiy,kiz) = fresnel2`r(sqrt(sq(kix-Dx) + sq(kiy-Dy) +
					sq(kiz-Dz))/2);
fresnel2dd`g(kix,kiy,kiz) = fresnel2`g(sqrt(sq(kix-Dx) + sq(kiy-Dy) +
					sq(kiz-Dz))/2);
fresnel2dd`b(kix,kiy,kiz) = fresnel2`b(sqrt(sq(kix-Dx) + sq(kiy-Dy) +
					sq(kiz-Dz))/2);
		{ G }
G(kix,kiy,kiz) = sq( (sq(kix-Dx)+sq(kiy-Dy)+sq(kiz-Dz)) /
			(NxP*(kix-Dx)+NyP*(kiy-Dy)+NzP*(kiz-Dz)) );
		{ D }
Dsum2(m,lt,c,t,e,g) = if(or(m-Dsummax,and(lt-t,Dsumlim-t)),0,
	t+Dsum2(m+1,t,c*g/(m+1),c*g/(m+1)*Exp(-g-e/(m+1))/(m+1),e,g));
Dsum(e,g) = Dsum2(1,0,g,g*Exp(-g-e),e,g);
D`r(kix,kiy,kiz) = sq(PI)/4/sq(lambda`r)*sq(tau) *
	Dsum(sq(2*PI/lambda`r)/4*sq(tau)*
		(sq(kix-Dx)+sq(kiy-Dy)+sq(kiz-Dz) -
			sq(NxP*(kix-Dx)+NyP*(kiy-Dy)+NzP*(kiz-Dz))),
		g`r(kix*NxP+kiy*NyP+kiz*NzP));
D`g(kix,kiy,kiz) = sq(PI)/4/sq(lambda`g)*sq(tau) *
	Dsum(sq(2*PI/lambda`g)/4*sq(tau)*
		(sq(kix-Dx)+sq(kiy-Dy)+sq(kiz-Dz) -
			sq(NxP*(kix-Dx)+NyP*(kiy-Dy)+NzP*(kiz-Dz))),
		g`g(kix*NxP+kiy*NyP+kiz*NzP));
D`b(kix,kiy,kiz) = sq(PI)/4/sq(lambda`b)*sq(tau) *
	Dsum(sq(2*PI/lambda`b)/4*sq(tau)*
		(sq(kix-Dx)+sq(kiy-Dy)+sq(kiz-Dz) -
			sq(NxP*(kix-Dx)+NyP*(kiy-Dy)+NzP*(kiz-Dz))),
		g`b(kix*NxP+kiy*NyP+kiz*NzP));
		{ rho_dd }
dd2(cti) = shadowf_r*shadowf(Acos(cti))/cti/RdotP;
dd`r(kix,kiy,kiz) = dd2(kix*NxP+kiy*NyP+kiz*NzP)*G(kix,kiy,kiz)*
		fresnel2dd`r(kix,kiy,kiz)/PI*D`r(kix,kiy,kiz);
dd`g(kix,kiy,kiz) = dd2(kix*NxP+kiy*NyP+kiz*NzP)*G(kix,kiy,kiz)*
		fresnel2dd`g(kix,kiy,kiz)/PI*D`g(kix,kiy,kiz);
dd`b(kix,kiy,kiz) = dd2(kix*NxP+kiy*NyP+kiz*NzP)*G(kix,kiy,kiz)*
		fresnel2dd`b(kix,kiy,kiz)/PI*D`b(kix,kiy,kiz);
