16 constexpr explicit Vector2() noexcept : x{0.0f}, y{0.0f} {}
17 constexpr explicit Vector2(
float x_,
float y_) noexcept : x{x_}, y{y_} {}
18 static constexpr Vector2 Splat(
float v)
noexcept {
return Vector2{v, v}; }
19 static constexpr Vector2 Zero()
noexcept {
return Vector2{ 0, 0}; }
22 static constexpr Vector2 Down()
noexcept {
return Vector2{ 0,-1}; }
23 static constexpr Vector2 Left()
noexcept {
return Vector2{-1, 0}; }
24 static constexpr Vector2 Right()
noexcept {
return Vector2{ 1, 0}; }
32 constexpr Vector3() noexcept : x{0.0f}, y{0.0f}, z{0.0f} {}
33 constexpr explicit Vector3(
float x_,
float y_,
float z_) noexcept : x{x_}, y{y_}, z{z_} {}
34 constexpr explicit Vector3(
const Vector2& v,
float z_) noexcept : x{v.x}, y{v.y}, z{z_} {}
35 static constexpr Vector3 Splat(
float v)
noexcept {
return Vector3{v, v, v}; }
36 static constexpr Vector3 Zero()
noexcept {
return Vector3{ 0, 0, 0}; }
37 static constexpr Vector3 One()
noexcept {
return Vector3{ 1, 1, 1}; }
38 static constexpr Vector3 Up()
noexcept {
return Vector3{ 0, 1, 0}; }
39 static constexpr Vector3 Down()
noexcept {
return Vector3{ 0,-1, 0}; }
40 static constexpr Vector3 Left()
noexcept {
return Vector3{-1, 0, 0}; }
41 static constexpr Vector3 Right()
noexcept {
return Vector3{ 1, 0, 0}; }
42 static constexpr Vector3 Front()
noexcept {
return Vector3{ 0, 0, 1}; }
43 static constexpr Vector3 Back()
noexcept {
return Vector3{ 0, 0,-1}; }
51 constexpr explicit Vector4() noexcept : x{0.0f}, y{0.0f}, z{0.0f}, w{0.0f} {}
52 constexpr explicit Vector4(
float x_,
float y_,
float z_,
float w_) noexcept : x{x_}, y{y_}, z{z_}, w{w_} {}
53 constexpr explicit Vector4(
const Vector3& v,
float w_) noexcept : x{v.x}, y{v.y}, z{v.z}, w{w_} {}
54 static constexpr Vector4 Splat(
float v)
noexcept {
return Vector4{v, v, v, v}; }
55 static constexpr Vector4 Zero()
noexcept {
return Vector4(0, 0, 0, 0); }
56 static constexpr Vector4 One()
noexcept {
return Vector4(1, 1, 1, 1); }
60constexpr auto operator ==(
const Vector2& lhs,
const Vector2& rhs)
noexcept -> bool;
61constexpr auto operator !=(
const Vector2& lhs,
const Vector2& rhs)
noexcept -> bool;
64constexpr auto operator *(
const Vector2& vec,
float scalar)
noexcept ->
Vector2;
65constexpr auto operator *(
float scalar,
const Vector2& vec)
noexcept ->
Vector2;
66constexpr auto operator /(
const Vector2& vec,
float scalar)
noexcept ->
Vector2;
68constexpr auto Dot(
const Vector2& lhs,
const Vector2& rhs)
noexcept -> float;
70constexpr auto SquareMagnitude(
const Vector2& vec)
noexcept -> float;
71inline auto Magnitude(
const Vector2& vec)
noexcept -> float;
73constexpr auto SquareDistance(
const Vector2& lhs,
const Vector2& rhs)
noexcept -> float;
74inline auto Distance(
const Vector2& lhs,
const Vector2& rhs)
noexcept -> float;
75constexpr auto HasAnyZeroElement(
const Vector2& vec)
noexcept -> bool;
76constexpr auto HasUniformElements(
const Vector2& vec)
noexcept -> bool;
79constexpr auto operator ==(
const Vector3& lhs,
const Vector3& rhs)
noexcept -> bool;
80constexpr auto operator !=(
const Vector3& lhs,
const Vector3& rhs)
noexcept -> bool;
83constexpr auto operator *(
const Vector3& vec,
float scalar)
noexcept ->
Vector3;
84constexpr auto operator *(
float scalar,
const Vector3& vec)
noexcept ->
Vector3;
85constexpr auto operator /(
const Vector3& vec,
float scalar)
noexcept ->
Vector3;
89constexpr auto operator *=(
Vector3& lhs,
float rhs)
noexcept ->
Vector3&;
90constexpr auto operator /=(
Vector3& lhs,
float rhs)
noexcept ->
Vector3&;
91constexpr auto Dot(
const Vector3& lhs,
const Vector3& rhs)
noexcept -> float;
95constexpr auto SquareMagnitude(
const Vector3& vec)
noexcept -> float;
96inline auto Magnitude(
const Vector3& vec)
noexcept -> float;
98constexpr auto SquareDistance(
const Vector3& lhs,
const Vector3& rhs)
noexcept -> float;
99inline auto Distance(
const Vector3& lhs,
const Vector3& rhs)
noexcept -> float;
100constexpr auto HasAnyZeroElement(
const Vector3& vec)
noexcept -> bool;
101constexpr auto HasUniformElements(
const Vector3& vec)
noexcept -> bool;
107constexpr auto operator ==(
const Vector4& lhs,
const Vector4& rhs)
noexcept -> bool;
108constexpr auto operator !=(
const Vector4& lhs,
const Vector4& rhs)
noexcept -> bool;
111constexpr auto operator *(
const Vector4& vec,
float scalar)
noexcept ->
Vector4;
112constexpr auto operator *(
float scalar,
const Vector4& vec)
noexcept ->
Vector4;
113constexpr auto operator /(
const Vector4& vec,
float scalar)
noexcept ->
Vector4;
115constexpr auto Dot(
const Vector4& lhs,
const Vector4& rhs)
noexcept -> float;
117constexpr auto SquareMagnitude(
const Vector4& vec)
noexcept -> float;
118inline auto Magnitude(
const Vector4& vec)
noexcept -> float;
120constexpr auto SquareDistance(
const Vector4& lhs,
const Vector4& rhs)
noexcept -> float;
121inline auto Distance(
const Vector4& lhs,
const Vector4& rhs)
noexcept -> float;
122constexpr auto HasAnyZeroElement(
const Vector4& vec)
noexcept -> bool;
123constexpr auto HasUniformElements(
const Vector4& vec)
noexcept -> bool;
127constexpr bool operator ==(
const Vector2& lhs,
const Vector2& rhs)
noexcept
129 return FloatEqual(lhs.x, rhs.x) && FloatEqual(lhs.y, rhs.y);
132constexpr bool operator !=(
const Vector2& lhs,
const Vector2& rhs)
noexcept
134 return !(lhs == rhs);
137constexpr Vector2 operator +(
const Vector2& lhs,
const Vector2& rhs)
noexcept
139 return Vector2(lhs.x + rhs.x, lhs.y + rhs.y);
142constexpr Vector2 operator -(
const Vector2& lhs,
const Vector2& rhs)
noexcept
144 return Vector2(lhs.x - rhs.x, lhs.y - rhs.y);
147constexpr Vector2 operator *(
const Vector2& vec,
float scalar)
noexcept
149 return Vector2(vec.x * scalar, vec.y * scalar);
152constexpr Vector2 operator *(
float scalar,
const Vector2& vec)
noexcept
157constexpr Vector2 operator /(
const Vector2& vec,
float scalar)
noexcept
159 return Vector2(vec.x / scalar, vec.y / scalar);
162constexpr Vector2 operator -(
const Vector2& vec)
noexcept
167constexpr float Dot(
const Vector2& lhs,
const Vector2& rhs)
noexcept
169 return lhs.x * rhs.x + lhs.y * rhs.y;
172constexpr Vector2 HadamardProduct(
const Vector2& lhs,
const Vector2& rhs)
noexcept
174 return Vector2{lhs.x * rhs.x, lhs.y * rhs.y};
177constexpr float SquareMagnitude(
const Vector2& vec)
noexcept
179 return Dot(vec, vec);
182float Magnitude(
const Vector2& vec)
noexcept
184 return sqrtf(SquareMagnitude(vec));
187Vector2
Normalize(
const Vector2& vec)
noexcept
189 return vec == Vector2::Zero() ? Vector2::Zero() : vec / Magnitude(vec);
192constexpr float SquareDistance(
const Vector2& lhs,
const Vector2& rhs)
noexcept
194 return SquareMagnitude(lhs - rhs);
197float Distance(
const Vector2& lhs,
const Vector2& rhs)
noexcept
199 return Magnitude(lhs - rhs);
202constexpr bool HasAnyZeroElement(
const Vector2& vec)
noexcept
204 return FloatEqual(vec.x, 0.0f) || FloatEqual(vec.y, 0.0f);
207constexpr auto HasUniformElements(
const Vector2& vec)
noexcept ->
bool
209 return FloatEqual(vec.x, vec.y);
213constexpr bool operator ==(
const Vector3& lhs,
const Vector3& rhs)
noexcept
215 return FloatEqual(lhs.x, rhs.x) && FloatEqual(lhs.y, rhs.y) && FloatEqual(lhs.z, rhs.z);
218constexpr bool operator !=(
const Vector3& lhs,
const Vector3& rhs)
noexcept
220 return !(lhs == rhs);
223constexpr Vector3 operator +(
const Vector3& lhs,
const Vector3& rhs)
noexcept
225 return Vector3{lhs.x + rhs.x, lhs.y + rhs.y, lhs.z + rhs.z};
228constexpr Vector3 operator -(
const Vector3& lhs,
const Vector3& rhs)
noexcept
230 return Vector3{lhs.x - rhs.x, lhs.y - rhs.y, lhs.z - rhs.z};
233constexpr Vector3 operator *(
const Vector3& vec,
float scalar)
noexcept
235 return Vector3{vec.x * scalar, vec.y * scalar, vec.z * scalar};
238constexpr Vector3 operator *(
float scalar,
const Vector3& vec)
noexcept
243constexpr Vector3 operator /(
const Vector3& vec,
float scalar)
noexcept
245 return Vector3{vec.x / scalar, vec.y / scalar, vec.z / scalar};
248constexpr Vector3 operator-(
const Vector3& vec)
noexcept
253constexpr Vector3& operator +=(Vector3& lhs,
const Vector3& rhs)
noexcept
259constexpr Vector3& operator -=(Vector3& lhs,
const Vector3& rhs)
noexcept
265constexpr Vector3& operator *=(Vector3& lhs,
float rhs)
noexcept
271constexpr Vector3& operator /=(Vector3& lhs,
float rhs)
noexcept
277constexpr float Dot(
const Vector3& lhs,
const Vector3& rhs)
noexcept
279 return lhs.x * rhs.x + lhs.y * rhs.y + lhs.z * rhs.z;
282constexpr Vector3 CrossProduct(
const Vector3& lhs,
const Vector3& rhs)
noexcept
286 lhs.y * rhs.z - lhs.z * rhs.y,
287 lhs.z * rhs.x - lhs.x * rhs.z,
288 lhs.x * rhs.y - lhs.y * rhs.x
292constexpr Vector3 TripleCrossProduct(
const Vector3& a,
const Vector3& b,
const Vector3& c)
noexcept
294 return (b * Dot(c, a)) - (a * Dot(c, b));
297constexpr Vector3 HadamardProduct(
const Vector3& lhs,
const Vector3& rhs)
noexcept
299 return Vector3{lhs.x * rhs.x, lhs.y * rhs.y, lhs.z * rhs.z};
302constexpr float SquareMagnitude(
const Vector3& vec)
noexcept
304 return Dot(vec, vec);
307float Magnitude(
const Vector3& vec)
noexcept
309 return sqrtf(SquareMagnitude(vec));
312Vector3
Normalize(
const Vector3& vec)
noexcept
314 return vec == Vector3::Zero() ? Vector3::Zero() : vec / Magnitude(vec);
317constexpr float SquareDistance(
const Vector3& lhs,
const Vector3& rhs)
noexcept
319 return SquareMagnitude(lhs - rhs);
322float Distance(
const Vector3& lhs,
const Vector3& rhs)
noexcept
324 return Magnitude(lhs - rhs);
327constexpr bool HasAnyZeroElement(
const Vector3& vec)
noexcept
329 return FloatEqual(vec.x, 0.0f) || FloatEqual(vec.y, 0.0f) || FloatEqual(vec.z, 0.0f);
332constexpr auto HasUniformElements(
const Vector3& vec)
noexcept ->
bool
334 return FloatEqual(vec.x, vec.y) && FloatEqual(vec.x, vec.z);
337Vector3 OrthogonalTo(
const Vector3& vec)
noexcept
339 const float invSqrt3 = 1.0f / sqrtf(3.0f);
340 return (fabs(vec.x) >= invSqrt3) ?
341 Normalize(Vector3{vec.y, -1.0f * vec.x, 0.0f}) :
342 Normalize(Vector3{0.0f, vec.z, -1.0f * vec.y});
345void OrthogonalBasis(
const Vector3& vec, Vector3* a, Vector3* b)
noexcept
347 *a = OrthogonalTo(vec);
348 *b = CrossProduct(*a, vec);
351auto ClosestOrthogonal(
const Vector3& target,
const Vector3& reference)
noexcept -> Vector3
353 constexpr auto parallelThreshold = 0.999f;
354 const auto projection = Dot(target, reference);
355 if (std::fabs(projection) < parallelThreshold)
357 return Normalize(target - reference * projection);
360 return OrthogonalTo(reference);
364constexpr bool operator ==(
const Vector4& lhs,
const Vector4& rhs)
noexcept
366 return FloatEqual(lhs.x, rhs.x) &&
367 FloatEqual(lhs.y, rhs.y) &&
368 FloatEqual(lhs.z, rhs.z) &&
369 FloatEqual(lhs.w, rhs.w);
372constexpr bool operator !=(
const Vector4& lhs,
const Vector4& rhs)
noexcept
374 return !(lhs == rhs);
377constexpr Vector4 operator +(
const Vector4& lhs,
const Vector4& rhs)
noexcept
379 return Vector4{lhs.x + rhs.x, lhs.y + rhs.y, lhs.z + rhs.z, lhs.w + rhs.w};
382constexpr Vector4 operator -(
const Vector4& lhs,
const Vector4& rhs)
noexcept
384 return Vector4{lhs.x - rhs.x, lhs.y - rhs.y, lhs.z - rhs.z, lhs.w - rhs.w};
387constexpr Vector4 operator *(
const Vector4& vec,
float scalar)
noexcept
389 return Vector4{vec.x * scalar, vec.y * scalar, vec.z * scalar, vec.w * scalar};
392constexpr Vector4 operator *(
float scalar,
const Vector4& vec)
noexcept
397constexpr Vector4 operator /(
const Vector4& vec,
float scalar)
noexcept
399 return Vector4{vec.x / scalar, vec.y / scalar, vec.z / scalar, vec.w / scalar};
402constexpr Vector4 operator -(
const Vector4& vec)
noexcept
407constexpr float Dot(
const Vector4& lhs,
const Vector4& rhs)
noexcept
409 return lhs.x * rhs.x + lhs.y * rhs.y + lhs.z * rhs.z + lhs.w * rhs.w;
412constexpr Vector4 HadamardProduct(
const Vector4& lhs,
const Vector4& rhs)
noexcept
414 return Vector4{lhs.x * rhs.x, lhs.y * rhs.y, lhs.z * rhs.z, lhs.w * rhs.w};
417constexpr float SquareMagnitude(
const Vector4& vec)
noexcept
419 return Dot(vec, vec);
422float Magnitude(
const Vector4& vec)
noexcept
424 return sqrtf(SquareMagnitude(vec));
427Vector4
Normalize(
const Vector4& vec)
noexcept
429 return vec == Vector4::Zero() ? Vector4::Zero() : vec / Magnitude(vec);
432constexpr float SquareDistance(
const Vector4& lhs,
const Vector4& rhs)
noexcept
434 return SquareMagnitude(lhs - rhs);
437float Distance(
const Vector4& lhs,
const Vector4& rhs)
noexcept
439 return Magnitude(lhs - rhs);
442constexpr bool HasAnyZeroElement(
const Vector4& vec)
noexcept
444 return FloatEqual(vec.x, 0.0f) ||
445 FloatEqual(vec.y, 0.0f) ||
446 FloatEqual(vec.z, 0.0f) ||
447 FloatEqual(vec.w, 0.0f);
450constexpr auto HasUniformElements(
const Vector4& vec)
noexcept ->
bool
452 return FloatEqual(vec.x, vec.y) && FloatEqual(vec.x, vec.z) && FloatEqual(vec.x, vec.w);
455constexpr Vector3 ToVector3(
const Vector4& vec)
noexcept
457 return Vector3{vec.x, vec.y, vec.z};
auto Normalize(const Quaternion &quat) -> Quaternion
Return a normalized quaternion.
A two component vector.
Definition: Vector.h:13
A three component vector.
Definition: Vector.h:29
A four component vector.
Definition: Vector.h:48