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 UnitX() { return vec_t{1.0f}; }
13 static auto UnitY() { return vec_t{0.0f, 1.0f}; }
14 static auto UnitZ() { return vec_t{0.0f, 0.0f, 1.0f}; }
15
16 Vec() : data() {}
17
18 Vec(std::array<T, N> data) : data(data) {}
19
20 // template <typename... Args>
21 // Vec(Args&&... d) : data({{std::forward<Args>(d)...}}) {}
22
23 auto at(std::size_t i) -> T& { return data.at(i); }
24 [[nodiscard]] auto at(std::size_t i) const { return data.at(i); }
25
26 constexpr auto operator*=(float scalar) const {
27 for (auto& d : data) {
28 d *= scalar;
29 }
30 }
31
32 constexpr auto operator*(float scalar) const {
33 vec_t v{};
34 for (std::size_t i = 0; i < N; i++) v.data.at(i) = data.at(i) * scalar;
35 return v;
36 }
37
38 constexpr auto operator*=(const vec_t& other) {
39 for (std::size_t i = 0; i < N; i++) {
40 data.at(i) = data.at(i) * other.data.at(i);
41 }
42 }
43
44 constexpr auto operator*(const vec_t& other) const {
45 vec_t v{};
46 for (std::size_t i = 0; i < N; i++) {
47 v.data.at(i) = data.at(i) * other.data.at(i);
48 }
49 return v;
50 }
51
52 [[nodiscard]] constexpr auto dot(const vec_t& other) const {
53 T dot = 0;
54 const auto a = this->normalized();
55 const auto b = other.normalized();
56 for (std::size_t i = 0; i < N; i++) dot += a.data.at(i) * b.data.at(i);
57 return dot;
58 }
59
60 constexpr auto operator+=(const vec_t& other) {
61 for (std::size_t i = 0; i < N; i++)
62 data.at(i) = data.at(i) + other.data.at(i);
63 }
64
65 constexpr auto operator+(const vec_t& other) const {
66 vec_t v{};
67 for (std::size_t i = 0; i < N; i++)
68 v.data.at(i) = data.at(i) + other.data.at(i);
69 return v;
70 }
71
72 constexpr auto operator-(const vec_t& other) const {
73 vec_t v{};
74 for (std::size_t i = 0; i < N; i++)
75 v.data.at(i) = data.at(i) - other.data.at(i);
76 return v;
77 }
78
79 constexpr auto operator-() const {
80 vec_t v{};
81 for (std::size_t i = 0; i < N; i++) v.data.at(i) = -data.at(i);
82 return v;
83 }
84
85 auto operator/(float scalar) const {
86 vec_t v{};
87 for (std::size_t i = 0; i < N; i++) v.data.at(i) = data.at(i) / scalar;
88 return v;
89 }
90
91 constexpr auto operator==(const vec_t& other) const {
92 return data == other.data;
93 }
94
95 constexpr auto operator!=(const vec_t& other) const {
96 return !(*this == other);
97 }
98
99 [[nodiscard]] constexpr auto length() const {
100 T sum = 0;
101 for (const auto& d : data) sum += d * d;
102 return std::sqrt(sum);
103 }
104
105 [[nodiscard]] auto normalized() const { return *this / length(); }
106
107 std::array<T, N> data{};
108};
109
110struct vec2i : Vec<int, 2> {
111 vec2i() : Vec<int, 2>() {}
112 vec2i(int x, int y) : Vec<int, 2>({x, y}) {}
113 vec2i(const Vec<int, 2>& other) : Vec<int, 2>(other) {}
114
115 [[nodiscard]] auto x() const { return data.at(0); }
116 [[nodiscard]] auto y() const { return data.at(1); }
117};
118
119struct vec2f : Vec<float, 2> {
120 vec2f() : Vec<float, 2>() {}
121 vec2f(float x, float y) : Vec<float, 2>({x, y}) {}
122 vec2f(const Vec<float, 2>& other) : Vec<float, 2>(other) {}
123
124 [[nodiscard]] auto x() const { return data.at(0); }
125 [[nodiscard]] auto y() const { return data.at(1); }
126};
127
128struct Vec3f : Vec<float, 3> {
129 Vec3f() : Vec<float, 3>() {}
130 Vec3f(float scalar): Vec<float, 3> ({scalar, scalar, scalar}){}
131 Vec3f(float x, float y, float z) : Vec<float, 3>({x, y, z}) {}
132 Vec3f(const Vec<float, 3>& other) : Vec<float, 3>(other) {}
133
134 [[nodiscard]] auto x() const { return data.at(0); }
135 auto x(float x) { data.at(0) = x; }
136 [[nodiscard]] auto y() const { return data.at(1); }
137 auto y(float y) { data.at(1) = y; }
138 [[nodiscard]] auto z() const { return data.at(2); }
139 auto z(float z) { data.at(2) = z; }
140
141 auto operator%(const Vec3f& other) const {
142 return Vec3f{y() * other.z() - z() * other.y(),
143 z() * other.x() - x() * other.z(),
144 x() * other.y() - y() * other.x()};
145 }
146};
147
148struct Vec4f : Vec<float, 4> {
149 Vec4f() : Vec<float, 4>() {}
150 Vec4f(float x, float y, float z, float w) : Vec<float, 4>({x, y, z, w}) {}
151 Vec4f(const Vec3f& vec, float w)
152 : Vec<float, 4>({vec.x(), vec.y(), vec.z(), w}) {}
153 Vec4f(const Vec<float, 4>& other) : Vec<float, 4>(other) {}
154 Vec4f(std::array<float, 4> data) : Vec<float, 4>(data) {}
155
156 [[nodiscard]] auto x() const { return data.at(0); }
157 [[nodiscard]] auto y() const { return data.at(1); }
158 [[nodiscard]] auto z() const { return data.at(2); }
159 [[nodiscard]] auto w() const { return data.at(3); }
160};
161
162} // namespace wren::math
Definition geometry.hpp:8
Definition vector.hpp:128
auto z(float z)
Definition vector.hpp:139
Vec3f(float scalar)
Definition vector.hpp:130
Vec3f(float x, float y, float z)
Definition vector.hpp:131
auto y() const
Definition vector.hpp:136
Vec3f(const Vec< float, 3 > &other)
Definition vector.hpp:132
Vec3f()
Definition vector.hpp:129
auto z() const
Definition vector.hpp:138
auto x() const
Definition vector.hpp:134
auto x(float x)
Definition vector.hpp:135
auto operator%(const Vec3f &other) const
Definition vector.hpp:141
auto y(float y)
Definition vector.hpp:137
Definition vector.hpp:148
auto z() const
Definition vector.hpp:158
auto x() const
Definition vector.hpp:156
auto w() const
Definition vector.hpp:159
Vec4f(float x, float y, float z, float w)
Definition vector.hpp:150
auto y() const
Definition vector.hpp:157
Vec4f(const Vec< float, 4 > &other)
Definition vector.hpp:153
Vec4f()
Definition vector.hpp:149
Vec4f(std::array< float, 4 > data)
Definition vector.hpp:154
Vec4f(const Vec3f &vec, float w)
Definition vector.hpp:151
Definition vector.hpp:9
Vec(std::array< T, N > data)
Definition vector.hpp:18
constexpr auto operator*=(float scalar) const
Definition vector.hpp:26
constexpr auto operator==(const vec_t &other) const
Definition vector.hpp:91
std::array< T, N > data
Definition vector.hpp:107
constexpr auto length() const
Definition vector.hpp:99
static auto UnitX()
Definition vector.hpp:12
constexpr auto operator*=(const vec_t &other)
Definition vector.hpp:38
constexpr auto operator+=(const vec_t &other)
Definition vector.hpp:60
auto normalized() const
Definition vector.hpp:105
constexpr auto operator-() const
Definition vector.hpp:79
Vec()
Definition vector.hpp:16
constexpr auto operator-(const vec_t &other) const
Definition vector.hpp:72
constexpr auto dot(const vec_t &other) const
Definition vector.hpp:52
auto at(std::size_t i) const
Definition vector.hpp:24
constexpr auto operator*(const vec_t &other) const
Definition vector.hpp:44
static auto UnitY()
Definition vector.hpp:13
constexpr auto operator+(const vec_t &other) const
Definition vector.hpp:65
static auto UnitZ()
Definition vector.hpp:14
auto operator/(float scalar) const
Definition vector.hpp:85
constexpr auto operator*(float scalar) const
Definition vector.hpp:32
auto at(std::size_t i) -> T &
Definition vector.hpp:23
constexpr auto operator!=(const vec_t &other) const
Definition vector.hpp:95
Definition vector.hpp:119
auto y() const
Definition vector.hpp:125
vec2f(float x, float y)
Definition vector.hpp:121
vec2f()
Definition vector.hpp:120
auto x() const
Definition vector.hpp:124
vec2f(const Vec< float, 2 > &other)
Definition vector.hpp:122
Definition vector.hpp:110
vec2i(const Vec< int, 2 > &other)
Definition vector.hpp:113
auto x() const
Definition vector.hpp:115
auto y() const
Definition vector.hpp:116
vec2i(int x, int y)
Definition vector.hpp:112
vec2i()
Definition vector.hpp:111