Moved matrix into new source/header pair in types/

Sun, 08 May 2016 20:54:52 +0300

author
Teemu Piippo <teemu@compsta2.com>
date
Sun, 08 May 2016 20:54:52 +0300
changeset 1037
4a9185e94d78
parent 1036
993c46d7eb75
child 1038
6b5058e95386

Moved matrix into new source/header pair in types/

CMakeLists.txt file | annotate | diff | comparison | revisions
src/basics.cpp file | annotate | diff | comparison | revisions
src/basics.h file | annotate | diff | comparison | revisions
src/types/matrix.cpp file | annotate | diff | comparison | revisions
src/types/matrix.h file | annotate | diff | comparison | revisions
--- a/CMakeLists.txt	Sun May 08 20:12:54 2016 +0300
+++ b/CMakeLists.txt	Sun May 08 20:54:52 2016 +0300
@@ -81,6 +81,7 @@
 	src/toolsets/movetoolset.cpp
 	src/toolsets/toolset.cpp
 	src/toolsets/viewtoolset.cpp
+	src/types/matrix.cpp
 )
 
 set (LDFORGE_HEADERS
@@ -137,6 +138,7 @@
 	src/toolsets/movetoolset.h
 	src/toolsets/toolset.h
 	src/toolsets/viewtoolset.h
+	src/types/matrix.h
 )
 
 set (LDFORGE_FORMS
--- a/src/basics.cpp	Sun May 08 20:12:54 2016 +0300
+++ b/src/basics.cpp	Sun May 08 20:54:52 2016 +0300
@@ -131,115 +131,6 @@
 
 // =============================================================================
 //
-Matrix::Matrix (double vals[])
-{
-	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;
-}
-
-// =============================================================================
-//
-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)
-	{
-		if (i > 0)
-			val += ' ';
-
-		val += QString::number (m_vals[i]);
-	}
-
-	return val;
-}
-
-// =============================================================================
-//
-void Matrix::zero()
-{
-	memset (&m_vals[0], 0, sizeof m_vals);
-}
-
-// =============================================================================
-//
-Matrix Matrix::mult (const Matrix& other) const
-{
-	Matrix val;
-	val.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];
-
-	return val;
-}
-
-// =============================================================================
-//
-Matrix& Matrix::operator= (const Matrix& other)
-{
-	memcpy (&m_vals[0], &other.m_vals[0], sizeof m_vals);
-	return *this;
-}
-
-// =============================================================================
-//
-double Matrix::getDeterminant() 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));
-}
-
-// =============================================================================
-//
-bool Matrix::operator== (const Matrix& other) const
-{
-	for (int i = 0; i < 9; ++i)
-	{
-		if (value (i) != other[i])
-			return false;
-	}
-
-	return true;
-}
-
-// =============================================================================
-//
 BoundingBox::BoundingBox()
 {
 	reset();
--- a/src/basics.h	Sun May 08 20:12:54 2016 +0300
+++ b/src/basics.h	Sun May 08 20:54:52 2016 +0300
@@ -25,6 +25,7 @@
 #include <functional>
 #include "macros.h"
 #include "transform.h"
+#include "types/matrix.h"
 
 class LDObject;
 class QFile;
@@ -88,78 +89,6 @@
 uint qHash(const Vertex& key);
 
 //
-// 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);
-
-	// 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;
-
-private:
-	double m_vals[9];
-};
-
-//
 // Defines a bounding box that encompasses a given set of objects.
 // vertex0 is the minimum vertex, vertex1 is the maximum vertex.
 //
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/types/matrix.cpp	Sun May 08 20:54:52 2016 +0300
@@ -0,0 +1,164 @@
+/*
+ *  LDForge: LDraw parts authoring CAD
+ *  Copyright (C) 2013 - 2016 Teemu Piippo
+ *
+ *  This program is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "../basics.h"
+#include "../format.h"
+#include "matrix.h"
+
+/**
+ * @brief Matrix::Matrix
+ * Default-constructor for a matrix -- does not actually initialize the member values.
+ */
+Matrix::Matrix() {}
+
+/**
+ * @brief Matrix::Matrix
+ * @param values
+ * Initializes the matrix from a C array
+ * Note: the array must have (at least) 9 values!
+ */
+Matrix::Matrix (double values[])
+{
+	for (int i = 0; i < 9; ++i)
+		m_values[i] = values[i];
+}
+
+/**
+ * @brief Matrix::Matrix
+ * @param fillvalue
+ * Constructs a matrix from a single fill value.
+ */
+Matrix::Matrix (double fillvalue)
+{
+	for (int i = 0; i < 9; ++i)
+		m_values[i] = fillvalue;
+}
+
+/**
+ * @brief Matrix::Matrix
+ * @param values
+ * Constructs a matrix from an initializer list.
+ * Note: the initializer list must have exactly 9 values.
+ */
+Matrix::Matrix (const std::initializer_list<double>& values)
+{
+	if (values.size() == 9)
+		memcpy (&m_values[0], values.begin(), sizeof m_values);
+}
+
+/**
+ * @brief Matrix::dump
+ * Prints the matrix out.
+ */
+void Matrix::dump() const
+{
+	for (int i = 0; i < 3; ++i)
+	{
+		for (int j = 0; j < 3; ++j)
+			print ("%1\t", m_values[i * 3 + j]);
+
+		print ("\n");
+	}
+}
+
+/**
+ * @brief Matrix::toString
+ * @return a string representation of the matrix
+ */
+QString Matrix::toString() const
+{
+	QString val;
+
+	for (int i = 0; i < 9; ++i)
+	{
+		if (i > 0)
+			val += ' ';
+
+		val += QString::number (m_values[i]);
+	}
+
+	return val;
+}
+
+/**
+ * @brief Matrix::zero
+ * Fills the matrix with zeros
+ */
+void Matrix::zero()
+{
+	memset (&m_values[0], 0, sizeof m_values);
+}
+
+/**
+ * @brief Matrix::multiply
+ * @param other
+ * @return this matrix multiplied with @c other.
+ * @note @c a.mult(b) is not equivalent to @c b.mult(a)
+ */
+Matrix Matrix::multiply (const Matrix& other) const
+{
+	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)
+		result[(i * 3) + j] += m_values[(i * 3) + k] * other[(k * 3) + j];
+
+	return result;
+}
+
+/**
+ * @brief Matrix::determinant
+ * @return the matrix's determinant
+ */
+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));
+}
+
+/**
+ * @brief Matrix::operator ==
+ * @param other
+ * @return whether the two matrices are equal
+ */
+bool Matrix::operator==(const Matrix& other) const
+{
+	for (int i = 0; i < countof(m_values); ++i)
+	{
+		if (not qFuzzyCompare(m_values[i], other.m_values[i]))
+			return false;
+	}
+	return true;
+}
+
+/**
+ * @brief Matrix::operator !=
+ * @param other
+ * @return whether the two matrices are not equal
+ */
+bool Matrix::operator!=(const Matrix& other) const
+{
+	return not operator==(other);
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/types/matrix.h	Sun May 08 20:54:52 2016 +0300
@@ -0,0 +1,59 @@
+/*
+ *  LDForge: LDraw parts authoring CAD
+ *  Copyright (C) 2013 - 2016 Teemu Piippo
+ *
+ *  This program is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#pragma once
+#include <QString>
+
+/**
+ * @brief The Matrix class
+ * A mathematical 3 x 3 matrix
+ */
+class Matrix
+{
+public:
+	Matrix();
+	Matrix (const std::initializer_list<double>& values);
+	Matrix (double fillval);
+	Matrix (double values[]);
+
+	double determinant() const;
+	Matrix multiply (const Matrix& other) const;
+	void dump() const;
+	QString toString() const;
+	void zero();
+	bool operator==(const Matrix& other) const;
+	bool operator!=(const Matrix& other) const;
+
+	/// @returns a mutable reference to a value by @c idx.
+	inline double& value (int idx) { return m_values[idx]; }
+
+	/// @returns a const reference to a value by @c idx.
+	inline const double& value (int idx) const { return m_values[idx]; }
+
+	/// @returns this matrix multiplied by @c other.
+	inline Matrix operator* (const Matrix& other) const { return multiply (other); }
+
+	/// @returns a mutable reference to a value by @c idx.
+	inline double& operator[] (int idx) { return value(idx); }
+
+	/// @returns a const reference to a value by @c idx.
+	inline const double& operator[] (int idx) const { return value (idx); }
+
+private:
+	double m_values[9];
+};

mercurial