LiteFX 0.3.1.2022
Computer Graphics Engine
vector.hpp
1#pragma once
2
3#include <cassert>
4#include <algorithm>
5
6namespace LiteFX::Math {
7
8 template <typename T, unsigned DIM>
9 class Vector {
10 public:
11 static constexpr size_t vec_size = DIM;
12 using scalar_type = T;
14
15 protected:
17
18 public:
19 Vector() noexcept = default;
20
21 Vector(const T& val) noexcept {
22 std::fill(std::begin(m_elements), std::end(m_elements), val);
23 }
24
25 inline Vector(const vec_type& _other) noexcept { operator=(_other); }
26 inline Vector(vec_type&& _other) noexcept { operator=(_other); }
27 //virtual inline ~Vector() noexcept = default;
28
29 public:
30 Vector<T, DIM>& operator= (const Vector<T, DIM>& _other) noexcept {
31 std::copy(std::begin(_other.m_elements), std::end(_other.m_elements), std::begin(m_elements));
32 return *this;
33 }
34
36 std::move(std::begin(_other.m_elements), std::end(_other.m_elements), std::begin(m_elements));
37 return *this;
38 }
39
40 inline const T& operator[](const unsigned int& i) const noexcept {
41 assert(i < DIM);
42
43 return m_elements[i];
44 }
45
46 inline T& operator[](const unsigned int& i) noexcept {
47 assert(i < DIM);
48
49 return m_elements[i];
50 }
51
52 public:
53 inline const scalar_type* elements() const noexcept {
54 return m_elements;
55 }
56
57 inline int size() const noexcept {
58 return vec_size;
59 }
60
61 template<typename = std::enable_if_t<(DIM > 0)>>
62 inline const scalar_type& x() const noexcept {
63 return m_elements[0];
64 }
65
66 template<typename = std::enable_if_t<(DIM > 0)>>
67 inline scalar_type& x() noexcept {
68 return m_elements[0];
69 }
70
71 template<typename = std::enable_if_t<(DIM > 1)>>
72 inline const scalar_type& y() const noexcept {
73 return m_elements[1];
74 }
75
76 template<typename = std::enable_if_t<(DIM > 1)>>
77 inline scalar_type& y() noexcept {
78 return m_elements[1];
79 }
80
81 template<typename = std::enable_if_t<(DIM > 2)>>
82 inline const scalar_type& z() const noexcept {
83 return m_elements[2];
84 }
85
86 template<typename = std::enable_if_t<(DIM > 2)>>
87 inline scalar_type& z() noexcept {
88 return m_elements[2];
89 }
90
91 template<typename = std::enable_if_t<(DIM > 3)>>
92 inline const scalar_type& w() const noexcept {
93 return m_elements[3];
94 }
95
96 template<typename = std::enable_if_t<(DIM > 3)>>
97 inline scalar_type& w() noexcept {
98 return m_elements[3];
99 }
100 };
101
102 template<typename T> using TVector1 = Vector<T, 1>;
103 template<typename T> using TVector2 = Vector<T, 2>;
104 template<typename T> using TVector3 = Vector<T, 3>;
105 template<typename T> using TVector4 = Vector<T, 4>;
106
107}
Definition: vector.hpp:9
Vector() noexcept=default
scalar_type & y() noexcept
Definition: vector.hpp:77
const scalar_type * elements() const noexcept
Definition: vector.hpp:53
const T & operator[](const unsigned int &i) const noexcept
Definition: vector.hpp:40
const scalar_type & w() const noexcept
Definition: vector.hpp:92
scalar_type & z() noexcept
Definition: vector.hpp:87
int size() const noexcept
Definition: vector.hpp:57
const scalar_type & y() const noexcept
Definition: vector.hpp:72
const scalar_type & z() const noexcept
Definition: vector.hpp:82
T & operator[](const unsigned int &i) noexcept
Definition: vector.hpp:46
T scalar_type
Definition: vector.hpp:12
scalar_type & w() noexcept
Definition: vector.hpp:97
const scalar_type & x() const noexcept
Definition: vector.hpp:62
Vector(const vec_type &_other) noexcept
Definition: vector.hpp:25
Vector< T, DIM > & operator=(const Vector< T, DIM > &_other) noexcept
Definition: vector.hpp:30
scalar_type m_elements[vec_size]
Definition: vector.hpp:16
scalar_type & x() noexcept
Definition: vector.hpp:67
static constexpr size_t vec_size
Definition: vector.hpp:11
Vector(vec_type &&_other) noexcept
Definition: vector.hpp:26
Definition: math.hpp:30