matrix: add various scalar operators

This commit is contained in:
Danny Robson 2014-08-19 20:46:00 +10:00
parent 3ab2e8ed57
commit 22dcf46c61
2 changed files with 57 additions and 0 deletions

View File

@ -294,6 +294,56 @@ matrix<T>::operator* (const matrix<T> &rhs) const {
} }
//-----------------------------------------------------------------------------
template <typename T>
vector<4>
matrix<T>::operator* (const vector<4> &rhs) const {
return vector<4> {
values[0][0] * rhs.x + values[0][1] * rhs.y + values[0][2] * rhs.z + values[0][3] * rhs.w,
values[1][0] * rhs.x + values[1][1] * rhs.y + values[1][2] * rhs.z + values[1][3] * rhs.w,
values[2][0] * rhs.x + values[2][1] * rhs.y + values[2][2] * rhs.z + values[2][3] * rhs.w,
values[3][0] * rhs.x + values[3][1] * rhs.y + values[3][2] * rhs.z + values[3][3] * rhs.w
};
}
//-----------------------------------------------------------------------------
template <typename T>
matrix<T>
matrix<T>::operator/ (T s) const {
matrix<T> m;
for (size_t r = 0; r < m.rows; ++r)
for (size_t c = 0; c < m.cols; ++c)
m.values[r][c] = values[r][c] / s;
return m;
}
//-----------------------------------------------------------------------------
template <typename T>
matrix<T>&
matrix<T>::operator/= (T s) {
for (size_t r = 0; r < rows; ++r)
for (size_t c = 0; c < cols; ++c)
values[r][c] /= s;
return *this;
}
//-----------------------------------------------------------------------------
template <typename T>
bool
matrix<T>::operator== (const matrix<T> &rhs) const {
for (size_t r = 0; r < rows; ++r)
for (size_t c = 0; c < cols; ++c)
if (!almost_equal (rhs.values[r][c], values[r][c]))
return false;
return true;
}
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
template <typename T> template <typename T>
util::point<3> util::point<3>

View File

@ -43,6 +43,13 @@ namespace util {
T det (void) const; T det (void) const;
matrix<T> operator* (const matrix<T>&) const; matrix<T> operator* (const matrix<T>&) const;
vector<4> operator* (const vector<4>&) const;
matrix<T> operator/ (T) const;
matrix<T>& operator/= (T);
bool operator== (const matrix<T>&) const;
point<3> to_local (const point<3> &p) const; point<3> to_local (const point<3> &p) const;
point<3> to_global (const point<3> &p) const; point<3> to_global (const point<3> &p) const;