summaryrefslogtreecommitdiff
path: root/src/types
diff options
context:
space:
mode:
Diffstat (limited to 'src/types')
-rw-r--r--src/types/Vec3D.cpp100
-rw-r--r--src/types/Vec3D.h9
2 files changed, 108 insertions, 1 deletions
diff --git a/src/types/Vec3D.cpp b/src/types/Vec3D.cpp
index 0d75e88..c70e540 100644
--- a/src/types/Vec3D.cpp
+++ b/src/types/Vec3D.cpp
@@ -1,18 +1,67 @@
#include "Vec3D.h"
#include <iomanip>
+static int GetPrecision(float f) {
+ int p = 0;
+ int shift = 1;
+ float approx = std::round(f);
+
+ while(f != approx && p < 12 ){
+ shift *= 10;
+ p++;
+ approx = std::round(f * shift) / shift;
+ }
+ return p;
+}
+
+static int GetMagnitude(float f) {
+ int w = 1;
+ float a = std::abs(f);
+
+ if(a >= 1) {
+ w += std::log10(a);
+ }
+ if(f < 0) {
+ w++;
+ }
+ return w;
+}
+
Vec3f::Vec3f(float xv, float yv, float zv) : x(xv), y(yv), z(zv) {}
+int Vec3f::precision() {
+ auto px = GetPrecision(this->x);
+ auto py = GetPrecision(this->y);
+ auto pz = GetPrecision(this->z);
+
+ return std::max(px, std::max(py, pz));
+}
+
+int Vec3f::width() {
+ auto wx = GetMagnitude(this->x);
+ auto wy = GetMagnitude(this->y);
+ auto wz = GetMagnitude(this->z);
+
+ return std::max(wx, std::max(wy, wz)) + 1 + this->precision();
+}
+
std::ostream& operator<< (std::ostream& stream, const Vec3f& vec) {
int width = stream.width();
stream << std::setw(0) << "{" << std::setw(width) << vec.x << ", " << std::setw(width) << vec.y << ", " << std::setw(width) << vec.z << "}";
-
return stream;
}
Vec3s::Vec3s(int16_t xv, int16_t yv, int16_t zv) : x(xv), y(yv), z(zv) {}
+int Vec3s::width() {
+ auto wx = GetMagnitude(this->x);
+ auto wy = GetMagnitude(this->y);
+ auto wz = GetMagnitude(this->z);
+
+ return std::max(wx, std::max(wy, wz));
+}
+
std::ostream& operator<< (std::ostream& stream, const Vec3s& vec) {
int width = stream.width();
@@ -22,6 +71,14 @@ std::ostream& operator<< (std::ostream& stream, const Vec3s& vec) {
Vec3i::Vec3i(int32_t xv, int32_t yv, int32_t zv) : x(xv), y(yv), z(zv) {}
+int Vec3i::width() {
+ auto wx = GetMagnitude(this->x);
+ auto wy = GetMagnitude(this->y);
+ auto wz = GetMagnitude(this->z);
+
+ return std::max(wx, std::max(wy, wz));
+}
+
std::ostream& operator<< (std::ostream& stream, const Vec3i& vec) {
int width = stream.width();
@@ -31,6 +88,20 @@ std::ostream& operator<< (std::ostream& stream, const Vec3i& vec) {
Vec2f::Vec2f(float xv, float zv) : x(xv), z(zv) {}
+int Vec2f::precision() {
+ auto px = GetPrecision(this->x);
+ auto pz = GetPrecision(this->z);
+
+ return std::max(px, pz);
+}
+
+int Vec2f::width() {
+ auto wx = GetMagnitude(this->x) + 1 + GetPrecision(this->x);
+ auto wz = GetMagnitude(this->z) + 1 + GetPrecision(this->z);
+
+ return std::max(wx, wz);
+}
+
std::ostream& operator<< (std::ostream& stream, const Vec2f& vec) {
int width = stream.width();
@@ -40,6 +111,24 @@ std::ostream& operator<< (std::ostream& stream, const Vec2f& vec) {
Vec4f::Vec4f(float xv, float yv, float zv, float wv) : x(xv), y(yv), z(zv), w(wv) {}
+int Vec4f::width() {
+ auto wx = GetMagnitude(this->x) + 1 + GetPrecision(this->x);
+ auto wy = GetMagnitude(this->y) + 1 + GetPrecision(this->y);
+ auto wz = GetMagnitude(this->z) + 1 + GetPrecision(this->z);
+ auto ww = GetMagnitude(this->w) + 1 + GetPrecision(this->w);
+
+ return std::max(std::max(wy, ww), std::max(wx, wz));
+}
+
+int Vec4f::precision() {
+ auto px = GetPrecision(this->x);
+ auto py = GetPrecision(this->y);
+ auto pz = GetPrecision(this->z);
+ auto pw = GetPrecision(this->w);
+
+ return std::max(std::max(px, pw), std::max(py, pz));
+}
+
std::ostream& operator<< (std::ostream& stream, const Vec4f& vec) {
int width = stream.width();
@@ -49,6 +138,15 @@ std::ostream& operator<< (std::ostream& stream, const Vec4f& vec) {
Vec4s::Vec4s(int16_t xv, int16_t yv, int16_t zv, int16_t wv) : x(xv), y(yv), z(zv), w(wv) {}
+int Vec4s::width() {
+ auto wx = GetMagnitude(this->x);
+ auto wy = GetMagnitude(this->y);
+ auto wz = GetMagnitude(this->z);
+ auto ww = GetMagnitude(this->w);
+
+ return std::max(std::max(wx, ww), std::max(wy, wz));
+}
+
std::ostream& operator<< (std::ostream& stream, const Vec4s& vec) {
int width = stream.width();
diff --git a/src/types/Vec3D.h b/src/types/Vec3D.h
index e861f12..8215730 100644
--- a/src/types/Vec3D.h
+++ b/src/types/Vec3D.h
@@ -9,6 +9,8 @@ public:
float z;
Vec3f(float xv = 0, float yv = 0, float zv = 0);
+ int width();
+ int precision();
friend std::ostream& operator<< (std::ostream& stream, const Vec3f& vec);
};
@@ -19,6 +21,7 @@ public:
int16_t z;
Vec3s(int16_t xv = 0, int16_t yv = 0, int16_t zv = 0);
+ int width();
friend std::ostream& operator<< (std::ostream& stream, const Vec3s& vec);
};
@@ -29,6 +32,7 @@ public:
int32_t z;
Vec3i(int32_t xv = 0, int32_t yv = 0, int32_t zv = 0);
+ int width();
friend std::ostream& operator<< (std::ostream& stream, const Vec3i& vec);
};
@@ -38,6 +42,8 @@ public:
float z;
Vec2f(float xv = 0, float zv = 0);
+ int precision();
+ int width();
friend std::ostream& operator<< (std::ostream& stream, const Vec2f& vec);
};
@@ -49,6 +55,8 @@ public:
float w;
Vec4f(float xv = 0, float yv = 0, float zv = 0, float wv = 0);
+ int precision();
+ int width();
friend std::ostream& operator<< (std::ostream& stream, const Vec4f& vec);
};
@@ -60,5 +68,6 @@ public:
int16_t w;
Vec4s(int16_t xv = 0, int16_t yv = 0, int16_t zv = 0, int16_t wv = 0);
+ int width();
friend std::ostream& operator<< (std::ostream& stream, const Vec4s& vec);
}; \ No newline at end of file