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