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 bool isNan() const { return sead::Mathf::isNan(this->x) || sead::Mathf::isNan(this->y); }
73
74 static const Vector2 zero;
75 static const Vector2 ex;
76 static const Vector2 ey;
77 static const Vector2 ones;
78};
79
80template <typename T>
81struct Vector3 : public Policies<T>::Vec3Base
82{
83 using Mtx33 = typename Policies<T>::Mtx33Base;
84 using Mtx34 = typename Policies<T>::Mtx34Base;
85 using Mtx44 = typename Policies<T>::Mtx44Base;
86 using Quat = typename Policies<T>::QuatBase;
87
88 /// @warning This constructor leaves member variables uninitialized.
89 Vector3() {}
90 Vector3(const Vector3& other) = default;
91 Vector3(T x, T y, T z);
92
93 Vector3& operator=(const Vector3& other);
94 bool operator==(const Vector3& rhs) const;
95 bool operator!=(const Vector3& rhs) const;
96
97 Vector3& operator+=(const Vector3& other);
98 friend Vector3 operator+(const Vector3& a, const Vector3& b)
99 {
100 Vector3 o;
101 Vector3CalcCommon<T>::add(o, a, b);
102 return o;
103 }
104
105 Vector3& operator-=(const Vector3& other);
106 friend Vector3 operator-(const Vector3& a, const Vector3& b)
107 {
108 Vector3 o;
109 Vector3CalcCommon<T>::sub(o, a, b);
110 return o;
111 }
112
113 Vector3& operator*=(T t);
114 Vector3& operator*=(const Mtx33& m);
115 Vector3& operator*=(const Mtx34& m);
116 Vector3& operator*=(const Mtx44& m);
117 friend Vector3 operator*(const Vector3& a, T t)
118 {
119 Vector3 o;
120 Vector3CalcCommon<T>::multScalar(o, a, t);
121 return o;
122 }
123 friend Vector3 operator*(T t, const Vector3& a) { return operator*(a, t); }
124 friend Vector3 operator*(const Mtx33& m, const Vector3& a)
125 {
126 Vector3 o;
127 o.setMul(m, a);
128 return o;
129 }
130 friend Vector3 operator*(const Mtx34& m, const Vector3& a)
131 {
132 Vector3 o;
133 o.setMul(m, a);
134 return o;
135 }
136 friend Vector3 operator*(const Mtx44& m, const Vector3& a)
137 {
138 Vector3 o;
139 o.setMul(m, a);
140 return o;
141 }
142
143 Vector3& operator/=(T t);
144 friend Vector3 operator/(const Vector3& a, T t) { return {a.x / t, a.y / t, a.z / t}; }
145
146 Vector3 operator-() const { return {-this->x, -this->y, -this->z}; }
147
148 Vector3 cross(const Vector3& t) const
149 {
150 Vector3 o;
151 o.setCross(*this, t);
152 return o;
153 }
154
155 T dot(const Vector3& t) const;
156 T length() const;
157 T squaredLength() const;
158
159 /// Checks if the differences of all components of lhs and rhs are within `epsilon`.
160 /// (i.e. -epsilon <= lhs.x - rhs.x <= epsilon, and so on).
161 bool equals(const Vector3& rhs, T epsilon = 0) const;
162
163 void add(const Vector3& a);
164 /// Apply a rotation `m` to this vector.
165 void mul(const Mtx33& m);
166 /// Apply a transformation `m` (rotation then translation) to this vector.
167 void mul(const Mtx34& m);
168 /// Apply a transformation `m` (rotation, translation, homogenous coordinates) to this vector.
169 void mul(const Mtx44& m);
170 /// Apply a rotation `m` to this vector.
171 void rotate(const Mtx33& m);
172 /// Apply a rotation `m` to this vector.
173 void rotate(const Mtx34& m);
174 /// Apply a rotation `q` to this vector.
175 void rotate(const Quat& q);
176 void multScalar(T t);
177
178 T normalize();
179 void negate();
180 void set(const Vector3& other);
181 void set(T x, T y, T z);
182 void setAdd(const Vector3<T>& a, const Vector3<T>& b);
183 void setCross(const Vector3<T>& a, const Vector3<T>& b);
184 void setScale(const Vector3<T>& a, T t);
185 void setScaleAdd(T t, const Vector3<T>& a, const Vector3<T>& b);
186 void setMul(const Mtx33& m, const Vector3& a);
187 void setMul(const Mtx34& m, const Vector3& a);
188 void setMul(const Mtx44& m, const Vector3& a);
189 void setRotated(const Mtx33& m, const Vector3& a);
190 void setRotated(const Mtx34& m, const Vector3& a);
191 void setRotated(const Quat& q, const Vector3& a);
192 void setSub(const Vector3& a, const Vector3& b);
193
194 bool isNan() const
195 {
196 return sead::Mathf::isNan(this->x) || sead::Mathf::isNan(this->y) ||
197 sead::Mathf::isNan(this->z);
198 }
199
200 static const Vector3 zero;
201 static const Vector3 ex;
202 static const Vector3 ey;
203 static const Vector3 ez;
204 static const Vector3 ones;
205};
206
207template <typename T>
208struct Vector4 : public Policies<T>::Vec4Base
209{
210 /// @warning This constructor leaves member variables uninitialized.
211 Vector4() {}
212 Vector4(const Vector4& other) = default;
213 Vector4(T x, T y, T z, T w);
214
215 Vector4& operator=(const Vector4& other);
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-=(const Vector4& other);
225
226 friend Vector4 operator-(const Vector4& a, const Vector4& b)
227 {
228 return {a.x - b.x, a.y - b.y, a.z - b.z, a.w - b.w};
229 }
230
231 Vector4& operator*=(T t);
232
233 friend Vector4 operator*(const Vector4& a, T t) { return {a.x * t, a.y * t, a.z * t, a.w * t}; }
234
235 friend Vector4 operator*(T t, const Vector4& a) { return operator*(a, t); }
236
237 Vector4& operator/=(T t);
238
239 friend Vector4 operator/(const Vector4& a, T t) { return {a.x / t, a.y / t, a.z / t, a.w / t}; }
240
241 bool operator==(const Vector4& rhs) const
242 {
243 return this->x == rhs.x && this->y == rhs.y && this->z == rhs.z && this->w == rhs.w;
244 }
245 bool operator!=(const Vector4& rhs) const { return !operator==(rhs); }
246
247 T normalize();
248 void negate();
249 T length() const;
250 T squaredLength() const;
251 void set(const Vector4& v);
252 void set(T x_, T y_, T z_, T w_);
253
254 bool isNan() const
255 {
256 return sead::Mathf::isNan(this->x) || sead::Mathf::isNan(this->y) ||
257 sead::Mathf::isNan(this->z) || sead::Mathf::isNan(this->w);
258 }
259
260 static const Vector4 zero;
261 static const Vector4 ex;
262 static const Vector4 ey;
263 static const Vector4 ez;
264 static const Vector4 ew;
265 static const Vector4 ones;
266};
267
268template <>
269const Vector2<f32> Vector2<f32>::zero;
270
271template <>
272const Vector2<f32> Vector2<f32>::ex;
273
274template <>
275const Vector2<f32> Vector2<f32>::ey;
276
277template <>
278const Vector2<f32> Vector2<f32>::ones;
279
280template <>
281const Vector3<f32> Vector3<f32>::zero;
282
283template <>
284const Vector3<f32> Vector3<f32>::ex;
285
286template <>
287const Vector3<f32> Vector3<f32>::ey;
288
289template <>
290const Vector3<f32> Vector3<f32>::ez;
291
292template <>
293const Vector3<f32> Vector3<f32>::ones;
294
295template <>
296const Vector4<f32> Vector4<f32>::zero;
297
298template <>
299const Vector4<f32> Vector4<f32>::ex;
300
301template <>
302const Vector4<f32> Vector4<f32>::ey;
303
304template <>
305const Vector4<f32> Vector4<f32>::ez;
306
307template <>
308const Vector4<f32> Vector4<f32>::ew;
309
310template <>
311const Vector4<f32> Vector4<f32>::ones;
312
313} // namespace sead
314
315#define SEAD_MATH_VECTOR_H_
316#include <math/seadVector.hpp>
317#undef SEAD_MATH_VECTOR_H_
318