int triBoxOverlap(float boxcenter[3],float boxhalfsize[3],float triverts[3][3])
{



    /*    use separating axis theorem to test overlap between triangle and box */

    /*    need to test for overlap in these directions: */

    /*    1) the {x,y,z}-directions (actually, since we use the AABB of the triangle */

    /*       we do not even need to test these) */

    /*    2) normal of the triangle */

    /*    3) crossproduct(edge from tri, {x,y,z}-directin) */

    /*       this gives 3x3=9 more tests */

    float v0[3],v1[3],v2[3];

//   float axis[3];

    float min,max,p0,p1,p2,rad,fex,fey,fez;		// -NJMP- "d" local variable removed

    float normal[3],e0[3],e1[3],e2[3];



    /* This is the fastest branch on Sun */

    /* move everything so that the boxcenter is in (0,0,0) */

    SUB(v0,triverts[0],boxcenter);

    SUB(v1,triverts[1],boxcenter);

    SUB(v2,triverts[2],boxcenter);



    /* compute triangle edges */

    SUB(e0,v1,v0);      /* tri edge 0 */

    SUB(e1,v2,v1);      /* tri edge 1 */

    SUB(e2,v0,v2);      /* tri edge 2 */



    /* Bullet 3:  */

    /*  test the 9 tests first (this was faster) */

    fex = fabsf(e0[X]);

    fey = fabsf(e0[Y]);

    fez = fabsf(e0[Z]);

    AXISTEST_X01(e0[Z], e0[Y], fez, fey);

    AXISTEST_Y02(e0[Z], e0[X], fez, fex);

    AXISTEST_Z12(e0[Y], e0[X], fey, fex);



    fex = fabsf(e1[X]);

    fey = fabsf(e1[Y]);

    fez = fabsf(e1[Z]);

    AXISTEST_X01(e1[Z], e1[Y], fez, fey);

    AXISTEST_Y02(e1[Z], e1[X], fez, fex);

    AXISTEST_Z0(e1[Y], e1[X], fey, fex);



    fex = fabsf(e2[X]);

    fey = fabsf(e2[Y]);

    fez = fabsf(e2[Z]);

    AXISTEST_X2(e2[Z], e2[Y], fez, fey);

    AXISTEST_Y1(e2[Z], e2[X], fez, fex);

    AXISTEST_Z12(e2[Y], e2[X], fey, fex);



    /* Bullet 1: */

    /*  first test overlap in the {x,y,z}-directions */

    /*  find min, max of the triangle each direction, and test for overlap in */

    /*  that direction -- this is equivalent to testing a minimal AABB around */

    /*  the triangle against the AABB */



    /* test in X-direction */

    FINDMINMAX(v0[X],v1[X],v2[X],min,max);

    if(min>boxhalfsize[X] || max<-boxhalfsize[X]) return 0;



    /* test in Y-direction */

    FINDMINMAX(v0[Y],v1[Y],v2[Y],min,max);

    if(min>boxhalfsize[Y] || max<-boxhalfsize[Y]) return 0;



    /* test in Z-direction */

    FINDMINMAX(v0[Z],v1[Z],v2[Z],min,max);

    if(min>boxhalfsize[Z] || max<-boxhalfsize[Z]) return 0;



    /* Bullet 2: */

    /*  test if the box intersects the plane of the triangle */

    /*  compute plane equation of triangle: normal*x+d=0 */

    CROSS(normal,e0,e1);

    // -NJMP- (line removed here)

    if(!planeBoxOverlap(normal,v0,boxhalfsize)) return 0;	// -NJMP-



    return 1;   /* box and triangle overlaps */

}
示例#2
0
static bool fast_tri_box_overlap(const Vector3& boxcenter,const Vector3 boxhalfsize,const Vector3 *triverts) {

  /*    use separating axis theorem to test overlap between triangle and box */
  /*    need to test for overlap in these directions: */
  /*    1) the {x,y,z}-directions (actually, since we use the AABB of the triangle */
  /*       we do not even need to test these) */
  /*    2) normal of the triangle */
  /*    3) crossproduct(edge from tri, {x,y,z}-directin) */
  /*       this gives 3x3=9 more tests */
   Vector3 v0,v1,v2;
   float min,max,d,p0,p1,p2,rad,fex,fey,fez;
   Vector3 normal,e0,e1,e2;

   /* This is the fastest branch on Sun */
   /* move everything so that the boxcenter is in (0,0,0) */

   v0=triverts[0]-boxcenter;
   v1=triverts[1]-boxcenter;
   v2=triverts[2]-boxcenter;

   /* compute triangle edges */
   e0=v1-v0;      /* tri edge 0 */
   e1=v2-v1;      /* tri edge 1 */
   e2=v0-v2;      /* tri edge 2 */

   /* Bullet 3:  */
   /*  test the 9 tests first (this was faster) */
   fex = Math::abs(e0.x);
   fey = Math::abs(e0.y);
   fez = Math::abs(e0.z);
   AXISTEST_X01(e0.z, e0.y, fez, fey);
   AXISTEST_Y02(e0.z, e0.x, fez, fex);
   AXISTEST_Z12(e0.y, e0.x, fey, fex);

   fex = Math::abs(e1.x);
   fey = Math::abs(e1.y);
   fez = Math::abs(e1.z);
   AXISTEST_X01(e1.z, e1.y, fez, fey);
   AXISTEST_Y02(e1.z, e1.x, fez, fex);
   AXISTEST_Z0(e1.y, e1.x, fey, fex);

   fex = Math::abs(e2.x);
   fey = Math::abs(e2.y);
   fez = Math::abs(e2.z);
   AXISTEST_X2(e2.z, e2.y, fez, fey);
   AXISTEST_Y1(e2.z, e2.x, fez, fex);
   AXISTEST_Z12(e2.y, e2.x, fey, fex);

   /* Bullet 1: */
   /*  first test overlap in the {x,y,z}-directions */
   /*  find min, max of the triangle each direction, and test for overlap in */
   /*  that direction -- this is equivalent to testing a minimal AABB around */
   /*  the triangle against the AABB */

   /* test in X-direction */
   FINDMINMAX(v0.x,v1.x,v2.x,min,max);
   if(min>boxhalfsize.x || max<-boxhalfsize.x) return false;

   /* test in Y-direction */
   FINDMINMAX(v0.y,v1.y,v2.y,min,max);
   if(min>boxhalfsize.y || max<-boxhalfsize.y) return false;

   /* test in Z-direction */
   FINDMINMAX(v0.z,v1.z,v2.z,min,max);
   if(min>boxhalfsize.z || max<-boxhalfsize.z) return false;

   /* Bullet 2: */
   /*  test if the box intersects the plane of the triangle */
   /*  compute plane equation of triangle: normal*x+d=0 */
   normal=e0.cross(e1);
   d=-normal.dot(v0);  /* plane eq: normal.x+d=0 */
   if(!planeBoxOverlap(normal,d,boxhalfsize)) return false;

   return true;   /* box and triangle overlaps */
}
示例#3
0
bool Octree::boxTriangleIntersection(BoundingBox Box, Triangle triangle)
{
  //This algorithm is a composition of 13 tests to determine intersection
	//if all pass, there is overlap

	float min,max,d,p0,p1,p2,rad,fex,fey,fez;

  //center the box at (0,0,0) with respect to the triangle

	//translate triangle vertices
	Vector v0,v1,v2;
	//edges of new triangle
	Vector e0,e1,e2;

	//Vector that goes half the diagonal of the bounding box
	Vector boxHalfSize = (Box.boxMaxExtent - Box.boxMinExtent) / 2.0;

	point boxCenter( (Box.boxMinExtent.px + Box.boxMaxExtent.px)/2.0,
                   (Box.boxMinExtent.py + Box.boxMaxExtent.py)/2.0,
									 (Box.boxMinExtent.pz + Box.boxMaxExtent.pz)/2.0 );

	/*v0.set(triangle.Vertex[0].px-boxCenter.px,triangle.Vertex[0].py-boxCenter.py,triangle.Vertex[0].pz-boxCenter.pz);
	v1.set(triangle.Vertex[1].px-boxCenter.px,triangle.Vertex[1].py-boxCenter.py,triangle.Vertex[1].pz-boxCenter.pz);
	v2.set(triangle.Vertex[2].px-boxCenter.px,triangle.Vertex[2].py-boxCenter.py,triangle.Vertex[2].pz-boxCenter.pz);*/

	v0 = triangle.Vertex[0] - boxCenter;
	v1 = triangle.Vertex[1] - boxCenter;
	v2 = triangle.Vertex[2] - boxCenter;

  e0 = v1 - v0;
	e1 = v2 - v1;
	e2 = v0 - v2;

	//the first 9 tests
   fex = fabs(e0.px);
   fey = fabs(e0.py);
   fez = fabs(e0.pz);
   AXISTEST_X01(e0.pz, e0.py, fez, fey);
   AXISTEST_Y02(e0.pz, e0.px, fez, fex);
   AXISTEST_Z12(e0.py, e0.px, fey, fex);

   fex = fabs(e1.px);
   fey = fabs(e1.py);
   fez = fabs(e1.pz);
   AXISTEST_X01(e1.pz, e1.py, fez, fey);
   AXISTEST_Y02(e1.pz, e1.px, fez, fex);
   AXISTEST_Z0(e1.py, e1.px, fey, fex);

   fex = fabs(e2.px);
   fey = fabs(e2.py);
   fez = fabs(e2.pz);
   AXISTEST_X2(e2.pz, e2.py, fez, fey);
   AXISTEST_Y1(e2.pz, e2.px, fez, fex);
   AXISTEST_Z12(e2.py, e2.px, fey, fex);

	 //now the 3 tests
	 //X test
   FINDMINMAX(v0.px,v1.px,v2.px,min,max);
   if(min>boxHalfSize.px || max<-boxHalfSize.px) 
		 return false;

   //Y test
   FINDMINMAX(v0.py,v1.py,v2.py,min,max);
   if(min>boxHalfSize.py || max<-boxHalfSize.py) 
		 return false;

   //Z test
   FINDMINMAX(v0.pz,v1.pz,v2.pz,min,max);
   if(min>boxHalfSize.pz || max<-boxHalfSize.pz) 
		 return false;

	Vector normal = e0.crossProduct(e1);
	d = -(normal.dotProduct(v0));

	if(!planeBoxOverlap(normal,d,boxHalfSize)) 
		return false;

	return true;
}
示例#4
0
// main function for triangle box overlaping
bool triBoxOverlap(Vector4 boxcenter, float boxhalfsize, Vector4 p0, Vector4 p1, Vector4 p2)
{
  /*    use separating axis theorem to test overlap between triangle and box */
  /*    need to test for overlap in these directions: */
  /*    1) the {x,y,z}-directions (actually, since we use the AABB of the triangle */
  /*       we do not even need to test these) */
  /*    2) normal of the triangle */
  /*    3) crossproduct(edge from tri, {x,y,z}-directin) */
  /*       this gives 3x3=9 more tests */

   Vector4 v0,v1,v2;
   Vector4 e0,e1,e2;
   Vector4 normal;
   float min,max,fex,fey,fez;

   /* This is the fastest branch on Sun */
   /* move everything so that the boxcenter is in (0,0,0) */
   v0 = p0 - boxcenter;
   v1 = p1 - boxcenter;
   v2 = p2 - boxcenter;

   /* compute triangle edges */
   e0 = v1 - v0;      /* tri edge 0 */
   e1 = v2 - v1;      /* tri edge 1 */
   e2 = v0 - v2;      /* tri edge 2 */

   /* Bullet 3:  */
   /*  test the 9 tests first (this was faster) */
   fex = fabs(e0.X);
   fey = fabs(e0.Y);
   fez = fabs(e0.Z);

   if (!AXISTEST(e0.Z, e0.Y, fez, fey, boxhalfsize, v0.Y, v0.Z, v2.Y, v2.Z)) return false;
   if (!AXISTEST(e0.Z, e0.X, fez, fex, boxhalfsize, -v0.X, -v0.Z, -v2.X, -v2.Z)) return false;
   if (!AXISTEST(e0.Y, e0.X, fey, fex, boxhalfsize, v1.X, v1.Y, v2.X, v2.Y)) return false;


   fex = fabs(e1.X);
   fey = fabs(e1.Y);
   fez = fabs(e1.Z);

   if (!AXISTEST(e1.Z, e1.Y, fez, fey, boxhalfsize, v0.Y, v0.Z, v2.Y, v2.Z)) return false;
   if (!AXISTEST(e1.Z, e1.X, fez, fex, boxhalfsize, -v0.X, -v0.Z, -v2.X, -v2.Z)) return false;
   if (!AXISTEST(e1.Y, e1.X, fey, fex, boxhalfsize, v0.X, v0.Y, v1.X, v1.Y)) return false;


   fex = fabs(e2.X);
   fey = fabs(e2.Y);
   fez = fabs(e2.Z);

   if (!AXISTEST(e2.Z, e2.Y, fez, fey, boxhalfsize, v0.Y, v0.Z, v1.Y, v1.Z)) return false;
   if (!AXISTEST(e2.Z, e2.X, fez, fex, boxhalfsize, -v0.X, -v0.Z, -v1.X, -v1.Z)) return false;
   if (!AXISTEST(e2.Y, e2.X, fey, fex, boxhalfsize, v1.X, v1.Y, v2.X, v2.Y)) return false;

   /* Bullet 1: */
   /*  first test overlap in the {x,y,z}-directions */
   /*  find min, max of the triangle each direction, and test for overlap in */
   /*  that direction -- this is equivalent to testing a minimal AABB around */
   /*  the triangle against the AABB */


   /* test in X-direction */
   FINDMINMAX(v0.X,v1.X,v2.X,min,max);
   if(min>boxhalfsize || max<-boxhalfsize)
	   return false;

   /* test in Y-direction */
   FINDMINMAX(v0.Y,v1.Y,v2.Y,min,max);
   if(min>boxhalfsize || max<-boxhalfsize)
	   return false;

   /* test in Z-direction */
   FINDMINMAX(v0.Z,v1.Z,v2.Z,min,max);
   if(min>boxhalfsize || max<-boxhalfsize)
	   return false;

   /* Bullet 2: */
   /*  test if the box intersects the plane of the triangle */
   /*  compute plane equation of triangle: normal*x+d=0 */

   normal = e0 % e1;
   if(!planeBoxOverlap(normal,v0,boxhalfsize))
	   return false;

   return true;   /* box and triangle overlaps */

}
示例#5
0
/* by Tomas Akenine-Möller                              */
bool BoundingBox::containsTriangle( const Vec& tv0, const Vec& tv1, const Vec& tv2 ) const
{
	Vec boxcenter(center);
	Vec boxhalfsize(xExtent, yExtent, zExtent);

	int X = 0, Y = 1, Z = 2;

	/*    use separating axis theorem to test overlap between triangle and box */
	/*    need to test for overlap in these directions: */
	/*    1) the {x,y,z}-directions (actually, since we use the AABB of the triangle */
	/*       we do not even need to test these) */
	/*    2) normal of the triangle */
	/*    3) crossproduct(edge from tri, {x,y,z}-directin) */
	/*       this gives 3x3=9 more tests */
	Vec v0,v1,v2;
	double min,max,p0,p1,p2,rad,fex,fey,fez;
	Vec normal,e0,e1,e2;

	/* This is the fastest branch on Sun */
	/* move everything so that the box center is in (0,0,0) */
	v0=tv0-boxcenter;
	v1=tv1-boxcenter;
	v2=tv2-boxcenter;
	
	/* compute triangle edges */
	e0=v1-v0;      /* tri edge 0 */
	e1=v2-v1;      /* tri edge 1 */
	e2=v0-v2;      /* tri edge 2 */
	
	/* Bullet 3:  */
	/*  test the 9 tests first (this was faster) */
	fex = fabsf(e0[X]);
	fey = fabsf(e0[Y]);
	fez = fabsf(e0[Z]);
	AXISTEST_X01(e0[Z], e0[Y], fez, fey);
	AXISTEST_Y02(e0[Z], e0[X], fez, fex);
	AXISTEST_Z12(e0[Y], e0[X], fey, fex);
	fex = fabsf(e1[X]);
	fey = fabsf(e1[Y]);
	fez = fabsf(e1[Z]);
	AXISTEST_X01(e1[Z], e1[Y], fez, fey);
	AXISTEST_Y02(e1[Z], e1[X], fez, fex);
	AXISTEST_Z0(e1[Y], e1[X], fey, fex);
	fex = fabsf(e2[X]);
	fey = fabsf(e2[Y]);
	fez = fabsf(e2[Z]);
	AXISTEST_X2(e2[Z], e2[Y], fez, fey);
	AXISTEST_Y1(e2[Z], e2[X], fez, fex);
	AXISTEST_Z12(e2[Y], e2[X], fey, fex);
	
	/* Bullet 1: */
	/*  first test overlap in the {x,y,z}-directions */
	/*  find min, max of the triangle each direction, and test for overlap in */
	/*  that direction -- this is equivalent to testing a minimal AABB around */
	/*  the triangle against the AABB */
	/* test in X-direction */
	FINDMINMAX(v0[X],v1[X],v2[X],min,max);
	if(min>boxhalfsize[X] || max<-boxhalfsize[X]) return 0;
	/* test in Y-direction */
	FINDMINMAX(v0[Y],v1[Y],v2[Y],min,max);
	if(min>boxhalfsize[Y] || max<-boxhalfsize[Y]) return 0;
	/* test in Z-direction */
	FINDMINMAX(v0[Z],v1[Z],v2[Z],min,max);
	if(min>boxhalfsize[Z] || max<-boxhalfsize[Z]) return 0;
	
	/* Bullet 2: */
	/*  test if the box intersects the plane of the triangle */
	/*  compute plane equation of triangle: normal*x+d=0 */
	normal = e0 ^ e1;

	if(!planeBoxOverlap(normal,v0,boxhalfsize)) return 0;
	return 1;   /* box and triangle overlaps */
}
示例#6
0
int IsTriangleIntersectBox(float boxcenter[3], float boxhalf[3], float triverts[3][3])
{
	/*    use separating axis theorem to test overlap between triangle and box */
	/*    need to test for overlap in these directions: */
	/*    1) the {x,y,z}-directions (actually, since we use the AABB of the triangle */
	/*       we do not even need to test these) */
	/*    2) normal of the triangle */
	/*    3) crossproduct(edge from tri, {x,y,z}-directin) */
	/*       this gives 3x3=9 more tests */
	float v0[3],v1[3],v2[3];
	float min,max,d,p0,p1,p2,rad,fex,fey,fez;  
	float normal[3],e0[3],e1[3],e2[3];

	/* 1) first test overlap in the {x,y,z}-directions */
	/*    find min, max of the triangle each direction, and test for overlap in */
	/*    that direction -- this is equivalent to testing a minimal AABB around */
	/*    the triangle against the AABB */

	/*    test in X */
	v0[X]=triverts[0][X]-boxcenter[X];
	v1[X]=triverts[1][X]-boxcenter[X];
	v2[X]=triverts[2][X]-boxcenter[X];
	FINDMINMAX(v0[X],v1[X],v2[X],min,max);
	if(min>boxhalf[X] || max<-boxhalf[X]) return 0;

	/*    test in Y */
	v0[Y]=triverts[0][Y]-boxcenter[Y];
	v1[Y]=triverts[1][Y]-boxcenter[Y];
	v2[Y]=triverts[2][Y]-boxcenter[Y];
	FINDMINMAX(v0[Y],v1[Y],v2[Y],min,max);
	if(min>boxhalf[Y] || max<-boxhalf[Y]) return 0;

	/*    test in Z */
	v0[Z]=triverts[0][Z]-boxcenter[Z];
	v1[Z]=triverts[1][Z]-boxcenter[Z];
	v2[Z]=triverts[2][Z]-boxcenter[Z];
	FINDMINMAX(v0[Z],v1[Z],v2[Z],min,max);
	if(min>boxhalf[Z] || max<-boxhalf[Z]) return 0;

	/*    2) */
	/*    test if the box intersects the plane of the triangle */
	/*    compute plane equation of triangle: normal*x+d=0 */
	SUB(e0,v1,v0);      /* tri edge 0 */
	SUB(e1,v2,v1);      /* tri edge 1 */
	CROSS(normal,e0,e1);
	d=-DOT(normal,v0);  /* plane eq: normal.x+d=0 */

	if(!planeBoxOverlap(normal,d,boxhalf)) return 0;

	/*    compute the last triangle edge */
	SUB(e2,v0,v2);

	/*    3) */
	fex = fabsf(e0[X]);
	fey = fabsf(e0[Y]);
	fez = fabsf(e0[Z]);
	AXISTEST_X01(e0[Z], e0[Y], fez, fey);
	AXISTEST_Y02(e0[Z], e0[X], fez, fex);
	AXISTEST_Z12(e0[Y], e0[X], fey, fex);

	fex = fabsf(e1[X]);
	fey = fabsf(e1[Y]);
	fez = fabsf(e1[Z]);
	AXISTEST_X01(e1[Z], e1[Y], fez, fey);
	AXISTEST_Y02(e1[Z], e1[X], fez, fex);
	AXISTEST_Z0(e1[Y], e1[X], fey, fex);

	fex = fabsf(e2[X]);
	fey = fabsf(e2[Y]);
	fez = fabsf(e2[Z]);
	AXISTEST_X2(e2[Z], e2[Y], fez, fey);
	AXISTEST_Y1(e2[Z], e2[X], fez, fex);
	AXISTEST_Z12(e2[Y], e2[X], fey, fex);

	return 1;
}
int IntersectionTests::triBoxOverlap(float boxhalfsize[3],float v0[3], float v1[3],float v2[3])
{

  /*    use separating axis theorem to test overlap between triangle and box */
  /*    need to test for overlap in these directions: */
  /*    1) the {x,y,z}-directions (actually, since we use the AABB of the triangle */
  /*       we do not even need to test these) */
  /*    2) normal of the triangle */
  /*    3) crossproduct(edge from tri, {x,y,z}-directin) */
  /*       this gives 3x3=9 more tests */
   //double v0[3],v1[3],v2[3];
  // double axis[3];
   float min,max,d,p0,p1,p2,rad,fex,fey,fez;  
   float normal[3],e0[3],e1[3],e2[3];

   /* This is the fastest branch on Sun */
   /* move everything so that the boxcenter is in (0,0,0) */
  // SUB(v0,trivertsA,boxcenter);
  // SUB(v1,trivertsB,boxcenter);
  // SUB(v2,trivertsC,boxcenter);

   /* compute triangle edges */
   //e0[0] = v1[0] - v0[0]; e0[1] = v1[1] - v0[1]; e0[2] = v1[2] - v0[2];
   //e1[0] = v2[0] - v1[0]; e1[1] = v2[1] - v1[1]; e1[2] = v2[2] - v1[2];
   //e2[0] = v0[0] - v2[0]; e2[1] = v0[1] - v2[1]; e2[2] = v0[2] - v2[2];
   SUB(e0,v1,v0);      /* tri edge 0 */
   SUB(e1,v2,v1);      /* tri edge 1 */
   SUB(e2,v0,v2);      /* tri edge 2 */

   /* Bullet 3:  */
   /*  test the 9 tests first (this was faster) */
   fex = e0[X];//fabs(e0[X]);
   fey = e0[Y];//fabs(e0[Y]);
   fez = e0[Z];//fabs(e0[Z]);
   if(e0[X] < 0)
	   fex = -e0[X];
   if(e0[Y] < 0)
	   fey = -e0[Y];
   if(e0[Z] < 0)
	   fez = -e0[Z];
   AXISTEST_X01(e0[Z], e0[Y], fez, fey);
   AXISTEST_Y02(e0[Z], e0[X], fez, fex);
   AXISTEST_Z12(e0[Y], e0[X], fey, fex);

   fex = e1[X];//fabs(e1[X]);
   fey = e1[Y];//fabs(e1[Y]);
   fez = e1[Z];//fabs(e1[Z]);
   if(e1[X] < 0)
	   fex = -e1[X];
   if(e1[Y] < 0)
	   fey = -e1[Y];
   if(e1[Z] < 0)
	   fez = -e1[Z];
   AXISTEST_X01(e1[Z], e1[Y], fez, fey);
   AXISTEST_Y02(e1[Z], e1[X], fez, fex);
   AXISTEST_Z0(e1[Y], e1[X], fey, fex);

   fex = e2[X];//fabs(e2[X]);
   fey = e2[Y];//fabs(e2[Y]);
   fez = e2[Z];//fabs(e2[Z]);
   if(e2[X] < 0)
	   fex = -e2[X];
   if(e2[Y] < 0)
	   fey = -e2[Y];
   if(e2[Z] < 0)
	   fez = -e2[Z];
   AXISTEST_X2(e2[Z], e2[Y], fez, fey);
   AXISTEST_Y1(e2[Z], e2[X], fez, fex);
   AXISTEST_Z12(e2[Y], e2[X], fey, fex);

   /* Bullet 1: */
   /*  first test overlap in the {x,y,z}-directions */
   /*  find min, max of the triangle each direction, and test for overlap in */
   /*  that direction -- this is equivalent to testing a minimal AABB around */
   /*  the triangle against the AABB */

   /* test in X-direction */
   FINDMINMAX(v0[X],v1[X],v2[X],min,max);
   if(min>boxhalfsize[X] || max<-boxhalfsize[X]) return 0;

   /* test in Y-direction */
   FINDMINMAX(v0[Y],v1[Y],v2[Y],min,max);
   if(min>boxhalfsize[Y] || max<-boxhalfsize[Y]) return 0;

   /* test in Z-direction */
   FINDMINMAX(v0[Z],v1[Z],v2[Z],min,max);
   if(min>boxhalfsize[Z] || max<-boxhalfsize[Z]) return 0;

   /* Bullet 2: */
   /*  test if the box intersects the plane of the triangle */
   /*  compute plane equation of triangle: normal*x+d=0 */
   CROSS(normal,e0,e1);
   d=-DOT(normal,v0);  /* plane eq: normal.x+d=0 */

   if(!planeBoxOverlap(normal,d,boxhalfsize)) return 0;

   return 1;   /* box and triangle overlaps */
}
示例#8
0
bool LinkedNode::triangleIntersect(Triangle p_tri)
{
	DirectX::XMFLOAT3 boxCenter	  = div(add(m_max, m_min), 2);
	DirectX::XMFLOAT3 boxHalfSize = div(sub(m_max, m_min), 2);
	
	//Move everything so that boxCenter is in (0, 0, 0)
	DirectX::XMFLOAT3 vertex0 = sub(p_tri.vertices[0].m_position, boxCenter);
	DirectX::XMFLOAT3 vertex1 = sub(p_tri.vertices[1].m_position, boxCenter);
	DirectX::XMFLOAT3 vertex2 = sub(p_tri.vertices[2].m_position, boxCenter);
	
	//Compute triangle edges
	DirectX::XMFLOAT3 edge0 = sub(vertex1, vertex0);
	DirectX::XMFLOAT3 edge1 = sub(vertex2, vertex1);
	DirectX::XMFLOAT3 edge2 = sub(vertex0, vertex2);

	float fex = fabsf(edge0.x);
	float fey = fabsf(edge0.y);
	float fez = fabsf(edge0.z);
	if(axisTestX(edge0.z, edge0.y, fez, fey, vertex0, vertex2, boxHalfSize) == false)
		return false;
	if(axisTestY(edge0.z, edge0.x, fez, fex, vertex0, vertex2, boxHalfSize) == false)
		return false;
	if(axisTestZ(edge0.y, edge0.x, fey, fex, vertex1, vertex2, boxHalfSize) == false)
		return false;

	fex = fabsf(edge1.x);
	fey = fabsf(edge1.y);
	fez = fabsf(edge1.z);
	if(axisTestX(edge1.z, edge1.y, fez, fey, vertex0, vertex2, boxHalfSize) == false)
		return false;
	if(axisTestY(edge1.z, edge1.x, fez, fex, vertex0, vertex2, boxHalfSize) == false)
		return false;
	if(axisTestZ(edge1.y, edge1.x, fey, fex, vertex0, vertex1, boxHalfSize) == false)
		return false;

	fex = fabsf(edge2.x);
	fey = fabsf(edge2.y);
	fez = fabsf(edge2.z);
	if(axisTestX(edge2.z, edge2.y, fez, fey, vertex0, vertex1, boxHalfSize) == false)
		return false;
	if(axisTestY(edge2.z, edge2.x, fez, fex, vertex0, vertex1, boxHalfSize) == false)
		return false;
	if(axisTestZ(edge2.y, edge2.x, fey, fex, vertex1, vertex2, boxHalfSize) == false)
		return false;

	float min, max;
	findMinMax(vertex0.x, vertex1.x, vertex2.x, min, max);
	if(min > boxHalfSize.x || max < -boxHalfSize.x)
		return false;

	findMinMax(vertex0.y, vertex1.y, vertex2.y, min, max);
	if(min > boxHalfSize.y || max < -boxHalfSize.y)
		return false;

	findMinMax(vertex0.z, vertex1.z, vertex2.z, min, max);
	if(min > boxHalfSize.z || max < -boxHalfSize.z)
		return false;

	DirectX::XMFLOAT3 normal = cross(edge0, edge1);
	if(planeBoxOverlap(normal, vertex0, boxHalfSize) == false)
		return false;

	return true;
}