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