Vector3 Class Reference
[Maths - 3D Vectors]

A class representing a 3vector. More...

#include <vector3.h>

List of all members.

Public Member Functions

 Vector3 ()
 Vector3 (fix x, fix y, fix z)
Vector3 operator- () const
Vector3 operator+ (const Vector3 &vector) const
Vector3 operator- (const Vector3 &vector) const
Vector3 operator* (fix scalar) const
Vector3 operator/ (fix scalar) const
Vector3operator+= (const Vector3 &vector)
Vector3operator-= (const Vector3 &vector)
Vector3operator*= (fix scalar)
Vector3operator/= (fix scalar)
bool operator== (const Vector3 &vector) const
bool operator!= (const Vector3 &vector) const
fix DotProduct (const Vector3 &vector) const
Vector3 CrossProduct (const Vector3 &vector) const
ufix Length () const
int CompareLength (ufix length) const
int CompareLengths (const Vector3 &vector) const
uint32_t LengthSquared (uint32_t &fraction) const
Vector3 UnitVector () const
Vector3 Normal (const Vector3 &vector) const
fixangle Angle (const Vector3 &vector) const
Vector3 Normal (const Vector3 &point1, const Vector3 &point2) const
fixangle Angle (const Vector3 &point1, const Vector3 &point2) const

Static Public Member Functions

static void Translate (Vector3 *outVectors, unsigned vectorCount, const Vector3 *inVectors, const Vector3 &offset)
static void Scale (Vector3 *outVectors, unsigned vectorCount, const Vector3 *inVectors, fix scale)

Public Attributes

fix X
fix Y
fix Z

Private Member Functions

void NormaliseComponents (unsigned bits)

Detailed Description

A class representing a 3vector.

The three components of the vector; X,Y and Z, are 32bit fixed point numbers with the binary point between bits 15 and 16.

See also:
fix Fix
Version:
2005-03-02
  • Made Normal() faster by using integer multiplication to calculate cross product, as opposed to using fixed-point multiplication.
2005-03-01
  • Modified Normal() so that it copes with vectors of any magnitude - by scaling the vectors prior to cross product calculation.
2006-05-20
  • Changed code to use standard typedefs, e.g. replaced uint8 with uint8_t, and made use of size_t.

Definition at line 69 of file vector3.h.


Constructor & Destructor Documentation

Vector3::Vector3 (  ) 

Constructor which doesn't initialise the vector object.

Definition at line 75 of file vector3.h.

Vector3::Vector3 ( fix  x,
fix  y,
fix  z 
)

Constructor which initialises the vector object.

Parameters:
x Value for the X component.
y Value for the Y component.
z Value for the Z component.

Definition at line 85 of file vector3.h.


Member Function Documentation

Vector3 Vector3::operator- (  )  const

Uninary minus operator.

Returns:
A vector with component values of the same magnitude as this one, but with the signs of each value reversed.

Definition at line 41 of file vector3.cpp.

Vector3 Vector3::operator+ ( const Vector3 vector  )  const

Calculate the sum of this vector and another. The resulting vector is the sum of the individual component values. If any component value overflows the range of 32bit fixed point numbers, then the result is undefined.

Parameters:
vector The vector to add to this.
Returns:
The sum of this and vector.

Definition at line 47 of file vector3.cpp.

Vector3 Vector3::operator- ( const Vector3 vector  )  const

Calculate the difference between this vector and another. The resulting vector is the difference between the individual component values. If any component value overflows the range of 32bit fixed point numbers, then the result is undefined.

Parameters:
vector The vector to subtract from this.
Returns:
The difference between this and vector.

Definition at line 53 of file vector3.cpp.

Vector3 Vector3::operator* ( fix  scalar  )  const

Scalar multiply. The resulting vector is the product of the individual component values of this vector multiplied by the given scalar value. If any component value overflows the range of 32bit fixed point numbers, then the result is undefined.

Parameters:
scalar The scalar value to multiply by.
Returns:
The product of this vector and scalar.

Definition at line 59 of file vector3.cpp.

Vector3 Vector3::operator/ ( fix  scalar  )  const

