00001 #ifndef __VECTOR3_H__ 00002 #define __VECTOR3_H__ 00003 00004 class Vector3 00005 { 00006 public: 00008 Vector3() : x(0), y(0), z(0) {}; 00009 00011 Vector3(float _x, float _y, float _z) : x(_x), y(_y), z(_z) { }; 00012 Vector3(const Vector3& other) : x(other.x), y(other.y), z(other.z) { }; 00013 00015 const Vector3& operator=(const Vector3& other); 00016 00018 bool operator!=(const Vector3& other) const { return ((x != other.x) || (y != other.y) || (z != other.z)); } 00019 bool operator==(const Vector3& other) const { return ((x == other.x) && (y == other.y) && (z == other.z)); } 00020 bool operator<(const Vector3& other) const { return x < other.x; } 00021 00023 Vector3 operator-() const { return Vector3(-x, -y, -z); } 00024 00026 Vector3 operator+(const Vector3& other) const { return Vector3(x + other.x, y + other.y, z + other.z); } 00027 const Vector3& operator+=(const Vector3& other) { x += other.x; y += other.y; z += other.z; return (*this); } 00028 00030 Vector3 operator-(const Vector3& other) const { return Vector3(x - other.x, y - other.y, z - other.z); } 00031 const Vector3& operator-=(const Vector3& other) { x -= other.x; y -= other.y; z -= other.z; return (*this); } 00032 00034 Vector3 operator*(const Vector3& other) const { return Vector3(x * other.x, y * other.y, z * other.z); } 00035 const Vector3& operator*=(const Vector3& other) { x *= other.x; y *= other.y; z *= other.z; return (*this); } 00036 00038 Vector3 operator*(float scalar) const { return Vector3(x * scalar, y * scalar, z * scalar); } 00039 const Vector3& operator*=(float scalar) { x *= scalar; y *= scalar; z *= scalar; return (*this); } 00040 00042 Vector3 operator/(const Vector3& other) const; 00043 const Vector3& operator/=(const Vector3& other); 00044 00046 Vector3 operator/(float scalar) const; 00047 const Vector3& operator/=(float scalar); 00048 00049 00051 void Set(float _x, float _y, float _z) { x = _x; y = _y; z = _z; } 00052 float Length() const; 00053 float SquaredLength() const { return (x * x + y * y + z * z); } 00054 00056 Vector3& RotateX(float d); 00058 Vector3& RotateY(float d); 00060 Vector3& RotateZ(float d); 00062 Vector3& RotateAbout(const Vector3& v, float d); 00063 00064 float DotProduct(const Vector3& other) const { return (x * other.x + y * other.y + z * other.z); } 00065 void Normalize(); 00066 00067 float Unitize(float tolerance = 1e-06f ); 00068 00070 Vector3 CrossProduct(const Vector3& other) const { return Vector3(y * other.z - z * other.y, z * other.x - x * other.z, x * other.y - y * other.x); } 00071 Vector3 UnitCrossProduct(const Vector3& other) const; 00072 00074 static void GenerateOrthonormalBasis(Vector3& u, Vector3& v, Vector3& w); 00075 00077 int GetMajorAxis(void) const; 00078 00080 Vector3 CalcPerpendicularVector(void) const; 00081 00083 void Interpolate(const Vector3& v0, const Vector3& v1, float t); 00084 00086 bool Equals(const Vector3 &v, float epsilon = 1e-3f) const; 00087 00088 public: 00089 union 00090 { 00091 struct 00092 { 00093 float x; 00094 float y; 00095 float z; 00096 }; 00097 00098 struct 00099 { 00100 float comp[3]; 00101 }; 00102 }; 00103 00104 static float Distance(const Vector3& v0, const Vector3& v1); 00105 00106 static float SquaredDistance(const Vector3& v0, const Vector3& v1) 00107 { 00108 return ((v0.x-v1.x) * (v0.x-v1.x) + (v0.y-v1.y) * (v0.y-v1.y) + (v0.z-v1.z) * (v0.z-v1.z)); 00109 } 00110 00111 static Vector3 RandomVector(void); 00112 00113 static const Vector3 ZERO; 00114 static const Vector3 ONE; 00115 static const Vector3 X_AXIS; 00116 static const Vector3 Y_AXIS; 00117 static const Vector3 Z_AXIS; 00118 }; 00119 00120 #endif //__VECTOR3_H__