Vegastrike 0.5.1 rc1  1.0
Original sources for Vegastrike Evolved
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
csVector3 Class Reference

#include <opvector3.h>

Public Member Functions

 csVector3 ()
 
 csVector3 (float m)
 
 csVector3 (float ix, float iy, float iz=0)
 Make a new vector and initialize with the given values. More...
 
 csVector3 (const csVector3 &v)
 Copy Constructor. More...
 
 csVector3 (const Vector v)
 
csVector3 operator+ (const csVector3 &v2) const
 
csVector3 operator- (const csVector3 &v2) const
 Subtract two vectors. More...
 
void Cross (const csVector3 &px, const csVector3 &py)
 Take cross product of two vectors and put result in this vector. More...
 
void Cross (const Opcode::Point &px, const Opcode::Point &py)
 
float operator[] (int n) const
 Returns n-th component of the vector. More...
 
floatoperator[] (int n)
 Returns n-th component of the vector. More...
 
csVector3operator+= (const csVector3 &v)
 Add another vector to this vector. More...
 
csVector3operator-= (const csVector3 &v)
 Subtract another vector from this vector. More...
 
csVector3operator*= (float f)
 Multiply this vector by a scalar. More...
 
csVector3operator/= (float f)
 Divide this vector by a scalar. More...
 
csVector3 operator+ () const
 Unary + operator. More...
 
csVector3 operator- () const
 Unary - operator. More...
 
void Set (float sx, float sy, float sz)
 Set the value of this vector. More...
 
void Set (const csVector3 &v)
 Set the value of this vector. More...
 
float Norm () const
 Returns the norm of this vector. More...
 
float SquaredNorm () const
 Return the squared norm (magnitude) of this vector. More...
 
csVector3 Unit () const
 
void Normalize ()
 Scale this vector to length = 1.0;. More...
 
bool IsZero () const
 Query if the vector is zero. More...
 

Static Public Member Functions

static float Norm (const csVector3 &v)
 Returns the norm (magnitude) of a vector. More...
 
static csVector3 Unit (const csVector3 &v)
 Normalizes a vector to a unit vector. More...
 

Public Attributes

float x
 The X component of the vector. More...
 
float y
 The Y component of the vector. More...
 
float z
 The Z component of the vector. More...
 

Friends

float operator* (const csVector3 &v1, const csVector3 &v2)
 Subtract two vectors of differing type, cast to double. More...
 
csVector3 operator% (const csVector3 &v1, const csVector3 &v2)
 Take the cross product of two vectors. More...
 
csVector3 operator* (const csVector3 &v, float f)
 Multiply a vector and a scalar. More...
 
csVector3 operator* (float f, const csVector3 &v)
 Multiply a vector and a scalar. More...
 
csVector3 operator* (const csVector3 &v, int f)
 Multiply a vector and a scalar int. More...
 
csVector3 operator* (int f, const csVector3 &v)
 Multiply a vector and a scalar int. More...
 
csVector3 operator/ (const csVector3 &v, float f)
 Divide a vector by a scalar. More...
 
csVector3 operator/ (const csVector3 &v, int f)
 Divide a vector by a scalar int. More...
 
bool operator== (const csVector3 &v1, const csVector3 &v2)
 Check if two vectors are equal. More...
 
bool operator!= (const csVector3 &v1, const csVector3 &v2)
 Check if two vectors are not equal. More...
 
csVector3 operator>> (const csVector3 &v1, const csVector3 &v2)
 Project one vector onto another. More...
 
csVector3 operator<< (const csVector3 &v1, const csVector3 &v2)
 Project one vector onto another. More...
 
bool operator< (const csVector3 &v, float f)
 Test if each component of a vector is less than a small epsilon value. More...
 
bool operator> (float f, const csVector3 &v)
 Test if each component of a vector is less than a small epsilon value. More...
 

Detailed Description

A 3D vector.

Definition at line 28 of file opvector3.h.

Constructor & Destructor Documentation

csVector3::csVector3 ( )
inline

Make a new vector. The vector is not initialized. This makes the code slightly faster as csVector3 objects are used a lot.

Definition at line 43 of file opvector3.h.

Referenced by operator+(), and operator-().

43 {}
csVector3::csVector3 ( float  m)
inline

Make a new initialized vector. Creates a new vector and initializes it to m*<1,1,1>. To create a vector initialized to the zero vector, use csVector3(0)

Definition at line 50 of file opvector3.h.

50 : x(m), y(m), z(m) {}
csVector3::csVector3 ( float  ix,
float  iy,
float  iz = 0 
)
inline

Make a new vector and initialize with the given values.

Definition at line 53 of file opvector3.h.

53 : x(ix), y(iy), z(iz) {}
csVector3::csVector3 ( const csVector3 v)
inline

Copy Constructor.

