Vector Class Reference

This reference page is linked to from the following overview topics: Math and Geometry Utilities.



Detailed Description

Represents a 3D vector or point with S23E8 floating point elements.

Examples:

CurveBrush/CurveCreator.h, MeshDisplace/displacer.h, and PLYImport/Importer.cpp.

Definition at line 35 of file math.h.

#include <math.h>

List of all members.

Public Member Functions

  Vector (void) throw ()
  Constructs the vector with zero values.
  Vector (float fX, float fY, float fZ=0.0f) throw ()
  Constructs the vector with specified values.
  Vector (const Vector &vVector) throw ()
  Copy constructor.
  Vector (const char *pVector) throw ()
  Construct the vector from a pointer to 8bit signed data.
  Vector (const short *pVector) throw ()
  Construct the vector from a pointer to 16bit signed data.
Vector Set (float fX, float fY, float fZ) throw ()
  Sets the values of the vector elements and returns the vector.
Vector Clear (void) throw ()
  Zeros all the components of the Vector, then returns it.
Vector Normalize (void) throw ()
  Normalizes the vector and returns it.
Vector NormalizeApprox (void) throw ()
  Normalizes the vector and returns it.
Vector  Normalized (void) const throw ()
  Returns a normalized version of the vector, without changing this vector.
Vector MakeOrthogonal (const Vector &vBase)
  Makes this vector orthogonal to the given vBase vector and normalizes it.
Vector SetLength (float fLength)
  Sets the vector to the given length, without changing its direction.
Vector RotateOrthogonal (const Vector &vBase)
  Rotates this vector to be orthogonal to vBase without changing its length, and returns it.
float  LengthSquare (void) const
  Returns the square of the length of the vector.
float  Length () const throw ()
  Returns the length of the vector.
float  Length2D () const throw ()
  Returns the 2D length of the vector, ignoring the z element.
float  DistanceFrom (const Vector &v) const
  Returns the distance between two points (this and another, represented as Vector objects).
float  DistanceFromLine (const Vector &v0, const Vector &v1) const
  Returns the shortest distance between this point and a line defined by two other points (all points represented by vector objects).
float  DistanceFromSegment (const Vector &v0, const Vector &v1) const
  Returns the distance between this point and a line segment defined by the two ending points.
float  DistanceFromTriangleSquared (const Vector &v0, const Vector &v1, const Vector &v2, float *aBaryCoords=0) const
  Returns the squared distance from this point to the closest point on the triangle defined by v0, v1, v2.
const Vector Relocate2D (const Vector &v0, const Vector &v1)
  Converts this (2d) vector to represent itself as a linear combination of the supplied basis vectors.
void  Relocate (const Vector &v0, const Vector &v1, const Vector &v2)
  Converts this vector to represent itself as a linear combination of the supplied basis vectors.
bool  Relocate2DQuad (const Vector &v0, const Vector &v1, const Vector &v2, const Vector &v3)
  This operation calculates the baricentric coordinates inside a quad where the quad corners are v0,v1,v2,v3.
bool  RelocateQuad (const Vector &v0, const Vector &v1, const Vector &v2, const Vector &v3)
  This operation calculates the baricentric coordinates inside a quad where the quad corners are v0,v1,v2,v3.
float  AngleCos (const Vector &v1) const throw ()
  Returns the cosine of the angle between this vector and the one passed in.
Vector  Minimum (const Vector &o)
  Returns the minimum of this and the argument vector.
Vector  Maximum (const Vector &o)
  Returns the maximum of this and the argument vector.
Vector  operator- (void) const throw ()
  Returns the inverse of the vector.
Vector  operator+ (float f) const throw ()
  Returns the sum of a vector and a scalar.
Vector  operator+ (const Vector &v) const throw ()
  Returns the sum of two vectors.
Vector  operator- (const Vector &v) const throw ()
  Returns the difference between two vectors/points.
Vector  operator* (const Vector &v) const throw ()
  Returns the product of two vectors by components.
