/*
 * Copyright 2000 Greg Velichansky
 * released under the GNU General Public License    
 * see the files README and COPYING for details.
 */

#define NormalLen 63                    /* Length of a unit vector */
									/* (because asm prog needs to do fixed point) */

typedef struct {
	double x, y, z;
} vector;

/*
double vectlen(vector *thevect){
	return sqrt(pow(thevect -> x, 2) + pow(thevect -> y, 2) + pow(thevect -> z, 2));
}
*/


double vectlen(vector *thevect){
	return sqrt((thevect -> x * thevect -> x + thevect -> y * thevect -> y + thevect -> z * thevect -> z));
}



int zero_length;

vector normalize(vector thevect){
	vector result;
	double thelen;

	zero_length = 0;
	thelen = vectlen(&thevect); 
	if(thelen == 0) {
		puts("Warning, zero length normal vector.");
		thelen = 1;
		zero_length = 1;
	}
	result.x = NormalLen * (thevect.x / thelen);
	result.y = NormalLen * (thevect.y / thelen);
	result.z = NormalLen * (thevect.z / thelen);

	return result;
}

vector getnormal(vector u, vector v){
	vector result;

	result.x = u.y * v.z - u.z * v.y;
	result.y = u.z * v.x - u.x * v.z;
	result.z = u.x * v.y - u.y * v.x;

	result = normalize(result);

	return result;
}
