围绕矢量三维旋转点



我想将P3(在附近的某个地方)围绕向量(与P1和P2相交)旋转x度。

P1和P2是与来自图像的矢量(线)e相交的2个点。我研究了很多,找到了一些好的材料,但我的三角学技能太差了。我需要这个PAWN(小),这里我有一些代码,但并没有真正按预期工作。如果有人能帮助我,我会非常感谢:)

图像链接:http://upload.wikimedia.org/wikipedia/commons/thumb/5/51/Euler_AxisAngle.png/220px-Euler_AxisAngle.png

P1/P2/P3=(x,y,z)

float vec[3];
SubtractVectors(P1, P2, vec);
float newp[3];
float rotation[4][4];
SetupMatrix(90.0, vec, rotation);
MultiplyMatrix(P3, rotation, newp);

//---------------------------------

stock void MultiplyMatrix(float input[3], float rotation[4][4], float output[3])
{
    float input2[4];
    input2[0] = input[0];
    input2[1] = input[1];
    input2[2] = input[2];
    input2[3] = 1.0;
    float output2[4];
    for(int i = 0 ; i < 4 ; i++)
    {
        for(int j = 0 ; j < 4 ; j++)
        {
            output2[i] += rotation[i][j] * input2[j];
        }
    }
    output[0] = output2[0];
    output[1] = output2[1];
    output[2] = output2[2];
}
stock void SetupMatrix(float angle, float vector[3], float rotation[4][4])
{
    float L = (vector[0] * vector[0] + vector[1] * vector[1] + vector[2] * vector[2]);
    angle = angle * M_PI / 180.0;
    float u2 = vector[0] * vector[0];
    float v2 = vector[1] * vector[1];
    float w2 = vector[2] * vector[2];
    rotation[0][0] = (u2 + (v2 + w2) * Cosine(angle)) / L;
    rotation[0][1] = (vector[0] * vector[1] * (1 - Cosine(angle)) - vector[2] * SquareRoot(L) * Sine(angle)) / L;
    rotation[0][2] = (vector[0] * vector[2] * (1 - Cosine(angle)) + vector[1] * SquareRoot(L) * Sine(angle)) / L;
    rotation[0][3] = 0.0; 
    rotation[1][0] = (vector[0] * vector[1] * (1 - Cosine(angle)) + vector[2] * SquareRoot(L) * Sine(angle)) / L;
    rotation[1][1] = (v2 + (u2 + w2) * Cosine(angle)) / L;
    rotation[1][2] = (vector[1] * vector[2] * (1 - Cosine(angle)) - vector[0] * SquareRoot(L) * Sine(angle)) / L;
    rotation[1][3] = 0.0; 
    rotation[2][0] = (vector[0] * vector[2] * (1 - Cosine(angle)) - vector[1] * SquareRoot(L) * Sine(angle)) / L;
    rotation[2][1] = (vector[1] * vector[2] * (1 - Cosine(angle)) + vector[0] * SquareRoot(L) * Sine(angle)) / L;
    rotation[2][2] = (w2 + (u2 + v2) * Cosine(angle)) / L;
    rotation[2][3] = 0.0; 
    rotation[3][0] = 0.0;
    rotation[3][1] = 0.0;
    rotation[3][2] = 0.0;
    rotation[3][3] = 1.0;
}

看看四元数,它们正是你所需要的。如果你不像我那样使用它们,那么:

  1. 构造表示旋转轴坐标系的变换矩阵M

    首先看这里:变换矩阵解剖学。例如,原点是点P1,因此X轴是P2-P1(旋转轴)。让Q = (1.0,0.0,0.0)(0.0,1.0,0.0)选择一个不平行于X轴矢量的矢量,这样

    X = P2-P1
    Q = (1.0,0.0,0.0) or (0.0,1.0,0.0)
    Y = cros(X,Q)
    Z = cros(X,Y)
    

    现在,您可以求取轴或更改它们的叉积操作数顺序,以匹配所需的轴方向(如果需要)。也不要忘记使所有轴的单位矢量

  2. 现在进行局部坐标系(LCS)旋转

    看这里LCS变换和这里LCS绕X轴旋转lrotx C++实现在答案末尾搜索lrotx

  3. 所以现在

    取您想要旋转的GCSP,得到其LCS坐标(对于未旋转的变换矩阵M

    Q = inverse(M)*P // if `M` before rotation was one then you can do Q=P instead
    

    旋转:

    M = inverse(inverse(M)*rotation)
    

    Q转换回GCS

    Q = M*Q
    

    这一切。。。

[注释]

如果你有错误的旋转方向,那么只要否定其中一个轴。。。或使用反角度

最新更新