MinGfx Toolkit  1.0
A minimal library for writing cross-platform (Windows, OSX, linux) graphics programs.
Public Member Functions | Static Public Member Functions | List of all members
mingfx::Vector3 Class Reference

Detailed Description

A 3D Vector with floating point coordinates, used for storing normals and all sorts of other 3D graphics operations.

Vector3s can be transformed by a Matrix4, and a Vector3 can be created by subtracting two Point3s. Example:

// subtracting two points creates a vector
Point3 a(0,0,0);
Point3 b(2,0,0);
Vector3 c = b - a;
// vectors can be transformed by Matrix4s
Vector3 dir = c.ToUnit();
Matrix4 M = Matrix4::RotateX(GfxMath::ToDegrees(30.0));
Vector3 dir_transformed = M * dir;
// vectors can be added and subtracted
Vector3 d(1,0,0);
Vector3 e = c + d;
// and we can do the usual dot products and cross products too
float f = d.Dot(e);
Vector3 g = b.Cross(d);
// you can access the individual components of the vector in two ways:
Vector3 v(1,2,3);
float option1 = v.x();
float option2 = v[0];
// to set an individual component of the vector use the [] operator:
w[0] = 0.4;
w[1] = 1.2;
w[2] = 3.1;
// you can print the vector by sending it to stdout:
std::cout << v << std::endl;
static float ToDegrees(float radians)
float w() const
In homogeneous coordinates, the w coordinate for all vectors is 0.0.
Definition: vector3.h:115
Vector3()
Default constructor to create zero vector.

Definition at line 62 of file vector3.h.

#include <vector3.h>

Public Member Functions

 Vector3 ()
 Default constructor to create zero vector. More...
 
 Vector3 (float x, float y, float z)
 Constructs a vector (x,y,z,0), where the 0 comes from the use of homogeneous coordinates in computer graphics. More...
 
 Vector3 (float *v)
 Constructs a vector given a pointer to x,y,z data. More...
 
 Vector3 (const Vector3 &v)
 Copy constructor for vector. More...
 
virtual ~Vector3 ()
 Vector destructor. More...
 
bool operator== (const Vector3 &v) const
 Check for "equality", taking floating point imprecision into account. More...
 
bool operator!= (const Vector3 &v) const
 Check for "inequality", taking floating point imprecision into account. More...
 
Vector3operator= (const Vector3 &v)
 Vector assignment operator. More...
 
float operator[] (const int i) const
 Read only access to the ith coordinate of the vector. More...
 
float & operator[] (const int i)
 Returns a reference to the ith coordinate of the vector. Use this accessor if you wish to set the coordinate rather than just request its value. Example: More...
 
float x () const
 Read only access to the x coordinate. Can also use my_vector[0]. Use the my_vector[0] = 1.0; form if you need to set the value. More...
 
float y () const
 Read only access to the y coordinate. Can also use my_vector[1]. Use the my_vector[1] = 1.0; form if you need to set the value. More...
 
float z () const
 Read only access to the z coordinate. Can also use my_vector[2]. Use the my_vector[2] = 1.0; form if you need to set the value. More...
 
float w () const
 In homogeneous coordinates, the w coordinate for all vectors is 0.0. More...
 
float Dot (const Vector3 &v) const
 Returns "this dot v", for example: More...
 
Vector3 Cross (const Vector3 &v) const
 Returns "this cross v", for example: More...
 
float Length () const
 Returns the length of the vector. More...
 
void Normalize ()
 Normalizes the vector by making it unit length. More...
 
Vector3 ToUnit () const
 Returns a normalized (i.e., unit length) version of the vector without modifying the original 'this' vector. More...
 
const float * value_ptr () const
 Returns a const pointer to the raw data array. More...
 
Vector3 Lerp (const Vector3 &b, float alpha) const
 Linear interpolation between this vector and another. Alpha=0.0 returns this vector, and alpha=1.0 returns the other vector, other values blend between the two. More...
 

