Saturday 18 April 2015

Rotation Using Quaternions

What is Quaternion ? 



Quaternions define a rotation of a certain amount of degrees (or radians) around a given axis without the undesired effects of Gimbal Lock associated to Euler Angles.





Quaternions consist on a real part (alpha: α) and 3 imaginary parts x,y,z. Mathematically speaking a quaternion can be written as: q= α+ xi + yj+ kj , being i=j=k=sqrt(-1). They are normally represented by the n-tuple (α,x,y,z);

A (α,x,y,z) quaternion represents a rotation of α radians around an arbitrary (x,y,z) axis. Quaternion multiplication represents the applicance of succesive rotations. As they represent rotations in 3D, quaternion multiplication is not conmutative.


In essence:

The concept of quaterinions was realized by the Irish mathematician Sir William Rowan Hamilton on Monday October 16th 1843 in Dublin, Ireland. Hamilton was on his way to the Royal Irish Academy with his wife and as he was passing over the Royal Canal on the Brougham Bridge he made a dramatic realization that he immediately carved into the stone of the bridge.


File:William Rowan Hamilton Plaque - geograph.org.uk - 347941.jpg

Quaternion Basics


A quaternion is a four-element vector that can be used to encode any rotation in a 3D coordinate system. Technically, a quaternion is composed of one real element and three complex elements, and it can be used for much more than rotations. In this application note we'll be ignoring the theoretical details about quaternions and providing only the information that is needed to use them for representing the attitude of an orientation sensor.

The attitude quaternion estimated orientation sensors encodes rotation from the "inertial frame" to the sensor "body frame." The inertial frame is an Earth-fixed coordinate frame defined so that the x-axis points north, the y-axis points east, and the z-axis points down as shown in the below Figure  The sensor body-frame is a coordinate frame that remains aligned with the sensor at all times. Unlike Euler Angle estimation, only the body frame and the inertial frame are needed when quaternions are used for estimation.

AHRS Inertial Frame

Quaternion Based Rotation:

Basically the initial orientation is set to a unitary rotation of (1,0,0,0) and we desire to rotate it a given angle around (x,y,z):
Graphic engines work with matrices, so to apply a quaternion based rotation we should follow these steps:

  • define the axis of rotation
  • indicate the angle to rotate
  • calculate the rotation quaternion assigned to those values.
  • multiply the current orientation quaternion by the rotation quaternion
  • convert the resulting rotation quaternion to a matrix
  • calculate point position by multiplying it by the obtained matrix


Necessary Mathematics Related to Quaternion Rotations


If you recall we defined a special form of the complex number called a Rotor that could be used to rotate a point through the 2D complex plane as:
Then by its similarities to complex numbers, it should be possible to express a quaternion that can be used to rotate a point in 3D-space as such:

Let’s test if this theory holds by computing the product of the quaternion q and the vector p. First, we can express p as a Pure quaternion in the form:
And q is a unit-norm quaternion in the form:
Then,

We see that the result is a general quaternion with both a scalar and a vector parts.
Let’s first consider the “special” case where p is perpendicular to  in which case, the dot-product term  and the result becomes the Pure quaternion:

In this case, to rotate p about  we just substitute  and .
As an example, let’s rotate a vector p 45° about the z-axis then our qu
aternion q is:
And let’s take a vector p that adheres to the special case that p is perpendicular to k:
Now let’s find the product of qp:

Which results in a Pure quaternion that is rotated 45° about the k axis.
We can also confirm that the magnitude of the resulting vector is maintained:
Which is exactly the result we expected!
We can visualize this by the following image:
Quaternion Rotation (1)
Quaternion Rotation (1)
Now let’s consider a quaternion that is not orthogonal to p. If we specify the vector part of our quaternion to 45° offset from p we get:
And multiplying our point p by q we get:
And substituting p and  gives:
Which is no longer a pure quaternion, and it has not been rotated 45° and the vector’s norm is no longer 2 (instead it has been reduced to ).
This result can be visualized by the image.
Quaternion Rotation (2)
Quaternion Rotation (2)
Technically, it’s incorrect to represent the quaternion p’ in 3D space because it’s actually a 4D vector! For the sake of simplicity, I will only visualize the vector component of quaternions.

However, all is not lost. Hamilton recognized (but didn’t publish) that if we post-multiply the result of qp by the inverse of q then the result is a pure quaternion and the norm of the vector component is maintained. Let’s see if we can apply this to our example.

First, let’s compute :
For  gives:
And combining the previous value of qp and  gives:
Which is a pure quaternion and the norm of the result is:
which is the same as p so the norm of the vector is maintained.
The image below visualizes the result of the rotation.
Quaternion Rotation (3)
Quaternion Rotation (3)
So we can see that the result is a pure quaternion and that the norm of the initial vector is maintained, but the vector has been rotated 90° rather than 45° which is twice as much as desired! So in order to correctly rotate a vector p by an angle about an arbitrary axis , we must consider the half-angle and construct the following quaternion:

Which is the general form of a rotation quaternion!
Refrences
  • http://3dgep.com/understanding-quaternions/
  • http://physicsforgames.blogspot.com/2010/02/quaternions.html
  • http://www.chrobotics.com/library/understanding-quaternions
  • http://www.euclideanspace.com/maths/geometry/rotations/for/

No comments:

Post a Comment