由具有整数坐标的 3 个点定义的平面的明确可哈希表示

发布于 2025-01-12 15:46:11 字数 283 浏览 3 评论 0原文

我需要能够使用平面作为哈希图中的键。这些平面由 3d 空间中的三个不同点定义。我一直无法找到平面的表示,无论使用平面上的哪些点来构造它,它都是相同的,因此可以对它进行哈希处理。平面方程已被淘汰,因为它会导致使用不精确的浮点。

可以在没有这种表示的情况下实现相等比较,但理想情况下,不需要特殊的逻辑。

问题是如何获得由三个点定义的平面的明确的可散列表示,这三个点具有整数坐标;或者失败了这样一个平面的哈希函数。代表同一平面的其他三个点应该具有相同的哈希码。

我正在使用 Rust,但伪代码没问题。性能是一个问题。

I need to be able to use planes as keys in a hash map. The planes are defined by three distinct points in 3d space. I have been unable to find a representation for a plane, that is the same no matter which points on it are used to construct it, so it can be hashed. The plane equation is out since it would lead to the usage of floating points, which are not precise.

Equality comparison can be implemented without such representation but ideally, there should be no need for special logic.

The question is how to obtain an unambiguous hashable representation for a plane defined by three points, which have integer coordinates; or failing that a hash function for such a plane. Other three points that represent the same plane should have the same hashcode.

I am using rust, but pseudo-code is ok. Performance is a concern.

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

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

发布评论

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

评论(1

舟遥客 2025-01-19 15:46:11

有了三个点P0、P1、P2,我们可以使用两个向量

P10 = P1 - P0
P20 = P2 - P0

并使用向量积得到法线:

N = P10 x P20

N 个分量是整数。然后计算Nx、Ny、Nz的GCD(最大公约数)并除以使它们互质(如果这个三者适用于三个值,也许“不可约”)

G = GCD(GCD(Nx, Ny), Nz)
A = Nx / G   //integer division
B = Ny / G
C = Nz / G

,最后找到第4个分量一般平面方程的任意点分量(例如P0)代入方程

A * P0x + B * P0y + C * P0z + D = 0
D = - (A * P0x + B * P0y + C * P0z)

肯定D是整数,所有系数互质/不可约,所以元组(A,B,C,D) 是一种“归一化”平面方程。

该方程和任何具有比例系数的方程都表示同一平面,并且可用于检查某个点 P 是否属于给定平面。

if (A * Px + B * Py + C * Pz + D == 0)...

Having three points P0, P1, P2, we can use two vectors

P10 = P1 - P0
P20 = P2 - P0

and get normal using vector product:

N = P10 x P20

N components are integers. Then calculate GCD (greatest common divisor) of Nx, Ny, Nz and divide components to make them mutually prime (if this ter, is applicable to three values, perhaps "irreducible")

G = GCD(GCD(Nx, Ny), Nz)
A = Nx / G   //integer division
B = Ny / G
C = Nz / G

and finally find 4th component of general plane equation substituting any point components (say P0) into equation

A * P0x + B * P0y + C * P0z + D = 0
D = - (A * P0x + B * P0y + C * P0z)

Definitely D is integer, all coefficients are mutual prime/irreducible, so tuple (A,B,C,D) is a kind of "normalized" plane equation.

Both this equation and any equation with proportional coefficients represents the same plane and might be used to check if some point P belongs to given plane.

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