Definition at line 56 of file opvector3.h.

56 : x(v.x), y(v.y), z(v.z) {}
csVector3::csVector3 ( const Vector  v)
inline

Definition at line 57 of file opvector3.h.

References i, j, and k.

57 : x(v.i), y(v.j), z(v.k) {}

Member Function Documentation

void csVector3::Cross ( const csVector3 px,
const csVector3 py 
)
inline

Take cross product of two vectors and put result in this vector.

Definition at line 84 of file opvector3.h.

References x, y, and z.

Referenced by ContinuousTerrain::Collide(), and Unit::InsideCollideTree().

85  {
86  x = px.y*py.z - px.z*py.y;
87  y = px.z*py.x - px.x*py.z;
88  z = px.x*py.y - px.y*py.x;
89  }
void csVector3::Cross ( const Opcode::Point px,
const Opcode::Point py 
)
inline

Definition at line 91 of file opvector3.h.

References x, Opcode::Point::x, y, Opcode::Point::y, z, and Opcode::Point::z.

92  {
93  x = px.y*py.z - px.z*py.y;
94  y = px.z*py.x - px.x*py.z;
95  z = px.x*py.y - px.y*py.x;
96  }
bool csVector3::IsZero ( ) const
inline

Query if the vector is zero.

Definition at line 218 of file opvector3.h.

References x, y, and z.

219  { return (x == 0) && (y == 0) && (z == 0); }
float csVector3::Norm ( ) const

Returns the norm of this vector.

Definition at line 29 of file opvector3.cpp.

References qsqrt, x, y, and z.

Referenced by Norm(), and Unit().

30 {
31  return qsqrt (x*x + y*y + z*z);
32 }
static float csVector3::Norm ( const csVector3 v)
inlinestatic

Returns the norm (magnitude) of a vector.

Definition at line 209 of file opvector3.h.

References Norm().

209 { return v.Norm(); }
void csVector3::Normalize ( )

Scale this vector to length = 1.0;.

Definition at line 34 of file opvector3.cpp.

References qisqrt, SMALL_EPSILON, x, y, and z.

Referenced by ContinuousTerrain::Collide(), and Unit::InsideCollideTree().

35 {
36  float sqlen = x*x + y*y + z*z;
37  if (sqlen < SMALL_EPSILON) return;
38  float invlen = qisqrt (sqlen);
39  *this *= invlen;
40 }
csVector3& csVector3::operator*= ( float  f)
inline

Multiply this vector by a scalar.

Definition at line 175 of file opvector3.h.

References x, y, and z.

176  { x *= f; y *= f; z *= f; return *this; }
csVector3 csVector3::operator+ ( const csVector3 v2) const
inline

Conversion from double precision vector to single. Add two vectors.

Definition at line 60 of file opvector3.h.

References csVector3(), x, y, and z.

61  { return csVector3(x+v2.x, y+v2.y, z+v2.z); }
csVector3 csVector3::operator+ ( ) const
inline

Unary + operator.

Definition at line 183 of file opvector3.h.

183 { return *this; }
csVector3& csVector3::operator+= ( const csVector3 v)
inline

Add another vector to this vector.

Definition at line 155 of file opvector3.h.

References x, y, and z.

156  {
157  x += v.x;
158  y += v.y;
159  z += v.z;
160 
161  return *this;
162  }
csVector3 csVector3::operator- ( const csVector3 v2) const
inline

Subtract two vectors.

Definition at line 64 of file opvector3.h.

References csVector3(), x, y, and z.

65  { return csVector3(x-v2.x, y-v2.y, z-v2.z); }
csVector3 csVector3::operator- ( ) const
inline

Unary - operator.

Definition at line 186 of file opvector3.h.

References csVector3(), x, y, and z.

186 { return csVector3(-x,-y,-z); }
csVector3& csVector3::operator-= ( const csVector3 v)
inline

Subtract another vector from this vector.

Definition at line 165 of file opvector3.h.

References x, y, and z.

166  {
167  x -= v.x;
168  y -= v.y;
169  z -= v.z;
170 
171  return *this;
172  }
csVector3& csVector3::operator/= ( float  f)
inline

Divide this vector by a scalar.

Definition at line 179 of file opvector3.h.

References x, y, and z.

180  { f = 1.0f / f; x *= f; y *= f; z *= f; return *this; }
float csVector3::operator[] ( int  n) const
inline

Returns n-th component of the vector.

Definition at line 149 of file opvector3.h.

References x, y, and z.

149 { return !n?x:n&1?y:z; }
float& csVector3::operator[] ( int  n)
inline

Returns n-th component of the vector.

Definition at line 152 of file opvector3.h.

References x, y, and z.

152 { return !n?x:n&1?y:z; }
void csVector3::Set ( float  sx,
float  sy,
float  sz 
)
inline

Set the value of this vector.

