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