使用 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):
$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++){//当前顶点$pi = $verticies[$i];//下一个顶点$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])));}回声 $nx.', '.$ny.', '.$nz;//当前结果 = -192.665, -145.6139, 115.1547//预期结果 = -192.665, -145.6139, -115.1547
任何帮助将不胜感激.谢谢...
此代码 gives 0, 2, -2
为矩形
和 0, -2, 2
用于顶点顺序颠倒的矩形,所以我认为代码是正确的
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...
This code gives 0, 2, -2
for rectangle
[
[0, 0, 0],
[1, 0, 0],
[1, 1, 1],
[0, 1, 1],
];
and 0, -2, 2
for rectangle with reversed vertex order, so I think code is right
这篇关于使用 Newell 算法计算 3d 多边形面法线的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!