SA_Maths
Sapphire Suite's C++ Maths Library
Loading...
Searching...
No Matches
Vector3.hpp
Go to the documentation of this file.
1// Copyright (c) 2023 Sapphire development team. All Rights Reserved.
2
3#pragma once
4
5#ifndef SAPPHIRE_MATHS_VECTOR3_GUARD
6#define SAPPHIRE_MATHS_VECTOR3_GUARD
7
8#include <cstdint>
9
10#include <SA/Maths/Debug.hpp>
11
17
28namespace SA
29{
30 template <typename T>
31 struct Vec2;
32
33 template <typename T>
34 struct Vec4;
35
41 template <typename T>
42 struct Vec3
43 {
45 using Type = T;
46
48 T x = T();
49
51 T y = T();
52
54 T z = T();
55
56//{ Constants
57
59 static const Vec3 Zero;
60
62 static const Vec3 One;
63
65 static const Vec3 Right;
66
68 static const Vec3 Left;
69
71 static const Vec3 Up;
72
74 static const Vec3 Down;
75
77 static const Vec3 Forward;
78
80 static const Vec3 Backward;
81
82//}
83
84//{ Constructors
85
87 Vec3() = default;
88
89
97 constexpr Vec3(T _x, T _y, T _z) noexcept;
98
104 constexpr Vec3(T _scale) noexcept;
105
106
108 Vec3(Vec3&&) = default;
109
111 Vec3(const Vec3&) = default;
112
120 template <typename TIn>
121 constexpr Vec3(const Vec3<TIn>& _other) noexcept;
122
131 template <typename TIn>
132 constexpr Vec3(const Vec2<TIn>& _other, T _z = T(0)) noexcept;
133
141 template <typename TIn>
142 constexpr Vec3(const Vec4<TIn>& _other) noexcept;
143
144//}
145
146//{ Equals
147
153 constexpr bool IsZero() const noexcept;
154
163 constexpr bool Equals(const Vec3& _other, T _epsilon = std::numeric_limits<T>::epsilon()) const noexcept;
164
165
173 constexpr bool operator==(const Vec3 & _rhs) const noexcept;
174
182 constexpr bool operator!=(const Vec3 & _rhs) const noexcept;
183
184//}
185
186//{ Accessors
187
193 T* Data() noexcept;
194
200 const T* Data() const noexcept;
201
202
210 T& operator[](uint32_t _index);
211
219 const T& operator[](uint32_t _index) const;
220
221//}
222
223//{ Length
224
230 constexpr T Length() const;
231
237 constexpr T SqrLength() const noexcept;
238
245
252
258 constexpr bool IsNormalized() const noexcept;
259
260//}
261
262//{ Project
263
272 Vec3 Reflect(const Vec3& _normal, float _elasticity = 1.0f) const noexcept;
273
283 Vec3 ProjectOnTo(const Vec3& _other) const noexcept;
284
296 Vec3 ProjectOnToNormal(const Vec3& _normal) const noexcept;
297
298//}
299
300//{ Dot/Cross
301
310 static constexpr T Dot(const Vec3& _lhs, const Vec3& _rhs) noexcept;
311
320 static constexpr Vec3<T> Cross(const Vec3& _lhs, const Vec3& _rhs) noexcept;
321
322//}
323
324//{ Angle
325
335 static Deg<T> Angle(const Vec3& _start, const Vec3& _end, const Vec3& _normal = Vec3::Up) noexcept;
336
345 static Deg<T> AngleUnsigned(const Vec3& _start, const Vec3& _end) noexcept;
346
347//}
348
349//{ Dist/Dir
350
359 static constexpr T Dist(const Vec3& _start, const Vec3& _end);
360
369 static constexpr T SqrDist(const Vec3& _start, const Vec3& _end) noexcept;
370
381 static constexpr Vec3 Dir(const Vec3& _start, const Vec3& _end) noexcept;
382
391 static constexpr Vec3 DirN(const Vec3& _start, const Vec3& _end);
392
393//}
394
395//{ Lerp
396
408 static Vec3 Lerp(const Vec3& _start, const Vec3& _end, float _alpha) noexcept;
409
421 static Vec3 LerpUnclamped(const Vec3& _start, const Vec3& _end, float _alpha) noexcept;
422
434 static Vec3 SLerp(const Vec3& _start, const Vec3& _end, float _alpha) noexcept;
435
447 static Vec3 SLerpUnclamped(const Vec3& _start, const Vec3& _end, float _alpha) noexcept;
448
449//}
450
451//{ Operators
452
458 Vec3& operator=(Vec3&&) = default;
459
465 Vec3& operator=(const Vec3&) = default;
466
467
473 constexpr Vec3 operator-() const noexcept;
474
482 Vec3 operator*(T _scale) const noexcept;
483
491 Vec3 operator/(T _scale) const;
492
500 constexpr Vec3 operator+(const Vec3& _rhs) const noexcept;
501
509 constexpr Vec3 operator-(const Vec3& _rhs) const noexcept;
510
518 constexpr Vec3 operator*(const Vec3& _rhs) const noexcept;
519
527 Vec3 operator/(const Vec3& _rhs) const;
528
536 constexpr T operator|(const Vec3& _rhs) const noexcept;
537
545 constexpr Vec3 operator^(const Vec3& _rhs) const noexcept;
546
547
555 Vec3& operator*=(T _scale) noexcept;
556
564 Vec3& operator/=(T _scale);
565
573 Vec3& operator+=(const Vec3& _rhs) noexcept;
574
582 Vec3& operator-=(const Vec3& _rhs) noexcept;
583
591 Vec3 operator*=(const Vec3& _rhs) noexcept;
592
600 Vec3 operator/=(const Vec3& _rhs);
601
602//}
603 };
604
605
614 template <typename T>
615 constexpr Vec3<T> operator*(typename std::remove_cv<T>::type _lhs, const Vec3<T>& _rhs) noexcept;
616
625 template <typename T>
626 Vec3<T> operator/(typename std::remove_cv<T>::type _lhs, const Vec3<T>& _rhs);
627
628
629#if SA_LOGGER_IMPL
630
642 template <typename T>
643 std::string ToString(const Vec3<T>& _v);
644
645#endif
646
647
648//{ Aliases
649
652
655
658
661
662
664 template <typename T>
666
669
672
675
678
679//}
680}
681
690#include <SA/Maths/Space/Vector3.inl>
691
692#endif // GUARD
Cosinus method implementation.
Maths module Debug compatibility definition.
Degree type implementation.
Equals method implementation.
Lerp algorithms implementation.
Square Root algorithm implementation.
Maths Degree type.
Definition Degree.hpp:37
Vector 2 Sapphire's class.
Definition Vector2.hpp:41
Vector 3 Sapphire-Maths class.
Definition Vector3.hpp:43
static const Vec3 Forward
Down vector constant {0, 0, 1}.
Definition Vector3.hpp:77
Vec3 Reflect(const Vec3 &_normal, float _elasticity=1.0f) const noexcept
Reflect this vector over the normal.
static const Vec3 Down
Down vector constant {0, -1, 0}.
Definition Vector3.hpp:74
static const Vec3 Zero
Zero vector constant {0, 0, 0}.
Definition Vector3.hpp:59
static Vec3 LerpUnclamped(const Vec3 &_start, const Vec3 &_end, float _alpha) noexcept
Unclamped Lerp from _start to _end at _alpha.
static constexpr Vec3 DirN(const Vec3 &_start, const Vec3 &_end)
Compute the Normalized Direction from _start to _end.
constexpr Vec3(T _scale) noexcept
Scale Value constructor.
Vec3()=default
Default constructor.
T z
Vector's Z component (axis value).
Definition Vector3.hpp:54
constexpr T Length() const
Getter of the length of this vector.
static const Vec3 Right
Right vector constant {1, 0, 0}.
Definition Vector3.hpp:65
static Vec3 SLerp(const Vec3 &_start, const Vec3 &_end, float _alpha) noexcept
Clamped SLerp from _start to _end at _alpha.
Vec3 & Normalize()
Normalize this vector.
Vec3 GetNormalized() const
Normalize this vector.
constexpr T SqrLength() const noexcept
Getter of the Squared Length of this vector.
Vec3 ProjectOnToNormal(const Vec3 &_normal) const noexcept
Project this vector onto s normal.
T Type
Type of the Vector.
Definition Vector3.hpp:45
constexpr bool IsZero() const noexcept
Whether this vector is a zero vector.
static Vec3 SLerpUnclamped(const Vec3 &_start, const Vec3 &_end, float _alpha) noexcept
Unclamped SLerp from _start to _end at _alpha.
constexpr bool Equals(const Vec3 &_other, T _epsilon=std::numeric_limits< T >::epsilon()) const noexcept
Compare 2 vector.
static constexpr T SqrDist(const Vec3 &_start, const Vec3 &_end) noexcept
Compute the Squared Distance between _lhs and _rhs.
constexpr Vec3(const Vec3< TIn > &_other) noexcept
Value constructor from another Vec3 type.
Vec3(Vec3 &&)=default
Default move constructor.
constexpr Vec3(const Vec2< TIn > &_other, T _z=T(0)) noexcept
Value constructor from Vec2.
static constexpr T Dist(const Vec3 &_start, const Vec3 &_end)
Compute the Distance between _lhs and _rhs.
constexpr bool IsNormalized() const noexcept
Whether this vector is normalized.
static const Vec3 One
One vector constant {1, 1, 1}.
Definition Vector3.hpp:62
static constexpr Vec3< T > Cross(const Vec3 &_lhs, const Vec3 &_rhs) noexcept
Compute the Cross product between _lhs and _rhs.
Vec3(const Vec3 &)=default
Default copy constructor.
T * Data() noexcept
Getter of vector data
static constexpr T Dot(const Vec3 &_lhs, const Vec3 &_rhs) noexcept
Compute the Dot product between _lhs and _rhs.
T y
Vector's Y component (axis value).
Definition Vector3.hpp:51
static Deg< T > AngleUnsigned(const Vec3 &_start, const Vec3 &_end) noexcept
Compute the Unsigned Angle between _lhs and _rhs.
static constexpr Vec3 Dir(const Vec3 &_start, const Vec3 &_end) noexcept
Compute the Non-Normalized Direction from _lhs and _rhs.
static const Vec3 Up
Up vector constant {0, 1, 0}.
Definition Vector3.hpp:71
T x
Vector's X component (axis value).
Definition Vector3.hpp:48
Vec3 ProjectOnTo(const Vec3 &_other) const noexcept
Project this vector onto an other vector.
constexpr Vec3(T _x, T _y, T _z) noexcept
Value constructor.
static const Vec3 Backward
Down vector constant {0, 0, -1}.
Definition Vector3.hpp:80
static const Vec3 Left
Left vector constant {-1, 0, 0}.
Definition Vector3.hpp:68
static Deg< T > Angle(const Vec3 &_start, const Vec3 &_end, const Vec3 &_normal=Vec3::Up) noexcept
Compute the Signed Angle between _lhs and _rhs.
static Vec3 Lerp(const Vec3 &_start, const Vec3 &_end, float _alpha) noexcept
Clamped Lerp from _start to _end at _alpha.
Vector 4 Sapphire-Maths class.
Definition Vector4.hpp:41