1#pragma once
2
3#include <basis/seadTypes.h>
4#include <cmath>
5#include <math/seadMathCalcCommon.h>
6#include <math/seadMathPolicies.h>
7#include <math/seadVectorCalcCommon.h>
8#include <math/seadVectorFwd.h>
9
10namespace sead
11{
12template <typename T>
13struct Vector2 : public Policies<T>::Vec2Base
14{
15 /// @warning This constructor leaves member variables uninitialized.
16 Vector2() {}
17 Vector2(const Vector2& other) = default;
18 Vector2(T x, T y);
19
20 Vector2& operator=(const Vector2& other);
21
22 Vector2& operator+=(const Vector2& other);
23
24 friend Vector2 operator+(const Vector2& a, const Vector2& b)
25 {
26 Vector2 o;
27 Vector2CalcCommon<T>::add(o, a, b);
28 return o;
29 }
30
31 Vector2& operator-=(const Vector2& other);
32
33 friend Vector2 operator-(const Vector2& a, const Vector2& b)
34 {
35 Vector2 o;
36 Vector2CalcCommon<T>::sub(o, a, b);
37 return o;
38 }
39
40 Vector2& operator*=(T t);
41
42 friend Vector2 operator*(const Vector2& a, T t)
43 {
44 Vector2 o;
45 Vector2CalcCommon<T>::multScalar(o, a, t);
46 return o;
47 }
48
49 friend Vector2 operator*(T t, const Vector2& a) { return operator*(a, t); }
50
51 Vector2& operator/=(T t);
52
53 friend Vector2 operator/(const Vector2& a, T t) { return {a.x / t, a.y / t}; }
54
55 bool operator==(const Vector2& rhs) const { return this->x == rhs.x && this->y == rhs.y; }
56 bool operator!=(const Vector2& rhs) const { return !operator==(rhs); }
57
58 void multScalar(T t);
59 void negate();
60 void set(const Vector2& other);
61 void set(T x_, T y_);
62 void setAdd(const Vector2<T>& a, const Vector2<T>& b);
63 void setScale(const Vector2<T>& a, T t);
64
65 T dot(const Vector2& other) const;
66 T cross(const Vector2& other) const;
67 T length() const;
68 T squaredLength() const;
69 T normalize();
70
71 bool isZero() const { return *this == zero; }
72
73 static const Vector2 zero;
74 static const Vector2 ex;
75 static const Vector2 ey;
76 static const Vector2 ones;
77};
78
79template <typename T>
80struct Vector3 : public Policies<T>::Vec3Base
81{
82 using Mtx33 = typename Policies<T>::Mtx33Base;
83 using Mtx34 = typename Policies<T>::Mtx34Base;
84 using Mtx44 = typename Policies<T>::Mtx44Base;
85 using Quat = typename Policies<T>::QuatBase;
86
87 /// @warning This constructor leaves member variables uninitialized.
88 Vector3() {}
89 Vector3(const Vector3& other) = default;
90 Vector3(T x, T y, T z);
91
92 Vector3& operator=(const Vector3& other);
93 bool operator==(const Vector3& rhs) const;
94 bool operator!=(const Vector3& rhs) const;
95
96 Vector3& operator+=(const Vector3& other);
97 friend Vector3 operator+(const Vector3& a, const Vector3& b)
98 {
99 Vector3 o;
100 Vector3CalcCommon<T>::add(o, a, b);
101 return o;
102 }
103
104 Vector3& operator-=(const Vector3& other);
105 friend Vector3 operator-(const Vector3& a, const Vector3& b)
106 {
107 Vector3 o;
108 Vector3CalcCommon<T>::sub(o, a, b);
109 return o;
110 }
111
112 Vector3& operator*=(T t);
113 Vector3& operator*=(const Mtx33& m);
114 Vector3& operator*=(const Mtx34& m);
115 Vector3& operator*=(const Mtx44& m);
116 friend Vector3 operator*(const Vector3& a, T t)
117 {
118 Vector3 o;
119 Vector3CalcCommon<T>::multScalar(o, a, t);
120 return o;
121 }
122 friend Vector3 operator*(T t, const Vector3& a) { return operator*(a, t); }
123 friend Vector3 operator*(const Mtx33& m, const Vector3& a)
124 {
125 Vector3 o;
126 o.setMul(m, a);
127 return o;
128 }
129 friend Vector3 operator*(const Mtx34& m, const Vector3& a)
130 {
131 Vector3 o;
132 o.setMul(m, a);
133 return o;
134 }
135 friend Vector3 operator*(const Mtx44& m, const Vector3& a)
136 {
137 Vector3 o;
138 o.setMul(m, a);
139 return o;
140 }
141
142 Vector3& operator/=(T t);
143 friend Vector3 operator/(const Vector3& a, T t) { return {a.x / t, a.y / t, a.z / t}; }
144
145 Vector3 operator-() const { return {-this->x, -this->y, -this->z}; }
146
147 Vector3 cross(const Vector3& t) const
148 {
149 Vector3 o;
150 o.setCross(*this, t);
151 return o;
152 }
153
154 T dot(const Vector3& t) const;
155 T length() const;
156 T squaredLength() const;
157
158 /// Checks if the differences of all components of lhs and rhs are within `epsilon`.
159 /// (i.e. -epsilon <= lhs.x - rhs.x <= epsilon, and so on).
160 bool equals(const Vector3& rhs, T epsilon = 0) const;
161
162 void add(const Vector3& a);
163 /// Apply a rotation `m` to this vector.
164 void mul(const Mtx33& m);
165 /// Apply a transformation `m` (rotation then translation) to this vector.
166 void mul(const Mtx34& m);
167 /// Apply a transformation `m` (rotation, translation, homogenous coordinates) to this vector.
168 void mul(const Mtx44& m);
169 /// Apply a rotation `m` to this vector.
170 void rotate(const Mtx33& m);
171 /// Apply a rotation `m` to this vector.
172 void rotate(const Mtx34& m);
173 /// Apply a rotation `q` to this vector.
174 void rotate(const Quat& q);
175 void multScalar(T t);
176
177 T normalize();
178 void negate();
179 void set(const Vector3& other);
180 void set(T x, T y, T z);
181 void setAdd(const Vector3<T>& a, const Vector3<T>& b);
182 void setCross(const Vector3<T>& a, const Vector3<T>& b);
183 void setScale(const Vector3<T>& a, T t);
184 void setScaleAdd(T t, const Vector3<T>& a, const Vector3<T>& b);
185 void setMul(const Mtx33& m, const Vector3& a);
186 void setMul(const Mtx34& m, const Vector3& a);
187 void setMul(const Mtx44& m, const Vector3& a);
188 void setRotated(const Mtx33& m, const Vector3& a);
189 void setRotated(const Mtx34& m, const Vector3& a);
190 void setRotated(const Quat& q, const Vector3& a);
191 void setSub(const Vector3& a, const Vector3& b);
192
193 static const Vector3 zero;
194 static const Vector3 ex;
195 static const Vector3 ey;
196 static const Vector3 ez;
197 static const Vector3 ones;
198};
199
200template <typename T>
201struct Vector4 : public Policies<T>::Vec4Base
202{
203 /// @warning This constructor leaves member variables uninitialized.
204 Vector4() {}
205 Vector4(const Vector4& other) = default;
206 Vector4(T x, T y, T z, T w);
207
208 Vector4& operator=(const Vector4& other);
209
210 Vector4& operator+=(const Vector4& other);
211
212 friend Vector4 operator+(const Vector4& a, const Vector4& b)
213 {
214 return {a.x + b.x, a.y + b.y, a.z + b.z, a.w + b.w};
215 }
216
217 Vector4& operator-=(const Vector4& other);
218
219 friend Vector4 operator-(const Vector4& a, const Vector4& b)
220 {
221 return {a.x - b.x, a.y - b.y, a.z - b.z, a.w - b.w};
222 }
223
224 Vector4& operator*=(T t);
225
226 friend Vector4 operator*(const Vector4& a, T t) { return {a.x * t, a.y * t, a.z * t, a.w * t}; }
227
228 friend Vector4 operator*(T t, const Vector4& a) { return operator*(a, t); }
229
230 Vector4& operator/=(T t);
231
232 friend Vector4 operator/(const Vector4& a, T t) { return {a.x / t, a.y / t, a.z / t, a.w / t}; }
233
234 bool operator==(const Vector4& rhs) const
235 {
236 return this->x == rhs.x && this->y == rhs.y && this->z == rhs.z && this->w == rhs.w;
237 }
238 bool operator!=(const Vector4& rhs) const { return !operator==(rhs); }
239
240 T normalize();
241 void negate();
242 T length() const;
243 T squaredLength() const;
244 void set(const Vector4& v);
245 void set(T x_, T y_, T z_, T w_);
246
247 static const Vector4 zero;
248 static const Vector4 ex;
249 static const Vector4 ey;
250 static const Vector4 ez;
251 static const Vector4 ew;
252 static const Vector4 ones;
253};
254
255template <>
256const Vector2<f32> Vector2<f32>::zero;
257
258template <>
259const Vector2<f32> Vector2<f32>::ex;
260
261template <>
262const Vector2<f32> Vector2<f32>::ey;
263
264template <>
265const Vector2<f32> Vector2<f32>::ones;
266
267template <>
268const Vector3<f32> Vector3<f32>::zero;
269
270template <>
271const Vector3<f32> Vector3<f32>::ex;
272
273template <>
274const Vector3<f32> Vector3<f32>::ey;
275
276template <>
277const Vector3<f32> Vector3<f32>::ez;
278
279template <>
280const Vector3<f32> Vector3<f32>::ones;
281
282template <>
283const Vector4<f32> Vector4<f32>::zero;
284
285template <>
286const Vector4<f32> Vector4<f32>::ex;
287
288template <>
289const Vector4<f32> Vector4<f32>::ey;
290
291template <>
292const Vector4<f32> Vector4<f32>::ez;
293
294template <>
295const Vector4<f32> Vector4<f32>::ew;
296
297template <>
298const Vector4<f32> Vector4<f32>::ones;
299
300} // namespace sead
301
302#define SEAD_MATH_VECTOR_H_
303#include <math/seadVector.hpp>
304#undef SEAD_MATH_VECTOR_H_
305