simplified Matrix

Thu, 04 Jan 2018 20:21:36 +0200

author
Santeri Piippo
date
Thu, 04 Jan 2018 20:21:36 +0200
changeset 1219
8e39b5d7c562
parent 1218
e0b59d183f96
child 1220
c4c37abbe22b

simplified Matrix

src/addObjectDialog.cpp file | annotate | diff | comparison | revisions
src/basics.cpp file | annotate | diff | comparison | revisions
src/basics.h file | annotate | diff | comparison | revisions
src/glRenderer.cpp file | annotate | diff | comparison | revisions
src/ldObject.cpp file | annotate | diff | comparison | revisions
src/main.cpp file | annotate | diff | comparison | revisions
src/toolsets/algorithmtoolset.cpp file | annotate | diff | comparison | revisions
--- a/src/addObjectDialog.cpp	Thu Jan 04 19:52:24 2018 +0200
+++ b/src/addObjectDialog.cpp	Thu Jan 04 20:21:36 2018 +0200
@@ -188,7 +188,7 @@
 		QLabel* lb_matrix = new QLabel("Matrix:");
 		le_matrix = new QLineEdit;
 		// le_matrix->setValidator(new QDoubleValidator);
-		Matrix defaultMatrix = IdentityMatrix;
+		Matrix defaultMatrix;
 
 		if (mo)
 		{
@@ -294,7 +294,7 @@
 		return; // Nothing to edit with empties
 
 	const bool newObject = (obj == nullptr);
-	Matrix transform = IdentityMatrix;
+	Matrix transform;
 	AddObjectDialog dlg(type, obj);
 
 	if (obj and obj->type() != type)
@@ -309,13 +309,10 @@
 
 		if (matrixstrvals.size() == 9)
 		{
-			double matrixvals[9];
 			int i = 0;
 
 			for (QString val : matrixstrvals)
-				matrixvals[i++] = val.toFloat();
-
-			transform = Matrix(matrixvals);
+				transform[i++] = val.toFloat();
 		}
 	}
 
--- a/src/basics.cpp	Thu Jan 04 19:52:24 2018 +0200
+++ b/src/basics.cpp	Thu Jan 04 20:21:36 2018 +0200
@@ -129,117 +129,78 @@
 	return false;
 }
 
-// =============================================================================
-//
-Matrix::Matrix(double vals[])
+Matrix::Matrix() :
+	coefficients {1, 0, 0, 0, 1, 0, 0, 0, 1} {}
+
+Matrix::Matrix(std::initializer_list<double> values)
 {
-	for (int i = 0; i < 9; ++i)
-		m_vals[i] = vals[i];
-}
-
-// =============================================================================
-//
-Matrix::Matrix(double fillval)
-{
-	for (int i = 0; i < 9; ++i)
-		m_vals[i] = fillval;
+	if (values.size() == 9)
+		memcpy(&coefficients[0], values.begin(), sizeof coefficients);
 }
 
-// =============================================================================
-//
-Matrix::Matrix(const std::initializer_list<double>& vals)
-{
-	if (vals.size() == 9)
-		memcpy(&m_vals[0], vals.begin(), sizeof m_vals);
-}
-
-// =============================================================================
-//
-void Matrix::dump() const
-{
-	for (int i = 0; i < 3; ++i)
-	{
-		for (int j = 0; j < 3; ++j)
-			print("%1\t", m_vals[i * 3 + j]);
-
-		print("\n");
-	}
-}
-
-// =============================================================================
-//
 QString Matrix::toString() const
 {
 	QString val;
 
-	for (int i = 0; i < 9; ++i)
+	for (int i = 0; i < countof(coefficients); ++i)
 	{
 		if (i > 0)
 			val += ' ';
 
-		val += QString::number(m_vals[i]);
+		val += QString::number(coefficients[i]);
 	}
 
 	return val;
 }
 
-// =============================================================================
-//
 void Matrix::zero()
 {
-	memset(&m_vals[0], 0, sizeof m_vals);
+	for (int i = 0; i < countof(coefficients); ++i)
+		coefficients[i] = 0;
 }
 
