mal-math
C++20 mathematics library.
All Classes Namespaces Files Functions Variables Typedefs Concepts
vec4.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "vec.hpp"
12#pragma warning(push)
13#pragma warning(disable : 4201)
14namespace mal_math
15{
21 template <Primitive T>
22 struct vec<4, T>
23 {
24 static_assert(!std::is_const_v<T>);
26 using type = T;
28 static constexpr size_t size = 4;
30 constexpr vec() = default;
37 template <Primitive U>
38 constexpr vec(U value);
51 template <Primitive A, Primitive B, Primitive C, Primitive D>
52 constexpr vec(A a, B b, C c, D d);
59 template <typename... U>
60 constexpr vec(U... data);
62 constexpr vec(std::array<T, size> const &arr) : data{ arr } {}
64 constexpr vec(std::array<T, size> &&arr) : data{ std::move(arr) } {}
66 constexpr static vec<4, T> zero() noexcept { return vec<4, T>(0); }
68 constexpr void reset() noexcept;
69
71 template <typename U>
72 constexpr vec<size, T> &operator=(_detail::rvec<size, U> const &b)
73 {
74 for (size_t i = 0; i < size; i++)
75 {
76 data[i] = b.data[i];
77 }
78 return *this;
79 }
80
82 template <typename U>
83 constexpr vec<size, T> &operator=(vec<size, U> const &b)
84 {
85 for (size_t i = 0; i < size; i++)
86 {
87 data[i] = b.data[i];
88 }
89 return *this;
90 }
91
93 [[nodiscard]] constexpr vec<4, T> const &operator+() const noexcept;
95 [[nodiscard]] constexpr vec<4, T> operator-() const noexcept;
96
97 template <Primitive U>
98 constexpr vec<4, T> &operator+=(U const value) noexcept;
99 template <Primitive U>
100 constexpr vec<4, T> &operator-=(U const value) noexcept;
101 template <Primitive U>
102 constexpr vec<4, T> &operator*=(U const value) noexcept;
103 template <Primitive U>
104 constexpr vec<4, T> &operator/=(U const value) noexcept;
105 template <Primitive U>
106 constexpr vec<4, T> &operator%=(U const value) noexcept;
107 template <AnyVec U>
108 constexpr vec<4, T> &operator+=(U const &other) noexcept requires(size == U::size);
109 template <AnyVec U>
110 constexpr vec<4, T> &operator-=(U const &other) noexcept requires(size == U::size);
111 template <AnyVec U>
112 constexpr vec<4, T> &operator*=(U const &other) noexcept requires(size == U::size);
113 template <AnyVec U>
114 constexpr vec<4, T> &operator/=(U const &other) noexcept requires(size == U::size);
115 template <AnyVec U>
116 constexpr vec<4, T> &operator%=(U const &other) noexcept requires(size == U::size);
117
119 [[nodiscard]] constexpr T &operator[](size_t i);
121 [[nodiscard]] constexpr T const &operator[](size_t i) const;
122
124 union
125 {
126 struct
127 {
128 union
129 {
130 T x, r, s;
131 };
132 union
133 {
134 T y, g, t;
135 };
136 union
137 {
138 T z, b, p;
139 };
140 union
141 {
142 T w, a, q;
143 };
144 };
145 std::array<T, size> data;
147 struct
148 {
151 };
152 struct
153 {
156 };
157 struct
158 {
161 };
162 };
163 static_assert(sizeof(data) == size * sizeof(T));
164
165 private:
171 template <Primitive _>
172 static constexpr size_t get_parameter_pack_size();
178 template <class V>
179 static constexpr size_t get_parameter_pack_size();
185 template <typename A, typename B, typename... C>
186 static constexpr size_t get_parameter_pack_size();
187
188
195 template <Primitive U>
196 constexpr void unpack_data(size_t offset, U u);
203 template <class V>
204 constexpr void unpack_data(size_t offset, V vec);
213 template <typename A, typename B, typename... C>
214 constexpr void unpack_data(size_t offset, A a, B b, C... c);
215 };
216}; // namespace mal_math
217
218#pragma warning(pop)
219#include "vec4.inl"
Concept to determine if a type is any kind of vector.
Concept that ensures a type is either floating point or integral.
Contains mathematical utility functions and classes.
constexpr mat< T::size.x, T::size.y, std::remove_const_t< typename T::type > > operator+(U const left, T const &right)
Adds a matrix to a primitive.
Definition mat_math.inl:105
STL namespace.
vec< 3, T > xyz
Definition vec4.hpp:146
constexpr vec(std::array< T, size > &&arr)
Move constructor from std::array.
Definition vec4.hpp:64
std::array< T, size > data
Definition vec4.hpp:145
constexpr vec(std::array< T, size > const &arr)
Constructor from std::array.
Definition vec4.hpp:62
constexpr void unpack_data(size_t offset, V vec)
Unpacks a vector into the matrix data.
vec< 2, T > xy
Definition vec4.hpp:154
vec< 2, T > zw
Definition vec4.hpp:155
static constexpr size_t get_parameter_pack_size()
Determines the size of a parameter pack for vector types.
vec< 2, T > yzw
Definition vec4.hpp:160
static constexpr vec< 4, T > zero() noexcept
Returns a zero vector.
Definition vec4.hpp:66
T type
Type of the vector elements.
Definition vec4.hpp:26
constexpr vec< size, T > & operator=(vec< size, U > const &b)
Overloaded assignment operator for another vec object.
Definition vec4.hpp:83
vec< 2, T > yz
Definition vec4.hpp:150
constexpr vec()=default
Default constructor.
Definition of the mathematical vector with fixed size L and type T
Definition vecn.hpp:22
std::array< T, size > data
The underlying data of the vector.
Definition vecn.hpp:101
static constexpr size_t get_parameter_pack_size()
Determines the size of a parameter pack for primitive types.
Definition vecn.inl:146
constexpr void unpack_data(size_t offset, U u)
Unpacks a single primitive value into the vector data.
Definition vecn.hpp:134
static constexpr size_t size
Number of elements in the vector.
Definition vecn.hpp:27
constexpr vec()=default
Default constructor.
constexpr void reset() noexcept
Resets the vector to the zero vector.
Definition vecn.inl:23