简体   繁体   English

在3D空间中旋转

[英]Rotations in 3D space

For a project I'm working on, I need to write a function that takes as input two points in 3D space that form a line, and gives as its output a rotation matrix that will make the line parallel to the Z axis. 对于我正在从事的项目,我需要编写一个函数,该函数以3D空间中形成一条线的两个点作为输入,并提供一个旋转矩阵作为其输出,该旋转矩阵使该线与Z轴平行。

I've learned enough about rotation matrices to know how to compose them from Euler angles. 我已经对旋转矩阵学到了足够的知识,知道如何从Euler角度进行组合。 But I'm flummoxed on how to figure out the correct angles of the input line. 但是我对如何弄清楚输入线的正确角度感到困惑。

Could anyone please offer some advice? 有人可以提供一些建议吗? I'm writing this in Matlab but a theoretical treatment would be more valuable, I think. 我是用Matlab写的,但是我认为理论上的处理会更有价值。

Edit: Rewrote the answer since it wasn't complete enough. 编辑:重写答案,因为它不够完整。 And instead of using the euler angles, you can create the matrix this way. 而且,除了使用欧拉角之外,您还可以通过这种方式创建矩阵。

You get the Z vector and use cross product to get the other vectors to compose the matrix. 您将获得Z向量,并使用叉积获得其他向量以构成矩阵。

Vec3 start;  // start of the line
Vec3 end;    // end of the line

Vec3 Z = end - start;
Z.normalize();

Vec3 X = Vec3(0,1,0).cross(Z);
X.normalize();

Vec3 Y = Z.cross(X);
Y.normalize();

// here's the 3X3 rotation matrix
_m11 = X.x; _m12 = Y.x; _m13 = Z.x; 
_m21 = X.y; _m22 = Y.y; _m23 = Z.y; 
_m31 = X.z; _m32 = Y.z; _m33 = Z.z; 

Link that explains the process of composing a rotation matrix: http://nghiaho.com/?page_id=846 . 解释旋转矩阵组成过程的链接: http : //nghiaho.com/?page_id=846

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM