Determining if a 3D point is in a 3D polygon

Started by
11 comments, last by Gamer Pro 9 years, 10 months ago

I you're destinations are convex then just check the point-plane distance of each polygon in the destination, and if the point is on the same side of all polygons then it's inside.

If they're concave, then the easiest is probably to divide them into convex subsections, for example with a BSP tree.

Advertisement



bool 		__fastcall IntersectedPolygon(LIGHT_TRAIL vPoly, t3dpoint vLine[2],int verticeCount)
{

Extended  MATCH_FACTOR = 0.9999999999;
 t3dpoint vNormal;
t3dpoint  vIntersection;
Extended  originDistance;
Extended  distance1;
Extended  distance2;
t3dpoint  vVector1;
t3dpoint  vVector2;
double  m_magnitude;
t3dpoint  vPoint;
t3dpoint  vLineDir;
Extended  Numerator;
Extended    Denominator;
Extended    dist;
Extended    Angle,tempangle;
t3dpoint	vA, vB;
int  i;
Extended    dotProduct;
Extended    vectorsMagnitude;

	vNormal.x = 0.0;
  vNormal.y = 0.0;
  vNormal.z = 0.0;

	originDistance = 0.0;
  distance1 = 0.0;
  distance2 = 0.0;
  vPoint.x = 0.0f;
  vPoint.y = 0.0f;
  vPoint.z = 0.0f;

  vLineDir.x = 0.0f;
  vLineDir.y = 0.0f;
  vLineDir.z = 0.0f;

	Numerator = 0.0;
  Denominator = 0.0;
  dist = 0.0;
  Angle = 0.0;




  vVector1.x = vPoly[2].x - vPoly[0].x;
	vVector1.y = vPoly[2].y - vPoly[0].y;
	vVector1.z = vPoly[2].z - vPoly[0].z;

  vVector2.x = vPoly[1].x - vPoly[0].x;
	vVector2.y = vPoly[1].y - vPoly[0].y;
	vVector2.z = vPoly[1].z - vPoly[0].z;




	vNormal.x = ((vVector1.y * vVector2.z) - (vVector1.z * vVector2.y));

	vNormal.y = ((vVector1.z * vVector2.x) - (vVector1.x * vVector2.z));

	vNormal.z = ((vVector1.x * vVector2.y) - (vVector1.y * vVector2.x));




  m_magnitude = sqrt((vNormal.x * vNormal.x) +
					  (vNormal.y * vNormal.y) +
					  (vNormal.z * vNormal.z) );



	vNormal.x = vNormal.x/m_magnitude;
	vNormal.y = vNormal.y/m_magnitude;
	vNormal.z = vNormal.z/m_magnitude;
	  if ( (IsNan(vNormal.x)== true) || (IsNan(vNormal.y)== true) || (IsNan(vNormal.z)== true) )
	  {
	  return false;
	  }
  originDistance = -1.0f * ((vNormal.x * vPoly[0].x) +
						  (vNormal.y * vPoly[0].y) +
						  (vNormal.z * vPoly[0].z));


	distance1 = ((vNormal.x * vLine[0].x)  +
				 (vNormal.y * vLine[0].y)  +
				 (vNormal.z * vLine[0].z)) + originDistance;

	distance2 = ((vNormal.x * vLine[1].x)  +
				 (vNormal.y * vLine[1].y)  +
				 (vNormal.z * vLine[1].z)) + originDistance;

	if(distance1 * distance2 >= 0.0f)
										return false;



  vLineDir.x = vLine[1].x - vLine[0].x;
	vLineDir.y = vLine[1].y - vLine[0].y;
	vLineDir.z = vLine[1].z - vLine[0].z;

  m_magnitude = sqrt((vLineDir.x * vLineDir.x) +
					  (vLineDir.y * vLineDir.y) +
					  (vLineDir.z * vLineDir.z) );


	vLineDir.x = vLineDir.x/m_magnitude;
	vLineDir.y = vLineDir.y/m_magnitude;
	vLineDir.z = vLineDir.z/m_magnitude;



	Numerator = -1.0f * (vNormal.x * vLine[0].x +
						   vNormal.y * vLine[0].y +
							 vNormal.z * vLine[0].z + originDistance);




	Denominator = ( (vNormal.x * vLineDir.x) + (vNormal.y * vLineDir.y) + (vNormal.z * vLineDir.z) );

	if( Denominator == 0.0f)    {
	vIntersection = vLine[0];
	temp_intersect_v = vIntersection;
	} else	{


	dist = Numerator / Denominator;



	vPoint.x = (vLine[0].x + (vLineDir.x * dist));
	vPoint.y = (vLine[0].y + (vLineDir.y * dist));
	vPoint.z = (vLine[0].z + (vLineDir.z * dist));

  temp_intersect_v = vPoint;								// Return the intersection point
}

//temp_intersect_v.x := vIntersection.x;
//temp_intersect_v.y := vIntersection.y;
//temp_intersect_v.z := vIntersection.z;
//temp_intersect_v2 :=  vPoint;
//CLIP_INTERSECT_POS := temp_intersect_v;

for (i = 0; i < verticeCount; i++) {


	vA.x = vPoly[i].x - temp_intersect_v.x;

	  vA.y = vPoly[i].y - temp_intersect_v.y;

	  vA.z = vPoly[i].z - temp_intersect_v.z;


	vB.x = vPoly[(i + 1) % verticeCount].x - temp_intersect_v.x;

	  vB.y = vPoly[(i + 1) % verticeCount].y - temp_intersect_v.y;

	  vB.z = vPoly[(i + 1) % verticeCount].z - temp_intersect_v.z;


	dotProduct = ( (vA.x * vB.x) +
					(vA.y * vB.y) +
					(vA.z * vB.z) );


	  vectorsMagnitude = sqrt(
					 (vA.x * vA.x) +
					 (vA.y * vA.y) +
					 (vA.z * vA.z)
						  )
						  *
					 sqrt(
					 (vB.x * vB.x) +
					 (vB.y * vB.y) +
					 (vB.z * vB.z)
						  );

	 tempangle = ArcCos( dotProduct / vectorsMagnitude );


	  if(IsNan(tempangle) == true)		tempangle = 1990.0f;



	  Angle = Angle + tempangle;
}



	if(Angle >= (MATCH_FACTOR * (2.0f * 3.140f)) )   return true;





return false;
}

Thanks WiredCat, I'll give your code a try but I don't understand what it's doing.

This topic is closed to new replies.

Advertisement