Pyrogenesis HEAD
Pyrogenesis, a RTS Engine
Vector3D.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 R3 and allows vector and
20 * scalar operations on it
21 */
22
23#ifndef INCLUDED_VECTOR3D
24#define INCLUDED_VECTOR3D
25
26#include <cstddef>
27#include <span>
28
29class CFixedVector3D;
30
32{
33 public:
34 float X, Y, Z;
35
36 public:
37 // Returns maximum/minimum possible position stored in the CVector3D.
38 static CVector3D Max();
39 static CVector3D Min();
40
41 CVector3D() : X(0.0f), Y(0.0f), Z(0.0f) {}
42 CVector3D(float x, float y, float z) : X(x), Y(y), Z(z) {}
43 CVector3D(const CFixedVector3D& v);
44
45 int operator!() const;
46
47 float& operator[](int index) { return *((&X)+index); }
48 const float& operator[](int index) const { return *((&X)+index); }
49
50 // vector equality (testing float equality, so please be careful if necessary)
51 bool operator==(const CVector3D &vector) const
52 {
53 return (X == vector.X && Y == vector.Y && Z == vector.Z);
54 }
55
56 bool operator!=(const CVector3D& vector) const
57 {
58 return !operator==(vector);
59 }
60
61 CVector3D operator+(const CVector3D& vector) const
62 {
63 return CVector3D(X + vector.X, Y + vector.Y, Z + vector.Z);
64 }
65
67 {
68 X += vector.X;
69 Y += vector.Y;
70 Z += vector.Z;
71 return *this;
72 }
73
74 CVector3D operator-(const CVector3D& vector) const
75 {
76 return CVector3D(X - vector.X, Y - vector.Y, Z - vector.Z);
77 }
78
80 {
81 X -= vector.X;
82 Y -= vector.Y;
83 Z -= vector.Z;
84 return *this;
85 }
86
87 CVector3D operator*(float value) const
88 {
89 return CVector3D(X * value, Y * value, Z * value);
90 }
91
92 CVector3D& operator*=(float value)
93 {
94 X *= value;
95 Y *= value;
96 Z *= value;
97 return *this;
98 }
99
101 {
102 return CVector3D(-X, -Y, -Z);
103 }
104
105 float Dot(const CVector3D& vector) const
106 {
107 return ( X * vector.X +
108 Y * vector.Y +
109 Z * vector.Z );
110 }
111
112 CVector3D Cross(const CVector3D& vector) const
113 {
114 CVector3D temp;
115 temp.X = (Y * vector.Z) - (Z * vector.Y);
116 temp.Y = (Z * vector.X) - (X * vector.Z);
117 temp.Z = (X * vector.Y) - (Y * vector.X);
118 return temp;
119 }
120
121 float Length() const;
122 float LengthSquared() const;
123 void Normalize();
124 CVector3D Normalized() const;
125
126 // Returns 3 element array of floats, e.g. for vec3 uniforms.
127 std::span<const float, 3> AsFloatArray() const
128 {
129 // Additional check to prevent a weird compiler has a different
130 // alignement for an array and a class members.
131 static_assert(
132 sizeof(CVector3D) == sizeof(float) * 3u &&
133 offsetof(CVector3D, X) == 0 &&
134 offsetof(CVector3D, Y) == sizeof(float) &&
135 offsetof(CVector3D, Z) == sizeof(float) * 2u,
136 "Vector3D should be properly layouted to use AsFloatArray");
137 return std::span<const float, 3>(&X, 3);
138 }
139};
140
141extern float MaxComponent(const CVector3D& v);
142
143#endif
float MaxComponent(const CVector3D &v)
Definition: Vector3D.cpp:93
Definition: FixedVector3D.h:25
Definition: Vector3D.h:32
bool operator==(const CVector3D &vector) const
Definition: Vector3D.h:51
const float & operator[](int index) const
Definition: Vector3D.h:48
CVector3D operator*(float value) const
Definition: Vector3D.h:87
bool operator!=(const CVector3D &vector) const
Definition: Vector3D.h:56
std::span< const float, 3 > AsFloatArray() const
Definition: Vector3D.h:127
void Normalize()
Definition: Vector3D.cpp:74
CVector3D & operator*=(float value)
Definition: Vector3D.h:92
float LengthSquared() const
Definition: Vector3D.cpp:64
float Z
Definition: Vector3D.h:34
CVector3D()
Definition: Vector3D.h:41
static CVector3D Max()
Definition: Vector3D.cpp:40
CVector3D operator+(const CVector3D &vector) const
Definition: Vector3D.h:61
CVector3D(float x, float y, float z)
Definition: Vector3D.h:42
static CVector3D Min()
Definition: Vector3D.cpp:47
CVector3D & operator+=(const CVector3D &vector)
Definition: Vector3D.h:66
float Length() const
Definition: Vector3D.cpp:69
float Y
Definition: Vector3D.h:34
float Dot(const CVector3D &vector) const
Definition: Vector3D.h:105
CVector3D Cross(const CVector3D &vector) const
Definition: Vector3D.h:112
CVector3D & operator-=(const CVector3D &vector)
Definition: Vector3D.h:79
float X
Definition: Vector3D.h:34
CVector3D operator-(const CVector3D &vector) const
Definition: Vector3D.h:74
CVector3D Normalized() const
Definition: Vector3D.cpp:83
CVector3D operator-() const
Definition: Vector3D.h:100
float & operator[](int index)
Definition: Vector3D.h:47
int operator!() const
Definition: Vector3D.cpp:53