www.pudn.com > mfcopentree.rar > 3DMath.h


#ifndef _3DMATH_H 
#define _3DMATH_H 
 
#define PI 3.1415926535897932					// This is our famous PI 
// This is used to hold 3D points and vectors.  This will eventually be added upon 
// to create a robut class with operator overloading.  For now we just need an x, y, z. 
 
typedef struct tagWeb 
{ 
	float nor1[3]; 
	float point1[3]; 
	float nor2[3]; 
	float point2[3]; 
	float nor3[3]; 
	float point3[3]; 
}WebTri; 
 
typedef struct STLStruct 
{ 
	float fNormal[3]; 
	float fPoint1[3]; 
	float fPoint2[3]; 
	float fPoint3[3]; 
	struct STLStruct* next; 
}STLData, *pSTLData; 
 
 
typedef struct STLDArray 
{ 
	STLData *stl; 
	UINT     flag; 
	long trinumber; 
	float	 boxsize[3]; 
	float	 center[3]; 
	struct STLDArray* next; 
}STLArray; 
 
struct CVector3 
{ 
public: 
	float x, y, z;						 
}; 
 
typedef struct tagOctTree 
{ 
	CVector3 m_vCenter;				//中心坐标 
	float m_Width;					//立方体宽度 
	struct tagOctTree *pchild[8]; 
	BOOL  IsHave; 
}STLOctTree, *pOctTree; 
 
//	This returns a perpendicular vector from 2 given vectors by taking the cross product. 
CVector3 Cross(CVector3 vVector1, CVector3 vVector2); 
 
//	This returns a vector between 2 points 
CVector3 Vector(CVector3 vPoint1, CVector3 vPoint2); 
 
//	This returns the magnitude of a normal (or any other vector) 
float Magnitude(CVector3 vNormal); 
 
//	This returns a normalize vector (A vector exactly of length 1) 
CVector3 Normalize(CVector3 vNormal); 
 
//	This returns the normal of a polygon (The direction the polygon is facing) 
CVector3 NormalVector(CVector3 vTriangle[]); 
 
// This returns the distance the plane is from the origin (0, 0, 0) 
// It takes the normal to the plane, along with ANY point that lies on the plane (any corner) 
float PlaneDistance(CVector3 NormalVector, CVector3 Point); 
 
// This takes a triangle (plane) and line and returns true if they intersected 
bool IntersectedPlane(CVector3 vPoly[], CVector3 vLine[], CVector3 &vNormal, float &originDistance); 
 
bool IntersectedPlane_2(CVector3 vLine[], CVector3 vNormal, float originDistance); 
 
bool IntersectedPlane_3(CVector3 vPoly[], CVector3 vLine[], CVector3 vNormal); 
/////// * /////////// * /////////// * NEW * /////// * /////////// * /////////// * 
 
 
// This returns the dot product between 2 vectors 
float Dot(CVector3 vVector1, CVector3 vVector2); 
 
// This returns the angle between 2 vectors 
double AngleBetweenVectors(CVector3 Vector1, CVector3 Vector2); 
 
// This returns an intersection point of a polygon and a line (assuming intersects the plane) 
CVector3 IntersectionPoint(CVector3 vNormal, CVector3 vLine[], double distance); 
 
// This returns true if the intersection point is inside of the polygon 
bool InsidePolygon(CVector3 vIntersection, CVector3 Poly[], long verticeCount); 
 
// Use this function to test collision between a line and polygon 
bool IntersectedPolygon(CVector3 vPoly[], CVector3 vLine[], int verticeCount); 
 
bool IntersectedPolygon2(CVector3 tri[3], CVector3 nor, CVector3 vLine[], float dis);//三角形平面方程已知 
 
bool IntersectedPolygon3(CVector3 vPoly[], CVector3 nor, CVector3 vLine[], int verticeCount, float dis); 
/////// * /////////// * /////////// * NEW * /////// * /////////// * /////////// * 
 
void CreateNode(STLOctTree *node, STLArray *data, float precision, int i); 
 
int ChildIsHave(STLOctTree *node, STLArray *data); 
 
#endif