LiteFX 0.4.1.2025
Computer Graphics Engine
Loading...
Searching...
No Matches
math.hpp
1#pragma once
2
3#include <litefx/core.h>
4
5#if !defined (LITEFX_MATH_API)
6# if defined(LiteFX_Math_EXPORTS) && (defined _WIN32 || defined WINCE)
7# define LITEFX_MATH_API __declspec(dllexport)
8# elif (defined(LiteFX_Math_EXPORTS) || defined(__APPLE__)) && defined __GNUC__ && __GNUC__ >= 4
9# define LITEFX_MATH_API __attribute__ ((visibility ("default")))
10# elif !defined(LiteFX_Math_EXPORTS) && (defined _WIN32 || defined WINCE)
11# define LITEFX_MATH_API __declspec(dllimport)
12# endif
13#endif
14
15#ifndef LITEFX_MATH_API
16# define LITEFX_MATH_API
17#endif
18
19#if defined(LITEFX_BUILD_WITH_GLM)
20#include <glm/glm.hpp>
21#endif
22
23#if defined(LITEFX_BUILD_WITH_DIRECTX_MATH)
24#include <DirectXMath.h>
25#endif
26
27#include <litefx/vector.hpp>
28#include <litefx/matrix.hpp>
29
30namespace LiteFX::Math {
31 using namespace LiteFX;
32
36 using Byte = uint8_t;
37
41 using Int16 = int16_t;
42
46 using UInt16 = uint16_t;
47
51 using Int32 = int32_t;
52
56 using UInt32 = uint32_t;
57
61 using Int64 = int64_t;
62
66 using UInt64 = uint64_t;
67
71 using Float = float_t;
72
76 using Double = double_t;
77
83 constexpr Byte operator "" _b(unsigned long long int arg) noexcept {
84 return static_cast<Byte>(arg);
85 }
86
92 constexpr Int16 operator "" _i16(unsigned long long int arg) noexcept {
93 return static_cast<Int16>(arg);
94 }
95
101 constexpr UInt16 operator "" _ui16(unsigned long long int arg) noexcept {
102 return static_cast<UInt16>(arg);
103 }
104
110 constexpr Int32 operator "" _i32(unsigned long long int arg) noexcept {
111 return static_cast<Int32>(arg);
112 }
113
119 constexpr UInt32 operator "" _ui32(unsigned long long int arg) noexcept {
120 return static_cast<UInt32>(arg);
121 }
122
128 constexpr Int64 operator "" _i64(unsigned long long int arg) noexcept {
129 return static_cast<Int64>(arg);
130 }
131
137 constexpr UInt64 operator "" _ui64(unsigned long long int arg) noexcept {
138 return static_cast<UInt64>(arg);
139 }
140
146 constexpr Float operator "" _f32(long double arg) noexcept {
147 return static_cast<Float>(arg);
148 }
149
155 constexpr Double operator "" _f64(long double arg) noexcept {
156 return static_cast<Double>(arg);
157 }
158
166 template <typename T = UInt32>
167 static constexpr T align(T size, T alignment) {
168 return (size + alignment - 1) & ~(alignment - 1);
169 }
170
171#pragma region Vector
175 class LITEFX_MATH_API Vector1f : public Vector<Float, 1> {
176 public:
177 using Vector::Vector;
178
179#if defined(LITEFX_BUILD_WITH_GLM)
180 public:
185 Vector1f(const glm::f32vec1& v) noexcept;
186
191 Vector1f(glm::f32vec1&& v) noexcept;
192
196 operator glm::f32vec1() const noexcept;
197#endif
198
199#if defined(LITEFX_BUILD_WITH_DIRECTX_MATH)
200 public:
205 Vector1f(const DirectX::XMVECTOR& v) noexcept;
206
211 Vector1f(DirectX::XMVECTOR&& v) noexcept;
212
216 operator DirectX::XMVECTOR() const noexcept;
217#endif
218 };
219
220 // NOTE: Vector1i unsupported, due to lack of `XMStoreSInt` overload for scalar signed integers.
221
225 class LITEFX_MATH_API Vector1u : public Vector<UInt32, 1> {
226 public:
227 using Vector::Vector;
228
229#if defined(LITEFX_BUILD_WITH_GLM)
230 public:
235 Vector1u(const glm::u32vec1& v) noexcept;
236
241 Vector1u(glm::u32vec1&& v) noexcept;
242
246 operator glm::u32vec1() const noexcept;
247#endif
248
249#if defined(LITEFX_BUILD_WITH_DIRECTX_MATH)
250 public:
255 Vector1u(const DirectX::XMVECTOR& v) noexcept;
256
261 Vector1u(DirectX::XMVECTOR&& v) noexcept;
262
266 operator DirectX::XMVECTOR() const noexcept;
267#endif
268 };
269
273 class LITEFX_MATH_API Vector2f : public Vector<Float, 2> {
274 public:
275 using Vector::Vector;
276
277#if defined(LITEFX_BUILD_WITH_GLM)
278 public:
283 Vector2f(const glm::f32vec2& v) noexcept;
284
289 Vector2f(glm::f32vec2&& v) noexcept;
290
294 operator glm::f32vec2() const noexcept;
295#endif
296
297#if defined(LITEFX_BUILD_WITH_DIRECTX_MATH)
298 public:
303 Vector2f(const DirectX::XMVECTOR& v) noexcept;
304
309 Vector2f(DirectX::XMVECTOR&& v) noexcept;
310
315 Vector2f(const DirectX::XMFLOAT2& v) noexcept;
316
321 Vector2f(DirectX::XMFLOAT2&& v) noexcept;
322
326 operator DirectX::XMVECTOR() const noexcept;
327
331 operator DirectX::XMFLOAT2() const noexcept;
332#endif
333 };
334
338 class LITEFX_MATH_API Vector2u : public Vector<UInt32, 2> {
339 public:
340 using Vector::Vector;
341
342#if defined(LITEFX_BUILD_WITH_GLM)
343 public:
348 Vector2u(const glm::u32vec2& v) noexcept;
349
354 Vector2u(glm::u32vec2&& v) noexcept;
355
359 operator glm::u32vec2() const noexcept;
360#endif
361
362#if defined(LITEFX_BUILD_WITH_DIRECTX_MATH)
363 public:
368 Vector2u(const DirectX::XMVECTOR& v) noexcept;
369
374 Vector2u(DirectX::XMVECTOR&& v) noexcept;
375
380 Vector2u(const DirectX::XMUINT2& v) noexcept;
381
386 Vector2u(DirectX::XMUINT2&& v) noexcept;
387
391 operator DirectX::XMVECTOR() const noexcept;
392
396 operator DirectX::XMUINT2() const noexcept;
397#endif
398 };
399
403 class LITEFX_MATH_API Vector2i : public Vector<Int32, 2> {
404 public:
405 using Vector::Vector;
406
407#if defined(LITEFX_BUILD_WITH_GLM)
408 public:
413 Vector2i(const glm::i32vec2& v) noexcept;
414
419 Vector2i(glm::i32vec2&& v) noexcept;
420
424 operator glm::i32vec2() const noexcept;
425#endif
426
427#if defined(LITEFX_BUILD_WITH_DIRECTX_MATH)
428 public:
433 Vector2i(const DirectX::XMVECTOR& v) noexcept;
434
439 Vector2i(DirectX::XMVECTOR&& v) noexcept;
440
445 Vector2i(const DirectX::XMINT2& v) noexcept;
446
451 Vector2i(DirectX::XMINT2&& v) noexcept;
452
456 operator DirectX::XMVECTOR() const noexcept;
457
461 operator DirectX::XMINT2() const noexcept;
462#endif
463 };
464
468 class LITEFX_MATH_API Vector3f : public Vector<Float, 3> {
469 public:
470 using Vector::Vector;
471
472#if defined(LITEFX_BUILD_WITH_GLM)
473 public:
474 Vector3f(const glm::f32vec3& v) noexcept;
475 Vector3f(glm::f32vec3&& v) noexcept;
476 operator glm::f32vec3() const noexcept;
477#endif
478
479#if defined(LITEFX_BUILD_WITH_DIRECTX_MATH)
480 public:
481 Vector3f(const DirectX::XMVECTOR& v) noexcept;
482 Vector3f(DirectX::XMVECTOR&& v) noexcept;
483 Vector3f(const DirectX::XMFLOAT3& v) noexcept;
484 Vector3f(DirectX::XMFLOAT3&& v) noexcept;
485 operator DirectX::XMVECTOR() const noexcept;
486 operator DirectX::XMFLOAT3() const noexcept;
487#endif
488 };
489
490 class LITEFX_MATH_API Vector3u : public Vector<UInt32, 3> {
491 public:
492 using Vector::Vector;
493
494#if defined(LITEFX_BUILD_WITH_GLM)
495 public:
496 Vector3u(const glm::u32vec3& v) noexcept;
497 Vector3u(glm::u32vec3&& v) noexcept;
498 operator glm::u32vec3() const noexcept;
499#endif
500
501#if defined(LITEFX_BUILD_WITH_DIRECTX_MATH)
502 public:
503 Vector3u(const DirectX::XMVECTOR& v) noexcept;
504 Vector3u(DirectX::XMVECTOR&& v) noexcept;
505 Vector3u(const DirectX::XMUINT3& v) noexcept;
506 Vector3u(DirectX::XMUINT3&& v) noexcept;
507 operator DirectX::XMVECTOR() const noexcept;
508 operator DirectX::XMUINT3() const noexcept;
509#endif
510 };
511
512 class LITEFX_MATH_API Vector3i : public Vector<Int32, 3> {
513 public:
514 using Vector::Vector;
515
516#if defined(LITEFX_BUILD_WITH_GLM)
517 public:
518 Vector3i(const glm::i32vec3& v) noexcept;
519 Vector3i(glm::i32vec3&& v) noexcept;
520 operator glm::i32vec3() const noexcept;
521#endif
522
523#if defined(LITEFX_BUILD_WITH_DIRECTX_MATH)
524 public:
525 Vector3i(const DirectX::XMVECTOR& v) noexcept;
526 Vector3i(DirectX::XMVECTOR&& v) noexcept;
527 Vector3i(const DirectX::XMINT3& v) noexcept;
528 Vector3i(DirectX::XMINT3&& v) noexcept;
529 operator DirectX::XMVECTOR() const noexcept;
530 operator DirectX::XMINT3() const noexcept;
531#endif
532 };
533
534 class LITEFX_MATH_API Vector4f : public Vector<Float, 4> {
535 public:
536 using Vector::Vector;
537
538#if defined(LITEFX_BUILD_WITH_GLM)
539 public:
540 Vector4f(const glm::f32vec4& v) noexcept;
541 Vector4f(glm::f32vec4&& v) noexcept;
542 operator glm::f32vec4() const noexcept;
543#endif
544
545#if defined(LITEFX_BUILD_WITH_DIRECTX_MATH)
546 public:
547 Vector4f(const DirectX::XMVECTOR& v) noexcept;
548 Vector4f(DirectX::XMVECTOR&& v) noexcept;
549 Vector4f(const DirectX::XMFLOAT4& v) noexcept;
550 Vector4f(DirectX::XMFLOAT4&& v) noexcept;
551 operator DirectX::XMVECTOR() const noexcept;
552 operator DirectX::XMFLOAT4() const noexcept;
553#endif
554 };
555
556 class LITEFX_MATH_API Vector4u : public Vector<UInt32, 4> {
557 public:
558 using Vector::Vector;
559
560#if defined(LITEFX_BUILD_WITH_GLM)
561 public:
562 Vector4u(const glm::u32vec4& v) noexcept;
563 Vector4u(glm::u32vec4&& v) noexcept;
564 operator glm::u32vec4() const noexcept;
565#endif
566
567#if defined(LITEFX_BUILD_WITH_DIRECTX_MATH)
568 public:
569 Vector4u(const DirectX::XMVECTOR& v) noexcept;
570 Vector4u(DirectX::XMVECTOR&& v) noexcept;
571 Vector4u(const DirectX::XMUINT4& v) noexcept;
572 Vector4u(DirectX::XMUINT4&& v) noexcept;
573 operator DirectX::XMVECTOR() const noexcept;
574 operator DirectX::XMUINT4() const noexcept;
575#endif
576 };
577
578 class LITEFX_MATH_API Vector4i : public Vector<Int32, 4> {
579 public:
580 using Vector::Vector;
581
582#if defined(LITEFX_BUILD_WITH_GLM)
583 public:
584 Vector4i(const glm::i32vec4& v) noexcept;
585 Vector4i(glm::i32vec4&& v) noexcept;
586 operator glm::i32vec4() const noexcept;
587#endif
588
589#if defined(LITEFX_BUILD_WITH_DIRECTX_MATH)
590 public:
591 Vector4i(const DirectX::XMVECTOR& v) noexcept;
592 Vector4i(DirectX::XMVECTOR&& v) noexcept;
593 Vector4i(const DirectX::XMINT4& v) noexcept;
594 Vector4i(DirectX::XMINT4&& v) noexcept;
595 operator DirectX::XMVECTOR() const noexcept;
596 operator DirectX::XMINT4() const noexcept;
597#endif
598 };
599
603 namespace Vectors {
608
613
618
623
628
633
638
643
648
653
658
663
668
673
678
683
688
693
698
703
708
713
718
723
728
733
738
743
748
753
758
763
768
773
778
783 }
784#pragma endregion
785
786#pragma region Size
787 class LITEFX_MATH_API Size4d : public Vector<size_t, 4> {
788 public:
789 Size4d() noexcept;
790 Size4d(size_t v) noexcept;
791 Size4d(size_t w, size_t h, size_t d, size_t a) noexcept;
792 Size4d(const Size4d&);
793 Size4d(Size4d&&) noexcept;
794 ~Size4d() noexcept = default;
795
796 public:
797 Size4d& operator=(const Size4d& _other);
798 Size4d& operator=(Size4d&& _other) noexcept;
799 Size4d operator/(size_t s) noexcept;
800 Size4d& operator/=(size_t s) noexcept;
801 Size4d operator*(size_t s) noexcept;
802 Size4d& operator*=(size_t s) noexcept;
803 Size4d operator+(const Size4d& s) noexcept;
804 Size4d& operator+=(const Size4d& s) noexcept;
805 Size4d operator-(const Size4d& s) noexcept;
806 Size4d& operator-=(const Size4d& s) noexcept;
807
808 public:
809 size_t width() const noexcept;
810 size_t& width() noexcept;
811 size_t height() const noexcept;
812 size_t& height() noexcept;
813 size_t depth() const noexcept;
814 size_t& depth() noexcept;
815 size_t alpha() const noexcept;
816 size_t& alpha() noexcept;
817 };
818
819 class LITEFX_MATH_API Size3d : public Vector<size_t, 3> {
820 public:
821 Size3d() noexcept;
822 Size3d(size_t v) noexcept;
823 Size3d(size_t w, size_t h, size_t d) noexcept;
824 Size3d(const Size3d&);
825 Size3d(Size3d&&) noexcept;
826 ~Size3d() noexcept = default;
827
828 public:
829 Size3d& operator=(const Size3d& _other);
830 Size3d& operator=(Size3d&& _other) noexcept;
831 operator Size4d() const noexcept;
832 Size3d operator/(size_t s) noexcept;
833 Size3d& operator/=(size_t s) noexcept;
834 Size3d operator*(size_t s) noexcept;
835 Size3d& operator*=(size_t s) noexcept;
836 Size3d operator+(const Size3d& s) noexcept;
837 Size3d& operator+=(const Size3d& s) noexcept;
838 Size3d operator-(const Size3d& s) noexcept;
839 Size3d& operator-=(const Size3d& s) noexcept;
840
841 public:
842 size_t width() const noexcept;
843 size_t& width() noexcept;
844 size_t height() const noexcept;
845 size_t& height() noexcept;
846 size_t depth() const noexcept;
847 size_t& depth() noexcept;
848 };
849
850 class LITEFX_MATH_API Size2d : public Vector<size_t, 2> {
851 public:
852 Size2d() noexcept;
853 Size2d(size_t v) noexcept;
854 Size2d(size_t w, size_t h) noexcept;
855 Size2d(const Size2d&);
856 Size2d(Size2d&&) noexcept;
857 ~Size2d() noexcept = default;
858
859 public:
860 Size2d& operator=(const Size2d& _other);
861 Size2d& operator=(Size2d&& _other) noexcept;
862 operator Size3d() const noexcept;
863 operator Size4d() const noexcept;
864 Size2d operator/(size_t s) noexcept;
865 Size2d& operator/=(size_t s) noexcept;
866 Size2d operator*(size_t s) noexcept;
867 Size2d& operator*=(size_t s) noexcept;
868 Size2d operator+(const Size2d& s) noexcept;
869 Size2d& operator+=(const Size2d& s) noexcept;
870 Size2d operator-(const Size2d& s) noexcept;
871 Size2d& operator-=(const Size2d& s) noexcept;
872
873 public:
874 size_t width() const noexcept;
875 size_t& width() noexcept;
876 size_t height() const noexcept;
877 size_t& height() noexcept;
878 };
879#pragma endregion
880
881#pragma region Rectangle
882 class LITEFX_MATH_API Rect : public Vector<size_t, 4> {
883 public:
884 Rect() noexcept;
885 Rect(const Vector<size_t, 2>& pos, size_t w, size_t h) noexcept;
886 Rect(size_t x, size_t y, size_t w, size_t h) noexcept;
887 Rect(const Rect&);
888 Rect(Rect&&) noexcept;
889 ~Rect() noexcept = default;
890
891 public:
892 Rect& operator=(const Rect& _other);
893 Rect& operator=(Rect&& _other) noexcept;
894
895 public:
896 Vector<size_t, 2> position() const noexcept;
897 Size2d extent() const noexcept;
898 size_t width() const noexcept;
899 size_t& width() noexcept;
900 size_t height() const noexcept;
901 size_t& height() noexcept;
902 };
903
904 class LITEFX_MATH_API RectI : public Vector<Int32, 4> {
905 public:
906 RectI() noexcept;
907 RectI(const Vector<Int32, 2>& pos, Int32 w, Int32 h) noexcept;
908 RectI(Int32 x, Int32 y, Int32 w, Int32 h) noexcept;
909 RectI(const RectI&);
910 RectI(RectI&&) noexcept;
911 ~RectI() noexcept = default;
912
913 public:
914 RectI& operator=(const RectI& _other);
915 RectI& operator=(RectI&& _other) noexcept;
916
917 public:
918 Vector<Int32, 2> position() const noexcept;
919 Size2d extent() const noexcept;
920 Int32 width() const noexcept;
921 Int32& width() noexcept;
922 Int32 height() const noexcept;
923 Int32& height() noexcept;
924 };
925
926 class LITEFX_MATH_API RectF : public Vector<Float, 4> {
927 public:
928 RectF() noexcept;
929 RectF(const Vector<Float, 2>& pos, Float w, Float h) noexcept;
930 RectF(Float x, Float y, Float w, Float h) noexcept;
931 RectF(const RectF&);
932 RectF(RectF&&) noexcept;
933 ~RectF() noexcept = default;
934
935 public:
936 RectF& operator=(const RectF& _other);
937 RectF& operator=(RectF&& _other) noexcept;
938
939 public:
940 Vector<Float, 2> position() const noexcept;
941 Size2d extent() const noexcept;
942 Float width() const noexcept;
943 Float& width() noexcept;
944 Float height() const noexcept;
945 Float& height() noexcept;
946 };
947#pragma endregion
948}
Definition math.hpp:926
~RectF() noexcept=default
Definition math.hpp:882
~Rect() noexcept=default
Definition math.hpp:904
~RectI() noexcept=default
Definition math.hpp:850
~Size2d() noexcept=default
Definition math.hpp:819
~Size3d() noexcept=default
Definition math.hpp:787
~Size4d() noexcept=default
A vector that contains a single float.
Definition math.hpp:175
A vector that contains a single unsigned integer.
Definition math.hpp:225
A vector that contains two floats.
Definition math.hpp:273
A vector that contains two signed integers.
Definition math.hpp:403
A vector that contains two unsigned integers.
Definition math.hpp:338
A vector that stores three floats.
Definition math.hpp:468
Definition math.hpp:512
Definition math.hpp:490
Definition math.hpp:534
Definition math.hpp:578
Definition math.hpp:556
Definition math.hpp:30
uint16_t UInt16
A type for an unsigned 16 bit integer.
Definition math.hpp:46
uint64_t UInt64
A type for an unsigned 64 bit integer.
Definition math.hpp:66
float_t Float
A type for a floating point value with single precision.
Definition math.hpp:71
double_t Double
A type for a floating point value with double precision.
Definition math.hpp:76
uint32_t UInt32
A type for an unsigned 32 bit integer.
Definition math.hpp:56
uint8_t Byte
A type for an unsigned 8 bit integer.
Definition math.hpp:36
int64_t Int64
A type for a signed 64 bit integer.
Definition math.hpp:61
int16_t Int16
A type for a signed 16 bit integer.
Definition math.hpp:41
int32_t Int32
A type for a signed 32 bit integer.
Definition math.hpp:51
Definition app.hpp:6
An algebraic vector type.
Definition vector.hpp:23