这个平面射线相交代码正确吗? [英] Is this plane-ray intersection code correct?

查看:85
本文介绍了这个平面射线相交代码正确吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Plane类有两个字段:

My Plane class has two fields:

public Vector3 Norm; //normal vector 
public double Offset; //signed distance to origin

这是我用于交集的代码,我不知道它是否正确.我加倍 检查了我的方程式和所有内容,但我想从人们那里得到反馈 对此更有经验.

This is the code I use for intersection, and I don't know if it is correct. I double checked my equations and everything, but I would like to get feedback from people more experienced with this.

public override Intersection Intersect(Ray ray)
{
    // Create Intersection.
    Intersection result = new Intersection();

    // Find t.
    double t = - (Vector3.Dot(Norm,ray.Start) + Offset) / (Vector3.Dot(Norm, ray.Dir));
    if (t < 0) // the ray does not hit the surface, that is, the surface is "behind" the ray
        return null;

    // Get a point on the plane.
    Vector3 p = ray.Start + t * ray.Dir;

    // Does the ray intersect the plane inside or outside?
    Vector3 planeToRayStart = ray.Start - p;
    double dot = Vector3.Dot (planeToRayStart, Norm);
    if (dot > 0) {
        result.Inside = false;
    } else {
        result.Inside = true;
    }

    result.Dist = t;
    return result;
}

此外,如果t接近0,我不确定该怎么办?我应该检查epsilon以及多大 应该是ε吗?另外,我不确定是否可以正确检查射线是否与平面相交 从内还是外?

Also, I am not sure what to do if t is close to 0? Should I check with epsilon and how big should epsilon be? Also I am not sure if I check correctly whether the ray intersects the plane from in or out?

谢谢

推荐答案

您的代码看起来基本上没问题,请参见

You're code looks mostly fine, see this slide. Since your plane is essentially defined as the set of rays which start at a particular point (which is encapsulate in the offset parameter) and are orthogonal to the normal vector, you just need to plug in the definition for a point on the viewing ray in order to determine which point on the viewing ray defines such an orthogonal ray.

问题将在于您的视线是否在飞机上.在这种情况下,视线和法线将是正交的,因此它们的点积将为0,并且您将获得除以0的异常.您需要事先检查:如果观察向量和法线的点积为0,则意味着观察光线与平面平行,因此不存在相交或存在无限数量的相交(射线在平面内).无论哪种方式,我认为对于光线追踪,您通常只会说没有交点(即,没有要渲染的点),因为在后一种情况下,您正在看的是正好正对着二维平面,因此您不会什么都没看到.

The problem will be if your viewing ray is in the plane. In this case, the viewing ray and the normal ray will be orthogonal so their dot product will be 0 and the you'll get a divide-by-0 exception. You'll need to check for that in advance: if the dot product of the viewing vector and the normal is 0, then it means the viewing ray is parallel to the plane so there either is no intersection, or there are an infinite number of intersections (the ray is inside the plane). Either way, I think for ray tracing you would generally just say there's no point of intersection (i.e., nothing to render), because in the latter case, you're looking at a two-dimensional flat surface exactly head on, so you wouldn't see anything.

我没有看到您需要专门处理接近0"的任何原因.光线与平面平行,没有任何要渲染的光线,或者不是,并且它与平面恰好在一个点处相交.最终您将遇到浮点取整错误,但这只是渲染中的一小部分错误,无论如何它仅仅是场景的近似值.只要您保持适当的尺寸,浮点误差就不会太大.

I don't see any reason you need to specially handle "close to 0". Either the ray is parallel to the plane and there's nothing to render, or it's not and it intersects the plane at exactly one point. You'll get into floating point rounding errors eventually, but this is just a small source of error in your rendering which is only an approximation of the scene anyway. As long as you keep your dimensions reasonably large, the floating point errors should be insubstantial.

这篇关于这个平面射线相交代码正确吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