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