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