Available on quaternions and their benefits
- Transfer
![](https://habrastorage.org/webt/wx/yq/yy/wxyqyyeuvo5us3kwciwntw5uigq.png)
From the translator: exactly 175 years and 3 days ago quaternions were invented. In honor of this round date, I decided to choose a material that explains this concept in an understandable language.
The quaternion concept was invented by the Irish mathematician Sir William Rowan Hamilton on Monday October 16, 1843 in Dublin, Ireland. Hamilton and his wife went to the Irish Royal Academy , and going over the Royal Canal along the Broome Bridge , he made an amazing discovery, which he immediately scrawled on the stone of the bridge.
![](https://habrastorage.org/getpro/habr/post_images/b34/87a/8a4/b3487a8a4aa6f5926a9232b84ba317b1.jpg)
Memorial plaque on Broome Bridge over the Royal Canal in honor of the discovery of the fundamental multiplication formula for quaternions.
In this article I will try to explain the concept of quaternions in a simple to understand way. I will explain how you can visualize quaternion, and also talk about the different operations that can be performed with quaternions. In addition, I will compare the use of matrices, Euler angles and quaternions, and then I will try to explain when to use quaternions instead of Euler angles or matrices, and when this is not necessary.
Content
- 1. Introduction
- 2. Complex numbers
- 2.1. Addition and subtraction of complex numbers
- 2.2. Multiply a complex number by a scalar value
- 2.3. The product of complex numbers
- 2.4. Square of complex numbers
- 2.5. Conjugate complex numbers
- 2.6. The absolute value of a complex number
- 2.7. Quotient of two complex numbers
- 3. Degrees
- 4. Complex plane
- 4.1. Rotors
- 5. Quaternions
- 5.1. Quaternions as an ordered pair
- 5.2. Addition and subtraction of quaternions
- 5.3. Quaternion product
- 5.4. Real Quaternion
- 5.5. Quaternion multiplication by scalar
- 5.6. Pure Quaternions
- 5.7. Additive Quaternion Form
- 5.8. Unit quaternion
- 5.9. Binary quaternion form
- 5.10. Conjugate Quaternions
- 5.11. Quaternion norm
- 5.12. Normalization of Quaternion
- 5.13. Reverse Quaternion
- 5.14. Scalar product of quaternions
- 6. Turns
- 7. Interpolation of Quaternions
- 7.1. SLERP
- 7.1.1. Quaternion Difference
- 7.1.2. Raising a quaternion to a power
- 7.1.3. Fractional Quaternion Difference
- 7.1.4. Factors to Consider
- 7.2. SQUAD
- 7.1. SLERP
- 8. Conclusion
- 9. Download demo
- 10. Reference materials
It’s impossible to fully understand quaternions in 45 minutes.
There is a lot of math in this article, so it’s not for wimps.
Introduction
In computer graphics, matrices are used to describe the position in space (movement) as well as orientation in space (rotation). You can also use a single transformation matrix to describe the scale of the object. This matrix can be considered the “basis space”. If we multiply a vector or a point (or even another matrix) by a transformation matrix, then we “transform” this vector, point or matrix into the space represented by this matrix.
In this article I will not talk in detail about the transformation matrices. Details on transformation matrices can be found in my article Matrices .
In this article I want to talk about an alternative way of describing the orientation of an object (rotation) in space using quaternions.
Complex numbers
In order to fully understand quaternions, we first need to understand where they came from. The quaternion principle is based on the concept of a system of complex numbers.
Along with the well-known sets of numbers ( natural , integer , real and rational ), the system of complex numbers adds a new set of numbers, called imaginary numbers. Imaginary numbers were invented to solve certain equations that did not have solutions, for example:
To solve this expression, we need to declare that
Mathematicians could not accept the fact that the expression has no solution, so a new concept was invented - an imaginary number that can be used to solve such equations.
The imaginary number is as follows:
Do not try to understand this assumption, because there are no logical reasons for its existence. We just need to accept that
The set of imaginary numbers can be denoted as
The set of complex numbers (denoted by the symbol
It can also be stated that all real numbers are complex with
Addition and subtraction of complex numbers
Complex numbers can be added and subtracted by adding and subtracting real and imaginary parts.
Addition:
Subtraction:
Multiply a complex number by a scalar value
The complex number is multiplied by the scalar by multiplying each member of the complex number by the scalar:
The product of complex numbers
In addition, complex numbers can also be multiplied using ordinary algebraic rules.
Square of complex numbers
Also, a complex number can be squared by multiplying by itself:
Conjugate complex numbers
The conjugate value of a complex number is a complex number with a modified sign of the imaginary part, denoted as
The multiplication of a complex number with its conjugate value gives an interesting result.
The absolute value of a complex number
We can use the adjoint number of a complex number to calculate the absolute value (or rate , or value ) of a complex number. The absolute value of a complex number is the square root of a complex number multiplied by its conjugate number . It is denoted as
Quotient of two complex numbers
To calculate the quotient of two complex numbers, we multiply the numerator and denominator by the conjugate number of the denominator.
Degrees ![$ i $](https://habrastorage.org/getpro/habr/formulas/bf8/3b5/32c/bf83b532cd867d34004f8eded8c5c79a.svg)
If we claim that
If we continue to record this series, we notice the pattern
A similar pattern occurs with increasing negative degrees.
You may have seen this pattern in mathematics, but in the form
![](https://habrastorage.org/getpro/habr/post_images/7ee/016/265/7ee016265905a34e6e27021c7a419105.png)
Cartesian plane
Complex plane
We can similarly apply complex numbers on a two-dimensional grid, called the complex plane , by tying the real part to the horizontal axis, and the imaginary part to the vertical one.
![](https://habrastorage.org/getpro/habr/post_images/e20/914/be6/e20914be6313213d1dd6bd05ca10c7c4.png)
Complex plane
As can be seen from the previous row, we can say that if we multiply the complex number by
Let's check if this is true. We take an arbitrary point on the complex plane.
and multiply it by
Multiplying
And multiplying
And multiplying
And we got exactly what we started (
![](https://habrastorage.org/getpro/habr/post_images/89c/8a2/da0/89c8a2da0a5c3f901a65024022ed2523.png)
Complex numbers on the complex plane
Now we can rotate on the complex plane and clockwise, multiplying the complex number by
Rotors
We can also perform arbitrary turns on the complex plane by specifying a complex number in the following form:
When multiplying any complex number by the rotor
What can also be written in matrix form:
What is the way to turn counterclockwise of an arbitrary point on the complex plane relative to the point of origin of coordinates.
Quaternions
Having learned about the system of complex numbers and the complex plane, we can derive them into three-dimensional space, adding to the system of numbers along with
Quaternions have the following generalized form.
Where according to Hamilton's famous expression:
You may notice that the relationship between
Hamilton also noticed that imaginary numbers
![](https://habrastorage.org/getpro/habr/post_images/e53/8a2/e2b/e538a2e2bdaaeaa1721bba02a0f35708.png)
Graphic representation of properties
The above image graphically presents the relationships between Cartesian unit vectors in the form
Quaternions as an ordered pair
We can also represent quaternions in the form of an ordered pair:
Where
With this entry, we can more easily present the common features of quaternions and complex numbers.
Addition and subtraction of quaternions
Quaternions can be added and subtracted in the same way as complex numbers:
Quaternion products
We can also express the product of two quaternions:
What gives us another quaternion. If we replace the imaginary numbers in the previous expression
And substituting back into the original expression with
Expanding this expression into the sum of ordered pairs, we get:
If you multiply by a quaternion unit and extract the common vector components, you can rewrite this equation as follows:
This equation gives us the sum of two ordered pairs. The first ordered pair is a real quaternion , and the second is a pure quaternion. These two ordered pairs can be combined into one ordered pair:
If we substitute, we get
We get:
This is the general equation of the product of quaternions.
Real Quaternion
A real quaternion is a quaternion that includes a vector
And the product of two real quaternions is another real quaternion:
Which is similar to the product of two complex numbers containing the zero imaginary term.
Quaternion multiplication by scalar
We can also multiply the quaternion by a scalar, while adhering to the following rule:
We can verify this with the help of the above product of real quaternions, by multiplying the quaternion by a scalar as the real quaternion:
Pure Quaternions
In addition to real quaternions, Hamilton also defined a pure quaternion as a quaternion with a zero scalar member:
Or if you write down the components:
And again we can take the product of two pure quaternions:
in accordance with the above rule of quaternion products.
Additive Quaternion Form
In addition, we can express quaternions as the sum of real and pure parts of a quaternion:
Unit quaternion
Taking an arbitrary vector
Combining this definition with the definition of a pure quaternion, we get:
We can also describe a unit quaternion having a zero scalar and a unit vector:
Binary quaternion form
Now we can combine the definitions of the single quaternion and the additive form of the quaternion, obtaining the form of quaternions, similar to the record used in the description of complex numbers:
This gives us a way to represent a quaternion in a form very similar to complex numbers:
Quaternion Conjugate Number
The adjoint number of a quaternion can be calculated by taking the vector part of the quaternion opposite in sign:
The product of a quaternion and its conjugate number gives us the following:
Quaternion norm
Recall the definition of the norm of a complex number:
Similarly, the rate (or value) of a quaternion is defined as:
This allows us to express the quaternion rate as follows:
Normalization of Quaternion
Having a definition of the quaternion norm, we can use it to normalize the quaternion. Quaternion is normalized by dividing by
For example, let's normalize the quaternion:
First we need to calculate the quaternion rate :
Then we must divide the quaternion by the quaternion norm to calculate the normalized quaternion:
Reverse Quaternion
Reverse quaternion is denoted by
To show this, we can use the definition of a reciprocal:
And multiply both sides by the conjugate number of the quaternion, which will give us:
By substitution we get:
For single quaternions-norms whose norm is equal to 1, we can write:
Scalar product of quaternions
Similarly to the scalar product of vectors, we can calculate the scalar product of two quaternions by multiplying the corresponding scalar parts and summing up the results:
We can also use the scalar product of quaternions to calculate the angular difference between quaternions:
For unit quaternions-norms, we can simplify the equation:
Turns
Let me remind you that we have defined a special form of a complex number called a rotor , which can be used to rotate a point on a two-dimensional plane as follows:
Due to the similarity of complex numbers with quaternions, it should be possible to express a quaternion that can be used to rotate a point in three-dimensional space:
Let's check if this theory is correct by calculating the quaternion product
BUT
Then
We see that the result is a generic quaternion with scalar and vector parts.
Let's first consider the “special” case in which
In this case, to turn
For example, let's rotate the vector
And let's take a vector
Now let's find the piece
What gives us a pure quaternion, rotated 45 ° relative to the axis
Exactly what we expected!
We can show it graphically with the following image:
![](https://habrastorage.org/getpro/habr/post_images/e9d/87b/2c9/e9d87b2c9784e8dfaa4c173efe8adb85.png)
The rotation of the quaternion (1)
Now let's consider a quaternion that is not orthogonal to
And multiplying our vector
After substitution
That is, it is no longer a pure quaternion, it is not rotated by 45 °, and the vector norm is no longer equal to 2 (it decreased to
This result can be shown graphically.
![](https://habrastorage.org/getpro/habr/post_images/e1d/483/02f/e1d48302f030b7045af4fbbb908a8d23.png)
Quaternion rotation (2)
Strictly speaking, it is incorrect to represent quaternionin three-dimensional space, because in fact it is a four-dimensional vector! For the sake of simplification, I will show only the vector component of quaternions.
However, all is not lost. Hamilton found out (but did not publish this) that if we then multiply the result
First, let's calculate
With
Combining the previous value
What is a pure quaternion, and the result rate is equal to:
which is equal to
The image below shows the result of the rotation.
![](https://habrastorage.org/getpro/habr/post_images/085/d25/966/085d25966ac77ecfbd94f309c62cab5b.png)
Rotation of the quaternion (3)
We see that the result is a pure quaternion, and the norm of the original vector is preserved, but the vector is rotated 90 °, not 45 °, which is twice as much as necessary! Therefore, to correctly rotate the vector
What is the general type of quaternion turning!
Quaternion Interpolation
One of the most important reasons for using quaternions in computer graphics is that quaternions describe turns in space very well. Quaternions eliminate the problems that aggravate other ways of turning points in 3D space, such as folding a frame , in which the problem lies in the representation of turning in the corners of Euler.
Using quaternions, we can define several methods that represent the interpolation of rotation in 3D space. The first method I consider is called SLERP . It is used to smoothly interpolate a point between two orientations. The second method is the development of SLERP and is called SQUAD . It is used for interpolation in a number of orientations that specify the path.
SLERP
SLERP stands for S pherical L inear Int erp olation (spherical linear interpolation). SLERP provides the ability to smoothly interpolate a point between two orientations.
I will mark the first orientation as
The standard linear interpolation formula is:
Here are the basic steps for applying this equation:
- Calculate the difference between
and
.
- Take the fractional part of this difference.
- Adjust the initial value to a fractional difference between two points.
We can use the same basic principle to interpolate between two orientations of quaternions.
Quaternion Difference
The first step means that we need to calculate the difference between
Raising a quaternion to a power
The next step is to take the fractional part of this difference. We can calculate the fractional part of a quaternion by raising it to a power whose value is in the interval
The general formula for raising a quaternion to a power is as follows:
Where the exponential function for quaternions looks like this:
And the logarithm of quaternion has the form:
With
And at
Fractional Quaternion Difference
To calculate the interpolated angular rotation, we change the initial orientation.
What is the general form of spherical linear interpolation for quaternions? However, this is not the kind of SLERP equation that is commonly used in practice.
We can apply a similar formula to perform spherical interpolation of vectors into quaternions. The general view of spherical interpolation for vectors is given by:
Graphically, this can be shown in the following image.
![](https://habrastorage.org/getpro/habr/post_images/f55/e29/d12/f55e29d12f9d13ed21b51b9e36502e4d.png)
Quaternion Interpolation
This formula can be applied to quaternions without any changes:
And we can get a corner
Factors to Consider
This implementation has two problems to consider when using.
First, if the scalar product of quaternions turns out to be a negative value, then the interpolation will go along the “long way” on the four-dimensional sphere, and this is not always desirable. To solve this problem, we can check the result of the scalar product, and if it is negative, then we can take the opposite of one of the orientations. Inverting the scalar and vector parts of the quaternion does not change the orientation it represents, but by doing this, we guarantee that the turn will occur along the “shortest” path.
Another problem arises if the angular difference between
SQUAD
Just as SLERP can be used for interpolation between two quaternions, SQUAD ( S pherical and Quad rangle - spherical and quadrilateral) can be used for smooth interpolation along the turn path.
If we have a number of quaternions:
And we defined the "auxiliary" quaternion (
Orientation along part of a curve is defined as:
at time t it gives us:
Conclusion
Despite the difficulty to understand, when working with turns, quaternions provide several obvious advantages compared to Euler matrices and angles.
- Interpolation of quaternions using SLERP and SQUAD provides a way to smoothly interpolate between orientations in space.
- The concatenation of turns using quaternions is faster than the union of turns expressed in a matrix form.
- For single quaternions-norms, the reciprocal of the rotation is taken by subtracting the vector part of the quaternion. The calculation of the inverse of the rotation matrix is much slower if the matrix is not orthonormal (if it is orthonormal, then this is just the matrix transposition).
- The conversion of quaternions to matrices is slightly faster than for Euler angles.
- To describe the rotation, quaternions require only 4 numbers (3, if they are normalized. The real part can be calculated during program execution), while matrices need at least 9 values.
However, along with all the advantages of using quaternions, there are also a few drawbacks.
- Quaternions may become invalid due to a round-off error of floating-point numbers; However, this "crept error" can be eliminated by renormalization of quaternion.
- Probably the most significant obstacle to the use of quaternions is the high complexity of their understanding. I hope you solve this problem by reading my article.
There are many mathematical libraries that implement quaternions, and only some of them implement quaternions correctly. In my own experience, a good mathematical library with a qualitative implementation of quaternions is the GLM (OpenGL Math Library). If you want to use quaternions in your own applications, then I recommend this library.
Download demo
I created a small demo demonstrating the use of quaternion to rotate an object in space. The demo was created in Unity 3.5.2, you can download free download this engine and view the source code of the demo. The zip file also contains the Windows binary executable, but in Unity you can build the application for Mac.
Understanding Quaternions.zip
Reference materials
![]() Vince, J (2011). Quaternions for Computer Graphics. 1st. ed. London: Springer. | ![]() Dunn, F. and Parberry, I. (2002). 3D Math Primer for Graphics and Game Development. 1st. ed. Plano, Texas: Wordware Publishing, Inc. |