2012-02-15 9 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); 

}; 

このオブジェクトには2つの部分があります。そして、各部分はそれ自身の中心とqを持つことができ、それらは空間全体(主要オブジェクトの中心に対してではない)に対するものです。

次に、オブジェクトを回転させたいと思います。そのすべての部分も回転して、新しい中心点とqに更新されます。パーツはメインオブジェクトの中心に対して回転します。

どうすればいいですか?私は変換マットでこれを行う方法を示す多くのリンクを見つけました。しかし、クォータニオンで直接それを行う方法はありますか?

多分、言い換えれば、原点をシフトした四元数をどのように回転させるのでしょうか?

ありがとうございました!

答えて

0

実際には思ったよりも簡単です。私はそれらの線形代数クラスは実際に有用だったと思います。

この場合、「GeomType」という名前のクラスとして定義されている3Dジオメトリがあるとします。 「オブジェクト」は多くの「GeomType」で構成されています。ここで 'Object'は単純な 'GeomType'のベクトルです。 'Object'の各 'GeomType'は、 'Object'の中心点を基準にした中心点の位置と、デフォルトのニュートラル位置からの回転を表す四元数によって定義されます。ここにいくつかのサンプルコードがあります。

基本的には(x、y、z)の2倍のPointTypeと(w、x、y、z)のQuaternionTypeも2倍になります。

//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 –

関連する問題