Quaternion Rotation Lab
quaternion.jpg

Learning Objectives

  1. Create a new class called Quaternion
  2. Overload operators two define operations on quaternions
  3. Define a method for rotating about an arbitrary axis through the origin via quaternions

Introduction

We have already established methods of rotating 3D objects around the 3 major axes using matrices. We also saw that rotating about an arbitrary axis in 3D required 7 matrix transformations. This process can be made more efficient via the use of quaternions

Quaternion Class

Before we can accomplish our rotation we need to create a class called Quaternion, so that we can store and manipulate quaternions. Remember that a quaternion has the form

(1)
\begin{equation} a+bi+cj+dk \end{equation}

This is sometimes expressed as a scalar/vector pair as follows

(2)
\begin{align} [a,\vec{v}] \end{align}

where $\vec{v}=<b,c,d>$. So in your Quaternion class, have the constructor take a float and a Vector3D.

Overloading Operators

We want to be able to manipulate our quaternions via all the defined operations. Given that $q_1=[s_1,\vec{v_1}]$ and $q_2=[s_2,\vec{v_2}]$, overload operators to achieve the following

  • Addition / Subtraction
(3)
\begin{align} q_1 \pm q_2 = [s_1+s_2,\vec{v_1}+\vec{v_2}] \end{align}
  • Scalar Multiplication
(4)
\begin{align} kq_1=[ks_1,k\vec{v_1}] \end{align}
  • Quaternion Multiplication
(5)
\begin{align} q_1 \cdot q_2 = [s_1s_2-\vec{v_1} \cdot \vec{v_2},s_1 \vec{v_2}+s_2\vec{v_1}+\vec{v_1} \times \vec{v_2}] \end{align}
  • Magnitude
(6)
\begin{align} |q_1|=\sqrt{s_1^2+|\vec{v_1}|^2} \end{align}
  • Conjugate
(7)
\begin{align} q_1^*=[s_1,-\vec{v_1}] \end{align}
  • Inverse
(8)
\begin{align} q_1^{-1}=\frac{q_1^*}{|q_1|^2} \end{align}

Rotations

Next we want to create a method on our Quaternion class that will take as parameters a quaternion representing the axis of rotation and an angle representing the angle of rotation. This method will be called by the quaternion representing our point and return a new quaternion representing our rotated point.
To rotate a point whose position is represented by the vector $\vec{p}$ by an angle $\theta$ (Using the right hand rule), we first create the quaternion $P=[0,\vec{p}]$ to represent this point. Now we create a quaternion to represent our axis of rotation. If our axis of rotation passes through the origin and is parallel to the unit vector $\vec{l}$, then our corresponding rotation quaternion is going to be $Q=[\cos \frac{\theta}{2},\vec{l} \sin \frac{\theta}{2}]$. To find the quaternion $P_r$ representing our rotated point, we do the following,

(9)
\begin{equation} P_r=QPQ^{-1} \end{equation}

As mentioned in the text, this is equivalent to

(10)
\begin{equation} P_r=QPQ^{*} \end{equation}

Prelab Questions and Exercises

Using the quaternion $p=[1,<0,2,-3>]$ and $q=[2,<4,-1,1>]$

  1. Calculate $p+q$
  2. Calculate $p-q$
  3. Calculate $5p$
  4. Calculate $pq$
  5. Calculate $|p|$
  6. Calculate $p^*$
  7. Calculate $p^{-1}$

Laboratory Procedures

1. Create a Quaternion class with the following overloaded operations

  • Addition / Subtraction
  • Scalar Multiplication
  • Quaternion Multiplication
  • Magnitude
  • Conjugate
  • Inverse

2. Create a method on the Quaternion class for rotating a point about an arbitrary axis through the origin

3. Create a program that asks the user for two quaternions and a scalar

  • report the sum of the two quaternions
  • report the difference of the two quaternions
  • report the scalar times each quaternion
  • report the product of the two quaternions
  • report the magnitude of each quaternion
  • report the conjugate of each quaternion
  • report the inverse of each quaternion

4. Test your code using the questions from the pre-lab, and your pre-lab results.

5. Execute the above program, for your instructor’s verification. Initials:_

6. Create a program that asks the user for the coordinates of a point, the components of a vector and an angle in degrees

  • report the coordinates of the new point obtained by rotating the given point about the line through the origin parallel to the given vector by the given angle.

7. Test your code using the questions from the pre-lab, and your pre-lab results.

8. Execute the above program, for your instructor’s verification. Initials:_

Postlab Questions

  1. What cautions if any must be taken in operating on quaternions?
  2. What cautions if any must be taken in rotating using quaternions?
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License