求一个三维几何算法

jk2004 2009-11-06 09:47:45
一条线段,一个凸六面体(8个顶点),从线段的一端依次求出和六面体相交的两个交点。最好能有C++的代码,请各位达人不吝赐教。
...全文
204 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
jk2004 2009-11-09
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 litaoye 的回复:]
算两个轴的投影交点就行了
[/Quote]

不好意思,周末同学聚会没有来关注回复。这个不是很明白,能不能具体说明一下,谢谢!
xingzhe2001 2009-11-06
  • 打赏
  • 举报
回复
如果只是6面体还好,d3d的sample pick里有段代码

//--------------------------------------------------------------------------------------
// Given a ray origin (orig) and direction (dir), and three vertices of a triangle, this
// function returns TRUE and the interpolated texture coordinates if the ray intersects
// the triangle
//--------------------------------------------------------------------------------------
bool IntersectTriangle( const D3DXVECTOR3& orig, const D3DXVECTOR3& dir,
D3DXVECTOR3& v0, D3DXVECTOR3& v1, D3DXVECTOR3& v2,
FLOAT* t, FLOAT* u, FLOAT* v )
{
// Find vectors for two edges sharing vert0
D3DXVECTOR3 edge1 = v1 - v0;
D3DXVECTOR3 edge2 = v2 - v0;

// Begin calculating determinant - also used to calculate U parameter
D3DXVECTOR3 pvec;
D3DXVec3Cross( &pvec, &dir, &edge2 );

// If determinant is near zero, ray lies in plane of triangle
FLOAT det = D3DXVec3Dot( &edge1, &pvec );

D3DXVECTOR3 tvec;
if( det > 0 )
{
tvec = orig - v0;
}
else
{
tvec = v0 - orig;
det = -det;
}

if( det < 0.0001f )
return FALSE;

// Calculate U parameter and test bounds
*u = D3DXVec3Dot( &tvec, &pvec );
if( *u < 0.0f || *u > det )
return FALSE;

// Prepare to test V parameter
D3DXVECTOR3 qvec;
D3DXVec3Cross( &qvec, &tvec, &edge1 );

// Calculate V parameter and test bounds
*v = D3DXVec3Dot( &dir, &qvec );
if( *v < 0.0f || *u + *v > det )
return FALSE;

// Calculate t, scale parameters, ray intersects triangle
*t = D3DXVec3Dot( &edge2, &qvec );
FLOAT fInvDet = 1.0f / det;
*t *= fInvDet;
*u *= fInvDet;
*v *= fInvDet;

return TRUE;
}


可以求出射线和三角形的交点,你遍历所有三角形就可以得到和6面体的交点
绿色夹克衫 2009-11-06
  • 打赏
  • 举报
回复
算两个轴的投影交点就行了
  • 打赏
  • 举报
回复
叉乘,听说的。
Paradin 2009-11-06
  • 打赏
  • 举报
回复
看计算几何
donkey301 2009-11-06
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 litaoye 的回复:]
算两个轴的投影交点就行了
[/Quote]
想了一下明白了,很不错的方法,而且求一条轴的交点可以用在两个面上。
donkey301 2009-11-06
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 litaoye 的回复:]
算两个轴的投影交点就行了
[/Quote]
这个想象不出应该怎么求

33,028

社区成员

发帖
与我相关
我的任务
社区描述
数据结构与算法相关内容讨论专区
社区管理员
  • 数据结构与算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