Vector  operator* (float f) const throw ()
  Returns the product of the vector and a scalar.
Vector  operator/ (const Vector &v) const throw ()
  Returns the quotient of two vectors by components.
Vector  operator/ (float f) const throw ()
  Returns the quotient of a vector and a scalar.
Vector  operator* (int i) const throw ()
  Returns the product of a vector and an integer.
Vector  operator/ (int i) const throw ()
  Returns the quotient of a vector and an integer.
Vector  operator/ (unsigned int i) const throw ()
  Returns the quotient of a vector and an unsigned integer.
float  operator| (const Vector &v) const throw ()
  Returns the dot product of two vectors.
Vector  operator& (const Vector &v) const
  Returns the cross product of two vectors.
bool  operator== (const Vector &v) const throw ()
  Returns true if the two vectors are identical, false otherwise.
bool  operator!= (const Vector &v) const throw ()
  Returns true if the two vectors are different, false otherwise.
  operator bool (void) const throw ()
  Returns true if the vector is not a zero vector, false otherwise.
bool  operator! (void) const throw ()
  Returns true if the vector is a zero vector, false otherwise.
Vector operator= (const Vector &v)
  Sets this vector to equal another one and returns it.
Vector operator<< (const Vector &v)
  Sets this vector to equal another one and returns it.
Vector operator-= (const Vector &v) throw ()
  Subtracts the component values of another vector from this one's, and returns the result.
Vector operator+= (const Vector &v) throw ()
  Adds another vector to this one and returns the result.
Vector operator*= (float f)
  Multiplies the components of the vector with a scalar value and returns the result.
Vector operator*= (const Vector &v)
  Multiplies the vector with another vector by components and returns the result.
Vector operator/= (float f)
  Divides the vector by a scalar and returns the result.
float &  operator[] (int i) throw ()
  Returns a individual components of the vector.
float &  operator[] (unsigned int i) throw ()
  Returns a component of the vector.
  operator const float * (void) const
  Returns a pointer to the data of the vector. Do not delete this pointer.

Public Attributes

union {
   struct {
      float   m_fX
      float   m_fY
      float   m_fZ
   } 
   struct {
      float   x
      float   y
      float   z
   } 
   float   m_aCoors [3]
}; 

Constructor & Destructor Documentation

Vector ( void  ) throw () [inline]

Constructs the vector with zero values.

Definition at line 39 of file math.h.

{ x = y = z = 0.0f; };
Vector ( float  fX,
float  fY,
float  fZ = 0.0f 
) throw () [inline]

Constructs the vector with specified values.

Definition at line 42 of file math.h.

    { x = fX; y = fY; z = fZ; };
Vector ( const Vector vVector ) throw () [inline]

Copy constructor.

Definition at line 46 of file math.h.

    { x = vVector.x; y = vVector.y; z = vVector.z; };
Vector ( const char *  pVector ) throw () [inline]

Construct the vector from a pointer to 8bit signed data.

The array must have at least 3 elements, with values in the range -127 to +127.

Definition at line 52 of file math.h.

    { x = pVector[0]*(1.0f/127.0f); y = pVector[1]*(1.0f/127.0f); z = pVector[2]*(1.0f/127.0f); };
Vector ( const short *  pVector ) throw () [inline]

Construct the vector from a pointer to 16bit signed data.

The array must have at least 3 elements in the range -32767 to +32767.

Definition at line 57 of file math.h.

    { x = pVector[0]*(1.0f/32767.0f); y = pVector[1]*(1.0f/32767.0f); z = pVector[2]*(1.0f/32767.0f); };

Member Function Documentation

Vector& Set ( float  fX,
float  fY,
float  fZ 
) throw () [inline]

Sets the values of the vector elements and returns the vector.

Definition at line 61 of file math.h.

    { x = fX; y = fY; z = fZ; return *this; };
Vector& Clear ( void  ) throw () [inline]

Zeros all the components of the Vector, then returns it.

Definition at line 65 of file math.h.

    { return Set( 0.0f, 0.0f, 0.0f ); };
