Logo Search packages:      
Sourcecode: blender version File versions  Download package

template<typename Scalar>
template<typename Derived1, typename Derived2>
Quaternion< Scalar > & Quaternion< Scalar >::setFromTwoVectors ( const MatrixBase< Derived1 > &  a,
const MatrixBase< Derived2 > &  b 
) [inline]

Sets *this to be a quaternion representing a rotation sending the vector a to the vector b.

Returns:
a reference to *this.
Note that the two input vectors do not have to be normalized.

Definition at line 373 of file Quaternion.h.

References MatrixBase< Derived >::cross(), MatrixBase< Derived >::dot(), MatrixBase< Derived >::normalized(), MatrixBase< Derived >::unitOrthogonal(), Quaternion< _Scalar >::vec(), and Quaternion< _Scalar >::w().

{
  Vector3 v0 = a.normalized();
  Vector3 v1 = b.normalized();
  Scalar c = v0.dot(v1);

  // if dot == 1, vectors are the same
  if (ei_isApprox(c,Scalar(1)))
  {
    // set to identity
    this->w() = 1; this->vec().setZero();
    return *this;
  }
  // if dot == -1, vectors are opposites
  if (ei_isApprox(c,Scalar(-1)))
  {
    this->vec() = v0.unitOrthogonal();
    this->w() = 0;
    return *this;
  }

  Vector3 axis = v0.cross(v1);
  Scalar s = ei_sqrt((Scalar(1)+c)*Scalar(2));
  Scalar invs = Scalar(1)/s;
  this->vec() = axis * invs;
  this->w() = s * Scalar(0.5);

  return *this;
}


Generated by  Doxygen 1.6.0   Back to index