这里有一些建议的伪代码。首先是简单的版本,后来更强大的版本(只是为了帮助将原理与细微差别分开)。 简单的版本:
// Assume the plane is given as the equation dot(N,X) + d = 0, where N is a (not
// neccessarily normalized) plane normal, and d is a scalar. Any way the plane is given -
// DistFromPlane should just let the input vector into the plane equation.
vector3d planeN;
float planeD;
float DistFromPlane(vector3d P)
{
// if N is not normalized this is *not* really the distance,
// but the computations work just the same.
return dot(planeN,P) + planeD;
}
bool GetSegmentPlaneIntersection(vector3d P1, vector3d P2, vector3d& outP)
{
float d1 = DistFromPlane(P1),
d2 = DistFromPlane(P2);
if (d1*d2 > 0) // points on the same side of plane
return false;
float t = d1/(d1 - d2); // 'time' of intersection point on the segment
outP = P1 + t * (P2 - P1);
return true;
}
void TrianglePlaneIntersection(vector3d triA, vector3d triB, vector3d triC,
vector3dArray& outSegTips)
{
vector3d IntersectionPoint;
if(GetSegmentPlaneIntersection(triA, triB, IntersectionPoint))
outSegTips.Add(IntersectionPoint);
if(GetSegmentPlaneIntersection(triB, triC, IntersectionPoint))
outSegTips.Add(IntersectionPoint);
if(GetSegmentPlaneIntersection(triC, triA, IntersectionPoint))
outSegTips.Add(IntersectionPoint);
}
现在增加了一些稳健性:
[编辑:增加了明确的考虑单个顶点上飞机的情况下],让一个想法
vector3d planeN;
float planeD;
float DistFromPlane(vector3d P)
{
return dot(planeN,P) + planeD;
}
void GetSegmentPlaneIntersection(vector3d P1, vector3d P2, vector3dArray& outSegTips)
{
float d1 = DistFromPlane(P1),
d2 = DistFromPlane(P2);
bool bP1OnPlane = (abs(d1) < eps),
bP2OnPlane = (abs(d2) < eps);
if (bP1OnPlane)
outSegTips.Add(P1);
if (bP2OnPlane)
outSegTips.Add(P2);
if (bP1OnPlane && bP2OnPlane)
return;
if (d1*d2 > eps) // points on the same side of plane
return;
float t = d1/(d1 - d2); // 'time' of intersection point on the segment
outSegTips.Add(P1 + t * (P2 - P1));
}
void TrianglePlaneIntersection(vector3d triA, vector3d triB, vector3d triC,
vector3dArray& outSegTips)
{
GetSegmentPlaneIntersection(triA, triB, outSegTips));
GetSegmentPlaneIntersection(triB, triC, outSegTips));
GetSegmentPlaneIntersection(triC, triA, outSegTips));
RemoveDuplicates(outSegTips); // not listed here - obvious functionality
}
希望,但有仍然有很多潜在的优化。例如,如果您计算大网格中每个三角形的这些交点,则可以计算并缓存每个顶点的DistanceFromPlane,并只为顶点参与的每条边检索它。还可以有更高级的缓存,取决于您的场景和数据表示。
非常感谢,这解释了奇妙 – Martin 2010-06-29 20:06:25
我认为应该是p1 + t *(p2 - p1);而不是你有什么? – Martin 2010-06-29 20:35:01
谢谢!另一个错字也是固定的。 – 2010-06-30 05:19:09