-// =============================================================================
-//
-Matrix Matrix::mult(const Matrix& other) const
+Matrix Matrix::operator*(const Matrix& other) const
 {
-	Matrix val;
-	val.zero();
+	Matrix result;
+	result.zero();
 
 	for (int i = 0; i < 3; ++i)
 	for (int j = 0; j < 3; ++j)
 	for (int k = 0; k < 3; ++k)
-		val[(i * 3) + j] += m_vals[(i * 3) + k] * other[(k * 3) + j];
+		result(i, j) += (*this)(i, k) * other(k, j);
 
-	return val;
+	return result;
 }
 
-// =============================================================================
-//
-Matrix& Matrix::operator= (const Matrix& other)
+Matrix& Matrix::operator=(const Matrix& other)
 {
-	memcpy(&m_vals[0], &other.m_vals[0], sizeof m_vals);
+	for (int i = 0; i < countof(coefficients); ++i)
+		(*this)[i] = other[i];
 	return *this;
 }
 
-// =============================================================================
-//
-double Matrix::getDeterminant() const
+double Matrix::determinant() const
 {
-	return (value(0) * value(4) * value(8)) +
-		  (value(1) * value(5) * value(6)) +
-		  (value(2) * value(3) * value(7)) -
-		  (value(2) * value(4) * value(6)) -
-		  (value(1) * value(3) * value(8)) -
-		  (value(0) * value(5) * value(7));
+	return
+		(*this)(0, 0) * (*this)(1, 1) * (*this)(2, 2) +
+		(*this)(0, 1) * (*this)(1, 2) * (*this)(2, 0) +
+		(*this)(0, 2) * (*this)(1, 0) * (*this)(2, 1) -
+		(*this)(0, 2) * (*this)(1, 1) * (*this)(2, 0) -
+		(*this)(0, 1) * (*this)(1, 0) * (*this)(2, 2) -
+		(*this)(0, 0) * (*this)(1, 2) * (*this)(2, 1);
 }
 
-// =============================================================================
-//
 bool Matrix::operator== (const Matrix& other) const
 {
-	for (int i = 0; i < 9; ++i)
+	for (int i = 0; i < countof(coefficients); ++i)
 	{
-		if (value(i) != other[i])
+		if ((*this)[i] != other[i])
 			return false;
 	}
 
 	return true;
 }
 
