16
$\begingroup$

Quaternions (a four-dimensional extension of complex numbers) can used to represent rotation and scaling of a 3D vector, and the application of a quaternion onto a 3D vector involves two quaternion multiplications, thus requiring fewer operations than multiplication by the corresponding transformation matrix. However, linear and affine transformation matrixes are often used instead, especially in shader code.

When is it appropriate and preferable (due to speed, stability, etc.) to use a quaternion to represent scaling and rotation in three dimensions, instead of the corresponding transformation matrix?

$\endgroup$
1

4 Answers 4

9
$\begingroup$

I want to start with misconceptions:

Modern GPUs (NVIDIA for quite a while, and AMD since Southern Islands) do not meaningfully support vector/matrix operations natively in hardware. They are vector architectures in a different direction: each component of a vector (x, y, z) are generally 32- or 64-valued, containing values for each element in a lane. So a 3D dot product is not usually an instruction, it is a multiply and two multiply-adds.

Additionally, counting primitive operations like multiply-add, transforming a vector by a quaternion is more expensive than transforming a vector by a matrix. Transforming a vector by a 3x3 matrix is 3 multiplies and 6 multiply-adds, and transforming a vector by a quaternion is two quaternion multiplies, each of which consist of 4 multiplies and 12 multiply-adds. (You can get less naïve than this—here's a writeup on a faster way—but it's still not as cheap as multiplying a vector by a matrix.)

However, performance is not always determined simply by counting the number of ALU operations it performs. Quaternions require less space than the equivalent matrix (assuming you are only doing pure rotation/scale), and that means less storage space and less memory traffic. This is often important in animation (which is conveniently also often where the nice interpolation properties of quaternions show up).

Other than that:

  • Matrices use more space because they support more operations. A 3x3 matrix can contain nonuniform scale, skew, reflection, and orthogonal projection.
  • Matrices can be naturally thought of as basis vectors, and easily constructed from those vectors.
  • Multiplying one quaternion by another (composing two rotations) is less operations than multiplying one matrix by another.
$\endgroup$
2
  • $\begingroup$ Funny, and on Intel Haswell GPU ARBfp's DP3 instruction appears implemented as 3 multiplies and 2 additions, see INTEL_DEBUG=fs output from Intel Linux driver: paste.ubuntu.com/23150494 . Not sure whether it's just poor driver or the HW really doesn't have special vector mul instructions. $\endgroup$
    – Ruslan
    Sep 8, 2016 at 15:14
  • $\begingroup$ @Ruslan Very likely that hardware just doesn't have special vector mul instructions. More accurately, though, they do, but they are vectorized across the SIMD width of the architecture (the lanes), not vectorized across the vec3/vec4 dimension. $\endgroup$ Sep 8, 2016 at 18:17
6
$\begingroup$

(A lot of information here I shamelessly borrowed from joojaa's and ratchet freak's answers, with some notes of my own.)

Matrix Advantages

  • Non uniform scaling and rotation, skewing, projection
  • Translation (unless using dual quaternions)
  • Native hardware support
  • Quaternions often require transcendental functions to construct
  • Easier to understand

Quaternion Advantages

  • Transforming a vector requires fewer operations (Or not - See John's answer)
  • Transforming by another quat requires many fewer operations
  • Quaternions occupy 4 floats, (8 if it's a dual) but Matrices occupy 9-16 floats

If you know you're only going to be doing uniform rigid body transforms, a vector/quat pair is usually a solid win on a 3x4 matrix in terms of storage space (vector/quat: 7 or 8 floats vs mat3x4: 12 floats) and processing speed. If Quaternions are still mystical voodoo to you, try this web series on them.

$\endgroup$
3
$\begingroup$

Matrices offer more possible transforms than quaternions, it is possible to skew, mirror and non uniformly scale the matrix. There is nothing that states you can not make your engine do just quaternion based transforms, if you have no need for the additional transform features.

Matrices are just very convenient when you need to build spaces where you know the basis vectors. Such as when doing projections into orthographic. Also doing perspective transform in a matrix space is easy. Matrices are superior when it comes to projecting stuff.

In a way matrices are usually used because they represent the most common denomination and aren't too complicated to master and understand. The benefits of standardization far outweighs the benefit you get form a custom workflow. Its well known how to do the matrix operations. Whereas quats are not something most get immediate introduction to in uni. Just ask around how many know how to invert a quaternion, whereas you dont find many students in higher education who dont know how to invert a matrix.

Note that graphics cards also have dedicated pipes for matrix operations.

$\endgroup$
1
  • $\begingroup$ I've actually been thinking about this. Ive also toyed on asking what other approaches modeling pipes than matrix and quat hierarchies one could use. $\endgroup$
    – joojaa
    Aug 9, 2015 at 13:17
3
$\begingroup$

A quaternion can only represent uniform scaling and rotation so if you need anything else you would need to add something to represent that.

Translation can be done with a single additional vec3 (or using dual quaternions). However non-uniform scaling and sheering is represented better by a mat4. Projection transforms (essentially non-uniform scaling and swapping z and w) cannot be represented by a quaternion.

Quaternions have a major advantage when interpolating. The slerp is most easily calculated using quaternions.

Applying a quaternion (or dual quaternion) is not built into the GPU so you would need to implement that using the vector operations. Most quaternion libraries assume that you won't be using the quaternion to represent scale so that's something to look out for.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.