Scalar division. The resulting vector is the result of dividing the individual component values of this vector by the given scalar value. If any component value overflows the range of 32bit fixed point numbers, then the result is undefined.

Parameters:
scalar The scalar value to divide by.
Returns:
The result of dividing this vector by scalar.

Definition at line 65 of file vector3.cpp.

Vector3 & Vector3::operator+= ( const Vector3 vector  ) 

Add a vector to this one. The result is the sum of the individual component values. If any component value overflows the range of 32bit fixed point numbers, then the result is undefined.

Parameters:
vector The vector to add to this.
Returns:
A reference to this.
Postcondition:
This vector is set to the calculated sum.

Definition at line 71 of file vector3.cpp.

Vector3 & Vector3::operator-= ( const Vector3 vector  ) 

Subract a vector from this one. The result is the difference between the individual component values. If any component value overflows the range of 32bit fixed point numbers, then the result is undefined.

Parameters:
vector The vector to subtract from this.
Returns:
A reference to this.
Postcondition:
This vector is set to the calculated difference.

Definition at line 80 of file vector3.cpp.

Vector3 & Vector3::operator*= ( fix  scalar  ) 

Scalar multiply of this vector. Multiply each component of this vector by a scalar value. If any component value overflows the range of 32bit fixed point numbers, then the result is undefined.

Parameters:
scalar The scalar value to multiply by.
Returns:
A reference to this.
Postcondition:
This vector is result of the scalar multiply.

Definition at line 89 of file vector3.cpp.

Vector3 & Vector3::operator/= ( fix  scalar  ) 

Scalar divide of this vector. Divide each component of this vector by a scalar value. If any component value overflows the range of 32bit fixed point numbers, then the result is undefined.

Parameters:
scalar The scalar value to divide by.
Returns:
A reference to this.
Postcondition:
This vector is result of the scalar division.

Definition at line 98 of file vector3.cpp.

bool Vector3::operator== ( const Vector3 vector  )  const

Equality operator.

Parameters:
vector The vector to test equality with.
Returns:
True, if the individual components of this vector are the same their counterparts in vector. False, otherwise.

Definition at line 107 of file vector3.cpp.

bool Vector3::operator!= ( const Vector3 vector  )  const

Inequality operator.

Parameters:
vector The vector to test equality with.
Returns:
True, if any of individual components of this vector differ from their counterparts in vector. False, otherwise.

Definition at line 220 of file vector3.h.

fix Vector3::DotProduct ( const Vector3 vector  )  const

Calculate the dot product of this vector and a second vector.

If any product of two components is greater than 0x2aaa.aaaa then the result is undefined. This limitation can be met if all components, in both vectors, have a magnitude less than 104. (0x68.00000)

Parameters:
vector The second vector.
Returns:
The dot product of this and vector.

Definition at line 113 of file vector3.cpp.

Vector3 Vector3::CrossProduct ( const Vector3 vector  )  const

Calculate the cross product of this vector and a second vector.

If any product of two components is greater than 0x4000.0000 then the result is undefined. This limitation can be met if all components, in both vectors, have a magnitude less than 128. (0x80.00000)

Parameters:
vector The second vector.
Returns:
The cross product of this and vector

Definition at line 119 of file vector3.cpp.

ufix Vector3::Length (  )  const

Calculate the length (magnitude) of this vector. Accuracy is limited to 24 significant bits.

Returns:
The length.

Definition at line 139 of file vector3.cpp.

int Vector3::CompareLength ( ufix  length  )  const

Compare the length (magnitude) of this vector with a given value. This is faster than comparing the value returned by Length(). E.g. instead of

    if(vector.Length()<distance)

use

    if(vector.CompareLength(distance)<0)
Parameters:
length The length value to compare with.
Returns:
0, if the length of this vector is equal to length.
1, if the length of this vector is greater than length.
-1, if the length of this vector is less than length.

Definition at line 174 of file vector3.cpp.

int Vector3::CompareLengths ( const Vector3 vector  )  const

Compare the length of this vector with another. This is faster than comparing two values returned by Length(). E.g. instead of

    if(vector1.Length()<vector2.Length())

use

    if(vector1.CompareLengths(vector2)<0)
