summaryrefslogtreecommitdiff
path: root/src/Math
diff options
context:
space:
mode:
authorMel <einebeere@gmail.com>2023-07-08 03:25:44 +0200
committerMel <einebeere@gmail.com>2023-07-08 03:25:44 +0200
commitfe2baedc760c2f29e2c720f6b1132a2de33c5430 (patch)
treedfbe1c72a17805a3cab6e0d47433e9021890c9ca /src/Math
parent41fbca10f6c6cdd9c1623f1347e7ecb40f5e7f59 (diff)
downloadmeowcraft-fe2baedc760c2f29e2c720f6b1132a2de33c5430.tar.zst
meowcraft-fe2baedc760c2f29e2c720f6b1132a2de33c5430.zip
Use own size types
Diffstat (limited to 'src/Math')
-rw-r--r--src/Math/Common.hpp2
-rw-r--r--src/Math/Constants.hpp6
-rw-r--r--src/Math/Grid.hpp4
-rw-r--r--src/Math/Interpolation.cpp6
-rw-r--r--src/Math/Interpolation.hpp6
-rw-r--r--src/Math/MVP.cpp51
-rw-r--r--src/Math/MVP.hpp59
-rw-r--r--src/Math/Matrix.hpp58
-rw-r--r--src/Math/Perlin.cpp10
-rw-r--r--src/Math/Perlin.hpp26
-rw-r--r--src/Math/Random.cpp16
-rw-r--r--src/Math/Random.hpp22
-rw-r--r--src/Math/Rotation.hpp10
-rw-r--r--src/Math/Tensor.hpp22
-rw-r--r--src/Math/Vector.hpp28
15 files changed, 164 insertions, 162 deletions
diff --git a/src/Math/Common.hpp b/src/Math/Common.hpp
index 1ea75f2..cf5a2c7 100644
--- a/src/Math/Common.hpp
+++ b/src/Math/Common.hpp
@@ -1,5 +1,7 @@
#pragma once
+#include "../Common/Sizes.hpp"
+
#include "Vector.hpp"
#include "Matrix.hpp"
#include "Rotation.hpp"
diff --git a/src/Math/Constants.hpp b/src/Math/Constants.hpp
index 9c62e94..7a712cc 100644
--- a/src/Math/Constants.hpp
+++ b/src/Math/Constants.hpp
@@ -1,8 +1,10 @@
#pragma once
+#include "../Common/Sizes.hpp"
+
namespace Math {
-constexpr double PI = 3.14159265358979323846;
-constexpr double E = 2.71828182845904523536;
+constexpr Real PI = 3.14159265358979323846;
+constexpr Real E = 2.71828182845904523536;
} \ No newline at end of file
diff --git a/src/Math/Grid.hpp b/src/Math/Grid.hpp
index e9907a0..8c0c423 100644
--- a/src/Math/Grid.hpp
+++ b/src/Math/Grid.hpp
@@ -5,7 +5,7 @@
namespace Math {
struct GridCellBoundaries {
- float x1, x2, y1, y2;
+ Real x1, x2, y1, y2;
[[nodiscard]] Vector<2> top_left() const;
[[nodiscard]] Vector<2> top_right() const;
@@ -16,7 +16,7 @@ struct GridCellBoundaries {
GridCellBoundaries grid_cell_for_point(Vector<2> point);
struct CubeCellBoundaries {
- float x1, x2, y1, y2, z1, z2;
+ Real x1, x2, y1, y2, z1, z2;
[[nodiscard]] Vector<3> front_top_left() const;
[[nodiscard]] Vector<3> front_top_right() const;
diff --git a/src/Math/Interpolation.cpp b/src/Math/Interpolation.cpp
index d5b2564..ce87435 100644
--- a/src/Math/Interpolation.cpp
+++ b/src/Math/Interpolation.cpp
@@ -3,18 +3,18 @@
namespace Math {
-float linear_interpolation(Vector<2> val, float left, float right, float pos) {
+Real linear_interpolation(Vector<2> val, Real left, Real right, Real pos) {
return val.x() + (pos - left) * (val.y() - val.x()) / (right - left);
}
-float bilinear_interpolation(Matrix<2, 2> val, GridCellBoundaries cell, Vector<2> pos) {
+Real bilinear_interpolation(Matrix<2, 2> val, GridCellBoundaries cell, Vector<2> pos) {
auto r1 = linear_interpolation(val.row(0), cell.x1, cell.x2, pos.x());
auto r2 = linear_interpolation(val.row(1), cell.x1, cell.x2, pos.x());
return linear_interpolation({r1, r2}, cell.y1, cell.y2, pos.y());
}
-float trilinear_interpolation(Matrix<2, 2> val_front, Matrix<2, 2> val_back, const CubeCellBoundaries& cell, Vector<3> pos) {
+Real trilinear_interpolation(Matrix<2, 2> val_front, Matrix<2, 2> val_back, const CubeCellBoundaries& cell, Vector<3> pos) {
auto r1 = bilinear_interpolation(val_front, cell.grid_cell(), {pos.x(), pos.y()});
auto r2 = bilinear_interpolation(val_back, cell.grid_cell(), {pos.x(), pos.y()});
diff --git a/src/Math/Interpolation.hpp b/src/Math/Interpolation.hpp
index eaaa67d..476df5c 100644
--- a/src/Math/Interpolation.hpp
+++ b/src/Math/Interpolation.hpp
@@ -5,8 +5,8 @@
namespace Math {
-float linear_interpolation(Vector<2> val, float left, float right, float pos);
-float bilinear_interpolation(Matrix<2, 2> val, GridCellBoundaries cell, Vector<2> pos);
-float trilinear_interpolation(Matrix<2, 2> val_front, Matrix<2, 2> val_back, const CubeCellBoundaries& cell, Vector<3> pos);
+Real linear_interpolation(Vector<2> val, Real left, Real right, Real pos);
+Real bilinear_interpolation(Matrix<2, 2> val, GridCellBoundaries cell, Vector<2> pos);
+Real trilinear_interpolation(Matrix<2, 2> val_front, Matrix<2, 2> val_back, const CubeCellBoundaries& cell, Vector<3> pos);
} \ No newline at end of file
diff --git a/src/Math/MVP.cpp b/src/Math/MVP.cpp
index de771d3..2f00b8e 100644
--- a/src/Math/MVP.cpp
+++ b/src/Math/MVP.cpp
@@ -4,56 +4,5 @@
namespace Math::MVP {
-Matrix<4, 4> model(Vector<3> position, Rotation angles) {
- auto transformation = Matrix<4, 4>::transformation(position);
- auto rotation = Matrix<4, 4>::rotation(angles);
-
- return transformation * rotation;
-}
-
-Matrix<4, 4> view(Vector<3> position, Rotation angles) {
- auto rotation = Matrix<4, 4>::rotation(angles);
- auto transformation = Matrix<4, 4>::transformation(-position);
-
- return rotation * transformation;
-}
-
-Matrix<4, 4> perspective_projection(float aspect, float fov, float near, float far) {
- auto fov_radians = radians(fov);
-
- float x_scale = 1.0f / (tan(fov_radians / 2.0f) * aspect);
- float y_scale = 1.0f / tan(fov_radians / 2.0f);
-
- float frustum_length = far - near;
- float z_near = -(far + near) / frustum_length;
- float z_far = -(2 * far * near) / frustum_length;
-
- Matrix<4, 4> projection{
- x_scale, 0.0f, 0.0f, 0.0f,
- 0.0f, y_scale, 0.0f, 0.0f,
- 0.0f, 0.0f, z_near, z_far,
- 0.0f, 0.0f, -1.0f, 0.0f,
- };
-
- return projection;
-}
-
-Matrix<4, 4> orthographic_projection(float width, float height, float near, float far) {
- float x_scale = 2.0f / width;
- float y_scale = 2.0f / -height;
-
- float frustum_length = far - near;
- float z_near = -(far + near) / frustum_length;
- float z_far = -2 / frustum_length;
-
- Matrix<4, 4> projection{
- x_scale, 0.0f, 0.0f, -1.0f,
- 0.0f, y_scale, 0.0f, 1.0f,
- 0.0f, 0.0f, z_near, z_far,
- 0.0f, 0.0f, 0.0f, 1.0f,
- };
-
- return projection;
-}
}
diff --git a/src/Math/MVP.hpp b/src/Math/MVP.hpp
index af4fbbc..4046a50 100644
--- a/src/Math/MVP.hpp
+++ b/src/Math/MVP.hpp
@@ -4,9 +4,60 @@
namespace Math::MVP {
-Matrix<4, 4> model(Vector<3> position, Rotation angles);
-Matrix<4, 4> view(Vector<3> position, Rotation angles);
-Matrix<4, 4> perspective_projection(float aspect, float fov, float near, float far);
-Matrix<4, 4> orthographic_projection(float width, float height, float near, float far);
+template<typename T = Real>
+Matrix<4, 4, T> model(Vector<3> position, Rotation angles) {
+ auto transformation = Matrix<4, 4, T>::transformation(position);
+ auto rotation = Matrix<4, 4, T>::rotation(angles);
+
+ return transformation * rotation;
+}
+
+template<typename T = Real>
+Matrix<4, 4, T> view(Vector<3> position, Rotation angles) {
+ auto rotation = Matrix<4, 4, T>::rotation(angles);
+ auto transformation = Matrix<4, 4, T>::transformation(-position);
+
+ return rotation * transformation;
+}
+
+template<typename T = Real>
+Matrix<4, 4, T> perspective_projection(Real aspect, Real fov, Real near, Real far) {
+ auto fov_radians = radians(fov);
+
+ Real x_scale = 1.0 / (tan(fov_radians / 2.0) * aspect);
+ Real y_scale = 1.0 / tan(fov_radians / 2.0);
+
+ Real frustum_length = far - near;
+ Real z_near = -(far + near) / frustum_length;
+ Real z_far = -(2 * far * near) / frustum_length;
+
+ Matrix<4, 4, T> projection{
+ x_scale, 0.0f, 0.0f, 0.0f,
+ 0.0f, y_scale, 0.0f, 0.0f,
+ 0.0f, 0.0f, z_near, z_far,
+ 0.0f, 0.0f, -1.0f, 0.0f,
+ };
+
+ return projection;
+}
+
+template<typename T = Real>
+Matrix<4, 4, T> orthographic_projection(Real width, Real height, Real near, Real far) {
+ Real x_scale = 2.0f / width;
+ Real y_scale = 2.0f / -height;
+
+ Real frustum_length = far - near;
+ Real z_near = -(far + near) / frustum_length;
+ Real z_far = -2 / frustum_length;
+
+ Matrix<4, 4, T> projection{
+ x_scale, 0.0f, 0.0f, -1.0f,
+ 0.0f, y_scale, 0.0f, 1.0f,
+ 0.0f, 0.0f, z_near, z_far,
+ 0.0f, 0.0f, 0.0f, 1.0f,
+ };
+
+ return projection;
+}
}
diff --git a/src/Math/Matrix.hpp b/src/Math/Matrix.hpp
index 56663e0..d51d171 100644
--- a/src/Math/Matrix.hpp
+++ b/src/Math/Matrix.hpp
@@ -1,11 +1,11 @@
#pragma once
#include <sstream>
-#include <cstddef>
+#include "../Common/Sizes.hpp"
#include "Rotation.hpp"
#include "Trig.hpp"
-template <size_t R, size_t C, typename T = float>
+template <uint R, uint C, typename T = Real>
struct Matrix {
Matrix() : elements{} {}
@@ -13,8 +13,8 @@ struct Matrix {
std::fill(elements, elements + R * C, scalar);
}
- template<typename ...Args, std::enable_if_t<sizeof...(Args) == R * C, int> = 0>
- Matrix(Args... args): elements{ args... } {}
+ template<typename ...Args, std::enable_if_t<sizeof...(Args) == R * C, Int> = 0>
+ Matrix(Args... args): elements{ static_cast<T>(args)... } {}
explicit Matrix(const T values[R * C]) {
std::copy(values, values + R * C, elements);
@@ -22,42 +22,42 @@ struct Matrix {
static Matrix<R, R, T> identity() {
Matrix<R, R, T> result{};
- for (int i = 0; i < R; i++) {
+ for (Int i = 0; i < R; i++) {
result(i, i) = 1;
}
return result;
}
- static Matrix<4, 4> transformation(Vector<3> position) {
+ static Matrix<4, 4, T> transformation(Vector<3> position) {
return {
- 1.0f, 0.0f, 0.0f, position.x(),
- 0.0f, 1.0f, 0.0f, position.y(),
- 0.0f, 0.0f, 1.0f, position.z(),
- 0.0f, 0.0f, 0.0f, 1.0f
+ 1.0, 0.0, 0.0, position.x(),
+ 0.0, 1.0, 0.0, position.y(),
+ 0.0, 0.0, 1.0, position.z(),
+ 0.0, 0.0, 0.0, 1.0
};
}
- static Matrix<4, 4> rotation(Rotation angles) {
+ static Matrix<4, 4, T> rotation(Rotation angles) {
auto radians = angles.vector.map([](auto a) { return Math::radians(a); });
auto c = radians.map([](auto a) { return cos(a); });
auto s = radians.map([](auto a) { return sin(a); });
- Matrix<4, 4> rotation_x{
+ Matrix<4, 4, T> rotation_x{
1.0f, 0.0f, 0.0f, 0.0f,
0.0f, c.x(), -s.x(), 0.0f,
0.0f, s.x(), c.x(), 0.0f,
0.0f, 0.0f, 0.0f, 1.0f,
};
- Matrix<4, 4> rotation_y{
+ Matrix<4, 4, T> rotation_y{
c.y(), 0.0f, s.y(), 0.0f,
0.0f, 1.0f, 0.0f, 0.0f,
-s.y(), 0.0f, c.y(), 0.0f,
0.0f, 0.0f, 0.0f, 1.0f,
};
- Matrix<4, 4> rotation_z{
+ Matrix<4, 4, T> rotation_z{
c.z(), -s.z(), 0.0f, 0.0f,
s.z(), c.z(), 0.0f, 0.0f,
0.0f, 0.0f, 1.0f, 0.0f,
@@ -67,13 +67,13 @@ struct Matrix {
return rotation_x * rotation_y * rotation_z;
}
- Vector<C, T> row(size_t index) const {
+ Vector<C, T> row(USize index) const {
return Vector<C, T>{ &elements[index * C] };
}
- Vector<R, T> col(size_t index) const {
+ Vector<R, T> col(USize index) const {
Vector<R, T> result{};
- for (int i = 0; i < R; i++) {
+ for (Int i = 0; i < R; i++) {
result[i] = this->operator()(index, i);
}
return result;
@@ -81,8 +81,8 @@ struct Matrix {
Matrix transpose() const {
Matrix result{};
- for (int y = 0; y < R; y++) {
- for (int x = 0; x < C; x++) {
+ for (Int y = 0; y < R; y++) {
+ for (Int x = 0; x < C; x++) {
result(x, y) = this->operator()(y, x);
}
}
@@ -91,25 +91,25 @@ struct Matrix {
Matrix operator+(const Matrix other) const {
Matrix result{};
- for (int i = 0; i < R * C; i++) {
+ for (Int i = 0; i < R * C; i++) {
result.elements[i] = elements[i] + other.elements[i];
}
return result;
}
- Matrix operator*(float scalar) {
+ Matrix operator*(Real scalar) {
Matrix result{};
- for (int i = 0; i < R * C; i++) {
+ for (Int i = 0; i < R * C; i++) {
result.elements[i] = elements[i] * scalar;
}
return result;
}
- template<size_t N>
+ template<uint N>
Matrix<R, N, T> operator*(const Matrix<C, N, T> other) const {
Matrix<R, N, T> result{};
- for (int y = 0; y < R; y++) {
- for (int x = 0; x < N; x++) {
+ for (Int y = 0; y < R; y++) {
+ for (Int x = 0; x < N; x++) {
auto r = row(y);
auto c = other.col(x);
@@ -127,17 +127,17 @@ struct Matrix {
return Vector<R, T>{ matrix.elements };
}
- const T& operator()(const size_t x, const size_t y) const {
+ const T& operator()(const USize x, const USize y) const {
return elements[y * C + x];
}
- T& operator()(const size_t x, const size_t y) {
+ T& operator()(const USize x, const USize y) {
return elements[y * C + x];
}
std::string string() {
std::stringstream str{};
- for (int x = 0; x < R; x++) {
- for (int y = 0; y < C; y++) {
+ for (Int x = 0; x < R; x++) {
+ for (Int y = 0; y < C; y++) {
str << this->operator()(x, y) << " ";
}
diff --git a/src/Math/Perlin.cpp b/src/Math/Perlin.cpp
index 5614bb6..992a08e 100644
--- a/src/Math/Perlin.cpp
+++ b/src/Math/Perlin.cpp
@@ -4,11 +4,11 @@
namespace Math::Perlin {
-float ease(float t) {
+Real ease(Real t) {
return t * t * t * ((6 * t - 15) * t + 10);
}
-uint8_t hash(uint8_t x) {
+U8 hash(U8 x) {
auto rot = x * 5 % 8;
return x << rot | x >> (8 - rot);
}
@@ -23,7 +23,7 @@ Vector<2> gradient(Vector<2> pos) {
return gradients[x % 4].normalize();
}
-float raw(Vector<2> pos) {
+Real raw(Vector<2> pos) {
auto cell = grid_cell_for_point(pos);
auto uv = pos - cell.top_left();
@@ -47,7 +47,7 @@ float raw(Vector<2> pos) {
}
Vector<3> gradient(Vector<3> pos) {
- constexpr float e = 0.5773502692;
+ constexpr Real e = 0.5773502692;
static Vector<3> gradients[] = {
{e, e, e}, {-e, e, e}, {e, -e, e}, {-e, -e, e},
{e, e, -e}, {-e, e, -e}, {e, -e, -e}, {-e, -e, -e},
@@ -57,7 +57,7 @@ Vector<3> gradient(Vector<3> pos) {
return gradients[x % 8];
}
-float raw(Vector<3> pos) {
+Real raw(Vector<3> pos) {
auto cell = cube_cell_for_point(pos);
auto uv = pos - cell.front_top_left();
diff --git a/src/Math/Perlin.hpp b/src/Math/Perlin.hpp
index 17d33fd..b28f992 100644
--- a/src/Math/Perlin.hpp
+++ b/src/Math/Perlin.hpp
@@ -4,27 +4,27 @@
namespace Math::Perlin {
-float raw(Vector<2> pos);
-float raw(Vector<3> pos);
+Real raw(Vector<2> pos);
+Real raw(Vector<3> pos);
-template <size_t D>
+template <USize D>
struct Noise {
static_assert(D > 1 && D < 4);
Vector<D> offset{20000.0f};
- float scale = 15.0f;
+ Real scale = 15.0f;
- uint octaves = 3;
- float persistence = 0.3f;
- float lacunarity = 2.0f;
+ UInt octaves = 3;
+ Real persistence = 0.3f;
+ Real lacunarity = 2.0f;
- float at(Vector<D> pos) const {
- float result = 0;
- float max = 0;
- float frequency = 1;
- float amplitude = 1;
+ Real at(Vector<D> pos) const {
+ Real result = 0;
+ Real max = 0;
+ Real frequency = 1;
+ Real amplitude = 1;
- for (uint octave = 0; octave < octaves; octave++) {
+ for (UInt octave = 0; octave < octaves; octave++) {
result += raw((pos + offset).abs() / scale * frequency) * amplitude;
max += amplitude;
diff --git a/src/Math/Random.cpp b/src/Math/Random.cpp
index aa0ff55..cf3bbad 100644
--- a/src/Math/Random.cpp
+++ b/src/Math/Random.cpp
@@ -2,10 +2,8 @@
namespace Math::Random {
-std::array<uint8_t, 4> break_float(const float f) {
- static_assert(sizeof(float) == 4);
-
- union { float f; uint8_t u[4]; } t{};
+std::array<U8, 4> break_float(const F32 f) {
+ union { F32 f; U8 u[4]; } t{};
t.f = f;
return {
@@ -13,18 +11,18 @@ std::array<uint8_t, 4> break_float(const float f) {
};
}
-float to_float(uint8_t u) {
- return (float)u / (float)255;
+F32 to_float(U8 u) {
+ return (F32)u / (F32)255;
}
-uint8_t hash(uint8_t x) {
+U8 hash(U8 x) {
auto o = (x ^ 0xAA) * 5;
auto rot = o % 8;
return o << rot | o >> (8 - rot);
}
-float random() {
- uint8_t r = std::rand() % 255;
+Real random() {
+ U8 r = std::rand() % 255;
return to_float(hash(r));
}
diff --git a/src/Math/Random.hpp b/src/Math/Random.hpp
index ab94871..d363694 100644
--- a/src/Math/Random.hpp
+++ b/src/Math/Random.hpp
@@ -1,27 +1,27 @@
#pragma once
-#include "array"
+#include <array>
#include "Common.hpp"
namespace Math::Random {
-std::array<uint8_t, 4> break_float(float f);
-float to_float(uint8_t u);
+std::array<U8, 4> break_float(F32 f);
+F32 to_float(U8 u);
-uint8_t hash(uint8_t x);
+U8 hash(U8 x);
-float random();
+Real random();
-template <size_t D>
+template <USize D>
struct Noise {
- float at(Vector<D> pos) const {
- uint8_t to_hash[D * 4];
- for (int i = 0; i < D; i++) {
+ Real at(Vector<D> pos) const {
+ U8 to_hash[D * 4];
+ for (Int i = 0; i < D; i++) {
auto b = break_float(pos[i]);
to_hash[i*4] = b[0]; to_hash[i*4+1] = b[1]; to_hash[i*4+2] = b[2]; to_hash[i*4+3] = b[3];
}
- uint8_t h = 0;
- for (int i = 0; i < D * 4; i++) {
+ U8 h = 0;
+ for (Int i = 0; i < D * 4; i++) {
h = hash(h) + to_hash[i];
}
return to_float(h);
diff --git a/src/Math/Rotation.hpp b/src/Math/Rotation.hpp
index a3420ed..83c1109 100644
--- a/src/Math/Rotation.hpp
+++ b/src/Math/Rotation.hpp
@@ -8,9 +8,9 @@ struct Rotation {
Rotation(Vector<3> vector) : vector{ wrap(vector) } {}
- explicit Rotation(float angles[3]) : Rotation(angles[0], angles[1], angles[2]) {}
+ explicit Rotation(Real angles[3]) : Rotation(angles[0], angles[1], angles[2]) {}
- Rotation(float pitch, float yaw, float roll) {
+ Rotation(Real pitch, Real yaw, Real roll) {
vector = wrap({pitch, yaw, roll });
}
@@ -26,15 +26,15 @@ struct Rotation {
return v.map([](auto a) { return fmod(a, 360.0f); });
}
- float& pitch() {
+ Real& pitch() {
return vector[0];
}
- float& yaw() {
+ Real& yaw() {
return vector[1];
}
- float& roll() {
+ Real& roll() {
return vector[2];
}
diff --git a/src/Math/Tensor.hpp b/src/Math/Tensor.hpp
index e373c1f..95c4548 100644
--- a/src/Math/Tensor.hpp
+++ b/src/Math/Tensor.hpp
@@ -1,14 +1,14 @@
#pragma once
-template<size_t O, typename T, size_t ...S>
+template<uint O, typename T, uint ...S>
struct Tensor {
- static constexpr size_t element_size = (S*...);
+ static constexpr USize element_size = (S*...);
template<typename ...Args>
- using EnableArgs = std::enable_if_t<sizeof...(Args) == O, bool>;
+ using EnableArgs = std::enable_if_t<sizeof...(Args) == O, Bool>;
template<typename ...Args>
- using EnableArgsPerElement = std::enable_if_t<sizeof...(Args) == element_size, bool>;
- using Enable = std::enable_if_t<sizeof...(S) == O, bool>;
+ using EnableArgsPerElement = std::enable_if_t<sizeof...(Args) == element_size, Bool>;
+ using Enable = std::enable_if_t<sizeof...(S) == O, Bool>;
template<Enable = true>
Tensor() : elements{} {}
@@ -19,7 +19,7 @@ struct Tensor {
}
template<Enable = true, typename ...Args, EnableArgsPerElement<Args...> = true>
- Tensor(Args... args) : elements{ args... } {}
+ Tensor(Args... args) : elements{ static_cast<T>(args)... } {}
template<typename ...Args, EnableArgs<Args...> = true>
auto& operator()(Args... args) {
@@ -27,12 +27,12 @@ struct Tensor {
}
template<typename ...Args, EnableArgs<Args...> = true>
- static constexpr size_t pos(Args... args) {
- size_t positions[O] = {static_cast<size_t>(args)...};
- size_t dimensions[O] = {S...};
+ static constexpr USize pos(Args... args) {
+ USize positions[O] = {static_cast<USize>(args)...};
+ USize dimensions[O] = {S...};
- size_t p = 0;
- for (int i = 0; i < O; i++) {
+ USize p = 0;
+ for (Int i = 0; i < O; i++) {
p *= dimensions[i];
p += positions[i];
}
diff --git a/src/Math/Vector.hpp b/src/Math/Vector.hpp
index f6bdd7a..3d5a5be 100644
--- a/src/Math/Vector.hpp
+++ b/src/Math/Vector.hpp
@@ -1,15 +1,15 @@
#pragma once
-#include <cstddef>
+#include "../Common/Sizes.hpp"
#include <sstream>
#include <cmath>
-template <size_t S, typename T = float>
+template <uint S, typename T = Real>
struct Vector {
Vector(): elements{} {}
- template<typename ...Args, std::enable_if_t<sizeof...(Args) == S, int> = 0>
- Vector(Args... args) : elements{ args... } {}
+ template<typename ...Args, std::enable_if_t<sizeof...(Args) == S, Int> = 0>
+ Vector(Args... args) : elements{ static_cast<T>(args)... } {}
explicit Vector(const T values[S]) {
std::copy(values, values + S, elements);
@@ -24,7 +24,7 @@ struct Vector {
elements[S - 1] = scalar;
}
- template<size_t N, std::enable_if_t<(N > S), int> = 0>
+ template<uint N, std::enable_if_t<(N > S), Int> = 0>
explicit Vector(const Vector<N, T> vector) {
std::copy(vector.elements, vector.elements + S, elements);
}
@@ -32,7 +32,7 @@ struct Vector {
template<typename F>
Vector map(F f) const {
Vector result{};
- for (int i = 0; i < S; i++) {
+ for (Int i = 0; i < S; i++) {
result[i] = f(elements[i]);
}
return result;
@@ -41,7 +41,7 @@ struct Vector {
template<typename F>
Vector map_indexed(F f) const {
Vector result{};
- for (int i = 0; i < S; i++) {
+ for (Int i = 0; i < S; i++) {
result[i] = f(i, elements[i]);
}
return result;
@@ -50,7 +50,7 @@ struct Vector {
template<typename F>
T reduce(F f) const {
T result = elements[0];
- for (int i = 1; i < S; i++) {
+ for (Int i = 1; i < S; i++) {
result = f(result, elements[i]);
}
return result;
@@ -92,11 +92,11 @@ struct Vector {
};
}
- T operator[](size_t index) const {
+ T operator[](USize index) const {
return elements[index];
}
- T& operator[](size_t index) {
+ T& operator[](USize index) {
return elements[index];
}
@@ -128,8 +128,8 @@ struct Vector {
return map([=](auto x) { return x / scalar; });
}
- bool operator==(const Vector& other) {
- for (int i = 0; i < S; i++) {
+ Bool operator==(const Vector& other) {
+ for (Int i = 0; i < S; i++) {
if (elements[i] != other[i]) {
return false;
}
@@ -137,7 +137,7 @@ struct Vector {
return true;
}
- bool operator!=(const Vector& other) {
+ Bool operator!=(const Vector& other) {
return !this->operator==(other);
}
@@ -162,7 +162,7 @@ struct Vector {
std::stringstream str{};
str << "[ ";
- for (int i = 0; i < S; i++) {
+ for (Int i = 0; i < S; i++) {
str << elements[i] << " ";
}
str << "]";