本文介绍了围绕其中心点/轴旋转3D形状的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在STL文件中定义了3D形状,并且我想使用偏航角,俯仰和横滚来围绕其轴/中心旋转。但是,我已经实现了一个解决方案,但由于它绕原点轴旋转3D形状,因此无法正常工作。这是我到目前为止所做的(用PHP编写):

I have a 3D shape defined in STL file and I'd like to rotate it around its axis/center using Yaw, Pitch and Roll. However, I've already implemented a solution and it doesn't work as expected as it rotates the 3D shape around the origin axis instead. Here's what I've done so far (written in PHP):

function rotate($vertices, $roll = 0, $pitch = 0, $yaw = 0)
{
    if(!empty($vertices))
    {
        $cosa = cos($yaw);
        $sina = sin($yaw);

        $cosb = cos($pitch);
        $sinb = sin($pitch);

        $cosc = cos($roll);
        $sinc = sin($roll);

        $Axx = $cosa * $cosb;
        $Axy = $cosa * $sinb * $sinc - $sina * $cosc;
        $Axz = $cosa * $sinb * $cosc + $sina * $sinc;

        $Ayx = $sina * $cosb;
        $Ayy = $sina * $sinb * $sinc + $cosa * $cosc;
        $Ayz = $sina * $sinb * $cosc - $cosa * $sinc;

        $Azx = -$sinb;
        $Azy = $cosb * $sinc;
        $Azz = $cosb * $cosc;

        //loop through all triangles
        foreach($vertices as $i => $vertex)
        {
            $px = $vertex->x;
            $py = $vertex->y;
            $pz = $vertex->z;

            $points =
            [
                'x' => ($Axx * $px + $Axy * $py + $Axz * $pz),
                'y' => ($Ayx * $px + $Ayy * $py + $Ayz * $pz),
                'z' => ($Azx * $px + $Azy * $py + $Azz * $pz)
            ];

            //update the vertex
            $vertices[$i]->setVertex($vi, $points);
        }
    }

    return $vertices;
}

请让我知道我是否缺少某些东西。

Please let me know if I'm missing something. any help would be appreciated.

推荐答案

上面的代码围绕原点旋转。如果旋转中心(例如$ c)不是原点,则可以在旋转之前移动对象:

The code above rotates about the origin. If your centre of rotation (say, $c), isn't the origin, you can move the object before rotating:

$px = $vertex->x - $c->x;
$py = $vertex->y - $c->y;
$pz = $vertex->z - $c->z;

旋转后,将点移回旋转中心:

After rotating, move the point back to the centre of rotation:

$points =
[
    'x' => ($Axx * $px + $Axy * $py + $Axz * $pz) + $c->x,
    'y' => ($Ayx * $px + $Ayy * $py + $Ayz * $pz) + $c->y,
    'z' => ($Azx * $px + $Azy * $py + $Azz * $pz) + $c->z
];

这篇关于围绕其中心点/轴旋转3D形状的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 12:51