-// =============================================================================
-//
 LDBoundingBox::LDBoundingBox()
 {
 	reset();
--- a/src/basics.h	Thu Jan 04 19:52:24 2018 +0200
+++ b/src/basics.h	Thu Jan 04 20:21:36 2018 +0200
@@ -85,78 +85,51 @@
 
 Q_DECLARE_METATYPE(Vertex)
 
-//
-// A mathematical 3 x 3 matrix
-//
+/*
+ * A mathematical 3 x 3 matrix
+ */
 class Matrix
 {
 public:
-	Matrix() {}
-	Matrix(const std::initializer_list<double>& vals);
-
-	// Constructs a matrix all 9 elements initialized to the same value.
-	Matrix(double fillval);
-
-	// Constructs a matrix with a C-array.
-	// note: @vals is expected to have exactly 9 elements.
-	Matrix(double vals[]);
-
-	// Calculates the matrix's determinant.
-	double			getDeterminant() const;
-
-	// Multiplies this matrix with @other
-	// note: a.mult(b) is not equivalent to b.mult(a)!
-	Matrix			mult(const Matrix& other) const;
-
-	// Prints the matrix to stdout.
-	void			dump() const;
-
-	// Yields a string representation of the matrix.
-	QString			toString() const;
-
-	// Zeroes the matrix out.
-	void			zero();
-
-	// Assigns the matrix values to the values of @other.
-	Matrix&			operator= (const Matrix& other);
+	Matrix();
+	Matrix(std::initializer_list<double> values);
 
-	// Returns a mutable reference to a value by @idx
-	inline double& value(int idx)
-	{
-		return m_vals[idx];
-	}
-
-	// An overload of value() for const matrices.
-	inline const double& value(int idx) const
-	{
-		return m_vals[idx];
-	}
-
-	// An operator overload for mult().
-	inline Matrix operator*(const Matrix& other) const
-	{
-		return mult(other);
-	}
-
-	// An operator overload for value().
-	inline double& operator[](int idx)
-	{
-		return value(idx);
-	}
-
-	// An operator overload for value() const.
-	inline const double& operator[](int idx) const
-	{
-		return value(idx);
-	}
-
-	// Checks whether the two matrices have the same values.
-	bool operator== (const Matrix& other) const;
+	double determinant() const;
+	QString toString() const;
+	void zero();
+	Matrix& operator=(const Matrix& other);
+	Matrix operator*(const Matrix& other) const;
+	double& operator[](int idx);
+	const double& operator[](int idx) const;
+	double& operator()(int row, int column);
+	const double& operator()(int row, int column) const;
+	bool operator==(const Matrix& other) const;
 
 private:
-	double m_vals[9];
+	double coefficients[9];
 };
 
+inline double& Matrix::operator[](int idx)
+{
+	return coefficients[idx];
+}
+
+inline const double& Matrix::operator[](int idx) const
+{
+	return coefficients[idx];
+}
+
+inline double& Matrix::operator()(int row, int column)
+{
+	return coefficients[row * 3 + column];
+}
+
+inline const double& Matrix::operator()(int row, int column) const
+{
+	return coefficients[row * 3 + column];
+}
+
+
 //
 // Defines a bounding box that encompasses a given set of objects.
 // vertex0 is the minimum vertex, vertex1 is the maximum vertex.
@@ -184,8 +157,6 @@
 	Vertex m_vertex1;
 };
 
-extern const Matrix IdentityMatrix;
-
 static const double Pi = 3.14159265358979323846;
 
 
--- a/src/glRenderer.cpp	Thu Jan 04 19:52:24 2018 +0200
+++ b/src/glRenderer.cpp	Thu Jan 04 20:21:36 2018 +0200
@@ -1595,7 +1595,7 @@
 		ref->setColor(MainColor);
 		ref->setFileInfo(m_documents->getDocumentByName(primitiveName));
 		ref->setPosition(Vertex {});
-		ref->setTransform(IdentityMatrix);
+		ref->setTransform(Matrix {});
 		currentDocument()->insertObj(m_window->suggestInsertPoint(), ref);
 		ref->select();
 		m_window->buildObjectList();
--- a/src/ldObject.cpp	Thu Jan 04 19:52:24 2018 +0200
+++ b/src/ldObject.cpp	Thu Jan 04 20:21:36 2018 +0200
@@ -722,7 +722,7 @@
 	{
 		// Subfile has all vertices zero on one specific plane, so it is flat.
 		// Let's flip it.
-		Matrix matrixModifier = IdentityMatrix;
+		Matrix matrixModifier = Matrix {};
 
 		if (axisSet &(1 << X))
 			matrixModifier[0] = -1;
--- a/src/main.cpp	Thu Jan 04 19:52:24 2018 +0200
+++ b/src/main.cpp	Thu Jan 04 20:21:36 2018 +0200
@@ -36,7 +36,6 @@
 
 MainWindow* g_win = nullptr;
 ConfigurationValueBag config;
-const Matrix IdentityMatrix({1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f});
 
 ConfigOption(bool FirstStart = true)
 
--- a/src/toolsets/algorithmtoolset.cpp	Thu Jan 04 19:52:24 2018 +0200
+++ b/src/toolsets/algorithmtoolset.cpp	Thu Jan 04 20:21:36 2018 +0200
@@ -610,7 +610,7 @@
 		ref->setColor(MainColor);
 		ref->setFileInfo(doc);
 		ref->setPosition(Vertex {});
-		ref->setTransform(IdentityMatrix);
+		ref->setTransform(Matrix {});
 		currentDocument()->insertObj(refidx, ref);
 
 		// Refresh stuff

mercurial