Tiberian Technologies Scripts Reference Revision: 9000
Loading...
Searching...
No Matches
Vector3.h
1/* Renegade Scripts.dll
2 Copyright 2013 Tiberian Technologies
3
4 This file is part of the Renegade scripts.dll
5 The Renegade scripts.dll is free software; you can redistribute it and/or modify it under
6 the terms of the GNU General Public License as published by the Free
7 Software Foundation; either version 2, or (at your option) any later
8 version. See the file COPYING for more details.
9 In addition, an exemption is given to allow Run Time Dynamic Linking of this code with any closed source module that does not contain code covered by this licence.
10 Only the source code to the module(s) containing the licenced code has to be released.
11*/
12#ifndef TT_INCLUDE__VECTOR3_H
13#define TT_INCLUDE__VECTOR3_H
14#include "wwmath.h"
15class Vector3
16{
17public:
18 float X;
19 float Y;
20 float Z;
21
22 TT_INLINE bool Is_Valid(void) const;
23 TT_INLINE Vector3()
24 {
25 X = Y = Z = 0.0f;
26 }
27 TT_INLINE Vector3(const Vector3 &v)
28 {
29 X = v.X;
30 Y = v.Y;
31 Z = v.Z;
32 }
33 TT_INLINE Vector3(float x, float y, float z)
34 {
35 X = x;
36 Y = y;
37 Z = z;
38 }
39 TT_INLINE explicit Vector3(const float vector[3])
40 {
41 TT_ASSERT(vector != NULL);
42 X = vector[0];
43 Y = vector[1];
44 Z = vector[2];
45 }
46 TT_INLINE Vector3 &operator= (const Vector3 &v)
47 {
48 X = v.X;
49 Y = v.Y;
50 Z = v.Z;
51 return *this;
52 }
53 TT_INLINE void Set(float x, float y, float z)
54 {
55 X = x;
56 Y = y;
57 Z = z;
58 }
59 TT_INLINE void Set(const Vector3 &that)
60 {
61 X = that.X;
62 Y = that.Y;
63 Z = that.Z;
64 }
65 TT_INLINE float &operator[](int i)
66 {
67 return (&X)[i];
68 }
69 TT_INLINE const float &operator[](int i) const
70 {
71 return (&X)[i];
72 }
73 TT_INLINE void Normalize()
74 {
75 float len2 = WWMATH_FLOAT_TINY + Length2();
76 float oolen = WWMath::Inv_Sqrt(len2);
77 X *= oolen;
78 Y *= oolen;
79 Z *= oolen;
80 }
81 Vector3 Normalized() const
82 {
83 Vector3 v = *this;
84 v.Normalize();
85 return v;
86 }
87 TT_INLINE float Length() const
88 {
89 return WWMath::Sqrt(Length2());
90 }
91 TT_INLINE float Length2() const
92 {
93 return X * X + Y * Y + Z * Z;
94 }
95 TT_INLINE void Scale(const Vector3 &scale)
96 {
97 X *= scale.X;
98 Y *= scale.Y;
99 Z *= scale.Z;
100 }
101 TT_INLINE void Rotate_X(float angle)
102 {
103 Rotate_X(sinf(angle),cosf(angle));
104 }
105 TT_INLINE void Rotate_X(float s_angle,float c_angle)
106 {
107 float tmp_y = Y;
108 float tmp_z = Z;
109 Y = c_angle * tmp_y - s_angle * tmp_z;
110 Z = s_angle * tmp_y + c_angle * tmp_z;
111 }
112 TT_INLINE void Rotate_Y(float angle)
113 {
114 Rotate_Y(sinf(angle),cosf(angle));
115 }
116 TT_INLINE void Rotate_Y(float s_angle,float c_angle)
117 {
118 float tmp_x = X;
119 float tmp_z = Z;
120 X = c_angle * tmp_x + s_angle * tmp_z;
121 Z = -s_angle * tmp_x + c_angle * tmp_z;
122 }
123 TT_INLINE void Rotate_Z(float angle)
124 {
125 Rotate_Z(sinf(angle),cosf(angle));
126 }
127 TT_INLINE void Rotate_Z(float s_angle,float c_angle)
128 {
129 float tmp_x = X;
130 float tmp_y = Y;
131 X = c_angle * tmp_x - s_angle * tmp_y;
132 Y = s_angle * tmp_x + c_angle * tmp_y;
133 }
134 TT_INLINE Vector3 operator-() const
135 {
136 return(Vector3(-X,-Y,-Z));
137 }
138 TT_INLINE Vector3 operator+() const
139 {
140 return *this;
141 }
142 TT_INLINE Vector3 &operator+= (const Vector3 &v)
143 {
144 X += v.X;
145 Y += v.Y;
146 Z += v.Z;
147 return *this;
148 }
149 TT_INLINE Vector3 &operator-= (const Vector3 &v)
150 {
151 X -= v.X;
152 Y -= v.Y;
153 Z -= v.Z;
154 return *this;
155 }
156 TT_INLINE Vector3 &operator*= (float k)
157 {
158 X = X * k;
159 Y = Y * k;
160 Z = Z * k;
161 return *this;
162 }
163 TT_INLINE Vector3 &operator/= (float k)
164 {
165 X = X * 1.0f / k;
166 Y = Y * 1.0f / k;
167 Z = Z * 1.0f / k;
168 return *this;
169 }
170 TT_INLINE Vector3 mul(const Vector3 &b){
171 return Vector3(
172 X*b.X,
173 Y*b.Y,
174 Z*b.Z);
175 }
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)
185 {
186 return a * b;
187 }
188 TT_INLINE static Vector3 Cross_Product(const Vector3 &a,const Vector3 &b)
189 {
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));
191 }
192 TT_INLINE static void Cross_Product(const Vector3 &a,const Vector3 &b, Vector3* __restrict set_result)
193 {
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);
198 }
199 TT_INLINE static float Cross_Product_X(const Vector3 &a,const Vector3 &b)
200 {
201 return a.Y * b.Z - a.Z * b.Y;
202 }
203 TT_INLINE static float Cross_Product_Y(const Vector3 &a,const Vector3 &b)
204 {
205 return a.Z * b.X - a.X * b.Z;
206 }
207 TT_INLINE static float Cross_Product_Z(const Vector3 &a,const Vector3 &b)
208 {
209 return a.X * b.Y - a.Y * b.X;
210 }
211 TT_INLINE static void Add(const Vector3 &a,const Vector3 &b,Vector3 *set_result)
212 {
213 set_result->X = a.X + b.X;
214 set_result->Y = a.Y + b.Y;
215 set_result->Z = a.Z + b.Z;
216 }
217 TT_INLINE static void Subtract(const Vector3 &a,const Vector3 &b,Vector3 *set_result)
218 {
219 set_result->X = a.X - b.X;
220 set_result->Y = a.Y - b.Y;
221 set_result->Z = a.Z - b.Z;
222 }
223 TT_INLINE static float Find_X_At_Y(float y, const Vector3 &p1, const Vector3 &p2)
224 {
225 return(p1.X + ((y - p1.Y) * ((p2.X - p1.X) / (p2.Y - p1.Y))));
226 }
227 TT_INLINE static float Find_X_At_Z(float z, const Vector3 &p1, const Vector3 &p2)
228 {
229 return(p1.X + ((z - p1.Z) * ((p2.X - p1.X) / (p2.Z - p1.Z))));
230 }
231 TT_INLINE static float Find_Y_At_X(float x, const Vector3 &p1, const Vector3 &p2)
232 {
233 return(p1.Y + ((x - p1.X) * ((p2.Y - p1.Y) / (p2.X - p1.X))));
234 }
235 TT_INLINE static float Find_Y_At_Z(float z, const Vector3 &p1, const Vector3 &p2)
236 {
237 return(p1.Y + ((z - p1.Z) * ((p2.Y - p1.Y) / (p2.Z - p1.Z))));
238 }
239 TT_INLINE static float Find_Z_At_X(float x, const Vector3 &p1, const Vector3 &p2)
240 {
241 return(p1.Z + ((x - p1.X) * ((p2.Z - p1.Z) / (p2.X - p1.X))));
242 }
243 TT_INLINE static float Find_Z_At_Y(float y, const Vector3 &p1, const Vector3 &p2)
244 {
245 return(p1.Z + ((y - p1.Y) * ((p2.Z - p1.Z) / (p2.Y - p1.Y))));
246 }
247 TT_INLINE void Update_Min(const Vector3 &a)
248 {
249 if (a.X < X)
250 {
251 X = a.X;
252 }
253 if (a.Y < Y)
254 {
255 Y = a.Y;
256 }
257 if (a.Z < Z)
258 {
259 Z = a.Z;
260 }
261 }
262 TT_INLINE void Update_Max(const Vector3 &a)
263 {
264 if (a.X > X)
265 {
266 X = a.X;
267 }
268 if (a.Y > Y)
269 {
270 Y = a.Y;
271 }
272 if (a.Z > Z)
273 {
274 Z = a.Z;
275 }
276 }
277 TT_INLINE void Cap_Absolute_To(const Vector3 &a)
278 {
279 if (X > 0)
280 {
281 if (a.X < X)
282 {
283 X = a.X;
284 }
285 }
286 else
287 {
288 if (-a.X > X)
289 {
290 X = -a.X;
291 }
292 }
293 if (Y > 0)
294 {
295 if (a.Y < Y)
296 {
297 Y = a.Y;
298 }
299 }
300 else
301 {
302 if (-a.Y > Y)
303 {
304 Y = -a.Y;
305 }
306 }
307 if (Z > 0)
308 {
309 if (a.Z < Z)
310 {
311 Z = a.Z;
312 }
313 }
314 else
315 {
316 if (-a.Z > Z)
317 {
318 Z = -a.Z;
319 }
320 }
321 }
322
323 TT_INLINE Vector3 abs() const
324 {
325 return Vector3(WWMath::Fabs(X), WWMath::Fabs(Y), WWMath::Fabs(Z));
326 }
327
328 TT_INLINE static float Distance(const Vector3 &p1, const Vector3 &p2)
329 {
330 return (p1 - p2).Length();
331 }
332 TT_INLINE static float Distance_Squared(const Vector3 &p1, const Vector3 &p2)
333 {
334 return (p1 - p2).Length2();
335 }
336 TT_INLINE static void Lerp(const Vector3 &a, const Vector3 &b, float alpha,Vector3 *set_result)
337 {
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);
341 }
342 unsigned long Convert_To_ARGB( void ) const;
343 float Quick_Length(void) const
344 {
345 float max = WWMath::Fabs(X);
346 float mid = WWMath::Fabs(Y);
347 float min = WWMath::Fabs(Z);
348 float tmp;
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;
353 }
354
355 TT_INLINE static Vector3 Replicate(float n)
356 {
357 return Vector3(n, n, n);
358 }
359};
360TT_INLINE Vector3 operator* (const Vector3 &a,float k)
361{
362 return Vector3((a.X * k),(a.Y * k),(a.Z * k));
363}
364TT_INLINE Vector3 operator* (float k, const Vector3 &a)
365{
366 return Vector3((a.X * k),(a.Y * k),(a.Z * k));
367}
368TT_INLINE Vector3 operator/ (const Vector3 &a,float k)
369{
370 return Vector3((a.X * 1.0f/k),(a.Y * 1.0f/k),(a.Z * 1.0f/k));
371}
372TT_INLINE Vector3 operator+ (const Vector3 &a,const Vector3 &b)
373{
374 return Vector3(a.X + b.X,a.Y + b.Y,a.Z + b.Z);
375}
376TT_INLINE Vector3 operator- (const Vector3 &a,const Vector3 &b)
377{
378 return Vector3(a.X - b.X,a.Y - b.Y,a.Z - b.Z);
379}
380TT_INLINE float operator* (const Vector3 &a,const Vector3 &b)
381{
382 return a.X * b.X + a.Y * b.Y + a.Z * b.Z;
383}
384TT_INLINE bool operator== (const Vector3 &a,const Vector3 &b)
385{
386 return ((a.X == b.X) && (a.Y == b.Y) && (a.Z == b.Z));
387}
388TT_INLINE bool operator != (const Vector3 &a,const Vector3 &b)
389{
390 return ((a.X != b.X) || (a.Y != b.Y) || (a.Z != b.Z));
391}
392TT_INLINE Vector3 Normalize(const Vector3 &vec)
393{
394 float len2 = WWMATH_FLOAT_TINY + vec.Length2();
395 return vec * WWMath::Inv_Sqrt(len2);
396}
397TT_INLINE void Swap(Vector3 &a,Vector3 &b)
398{
399 Vector3 tmp(a);
400 a = b;
401 b = tmp;
402}
403TT_INLINE Vector3 Lerp(const Vector3 &a, const Vector3 &b, float alpha)
404{
405 return Vector3((a.X + (b.X - a.X) * alpha),(a.Y + (b.Y - a.Y) * alpha),(a.Z + (b.Z - a.Z) * alpha));
406}
407TT_INLINE void Lerp(const Vector3 &a, const Vector3 &b, float alpha,Vector3 *set_result)
408{
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);
412}
413TT_INLINE bool Is_Valid_Float(float x)
414{
415 unsigned long * plong = (unsigned long *)(&x);
416 unsigned long exponent = ((*plong) & 0x7F800000) >> (32-9);
417
418 // if exponent is 0xFF, this is a NAN
419 if (exponent == 0xFF) {
420 return false;
421 }
422 return true;
423}
424TT_INLINE bool Vector3::Is_Valid(void) const
425{
426 return (Is_Valid_Float(X) && Is_Valid_Float(Y) && Is_Valid_Float(Z));
427}
428
429TT_INLINE unsigned long Vector3::Convert_To_ARGB( void ) const
430{
431 return (unsigned(255)<<24) |
432 (unsigned(X*255.0f)<<16) |
433 (unsigned(Y*255.0f)<<8) |
434 (unsigned(Z*255.0f));
435}
436#endif