1

android使用以下代码计算旋转矩阵:

float Ax = gravity[0];
float Ay = gravity[1];
float Az = gravity[2];
final float Ex = geomagnetic[0];
final float Ey = geomagnetic[1];
final float Ez = geomagnetic[2];
float Hx = Ey*Az - Ez*Ay;
float Hy = Ez*Ax - Ex*Az;
float Hz = Ex*Ay - Ey*Ax;
final float normH = (float)Math.sqrt(Hx*Hx + Hy*Hy + Hz*Hz);
if (normH < 0.1f) {
    // device is close to free fall (or in space?), or close to
    // magnetic north pole. Typical values are  > 100.
    return false;
}
final float invH = 1.0f / normH;
Hx *= invH;
Hy *= invH;
Hz *= invH;
final float invA = 1.0f / (float)Math.sqrt(Ax*Ax + Ay*Ay + Az*Az);
Ax *= invA;
Ay *= invA;
Az *= invA;
final float Mx = Ay*Hz - Az*Hy;
final float My = Az*Hx - Ax*Hz;
final float Mz = Ax*Hy - Ay*Hx;
if (R != null) {
    if (R.length == 9) {
        R[0] = Hx;     R[1] = Hy;     R[2] = Hz;
        R[3] = Mx;     R[4] = My;     R[5] = Mz;
        R[6] = Ax;     R[7] = Ay;     R[8] = Az;
    } else if (R.length == 16) {
        R[0]  = Hx;    R[1]  = Hy;    R[2]  = Hz;   R[3]  = 0;
        R[4]  = Mx;    R[5]  = My;    R[6]  = Mz;   R[7]  = 0;
        R[8]  = Ax;    R[9]  = Ay;    R[10] = Az;   R[11] = 0;
        R[12] = 0;     R[13] = 0;     R[14] = 0;    R[15] = 1;
    }
}

我想知道这背后的逻辑是什么。我应该如何使用加速度计和磁力计来获得旋转矩阵?

4

1 回答 1

2

带注释的,删除了角盒处理:

// Down vector
float Ax = gravity[0];
float Ay = gravity[1];
float Az = gravity[2];

// North vector
final float Ex = geomagnetic[0];
final float Ey = geomagnetic[1];
final float Ez = geomagnetic[2];

H 垂直于 E 和 A

// H = E x A
float Hx = Ey*Az - Ez*Ay;
float Hy = Ez*Ax - Ex*Az;
float Hz = Ex*Ay - Ey*Ax;
final float normH = (float)Math.sqrt(Hx*Hx + Hy*Hy + Hz*Hz);

矩阵中的每一列的长度应为 1

// Force H to unit length
final float invH = 1.0f / normH;
Hx *= invH;
Hy *= invH;
Hz *= invH;

// Force A to unit length
final float invA = 1.0f / (float)Math.sqrt(Ax*Ax + Ay*Ay + Az*Az);
Ax *= invA;
Ay *= invA;
Az *= invA;

由于 A 垂直于 H,并且两者都是单位长度,因此 M 也必须具有单位长度,所以这里不需要归一化。

// M = A x H
// Forward vector
final float Mx = Ay*Hz - Az*Hy;
final float My = Az*Hx - Ax*Hz;
final float Mz = Ax*Hy - Ay*Hx;

H、M、A相互垂直,所以我们有一个旋转矩阵

R[0] = Hx;     R[1] = Hy;     R[2] = Hz;
R[3] = Mx;     R[4] = My;     R[5] = Mz;
R[6] = Ax;     R[7] = Ay;     R[8] = Az;
于 2013-03-13T10:19:26.137 回答