Pyrogenesis HEAD
Pyrogenesis, a RTS Engine
Vector4D.h
Go to the documentation of this file.
1/* Copyright (C) 2022 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 "ps/containers/Span.h"
27
28#include <math.h>
29
31{
32public:
33 CVector4D() : X(0.0f), Y(0.0f), Z(0.0f), W(0.0f) { }
34
35 CVector4D(float x, float y, float z, float w) : X(x), Y(y), Z(z), W(w) { }
36
37 bool operator==(const CVector4D& t) const
38 {
39 return (X == t.X && Y == t.Y && Z == t.Z && W == t.W);
40 }
41
42 bool operator!=(const CVector4D& t) const
43 {
44 return !(*this == t);
45 }
46
48 {
49 return CVector4D(-X, -Y, -Z, -W);
50 }
51
53 {
54 return CVector4D(X+t.X, Y+t.Y, Z+t.Z, W+t.W);
55 }
56
58 {
59 return CVector4D(X-t.X, Y-t.Y, Z-t.Z, W-t.W);
60 }
61
63 {
64 return CVector4D(X*t.X, Y*t.Y, Z*t.Z, W*t.W);
65 }
66
67 CVector4D operator*(float f) const
68 {
69 return CVector4D(X*f, Y*f, Z*f, W*f);
70 }
71
72 CVector4D operator/(float f) const
73 {
74 float inv_f = 1.0f / f;
75 return CVector4D(X*inv_f, Y*inv_f, Z*inv_f, W*inv_f);
76 }
77
79 {
80 X += t.X;
81 Y += t.Y;
82 Z += t.Z;
83 W += t.W;
84 return *this;
85 }
86
88 {
89 X -= t.X;
90 Y -= t.Y;
91 Z -= t.Z;
92 W -= t.W;
93 return *this;
94 }
95
97 {
98 X *= t.X;
99 Y *= t.Y;
100 Z *= t.Z;
101 W *= t.W;
102 return *this;
103 }
104
106 {
107 X *= f;
108 Y *= f;
109 Z *= f;
110 W *= f;
111 return *this;
112 }
113
115 {
116 float inv_f = 1.0f / f;
117 X *= inv_f;
118 Y *= inv_f;
119 Z *= inv_f;
120 W *= inv_f;
121 return *this;
122 }
123
124 float Dot(const CVector4D& a) const
125 {
126 return X*a.X + Y*a.Y + Z*a.Z + W*a.W;
127 }
128
129 // Returns 4 element array of floats, e.g. for vec4 uniforms.
131 {
132 // Additional check to prevent a weird compiler has a different
133 // alignement for an array and a class members.
134 static_assert(
135 sizeof(CVector4D) == sizeof(float) * 4u &&
136 offsetof(CVector4D, X) == 0 &&
137 offsetof(CVector4D, W) == sizeof(float) * 3u,
138 "CVector4D should be properly layouted to use AsFloatArray");
139 return PS::span<const float>(&X, 4);
140 }
141
142 float X, Y, Z, W;
143};
144
145#endif // INCLUDED_VECTOR4D
Definition: Vector4D.h:31
CVector4D & operator+=(const CVector4D &t)
Definition: Vector4D.h:78
CVector4D operator/(float f) const
Definition: Vector4D.h:72
PS::span< const float > AsFloatArray() const
Definition: Vector4D.h:130
float Dot(const CVector4D &a) const
Definition: Vector4D.h:124
CVector4D & operator/=(float f)
Definition: Vector4D.h:114
CVector4D & operator-=(const CVector4D &t)
Definition: Vector4D.h:87
CVector4D operator-(const CVector4D &t) const
Definition: Vector4D.h:57
CVector4D operator*(float f) const
Definition: Vector4D.h:67
bool operator!=(const CVector4D &t) const
Definition: Vector4D.h:42
CVector4D()
Definition: Vector4D.h:33
float Z
Definition: Vector4D.h:142
CVector4D operator-() const
Definition: Vector4D.h:47
CVector4D operator+(const CVector4D &t) const
Definition: Vector4D.h:52
float X
Definition: Vector4D.h:142
CVector4D & operator*=(float f)
Definition: Vector4D.h:105
bool operator==(const CVector4D &t) const
Definition: Vector4D.h:37
CVector4D operator*(const CVector4D &t) const
Definition: Vector4D.h:62
float W
Definition: Vector4D.h:142
CVector4D(float x, float y, float z, float w)
Definition: Vector4D.h:35
CVector4D & operator*=(const CVector4D &t)
Definition: Vector4D.h:96
float Y
Definition: Vector4D.h:142
Simplifed version of std::span (C++20) as we don't support the original one yet.
Definition: Span.h:37