1.使用
//构造旋转四元数
vtkQuaterniond rotation;
rotation.SetRotationAngleAndAxis(vtkMath::RadiansFromDegrees(90.0),0.0, 1.0, 0.0);
//构造旋转点四元数
vtkQuaterniond p;
p.Set(0.0,1.0,0.0,0.0);
//转置
vtkQuaterniond inverse = rotation.Inverse();
//计算旋转结果
vtkQuaterniond res = rotation*p*inverse;
double resdata[4] = {0};
res.Get(resdata);
2.自定义
void QuaternionMulti(float q1[4],float q2[4],float res[4]){
float ww = q1[0]*q2[0];
float wx = q1[0]*q2[1];
float wy = q1[0]*q2[2];
float wz = q1[0]*q2[3];
float xw = q1[1]*q2[0];
float xx = q1[1]*q2[1];
float xy = q1[1]*q2[2];
float xz = q1[1]*q2[3];
float yw = q1[2]*q2[0];
float yx = q1[2]*q2[1];
float yy = q1[2]*q2[2];
float yz = q1[2]*q2[3];
float zw = q1[3]*q2[0];
float zx = q1[3]*q2[1];
float zy = q1[3]*q2[2];
float zz = q1[3]*q2[3];
res[0] = ww-xx-yy-zz;
res[1] = wx+xw+yz-zy;
res[2] = wy-xz+yw+zx;
res[3] = wz+xy-yx+zw;
}
//angle使用角度,延旋转轴方向,逆时针
void RotateByQuaternion(float pos[3],float axis[3],float angle,float resPos[3]){
float radian = angle*3.1415926/180.0;
float axisNorm = sqrt(axis[0]*axis[0] + axis[1]*axis[1] + axis[2]*axis[2]);
float v1 = sin(0.5*radian);
float r1 = cos(0.5*radian);
float q1[4] = {r1,v1*(axis[0]/axisNorm),v1*(axis[1]/axisNorm),v1*(axis[2]/axisNorm)};
float p[4] = {0.0,pos[0],pos[1],pos[2]};
float tmpdata[4] = {0.0};
QuaternionMulti(q1,p,tmpdata);
float q1_[4] = {q1[0],-q1[1],-q1[2],-q1[3]};
float resQuaternion[4] = {0.0};
QuaternionMulti(tmpdata,q1_,resQuaternion);
resPos[0] = resQuaternion[1];
resPos[1] = resQuaternion[2];
resPos[2] = resQuaternion[3];
}
//测试
int main(int argc, char *argv[])
{
float pos[3] = {1.0,1.0,0.0};
float axis[3] = {0.0,0.0,1.0};
float angle = 90;
float resdata[3] = {0.0};
RotateByQuaternion(pos,axis,angle,resdata);
qDebug() << "resdata:" << resdata[0]<<resdata[1]<<resdata[2];
return 0;
}