#include "maps.h"

/*----------------------------------------------------------------------
 *
 * sinknurl puts an impressed sinusoidal parallel pattern on the surface
 * of a "cylindrical workpiece" with a cylindrical projection.
 *
 * freq - "frequency" of the knurl
 * depth - magnitude of knurl
 * zmin,zmax - extent of knurl
 * dampzone - distance from ends to do a smoothstep diminishing of depth
 * maporigin - the bottom point of the center axis of the cylindrical proj.
 * xaxis, yaxis, zaxis - 3 points defining the coordinate system of
 *        the cylinder.  The central axis is along zaxis-maporigin.
 *	  The point on the equator where (uu,vv)==(0,0) is mapped is at xaxis.
 *--------------------------------------------------------------------*/

displacement
sinknurl(float freq = 100, float depth = 0.005,
	float zmin = -1e20, float zmax = 1e20, float dampzone = 0,
	point maporigin=point ("shader",0,0,0),
	point xaxis=point ("shader",1,0,0), point yaxis=point ("shader",0,1,0),
	point zaxis=point ("shader",0,0,1))
{
    point	PP,O,X,Y,Z;
    normal      sN;
    float	uu, vv, magnitude;

    /* do projection in shader space */
	PP=transform("shader",P);
	O=transform("shader",maporigin);
	X=transform("shader",xaxis);
	Y=transform("shader",yaxis);
	Z=transform("shader",zaxis);
	cylinmap(PP,uu,vv,O,X,Y,Z);

    /* if inside knurl boundary... */
    if ((vv >= zmin) && (vv <= zmax)) {
    /* calculate new surface point and normal */
	if ((vv >= zmin+dampzone) && (vv <= zmax-dampzone)) {
		magnitude = depth;
	} else {
		magnitude = depth * min(smoothstep(zmin,zmin+dampzone,vv),
			1-smoothstep(zmax-dampzone,zmax,vv));
	}
      /* calculate sine wave pattern */
	sN = normalize(transform("shader",N));
	P = transform("shader","common",PP + sN *
	    magnitude*(sin(2*M_PI*freq*uu) - 1));
	N = calculatenormal(P);
    }
}
