射线平面相交:不准确的结果-舍入误差

Ray-plane intersection: inaccurate results - rounding errors?

本文关键字:结果 舍入误差 不准确 平面 面相      更新时间:2023-10-16

我已经使用c++和OpenGL设置了一个简单的3d第一人称演示,它似乎工作得相当好。我的目标是这样的:当用户将相机指向一个平面并点击鼠标左键时,我想要画出从玩家位置与该平面出发,指向相机所面对方向的光线的交点。

那么,我从两个向量开始,Vector positionVector rotation,其中Vector是一个非常标准的三维向量类:

class Vector
{
  public:
    GLfloat x, y, z;
    Vector() {};
    Vector(GLfloat x, GLfloat y, GLfloat z)
    {
      this->x = x;
      this->y = y;
      this->z = z;
    }
    GLfloat dot(const Vector &vector) const
    {
      return x * vector.x + y * vector.y + z * vector.z;
    }
    ... etc ...

Plane p, Plane是一个简单的结构体,存储平面和d的法线。我直接从christerickson的书"实时碰撞检测"中复制了这个结构体:

struct Plane 
{
  Vector n; // Plane normal. Points x on the plane satisfy Dot(n,x) = d
  float d; // d = dot(n,p) for a given point p on the plane
};

首先,我取position作为射线的起点,我称之为a。我用这个点和rotation找到射线的终点b。然后我用一种算法来求出同一本书中射线与平面的交点。实际上我自己也实现了相同的方法,但我直接使用了书中的代码,以确保我没有搞砸任何东西:

void pickPoint()
{
    const float length = 100.0f;
    // Points a and b
    Vector a = State::position;
    Vector b = a;
    // Find point b of directed line ab
    Vector radians(Math::rad(State::rotation.x), Math::rad(State::rotation.y), 0);
    const float lengthYZ = Math::cos(radians.x) * length;
    b.y -= Math::sin(radians.x) * length;
    b.x += Math::sin(radians.y) * lengthYZ;
    b.z -= Math::cos(radians.y) * lengthYZ;
    // Compute the t value for the directed line ab intersecting the plane
    Vector ab = b - a;
    GLfloat t = (p.d - p.n.dot(a)) / p.n.dot(ab);
    printf("Plane normal: %f, %f, %fn", p.n.x, p.n.y, p.n.z);
    printf("Plane value d: %fn", p.d);
    printf("Rotation (degrees): %f, %f, %fn", State::rotation.x, State::rotation.y, State::rotation.z);
    printf("Rotation (radians): %f, %f, %fn", radians.x, radians.y, radians.z);
    printf("Point a: %f, %f, %fn", a.x, a.y, a.z);
    printf("Point b: %f, %f, %fn", b.x, b.y, b.z);
    printf("Expected length of ray: %fn", length);
    printf("Actual length of ray: %fn", ab.length());
    printf("Value t: %fn", t);
    // If t in [0..1] compute and return intersection point
    if(t >= 0.0f && t <= 1.0f) 
    {
        point = a + t * ab;
        printf("Intersection: %f, %f, %fn", point.x, point.y, point.z);
    }
    // Else no intersection
    else
    {
        printf("No intersection foundn");
    }
    printf("nn");
}

当我用OpenGL渲染这个点时,它看起来非常接近光线和平面的交点。但是从打印出的实际值中,我发现对于特定的位置和旋转,交点最多可以偏离0.000004。这里有一个交点不准确的例子——我知道交点不在平面上,因为它的Y值应该是0,而不是0.000002。我也可以把它代入平面方程得到不等式

Plane normal: 0.000000, 1.000000, 0.000000
Plane value d: 0.000000
Rotation (degrees): 70.100044, 1.899823, 0.000000
Rotation (radians): 1.223477, 0.033158, 0.000000
Point a: 20.818802, 27.240383, 15.124892
Point b: 21.947229, -66.788452, -18.894285
Expected length of ray: 100.000000
Actual length of ray: 100.000000
Value t: 0.289702
Intersection: 21.145710, 0.000002, 5.269455

现在,我知道浮点数只是实数的近似值,所以我猜这种不准确性只是浮点舍入的影响,尽管我可能在代码的其他地方犯了错误。我知道交点只偏离了极少量,但我仍然关心它,因为我计划使用这些点来定义模型或关卡的顶点,通过将它们捕捉到任意方向的网格,所以我实际上希望这些点在网格上,即使它们有点不准确。这可能是一种被误导的方法——我真的不知道。

所以我的问题是:这个不准确只是浮点舍入工作,还是我在其他地方犯了错误?

如果它只是浮点四舍五入,有什么方法来处理它吗?我尝试过以各种方式四舍五入旋转和位置向量的值,这显然会导致不太准确的交点,但我有时仍然会得到不在平面上的交点。我确实读过一个类似问题的答案(这个平面射线相交代码是正确的吗?)提到保持尺寸大,但我不确定这到底是什么意思。

抱歉,如果这个问题之前已经问过了-我搜索了,但我没有看到任何东西,这是我遇到的麻烦。谢谢!

你的数学似乎是正确的,这显然看起来像一个舍入误差。我有一种强烈的感觉,就是这一行:

GLfloat t = (p.d - p.n.dot(a)) / p.n.dot(ab);

也就是说,我没有看到任何其他方法来计算t。你可以通过使用"%"来验证你是否失去了精度。在您的printf语句中使用12f"(或更多)。另一种找出问题的方法是一步一步地进行t计算,并在此过程中打印结果,看看是否在某处失去了精度。

你是否尝试过使用双精度浮点数,如果精度对你来说真的那么重要的话?