Vector& Normalize ( void  ) throw ()

Normalizes the vector and returns it.

If the vector is zero length, it is set to (0,1,0)

Vector& NormalizeApprox ( void  ) throw ()

Normalizes the vector and returns it.

If the vector is zero length, it is set to (0,1,0) TO BE REMOVED. This was an experiment at improving speed in exchange for accurracy.

Vector Normalized ( void  ) const throw ()

Returns a normalized version of the vector, without changing this vector.

Vector& MakeOrthogonal ( const Vector vBase )

Makes this vector orthogonal to the given vBase vector and normalizes it.

Returns the vector.

The resulting vector will lie on the plane defined by the original value of this vector, and vBase.

Vector& SetLength ( float  fLength ) [inline]

Sets the vector to the given length, without changing its direction.

If the vector is of zero length, then it is arbitrarily made parallel to the Y axis.

Definition at line 94 of file math.h.

    { Normalize(); operator *=( fLength ); return *this; };
Vector& RotateOrthogonal ( const Vector vBase ) [inline]

Rotates this vector to be orthogonal to vBase without changing its length, and returns it.

The resulting vector will lie on the plane defined by the original value of this vector, and vBase.

Definition at line 103 of file math.h.

    { float fLen = Length(); MakeOrthogonal( vBase ); SetLength( fLen ); return *this; };
float LengthSquare ( void  ) const [inline]

Returns the square of the length of the vector.

Definition at line 108 of file math.h.

{ return x*x+y*y+z*z; };
float Length ( ) const throw ()

Returns the length of the vector.

float Length2D ( ) const throw ()

Returns the 2D length of the vector, ignoring the z element.

float DistanceFrom ( const Vector v ) const [inline]

Returns the distance between two points (this and another, represented as Vector objects).

Definition at line 118 of file math.h.

{ return operator -(v).Length(); };
float DistanceFromLine ( const Vector v0,
const Vector v1 
) const

Returns the shortest distance between this point and a line defined by two other points (all points represented by vector objects).

float DistanceFromSegment ( const Vector v0,
const Vector v1 
) const

Returns the distance between this point and a line segment defined by the two ending points.

float DistanceFromTriangleSquared ( const Vector v0,
const Vector v1,
const Vector v2,
float *  aBaryCoords = 0 
) const

Returns the squared distance from this point to the closest point on the triangle defined by v0, v1, v2.

Optionally, the barycentric coordinates of the point on the triangle are returned in aBaryCoords, which must be an array of 3 floats, or NULL. To compute the 3d point use: Vector vPoint = v0 * aBaryCoords[0] + v1 * aBaryCoords[1] + v2 * aBaryCoords[2]

const Vector& Relocate2D ( const Vector v0,
const Vector v1 
)

Converts this (2d) vector to represent itself as a linear combination of the supplied basis vectors.

Returns the result.

This function works in 2d, ignoring the Z component of all the vectors. The supplied basis vectors must not be parallel.

This method figures out how to express this vector (x,y) as a linear combination of the two vector arguments. X is set to the multiplier on the first vector, and Y to the multiplier on the second vector. So the original vector could be reconstructed from the final one like this: original = x*v0 + y*v1

void Relocate ( const Vector v0,
const Vector v1,
const Vector v2 
)

Converts this vector to represent itself as a linear combination of the supplied basis vectors.

This method figures out how to express this vector (x,y,z) as a linear combination of the three vector arguments. X is set to the multiplier on the first vector, Y to the multiplier on the second vector, and Z to the multiplier on the third. The supplied basis vectors must not be parallel. So the original vector could be reconstructed from the final one like this: original = x*v0 + y*v1 + z*v2

bool Relocate2DQuad ( const Vector v0,
const Vector v1,
const Vector v2,
const Vector v3 
)

This operation calculates the baricentric coordinates inside a quad where the quad corners are v0,v1,v2,v3.

