00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #ifndef _VECTOR3_H_
00011 #define _VECTOR3_H_
00012
00013 #include "cglib/definitions.h"
00014
00015 struct Point3;
00016
00018 struct Vector3
00019 {
00020 public:
00022 float x;
00024 float y;
00026 float z;
00027
00029 Vector3(): x(0), y(0), z(0)
00030 {
00031 }
00032
00034 Vector3(float c): x(c), y(c), z(c)
00035 {
00036 }
00037
00039
00044 Vector3(float _x, float _y, float _z)
00045 : x(_x), y(_y), z(_z)
00046 {
00047 }
00048
00050 explicit Vector3(const Point3 &p);
00051
00053 Vector3 operator+(const Vector3 &v) const
00054 {
00055 return Vector3(x + v.x, y + v.y, z + v.z);
00056 }
00057
00059 Vector3& operator+=(const Vector3 &v)
00060 {
00061 x += v.x; y += v.y; z += v.z;
00062 return *this;
00063 }
00064
00066 Vector3 operator-(const Vector3 &v) const
00067 {
00068 return Vector3(x - v.x, y - v.y, z - v.z);
00069 }
00070
00072 Vector3& operator-=(const Vector3 &v)
00073 {
00074 x -= v.x; y -= v.y; z -= v.z;
00075 return *this;
00076 }
00077
00079 bool operator==(const Vector3 &v) const
00080 {
00081 return x == v.x && y == v.y && z == v.z;
00082 }
00083
00085 Vector3 operator*(float f) const
00086 {
00087 return Vector3(f*x, f*y, f*z);
00088 }
00089
00091 Vector3 &operator*=(float f)
00092 {
00093 x *= f; y *= f; z *= f;
00094 return *this;
00095 }
00096
00098 Vector3 operator/(float f) const
00099 {
00100 float inv = 1.f / f;
00101 return Vector3(x * inv, y * inv, z * inv);
00102 }
00103
00105 Vector3 &operator/=(float f)
00106 {
00107 float inv = 1.f / f;
00108 x *= inv; y *= inv; z *= inv;
00109 return *this;
00110 }
00111
00113 Vector3 operator-() const
00114 {
00115 return Vector3(-x, -y, -z);
00116 }
00117
00119
00125 float operator[](int i) const
00126 {
00127 return (&x)[i];
00128 }
00129
00131 bool operator!=(const Vector3 &other) const
00132 {
00133 return !(*this == other);
00134 }
00135
00137
00151 float &operator[](int i)
00152 {
00153 return (&x)[i];
00154 }
00155
00157
00163 void fill_array_3(float *a)
00164 {
00165 a[0] = x;
00166 a[1] = y;
00167 a[2] = z;
00168 }
00169
00171
00178 void fill_array_4(float *a)
00179 {
00180 a[0] = x;
00181 a[1] = y;
00182 a[2] = z;
00183 a[3] = 0.0f;
00184 }
00185
00187 float length_squared() const
00188 {
00189 return x*x + y*y + z*z;
00190 }
00191
00193 float length() const
00194 {
00195 return sqrtf(x*x + y*y + z*z);
00196 }
00197
00199
00202 bool is_near(const Vector3 &other, float threshold=EPSILON) const
00203 {
00204 float absx = fabs(x-other.x);
00205 float absy = fabs(y-other.y);
00206 float absz = fabs(z-other.z);
00207 return (absx < EPSILON && absy < EPSILON && absz < EPSILON);
00208 }
00209 };
00210
00212 inline Vector3 operator*(float f, const Vector3 &v)
00213 {
00214 return v*f;
00215 }
00216
00218 inline float dot(const Vector3 &v1, const Vector3 &v2)
00219 {
00220 return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z;
00221 }
00222
00224 inline float absdot(const Vector3 &v1, const Vector3 &v2)
00225 {
00226 return fabsf(dot(v1, v2));
00227 }
00228
00230 inline Vector3 cross(const Vector3 &v1, const Vector3 &v2)
00231 {
00232 return Vector3((v1.y * v2.z) - (v1.z * v2.y),
00233 (v1.z * v2.x) - (v1.x * v2.z),
00234 (v1.x * v2.y) - (v1.y * v2.x));
00235 }
00236
00238 inline Vector3 normalize(const Vector3 &v)
00239 {
00240 return v / v.length();
00241 }
00242
00243 #endif