diff --git a/fastmath.hpp b/fastmath.hpp index 3dca49c..b90a147 100644 --- a/fastmath.hpp +++ b/fastmath.hpp @@ -3,8 +3,10 @@ #include #include +#include #include #include +#include #define SHIFT_AMOUNT 16 #define HALF_SHIFT (SHIFT_AMOUNT / 2) @@ -20,6 +22,7 @@ struct decimal { const int32_t i; + decimal() : i(0) {} decimal(float i) : i(TO_INT(i)) {} decimal(double i) : i(TO_INT(i)) {} decimal(int32_t i) : i(i) {} @@ -71,70 +74,122 @@ struct decimal { float to_float() { return TO_FLOAT(i); } }; -struct vec3 { - const decimal x; - const decimal y; - const decimal z; +template struct vec { - vec3(float x, float y, float z) - : x(decimal(x)), y(decimal(y)), z(decimal(z)) {} - - vec3(double x, double y, double z) - : x(decimal(x)), y(decimal(y)), z(decimal(z)) {} - - vec3(int32_t x, int32_t y, int32_t z) - : x(decimal(x)), y(decimal(y)), z(decimal(z)) {} - vec3(decimal x, decimal y, decimal z) : x(x), y(y), z(z) {} - - friend vec3 operator+(const vec3 &v1, const vec3 &v2) { - return vec3(v1.x + v2.x, v1.y + v2.y, v1.z + v2.z); - } - - friend vec3 operator-(const vec3 &v1, const vec3 &v2) { - return vec3(v1.x - v2.x, v1.y - v2.y, v1.z - v2.z); - } - - vec3 operator-() { return vec3(-x, -y, -z); } - - friend vec3 operator*(const vec3 &v, const decimal &n) { - int32_t f = n.i >> HALF_SHIFT; - return vec3({(v.x.i >> HALF_SHIFT) * f}, {(v.y.i >> HALF_SHIFT) * f}, - {(v.z.i >> HALF_SHIFT) * f}); - } - friend vec3 operator*(const decimal &n, const vec3 &v) { return v * n; } - - decimal operator*(const vec3 &v) { return v.x * x + v.y * y + v.z * z; } - - friend bool operator==(const vec3 &v1, const vec3 &v2) { - return (v1.x == v2.x) & (v1.y == v2.y) & (v1.z == v2.z); - } - - vec3 &operator=(vec3 const &in) { - if (this != &in) { - std::destroy_at(this); - std::construct_at(this, in); + vec(decimal newV[n]) { + for (int i = 0; i < n; i++) { + v[i] = newV[i]; } - return *this; } - friend std::ostream &operator<<(std::ostream &os, const vec3 &v) { - return (os << "(" << v.x << ", " << v.y << ", " << v.z << ")" - << std::endl); + vec(std::vector newV) { + for (int i = 0; i < n; i++) { + v[i] = newV[i]; + } } - vec3 cross(const vec3 &v) { - return vec3((y * v.z) - (z * v.y), (z * v.x) - (x * v.z), - (x * v.y) - (y * v.x)); + vec() : v{} {} + + friend Dev operator+(const vec &v1, const vec &v2) { + Dev newV = {}; + + for (int i = 0; i < n; i++) { + newV.v[i] = v1.v[i] + v2.v[i]; + } + return newV; + } + + friend Dev operator-(const vec &v1, const vec &v2) { + Dev newV = {}; + + for (int i = 0; i < n; i++) { + newV.v[i] = v1.v[i] - v2.v[i]; + } + return static_cast(newV); + } + + friend std::ostream &operator<<(std::ostream &os, const vec &v) { + os << "(" << v.v[0]; + for (int i = 1; i < n; i++) { + os << ", " << v.v[i]; + } + return (os << ")" << std::endl); + } + + Dev operator-() { + Dev newV = {}; + for (int i = 0; i < n; i++) { + newV.v[i] = -v[i]; + } + return newV; + } + + friend Dev operator*(const vec &v, const decimal &d) { + int32_t f = d.i >> HALF_SHIFT; + + Dev newV = {}; + for (int i = 0; i < n; i++) { + newV.v[i] = (v.v[i].i >> HALF_SHIFT) * f; + } + return newV; + } + + friend Dev operator*(const decimal &d, const vec &v) { + return v * d; + } + + decimal operator*(const vec &vec) { + decimal res; + for (int i = 0; i < n; i++) { + res += vec.v[i] * v[i]; + } + return res; + } + + friend bool operator==(const vec &v1, const vec &v2) { + bool res = true; + for (int i = 0; i < n; i++) { + res &= v1.v[i] == v2.v[i]; + } + return res; } decimal len_sq() { return *this * *this; } decimal len() { return this->len_sq().sqrt(); } - vec3 normalize() { + Dev normalize() { decimal f = decimal(1.0) / this->len(); return (*this * f); } + + protected: + decimal v[n]; }; +struct vec3 : public vec<3, vec3> { + + vec3() : vec<3, vec3>() {} + + vec3(float x, float y, float z) + : vec<3, vec3>({decimal(x), decimal(y), decimal(z)}) {} + + vec3(double x, double y, double z) + : vec<3, vec3>({decimal(x), decimal(y), decimal(z)}) {} + + vec3(int32_t x, int32_t y, int32_t z) + : vec<3, vec3>({decimal(x), decimal(y), decimal(z)}) {} + + vec3(decimal x, decimal y, decimal z) : vec<3, vec3>({x, y, z}) {} + + decimal x() const { return v[0]; } + decimal y() const { return v[1]; } + decimal z() const { return v[2]; } + + vec3 cross(const vec3 &v) { + return vec3((y() * v.z()) - (z() * v.y()), + (z() * v.x()) - (x() * v.z()), + (x() * v.y()) - (y() * v.x())); + } +}; #endif diff --git a/polygon.hpp b/polygon.hpp index c683986..1b4aed1 100644 --- a/polygon.hpp +++ b/polygon.hpp @@ -2,7 +2,6 @@ #define POLYGON_H #include "fastmath.hpp" - struct polygon { const vec3 points[3]; @@ -15,7 +14,7 @@ struct polygon { int n = (i + 1) % 3; vec3 d = points[n] - points[i]; - d = vec3(d.y, -d.x, d.z); + d = vec3(d.y(), -d.x(), d.z()); vec3 s = p - points[n];