Static Public Member Functions

static const Vector3Zero ()
 (0,0,0) - a shortcut for a special vector that is frequently needed More...
 
static const Vector3One ()
 (1,1,1) - a shortcut for a special vector that is frequently needed More...
 
static const Vector3UnitX ()
 (1,0,0) - a shortcut for a special vector that is frequently needed More...
 
static const Vector3UnitY ()
 (0,1,0) - a shortcut for a special vector that is frequently needed More...
 
static const Vector3UnitZ ()
 (0,0,1) - a shortcut for a special vector that is frequently needed More...
 
static Vector3 Normalize (const Vector3 &v)
 Returns a new vector that is the unit version of v. More...
 
static Vector3 Cross (const Vector3 &v1, const Vector3 &v2)
 Returns v1 cross v2. More...
 
static float Dot (const Vector3 &v1, const Vector3 &v2)
 Returns v1 dot v2. More...
 
static Vector3 Lerp (const Vector3 &a, const Vector3 &b, float alpha)
 Linear interpolation between two vectors. Alpha=0.0 returns 'a' and alpha=1.0 returns 'b', other values blend between the two. More...
 

Constructor & Destructor Documentation

◆ Vector3() [1/4]

mingfx::Vector3::Vector3 ( )

Default constructor to create zero vector.

◆ Vector3() [2/4]

mingfx::Vector3::Vector3 ( float  x,
float  y,
float  z 
)

Constructs a vector (x,y,z,0), where the 0 comes from the use of homogeneous coordinates in computer graphics.

◆ Vector3() [3/4]

mingfx::Vector3::Vector3 ( float *  v)

Constructs a vector given a pointer to x,y,z data.

◆ Vector3() [4/4]

mingfx::Vector3::Vector3 ( const Vector3 v)

Copy constructor for vector.

◆ ~Vector3()

virtual mingfx::Vector3::~Vector3 ( )
virtual

Vector destructor.

Member Function Documentation

◆ Cross() [1/2]

Vector3 mingfx::Vector3::Cross ( const Vector3 v) const

Returns "this cross v", for example:

Vector3 x(1,0,0);
Vector3 y(0,1,0);
Vector3 z = x.Cross(y);
float z() const
Read only access to the z coordinate. Can also use my_vector[2]. Use the my_vector[2] = 1....
Definition: vector3.h:112
float x() const
Read only access to the x coordinate. Can also use my_vector[0]. Use the my_vector[0] = 1....
Definition: vector3.h:104
float y() const
Read only access to the y coordinate. Can also use my_vector[1]. Use the my_vector[1] = 1....
Definition: vector3.h:108

◆ Cross() [2/2]

static Vector3 mingfx::Vector3::Cross ( const Vector3 v1,
const Vector3 v2 
)
static

Returns v1 cross v2.

This is just an alternative syntax for Cross(). Example: ~~~ Vector3 x(1,0,0); Vector3 y(0,1,0); Vector3 z1 = Vector3::Cross(x,y); Vector3 z2 = x.Cross(y); z1 and z2 are the same. ~~~

◆ Dot() [1/2]

float mingfx::Vector3::Dot ( const Vector3 v) const

Returns "this dot v", for example:

Vector3 a(1,0,0);
Vector3 b(0.5,0,0);
float c = a.Dot(b);

◆ Dot() [2/2]

static float mingfx::Vector3::Dot ( const Vector3 v1,
const Vector3 v2 
)
static

Returns v1 dot v2.

This is just an alternative syntax for Dot(). Example: ~~~ Vector3 a(1,0,0); Vector3 b(0.5,0,0); Vector3 c1 = a.Dot(b); Vector3 c2 = Vector3::Dot(a,b); c1 and c2 are the same. ~~~

◆ Length()

float mingfx::Vector3::Length ( ) const

Returns the length of the vector.

