
/* Generated by Interface Builder */

#import "GloveView.h"
#import <math.h>
#import <dpsclient/psops.h>
#import <dpsclient/wraps.h>

void convertToScreen( double x, double y, double z, double *newX, double *newY);
void PS3Dlineto( double x, double y, double z);
void PS3Dmoveto( double x, double y, double z);
void PS4Dlineto( double x, double y, double z, double theta);
void PS4Dmoveto( double x, double y, double z, double theta);


@implementation GloveView
+ newFrame: (const NXRect *) frameRect
{
	self = [super newFrame: frameRect];
	x = y = z = 0.0;
	length = 15;
	width = 15.0;
	height = 6.0;
	
	return self;
}

void convertToScreen( double x, double y, double z, double *newX, double *newY)
{
//	*newX = x;
//	*newY = y;
	
	*newX = x - z * cos( M_PI / 4.0 );
	*newY = y - z * sin( M_PI / 4.0 );

	#ifdef DEBUG2
		fprintf(stderr,"Converting from %2f, %2f, %2f to %2f %2f.\n",
			x, y, z, *newX, *newY );
	#endif
	
	return;
}

void PS3Dlineto( double x, double y, double z)
{
	double newX, newY;
	convertToScreen( x, y , z, &newX, &newY);
	PSlineto( newX, newY);
	return;
}

void PS3Dmoveto( double x, double y, double z)
{
	double newX, newY;
	convertToScreen( x, y , z, &newX, &newY);
	PSmoveto( newX, newY);
	return;
}

void PS4Dlineto( double x, double y, double z, double theta)
{
	double newX, newY;
	convertToScreen( x * cos( theta) + x * sin(theta),
			 y * cos(theta) - y * sin(theta), 
			 z, &newX, &newY);
	PSlineto( newX, newY);
	return;
}

void PS4Dmoveto( double x, double y, double z, double theta)
{
	double newX, newY;
	convertToScreen( x * cos( theta) + x * sin(theta),
			 y * cos(theta) - y * sin(theta), 
			 z, &newX, &newY);
	PSmoveto( newX, newY);
	return;
}



- drawSelf: (const NXRect *) rects : (int) rectCount
{	
	[self lockFocus];
        PSsetgray (NX_BLACK);
        NXRectFill (&bounds);
	
	PSscale( 1.8, 1.8);
	PStranslate( 140.0, 140.0);
	
	PSsetgray( NX_DKGRAY);
	PSsetlinewidth( 2.0 );
	
	// Draw Z origin
	PSsetgray( 	NX_DKGRAY);
	PS3Dmoveto( 	0.0, 	0.0, 	-128.0);
	PS3Dlineto( 	0.0, 	0.0, 	0.0 );
	PSstroke();
	PSsetgray( 	NX_LTGRAY);
	PS3Dmoveto( 	0.0, 	0.0, 	0.0 );
	PS3Dlineto( 	0.0, 	0.0, 	128.0 );
	PSstroke();

	// Draw X origin
	PSsetgray( 	NX_DKGRAY );
	PS3Dmoveto( 	-128.0, 0.0, 	0.0 );
	PS3Dlineto( 	0.0, 	0.0, 	0.0 );
	PSstroke();
	PSsetgray( 	NX_LTGRAY );
	PS3Dmoveto( 	0.0, 	0.0, 	0.0 );
	PS3Dlineto( 	128.0, 	0.0, 	0.0 );
	PSstroke();
	
	// Draw Y origin
	PSsetgray( NX_DKGRAY);
	PS3Dmoveto( 	0.0, 	-128.0,	0.0 );
	PS3Dlineto( 	0.0, 	0.0, 	0.0 );
	PSstroke();
	PSsetgray( NX_LTGRAY);
	PS3Dmoveto( 	0.0,	0.0, 	0.0 );
	PS3Dlineto( 	0.0, 	128.0, 	0.0 );
	PSstroke();
	

	[self unlockFocus];	
	return self;
}

