From 9ab061034720c1c0db4e8bff5dae4191ccd2a26f Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Sun, 2 Jun 2019 14:29:25 +0300 Subject: [PATCH] Add formatted output operators for vector and matrix classes --- source/linal/dynamicmatrix.h | 22 ++++++++++++++++++++++ source/linal/dynamicvector.h | 15 +++++++++++++++ source/linal/matrix.h | 22 ++++++++++++++++++++++ source/linal/vector.h | 15 +++++++++++++++ tests/boundingbox.cpp | 10 ---------- tests/matrix.cpp | 4 ++-- 6 files changed, 76 insertions(+), 12 deletions(-) diff --git a/source/linal/dynamicmatrix.h b/source/linal/dynamicmatrix.h index 1bbed27..6d45657 100644 --- a/source/linal/dynamicmatrix.h +++ b/source/linal/dynamicmatrix.h @@ -2,6 +2,7 @@ #define MSP_LINAL_DYNAMICMATRIX_H_ #include +#include #include "dynamicvector.h" #include "matrixops.h" @@ -317,6 +318,27 @@ inline DynamicMatrix invert(const DynamicMatrix &m) return r.invert(); } +template +inline std::ostream &operator<<(std::ostream &s, const DynamicMatrix &m) +{ + s << "DynamicMatrix" << m.rows() << 'x' << m.columns() << '('; + for(unsigned i=0; i #include +#include #include namespace Msp { @@ -243,6 +244,20 @@ inline DynamicVector normalize(const DynamicVector &v) return r.normalize(); } +template +inline std::ostream &operator<<(std::ostream &s, const DynamicVector &v) +{ + s << "DynamicVector" << v.size() << '('; + for(unsigned i=0; i +#include #include "vector.h" namespace Msp { @@ -260,6 +261,27 @@ inline Matrix transpose(const Matrix &m) return r; } +template +inline std::ostream &operator<<(std::ostream &s, const Matrix &m) +{ + s << "Matrix" << M << 'x' << N << '('; + for(unsigned i=0; i #include +#include namespace Msp { namespace LinAl { @@ -325,6 +326,20 @@ inline Vector cross(const Vector &v1, const Vector &v2) return Vector(v1.y*v2.z-v1.z*v2.y, v1.z*v2.x-v1.x*v2.z, v1.x*v2.y-v1.y*v2.x); } +template +inline std::ostream &operator<<(std::ostream &s, const Vector &v) +{ + s << "Vector" << N << '('; + for(unsigned i=0; i &vec) -{ - conv.result(format("[%g %g %g]", vec.x, vec.y, vec.z)); -} - -} } - BoundingBoxTests::BoundingBoxTests() { add(&BoundingBoxTests::simple_union, "Union"); diff --git a/tests/matrix.cpp b/tests/matrix.cpp index 10e3ffd..5b303db 100644 --- a/tests/matrix.cpp +++ b/tests/matrix.cpp @@ -35,8 +35,8 @@ void MatrixTests::multiply() LinAl::Matrix a(data); LinAl::Matrix b(data+6); - EXPECT(a*b == (LinAl::Matrix(data+12))); - EXPECT(b*a == (LinAl::Matrix(data+21))); + EXPECT_EQUAL(a*b, (LinAl::Matrix(data+12))); + EXPECT_EQUAL(b*a, (LinAl::Matrix(data+21))); } template -- 2.43.0