使用Newell算法计算3d多边形的面法线的问题 [英] Issue with calculating 3d polygon's face normal using Newell's algorithm
问题描述
我正在尝试使用Newell的方法来计算3D多边形的法线.我面临的问题是,即使多边形朝向-z,z的值也总是返回正数.但是,当它面对-z时,它也会同时翻转x和y的值,因此,如果x的实际值是-x,它将是+ x,并且y也是如此.我不知道为什么会这样.我希望有人能指出我做错了什么.这是我到目前为止(使用PHP)编写的内容:
I'm trying to calculate 3D polygon's normal using Newell's approach. The problem I'm facing is that the value of z is always returned in positive even though the polygon is facing -z. However, when it's facing -z it also flips the values of x and y as well so if actual value of x is -x it'll be +x and same goes for the y. I can't figure out why that is happening. I hope someone can point out what I'm doing wrong. Here's what I wrote so far (using PHP):
$verticies =
[
[57.36, 30.98, 0.0],
[52.57, 39.04, 2.76],
[58.00, 38.33, 10.50],
[59.89, 31.16, 4.77],
[62.28, 30.75, 8.01],
[64.70, 26.11, 6.46],
[64.90, 21.54, 1.21]
];
for ($i = 0; $i < count($verticies); $i++)
{
//current vertex
$pi = $verticies[$i];
//next vertex
$pj = $verticies[($i+1) % count($verticies)];
//0 = x, 1 = y, 2 = z
$nx += ((($pi[2]) + ($pj[2])) * (($pj[1]) - ($pi[1])));
$ny += ((($pi[0]) + ($pj[0])) * (($pj[2]) - ($pi[2])));
$nz += ((($pi[1]) + ($pj[1])) * (($pj[0]) - ($pi[0])));
}
echo $nx.', '.$ny.', '.$nz;
//Current Result = -192.665, -145.6139, 115.1547
//Expected Result = -192.665, -145.6139, -115.1547
任何帮助将不胜感激.谢谢...
Any help would be appreciated. Thanks...
推荐答案
此代码给予 0, 2, -2
矩形
[
[0, 0, 0],
[1, 0, 0],
[1, 1, 1],
[0, 1, 1],
];
和0, -2, 2
用于具有颠倒顶点顺序的矩形,所以我认为代码是正确的
and 0, -2, 2
for rectangle with reversed vertex order, so I think code is right
这篇关于使用Newell算法计算3d多边形的面法线的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!