- setX: (int) ax y: (int) ay z: (int) az roll: (int) aroll
{
	double	dxx, dxy, dyx, dyy, dz, theta;


	x = ax;
	y = ay;
	z = az;
	roll = aroll;

	[self lockFocus];
	PSscale( 1.8, 1.8);
	PStranslate( 140.0, 140.0);

	PSsetinstance( YES );
	PSnewinstance();
	// Draw false looking "depth cues"
	// X depth cue
	PSsetgray( (x > 0) ? NX_WHITE: NX_LTGRAY);
	PS3Dmoveto( 	x, 	- 2.0, 	0.0 );
	PS3Dlineto( 	x,  	2.0, 	0.0 );
	PSstroke();
	// Y depth cue
	PSsetgray( (y > 0) ? NX_WHITE : NX_LTGRAY);
	PS3Dmoveto( 	-2.0, 	y, 	0.0 );
	PS3Dlineto( 	2.0,  	y, 	0.0 );
	PSstroke();
	// Z depth cue
	PSsetgray( (z > 0) ? NX_WHITE : NX_LTGRAY);
	PS3Dmoveto( 	1.4, 	- 1.4, 	z );
	PS3Dlineto( 	- 1.4, 	1.4, 	z );
	PSstroke();


	// Draw "glove"
	//	
	theta = roll * (M_PI / 6);
	dxx = (width / 2.0) * cos( theta);
	dxy = height * sin(theta);
	dyx = - (width / 2.0)  * sin(theta);
	dyy = height * cos( theta);
	dz = length;
	PSsetgray( NX_WHITE);
	PSsetlinewidth( 1.0 );
	PS3Dmoveto( x - dxx, 	y - dyx, 	z );		// -1, 0, 0
	PS3Dlineto( x + dxx, 	y + dyx, 	z );		//  1, 0, 0
	PS3Dlineto( x + dxx, 	y + dyx, 	z + dz );	//  1, 0, 1
	PS3Dlineto( x - dxx, 	y - dyx,	z + dz );	// -1, 0, 1
	PS3Dlineto( x - dxx, 	y - dyx, 	z );		// -1, 0, 0
	PSstroke();	
	PS3Dmoveto( x -dxx-dxy, y - dyx - dyy,	z );		// -1,-1, 0
	PS3Dlineto( x +dxx-dxy, y + dyx - dyy, 	z );		//  1,-1, 0
	PS3Dlineto( x +dxx-dxy, y + dyx - dyy, 	z + dz );	//  1,-1, 1
	PS3Dlineto( x -dxx-dxy, y - dyx - dyy, 	z + dz );	// -1,-1, 1
	PS3Dlineto( x -dxx-dxy, y - dyx - dyy,  z );		// -1,-1, 0
	PSstroke();
	PS3Dmoveto( x - dxx, 	y - dyx, 	z );		// -1, 0, 0
	PS3Dlineto( x -dxx-dxy, y - dyx - dyy,	z );		// -1,-1, 0
	PS3Dmoveto( x + dxx, 	y + dyx, 	z );		//  1, 0, 0
	PS3Dlineto( x +dxx-dxy, y + dyx - dyy, 	z );		//  1,-1, 0
	PS3Dmoveto( x + dxx, 	y + dyx, 	z + dz );	//  1, 0, 1
	PS3Dlineto( x +dxx-dxy, y + dyx - dyy, 	z + dz );	//  1,-1, 1
	PS3Dmoveto( x - dxx, 	y - dyx, 	z + dz);	// -1, 0, 1
	PS3Dlineto( x -dxx-dxy, y - dyx - dyy,  z + dz);	// -1,-1, 1
	PSstroke();
	PSsetinstance( NO );
	[self unlockFocus];	
	return self;
}




- setLength: (int) al height: (int) ah width: (int) aw
{
	length = al;
	height = ah;
	width = aw;
	[self display];
	return self;
}
	




@end
