Merge 'remotes/trunk'
[0ad.git] / source / maths / Vector4D.h
blob9765b44a1e2d4a365a35db1779bece12ee4a37a2
1 /* Copyright (C) 2012 Wildfire Games.
2 * This file is part of 0 A.D.
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.
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.
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/>.
19 * Provides an interface for a vector in R4 and allows vector and
20 * scalar operations on it
23 #ifndef INCLUDED_VECTOR4D
24 #define INCLUDED_VECTOR4D
26 #include <math.h>
28 class CVector4D
30 public:
31 CVector4D() : X(0.0f), Y(0.0f), Z(0.0f), W(0.0f) { }
33 CVector4D(float x, float y, float z, float w) : X(x), Y(y), Z(z), W(w) { }
35 bool operator==(const CVector4D& t) const
37 return (X == t.X && Y == t.Y && Z == t.Z && W == t.W);
40 bool operator!=(const CVector4D& t) const
42 return !(*this == t);
45 CVector4D operator-() const
47 return CVector4D(-X, -Y, -Z, -W);
50 CVector4D operator+(const CVector4D& t) const
52 return CVector4D(X+t.X, Y+t.Y, Z+t.Z, W+t.W);
55 CVector4D operator-(const CVector4D& t) const
57 return CVector4D(X-t.X, Y-t.Y, Z-t.Z, W-t.W);
60 CVector4D operator*(const CVector4D& t) const
62 return CVector4D(X*t.X, Y*t.Y, Z*t.Z, W*t.W);
65 CVector4D operator*(float f) const
67 return CVector4D(X*f, Y*f, Z*f, W*f);
70 CVector4D operator/(float f) const
72 float inv_f = 1.0f / f;
73 return CVector4D(X*inv_f, Y*inv_f, Z*inv_f, W*inv_f);
76 CVector4D& operator+=(const CVector4D& t)
78 X += t.X;
79 Y += t.Y;
80 Z += t.Z;
81 W += t.W;
82 return *this;
85 CVector4D& operator-=(const CVector4D& t)
87 X -= t.X;
88 Y -= t.Y;
89 Z -= t.Z;
90 W -= t.W;
91 return *this;
94 CVector4D& operator*=(const CVector4D& t)
96 X *= t.X;
97 Y *= t.Y;
98 Z *= t.Z;
99 W *= t.W;
100 return *this;
103 CVector4D& operator*=(float f)
105 X *= f;
106 Y *= f;
107 Z *= f;
108 W *= f;
109 return *this;
112 CVector4D& operator/=(float f)
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;
122 float Dot(const CVector4D& a) const
124 return X*a.X + Y*a.Y + Z*a.Z + W*a.W;
128 float X, Y, Z, W;
131 #endif