Merge 'remotes/trunk'
[0ad.git] / source / maths / Vector3D.h
blobd5a75df0901e434c318bacef52c9f7a1852901d3
1 /* Copyright (C) 2022 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 R3 and allows vector and
20 * scalar operations on it
23 #ifndef INCLUDED_VECTOR3D
24 #define INCLUDED_VECTOR3D
26 #include "ps/containers/Span.h"
28 class CFixedVector3D;
30 class CVector3D
32 public:
33 float X, Y, Z;
35 public:
36 // Returns maximum/minimum possible position stored in the CVector3D.
37 static CVector3D Max();
38 static CVector3D Min();
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);
44 int operator!() const;
46 float& operator[](int index) { return *((&X)+index); }
47 const float& operator[](int index) const { return *((&X)+index); }
49 // vector equality (testing float equality, so please be careful if necessary)
50 bool operator==(const CVector3D &vector) const
52 return (X == vector.X && Y == vector.Y && Z == vector.Z);
55 bool operator!=(const CVector3D& vector) const
57 return !operator==(vector);
60 CVector3D operator+(const CVector3D& vector) const
62 return CVector3D(X + vector.X, Y + vector.Y, Z + vector.Z);
65 CVector3D& operator+=(const CVector3D& vector)
67 X += vector.X;
68 Y += vector.Y;
69 Z += vector.Z;
70 return *this;
73 CVector3D operator-(const CVector3D& vector) const
75 return CVector3D(X - vector.X, Y - vector.Y, Z - vector.Z);
78 CVector3D& operator-=(const CVector3D& vector)
80 X -= vector.X;
81 Y -= vector.Y;
82 Z -= vector.Z;
83 return *this;
86 CVector3D operator*(float value) const
88 return CVector3D(X * value, Y * value, Z * value);
91 CVector3D& operator*=(float value)
93 X *= value;
94 Y *= value;
95 Z *= value;
96 return *this;
99 CVector3D operator-() const
101 return CVector3D(-X, -Y, -Z);
104 float Dot(const CVector3D& vector) const
106 return ( X * vector.X +
107 Y * vector.Y +
108 Z * vector.Z );
111 CVector3D Cross(const CVector3D& vector) const
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;
120 float Length() const;
121 float LengthSquared() const;
122 void Normalize();
123 CVector3D Normalized() const;
125 // Returns 3 element array of floats, e.g. for vec3 uniforms.
126 PS::span<const float> AsFloatArray() const
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);
140 extern float MaxComponent(const CVector3D& v);
142 #endif