计算两个 3D 三角形是否在同一平面上
对于我正在开发的 3D 游戏引擎,我需要计算两个 3D 三角形是否在同一平面上才能相应地显示它。如何计算 3D 空间中三角形的角度?
计算表面法线并比较它们是否会给出 2 个等效法线?
For a 3D game engine I'm working on I need to calculate if two 3D triangles are on the same plane to display it accordingly. How do I calculate the angles of a triangle in 3D space?
Would calculating a surface normal and comparing those ever give me 2 equivalent normals?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你为什么要这么做?您希望测试的三角形数量是多少?对于实时渲染算法来说似乎有点复杂!
无论如何:
计算三角形的法线
n
。然后计算平面方程:ax + by + cz + d = 0
,其中(a,b,c)
为三角形法线,d = - dot(n,P)
代码>(P是三角形顶点之一)。对第二个三角形执行相同的操作。如果四个值
abcd
相等或相反(全部在一起),则两个平面相同。Why do you want to do that? What is the number of triangle you expect to test? It seems a little bit complex for a real time rendering algorithm!
Anyway:
Compute the normal
n
of the triangle. Then compute the plane equation:a.x + b.y + c.z + d = 0
with(a,b,c)
being your triangle normal andd = - dot(n,P)
(P is one of your triangle vertex). Do the same for the second triangle.The two planes are the same if the four values
abcd
are equals or opposite (all together).你所问的问题在数字上是不可能的。舍入误差将使此类测试完全无关。但是,您可能想要测试“两个三角形是否在同一平面上,且在一定的公差范围内”。这很难做到,而且在这里,舍入误差也可能会扰乱任何可能的方法。事实上,只要三角形很薄,它们所在的平面就有很大的不确定性。
如果您确实想要的话,我可以向您推荐一些文献(您最好的选择是查看 CGAL 库看看他们是否实施了与您的问题相关的措施)。任何事情都可能涉及任意精度的浮点、巧妙的运算重新排序,并且无论如何都会导致不精确的结果。
因此,我强烈建议您为实际问题找到另一种方法。如果您尝试计算通过三个点的平面方程,然后测试其他三个点,则舍入误差是一个(巨大)问题。还有另一种解决方案。
您可能想要计算六个点的惯性矩阵,将其对角化并查看其是否最小特征值在其他两个特征值的某个微小值之内。这意味着您的六个点实际上位于同一平面上,且在公差范围内。
What you are asking is impossible numerically. Roundoff errors will make such a test completely irrelevant.However, you may want to test "if two triangles are on the same plane, within some tolerance". This is very difficult to do, and here too, roundoff errors will likely mess up any method possible. Indeed, whenever the triangles are thin, there is a great incertitude about the plane on which they live.
I could point you to some litterature if you really want (your best bet would be to look at the CGAL library and see if they implemented something relevant to your problem). Anything will likely involves arbitrary precision floating points, clever reordering of operations, and will anyway lead to unprecise results.
I thus strongly suggest you find another approach for your actual problem.Roundoff errors are a (huge) problem if you try to compute the equation of the plane passing through three points and then testing the three other ones. There is another solution.
You may want to compute the inertia matrix of your six points, diagonalize it and see if its smallest eigenvalue is within some tiny value of the two other ones. This will imply that your six points actually lie on the same plane, within a tolerance.