Parameters:
vector The vector to compare with.
Returns:
0, if the lengths are the same.
1, if the length of this is greater than the length of vector.
-1, if the length of this is less than the length of vector.

Definition at line 204 of file vector3.cpp.

uint32_t Vector3::LengthSquared ( uint32_t fraction  )  const

Calculate the square of the length of this vector. This is faster than Length() and is useful when comparing a vector length with a constant or pre-calculated value. (Comparing the square of lengths will give the same result as comparing the lengths.)

Parameters:
fraction Set to the fraction part of the result, i.e the 32 bits to the right of the binary point.
Returns:
The integer part of the result.

Definition at line 222 of file vector3.cpp.

Vector3 Vector3::UnitVector (  )  const

Calculate the unit vector which lies in the same direction as this vector. I.e. scale this vector so it has a length of 1.0.

Rounding inacuracies mean the length of this 'unit' vector will actualy lie in the range 0x0.fffe to 0x1.0000.

Returns:
The unit vector.

Definition at line 317 of file vector3.cpp.

Vector3 Vector3::Normal ( const Vector3 vector  )  const

Calculate the unit vector which is normal to this vector and a second vector. I.e. the unit vector which is at right angles to both this vector and the second vector; acording to the right-hand rule.

Rounding inacuracies mean the length of this 'unit' vector will actualy lie in the range 0x0.fffe to 0x1.0000.

Parameters:
vector The second vector.
Returns:
The unit vector normal to this and vector.

Definition at line 338 of file vector3.cpp.

fixangle Vector3::Angle ( const Vector3 vector  )  const

Calculate the angle between this vector and a second vector.

Parameters:
vector The second vector.
Returns:
The angle between this and vector. This lies in the range 0x0000 and 0x8000.
See also:
fixangle

Definition at line 356 of file vector3.cpp.

Vector3 Vector3::Normal ( const Vector3 point1,
const Vector3 point2 
) const

Calculate the unit vector which is normal to the two vectors formed by (point1 - this) and (point2 - this). I.e. the unit vector which is at right angles to both of these; acording to the right-hand rule.

Rounding inacuracies mean the length of this 'unit' vector will actualy lie in the range 0x0.fffe to 0x1.0000.

Parameters:
point1 The first point.
point2 The second point.
Returns:
The normal vector.

Definition at line 362 of file vector3.cpp.

fixangle Vector3::Angle ( const Vector3 point1,
const Vector3 point2 
) const

Calculate the angle between the two vectors formed by (point1 - this) and (point2 - this).

Parameters:
point1 The first point.
point2 The second point.
Returns:
The angle. This lies in the range 0x0000 and 0x8000.
See also:
fixangle

Definition at line 368 of file vector3.cpp.

void Vector3::Translate ( Vector3 outVectors,
unsigned  vectorCount,
const Vector3 inVectors,
const Vector3 offset 
) [static]

Translate an array of vectors. I.e. add the given offset to each vector in an array.

Parameters:
outVectors Pointer to array where translated vectors will be written to.
vectorCount Number of vectors in array.
inVectors Pointer to array vectors to be translated.
offset The offset to add to each vector in the array.

Definition at line 374 of file vector3.cpp.

void Vector3::Scale ( Vector3 outVectors,
unsigned  vectorCount,
const Vector3 inVectors,
fix  scale 
) [static]

Scale an array of vectors. I.e. perform a scalar multiply on each vector in an array.

Parameters:
outVectors Pointer to array where scaled vectors will be written to.
vectorCount Number of vectors in array.
inVectors Pointer to array vectors to be scaled.
scale The value to scale each component of each vector by.

Definition at line 391 of file vector3.cpp.

void Vector3::NormaliseComponents ( unsigned  bits  )  [private]

Scale the components of this array such that the component with the largest magnitude has its most-significant bit at the given position.

Parameters:
bits Bit position for MSB of largest component.

Definition at line 269 of file vector3.cpp.


Member Data Documentation

The X component

Definition at line 404 of file vector3.h.

The Y component

Definition at line 405 of file vector3.h.

The Z component

Definition at line 406 of file vector3.h.


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

Generated by  doxygen 1.6.1