| Revision 1 (by moose, 2006/03/06 08:49:15) |
|
#ifndef __XGL_VECTOR_H__
#define __XGL_VECTOR_H__
#include <math.h>
#ifndef MAX
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#endif
#ifndef MIN
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#endif
class Vector
{
public:
double x, y, z;
// CtorVector3DDtor
Vector() : x(0), y(0), z(0)
{
}
Vector (float _c) : x(_c), y(_c), z(_c)
{
}
Vector(float _x, float _y, float _z) : x(_x), y(_y), z(_z)
{
}
Vector(const Vector &v)
{
x = v.x;
y = v.y;
z = v.z;
}
// Operators
inline Vector operator += (Vector v)
{
x += v.x;
y += v.y;
z += v.z;
return *this;
}
inline Vector operator + (Vector v)
{
return Vector(x + v.x, y + v.y, z + v.z);
}
inline Vector operator - ()
{
return Vector(-x, -y, -z);
}
inline Vector operator -= (Vector v)
{
x -= v.x;
y -= v.y;
z -= v.z;
return *this;
}
inline Vector operator - (Vector v)
{
return Vector(x - v.x, y - v.y, z - v.z);
}
inline Vector operator *= (float f)
{
x *= f;
y *= f;
z *= f;
return *this;
}
inline Vector operator /= (float f)
{
x /= f;
y /= f;
z /= f;
return *this;
}
inline Vector operator * (float f)
{
return Vector(x * f, y * f, z * f);
}
inline Vector operator / (float f)
{
return Vector(x / f, y / f, z / f);
}
// Dot and cross product
inline float Dot(Vector v)
{
return x*v.x + y*v.y + z*v.z;
}
inline Vector Cross(Vector v)
{
Vector t;
// a x b = (ay*bz - az*by, az*bx - ax*bz, ax*by - ay*bx)
t.x = y * v.z - z * v.y;
t.y = z * v.x - x * v.z;
t.z = x * v.y - y * v.x;
return t;
}
// Some utilities
inline Vector Normalize()
{
float r = sqrt(x*x + y*y + z*z);
x /= r;
y /= r;
z /= r;
return *this;
}
inline void Clamp(float min, float max)
{
x = MIN(MAX(min, x), max);
y = MIN(MAX(min, y), max);
z = MIN(MAX(min, z), max);
}
};
#endif /* __XGL_VECTOR_H__ */