feat: added template for vec so further demensions will be easier to
implement
This commit is contained in:
153
fastmath.hpp
153
fastmath.hpp
@@ -3,8 +3,10 @@
|
|||||||
|
|
||||||
#include <cmath>
|
#include <cmath>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <sstream>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <vector>
|
||||||
#define SHIFT_AMOUNT 16
|
#define SHIFT_AMOUNT 16
|
||||||
#define HALF_SHIFT (SHIFT_AMOUNT / 2)
|
#define HALF_SHIFT (SHIFT_AMOUNT / 2)
|
||||||
|
|
||||||
@@ -20,6 +22,7 @@ struct decimal {
|
|||||||
|
|
||||||
const int32_t i;
|
const int32_t i;
|
||||||
|
|
||||||
|
decimal() : i(0) {}
|
||||||
decimal(float i) : i(TO_INT(i)) {}
|
decimal(float i) : i(TO_INT(i)) {}
|
||||||
decimal(double i) : i(TO_INT(i)) {}
|
decimal(double i) : i(TO_INT(i)) {}
|
||||||
decimal(int32_t i) : i(i) {}
|
decimal(int32_t i) : i(i) {}
|
||||||
@@ -71,70 +74,122 @@ struct decimal {
|
|||||||
float to_float() { return TO_FLOAT(i); }
|
float to_float() { return TO_FLOAT(i); }
|
||||||
};
|
};
|
||||||
|
|
||||||
struct vec3 {
|
template <int n, class Dev> struct vec {
|
||||||
const decimal x;
|
|
||||||
const decimal y;
|
|
||||||
const decimal z;
|
|
||||||
|
|
||||||
vec3(float x, float y, float z)
|
vec(decimal newV[n]) {
|
||||||
: x(decimal(x)), y(decimal(y)), z(decimal(z)) {}
|
for (int i = 0; i < n; i++) {
|
||||||
|
v[i] = newV[i];
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
return *this;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
friend std::ostream &operator<<(std::ostream &os, const vec3 &v) {
|
vec(std::vector<decimal> newV) {
|
||||||
return (os << "(" << v.x << ", " << v.y << ", " << v.z << ")"
|
for (int i = 0; i < n; i++) {
|
||||||
<< std::endl);
|
v[i] = newV[i];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
vec3 cross(const vec3 &v) {
|
vec() : v{} {}
|
||||||
return vec3((y * v.z) - (z * v.y), (z * v.x) - (x * v.z),
|
|
||||||
(x * v.y) - (y * v.x));
|
friend Dev operator+(const vec<n, Dev> &v1, const vec<n, Dev> &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<n, Dev> &v1, const vec<n, Dev> &v2) {
|
||||||
|
Dev newV = {};
|
||||||
|
|
||||||
|
for (int i = 0; i < n; i++) {
|
||||||
|
newV.v[i] = v1.v[i] - v2.v[i];
|
||||||
|
}
|
||||||
|
return static_cast<Dev>(newV);
|
||||||
|
}
|
||||||
|
|
||||||
|
friend std::ostream &operator<<(std::ostream &os, const vec<n, Dev> &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<n, Dev> &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<n, Dev> &v) {
|
||||||
|
return v * d;
|
||||||
|
}
|
||||||
|
|
||||||
|
decimal operator*(const vec<n, Dev> &vec) {
|
||||||
|
decimal res;
|
||||||
|
for (int i = 0; i < n; i++) {
|
||||||
|
res += vec.v[i] * v[i];
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
friend bool operator==(const vec<n, Dev> &v1, const vec<n, Dev> &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_sq() { return *this * *this; }
|
||||||
|
|
||||||
decimal len() { return this->len_sq().sqrt(); }
|
decimal len() { return this->len_sq().sqrt(); }
|
||||||
|
|
||||||
vec3 normalize() {
|
Dev normalize() {
|
||||||
decimal f = decimal(1.0) / this->len();
|
decimal f = decimal(1.0) / this->len();
|
||||||
return (*this * f);
|
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
|
#endif
|
||||||
|
|||||||
@@ -2,7 +2,6 @@
|
|||||||
#define POLYGON_H
|
#define POLYGON_H
|
||||||
|
|
||||||
#include "fastmath.hpp"
|
#include "fastmath.hpp"
|
||||||
|
|
||||||
struct polygon {
|
struct polygon {
|
||||||
|
|
||||||
const vec3 points[3];
|
const vec3 points[3];
|
||||||
@@ -15,7 +14,7 @@ struct polygon {
|
|||||||
int n = (i + 1) % 3;
|
int n = (i + 1) % 3;
|
||||||
|
|
||||||
vec3 d = points[n] - points[i];
|
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];
|
vec3 s = p - points[n];
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user