blQuaternion — A simple quaternion in c++

Introduction

This is part of my on going effort to publish all the mathematical tools that I have developed and used over the years.  In this article, I present a simple quaternion class, which I use in many applications, from machine vision to physics simulation, to 3d graphics. As with the rest of my work, simplicity and code readability are my top priorities, because the key to code reuse, is to make the code understandable.

Here’s a little snippet showing how easy this quaternion class is to use:

int main(int argc, char *argv[])
{
    // Create a quaternion using angle/axis
    blQuaternion<double> Qtn1(10,1,10,2);

    cout < < "Qtn1 = " << Qtn1 << "\n";
    cout << "Qtn1 Angle = " << Qtn1.m_Angle << "\n";
    cout << "Qtn1 Axis = " << blVector3d<double>(Qtn1.m_Ax,Qtn1.m_Ay,Qtn1.m_Az) < < "\n\n";

    // Create a quaternion using its components
    blQuaternion<double> Qtn2(0,blVector3d</double><double>(1,2,4));

    cout < < "Qtn2 = " << Qtn2 << "\n";
    cout << "Qtn2 Angle = " << Qtn2.m_Angle << "\n";
    cout << "Qtn2 Axis = " << blVector3d<double>(Qtn2.m_Ax,Qtn2.m_Ay,Qtn2.m_Az) < < "\n";

    // Multiply the two quaternions normalizing and without normalizing
    // Notice how multiplying by scalar makes no difference
    // when using normalization
    cout << "Qtn1*Qtn2 (with normalization) = " << Qtn1*(3.0*Qtn2) << "\n";
    cout << "Qtn1*Qtn2 (without normalization) = " << MultiplyQtnsWithoutNormalizing(Qtn1,3.0*Qtn2) << "\n";

    int waitforkey;
    cin >> waitforkey;
}

The resulting console output is:

Dependencies

  1. blVector3d from my blMathAPI library
    1. Used to store its imaginary values
  2. iostream library
    1. Used to output to console

The code

blQuaternion structure is simple, but provides overloaded operators to make the code very readable. The structure is saved in a file called blQuaternion.hpp, which uses inline documentation.

blQuaternion.hpp (Click to see code…)

About Vincenzo Barbato

Known to his friends as Enzo, he's an outside-the-box engineer/researcher whose interests and expertise span many fields, including controls systems, multi-physics simulations, mechatronics, oil technologies, data analysis and machine vision just to name a few.

Refusing to grow up, he's on a continuous journey to develop simple and creative solutions that have the power of disrupting industries by optimizing systems and processes.

Married to a beautiful wife, with two beautiful daughters and two identical twin boys, his home is a never ending chaotic fountain of inspiration.

His outlook on life:

"Never blindly accept what you're told, listen, but then question, with curiosity, creativity and collaboration we can change the world"

About Enzo

Known to his friends as Enzo, he's an outside-the-box engineer/researcher whose interests and expertise span many fields, including controls systems, multi-physics simulations, mechatronics, oil technologies, data analysis and machine vision just to name a few. Refusing to grow up, he's on a continuous journey to develop simple and creative solutions that have the power of disrupting industries by optimizing systems and processes. Married to a beautiful wife, with two beautiful daughters and two identical twin boys, his home is a never ending chaotic fountain of inspiration. His outlook on life: "Never blindly accept what you're told, listen, but then question, with curiosity, creativity and collaboration we can change the world"

One Comment

  1. Pingback: blRigidBodyAPI — A simple rigid body API in c++ | BarbatoLabs

Leave a Reply

Your email address will not be published. Required fields are marked *