This forum has been archived. All content is frozen. Please use KDE Discuss instead.

[SOLVED] Bug in Quaternion::setFromTwoVectors

Tags: None
(comma "," separated)
benv
Registered Member
Posts
2
Karma
0
OS
Hi !

There are two bugs in Quaternion::setFromTwoVectors :

First is a missing return in the test for dot == 1 and the second is the case where dot == -1...

In this second case, I just try to set the quaternion between from and UnitX or UnitY

Hope this helps.

Bye,
Ben.

Last edited by ggael on Tue Feb 17, 2009 9:53 am, edited 1 time in total.
benv
Registered Member
Posts
2
Karma
0
OS
Proposed new implementation :

Code: Select all
template
template
inline Quaternion& Quaternion::setFromTwoVectors(const MatrixBase& a, const MatrixBase& b)
{
  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)))
  {
    v1 = Vector3::UnitX();
    c = v0.dot(v1);
    if (ei_isApprox(c,Scalar(1)) || ei_isApprox(c,Scalar(-1)))
    {
      v1 = Vector3::UnitY();
      c = v0.dot(v1);
    }
    this->vec() = v0.cross(v1);
    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;
}
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
hi,

thanks a lot for the patch !

(applied in rev 927280)


Bookmarks



Who is online

Registered users: Bing [Bot], Evergrowing, Google [Bot], rblackwell