2d points in this function are represented by 3d Vector objects, with the Z component ignored. This function works in 2d only. If v0, v1, v2 and v3 are the corners of a quad in a 2d space (such as UV space), and this object's (x,y) coordinates represent a point inside that quad, then this function will calculate the baricentric coordinates inside the quad (v0 and v3 are opposite corners). After this operation the following code can calculate the original value stored in the vector (this can help you understand what the function does):

 Vector vEdgeA = v1-v0;
 Vector vEdgeB = v3-v2;
 Vector vPointA = v0+vEdgeA*x;
 Vector vPointB = v2+vEdgeB*x;
 Vector vEdge = vPointB-vPointA;
 Vector vValueBeforeOperation = vPointA+vEdge*y;
bool RelocateQuad ( const Vector v0,
const Vector v1,
const Vector v2,
const Vector v3 
)

This operation calculates the baricentric coordinates inside a quad where the quad corners are v0,v1,v2,v3.

This is the 3d version of the Relocate2DQuad function. The result will only be correct if the four points lie on the same plane.

float AngleCos ( const Vector v1 ) const throw () [inline]

Returns the cosine of the angle between this vector and the one passed in.

Definition at line 195 of file math.h.

    { return Vector(*this).Normalize()|Vector(v1).Normalize(); };
Vector Minimum ( const Vector o ) [inline]

Returns the minimum of this and the argument vector.

Minimums are calculated individually for z, y, and z.

Definition at line 201 of file math.h.

    { return Vector( Min(x, o.x), Min(y, o.y), Min(z, o.z) ); };
Vector Maximum ( const Vector o ) [inline]

Returns the maximum of this and the argument vector.

Maximums are calculated individually for z, y, and z.

Definition at line 206 of file math.h.

    { return Vector( Max(x, o.x), Max(y, o.y), Max(z, o.z) ); };
Vector operator- ( void  ) const throw () [inline]

Returns the inverse of the vector.

Definition at line 210 of file math.h.

    { return Vector( -x, -y, -z ); };
Vector operator+ ( float  f ) const throw () [inline]

Returns the sum of a vector and a scalar.

(The scalar is added to each component individually).

Definition at line 215 of file math.h.

    { return Vector( x+f, y+f, z+f ); };
Vector operator+ ( const Vector v ) const throw () [inline]

Returns the sum of two vectors.

Definition at line 219 of file math.h.

    { return Vector( x+v.x, y+v.y, z+v.z ); };
Vector operator- ( const Vector v ) const throw () [inline]

Returns the difference between two vectors/points.

Definition at line 223 of file math.h.

    { return Vector( x-v.x, y-v.y, z-v.z ); };
Vector operator* ( const Vector v ) const throw () [inline]

Returns the product of two vectors by components.

Definition at line 227 of file math.h.

    { return Vector( x*v.x, y*v.y, z*v.z ); };
Vector operator* ( float  f ) const throw () [inline]

Returns the product of the vector and a scalar.

Definition at line 231 of file math.h.

    { return Vector( x*f, y*f, z*f ); };
Vector operator/ ( const Vector v ) const throw () [inline]

Returns the quotient of two vectors by components.

Definition at line 235 of file math.h.

    { return Vector( x/v.x, y/v.y, z/v.z ); };
Vector operator/ ( float  f ) const throw () [inline]

Returns the quotient of a vector and a scalar.

Definition at line 239 of file math.h.

    { return operator *( 1.0f/f ); };
Vector operator* ( int  i ) const throw () [inline]

Returns the product of a vector and an integer.

Definition at line 243 of file math.h.

    { return operator *( float(i) ); };
Vector operator/ ( int  i ) const throw () [inline]

Returns the quotient of a vector and an integer.

Definition at line 247 of file math.h.

    { return operator /( float(i) ); };
Vector operator/ ( unsigned int  i ) const throw () [inline]

Returns the quotient of a vector and an unsigned integer.

Definition at line 251 of file math.h.

    { return operator /( float(i) ); };
float operator| ( const Vector v ) const throw () [inline]

Returns the dot product of two vectors.

