From 60887138ff52e6be62fa2b2e837294d1f8de78c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20=E2=80=98Touky=E2=80=99=20Huet?= Date: Mon, 16 Jun 2014 03:42:21 +0000 Subject: [PATCH] Added tostring to vectors, similar to printf, but with tostring. --- src/lol/math/vector.h | 6 +++- src/math/vector.cpp | 80 +++++++++++++++++++++++-------------------- 2 files changed, 47 insertions(+), 39 deletions(-) diff --git a/src/lol/math/vector.h b/src/lol/math/vector.h index e4982a55..60ab8905 100644 --- a/src/lol/math/vector.h +++ b/src/lol/math/vector.h @@ -178,7 +178,8 @@ template struct XVec4 return ret; \ } \ \ - void printf() const; + void printf() const; \ + String tostring() const; /* * 2-element vectors @@ -1616,6 +1617,7 @@ template struct Mat2 } void printf() const; + String tostring() const; template friend std::ostream &operator<<(std::ostream &stream, Mat2 const &m); @@ -1742,6 +1744,7 @@ template struct Mat3 } void printf() const; + String tostring() const; template friend std::ostream &operator<<(std::ostream &stream, Mat3 const &m); @@ -1915,6 +1918,7 @@ template struct Mat4 static Mat4 shifted_perspective(T fov_y, T screen_size, T screen_ratio_yx, T near, T far); void printf() const; + String tostring() const; template friend std::ostream &operator<<(std::ostream &stream, Mat4 const &m); diff --git a/src/math/vector.cpp b/src/math/vector.cpp index 235cadc8..b59256fa 100644 --- a/src/math/vector.cpp +++ b/src/math/vector.cpp @@ -171,52 +171,33 @@ template<> mat4 inverse(mat4 const &mat) return ret; } -template<> void vec2::printf() const -{ - Log::Debug("[ %6.6f %6.6f ]\n", x, y); -} - -template<> void ivec2::printf() const -{ - Log::Debug("[ %i %i ]\n", x, y); -} - -template<> void cmplx::printf() const -{ - Log::Debug("[ %6.6f %6.6f ]\n", x, y); -} - -template<> void vec3::printf() const -{ - Log::Debug("[ %6.6f %6.6f %6.6f ]\n", x, y, z); -} - -template<> void ivec3::printf() const -{ - Log::Debug("[ %i %i %i ]\n", x, y, z); -} +#define LOL_PRINTF_TOSTRING(type, ...) \ +template<> void type::printf() const { Log::Debug(__VA_ARGS__); } \ +template<> String type::tostring() const { return String::Printf(__VA_ARGS__); } + +LOL_PRINTF_TOSTRING(vec2, "[ %6.6f %6.6f ]\n", x, y); +LOL_PRINTF_TOSTRING(ivec2, "[ %i %i ]\n", x, y); +LOL_PRINTF_TOSTRING(cmplx, "[ %6.6f %6.6f ]\n", x, y); +LOL_PRINTF_TOSTRING(vec3, "[ %6.6f %6.6f %6.6f ]\n", x, y, z); +LOL_PRINTF_TOSTRING(ivec3, "[ %i %i %i ]\n", x, y, z); +LOL_PRINTF_TOSTRING(vec4, "[ %6.6f %6.6f %6.6f %6.6f ]\n", x, y, z, w); +LOL_PRINTF_TOSTRING(ivec4, "[ %i %i %i %i ]\n", x, y, z, w); +LOL_PRINTF_TOSTRING(quat, "[ %6.6f %6.6f %6.6f %6.6f ]\n", w, x, y, z); -template<> void vec4::printf() const -{ - Log::Debug("[ %6.6f %6.6f %6.6f %6.6f ]\n", x, y, z, w); -} - -template<> void ivec4::printf() const +template<> void mat2::printf() const { - Log::Debug("[ %i %i %i %i ]\n", x, y, z, w); -} + mat2 const &p = *this; -template<> void quat::printf() const -{ - Log::Debug("[ %6.6f %6.6f %6.6f %6.6f ]\n", w, x, y, z); + Log::Debug("[ %6.6f %6.6f\n", p[0][0], p[1][0]); + Log::Debug(" %6.6f %6.6f ]\n", p[0][1], p[1][1]); } -template<> void mat2::printf() const +template<> String mat2::tostring() const { mat2 const &p = *this; - Log::Debug("[ %6.6f %6.6f\n", p[0][0], p[1][0]); - Log::Debug(" %6.6f %6.6f ]\n", p[0][1], p[1][1]); + return String::Printf("[ %6.6f %6.6f\n", p[0][0], p[1][0]) + + String::Printf(" %6.6f %6.6f ]\n", p[0][1], p[1][1]); } template<> void mat3::printf() const @@ -228,6 +209,15 @@ template<> void mat3::printf() const Log::Debug(" %6.6f %6.6f %6.6f ]\n", p[0][2], p[1][2], p[2][2]); } +template<> String mat3::tostring() const +{ + mat3 const &p = *this; + + return String::Printf("[ %6.6f %6.6f %6.6f\n", p[0][0], p[1][0], p[2][0]) + + String::Printf(" %6.6f %6.6f %6.6f\n", p[0][1], p[1][1], p[2][1]) + + String::Printf(" %6.6f %6.6f %6.6f ]\n", p[0][2], p[1][2], p[2][2]); +} + template<> void mat4::printf() const { mat4 const &p = *this; @@ -242,6 +232,20 @@ template<> void mat4::printf() const p[0][3], p[1][3], p[2][3], p[3][3]); } +template<> String mat4::tostring() const +{ + mat4 const &p = *this; + + return String::Printf("[ %6.6f %6.6f %6.6f %6.6f\n", + p[0][0], p[1][0], p[2][0], p[3][0]) + + String::Printf(" %6.6f %6.6f %6.6f %6.6f\n", + p[0][1], p[1][1], p[2][1], p[3][1]) + + String::Printf(" %6.6f %6.6f %6.6f %6.6f\n", + p[0][2], p[1][2], p[2][2], p[3][2]) + + String::Printf(" %6.6f %6.6f %6.6f %6.6f ]\n", + p[0][3], p[1][3], p[2][3], p[3][3]); +} + template<> std::ostream &operator<<(std::ostream &stream, ivec2 const &v) { return stream << "(" << v.x << ", " << v.y << ")";