Retrieve 3 Euler angles from 2 Vector3D

拟墨画扇 提交于 2019-12-11 05:15:29

问题


How can i retrieve the 3 euler angles from 2 vector3D ?

Thanks

Cedre

dim vector1 = new Vector3D(0, 0, 1);

dim vector2 = new Vector3D(0.33, 0.45, 0.49);


dim myEuler = GetEulerFrom2Vector(vector1,vector2); // ?????

I work in a right angle coordinate system and i use the ZYX euler convention


回答1:


Can we assume the two vectors are perpendicular to each other vector1.Dot(vector2)==0? If yes then find the third vector to form a coordinate system

vector1 = vector1.Normalized();
vector2 = vector2.Normalized();
vector3 = VectorCross(vector1,vector2).Normalized();

where VectorCross is the 3D vector cross product, and Normalized() returns a unit vector.

Now your rotation matrix E is

 | vector1.x   vector2.x   vector3.x |
 | vector1.y   vector2.y   vector3.y |
 | vector1.z   vector2.z   vector3.z |

Now you can go from the rotation matrix to Euler angles using the instructions here.

PS. If vector2 is not perpendicular to vector1 you can make it perpendicular by vector2 = CrossProduct(vector3, vector1).Normalized() after vector3 is calculated.

Here is the code I use to go from two axes to the rotation matrix:

    public static mat3 AlignZX(vec3 unit_z, vec3 unit_x)
    {
        unit_x=unit_x.Normalized();
        unit_z=unit_z.Normalized();
        vec3 unit_y=unit_z.Cross(unit_x);
        unit_x=unit_y.Cross(unit_z);
        return mat3.Combine(unit_x, unit_y, unit_z);
    }
    public static mat3 AlignXY(vec3 unit_x, vec3 unit_y)
    {
        unit_x=unit_x.Normalized();
        unit_y=unit_y.Normalized();
        vec3 unit_z=unit_x.Cross(unit_y);
        unit_y=unit_z.Cross(unit_x);
        return mat3.Combine(unit_x, unit_y, unit_z);
    }
    public static mat3 AlignYZ(vec3 unit_y, vec3 unit_z)
    {
        unit_y=unit_y.Normalized();
        unit_z=unit_z.Normalized();
        vec3 unit_x=unit_y.Cross(unit_z);
        unit_z=unit_x.Cross(unit_y);
        return mat3.Combine(unit_x, unit_y, unit_z);
    }



回答2:


I use a rotation matrix :

R11 R12 R13
R21 R22 R23
R31 R32 R33

with R = Rz Ry Rx

if (R31 <> ±1)
    y1 = -sin-1(R31)
    y2 = pi + sin-1(R31)

    x1 = atan2 (R32/cos y1,R33/cos y1)
    x2 = atan2 (R32/cos y2,R33/cos y2)

    z1 = atan2( R21/cos y1,R11/cos y1)
    z2 = atan2( R21/cos y2,R11/cos y2)
        Else
    z= anything; can set to 0
    if (R31 = -1)
        y = -pi / 2
        x = z  + atan2(R12,R13)
     Else
         y = -pi / 2
         x  = -z + atan2(-R12,-R13)     
    End If
End If

https://truesculpt.googlecode.com/hg-history/38000e9dfece971460473d5788c235fbbe82f31b/Doc/rotation_matrix_to_euler.pdf

or a simple version

    result.X = Math.Atan2(R32, R33) * (180.0 / Math.PI)
    result.Y = Math.Atan2(-1 * R31, Math.Sqrt(R32 * R32 + R33 * R33)) * (180.0 / Math.PI)
    result.Z = Math.Atan2(R21, R11) * (180.0 / Math.PI)


来源:https://stackoverflow.com/questions/15618304/retrieve-3-euler-angles-from-2-vector3d

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!