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