From 6f8e8da4662d8e01dd709d7480db3007c8bf2f3d Mon Sep 17 00:00:00 2001 From: Aitor Date: Fri, 10 Mar 2023 12:27:56 +0100 Subject: [PATCH] wip --- frontend/resources/public/wasm/NOTAS.md | 20 ++ frontend/resources/public/wasm/src/Box2.h | 54 +++++ frontend/resources/public/wasm/src/Color.h | 96 +++++++++ .../resources/public/wasm/src/Interpolation.h | 20 ++ frontend/resources/public/wasm/src/Matrix23.h | 105 ++++++++++ frontend/resources/public/wasm/src/Vector2.h | 197 ++++++++++++++++++ frontend/resources/public/wasm/src/main.cpp | 34 +++ 7 files changed, 526 insertions(+) create mode 100644 frontend/resources/public/wasm/NOTAS.md create mode 100644 frontend/resources/public/wasm/src/Box2.h create mode 100644 frontend/resources/public/wasm/src/Color.h create mode 100644 frontend/resources/public/wasm/src/Interpolation.h create mode 100644 frontend/resources/public/wasm/src/Matrix23.h create mode 100644 frontend/resources/public/wasm/src/Vector2.h create mode 100644 frontend/resources/public/wasm/src/main.cpp diff --git a/frontend/resources/public/wasm/NOTAS.md b/frontend/resources/public/wasm/NOTAS.md new file mode 100644 index 000000000..29e02fedf --- /dev/null +++ b/frontend/resources/public/wasm/NOTAS.md @@ -0,0 +1,20 @@ +# Notas + +- [ ] Mover todo esto a algún otro sitio mejor que no sea `resources/public`. +- [ ] Implementar tanto `clang-format` como `clang-tidy` para formatear el código. +- [ ] Implementar algún sistema de testing (Catch2, Google Test, CppUnit, etc). +- [ ] Implementar CMake para construir el proyecto. + +Para compilar el código en C++ se puede usar la siguiente línea: + +```sh +g++ -std=c++20 src/main.cpp -o main +``` + +## Documentos + +- [C++ Core Guidelines](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#S-introduction) + +## Recursos + +- [Compiling C to WebAssembly without Emscripten](https://surma.dev/things/c-to-webassembly/) diff --git a/frontend/resources/public/wasm/src/Box2.h b/frontend/resources/public/wasm/src/Box2.h new file mode 100644 index 000000000..44e28c5a1 --- /dev/null +++ b/frontend/resources/public/wasm/src/Box2.h @@ -0,0 +1,54 @@ +#pragma once + +#include "Vector2.h" + +template +struct Box2 +{ + Vector2 position; + Vector2 size; + + Box2(T x, T y, T width, T height) { + position.set(x, y); + size.set(width, height); + } + Box2(const Box2& box) : position(box.position), size(box.size) {} + Box2(const Vector2& position, const Vector2& size) : position(position), size(size) {} + + auto left() const + { + return position.x; + } + + auto right() const + { + return position.x + size.x; + } + + auto top() const + { + return position.y; + } + + auto bottom() const + { + return position.y + size.y; + } + + bool contains(const Vector2& point) const { + return point.x > position.x + && point.x < position.x + size.x + && point.y > position.y + && point.y < position.y + size.y; + } + + bool intersects(const Box2 &other) const { + if (left() > other.right() || right() < other.left()) + return false; + + if (top() > other.bottom() || bottom() < other.top()) + return false; + + return true; + } +}; diff --git a/frontend/resources/public/wasm/src/Color.h b/frontend/resources/public/wasm/src/Color.h new file mode 100644 index 000000000..5d8b59b0d --- /dev/null +++ b/frontend/resources/public/wasm/src/Color.h @@ -0,0 +1,96 @@ +#pragma once + +template +struct Color { + T r, g, b, a; + + Color& set(T new_r, T new_g, T new_b, T new_a) + { + r = new_r; + g = new_g; + b = new_b; + a = new_a; + return *this; + } + + Color& set(T new_r, T new_g, T new_b) + { + r = new_r; + g = new_g; + b = new_b; + return *this; + } + + Color operator+(const Color other) + { + return { + r + other.r, + g + other.g, + b + other.b + }; + } + + Color operator-(const Color other) + { + return { + r - other.r, + g - other.g, + b - other.b + }; + } + + Color operator*(const Color other) + { + return { + r * other.r, + g * other.g, + b * other.b + }; + } + + Color operator/(const Color other) + { + return { + r / other.r, + g / other.g, + b / other.b + }; + } + + + Color operator+(const T scalar) + { + return { + r + scalar, + g + scalar, + b + scalar + }; + } + + Color operator-(const T scalar) + { + return { + r - scalar, + g - scalar, + b - scalar + }; + } + + Color operator*(const T scalar) + { + return { + r * scalar, + g * scalar, + b * scalar + }; + } + + Color operator/(const T scalar) + { + return { + r / scalar, + g / scalar, + b / scalar + }; + } +} diff --git a/frontend/resources/public/wasm/src/Interpolation.h b/frontend/resources/public/wasm/src/Interpolation.h new file mode 100644 index 000000000..cd5241fcf --- /dev/null +++ b/frontend/resources/public/wasm/src/Interpolation.h @@ -0,0 +1,20 @@ +#pragma once + +namespace Interpolation { + + template + T linear(const T x, const T a, const T b) { + return (1 - x) * a + x * b; + } + + template + T quadratic(const T x, const T a, const T b, const T c) { + return linear(x, linear(x, a, b), linear(x, b, c)); + } + + template + T cubic(const T x, const T a, const T b, const T c, const T d) { + return linear(x, quadratic(x, a, b, c), quadratic(x, b, c, d)); + } + +} diff --git a/frontend/resources/public/wasm/src/Matrix23.h b/frontend/resources/public/wasm/src/Matrix23.h new file mode 100644 index 000000000..d56ed3ca3 --- /dev/null +++ b/frontend/resources/public/wasm/src/Matrix23.h @@ -0,0 +1,105 @@ +#pragma once + +#include +#include + +template +struct Matrix23 { + // a c tx + // b d ty + T a, b, c, d, tx, ty; + + Matrix23() : a(1), b(0), c(0), d(1), tx(0), ty(0) {} + Matrix23(T a, T b, T c, T d, T tx, T ty) : a(a), b(b), c(c), d(d), tx(tx), ty(ty) {} + Matrix23(const Matrix23& other) : a(other.a), b(other.b), c(other.c), d(other.d), tx(other.tx), ty(other.ty) {} + + auto determinant() const { + return a * d - b * c; + } + + Matrix23& identity() + { + a = 1; + b = 0; + c = 0; + d = 1; + tx = 0; + ty = 0; + return *this; + } + + Matrix23& translate(T x, T y) + { + tx += x; + ty += y; + return *this; + } + + Matrix23& scale(T x, T y) + { + a *= x; + b *= y; + c *= x; + d *= y; + return *this; + } + + Matrix23& rotate(auto angle) + { + auto cos = std::cos(angle); + auto sin = std::sin(angle); + + auto new_a = a * cos + c * sin; + auto new_b = b * cos + d * sin; + auto new_c = c * cos - a * sin; + auto new_d = d * cos - b * sin; + + a = new_a; + b = new_b; + c = new_c; + d = new_d; + + return *this; + } + + Matrix23 invert() + { + auto det = determinant(); + if (det == 0) + { + return *this; + } + auto inv_det = 1.0 / det; + return { + d * inv_det, + -b * inv_det, + -c * inv_det, + a * inv_det, + (c * ty - d * tx) * inv_det, + (b * tx - a * ty) * inv_det + }; + } + + Matrix23 operator*(const Matrix23& other) + { + // M N + // a c x a c x + // b d y x b d y = T + // 0 0 1 0 0 1 + return { + a * other.a + b * other.c, + a * other.b + b * other.d, + c * other.a + d * other.c, + c * other.b + d * other.d, + tx * other.a + ty * other.c + other.tx, + tx * other.b + ty * other.d + other.ty + }; + } +}; + +template +std::ostream &operator<<(std::ostream &os, const Matrix23 &matrix) +{ + os << "Matrix23(" << matrix.a << ", " << matrix.b << ", " << matrix.c << ", " << matrix.d << ", " << matrix.tx << ", " << matrix.ty << ")"; + return os; +} diff --git a/frontend/resources/public/wasm/src/Vector2.h b/frontend/resources/public/wasm/src/Vector2.h new file mode 100644 index 000000000..5a037e451 --- /dev/null +++ b/frontend/resources/public/wasm/src/Vector2.h @@ -0,0 +1,197 @@ +#pragma once + +#include +#include + +#include "Interpolation.h" + +template +struct Vector2 { + T x, y; + + Vector2() : x(0), y(0) {} + Vector2(T x, T y) : x(x), y(y) {} + Vector2(const Vector2& other) : x(other.x), y(other.y) {} + + auto dot(const Vector2 &other) const + { + return x * other.x + y * other.y; + } + + auto cross(const Vector2 &other) const + { + return x * other.y - y * other.x; + } + + auto direction() const + { + return std::atan2(y, x); + } + + auto length() const + { + return std::hypot(x, y); + } + + auto lengthSquared() const + { + return x * x + y * y; + } + + Vector2& normalize() + { + auto l = length(); + return set(x / l, x / l); + } + + Vector2& perpLeft() + { + return set(y, -x); + } + + Vector2& perpRight() + { + return set(-y, x); + } + + Vector2& rotate(auto rotation) + { + auto c = std::cos(rotation); + auto s = std::sin(rotation); + return set( + c * x - s * y, + s * x + c * y + ); + } + + Vector2& scale(auto s) + { + return set( + x * s, + y * s + ); + } + + Vector2& set(T newX, T newY) + { + x = newX; + y = newY; + return *this; + } + + Vector2& copy(const Vector2& other) + { + return set(other.x, other.y); + } + + Vector2& linear(auto p, Vector2& a, Vector2& b) + { + return set( + Interpolation::linear(p, a.x, b.x), + Interpolation::linear(p, a.y, b.y) + ); + } + + Vector2& quadratic(auto p, Vector2& a, Vector2& b, Vector2& c) + { + return set( + Interpolation::quadratic(p, a.x, b.x, c.x), + Interpolation::quadratic(p, a.y, b.y, c.y) + ); + } + + Vector2& cubic(auto p, Vector2& a, Vector2& b, Vector2& c, Vector2& d) + { + return set( + Interpolation::cubic(p, a.x, b.x, c.x, d.x), + Interpolation::cubic(p, a.y, b.y, c.y, d.y) + ); + } + + Vector2 operator*(const Matrix23 m) + { + return { + x * m.a + y * m.c + m.tx, + x * m.b + y * m.d + m.ty + }; + } + + Vector2 operator+(const Vector2 other) + { + return { + x + other.x, + y + other.y + }; + } + + Vector2 operator-(const Vector2 other) + { + return { + x - other.x, + y - other.y + }; + } + + Vector2 operator*(const Vector2 other) + { + return { + x * other.x, + y * other.y + }; + } + + Vector2 operator/(const Vector2 other) + { + return { + x / other.x, + y / other.y + }; + } + + Vector2 operator+(T scalar) + { + return { + x + scalar, + y + scalar + }; + } + + Vector2 operator-(T scalar) + { + return { + x - scalar, + y - scalar + }; + } + + Vector2 operator*(T scalar) + { + return { + x * scalar, + y * scalar + }; + } + + Vector2 operator/(T scalar) + { + return { + x / scalar, + y / scalar + }; + } + + Vector2 operator-() const + { + return { + -x, + -y + }; + } +}; + +template +std::ostream& operator<<(std::ostream &os, const Vector2& vector) +{ + os << "Vector2(" << vector.x << ", " << vector.y << ")"; + return os; +} diff --git a/frontend/resources/public/wasm/src/main.cpp b/frontend/resources/public/wasm/src/main.cpp new file mode 100644 index 000000000..6ed215545 --- /dev/null +++ b/frontend/resources/public/wasm/src/main.cpp @@ -0,0 +1,34 @@ +#include +#include + +#include "Interpolation.h" +#include "Matrix23.h" +#include "Vector2.h" +#include "Box2.h" + +void resize() +{ + +} + +int main(int argc, char** argv) +{ + Vector2 a(1, 0); + Vector2 b(0, 1); + + Matrix23 m; + + std::cout << m << std::endl; + + std::cout << m.rotate(M_PI / 2) << std::endl; + + Vector2 a2 = a * m; + Vector2 b2 = b * m; + + std::cout << a << ", " << a2 << std::endl; + std::cout << b << ", " << b2 << std::endl; + + std::cout << "Hello, World!" << std::endl; + + return 0; +}