旋转关于另一个向量的向量

YAH*_*OOO 4 c++ math 3d vector rotation

我正在为OpenGL编写一个3d矢量类.如何将矢量v1绕另一个矢量v2旋转角度A?

Dri*_*ise 6

这可能有用:

double c = cos(A);
double s = sin(A);
double C = 1.0 - c;

double Q[3][3];
Q[0][0] = v2[0] * v2[0] * C + c;
Q[0][1] = v2[1] * v2[0] * C + v2[2] * s;
Q[0][2] = v2[2] * v2[0] * C - v2[1] * s;

Q[1][0] = v2[1] * v2[0] * C - v2[2] * s;
Q[1][1] = v2[1] * v2[1] * C + c;
Q[1][2] = v2[2] * v2[1] * C + v2[0] * s;

Q[2][0] = v2[0] * v2[2] * C + v2[1] * s;
Q[2][1] = v2[2] * v2[1] * C - v2[0] * s;
Q[2][2] = v2[2] * v2[2] * C + c;

v1[0] = v1[0] * Q[0][0] + v1[0] * Q[0][1] + v1[0] * Q[0][2];
v1[1] = v1[1] * Q[1][0] + v1[1] * Q[1][1] + v1[1] * Q[1][2];
v1[2] = v1[2] * Q[2][0] + v1[2] * Q[2][1] + v1[2] * Q[2][2];
Run Code Online (Sandbox Code Playgroud)


Bre*_*ale 5

您可能会发现四元数是一种更优雅,更有效的解决方案.


看到这个答案最近出现后,我虽然提供了更加健全的答案.一个可以使用而不必理解四元数的完整数学含义.我将假设(给定C++标签)你有类似于Vector3具有'明显'函数的类inner,如cross,和*=标量运算符等......

#include <cfloat>
#include <cmath>

...

void make_quat (float quat[4], const Vector3 & v2, float angle)
{
    // BTW: there's no reason you can't use 'doubles' for angle, etc.
    // there's not much point in applying a rotation outside of [-PI, +PI];
    // as that covers the practical 2.PI range.

    // any time graphics / floating point overlap, we have to think hard
    // about degenerate cases that can arise quite naturally (think of
    // pathological cancellation errors that are *possible* in seemingly
    // benign operations like inner products - and other running sums).

    Vector3 axis (v2);

    float rl = sqrt(inner(axis, axis));
    if (rl < FLT_EPSILON) // we'll handle this as no rotation:
    {
        quat[0] = 0.0, quat[1] = 0.0, quat[2] = 0.0, quat[3] = 1.0;
        return; // the 'identity' unit quaternion.
    }

    float ca = cos(angle);

    // we know a maths library is never going to yield a value outside
    // of [-1.0, +1.0] right? Well, maybe we're using something else -
    // like an approximating polynomial, or a faster hack that's a little
    // rough 'around the edge' cases? let's *ensure* a clamped range:
    ca = (ca < -1.0f) ? -1.0f : ((ca > +1.0f) ? +1.0f : ca);

    // now we find cos / sin of a half-angle. we can use a faster identity
    // for this, secure in the knowledge that 'sqrt' will be valid....

    float cq = sqrt((1.0f + ca) / 2.0f); // cos(acos(ca) / 2.0);
    float sq = sqrt((1.0f - ca) / 2.0f); // sin(acos(ca) / 2.0);

    axis *= sq / rl; // i.e., scaling each element, and finally:

    quat[0] = axis[0], quat[1] = axis[1], quat[2] = axis[2], quat[3] = cq;
}
Run Code Online (Sandbox Code Playgroud)

因此float quat[4],在给定原始参数的情况下,保持表示轴和旋转角的单位四元数(, v2, A).

这是四元数乘法的例程.SSE/SIMD可能会加快速度,但在大多数情况下,复杂的变换和照明通常都是GPU驱动的.如果你记得复数乘法有点奇怪,四元数乘法更是如此.复数乘法是一种可交换操作:a*b = b*a.四元数甚至不保留此属性,即q*p != p*q:

static inline void
qmul (float r[4], const float q[4], const float p[4])
{
    // quaternion multiplication: r = q * p

    float w0 = q[3], w1 = p[3];
    float x0 = q[0], x1 = p[0];
    float y0 = q[1], y1 = p[1];
    float z0 = q[2], z1 = p[2];

    r[3] = w0 * w1 - x0 * x1 - y0 * y1 - z0 * z1;
    r[0] = w0 * x1 + x0 * w1 + y0 * z1 - z0 * y1;
    r[1] = w0 * y1 + y0 * w1 + z0 * x1 - x0 * z1;
    r[2] = w0 * z1 + z0 * w1 + x0 * y1 - y0 * x1;
}
Run Code Online (Sandbox Code Playgroud)

最后,使用四元数旋转3D'向量' v(或者如果您愿意,或者v问题已经命名的'点' v1,表示为向量):float q[4]有一个奇怪的公式:v' = q * v * conjugate(q).四元数具有共轭,类似于复数.这是例程:

static inline void
qrot (float v[3], const float q[4])
{
    // 3D vector rotation: v = q * v * conj(q)

    float r[4], p[4];

    r[0] = + v[0], r[1] = + v[1], r[2] = + v[2], r[3] = +0.0;
    glView__qmul(r, q, r);

    p[0] = - q[0], p[1] = - q[1], p[2] = - q[2], p[3] = q[3];
    glView__qmul(r, r, p);

    v[0] = r[0], v[1] = r[1], v[2] = r[2];
}
Run Code Online (Sandbox Code Playgroud)

把它们放在一起.显然,您可以static在适当的地方使用关键字.现代优化编译器可能会inline根据自己的代码生成启发式忽略提示.但是现在让我们专注于正确性:

如何将矢量v1绕另一个矢量v2旋转角度A?

假设某种Vector3类,并且(A)以弧度为单位,我们希望四元数表示(A)围绕轴的角度旋转v2,我们希望将四元数旋转应用于v1结果:

float q[4]; // we want to find the unit quaternion for `v2` and `A`...

make_quat(q, v2, A);

// what about `v1`? can we access elements with `operator [] (int)` (?)
// if so, let's assume the memory: `v1[0] .. v1[2]` is contiguous.
// you can figure out how you want to store and manage your Vector3 class.

qrot(& v1[0], q);

// `v1` has been rotated by `(A)` radians about the direction vector `v2` ...
Run Code Online (Sandbox Code Playgroud)

这是人们希望在Beta文档站点中扩展的那种东西吗?我并不完全清楚它的要求,预期的严谨性等.


thi*_*ton 3

最容易理解的方法是旋转坐标轴,使向量 v2 与 Z 轴对齐,然后绕 Z 轴旋转 A,然后再旋转回来,使 Z 轴与 v2 对齐。

当您写下这三个运算的旋转矩阵时,您可能会注意到您依次应用了三个矩阵。为了达到相同的效果,您可以将三个矩阵相乘。