◆ Lerp() [1/2]

static Vector3 mingfx::Vector3::Lerp ( const Vector3 a,
const Vector3 b,
float  alpha 
)
static

Linear interpolation between two vectors. Alpha=0.0 returns 'a' and alpha=1.0 returns 'b', other values blend between the two.

◆ Lerp() [2/2]

Vector3 mingfx::Vector3::Lerp ( const Vector3 b,
float  alpha 
) const

Linear interpolation between this vector and another. Alpha=0.0 returns this vector, and alpha=1.0 returns the other vector, other values blend between the two.

◆ Normalize() [1/2]

void mingfx::Vector3::Normalize ( )

Normalizes the vector by making it unit length.

◆ Normalize() [2/2]

static Vector3 mingfx::Vector3::Normalize ( const Vector3 v)
static

Returns a new vector that is the unit version of v.

This is just an alternative syntax for ToUnit(). Example: ~~~ Vector3 a(100,150,80); Vector3 b = Vector3::Normalize(a); Vector3 c = a.ToUnit(); b and c are the same. ~~~

◆ One()

static const Vector3& mingfx::Vector3::One ( )
static

(1,1,1) - a shortcut for a special vector that is frequently needed

◆ operator!=()

bool mingfx::Vector3::operator!= ( const Vector3 v) const

Check for "inequality", taking floating point imprecision into account.

◆ operator=()

Vector3& mingfx::Vector3::operator= ( const Vector3 v)

Vector assignment operator.

◆ operator==()

bool mingfx::Vector3::operator== ( const Vector3 v) const

Check for "equality", taking floating point imprecision into account.

◆ operator[]() [1/2]

float& mingfx::Vector3::operator[] ( const int  i)

Returns a reference to the ith coordinate of the vector. Use this accessor if you wish to set the coordinate rather than just request its value. Example:

a[0] = 5.0; // set the x-coordinate of the vector

◆ operator[]() [2/2]

float mingfx::Vector3::operator[] ( const int  i) const

Read only access to the ith coordinate of the vector.

◆ ToUnit()

Vector3 mingfx::Vector3::ToUnit ( ) const

Returns a normalized (i.e., unit length) version of the vector without modifying the original 'this' vector.

◆ UnitX()

static const Vector3& mingfx::Vector3::UnitX ( )
static

(1,0,0) - a shortcut for a special vector that is frequently needed

◆ UnitY()

static const Vector3& mingfx::Vector3::UnitY ( )
static

(0,1,0) - a shortcut for a special vector that is frequently needed

◆ UnitZ()

static const Vector3& mingfx::Vector3::UnitZ ( )
static

(0,0,1) - a shortcut for a special vector that is frequently needed

◆ value_ptr()

const float* mingfx::Vector3::value_ptr ( ) const

Returns a const pointer to the raw data array.

◆ w()

float mingfx::Vector3::w ( ) const
inline

In homogeneous coordinates, the w coordinate for all vectors is 0.0.

Definition at line 115 of file vector3.h.

◆ x()

float mingfx::Vector3::x ( ) const
inline

Read only access to the x coordinate. Can also use my_vector[0]. Use the my_vector[0] = 1.0; form if you need to set the value.

Definition at line 104 of file vector3.h.

◆ y()

float mingfx::Vector3::y ( ) const
inline

Read only access to the y coordinate. Can also use my_vector[1]. Use the my_vector[1] = 1.0; form if you need to set the value.

Definition at line 108 of file vector3.h.

◆ z()

float mingfx::Vector3::z ( ) const
inline

Read only access to the z coordinate. Can also use my_vector[2]. Use the my_vector[2] = 1.0; form if you need to set the value.

Definition at line 112 of file vector3.h.

◆ Zero()

static const Vector3& mingfx::Vector3::Zero ( )
static

(0,0,0) - a shortcut for a special vector that is frequently needed


The documentation for this class was generated from the following file: