summaryrefslogtreecommitdiff
path: root/hw6/src/mat3d.cpp
diff options
context:
space:
mode:
author53hornet <53hornet@gmail.com>2019-02-02 23:33:15 -0500
committer53hornet <53hornet@gmail.com>2019-02-02 23:33:15 -0500
commitdb072ad4dc181eca5a1458656b130beb43f475bf (patch)
treea3c03c7f5497cb70503e2486662fa85cfb53415a /hw6/src/mat3d.cpp
downloadcsci427-master.tar.xz
csci427-master.zip
Diffstat (limited to 'hw6/src/mat3d.cpp')
-rw-r--r--hw6/src/mat3d.cpp260
1 files changed, 260 insertions, 0 deletions
diff --git a/hw6/src/mat3d.cpp b/hw6/src/mat3d.cpp
new file mode 100644
index 0000000..7674777
--- /dev/null
+++ b/hw6/src/mat3d.cpp
@@ -0,0 +1,260 @@
+/******************************************************************/
+/* This file is part of the homework assignments for CSCI-427/527 */
+/* at The College of William & Mary and authored by Pieter Peers. */
+/* No part of this file, whether altered or in original form, can */
+/* be distributed or used outside the context of CSCI-427/527 */
+/* without consent of either the College of William & Mary or */
+/* Pieter Peers. */
+/******************************************************************/
+#include <cassert>
+#include <algorithm>
+
+#include "mat3d.h"
+
+
+/////////////////
+// Constructor //
+/////////////////
+mat3d::mat3d(void)
+{
+ clear(0.0f);
+}
+
+
+mat3d::mat3d(mat3d::value_type diag)
+ : mat3d()
+{
+ setDiagonal(diag);
+}
+
+
+mat3d::mat3d(const vec3d& X, const vec3d& Y, const vec3d& Z)
+{
+ for(size_type i=0; i < 3; i++)
+ {
+ this->operator()(i,0) = X[i];
+ this->operator()(i,1) = Y[i];
+ this->operator()(i,2) = Z[i];
+ }
+}
+
+
+mat3d::mat3d(const mat3d& m)
+{
+ _data = m._data;
+}
+
+
+////////////////
+// Inspectors //
+////////////////
+mat3d::iterator mat3d::begin(void)
+{
+ return _data.begin();
+}
+
+
+mat3d::const_iterator mat3d::begin(void) const
+{
+ return _data.begin();
+}
+
+
+mat3d::iterator mat3d::end(void)
+{
+ return _data.end();
+}
+
+
+mat3d::const_iterator mat3d::end(void) const
+{
+ return _data.end();
+}
+
+
+//////////////
+// Mutators //
+//////////////
+void mat3d::clear(mat3d::value_type value)
+{
+ std::fill(begin(), end(), value);
+}
+
+
+void mat3d::setDiagonal(mat3d::value_type value)
+{
+ (*this)(0,0) = (*this)(1,1) = (*this)(2,2) = value;
+}
+
+
+mat3d& mat3d::transpose(void)
+{
+ for(size_type row=0; row < height(); row++)
+ for(size_type column=row+1; column < width(); column++)
+ std::swap( (*this)(row,column), (*this)(column,row) );
+ return *this;
+}
+
+
+
+///////////////
+// Operators //
+///////////////
+mat3d& mat3d::operator=(const mat3d& m)
+{
+ _assign(m);
+ return *this;
+}
+
+
+mat3d::reference mat3d::operator()(mat3d::size_type row, mat3d::size_type col)
+{
+ assert(row < height() && col < width());
+ return _data[ row*width() + col];
+}
+
+
+mat3d::const_reference mat3d::operator()(mat3d::size_type row, mat3d::size_type col) const
+{
+ assert(row < height() && col < width());
+ return _data[ row*width() + col];
+}
+
+
+mat3d mat3d::operator+(const mat3d& m) const
+{
+ mat3d result;
+ std::transform(begin(), end(), m.begin(), result.begin(), [](const_reference a, const_reference b)
+ {
+ return a+b;
+ });
+ return result;
+}
+
+
+mat3d mat3d::operator-(const mat3d& m) const
+{
+ mat3d result;
+ std::transform(begin(), end(), m.begin(), result.begin(), [](const_reference a, const_reference b)
+ {
+ return a-b;
+ });
+ return result;
+}
+
+
+mat3d mat3d::operator*(const mat3d& m) const
+{
+ mat3d result;
+ for(size_type i=0; i < result.height(); i++)
+ for(size_type j=0; j < result.width(); j++)
+ for(size_type k=0; k < width(); k++)
+ result(i,j) += (*this)(i,k) * m(k, j);
+ return result;
+}
+
+
+vec3d mat3d::operator*(const vec3d& v) const
+{
+ vec3d result;
+ for(size_type i=0; i < height(); i++)
+ for(size_type j=0; j < width(); j++)
+ result[i] += v[j] * (*this)(i,j);
+ return result;
+}
+
+
+mat3d mat3d::operator*(mat3d::value_type scale) const
+{
+ mat3d result;
+ std::transform(begin(), end(), result.begin(), [&](const_reference v)
+ {
+ return v * scale;
+ });
+ return result;
+}
+
+mat3d mat3d::operator/(mat3d::value_type scale) const
+{
+ mat3d result;
+ std::transform(begin(), end(), result.begin(), [&](const_reference v)
+ {
+ return v / scale;
+ });
+ return result;
+}
+
+
+mat3d& mat3d::operator+=(const mat3d& m)
+{
+ std::transform(begin(), end(), m.begin(), begin(), [](const_reference a, const_reference b)
+ {
+ return a+b;
+ });
+ return *this;
+}
+
+
+mat3d& mat3d::operator-=(const mat3d& m)
+{
+ std::transform(begin(), end(), m.begin(), begin(), [](const_reference a, const_reference b)
+ {
+ return a-b;
+ });
+ return *this;
+}
+
+
+mat3d& mat3d::operator*=(const mat3d& m)
+{
+ *this = *this * m;
+ return *this;
+}
+
+
+mat3d& mat3d::operator*=(mat3d::value_type scale)
+{
+ std::for_each(begin(), end(), [&](reference v)
+ {
+ v *= scale;
+ });
+ return *this;
+}
+
+
+mat3d& mat3d::operator/=(mat3d::value_type scale)
+{
+ std::for_each(begin(), end(), [&](reference v)
+ {
+ v /= scale;
+ });
+ return *this;
+}
+
+
+/////////////////////
+// Private Methods //
+/////////////////////
+void mat3d::_swap(mat3d& m)
+{
+ if(&m == this) return;
+ std::swap(_data, m._data);
+}
+
+
+void mat3d::_assign(const mat3d& m)
+{
+ if(&m == this) return;
+ _data = m._data;
+}
+
+
+vec3d mat3d::_premultiply(const vec3d& v) const
+{
+ // result = v * *this
+ vec3d result;
+ for(size_type i=0; i < height(); i++)
+ for(size_type j=0; j < width(); j++)
+ result[j] += v[i] * (*this)(i,j);
+ return result;
+}