wren
Vulkan-based game engine
Loading...
Searching...
No Matches
vector.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <array>
4#include <cmath>
5
6namespace wren::math {
7
8template <typename T, std::size_t N>
9struct Vec {
11
12 static auto unit_x() { return vec_t{1.0f}; }
13 static auto unit_y() { return vec_t{0.0f, 1.0f}; }
14 static auto unit_z() { return vec_t{0.0f, 0.0f, 1.0f}; }
15
16 Vec() : data() {}
17
18 Vec(std::array<T, N> data) : data(data) {}
19 Vec(float scalar) {
20 for (size_t i = 0; i < N; ++i) {
21 data.at(i) = scalar;
22 }
23 }
24
25 template <typename... Args>
26 Vec(Args... args)
27 requires(sizeof...(Args) > 1)
28 : data({std::forward<Args>(args)...}) {}
29
30 // Swizzles
31 [[nodiscard]] auto xyz() const {
32 static_assert(N > 3, "xyz swizzle requires 4 or more components ");
33 return Vec<T, 3>{data.at(0), data.at(1), data.at(2)};
34 }
35
36 auto at(std::size_t i) -> T& { return data.at(i); }
37 [[nodiscard]] auto at(std::size_t i) const { return data.at(i); }
38
39 constexpr auto operator*=(float scalar) const {
40 for (auto& d : data) {
41 d *= scalar;
42 }
43 }
44
45 constexpr auto operator*(float scalar) const {
46 vec_t v{};
47 for (std::size_t i = 0; i < N; i++) v.data.at(i) = data.at(i) * scalar;
48 return v;
49 }
50
51 constexpr auto operator*=(const vec_t& other) {
52 for (std::size_t i = 0; i < N; i++) {
53 data.at(i) = data.at(i) * other.data.at(i);
54 }
55 }
56
57 constexpr auto operator*(const vec_t& other) const {
58 vec_t v{};
59 for (std::size_t i = 0; i < N; i++) {
60 v.data.at(i) = data.at(i) * other.data.at(i);
61 }
62 return v;
63 }
64
65 [[nodiscard]] constexpr auto dot(const vec_t& other) const {
66 T dot = 0;
67 for (std::size_t i = 0; i < N; i++)
68 dot += this->data.at(i) * other.data.at(i);
69 return dot;
70 }
71
72 constexpr auto operator+=(const vec_t& other) {
73 for (std::size_t i = 0; i < N; i++)
74 data.at(i) = data.at(i) + other.data.at(i);
75 }
76
77 constexpr auto operator+(const vec_t& other) const {
78 vec_t v{};
79 for (std::size_t i = 0; i < N; i++)
80 v.data.at(i) = data.at(i) + other.data.at(i);
81 return v;
82 }
83
84 constexpr auto operator-=(const vec_t& other) {
85 for (std::size_t i = 0; i < N; i++) {
86 data.at(i) = data.at(i) - other.data.at(i);
87 }
88 }
89
90 constexpr auto operator-(const vec_t& other) const {
91 vec_t v{};
92 for (std::size_t i = 0; i < N; i++)
93 v.data.at(i) = data.at(i) - other.data.at(i);
94 return v;
95 }
96
97 constexpr auto operator-() const {
98 vec_t v{};
99 for (std::size_t i = 0; i < N; i++) v.data.at(i) = -data.at(i);
100 return v;
101 }
102
103 auto operator/(float scalar) const {
104 vec_t v{};
105 for (std::size_t i = 0; i < N; i++) v.data.at(i) = data.at(i) / scalar;
106 return v;
107 }
108
109 constexpr auto operator==(const vec_t& other) const {
110 return data == other.data;
111 }
112
113 constexpr auto operator!=(const vec_t& other) const {
114 return !(*this == other);
115 }
116
117 [[nodiscard]] constexpr auto length() const {
118 T sum = 0;
119 for (const auto& d : data) sum += d * d;
120 return std::sqrt(sum);
121 }
122
123 [[nodiscard]] auto normalized() const { return *this / length(); }
124
125 std::array<T, N> data{};
126};
127
128template <typename T, std::size_t N>
129auto operator*(float scalar, Vec<T, N> vec) -> Vec<T, N> {
130 return vec * scalar;
131}
132
133// // struct vec2i : Vec<int, 2> {
134// vec2i() : Vec<int, 2>() {}
135// vec2i(int x, int y) : Vec<int, 2>({x, y}) {}
136// vec2i(const Vec<int, 2>& other) : Vec<int, 2>(other) {}
137
138// [[nodiscard]] auto x() const { return data.at(0); }
139// [[nodiscard]] auto y() const { return data.at(1); }
140// };
141
142struct Vec2f : Vec<float, 2> {
143 Vec2f() : Vec<float, 2>() {}
144 Vec2f(float x, float y) : Vec<float, 2>(x, y) {}
145 Vec2f(const Vec<float, 2>& other) : Vec<float, 2>(other) {}
146
147 [[nodiscard]] auto x() const { return data.at(0); }
148 auto x(float x) { data.at(0) = x; }
149 [[nodiscard]] auto y() const { return data.at(1); }
150 auto y(float y) { data.at(1) = y; }
151};
152
153struct Vec3f : Vec<float, 3> {
154 Vec3f() : Vec<float, 3>() {}
155 Vec3f(const auto& other) : Vec<float, 3>(other) {}
156 // Vec3f(float scalar) : Vec<float, 3>({scalar, scalar, scalar}) {}
157 Vec3f(float x, float y, float z) : Vec<float, 3>(x, y, z) {}
158 Vec3f(const Vec<float, 3>& other) : Vec<float, 3>(other) {}
159
160 [[nodiscard]] auto x() const { return data.at(0); }
161 auto x(float x) { data.at(0) = x; }
162 [[nodiscard]] auto y() const { return data.at(1); }
163 auto y(float y) { data.at(1) = y; }
164 [[nodiscard]] auto z() const { return data.at(2); }
165 auto z(float z) { data.at(2) = z; }
166
167 auto operator%(const Vec3f& other) const {
168 return Vec3f{y() * other.z() - z() * other.y(),
169 z() * other.x() - x() * other.z(),
170 x() * other.y() - y() * other.x()};
171 }
172};
173
174struct Vec4f : Vec<float, 4> {
175 Vec4f() : Vec<float, 4>() {}
176 Vec4f(const auto& other) : Vec<float, 4>(other) {}
177 Vec4f(float x, float y, float z, float w) : Vec<float, 4>(x, y, z, w) {}
178 Vec4f(const Vec3f& vec, float w)
179 : Vec<float, 4>(vec.x(), vec.y(), vec.z(), w) {}
180 Vec4f(const Vec<float, 4>& other) : Vec<float, 4>(other) {}
181 // Vec4f(std::array<float, 4> data) : Vec<float, 4>(data) {}
182
183 [[nodiscard]] auto x() const { return data.at(0); }
184 [[nodiscard]] auto y() const { return data.at(1); }
185 [[nodiscard]] auto z() const { return data.at(2); }
186 [[nodiscard]] auto w() const { return data.at(3); }
187};
188
189} // namespace wren::math
Definition geometry.hpp:8
auto operator*(float scalar, Vec< T, N > vec) -> Vec< T, N >
Definition vector.hpp:129
Definition vector.hpp:142
auto x() const
Definition vector.hpp:147
Vec2f(float x, float y)
Definition vector.hpp:144
Vec2f(const Vec< float, 2 > &other)
Definition vector.hpp:145
Vec2f()
Definition vector.hpp:143
auto y() const
Definition vector.hpp:149
auto y(float y)
Definition vector.hpp:150
auto x(float x)
Definition vector.hpp:148
Definition vector.hpp:153
auto z(float z)
Definition vector.hpp:165
Vec3f(const auto &other)
Definition vector.hpp:155
Vec3f(float x, float y, float z)
Definition vector.hpp:157
auto y() const
Definition vector.hpp:162
Vec3f(const Vec< float, 3 > &other)
Definition vector.hpp:158
Vec3f()
Definition vector.hpp:154
auto z() const
Definition vector.hpp:164
auto x() const
Definition vector.hpp:160
auto x(float x)
Definition vector.hpp:161
auto operator%(const Vec3f &other) const
Definition vector.hpp:167
auto y(float y)
Definition vector.hpp:163
Definition vector.hpp:174
auto z() const
Definition vector.hpp:185
auto x() const
Definition vector.hpp:183
Vec4f(const auto &other)
Definition vector.hpp:176
auto w() const
Definition vector.hpp:186
Vec4f(float x, float y, float z, float w)
Definition vector.hpp:177
auto y() const
Definition vector.hpp:184
Vec4f(const Vec< float, 4 > &other)
Definition vector.hpp:180
Vec4f()
Definition vector.hpp:175
Vec4f(const Vec3f &vec, float w)
Definition vector.hpp:178
Definition vector.hpp:9
Vec(std::array< T, N > data)
Definition vector.hpp:18
constexpr auto operator*=(float scalar) const
Definition vector.hpp:39
constexpr auto operator==(const vec_t &other) const
Definition vector.hpp:109
std::array< T, N > data
Definition vector.hpp:125
constexpr auto length() const
Definition vector.hpp:117
constexpr auto operator*=(const vec_t &other)
Definition vector.hpp:51
constexpr auto operator+=(const vec_t &other)
Definition vector.hpp:72
auto xyz() const
Definition vector.hpp:31
auto normalized() const
Definition vector.hpp:123
Vec(float scalar)
Definition vector.hpp:19
static auto unit_z()
Definition vector.hpp:14
static auto unit_x()
Definition vector.hpp:12
constexpr auto operator-() const
Definition vector.hpp:97
static auto unit_y()
Definition vector.hpp:13
constexpr auto operator-=(const vec_t &other)
Definition vector.hpp:84
Vec()
Definition vector.hpp:16
constexpr auto operator-(const vec_t &other) const
Definition vector.hpp:90
constexpr auto dot(const vec_t &other) const
Definition vector.hpp:65
auto at(std::size_t i) const
Definition vector.hpp:37
constexpr auto operator*(const vec_t &other) const
Definition vector.hpp:57
constexpr auto operator+(const vec_t &other) const
Definition vector.hpp:77
auto operator/(float scalar) const
Definition vector.hpp:103
constexpr auto operator*(float scalar) const
Definition vector.hpp:45
auto at(std::size_t i) -> T &
Definition vector.hpp:36
constexpr auto operator!=(const vec_t &other) const
Definition vector.hpp:113
Vec(Args... args)
Definition vector.hpp:26