#include #include "Quaternions.h" void CrossProduct(XYZ P, XYZ Q, XYZ *V){ V->x = P.y * Q.z - P.z * Q.y; V->y = P.z * Q.x - P.x * Q.z; V->z = P.x * Q.y - P.y * Q.x; } void Normalise(XYZ *vectory) { float d = sqrt(vectory->x*vectory->x+vectory->y*vectory->y+vectory->z*vectory->z); if(d==0){return;} vectory->x /= d; vectory->y /= d; vectory->z /= d; } extern float u0, u1, u2; extern float v0, v1, v2; extern float a, b; extern int i, j; extern bool bInter; extern float pointv[3]; extern float p1v[3]; extern float p2v[3]; extern float p3v[3]; extern float normalv[3]; bool PointInTriangle(XYZ *p, XYZ normal, XYZ *p1, XYZ *p2, XYZ *p3) { bInter=0; pointv[0]=p->x; pointv[1]=p->y; pointv[2]=p->z; p1v[0]=p1->x; p1v[1]=p1->y; p1v[2]=p1->z; p2v[0]=p2->x; p2v[1]=p2->y; p2v[2]=p2->z; p3v[0]=p3->x; p3v[1]=p3->y; p3v[2]=p3->z; normalv[0]=normal.x; normalv[1]=normal.y; normalv[2]=normal.z; #define ABS(X) (((X)<0.f)?-(X):(X) ) #define MAX(A, B) (((A)<(B))?(B):(A)) float max = MAX(MAX(ABS(normalv[0]), ABS(normalv[1])), ABS(normalv[2])); #undef MAX if (max == ABS(normalv[0])) {i = 1; j = 2;} // y, z if (max == ABS(normalv[1])) {i = 0; j = 2;} // x, z if (max == ABS(normalv[2])) {i = 0; j = 1;} // x, y #undef ABS u0 = pointv[i] - p1v[i]; v0 = pointv[j] - p1v[j]; u1 = p2v[i] - p1v[i]; v1 = p2v[j] - p1v[j]; u2 = p3v[i] - p1v[i]; v2 = p3v[j] - p1v[j]; if (u1 > -1.0e-05f && u1 < 1.0e-05f)// == 0.0f) { b = u0 / u2; if (0.0f <= b && b <= 1.0f) { a = (v0 - b * v2) / v1; if ((a >= 0.0f) && (( a + b ) <= 1.0f)) bInter = 1; } } else { b = (v0 * u1 - u0 * v1) / (v2 * u1 - u2 * v1); if (0.0f <= b && b <= 1.0f) { a = (u0 - b * u2) / u1; if ((a >= 0.0f) && (( a + b ) <= 1.0f )) bInter = 1; } } return bInter; } extern float d; extern float a1,a2,a3; extern float total,denom,mu; extern XYZ pa1,pa2,pa3,n; float LineFacetd(XYZ p1,XYZ p2,XYZ pa,XYZ pb,XYZ pc, XYZ n, XYZ *p) { //Calculate the parameters for the plane d = - n.x * pa.x - n.y * pa.y - n.z * pa.z; //Calculate the position on the line that intersects the plane denom = n.x * (p2.x - p1.x) + n.y * (p2.y - p1.y) + n.z * (p2.z - p1.z); if (abs(denom) < 0.0000001) // Line and plane don't intersect return 0; mu = - (d + n.x * p1.x + n.y * p1.y + n.z * p1.z) / denom; p->x = p1.x + mu * (p2.x - p1.x); p->y = p1.y + mu * (p2.y - p1.y); p->z = p1.z + mu * (p2.z - p1.z); if (mu < 0 || mu > 1) // Intersection not along line segment return 0; if(!PointInTriangle( p, n, &pa, &pb, &pc)){return 0;} return 1; } void ReflectVector(XYZ *vel, XYZ *n) { XYZ vn; XYZ vt; float dotprod; dotprod=dotproduct(*n,*vel); vn.x=n->x*dotprod; vn.y=n->y*dotprod; vn.z=n->z*dotprod; vt.x=vel->x-vn.x; vt.y=vel->y-vn.y; vt.z=vel->z-vn.z; vel->x = vt.x - vn.x; vel->y = vt.y - vn.y; vel->z = vt.z - vn.z; } float dotproduct(XYZ point1, XYZ point2){ return point1.x * point2.x + point1.y * point2.y + point1.z * point2.z; } float findDistance(XYZ point1, XYZ point2){ return sqrt((point1.x-point2.x)*(point1.x-point2.x)+(point1.y-point2.y)*(point1.y-point2.y)+(point1.z-point2.z)*(point1.z-point2.z)); } float findLengthfast(XYZ point1){ return((point1.x)*(point1.x)+(point1.y)*(point1.y)+(point1.z)*(point1.z)); } float findDistancefast(XYZ point1, XYZ point2){ return((point1.x-point2.x)*(point1.x-point2.x)+(point1.y-point2.y)*(point1.y-point2.y)+(point1.z-point2.z)*(point1.z-point2.z)); } XYZ DoRotation(XYZ thePoint, float xang, float yang, float zang){ XYZ newpoint; if(xang){ xang*=6.283185; xang/=360; } if(yang){ yang*=6.283185; yang/=360; } if(zang){ zang*=6.283185; zang/=360; } if(yang){ newpoint.z=thePoint.z*cos(yang)-thePoint.x*sin(yang); newpoint.x=thePoint.z*sin(yang)+thePoint.x*cos(yang); thePoint.z=newpoint.z; thePoint.x=newpoint.x; } if(zang){ newpoint.x=thePoint.x*cos(zang)-thePoint.y*sin(zang); newpoint.y=thePoint.y*cos(zang)+thePoint.x*sin(zang); thePoint.x=newpoint.x; thePoint.y=newpoint.y; } if(xang){ newpoint.y=thePoint.y*cos(xang)-thePoint.z*sin(xang); newpoint.z=thePoint.y*sin(xang)+thePoint.z*cos(xang); thePoint.z=newpoint.z; thePoint.y=newpoint.y; } return thePoint; } float square( float f ) { return (f*f) ;} bool sphere_line_intersection(float x1, float y1, float z1, float x2, float y2, float z2, float x3, float y3, float z3, float r) { // x1,y1,z1 P1 coordinates (point of line) // x2,y2,z2 P2 coordinates (point of line) // x3,y3,z3, r P3 coordinates and radius (sphere) // x,y,z intersection coordinates // // This function returns a pointer array which first index indicates // the number of intersection point, followed by coordinate pairs. float a, b, c, i; if(x1>x3+r&&x2>x3+r)return(0); if(x1y3+r&&y2>y3+r)return(0); if(y1z3+r&&z2>z3+r)return(0); if(z1