LiteFX 0.5.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 = std::byte;
37
41 using UInt8 = uint8_t;
42
46 using Int16 = int16_t;
47
51 using UInt16 = uint16_t;
52
56 using Int32 = int32_t;
57
61 using UInt32 = uint32_t;
62
66 using Int64 = int64_t;
67
71 using UInt64 = uint64_t;
72
76 using Float = float_t;
77
81 using Double = double_t;
82
88 constexpr Byte operator ""_b(unsigned long long int arg) noexcept {
89 return static_cast<Byte>(arg);
90 }
91
97 constexpr UInt8 operator ""_ui8(unsigned long long int arg) noexcept {
98 return static_cast<UInt8>(arg);
99 }
100
106 constexpr Int16 operator ""_i16(unsigned long long int arg) noexcept {
107 return static_cast<Int16>(arg);
108 }
109
115 constexpr UInt16 operator ""_ui16(unsigned long long int arg) noexcept {
116 return static_cast<UInt16>(arg);
117 }
118
124 constexpr Int32 operator ""_i32(unsigned long long int arg) noexcept {
125 return static_cast<Int32>(arg);
126 }
127
133 constexpr UInt32 operator ""_ui32(unsigned long long int arg) noexcept {
134 return static_cast<UInt32>(arg);
135 }
136
142 constexpr Int64 operator ""_i64(unsigned long long int arg) noexcept {
143 return static_cast<Int64>(arg);
144 }
145
151 constexpr UInt64 operator ""_ui64(unsigned long long int arg) noexcept {
152 return static_cast<UInt64>(arg);
153 }
154
160 constexpr Float operator ""_f32(long double arg) noexcept {
161 return static_cast<Float>(arg);
162 }
163
169 constexpr Double operator ""_f64(long double arg) noexcept {
170 return static_cast<Double>(arg);
171 }
172
180 template <typename T = UInt32>
181 static constexpr T align(T size, T alignment) {
182 return (size + alignment - 1) & ~(alignment - 1);
183 }
184
185#pragma region Vector
189 class LITEFX_MATH_API Vector1f : public Vector<Float, 1> {
190 public:
191 using Vector::Vector;
192
193#if defined(LITEFX_BUILD_WITH_GLM)
194 public:
199 Vector1f(const glm::f32vec1& v) noexcept;
200
205 Vector1f(glm::f32vec1&& v) noexcept;
206
210 operator glm::f32vec1() const noexcept;
211#endif
212
213#if defined(LITEFX_BUILD_WITH_DIRECTX_MATH)
214 public:
219 Vector1f(const DirectX::XMVECTOR& v) noexcept;
220
225 Vector1f(DirectX::XMVECTOR&& v) noexcept;
226
230 operator DirectX::XMVECTOR() const noexcept;
231#endif
232 };
233
234 // NOTE: Vector1i unsupported, due to lack of `XMStoreSInt` overload for scalar signed integers.
235
239 class LITEFX_MATH_API Vector1u : public Vector<UInt32, 1> {
240 public:
241 using Vector::Vector;
242
243#if defined(LITEFX_BUILD_WITH_GLM)
244 public:
249 Vector1u(const glm::u32vec1& v) noexcept;
250
255 Vector1u(glm::u32vec1&& v) noexcept;
256
260 operator glm::u32vec1() const noexcept;
261#endif
262
263#if defined(LITEFX_BUILD_WITH_DIRECTX_MATH)
264 public:
269 Vector1u(const DirectX::XMVECTOR& v) noexcept;
270
275 Vector1u(DirectX::XMVECTOR&& v) noexcept;
276
280 operator DirectX::XMVECTOR() const noexcept;
281#endif
282 };
283
287 class LITEFX_MATH_API Vector2f : public Vector<Float, 2> {
288 public:
289 using Vector::Vector;
290
291#if defined(LITEFX_BUILD_WITH_GLM)
292 public:
297 Vector2f(const glm::f32vec2& v) noexcept;
298
303 Vector2f(glm::f32vec2&& v) noexcept;
304
308 operator glm::f32vec2() const noexcept;
309#endif
310
311#if defined(LITEFX_BUILD_WITH_DIRECTX_MATH)
312 public:
317 Vector2f(const DirectX::XMVECTOR& v) noexcept;
318
323 Vector2f(DirectX::XMVECTOR&& v) noexcept;
324
329 Vector2f(const DirectX::XMFLOAT2& v) noexcept;
330
335 Vector2f(DirectX::XMFLOAT2&& v) noexcept;
336
340 operator DirectX::XMVECTOR() const noexcept;
341
345 operator DirectX::XMFLOAT2() const noexcept;
346#endif
347 };
348
352 class LITEFX_MATH_API Vector2u : public Vector<UInt32, 2> {
353 public:
354 using Vector::Vector;
355
356#if defined(LITEFX_BUILD_WITH_GLM)
357 public:
362 Vector2u(const glm::u32vec2& v) noexcept;
363
368 Vector2u(glm::u32vec2&& v) noexcept;
369
373 operator glm::u32vec2() const noexcept;
374#endif
375
376#if defined(LITEFX_BUILD_WITH_DIRECTX_MATH)
377 public:
382 Vector2u(const DirectX::XMVECTOR& v) noexcept;
383
388 Vector2u(DirectX::XMVECTOR&& v) noexcept;
389
394 Vector2u(const DirectX::XMUINT2& v) noexcept;
395
400 Vector2u(DirectX::XMUINT2&& v) noexcept;
401
405 operator DirectX::XMVECTOR() const noexcept;
406
410 operator DirectX::XMUINT2() const noexcept;
411#endif
412 };
413
417 class LITEFX_MATH_API Vector2i : public Vector<Int32, 2> {
418 public:
419 using Vector::Vector;
420
421#if defined(LITEFX_BUILD_WITH_GLM)
422 public:
427 Vector2i(const glm::i32vec2& v) noexcept;
428
433 Vector2i(glm::i32vec2&& v) noexcept;
434
438 operator glm::i32vec2() const noexcept;
439#endif
440
441#if defined(LITEFX_BUILD_WITH_DIRECTX_MATH)
442 public:
447 Vector2i(const DirectX::XMVECTOR& v) noexcept;
448
453 Vector2i(DirectX::XMVECTOR&& v) noexcept;
454
459 Vector2i(const DirectX::XMINT2& v) noexcept;
460
465 Vector2i(DirectX::XMINT2&& v) noexcept;
466
470 operator DirectX::XMVECTOR() const noexcept;
471
475 operator DirectX::XMINT2() const noexcept;
476#endif
477 };
478
482 class LITEFX_MATH_API Vector3f : public Vector<Float, 3> {
483 public:
484 using Vector::Vector;
485
486#if defined(LITEFX_BUILD_WITH_GLM)
487 public:
488 Vector3f(const glm::f32vec3& v) noexcept;
489 Vector3f(glm::f32vec3&& v) noexcept;
490 operator glm::f32vec3() const noexcept;
491#endif
492
493#if defined(LITEFX_BUILD_WITH_DIRECTX_MATH)
494 public:
495 Vector3f(const DirectX::XMVECTOR& v) noexcept;
496 Vector3f(DirectX::XMVECTOR&& v) noexcept;
497 Vector3f(const DirectX::XMFLOAT3& v) noexcept;
498 Vector3f(DirectX::XMFLOAT3&& v) noexcept;
499 operator DirectX::XMVECTOR() const noexcept;
500 operator DirectX::XMFLOAT3() const noexcept;
501#endif
502 };
503
504 class LITEFX_MATH_API Vector3u : public Vector<UInt32, 3> {
505 public:
506 using Vector::Vector;
507
508#if defined(LITEFX_BUILD_WITH_GLM)
509 public:
510 Vector3u(const glm::u32vec3& v) noexcept;
511 Vector3u(glm::u32vec3&& v) noexcept;
512 operator glm::u32vec3() const noexcept;
513#endif
514
515#if defined(LITEFX_BUILD_WITH_DIRECTX_MATH)
516 public:
517 Vector3u(const DirectX::XMVECTOR& v) noexcept;
518 Vector3u(DirectX::XMVECTOR&& v) noexcept;
519 Vector3u(const DirectX::XMUINT3& v) noexcept;
520 Vector3u(DirectX::XMUINT3&& v) noexcept;
521 operator DirectX::XMVECTOR() const noexcept;
522 operator DirectX::XMUINT3() const noexcept;
523#endif
524 };
525
526 class LITEFX_MATH_API Vector3i : public Vector<Int32, 3> {
527 public:
528 using Vector::Vector;
529
530#if defined(LITEFX_BUILD_WITH_GLM)
531 public:
532 Vector3i(const glm::i32vec3& v) noexcept;
533 Vector3i(glm::i32vec3&& v) noexcept;
534 operator glm::i32vec3() const noexcept;
535#endif
536
537#if defined(LITEFX_BUILD_WITH_DIRECTX_MATH)
538 public:
539 Vector3i(const DirectX::XMVECTOR& v) noexcept;
540 Vector3i(DirectX::XMVECTOR&& v) noexcept;
541 Vector3i(const DirectX::XMINT3& v) noexcept;
542 Vector3i(DirectX::XMINT3&& v) noexcept;
543 operator DirectX::XMVECTOR() const noexcept;
544 operator DirectX::XMINT3() const noexcept;
545#endif
546 };
547
548 class LITEFX_MATH_API Vector4f : public Vector<Float, 4> {
549 public:
550 using Vector::Vector;
551
552#if defined(LITEFX_BUILD_WITH_GLM)
553 public:
554 Vector4f(const glm::f32vec4& v) noexcept;
555 Vector4f(glm::f32vec4&& v) noexcept;
556 operator glm::f32vec4() const noexcept;
557#endif
558
559#if defined(LITEFX_BUILD_WITH_DIRECTX_MATH)
560 public:
561 Vector4f(const DirectX::XMVECTOR& v) noexcept;
562 Vector4f(DirectX::XMVECTOR&& v) noexcept;
563 Vector4f(const DirectX::XMFLOAT4& v) noexcept;
564 Vector4f(DirectX::XMFLOAT4&& v) noexcept;
565 operator DirectX::XMVECTOR() const noexcept;
566 operator DirectX::XMFLOAT4() const noexcept;
567#endif
568 };
569
570 class LITEFX_MATH_API Vector4u : public Vector<UInt32, 4> {
571 public:
572 using Vector::Vector;
573
574#if defined(LITEFX_BUILD_WITH_GLM)
575 public:
576 Vector4u(const glm::u32vec4& v) noexcept;
577 Vector4u(glm::u32vec4&& v) noexcept;
578 operator glm::u32vec4() const noexcept;
579#endif
580
581#if defined(LITEFX_BUILD_WITH_DIRECTX_MATH)
582 public:
583 Vector4u(const DirectX::XMVECTOR& v) noexcept;
584 Vector4u(DirectX::XMVECTOR&& v) noexcept;
585 Vector4u(const DirectX::XMUINT4& v) noexcept;
586 Vector4u(DirectX::XMUINT4&& v) noexcept;
587 operator DirectX::XMVECTOR() const noexcept;
588 operator DirectX::XMUINT4() const noexcept;
589#endif
590 };
591
592 class LITEFX_MATH_API Vector4i : public Vector<Int32, 4> {
593 public:
594 using Vector::Vector;
595
596#if defined(LITEFX_BUILD_WITH_GLM)
597 public:
598 Vector4i(const glm::i32vec4& v) noexcept;
599 Vector4i(glm::i32vec4&& v) noexcept;
600 operator glm::i32vec4() const noexcept;
601#endif
602
603#if defined(LITEFX_BUILD_WITH_DIRECTX_MATH)
604 public:
605 Vector4i(const DirectX::XMVECTOR& v) noexcept;
606 Vector4i(DirectX::XMVECTOR&& v) noexcept;
607 Vector4i(const DirectX::XMINT4& v) noexcept;
608 Vector4i(DirectX::XMINT4&& v) noexcept;
609 operator DirectX::XMVECTOR() const noexcept;
610 operator DirectX::XMINT4() const noexcept;
611#endif
612 };
613
617 namespace Vectors {
622
627
632
637
642
647
652
657
662
667
672
677
682
687
692
697
702
707
712
717
722
727
732
737
742
747
752
757
762
767
772
777
782
787
792
797 }
798#pragma endregion
799
800#pragma region Size
801 class LITEFX_MATH_API Size4d : public Vector<size_t, 4> {
802 public:
803 Size4d() noexcept;
804 Size4d(size_t v) noexcept;
805 Size4d(size_t w, size_t h, size_t d, size_t a) noexcept;
806 Size4d(const Size4d&);
807 Size4d(Size4d&&) noexcept;
808 ~Size4d() noexcept = default;
809
810 public:
811 Size4d& operator=(const Size4d& _other);
812 Size4d& operator=(Size4d&& _other) noexcept;
813 Size4d operator/(size_t s) noexcept;
814 Size4d& operator/=(size_t s) noexcept;
815 Size4d operator*(size_t s) noexcept;
816 Size4d& operator*=(size_t s) noexcept;
817 Size4d operator+(const Size4d& s) noexcept;
818 Size4d& operator+=(const Size4d& s) noexcept;
819 Size4d operator-(const Size4d& s) noexcept;
820 Size4d& operator-=(const Size4d& s) noexcept;
821
822 public:
823 size_t width() const noexcept;
824 size_t& width() noexcept;
825 size_t height() const noexcept;
826 size_t& height() noexcept;
827 size_t depth() const noexcept;
828 size_t& depth() noexcept;
829 size_t alpha() const noexcept;
830 size_t& alpha() noexcept;
831 };
832
833 class LITEFX_MATH_API Size3d : public Vector<size_t, 3> {
834 public:
835 Size3d() noexcept;
836 Size3d(size_t v) noexcept;
837 Size3d(size_t w, size_t h, size_t d) noexcept;
838 Size3d(const Size3d&);
839 Size3d(Size3d&&) noexcept;
840 ~Size3d() noexcept = default;
841
842 public:
843 Size3d& operator=(const Size3d& _other);
844 Size3d& operator=(Size3d&& _other) noexcept;
845 operator Size4d() const noexcept;
846 Size3d operator/(size_t s) noexcept;
847 Size3d& operator/=(size_t s) noexcept;
848 Size3d operator*(size_t s) noexcept;
849 Size3d& operator*=(size_t s) noexcept;
850 Size3d operator+(const Size3d& s) noexcept;
851 Size3d& operator+=(const Size3d& s) noexcept;
852 Size3d operator-(const Size3d& s) noexcept;
853 Size3d& operator-=(const Size3d& s) noexcept;
854
855 public:
856 size_t width() const noexcept;
857 size_t& width() noexcept;
858 size_t height() const noexcept;
859 size_t& height() noexcept;
860 size_t depth() const noexcept;
861 size_t& depth() noexcept;
862 };
863
864 class LITEFX_MATH_API Size2d : public Vector<size_t, 2> {
865 public:
866 Size2d() noexcept;
867 Size2d(size_t v) noexcept;
868 Size2d(size_t w, size_t h) noexcept;
869 Size2d(const Size2d&);
870 Size2d(Size2d&&) noexcept;
871 ~Size2d() noexcept = default;
872
873 public:
874 Size2d& operator=(const Size2d& _other);
875 Size2d& operator=(Size2d&& _other) noexcept;
876 operator Size3d() const noexcept;
877 operator Size4d() const noexcept;
878 Size2d operator/(size_t s) noexcept;
879 Size2d& operator/=(size_t s) noexcept;
880 Size2d operator*(size_t s) noexcept;
881 Size2d& operator*=(size_t s) noexcept;
882 Size2d operator+(const Size2d& s) noexcept;
883 Size2d& operator+=(const Size2d& s) noexcept;
884 Size2d operator-(const Size2d& s) noexcept;
885 Size2d& operator-=(const Size2d& s) noexcept;
886
887 public:
888 size_t width() const noexcept;
889 size_t& width() noexcept;
890 size_t height() const noexcept;
891 size_t& height() noexcept;
892 };
893#pragma endregion
894
895#pragma region Rectangle
896 class LITEFX_MATH_API Rect : public Vector<size_t, 4> {
897 public:
898 Rect() noexcept;
899 Rect(const Vector<size_t, 2>& pos, size_t w, size_t h) noexcept;
900 Rect(size_t x, size_t y, size_t w, size_t h) noexcept;
901 Rect(const Rect&);
902 Rect(Rect&&) noexcept;
903 ~Rect() noexcept = default;
904
905 public:
906 Rect& operator=(const Rect& _other);
907 Rect& operator=(Rect&& _other) noexcept;
908
909 public:
910 Vector<size_t, 2> position() const noexcept;
911 Size2d extent() const noexcept;
912 size_t width() const noexcept;
913 size_t& width() noexcept;
914 size_t height() const noexcept;
915 size_t& height() noexcept;
916 };
917
918 class LITEFX_MATH_API RectI : public Vector<Int32, 4> {
919 public:
920 RectI() noexcept;
921 RectI(const Vector<Int32, 2>& pos, Int32 w, Int32 h) noexcept;
922 RectI(Int32 x, Int32 y, Int32 w, Int32 h) noexcept;
923 RectI(const RectI&);
924 RectI(RectI&&) noexcept;
925 ~RectI() noexcept = default;
926
927 public:
928 RectI& operator=(const RectI& _other);
929 RectI& operator=(RectI&& _other) noexcept;
930
931 public:
932 Vector<Int32, 2> position() const noexcept;
933 Size2d extent() const noexcept;
934 Int32 width() const noexcept;
935 Int32& width() noexcept;
936 Int32 height() const noexcept;
937 Int32& height() noexcept;
938 };
939
940 class LITEFX_MATH_API RectF : public Vector<Float, 4> {
941 public:
942 RectF() noexcept;
943 RectF(const Vector<Float, 2>& pos, Float w, Float h) noexcept;
944 RectF(Float x, Float y, Float w, Float h) noexcept;
945 RectF(const RectF&);
946 RectF(RectF&&) noexcept;
947 ~RectF() noexcept = default;
948
949 public:
950 RectF& operator=(const RectF& _other);
951 RectF& operator=(RectF&& _other) noexcept;
952
953 public:
954 Vector<Float, 2> position() const noexcept;
955 Size2d extent() const noexcept;
956 Float width() const noexcept;
957 Float& width() noexcept;
958 Float height() const noexcept;
959 Float& height() noexcept;
960 };
961#pragma endregion
962}
Definition math.hpp:940
~RectF() noexcept=default
Definition math.hpp:896
~Rect() noexcept=default
Definition math.hpp:918
~RectI() noexcept=default
Definition math.hpp:864
~Size2d() noexcept=default
Definition math.hpp:833
~Size3d() noexcept=default
Definition math.hpp:801
~Size4d() noexcept=default
A vector that contains a single float.
Definition math.hpp:189
A vector that contains a single unsigned integer.
Definition math.hpp:239
A vector that contains two floats.
Definition math.hpp:287
A vector that contains two signed integers.
Definition math.hpp:417
A vector that contains two unsigned integers.
Definition math.hpp:352
A vector that stores three floats.
Definition math.hpp:482
Definition math.hpp:526
Definition math.hpp:504
Definition math.hpp:548
Definition math.hpp:592
Definition math.hpp:570
Definition math.hpp:30
uint16_t UInt16
A type for an unsigned 16 bit integer.
Definition math.hpp:51
uint8_t UInt8
A type for an unsigned 8 bit integer.
Definition math.hpp:41
uint64_t UInt64
A type for an unsigned 64 bit integer.
Definition math.hpp:71
float_t Float
A type for a floating point value with single precision.
Definition math.hpp:76
double_t Double
A type for a floating point value with double precision.
Definition math.hpp:81
uint32_t UInt32
A type for an unsigned 32 bit integer.
Definition math.hpp:61
int64_t Int64
A type for a signed 64 bit integer.
Definition math.hpp:66
int16_t Int16
A type for a signed 16 bit integer.
Definition math.hpp:46
int32_t Int32
A type for a signed 32 bit integer.
Definition math.hpp:56
std::byte Byte
A type for a single byte of raw memory.
Definition math.hpp:36
Definition app.hpp:6
An algebraic vector type.
Definition vector.hpp:23