Definition at line 189 of file opvector3.h.

References x, y, and z.

Referenced by csOPCODECollider::Collide().

189 { x = sx; y = sy; z = sz; }
void csVector3::Set ( const csVector3 v)
inline

Set the value of this vector.

Definition at line 192 of file opvector3.h.

References x, y, and z.

192 { x = v.x; y = v.y; z = v.z; }
float csVector3::SquaredNorm ( ) const
inline

Return the squared norm (magnitude) of this vector.

Definition at line 198 of file opvector3.h.

References x, y, and z.

199  { return x * x + y * y + z * z; }
csVector3 csVector3::Unit ( ) const
inline

Returns the unit vector in the direction of this vector. Attempting to normalize a zero-vector will result in a divide by zero error. This is as it should be... fix the calling code.

Definition at line 206 of file opvector3.h.

References Norm().

Referenced by Unit().

206 { return (*this)/(this->Norm()); }
static csVector3 csVector3::Unit ( const csVector3 v)
inlinestatic

Normalizes a vector to a unit vector.

Definition at line 212 of file opvector3.h.

References Unit().

212 { return v.Unit(); }

Friends And Related Function Documentation

bool operator!= ( const csVector3 v1,
const csVector3 v2 
)
friend

Check if two vectors are not equal.

Definition at line 129 of file opvector3.h.

130  { return v1.x!=v2.x || v1.y!=v2.y || v1.z!=v2.z; }
csVector3 operator% ( const csVector3 v1,
const csVector3 v2 
)
friend

Take the cross product of two vectors.

Definition at line 76 of file opvector3.h.

77  {
78  return csVector3 (v1.y*v2.z-v1.z*v2.y,
79  v1.z*v2.x-v1.x*v2.z,
80  v1.x*v2.y-v1.y*v2.x);
81  }
float operator* ( const csVector3 v1,
const csVector3 v2 
)
friend

Subtract two vectors of differing type, cast to double.

Subtract two vectors of differing type, cast to double. Take the dot product of two vectors.

Definition at line 72 of file opvector3.h.

73  { return v1.x*v2.x + v1.y*v2.y + v1.z*v2.z; }
csVector3 operator* ( const csVector3 v,
float  f 
)
friend

Multiply a vector and a scalar.

Definition at line 99 of file opvector3.h.

100  { return csVector3(v.x*f, v.y*f, v.z*f); }
csVector3 operator* ( float  f,
const csVector3 v 
)
friend

Multiply a vector and a scalar.

Definition at line 103 of file opvector3.h.

104  { return csVector3(v.x*f, v.y*f, v.z*f); }
csVector3 operator* ( const csVector3 v,
int  f 
)
friend

Multiply a vector and a scalar int.

Definition at line 108 of file opvector3.h.

109  { return csVector3(v.x*f, v.y*f, v.z*f); }
csVector3 operator* ( int  f,
const csVector3 v 
)
friend

Multiply a vector and a scalar int.

Definition at line 112 of file opvector3.h.

113  { return csVector3(v.x*f, v.y*f, v.z*f); }
csVector3 operator/ ( const csVector3 v,
float  f 
)
friend

Divide a vector by a scalar.

Definition at line 116 of file opvector3.h.

117  { f = 1.0f/f; return csVector3(v.x*f, v.y*f, v.z*f); }
csVector3 operator/ ( const csVector3 v,
int  f 
)
friend

Divide a vector by a scalar int.

Definition at line 121 of file opvector3.h.

122  { float F = 1.0f/f; return csVector3(v.x*F, v.y*F, v.z*F); }
bool operator< ( const csVector3 v,
float  f 
)
friend

Test if each component of a vector is less than a small epsilon value.

Definition at line 141 of file opvector3.h.

142  { return ABS(v.x)<f && ABS(v.y)<f && ABS(v.z)<f; }
csVector3 operator<< ( const csVector3 v1,
const csVector3 v2 
)
friend

Project one vector onto another.

Definition at line 137 of file opvector3.h.

138  { return ((v1*v2)*v1)/(v1*v1); }
bool operator== ( const csVector3 v1,
const csVector3 v2 
)
friend

Check if two vectors are equal.

Definition at line 125 of file opvector3.h.

126  { return v1.x==v2.x && v1.y==v2.y && v1.z==v2.z; }
bool operator> ( float  f,
const csVector3 v 
)
friend

Test if each component of a vector is less than a small epsilon value.

Definition at line 145 of file opvector3.h.

146  { return ABS(v.x)<f && ABS(v.y)<f && ABS(v.z)<f; }
csVector3 operator>> ( const csVector3 v1,
const csVector3 v2 
)
friend

Project one vector onto another.

Definition at line 133 of file opvector3.h.

134  { return ((v1*v2)*v2)/(v2*v2); }

Member Data Documentation


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