Tiberian Technologies Scripts Reference Revision: 9000
Loading...
Searching...
No Matches
vector4.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__VECTOR4_H
13#define TT_INCLUDE__VECTOR4_H
14#include "wwmath.h"
15class Vector4
16{
17public:
18 float X;
19 float Y;
20 float Z;
21 float W;
22
23 // default constructors
24 TT_INLINE Vector4() {};
25 TT_INLINE Vector4(const Vector4& v) { X = v.X; Y = v.Y; Z = v.Z; W = v.W; };
26 TT_INLINE Vector4(float x, float y, float z, float w) { X = x; Y = y; Z = z; W = w; };
27 TT_INLINE explicit Vector4(const float v[4]) { TT_ASSERT(v != NULL); X = v[0]; Y = v[1]; Z = v[2]; W = v[3]; };
28
29 // conversion constructors
30 //TT_INLINE explicit Vector4(const Vector2& v) { X = v.X; Y = v.Y; Z = 0.0f; W = 0.0f; };
31 //TT_INLINE explicit Vector4(const Vector3& v) { X = v.X; Y = v.Y; Z = v.Z; W = 0.0f; };
32
33 // assignment operators
34 TT_INLINE Vector4& operator=(const Vector4& v) { X = v.X; Y = v.Y; Z = v.Z; W = v.W; return *this; };
35 TT_INLINE void Set(float x, float y, float z, float w) { X = x; Y = y; Z = z; W = w; };
36
37 // array access operators
38 TT_INLINE float& operator[](int i) { return (&X)[i]; };
39 TT_INLINE const float& operator[](int i) const { return (&X)[i]; };
40 TT_INLINE void Normalize(void)
41 {
42 float len2 = WWMATH_FLOAT_TINY + Length2();
43 float oolen = WWMath::Inv_Sqrt(len2);
44 X *= oolen;
45 Y *= oolen;
46 Z *= oolen;
47 W *= oolen;
48 }
49 TT_INLINE float Length(void) const
50 {
51 return WWMath::Sqrt(Length2());
52 }
53 TT_INLINE float Length2(void) const
54 {
55 return X*X + Y*Y + Z*Z + W*W;
56 }
57
58 // unary operators
59 TT_INLINE Vector4 operator-() const { return(Vector4(-X,-Y,-Z,-W)); } ;
60 TT_INLINE Vector4 operator+() const { return *this; };
61
62 TT_INLINE Vector4& operator+=(const Vector4& v) { X += v.X; Y += v.Y; Z += v.Z; W += v.W; return *this; };
63 TT_INLINE Vector4& operator-=(const Vector4& v) { X -= v.X; Y -= v.Y; Z -= v.Z; W -= v.W; return *this; };
64 TT_INLINE Vector4& operator*=(float f) { X *= f; Y *= f; Z *= f; W *= f; return *this; };
65 TT_INLINE Vector4& operator/=(float f) { f = 1.0f / f; X /= f; Y /= f; Z /= f; W /= f; return *this; };
66
67 // vector addition/subtraction
68 friend Vector4 operator+(const Vector4& a, const Vector4& b);
69 friend Vector4 operator-(const Vector4& a, const Vector4& b);
70
71 // scalar multiplication/division
72 friend Vector4 operator*(const Vector4 &a, float b);
73 friend Vector4 operator*(float a, const Vector4 &b);
74 friend Vector4 operator/(const Vector4 &a, float b);
75
76 // equality operators
77 friend bool operator== (const Vector4 &a, const Vector4 &b);
78 friend bool operator!= (const Vector4 &a, const Vector4 &b);
79
80 friend float operator * (const Vector4 &a,const Vector4 &b);
81 TT_INLINE static float Dot_Product(const Vector4 &a,const Vector4 &b)
82 {
83 return a * b;
84 }
85 TT_INLINE static Vector4 Lerp(const Vector4 & a, const Vector4 & b, float alpha)
86 {
87 return Vector4((a.X + (b.X - a.X)*alpha),(a.Y + (b.Y - a.Y)*alpha),(a.Z + (b.Z - a.Z)*alpha),(a.W + (b.W - a.W)*alpha));
88 }
89 TT_INLINE static void Lerp(const Vector4 & a, const Vector4 & b, float alpha,Vector4 * set_result)
90 {
91 set_result->X = (a.X + (b.X - a.X)*alpha);
92 set_result->Y = (a.Y + (b.Y - a.Y)*alpha);
93 set_result->Z = (a.Z + (b.Z - a.Z)*alpha);
94 set_result->W = (a.W + (b.W - a.W)*alpha);
95 }
96};
97
98TT_INLINE Vector4 operator+(const Vector4& a, const Vector4& b)
99{
100 return Vector4(a.X + b.X, a.Y + b.Y, a.Z + b.Z, a.W + b.W);
101};
102
103TT_INLINE Vector4 operator-(const Vector4& a, const Vector4& b)
104{
105 return Vector4(a.X - b.X, a.Y - b.Y, a.Z - b.Z, a.W - b.W);
106};
107
108TT_INLINE Vector4 operator*(const Vector4 &a, float b)
109{
110 return Vector4(a.X * b, a.Y * b, a.Z * b, a.W * b);
111};
112
113TT_INLINE Vector4 operator*(float a, const Vector4 &b)
114{
115 return b * a;
116};
117
118TT_INLINE Vector4 operator/(const Vector4 &a, float k)
119{
120 return Vector4(a.X * 1.0f / k, a.Y * 1.0f / k, a.Z * 1.0f / k, a.W * 1.0f / k);
121};
122
123TT_INLINE bool operator==(const Vector4 &a, const Vector4 &b)
124{
125 return (a.X == b.X) && (a.Y == b.Y) && (a.Z == b.Z) && (a.W == b.W);
126};
127
128TT_INLINE bool operator!=(const Vector4 &a, const Vector4 &b)
129{
130 return (a.X != b.X) || (a.Y != b.Y) || (a.Z != b.Z) || (a.W != b.W);
131};
132
133TT_INLINE float operator * (const Vector4 &a,const Vector4 &b)
134{
135 return a[0]*b[0] + a[1]*b[1] + a[2]*b[2] + a[3]*b[3];
136};
137
138TT_INLINE Vector4 Normalize(const Vector4 & vec)
139{
140 float len2 = WWMATH_FLOAT_TINY + vec.Length2();
141 return vec * WWMath::Inv_Sqrt(len2);
142}
143
144TT_INLINE void Swap(Vector4 & a,Vector4 & b)
145{
146 Vector4 tmp(a);
147 a = b;
148 b = tmp;
149}
150
151TT_INLINE Vector4 Lerp(const Vector4 & a, const Vector4 & b, float alpha)
152{
153 return Vector4((a.X + (b.X - a.X) * alpha),(a.Y + (b.Y - a.Y) * alpha),(a.Z + (b.Z - a.Z) * alpha),(a.W + (b.W - a.W) * alpha));
154}
155
156#endif