Definition at line 255 of file math.h.

    { return x*v.x+y*v.y+z*v.z; };
Vector operator& ( const Vector v ) const [inline]

Returns the cross product of two vectors.

Definition at line 259 of file math.h.

    {
        Vector vR;
        vR.m_fX = m_fY*v.m_fZ-m_fZ*v.m_fY;
        vR.m_fY = m_fZ*v.m_fX-m_fX*v.m_fZ;
        vR.m_fZ = m_fX*v.m_fY-m_fY*v.m_fX;
        return vR;
    };
bool operator== ( const Vector v ) const throw () [inline]

Returns true if the two vectors are identical, false otherwise.

Definition at line 271 of file math.h.

    { return x == v.x && y == v.y && z == v.z; };
bool operator!= ( const Vector v ) const throw () [inline]

Returns true if the two vectors are different, false otherwise.

Definition at line 275 of file math.h.

    { return !operator ==( v ); };
operator bool ( void  ) const throw () [inline]

Returns true if the vector is not a zero vector, false otherwise.

Definition at line 279 of file math.h.

{ return x || y || z; };
bool operator! ( void  ) const throw () [inline]

Returns true if the vector is a zero vector, false otherwise.

Definition at line 282 of file math.h.

{ return !operator bool(); };
Vector& operator= ( const Vector v ) [inline]

Sets this vector to equal another one and returns it.

Definition at line 285 of file math.h.

    { x = v.x; y = v.y; z = v.z; return *this; };
Vector& operator<< ( const Vector v ) [inline]

Sets this vector to equal another one and returns it.

Definition at line 289 of file math.h.

    { x = v.x; y = v.y; z = v.z; return *this; };
Vector& operator-= ( const Vector v ) throw () [inline]

Subtracts the component values of another vector from this one's, and returns the result.

Definition at line 294 of file math.h.

    { x-=v.x; y-=v.y; z-=v.z; return *this; };
Vector& operator+= ( const Vector v ) throw () [inline]

Adds another vector to this one and returns the result.

Definition at line 298 of file math.h.

        { x+=v.x; y+=v.y; z+=v.z; return *this; };
Vector& operator*= ( float  f ) [inline]

Multiplies the components of the vector with a scalar value and returns the result.

Definition at line 303 of file math.h.

{ x *= f; y *= f; z *= f; return *this; };
Vector& operator*= ( const Vector v ) [inline]

Multiplies the vector with another vector by components and returns the result.

Definition at line 307 of file math.h.

        { x *= v.x; y *= v.y; z *= v.z; return *this; };
Vector& operator/= ( float  f ) [inline]

Divides the vector by a scalar and returns the result.

Definition at line 311 of file math.h.

{ return operator *=( 1.0f/f ); };
float& operator[] ( int  i ) throw () [inline]

Returns a individual components of the vector.

Parameters:
[in] i Allowed range: 0 to 2.

Definition at line 314 of file math.h.

                  { return m_aCoors[i]; };
float& operator[] ( unsigned int  i ) throw () [inline]

Returns a component of the vector.

Parameters:
[in] i Allowed range: 0 to 2.

Definition at line 319 of file math.h.

                  { return m_aCoors[i]; };
operator const float * ( void  ) const [inline]

Returns a pointer to the data of the vector. Do not delete this pointer.

Definition at line 324 of file math.h.

{ return &x; };

Member Data Documentation

float m_fX

Definition at line 327 of file math.h.

float m_fY

Definition at line 327 of file math.h.

float m_fZ

Definition at line 327 of file math.h.

float x
Examples:
PLYImport/Importer.cpp.

Definition at line 328 of file math.h.

float y
Examples:
PLYImport/Importer.cpp.

Definition at line 328 of file math.h.

float z
Examples:
PLYImport/Importer.cpp.

Definition at line 328 of file math.h.

float m_aCoors[3]

Definition at line 329 of file math.h.


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

Vector Vector Vector Vector Vector Vector Vector Vector Vector Vector
Vector Vector Vector Vector Vector Vector Vector Vector Vector Vector