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 setAdd(const Vector3<T>& a, const Vector3<T>& b);
171 void setCross(const Vector3<T>& a, const Vector3<T>& b);
172 void setScale(const Vector3<T>& a, T t);
173 void setScaleAdd(T t, const Vector3<T>& a, const Vector3<T>& b);
174 void setMul(const Mtx33& m, const Vector3& a);
175 void setMul(const Mtx34& m, const Vector3& a);
176 void setRotated(const Mtx33& m, const Vector3& a);
177 void setRotated(const Mtx34& m, const Vector3& a);
178 void setRotated(const Quat& q, const Vector3& a);
179 void setSub(const Vector3& a, const Vector3& b);
180
181 static const Vector3 zero;
182 static const Vector3 ex;
183 static const Vector3 ey;
184 static const Vector3 ez;
185 static const Vector3 ones;
186};
187
188template <typename T>
189struct Vector4 : public Policies<T>::Vec4Base
190{
191 /// @warning This constructor leaves member variables uninitialized.
192 Vector4() {}
193 Vector4(const Vector4& other) = default;
194 Vector4(T x, T y, T z, T w);
195
196 Vector4& operator=(const Vector4& other);
197
198 Vector4& operator+=(const Vector4& other);
199
200 friend Vector4 operator+(const Vector4& a, const Vector4& b)
201 {
202 return {a.x + b.x, a.y + b.y, a.z + b.z, a.w + b.w};
203 }
204
205 Vector4& operator-=(const Vector4& other);
206
207 friend Vector4 operator-(const Vector4& a, const Vector4& b)
208 {
209 return {a.x - b.x, a.y - b.y, a.z - b.z, a.w - b.w};
210 }
211
212 Vector4& operator*=(T t);
213
214 friend Vector4 operator*(const Vector4& a, T t) { return {a.x * t, a.y * t, a.z * t, a.w * t}; }
215
216 friend Vector4 operator*(T t, const Vector4& a) { return operator*(a, t); }
217
218 Vector4& operator/=(T t);
219
220 friend Vector4 operator/(const Vector4& a, T t) { return {a.x / t, a.y / t, a.z / t, a.w / t}; }
221
222 bool operator==(const Vector4& rhs) const
223 {
224 return this->x == rhs.x && this->y == rhs.y && this->z == rhs.z && this->w == rhs.w;
225 }
226 bool operator!=(const Vector4& rhs) const { return !operator==(rhs); }
227
228 T normalize();
229 void negate();
230 T length() const;
231 T squaredLength() const;
232 void set(const Vector4& v);
233 void set(T x_, T y_, T z_, T w_);
234
235 static const Vector4 zero;
236 static const Vector4 ex;
237 static const Vector4 ey;
238 static const Vector4 ez;
239 static const Vector4 ew;
240 static const Vector4 ones;
241};
242
243template <>
244const Vector2<f32> Vector2<f32>::zero;
245
246template <>
247const Vector2<f32> Vector2<f32>::ex;
248
249template <>
250const Vector2<f32> Vector2<f32>::ey;
251
252template <>
253const Vector2<f32> Vector2<f32>::ones;
254
255template <>
256const Vector3<f32> Vector3<f32>::zero;
257
258template <>
259const Vector3<f32> Vector3<f32>::ex;
260
261template <>
262const Vector3<f32> Vector3<f32>::ey;
263
264template <>
265const Vector3<f32> Vector3<f32>::ez;
266
267template <>
268const Vector3<f32> Vector3<f32>::ones;
269
270template <>
271const Vector4<f32> Vector4<f32>::zero;
272
273template <>
274const Vector4<f32> Vector4<f32>::ex;
275
276template <>
277const Vector4<f32> Vector4<f32>::ey;
278
279template <>
280const Vector4<f32> Vector4<f32>::ez;
281
282template <>
283const Vector4<f32> Vector4<f32>::ew;
284
285template <>
286const Vector4<f32> Vector4<f32>::ones;
287
288} // namespace sead
289
290#define SEAD_MATH_VECTOR_H_
291#include <math/seadVector.hpp>
292#undef SEAD_MATH_VECTOR_H_
293