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/

Complex Numbers

Complex Numbers

Before we can fully understand quaterions, we must first understand where they came from. The root of quaternions is based on the concept of the complex number system.
In addition to the well-known number sets (NaturalIntegerReal, and Rational), the Complex Number system introduces a new set of numbers called imaginary numbers. Imaginary numbers were invented to solve certain equations that had no solutions such as:
To solve this expression, we must state that  which we know is not possible because the square of any number (positive or negative) is always positive.
Mathematicians generally can’t accept that an expression does not have a solution so a new term was invented called the imaginary number that can be used to solve such equations.
The imaginary number has the form:
Don’t try to actually understand this term as there is no logical reason why it exists. We just have to accept that  is just something that squares to .
The set of imaginary numbers can be represented by .
The set of complex numbers (represented by the symbol ) is the sum of a real number and an imaginary number and has the form:
It could also be stated that all Real numbers are complex numbers with  and all imaginary numbers are complex numbers with .

Adding and Subtracting Complex Numbers

Complex numbers can be added and subtracted by adding or subtracting the real, and imaginary parts.
Addition:
Subtraction:

Multiply a Complex Number by a Scalar

A complex number is multiplied by a scalar by multiplying each term of the complex number by the scalar:

Product of Complex Numbers

Complex numbers can also be multiplied by applying normal algebraic rules.

Square of Complex Numbers

A complex number can also be squared by multiplying by itself:

Complex Conjugate


The conjugate of a complex number is a complex number with the imaginary part negated and is denoted as either  or .
The product of a complex number and its conjugate gives a special result.

Absolute Value of a Complex Number

We can use the conjugate of a complex number to compute the absolute value (ornorm, or magnitude) of a complex number. The absolute value of a complex number is the square-root of the complex number multiplied by its conjugate and is denoted :

Quotient of Two Complex Numbers

To compute the quotient of two complex numbers, we multiply the numerator and denominator by the complex conjugate of the denominator.

Powers of i

If we state that  then it should be possible to raise i to other powers as well.
If we keep writing this sequence, we will see a pattern emerge .
A similar pattern emerges from the increasing negative powers.
You may have seen a similar pattern in mathematics before but in the form  which is generated by rotating a point 90 degrees counter-clockwise on a 2D Cartesian plane and the sequence  is generated by rotating a point 90 degrees clockwise on a 2D Cartesian plane.

Cartesian Plane
Cartesian Plane

The Complex Plane

We can also map complex numbers in a 2D grid called the Complex Plane by mapping the Real part on the horizontal axis and the Imaginary part on the vertical axis.
Complex Plane
Complex Plane

As shown in the previous sequence, we can say that if we multiply a complex number by i, we can rotate the complex number through the complex plane at 90 degree increments.
Let’s see if this is true. We’ll take an arbitrary point p in the complex plane:
and we multiply it by i gives q:
Multiplying q by i gives r:
And multiplying r by i gives s:
And multiplying s by i gives t:
Which is exactly what we started with (p). If we plot these complex numbers on the complex plane, we get the following result.
Complex Numbers on the Complex Plane
Complex Numbers on the Complex Plane

We can also rotate clock-wise in the complex plane by multiplying the complex number by -i.

Rotors

We can also perform arbitrary rotations in the complex plane by defining a complex number of the form:
Multiplying any complex number by the rotor q produces the general formula:
Which can also be written in matrix form:

Which is the method to rotate an arbitrary point in the complex plane counter-clockwise about the origin.

Refrences:


  • http://3dgep.com/understanding-quaternions/