12#ifndef TT_INCLUDE__VECTOR3_H
13#define TT_INCLUDE__VECTOR3_H
22 TT_INLINE
bool Is_Valid(
void)
const;
27 TT_INLINE Vector3(
const Vector3 &v)
33 TT_INLINE Vector3(
float x,
float y,
float z)
39 TT_INLINE
explicit Vector3(
const float vector[3])
41 TT_ASSERT(vector != NULL);
46 TT_INLINE Vector3 &operator= (
const Vector3 &v)
53 TT_INLINE
void Set(
float x,
float y,
float z)
59 TT_INLINE
void Set(
const Vector3 &that)
65 TT_INLINE
float &operator[](
int i)
69 TT_INLINE
const float &operator[](
int i)
const
73 TT_INLINE
void Normalize()
75 float len2 = WWMATH_FLOAT_TINY + Length2();
76 float oolen = WWMath::Inv_Sqrt(len2);
81 Vector3 Normalized()
const
87 TT_INLINE
float Length()
const
89 return WWMath::Sqrt(Length2());
91 TT_INLINE
float Length2()
const
93 return X * X + Y * Y + Z * Z;
95 TT_INLINE
void Scale(
const Vector3 &scale)
101 TT_INLINE
void Rotate_X(
float angle)
103 Rotate_X(sinf(angle),cosf(angle));
105 TT_INLINE
void Rotate_X(
float s_angle,
float c_angle)
109 Y = c_angle * tmp_y - s_angle * tmp_z;
110 Z = s_angle * tmp_y + c_angle * tmp_z;
112 TT_INLINE
void Rotate_Y(
float angle)
114 Rotate_Y(sinf(angle),cosf(angle));
116 TT_INLINE
void Rotate_Y(
float s_angle,
float c_angle)
120 X = c_angle * tmp_x + s_angle * tmp_z;
121 Z = -s_angle * tmp_x + c_angle * tmp_z;
123 TT_INLINE
void Rotate_Z(
float angle)
125 Rotate_Z(sinf(angle),cosf(angle));
127 TT_INLINE
void Rotate_Z(
float s_angle,
float c_angle)
131 X = c_angle * tmp_x - s_angle * tmp_y;
132 Y = s_angle * tmp_x + c_angle * tmp_y;
134 TT_INLINE Vector3 operator-()
const
136 return(Vector3(-X,-Y,-Z));
138 TT_INLINE Vector3 operator+()
const
142 TT_INLINE Vector3 &operator+= (
const Vector3 &v)
149 TT_INLINE Vector3 &operator-= (
const Vector3 &v)
156 TT_INLINE Vector3 &operator*= (
float k)
163 TT_INLINE Vector3 &operator/= (
float k)
170 TT_INLINE Vector3 mul(
const Vector3 &b){
176 friend Vector3 operator* (
const Vector3 &a,
float k);
177 friend Vector3 operator* (
float k,
const Vector3 &a);
178 friend Vector3 operator/ (
const Vector3 &a,
float k);
179 friend Vector3 operator+ (
const Vector3 &a,
const Vector3 &b);
180 friend Vector3 operator- (
const Vector3 &a,
const Vector3 &b);
181 friend bool operator== (
const Vector3 &a,
const Vector3 &b);
182 friend bool operator!= (
const Vector3 &a,
const Vector3 &b);
183 friend float operator* (
const Vector3 &a,
const Vector3 &b);
184 TT_INLINE
static float Dot_Product(
const Vector3 &a,
const Vector3 &b)
188 TT_INLINE
static Vector3 Cross_Product(
const Vector3 &a,
const Vector3 &b)
190 return Vector3((a.Y * b.Z - a.Z * b.Y),(a.Z * b.X - a.X * b.Z),(a.X * b.Y - a.Y * b.X));
192 TT_INLINE
static void Cross_Product(
const Vector3 &a,
const Vector3 &b, Vector3* __restrict set_result)
194 TT_ASSERT(!(set_result == &a || set_result == &b));
195 set_result->X = (a.Y * b.Z - a.Z * b.Y);
196 set_result->Y = (a.Z * b.X - a.X * b.Z);
197 set_result->Z = (a.X * b.Y - a.Y * b.X);
199 TT_INLINE
static float Cross_Product_X(
const Vector3 &a,
const Vector3 &b)
201 return a.Y * b.Z - a.Z * b.Y;
203 TT_INLINE
static float Cross_Product_Y(
const Vector3 &a,
const Vector3 &b)
205 return a.Z * b.X - a.X * b.Z;
207 TT_INLINE
static float Cross_Product_Z(
const Vector3 &a,
const Vector3 &b)
209 return a.X * b.Y - a.Y * b.X;
211 TT_INLINE
static void Add(
const Vector3 &a,
const Vector3 &b,Vector3 *set_result)
213 set_result->X = a.X + b.X;
214 set_result->Y = a.Y + b.Y;
215 set_result->Z = a.Z + b.Z;
217 TT_INLINE
static void Subtract(
const Vector3 &a,
const Vector3 &b,Vector3 *set_result)
219 set_result->X = a.X - b.X;
220 set_result->Y = a.Y - b.Y;
221 set_result->Z = a.Z - b.Z;
223 TT_INLINE
static float Find_X_At_Y(
float y,
const Vector3 &p1,
const Vector3 &p2)
225 return(p1.X + ((y - p1.Y) * ((p2.X - p1.X) / (p2.Y - p1.Y))));
227 TT_INLINE
static float Find_X_At_Z(
float z,
const Vector3 &p1,
const Vector3 &p2)
229 return(p1.X + ((z - p1.Z) * ((p2.X - p1.X) / (p2.Z - p1.Z))));
231 TT_INLINE
static float Find_Y_At_X(
float x,
const Vector3 &p1,
const Vector3 &p2)
233 return(p1.Y + ((x - p1.X) * ((p2.Y - p1.Y) / (p2.X - p1.X))));
235 TT_INLINE
static float Find_Y_At_Z(
float z,
const Vector3 &p1,
const Vector3 &p2)
237 return(p1.Y + ((z - p1.Z) * ((p2.Y - p1.Y) / (p2.Z - p1.Z))));
239 TT_INLINE
static float Find_Z_At_X(
float x,
const Vector3 &p1,
const Vector3 &p2)
241 return(p1.Z + ((x - p1.X) * ((p2.Z - p1.Z) / (p2.X - p1.X))));
243 TT_INLINE
static float Find_Z_At_Y(
float y,
const Vector3 &p1,
const Vector3 &p2)
245 return(p1.Z + ((y - p1.Y) * ((p2.Z - p1.Z) / (p2.Y - p1.Y))));
247 TT_INLINE
void Update_Min(
const Vector3 &a)
262 TT_INLINE
void Update_Max(
const Vector3 &a)
277 TT_INLINE
void Cap_Absolute_To(
const Vector3 &a)
323 TT_INLINE Vector3 abs()
const
325 return Vector3(WWMath::Fabs(X), WWMath::Fabs(Y), WWMath::Fabs(Z));
328 TT_INLINE
static float Distance(
const Vector3 &p1,
const Vector3 &p2)
330 return (p1 - p2).Length();
332 TT_INLINE
static float Distance_Squared(
const Vector3 &p1,
const Vector3 &p2)
334 return (p1 - p2).Length2();
336 TT_INLINE
static void Lerp(
const Vector3 &a,
const Vector3 &b,
float alpha,Vector3 *set_result)
338 set_result->X = (a.X + (b.X - a.X) * alpha);
339 set_result->Y = (a.Y + (b.Y - a.Y) * alpha);
340 set_result->Z = (a.Z + (b.Z - a.Z) * alpha);
342 unsigned long Convert_To_ARGB(
void )
const;
343 float Quick_Length(
void)
const
345 float max = WWMath::Fabs(X);
346 float mid = WWMath::Fabs(Y);
347 float min = WWMath::Fabs(Z);
349 if (max < mid) { tmp = max; max = mid; mid = tmp; }
350 if (max < min) { tmp = max; max = min; min = tmp; }
351 if (mid < min) { tmp = mid; mid = min; min = mid; }
352 return max + (11.0f / 32.0f)*mid + (1.0f / 4.0f)*min;
355 TT_INLINE
static Vector3 Replicate(
float n)
357 return Vector3(n, n, n);
360TT_INLINE Vector3 operator* (
const Vector3 &a,
float k)
362 return Vector3((a.X * k),(a.Y * k),(a.Z * k));
364TT_INLINE Vector3 operator* (
float k,
const Vector3 &a)
366 return Vector3((a.X * k),(a.Y * k),(a.Z * k));
368TT_INLINE Vector3 operator/ (
const Vector3 &a,
float k)
370 return Vector3((a.X * 1.0f/k),(a.Y * 1.0f/k),(a.Z * 1.0f/k));
372TT_INLINE Vector3 operator+ (
const Vector3 &a,
const Vector3 &b)
374 return Vector3(a.X + b.X,a.Y + b.Y,a.Z + b.Z);
376TT_INLINE Vector3 operator- (
const Vector3 &a,
const Vector3 &b)
378 return Vector3(a.X - b.X,a.Y - b.Y,a.Z - b.Z);
380TT_INLINE
float operator* (
const Vector3 &a,
const Vector3 &b)
382 return a.X * b.X + a.Y * b.Y + a.Z * b.Z;
384TT_INLINE
bool operator== (
const Vector3 &a,
const Vector3 &b)
386 return ((a.X == b.X) && (a.Y == b.Y) && (a.Z == b.Z));
388TT_INLINE
bool operator != (
const Vector3 &a,
const Vector3 &b)
390 return ((a.X != b.X) || (a.Y != b.Y) || (a.Z != b.Z));
392TT_INLINE Vector3 Normalize(
const Vector3 &vec)
394 float len2 = WWMATH_FLOAT_TINY + vec.Length2();
395 return vec * WWMath::Inv_Sqrt(len2);
397TT_INLINE
void Swap(Vector3 &a,Vector3 &b)
403TT_INLINE Vector3 Lerp(
const Vector3 &a,
const Vector3 &b,
float alpha)
405 return Vector3((a.X + (b.X - a.X) * alpha),(a.Y + (b.Y - a.Y) * alpha),(a.Z + (b.Z - a.Z) * alpha));
407TT_INLINE
void Lerp(
const Vector3 &a,
const Vector3 &b,
float alpha,Vector3 *set_result)
409 set_result->X = (a.X + (b.X - a.X) * alpha);
410 set_result->Y = (a.Y + (b.Y - a.Y) * alpha);
411 set_result->Z = (a.Z + (b.Z - a.Z) * alpha);
413TT_INLINE
bool Is_Valid_Float(
float x)
415 unsigned long * plong = (
unsigned long *)(&x);
416 unsigned long exponent = ((*plong) & 0x7F800000) >> (32-9);
419 if (exponent == 0xFF) {
424TT_INLINE
bool Vector3::Is_Valid(
void)
const
426 return (Is_Valid_Float(X) && Is_Valid_Float(Y) && Is_Valid_Float(Z));
429TT_INLINE
unsigned long Vector3::Convert_To_ARGB(
void )
const
431 return (
unsigned(255)<<24) |
432 (unsigned(X*255.0f)<<16) |
433 (
unsigned(Y*255.0f)<<8) |
434 (
unsigned(Z*255.0f));