Implement more robust * and / for Matrix

This commit is contained in:
Eryn Wells 2014-08-08 23:05:24 -07:00
parent be15d553f1
commit 9b076f1533

View file

@ -48,6 +48,10 @@ struct Matrix
/** Scalar multiplication */ /** Scalar multiplication */
Matrix<N,M> operator*(const Double& rhs) const; Matrix<N,M> operator*(const Double& rhs) const;
Matrix<N,M>& operator*=(const Double& rhs);
Matrix<N,M> operator/(const Double& rhs) const;
Matrix<N,M>& operator/=(const Double& rhs);
/** Matrix multiplication */ /** Matrix multiplication */
template<UInt P> template<UInt P>
@ -62,6 +66,12 @@ protected:
}; };
/** Scalar multiplication, scalar factor on the left. */
template<UInt N, UInt M>
Matrix<N,M> operator*(const Double& lhs, const Matrix<N,M>& rhs);
/** A standard 4x4 matrix. */
typedef Matrix<4> Matrix4; typedef Matrix<4> Matrix4;
@ -71,11 +81,7 @@ typedef Matrix<4> Matrix4;
*/ */
static Matrix4 TranslationMatrix(const Double& x, const Double& y, const Double& z); static Matrix4 TranslationMatrix(const Double& x, const Double& y, const Double& z);
#pragma mark Static Methods
/** Scalar multiplication, scalar factor on the left. */
template<UInt N, UInt M>
Matrix<N,M> operator*(const Double& lhs, const Matrix<N,M>& rhs);
/* /*
* charles::basics::Matrix<>::Zero -- * charles::basics::Matrix<>::Zero --
@ -106,6 +112,7 @@ Matrix<N,M>::Identity()
return m; return m;
} }
#pragma mark Instance Methods
/* /*
* charles::basics::Matrix<>::Matrix -- * charles::basics::Matrix<>::Matrix --
@ -197,11 +204,35 @@ Matrix<N,M>
Matrix<N,M>::operator*(const Double& rhs) Matrix<N,M>::operator*(const Double& rhs)
const const
{ {
Matrix<N,M> result; return Matrix<N,M>(*this) *= rhs;
for (int i = 0; i < N*M; i++) { }
result.mData = mData[i] * rhs;
template<UInt N, UInt M>
Matrix<N,M>&
Matrix<N,M>::operator*=(const Double& rhs)
{
for (size_t i = 0; i < N*M; i++) {
mData[i] *= rhs;
} }
return result; return *this;
}
template<UInt N, UInt M>
Matrix<N,M>
Matrix<N,M>::operator/(const Double& rhs)
const
{
return Matrix<N,M>(*this) /= rhs;
}
template<UInt N, UInt M>
Matrix<N,M>&
Matrix<N,M>::operator/=(const Double& rhs)
{
return *this *= (1.0 / rhs);
} }