|
bool InterSection(XRay& ray,XPlan& plan,XPoint& point,float & t)
{
t = 0;
/***
-D - P0 * N(A,B,C)
t = --------------------
d * N(A,B,C)
其中D 为平面方程中的D,
P0为射线的起点。而 d 为射线的方向
**/
t= -(plan.D + ray.m_Point.x * plan.A + ray.m_Point.y * plan.B + ray.m_Point.z * plan.C);
float t2 = (ray.m_Dir.x * plan.A + ray.m_Dir.y * plan.B + ray.m_Dir.z * plan.C) ;
if(t2 == 0.00)
return false;
t /= t2;
//求出交点
point = ray.m_Point + ray.m_Dir * t;
if( t < 0)
return false;
return true;
}
bool InterSection(XRay& ray,XTriangle& tri,XPoint& point,float & t)
{
XVector3D v1 = tri.m_points[1] - tri.m_points[0];
XVector3D v2 = tri.m_points[2] - tri.m_points[0];
XVector3D n = v1.cp(v2);
/**********************************************************************
ray = p0 + D * t
Tri : p1 - p3
(P2-P1) X (P3-P1) = N
N * (P - P1) = 0 ===>
N * (P0 + D * t - P1) = 0
==>
N * (P0 - P1) + N * D * t = 0;
(P1-P0) * N
t = ----------------------
D * N
******************************************************************/
t = n.dp(tri.m_points[0] - ray.m_Point);
t /= (ray.m_Dir.dp(n));
if( t < 0)
return false;
//求出交点
point = ray.m_Point + ray.m_Dir * t;
//判断点是不是在三角内部
v1 = point - tri.m_points[0];
v2 = point - tri.m_points[1];
XVector3D v3 = point - tri.m_points[2];
n = v1.cp(v2);
XVector3D n1 = v2.cp(v3);
if(n.isZero())
return true;
if(n1.isZero())
return true;
if( n.x * n1.x <= 0 )
return false;
if( n.y * n1.y <= 0 )
return false;
if( n.z * n1.z <= 0 )
return false;
return true;
}
|
|