Bug in "quaternion.h"

Discussions regarding ARGoS bugs. Report bugs here.
haithamelfaham
Posts: 2
Joined: Mon Feb 10, 2014 12:24 pm

Bug in "quaternion.h"

Postby haithamelfaham » Mon Feb 10, 2014 12:37 pm

BetweenTwoVectors has a problem returning the right vector in the anti-parallel vectors case. A modified version of "quaternion.h" is attached with a suggested bug fix. Please verify the code and update the old one.
Attachments
quaternion.h
(11.8 KiB) Downloaded 1011 times

pincy
Site Admin
Posts: 632
Joined: Thu Mar 08, 2012 8:04 pm
Location: Boston, MA
Contact:

Re: Bug in "quaternion.h"

Postby pincy » Mon Feb 10, 2014 4:43 pm

Hi haithamelfaham,

Thanks for spotting this bug. You're absolutely right. I have checked your code, refactored it, and optimized it a little.

Can you please check that this version works?

As soon as you confirm it, I'll commit the fix and credit you for it.

Cheers,
Carlo

Code: Select all

inline CQuaternion& BetweenTwoVectors(const CVector3& c_vector1, const CVector3& c_vector2) { Real fProd = c_vector1.DotProduct(c_vector2); if(fProd > 0.999999f) { /* The two vectors are parallel, no rotation */ m_fValues[0] = 1.0; m_fValues[1] = 0.0; m_fValues[2] = 0.0; m_fValues[3] = 0.0; } else if(fProd < -0.999999f) { /* The two vectors are anti-parallel */ /* We need to set an arbitrary rotation axis */ /* To find it, we calculate the cross product of c_vector1 with either X or Y, depending on which is not coplanar with c_vector1 */ CVector3 cRotAxis = c_vector1; if(Abs(c_vector1.DotProduct(CVector3::X)) < 0.999999) { /* Use the X axis */ cRotAxis.CrossProduct(CVector3::X); } else { /* Use the Y axis */ cRotAxis.CrossProduct(CVector3::Y); } /* The wanted quaternion is a rotation around cRotAxis by 180 degrees */ FromAngleAxis(CRadians::PI, cRotAxis); } else { /* The two vectors are not parallel nor anti-parallel */ m_fValues[0] = ::sqrt(c_vector1.SquareLength() * c_vector2.SquareLength()) + fProd; CVector3 cCrossProd(c_vector1); cCrossProd.CrossProduct(c_vector2); m_fValues[1] = cCrossProd.GetX(); m_fValues[2] = cCrossProd.GetY(); m_fValues[3] = cCrossProd.GetZ(); Normalize(); } return *this; }
I made ARGoS.

haithamelfaham
Posts: 2
Joined: Mon Feb 10, 2014 12:24 pm

Re: Bug in "quaternion.h"

Postby haithamelfaham » Mon Feb 10, 2014 5:23 pm

Yes this version works.. I am not sure though if the parallel case is necessary I think without it would still work fine. I tried with 2 parallel vectors and it works fine. Maybe you can verify that as well and if so I guess you can comment it out as a further optimization.

Haitham

pincy
Site Admin
Posts: 632
Joined: Thu Mar 08, 2012 8:04 pm
Location: Boston, MA
Contact:

Re: Bug in "quaternion.h"

Postby pincy » Mon Feb 10, 2014 7:09 pm

Hi Haitham,

Thanks for testing it.

Indeed the code would work, but singling out the parallel case spares a little computation.

I'll commit this version.

Cheers,
Carlo
I made ARGoS.


Return to “Bugs”