到飞机的距离

发布于 2024-10-07 10:54:28 字数 1120 浏览 7 评论 0原文

我编写了一个简单的小辅助方法,用于计算从点到平面的距离。然而,它似乎返回了无意义的结果。我创建飞机的代码是这样的:

Plane = new Plane(vertices.First().Position, vertices.Skip(1).First().Position, vertices.Skip(2).First().Position);

相当简单,我希望你会同意。它使用三个点创建 XNA 平面结构。

现在,在此之后我立即执行以下操作:

foreach (var v in vertices)
{
    float d = Math.Abs(v.ComputeDistance(Plane));
    if (d > Constants.TOLERANCE)
        throw new ArgumentException("all points in a polygon must share a common plane");
}

使用我用来构造平面的同一组顶点,我抛出了异常!从数学上讲这是不可能的,因为这三个点必须位于平面上。

我的ComputeDistance方法是:

public static float ComputeDistance(this Vector3 point, Plane plane)
{
    float dot = Vector3.Dot(plane.Normal, point);
    float value = dot - plane.D;
    return value;
}

据我了解,这是正确的。那么我可能做错了什么?或者我在 XNA 的实现中可能遇到错误?

一些示例数据:

Points:
{X:0 Y:-0.5000001 Z:0.8660254}
{X:0.75 Y:-0.5000001 Z:-0.4330128}
{X:-0.75 Y:-0.5000001 Z:-0.4330126}

Plane created:
{Normal:{X:0 Y:0.9999999 Z:0} D:0.5} //I believe D should equal -0.5?

Distance from point 1 to plane:
1.0

I've written a simple little helper method whoch calculates the distance from a point to a plane. However, it seems to be returning nonsensical results. The code i have for creating a plane is thus:

Plane = new Plane(vertices.First().Position, vertices.Skip(1).First().Position, vertices.Skip(2).First().Position);

Fairly simple, I hope you'll agree. It creates an XNA plane structure using three points.

Now, immediately after this I do:

foreach (var v in vertices)
{
    float d = Math.Abs(v.ComputeDistance(Plane));
    if (d > Constants.TOLERANCE)
        throw new ArgumentException("all points in a polygon must share a common plane");
}

Using the same set of vertices I used to construct the plane, I get that exception thrown! Mathematically this is impossible, since those three points must lie on the plane.

My ComputeDistance method is:

public static float ComputeDistance(this Vector3 point, Plane plane)
{
    float dot = Vector3.Dot(plane.Normal, point);
    float value = dot - plane.D;
    return value;
}

AsI understand it, this is correct. So what could I be doing wrong? Or might I be encountering a bug in the implementation of XNA?

Some example data:

Points:
{X:0 Y:-0.5000001 Z:0.8660254}
{X:0.75 Y:-0.5000001 Z:-0.4330128}
{X:-0.75 Y:-0.5000001 Z:-0.4330126}

Plane created:
{Normal:{X:0 Y:0.9999999 Z:0} D:0.5} //I believe D should equal -0.5?

Distance from point 1 to plane:
1.0

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

鸢与 2024-10-14 10:54:28

看来您的 Plane 已实现,因此 D 不是您的点之一到平面法线的投影,而是其负数。您可以将其视为将矢量从平面原点投影到法线上。

无论如何,我相信改变

float value = dot - plane.D;

应该

float value = dot + plane.D;

可以解决问题。 HTH。

It seems that your Plane is implemented so that D is not the projection of one of your points onto the plane normal, but rather the negative of this. You can think of this as projecting a vector from the plane to the origin onto the normal.

In any case, I believe that changing

float value = dot - plane.D;

to

float value = dot + plane.D;

should fix things. HTH.

负佳期 2024-10-14 10:54:28

好吧,我不完全确定我理解这里的数学,但我怀疑(基于 http 的公式://mathworld.wolfram.com/Point-PlaneDistance.html 等)

float value = dot - plane.D;

实际上应该是

float value = dot / plane.D;

编辑:好的,正如下面的评论中提到的,这不起作用。我最好的建议是查看链接或谷歌“点和平面之间的距离”,并尝试以不同的方式实现该公式。

Ok, I'm not totally sure I understand the math here, but I suspect (based on formulas from http://mathworld.wolfram.com/Point-PlaneDistance.html among others) that

float value = dot - plane.D;

should actually be

float value = dot / plane.D;

EDIT: Ok, as mentioned in comments below, this didn't work. My best suggestion then is to go look at the link or google "distance between a point and a plane" and try implementing the formula a different way.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文