# [vtkusers] Bug in vtkQuaternion

Mengda Wu wumengda at gmail.com
Thu Dec 5 16:14:14 EST 2013

```Thanks a lot for your reply.

But for the identity quaternion (1, 0, 0, 0), the axis is indeed (0,0,0).
This is related to GetRotationAngleAndAxis, that function will give a
nozero angle and axis (0,0,0) for the identity quaternion.
Then if I plug this result to SetRotationAngleAndAxis, I will get a
quaternion (0,0,0,0), which does not make sense.
Then if I use ToMatrix3x3 on the quaternion (0,0,0,0), which will be wrong.

So I suggest to change

template<typename T> void
vtkQuaternion<T>::SetRotationAngleAndAxis (const T& angle,
const T& x,
const T& y,
const T& z)
{
T axisNorm = x*x + y*y + z*z;
if (axisNorm != 0.0)
{
T w = cos(angle / 2.0);
this->SetW(w);

T f = sin( angle / 2.0);
this->SetX((x / axisNorm) * f);
this->SetY((y / axisNorm) * f);
this->SetZ((z / axisNorm) * f);
}
else if ( angle != 0.0 )
{
this->Set(1.0, 0.0, 0.0, 0.0);
}
else
{
this->Set(0.0, 0.0, 0.0, 0.0);
}
}

How is that?

Mengda

On Thu, Dec 5, 2013 at 4:04 PM, David Gobbi <david.gobbi at gmail.com> wrote:

> Hi Mengda,
>
> The SetRotationAngleAndAxis(angle, x, y, z) method does not
> directly set the quaternion elements, for that you would use the
> method Set(w, x, y, z).
>
> For SetRotationAngleAndAxis, it does not make sense to specify
> xyz=(0,0,0) with a nonzero angle because (0,0,0) is not a valid axis.
>
>   David
>
> On Thu, Dec 5, 2013 at 1:53 PM, Mengda Wu <wumengda at gmail.com> wrote:
> > Hi all,
> >
> >    I think there is a bug in vtkQuaternion<T>::SetRotationAngleAndAxis.
> What
> > if I want to
> > set angle=1.0 and xyz=(0, 0, 0)? I need an identity matrix from this
> > quaternion. Should it call this->Set(1.0, 0.0, 0.0, 0.0) instead of
> > this->Set(0.0, 0.0, 0.0, 0.0) in this case?
> >
> > Thanks,
> > Mengda
> >
> >
> >
> > The code is pasted here:
> >
> > template<typename T> void
> > vtkQuaternion<T>::SetRotationAngleAndAxis (const T& angle,
> >                                            const T& x,
> >                                            const T& y,
> >                                            const T& z)
> > {
> >   T axisNorm = x*x + y*y + z*z;
> >   if (axisNorm != 0.0)
> >     {
> >     T w = cos(angle / 2.0);
> >     this->SetW(w);
> >
> >     T f = sin( angle / 2.0);
> >     this->SetX((x / axisNorm) * f);
> >     this->SetY((y / axisNorm) * f);
> >     this->SetZ((z / axisNorm) * f);
> >     }
> >   else
> >     {
> >     this->Set(0.0, 0.0, 0.0, 0.0);
> >     }
> > }
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20131205/e78df7df/attachment.htm>
```