绕任意轴旋转

• 将旋转轴平移至与坐标轴重合，对应平移操作
• 旋转，对应操作
• 步骤1的逆过程，对应操作

1. 将旋转轴平移至原点
2. 将旋转轴旋转至YOZ平面
3. 将旋转轴旋转至于Z轴重合
4. 绕Z轴旋转θ度
5. 执行步骤3的逆过程
6. 执行步骤2的逆过程
7. 执行步骤1的逆过程

,

,

void RotateArbitraryAxis(D3DXMATRIX* pOut, D3DXVECTOR3* axis, float theta)
{
D3DXVec3Normalize(axis, axis);
float u = axis->x;
float v = axis->y;
float w = axis->z;

pOut->m[0][0] = cosf(theta) + (u * u) * (1 - cosf(theta));
pOut->m[0][1] = u * v * (1 - cosf(theta)) + w * sinf(theta);
pOut->m[0][2] = u * w * (1 - cosf(theta)) - v * sinf(theta);
pOut->m[0][3] = 0;

pOut->m[1][0] = u * v * (1 - cosf(theta)) - w * sinf(theta);
pOut->m[1][1] = cosf(theta) + v * v * (1 - cosf(theta));
pOut->m[1][2] = w * v * (1 - cosf(theta)) + u * sinf(theta);
pOut->m[1][3] = 0;

pOut->m[2][0] = u * w * (1 - cosf(theta)) + v * sinf(theta);
pOut->m[2][1] = v * w * (1 - cosf(theta)) - u * sinf(theta);
pOut->m[2][2] = cosf(theta) + w * w * (1 - cosf(theta));
pOut->m[2][3] = 0;

pOut->m[3][0] = 0;
pOut->m[3][1] = 0;
pOut->m[3][2] = 0;
pOut->m[3][3] = 1;

void RotateArbitraryLine(D3DXMATRIX* pOut, D3DXVECTOR3* v1, D3DXVECTOR3* v2, float theta)
{
float a = v1->x;
float b = v1->y;
float c = v1->z;

D3DXVECTOR3 p = *v2 - *v1;
D3DXVec3Normalize(&p, &p);
float u = p.x;
float v = p.y;
float w = p.z;

float uu = u * u;
float uv = u * v;
float uw = u * w;
float vv = v * v;
float vw = v * w;
float ww = w * w;
float au = a * u;
float av = a * v;
float aw = a * w;
float bu = b * u;
float bv = b * v;
float bw = b * w;
float cu = c * u;
float cv = c * v;
float cw = c * w;

float costheta = cosf(theta);
float sintheta = sinf(theta);

pOut->m[0][0] = uu + (vv + ww) * costheta;
pOut->m[0][1] = uv * (1 - costheta) + w * sintheta;
pOut->m[0][2] = uw * (1 - costheta) - v * sintheta;
pOut->m[0][3] = 0;

pOut->m[1][0] = uv * (1 - costheta) - w * sintheta;
pOut->m[1][1] = vv + (uu + ww) * costheta;
pOut->m[1][2] = vw * (1 - costheta) + u * sintheta;
pOut->m[1][3] = 0;

pOut->m[2][0] = uw * (1 - costheta) + v * sintheta;
pOut->m[2][1] = vw * (1 - costheta) - u * sintheta;
pOut->m[2][2] = ww + (uu + vv) * costheta;
pOut->m[2][3] = 0;

pOut->m[3][0] = (a * (vv + ww) - u * (bv + cw)) * (1 - costheta) + (bw - cv) * sintheta;
pOut->m[3][1] = (b * (uu + ww) - v * (au + cw)) * (1 - costheta) + (cu - aw) * sintheta;
pOut->m[3][2] = (c * (uu + vv) - w * (au + bv)) * (1 - costheta) + (av - bu) * sintheta;
pOut->m[3][3] = 1;
}

参考

http://inside.mines.edu/~gmurray/ArbitraryAxisRotation/

http://en.wikipedia.org/wiki/Rotation_matrix#Rotation_matrix_from_axis_and_angle

http://en.wikipedia.org/wiki/Rodrigues%27_rotation_formula

== Happy Coding ==

posted on 2012-08-10 09:20 zdd 阅读(...) 评论(...) 编辑 收藏