#include "view.h"

Point3D *newPoint3D(x,y,z)
REAL x,y,z;
{
	Point3D *p;
	
	p = Malloc(Point3D,1);
	p->x = x;
	p->y = y;
	p->z = z;
	
	return(p);
}


REAL detFor3Point3D(p)
Point3D p[3];
{
	return(
			X(p[0])*(Y(p[1])*Z(p[2])-Y(p[2])*Z(p[1]))
			+
			X(p[1])*(Y(p[2])*Z(p[0])-Y(p[0])*Z(p[2]))
			+
			X(p[2])*(Y(p[0])*Z(p[1])-Y(p[1])*Z(p[0]))
		  );
}


void normalPoint3D(p,coeff)
Point3D p[3];
PlaneEq *coeff;
{
	
	coeff->a = Y(p[0])*(Z(p[1])-Z(p[2])) + 
			   Y(p[1])*(Z(p[2])-Z(p[0])) +
			   Y(p[2])*(Z(p[0])-Z(p[1]));
			   
	coeff->b = X(p[0])*(Z(p[2])-Z(p[1])) + 
			   X(p[1])*(Z(p[0])-Z(p[2])) +
			   X(p[2])*(Z(p[1])-Z(p[0]));
			   
	coeff->c = X(p[0])*(Y(p[1])-Y(p[2])) + 
			   X(p[1])*(Y(p[2])-Y(p[0])) +
			   X(p[2])*(Y(p[0])-Y(p[1]));	
			   
}

Classification classifyPoint3D(plane,point,discriminant)
/*
  this classifies a point with respect to a plane. It returns as
  the discriminant the value of the plane equation at the point
*/
PlaneEq *plane;
Point3D	*point;
REAL *discriminant;
{
	Point3D p;
	
	*discriminant =   APL(plane)*X(*point) + BPL(plane)*Y(*point)
					+ CPL(plane)*Z(*point) - DPL(plane); 
	
	return(CLASSIFY(*discriminant));
}

#define PBYMAT(p,m,j)	(X(*p)*MATRIX(m)[0][j]+	\
						 Y(*p)*MATRIX(m)[1][j]+	\
						 Z(*p)*MATRIX(m)[2][j]+	\
						 MATRIX(m)[3][j])
	
void transformPoint3D(p,m)
/*changes p to p.m*/
Point3D *p;
TMatrix *m;
{
	Point3D q;
	
	X(q) = PBYMAT(p,m,0);
	Y(q) = PBYMAT(p,m,1);
	Z(q) = PBYMAT(p,m,2);
	*p = q;
}

REAL distanceOfPoint(p,q)
/*returns the distance of q from p*/
Point3D *p,*q;
{
	return(SQRT(SQR(p->x - q->x) + SQR(p->y - q->y) + SQR(p->z - q->z)));
}