Pyrogenesis  trunk
Vector4D.h
Go to the documentation of this file.
1 /* Copyright (C) 2012 Wildfire Games.
2  * This file is part of 0 A.D.
3  *
4  * 0 A.D. is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * 0 A.D. is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 /*
19  * Provides an interface for a vector in R4 and allows vector and
20  * scalar operations on it
21  */
22 
23 #ifndef INCLUDED_VECTOR4D
24 #define INCLUDED_VECTOR4D
25 
26 #include <math.h>
27 
28 class CVector4D
29 {
30 public:
31  CVector4D() : X(0.0f), Y(0.0f), Z(0.0f), W(0.0f) { }
32 
33  CVector4D(float x, float y, float z, float w) : X(x), Y(y), Z(z), W(w) { }
34 
35  bool operator==(const CVector4D& t) const
36  {
37  return (X == t.X && Y == t.Y && Z == t.Z && W == t.W);
38  }
39 
40  bool operator!=(const CVector4D& t) const
41  {
42  return !(*this == t);
43  }
44 
46  {
47  return CVector4D(-X, -Y, -Z, -W);
48  }
49 
50  CVector4D operator+(const CVector4D& t) const
51  {
52  return CVector4D(X+t.X, Y+t.Y, Z+t.Z, W+t.W);
53  }
54 
55  CVector4D operator-(const CVector4D& t) const
56  {
57  return CVector4D(X-t.X, Y-t.Y, Z-t.Z, W-t.W);
58  }
59 
60  CVector4D operator*(const CVector4D& t) const
61  {
62  return CVector4D(X*t.X, Y*t.Y, Z*t.Z, W*t.W);
63  }
64 
65  CVector4D operator*(float f) const
66  {
67  return CVector4D(X*f, Y*f, Z*f, W*f);
68  }
69 
70  CVector4D operator/(float f) const
71  {
72  float inv_f = 1.0f / f;
73  return CVector4D(X*inv_f, Y*inv_f, Z*inv_f, W*inv_f);
74  }
75 
77  {
78  X += t.X;
79  Y += t.Y;
80  Z += t.Z;
81  W += t.W;
82  return *this;
83  }
84 
86  {
87  X -= t.X;
88  Y -= t.Y;
89  Z -= t.Z;
90  W -= t.W;
91  return *this;
92  }
93 
95  {
96  X *= t.X;
97  Y *= t.Y;
98  Z *= t.Z;
99  W *= t.W;
100  return *this;
101  }
102 
104  {
105  X *= f;
106  Y *= f;
107  Z *= f;
108  W *= f;
109  return *this;
110  }
111 
113  {
114  float inv_f = 1.0f / f;
115  X *= inv_f;
116  Y *= inv_f;
117  Z *= inv_f;
118  W *= inv_f;
119  return *this;
120  }
121 
122  float Dot(const CVector4D& a) const
123  {
124  return X*a.X + Y*a.Y + Z*a.Z + W*a.W;
125  }
126 
127 
128  float X, Y, Z, W;
129 };
130 
131 #endif
CVector4D()
Definition: Vector4D.h:31
CVector4D & operator*=(const CVector4D &t)
Definition: Vector4D.h:94
CVector4D operator+(const CVector4D &t) const
Definition: Vector4D.h:50
CVector4D & operator/=(float f)
Definition: Vector4D.h:112
CVector4D(float x, float y, float z, float w)
Definition: Vector4D.h:33
bool operator!=(const CVector4D &t) const
Definition: Vector4D.h:40
CVector4D & operator-=(const CVector4D &t)
Definition: Vector4D.h:85
float Y
Definition: Vector4D.h:128
Definition: Vector4D.h:28
CVector4D & operator*=(float f)
Definition: Vector4D.h:103
CVector4D & operator+=(const CVector4D &t)
Definition: Vector4D.h:76
float Z
Definition: Vector4D.h:128
bool operator==(const CVector4D &t) const
Definition: Vector4D.h:35
CVector4D operator*(float f) const
Definition: Vector4D.h:65
float Dot(const CVector4D &a) const
Definition: Vector4D.h:122
CVector4D operator*(const CVector4D &t) const
Definition: Vector4D.h:60
float W
Definition: Vector4D.h:128
CVector4D operator/(float f) const
Definition: Vector4D.h:70
CVector4D operator-(const CVector4D &t) const
Definition: Vector4D.h:55
float X
Definition: Vector4D.h:128
CVector4D operator-() const
Definition: Vector4D.h:45