blVector3d — A simple 3d vector in c++

Introduction

As I have mentioned in my blPoint2d article, over the years, I have developed many mathematical tools that I have used time over time. In this article, I present a simple three-dimensional vector 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 3d vector class is to use:

int main(int argc, char *argv[])
{
    // Create a few Vectors of different types
    blVector3d<double> V1(5.1,1.89,11.2);
    blVector3d<int> V2(10,2,5);
    blVector3d<float> V3(8,6.7,0.9);
    blVector3d<char> V4(98,110,6);

    cout << "V1 = " << V1 << "\n";
    cout << "V2 = " << V2 << "\n";
    cout << "V3 = " << V3 << "\n";
    cout << "V4 = " << V4 << "\n";
    cout << "DotV1V2 = " << V1*V2 << "\n";
    cout << "Mag of V1 = " << V1.GetMagnitude() << "\n";
    cout << "Norm1 of V1 = " << V1.Norm1() << "\n";
    cout << "Norm2 of V1 = " << V1.Norm2() << "\n";
    cout << "Infinity Norm of V1 = " << V1.NormInf() << "\n";

    int waitforkey;
    cin >> waitforkey;
}

The resulting console output is:

Dependencies

  1. std::abs from the standard library
    1. Used to calculate the norms
  2. std::sqrt from the standard library
    1. Used to calculate the magnitude
  3. iostream library
    1. Used to output to console

The code

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

blVector3d.hpp (Click to see code…)

Usage

This 3d vector structure can be used for anything from graphics to physics simulations, and the code is very readable. Here’s a little snippet showing just how easy it is to use:

int main(int argc, char *argv[])
{
    // Create a few Vectors of different types
    blVector3d<double> V1(5.1,1.89,11.2);
    blVector3d<int> V2(10,2,5);
    blVector3d<float> V3(8,6.7,0.9);
    blVector3d<char> V4(98,110,6);

    cout << "V1 = " << V1 << "\n";
    cout << "V2 = " << V2 << "\n";
    cout << "V3 = " << V3 << "\n";
    cout << "V4 = " << V4 << "\n";
    cout << "DotV1V2 = " << V1*V2 << "\n";
    cout << "Mag of V1 = " << V1.GetMagnitude() << "\n";
    cout << "Norm1 of V1 = " << V1.Norm1() << "\n";
    cout << "Norm2 of V1 = " << V1.Norm2() << "\n";
    cout << "Infinity Norm of V1 = " << V1.NormInf() << "\n";

    int waitforkey;
    cin >> waitforkey;
}

The resulting console output follows:

Downloads

I have put all the files into a zip file which can be downloaded here.  All you have to do is extract it somewhere, let’s say in a directory called blMathAPI, and then include the blMathAPI.hpp file as follows:

#include "blMathAPI.hpp"
using namespace blMathAPI;

Note: Everything is declared in the namespace blMathAPI, and such you would use it as: blMathAPI::blVector3d
[download#2]

Updates

  1. Nov/16/2010 — Somehow I had forgotten to include a Cross Product function in the file, so I just did that and re-uploaded the blMathAPI library.
  2. Dec/13/2010 — Changed its overloaded operators to “const”. This fixes any obscure error about passing “this” as const argument when passing blVector3d as const
  3. Feb/09/2011 — Added equality and inequality operators so to be able to compare two vectors
  4. Feb/22/2011 — Added a function to get inverted elements of a vector

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"

4 Comments

  1. Pingback: blQuaternion — A simple quaternion in c++ | BarbatoLabs

  2. Pingback: blMatrix3d — A simple 3×3 matrix in c++ | BarbatoLabs

  3. Pingback: blShapeAPI — A simple c++ shape API to construct and render static and live shapes in opengl | BarbatoLabs

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

Leave a Reply

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