空间平面方程 java,确定三角形和平面的交点

这是一些建议的伪代码 . 首先是简单版本,后面是更强大的版本(只是为了帮助将原理与联盟分开) . 简单版本:

// 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,并为顶点参与的每个边缘检索它 . 还可以有更高级的缓存,取决于您的方案和数据表示 .

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值