射线和平面的交点

1.1 程序算法:

/** Tests whether this ray intersects the given plane.

                   @returns A pair structure where the first element indicates whether

                            an intersection occurs, and if true, the second element will

                            indicate the distance along the ray at which it intersects.

                            This can be converted to a point in space by calling getPoint().

                   */

//-----------------------------------------------------------------------

    std::pair<bool, Real> Math::intersects(const Ray& ray, const Plane& plane)

    {

        Real denom = plane.normal.dotProduct(ray.getDirection());

平面法向量和射线的方向点乘,= |ray.getDir()|*cosa;(normal为单位向量)

射线在平面法向量上的分量标量长度

        if (Math::Abs(denom) < std::numeric_limits<Real>::epsilon())

        {

            // Parallel

            return std::pair<bool, Real>(false, 0);

        }

        else

        {

            Real nom = plane.normal.dotProduct(ray.getOrigin()) + plane.d

Cosa = d/t è t = d/cosa , 由此可以判断:nom为起点到平面的距离ddenomcosa

plane.normal.dotProduct(ray.getOrigin())为向量(射线起点)在平面法向量上的投影

            Real t = -(nom/denom);

            return std::pair<bool, Real>(t >= 0, t);

        }

}

1.2 理论分析

 

 

射线的表示方法

 

p(t) = p0 + tu;

其中p0是射线的起始点,u是射线的方向向量(需要单位化),t[0+无穷]

 

面的表示方法

 

由上图可以知道

n(p-p0) = 0;

d = -np0   由此可以知道,d的几何意义

所以有:np+d = 0;为平面方程,其中p为平面上的任意一点,n为平面的单位法向量,

如果空间中一点Px,如果nPx + d = 0,那么Px在(np+d=0)表示的平面上,如果大于0,该点在平面的上面(法线所指的方向),如果小于0,该点在平面的下面。

 

3.求射线和平面的交点

 

要求利用上面所介绍的射线和面的交点只要把p(t)代入(np+d=0)这个方程,这样我们就得到

np(t)+d=0;

n(p0+tu)+d=0;

np0+tnu+d=0;

t=(-d-np0)/nu;

如果t大于等于0说明射线和面有交点,根据p(t)=p0+tu;

 

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值