2012-02-15 46 views
1

說一個對象的方向是由一個四元數表示的。 如果我想旋轉該對象,我只需將四元數與旋轉四元數相乘即可。如何旋轉由四元數表示的對象集?

object.q = q_rotation*(object.q) 

然後對於由由一組較小的對象中的一個對象。我如何旋轉它?

class Object 
{ 
public: 
    std::vector<Object> part; 
    Point centre; //Point is basically double x, y, z 
    Quaternion q; 

    void RotateBy(Quaternion q_rotation); 

}; 

只是說這個對象有兩部分的。每個部分可以有自己的中心和q,並且它們相對於整個空間(不是相對於主要對象的中心)。

然後現在我想旋轉對象,並且它的所有部分都應該旋轉並更新到它們的新中心和q。零件將相對於主體的中心旋轉。

我該怎麼做?我發現了許多鏈接,顯示瞭如何使用轉換矩陣做到這一點。但有沒有辦法直接與四元數做到這一點?

也許,換句話說,如何旋轉一個四元數的原點偏移?

謝謝你的幫助!

回答

0

它實際上比我想象的要容易。我猜那些線性代數類實際上是有用的。

在這種情況下,我假設你有一些3D幾何被定義爲一個名爲'GeomType'的類。一個'對象'由許多'GeomType'組成。這裏'對象'很簡單,就是一個'GeomType'向量。 「對象」中的每個「GeomType」都是由相對於「對象」中心點的中心點位置和表示從默認中性位置旋轉的四元數定義的。這裏是一些示例代碼。

還有一個PointType,其基本上是(x,y,z)的double,而QuaternionType也是(w,x,y,z),也是double。

//suppose you have this Object 
std::vector <GeomType> Object; //and it is already initialized 
//you were given the rotation quaternion and the point to rotate about. 
PointType origin; 
QuaternionType Q2; 

//rotate the GeomTypes 
unsigned int numGeom = Object.size(); 
for (unsigned int i = 0; i <numGeom; i++) 
{ 
    //1. translate the individual GeomType 
    const PointType geomCentre= Object[i].GetCentrePosition(); 

    //Rotate vector representing the direction and distance from origin to geom 
    //note that the origin of rotation does not need to be the centre 
    const PointType newPos = 
      RotateAbout(PointType(geomCentre[0], geomCentre[1], dGeomPos[2]), 
                 Q2, origin); 
    //move the GeomType to the new position 
    Object[i].SetCentrePosition(newPos.GetX(), newPos.GetY(), newPos.GetZ()); 

    //2. Rotate the GeomType 
    QuaternionType Qo = Object[i].GetQuaternion(); //current quaternion of the geom 
    QuaternionType Qr; //resultant quaternion of the geom 

    Qr = Q2*Qo; //rotating by multiplication 
    //(please check other sites on how to multiply quaternions) 

    Object[i].SetQuaternion(Qr); //set the new quaternion 
} 

這是其用於以繞點

PointType RotateAbout(const PointType &InitPos, const QuaternionType &Qr, const PointType& Origin) 
{ 
    //the vector from the origin 
    PointType newVec = InitPos-Origin; 
    //extract the magnitude 
    const double vecLength = newVec.Length(); 

    //normalize the vector and rotate that vector 
    //then translate the geom to the new position 
    newVec = Origin + Qr*(newVec.GetUnitVector())*vecLength; 

    return newVec; 
} 

這裏的向量的函數RotateAbout是一般程序的3D對象相對於一個點旋轉一組。它應該適用於任何編程語言,儘管它是基於C++編寫的。

+0

此代碼可能有助於乘以四元數。 http://stackoverflow.com/questions/10781033/eficient-c-quaternion-multiplication-using-cvmat – 2012-05-28 08:57:28