From 9b076f153329ea4a44800ef451c3a1ba5fc2d3b0 Mon Sep 17 00:00:00 2001 From: Eryn Wells Date: Fri, 8 Aug 2014 23:05:24 -0700 Subject: [PATCH] Implement more robust * and / for Matrix --- src/basics/matrix.hh | 49 ++++++++++++++++++++++++++++++++++++-------- 1 file changed, 40 insertions(+), 9 deletions(-) diff --git a/src/basics/matrix.hh b/src/basics/matrix.hh index 1bb0945..5e7682f 100644 --- a/src/basics/matrix.hh +++ b/src/basics/matrix.hh @@ -48,6 +48,10 @@ struct Matrix /** Scalar multiplication */ Matrix operator*(const Double& rhs) const; + Matrix& operator*=(const Double& rhs); + + Matrix operator/(const Double& rhs) const; + Matrix& operator/=(const Double& rhs); /** Matrix multiplication */ template @@ -62,6 +66,12 @@ protected: }; +/** Scalar multiplication, scalar factor on the left. */ +template +Matrix operator*(const Double& lhs, const Matrix& rhs); + + +/** A standard 4x4 matrix. */ typedef Matrix<4> Matrix4; @@ -71,11 +81,7 @@ typedef Matrix<4> Matrix4; */ static Matrix4 TranslationMatrix(const Double& x, const Double& y, const Double& z); - -/** Scalar multiplication, scalar factor on the left. */ -template -Matrix operator*(const Double& lhs, const Matrix& rhs); - +#pragma mark Static Methods /* * charles::basics::Matrix<>::Zero -- @@ -106,6 +112,7 @@ Matrix::Identity() return m; } +#pragma mark Instance Methods /* * charles::basics::Matrix<>::Matrix -- @@ -197,11 +204,35 @@ Matrix Matrix::operator*(const Double& rhs) const { - Matrix result; - for (int i = 0; i < N*M; i++) { - result.mData = mData[i] * rhs; + return Matrix(*this) *= rhs; +} + + +template +Matrix& +Matrix::operator*=(const Double& rhs) +{ + for (size_t i = 0; i < N*M; i++) { + mData[i] *= rhs; } - return result; + return *this; +} + + +template +Matrix +Matrix::operator/(const Double& rhs) + const +{ + return Matrix(*this) /= rhs; +} + + +template +Matrix& +Matrix::operator/=(const Double& rhs) +{ + return *this *= (1.0 / rhs); }