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