Line data Source code
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 :
28 : class CFixedVector3D;
29 :
30 : class CVector3D
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 299150 : CVector3D() : X(0.0f), Y(0.0f), Z(0.0f) {}
41 3992 : 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 93 : float& operator[](int index) { return *((&X)+index); }
47 82 : const float& operator[](int index) const { return *((&X)+index); }
48 :
49 : // vector equality (testing float equality, so please be careful if necessary)
50 63 : bool operator==(const CVector3D &vector) const
51 : {
52 63 : return (X == vector.X && Y == vector.Y && Z == vector.Z);
53 : }
54 :
55 0 : bool operator!=(const CVector3D& vector) const
56 : {
57 0 : return !operator==(vector);
58 : }
59 :
60 129 : CVector3D operator+(const CVector3D& vector) const
61 : {
62 129 : return CVector3D(X + vector.X, Y + vector.Y, Z + vector.Z);
63 : }
64 :
65 0 : CVector3D& operator+=(const CVector3D& vector)
66 : {
67 0 : X += vector.X;
68 0 : Y += vector.Y;
69 0 : Z += vector.Z;
70 0 : return *this;
71 : }
72 :
73 93 : CVector3D operator-(const CVector3D& vector) const
74 : {
75 93 : return CVector3D(X - vector.X, Y - vector.Y, Z - vector.Z);
76 : }
77 :
78 12 : CVector3D& operator-=(const CVector3D& vector)
79 : {
80 12 : X -= vector.X;
81 12 : Y -= vector.Y;
82 12 : Z -= vector.Z;
83 12 : return *this;
84 : }
85 :
86 230 : CVector3D operator*(float value) const
87 : {
88 230 : return CVector3D(X * value, Y * value, Z * value);
89 : }
90 :
91 20 : CVector3D& operator*=(float value)
92 : {
93 20 : X *= value;
94 20 : Y *= value;
95 20 : Z *= value;
96 20 : return *this;
97 : }
98 :
99 27 : CVector3D operator-() const
100 : {
101 27 : return CVector3D(-X, -Y, -Z);
102 : }
103 :
104 208 : float Dot(const CVector3D& vector) const
105 : {
106 416 : return ( X * vector.X +
107 208 : Y * vector.Y +
108 208 : Z * vector.Z );
109 : }
110 :
111 46 : CVector3D Cross(const CVector3D& vector) const
112 : {
113 46 : CVector3D temp;
114 46 : temp.X = (Y * vector.Z) - (Z * vector.Y);
115 46 : temp.Y = (Z * vector.X) - (X * vector.Z);
116 46 : temp.Z = (X * vector.Y) - (Y * vector.X);
117 46 : 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.
126 0 : PS::span<const float> AsFloatArray() const
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 0 : return PS::span<const float>(&X, 3);
137 : }
138 : };
139 :
140 : extern float MaxComponent(const CVector3D& v);
141 :
142 : #endif
|