3d 如何旋转由四元数表示的对象集?

3d 如何旋转由四元数表示的对象集?,3d,rotation,quaternions,3d,Rotation,Quaternions,假设对象的方向由四元数表示。 如果我想旋转那个物体,我只需要用旋转的四元数乘以四元数 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

假设对象的方向由四元数表示。 如果我想旋转那个物体,我只需要用旋转的四元数乘以四元数

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);

};
类对象
{
公众:
std::向量部分;
点中心;//点基本上是双x,y,z
四元数q;
void RotateBy(四元数q_旋转);
};
就说这个物体有两部分。每个部分都有自己的中心和q,它们相对于整个空间(而不是相对于主要物体的中心)

现在我想旋转对象,它的所有部分也应该旋转,并更新到它们的新中心和q。零件将相对于主要对象的中心旋转

我该怎么做???我发现了很多链接,展示了如何使用转换矩阵实现这一点。但是有没有办法直接用四元数呢

也许,换句话说,如何在原点移动的情况下旋转四元数


谢谢你的帮助

实际上比我想象的要容易。我想那些线性代数课实际上很有用

在本例中,我假设您有一些三维几何体定义为一个名为“GeomType”的类。“对象”由许多“几何类型”组成。这里的“对象”是简单的“几何类型”向量。 “对象”中的每个“几何类型”由其相对于“对象”中心点的中心点位置和表示从默认中性位置旋转的四元数定义。下面是一些示例代码

也有double中基本上是(x,y,z)的PointType和double中也是(w,x,y,z)的四元数type

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

这是相对于一个点旋转一组三维对象的通用程序。它应该适用于任何编程语言,虽然它是基于C++编写的。< /P>这代码可以在四元数乘法时帮助。
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;
}