merged mathfunctions.cpp into algorithms/geometry.cpp

Sat, 24 Mar 2018 13:03:02 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Sat, 24 Mar 2018 13:03:02 +0200
changeset 1328
d68d1ce89d05
parent 1327
b179ab2f2c4e
child 1329
025578d6e491

merged mathfunctions.cpp into algorithms/geometry.cpp

CMakeLists.txt file | annotate | diff | comparison | revisions
src/algorithms/geometry.cpp file | annotate | diff | comparison | revisions
src/algorithms/geometry.h file | annotate | diff | comparison | revisions
src/basics.h file | annotate | diff | comparison | revisions
src/editmodes/circleMode.cpp file | annotate | diff | comparison | revisions
src/hierarchyelement.cpp file | annotate | diff | comparison | revisions
src/hierarchyelement.h file | annotate | diff | comparison | revisions
src/mainwindow.cpp file | annotate | diff | comparison | revisions
src/mainwindow.h file | annotate | diff | comparison | revisions
src/mathfunctions.cpp file | annotate | diff | comparison | revisions
src/mathfunctions.h file | annotate | diff | comparison | revisions
src/toolsets/algorithmtoolset.cpp file | annotate | diff | comparison | revisions
src/toolsets/movetoolset.cpp file | annotate | diff | comparison | revisions
--- a/CMakeLists.txt	Sat Mar 24 12:58:01 2018 +0200
+++ b/CMakeLists.txt	Sat Mar 24 13:03:02 2018 +0200
@@ -49,7 +49,6 @@
 	src/librariesmodel.cpp
 	src/main.cpp
 	src/mainwindow.cpp
-	src/mathfunctions.cpp
 	src/matrixinput.cpp
 	src/messageLog.cpp
 	src/model.cpp
@@ -120,7 +119,6 @@
 	src/librariesmodel.h
 	src/main.h
 	src/mainwindow.h
-	src/mathfunctions.h
 	src/matrixinput.h
 	src/messageLog.h
 	src/model.h
--- a/src/algorithms/geometry.cpp	Sat Mar 24 12:58:01 2018 +0200
+++ b/src/algorithms/geometry.cpp	Sat Mar 24 13:03:02 2018 +0200
@@ -17,6 +17,8 @@
  */
 
 #include "geometry.h"
+#include "../linetypes/modelobject.h"
+#include "../types/boundingbox.h"
 
 /*
  * LDraw uses 4 points of precision for sin and cos values. Primitives must be generated
@@ -66,6 +68,100 @@
 	return lines;
 }
 
+
+void rotateVertex(Vertex& vertex, const Vertex& rotationPoint, const Matrix& transformationMatrix)
+{
+	vertex -= rotationPoint.toVector();
+	vertex.transform (transformationMatrix, {0, 0, 0});
+	vertex += rotationPoint.toVector();
+}
+
+
+void rotateObjects(int l, int m, int n, double angle, const QVector<LDObject*>& objects)
+{
+	Vertex rotationPoint = getRotationPoint (objects);
+	double cosAngle = cos(angle);
+	double sinAngle = sin(angle);
+
+	// ref: http://en.wikipedia.org/wiki/Transformation_matrix#Rotation_2
+	Matrix transformationMatrix (
+	{
+		(l * l * (1 - cosAngle)) + cosAngle,
+		(m * l * (1 - cosAngle)) - (n * sinAngle),
+		(n * l * (1 - cosAngle)) + (m * sinAngle),
+
+		(l * m * (1 - cosAngle)) + (n * sinAngle),
+		(m * m * (1 - cosAngle)) + cosAngle,
+		(n * m * (1 - cosAngle)) - (l * sinAngle),
+
+		(l * n * (1 - cosAngle)) - (m * sinAngle),
+		(m * n * (1 - cosAngle)) + (l * sinAngle),
+		(n * n * (1 - cosAngle)) + cosAngle
+	});
+
+	// Apply the above matrix to everything
+	for (LDObject* obj : objects)
+	{
+		if (obj->numVertices())
+		{
+			for (int i = 0; i < obj->numVertices(); ++i)
+			{
+				Vertex v = obj->vertex (i);
+				rotateVertex(v, rotationPoint, transformationMatrix);
+				obj->setVertex (i, v);
+			}
+		}
+		else if (obj->hasMatrix())
+		{
+			LDMatrixObject* mo = dynamic_cast<LDMatrixObject*> (obj);
+
+			// Transform the position
+			Vertex v = mo->position();
+			rotateVertex(v, rotationPoint, transformationMatrix);
+			mo->setPosition (v);
+
+			// Transform the matrix
+			mo->setTransformationMatrix(transformationMatrix * mo->transformationMatrix());
+		}
+	}
+}
+
+
+Vertex getRotationPoint(const QVector<LDObject*>& objs)
+{
+	switch (static_cast<RotationPoint>(config::rotationPointType()))
+	{
+	case ObjectOrigin:
+		{
+			BoundingBox box;
+
+			// Calculate center vertex
+			for (LDObject* obj : objs)
+			{
+				if (obj->hasMatrix())
+				{
+					box << static_cast<LDMatrixObject*> (obj)->position();
+				}
+				else
+				{
+					for (int i = 0; i < obj->numVertices(); ++i)
+						box << obj->vertex(i);
+				}
+			}
+
+			return box.center();
+		}
+
+	case WorldOrigin:
+		return Vertex();
+
+	case CustomPoint:
+		return config::customRotationPoint();
+	}
+
+	return Vertex();
+}
+
 /*
  * Computes the shortest distance from a point to a rectangle.
  *
--- a/src/algorithms/geometry.h	Sat Mar 24 12:58:01 2018 +0200
+++ b/src/algorithms/geometry.h	Sat Mar 24 13:03:02 2018 +0200
@@ -15,11 +15,22 @@
  *  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 "../main.h"
 
+enum RotationPoint
+{
+	ObjectOrigin,
+	WorldOrigin,
+	CustomPoint,
+};
+
 double ldrawsin(double angle);
 double ldrawcos(double angle);
 QPointF pointOnLDrawCircumference(int segment, int divisions);
 QVector<QLineF> makeCircle(int segments, int divisions, double radius);
 qreal distanceFromPointToRectangle(const QPointF& point, const QRectF& rectangle);
+void rotateObjects(int l, int m, int n, double angle, const QVector<LDObject*>& objects);
+Vertex getRotationPoint(const QVector<LDObject*>& objs);
+void rotateVertex(Vertex& vertex, const Vertex& rotationPoint, const Matrix& transformationMatrix);
--- a/src/basics.h	Sat Mar 24 12:58:01 2018 +0200
+++ b/src/basics.h	Sat Mar 24 13:03:02 2018 +0200
@@ -50,6 +50,7 @@
 # define DIRSLASH_CHAR '/'
 #endif // WIN32
 
+class LDObject;
 using GLRotationMatrix = QMatrix4x4;
 
 enum Axis
--- a/src/editmodes/circleMode.cpp	Sat Mar 24 12:58:01 2018 +0200
+++ b/src/editmodes/circleMode.cpp	Sat Mar 24 13:03:02 2018 +0200
@@ -23,7 +23,6 @@
 #include "../primitives.h"
 #include "../canvas.h"
 #include "../mainwindow.h"
-#include "../mathfunctions.h"
 #include "../documentmanager.h"
 #include "../grid.h"
 #include "../linetypes/modelobject.h"
--- a/src/hierarchyelement.cpp	Sat Mar 24 12:58:01 2018 +0200
+++ b/src/hierarchyelement.cpp	Sat Mar 24 13:03:02 2018 +0200
@@ -75,12 +75,6 @@
 }
 
 
-MathFunctions* HierarchyElement::math() const
-{
-	return m_window->mathFunctions();
-}
-
-
 QString HierarchyElement::preferredLicenseText() const
 {
 	QString caLicenseText = "!LICENSE Redistributable under CCAL version 2.0 : see CAreadme.txt";
--- a/src/hierarchyelement.h	Sat Mar 24 12:58:01 2018 +0200
+++ b/src/hierarchyelement.h	Sat Mar 24 13:03:02 2018 +0200
@@ -28,7 +28,6 @@
 class DocumentManager;
 class PrimitiveManager;
 class Grid;
-class MathFunctions;
 class MainWindow;
 
 //
@@ -45,7 +44,6 @@
 	GuiUtilities* guiUtilities() const;
 	PrimitiveManager* primitives();
 	Grid* grid() const;
-	MathFunctions* math() const;
 
 	// Utility functions
 	QString preferredLicenseText() const;
--- a/src/mainwindow.cpp	Sat Mar 24 12:58:01 2018 +0200
+++ b/src/mainwindow.cpp	Sat Mar 24 13:03:02 2018 +0200
@@ -39,7 +39,6 @@
 #include "documentmanager.h"
 #include "ldobjectiterator.h"
 #include "grid.h"
-#include "mathfunctions.h"
 #include "editHistory.h"
 
 struct MainWindow::ToolInfo
@@ -55,7 +54,6 @@
 	m_guiUtilities (new GuiUtilities (this)),
 	m_primitives(new PrimitiveManager(this)),
 	m_grid(new Grid(this)),
-	m_mathFunctions(new MathFunctions(this)),
 	ui (*new Ui_MainWindow),
 	m_externalPrograms (nullptr),
 	m_documents (new DocumentManager (this)),
@@ -169,7 +167,6 @@
 	delete m_guiUtilities;
 	delete m_primitives;
 	delete m_grid;
-	delete m_mathFunctions;
 	delete &ui;
 
 	for (Toolset* toolset : m_toolsets)
@@ -1047,11 +1044,6 @@
 	return m_grid;
 }
 
-MathFunctions* MainWindow::mathFunctions() const
-{
-	return m_mathFunctions;
-}
-
 void MainWindow::clearSelection()
 {
 	m_selections[m_currentDocument]->clear();
--- a/src/mainwindow.h	Sat Mar 24 12:58:01 2018 +0200
+++ b/src/mainwindow.h	Sat Mar 24 13:03:02 2018 +0200
@@ -33,7 +33,6 @@
 class Toolset;
 class PrimitiveManager;
 class Grid;
-class MathFunctions;
 class DocumentManager;
 class LDDocument;
 
@@ -84,7 +83,6 @@
 	Grid* grid();
 	class GuiUtilities* guiUtilities();
 	void loadShortcuts();
-	MathFunctions* mathFunctions() const;
 	MessageManager* messageLog() const;
 	LDDocument* newDocument (bool cache = false);
 	void openDocumentForEditing(LDDocument* document);
@@ -144,7 +142,6 @@
 	QMap<LDDocument*, QItemSelectionModel*> m_selections;
 	PrimitiveManager* m_primitives;
 	Grid* m_grid;
-	MathFunctions* m_mathFunctions;
 	QVector<ColorToolbarItem>	m_quickColors;
 	QList<QToolButton*>	m_colorButtons;
 	QList<QAction*> m_recentFiles;
--- a/src/mathfunctions.cpp	Sat Mar 24 12:58:01 2018 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,118 +0,0 @@
-/*
- *  LDForge: LDraw parts authoring CAD
- *  Copyright (C) 2013 - 2018 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 "mathfunctions.h"
-#include "linetypes/modelobject.h"
-#include "types/boundingbox.h"
-
-MathFunctions::MathFunctions(QObject* parent) :
-	HierarchyElement(parent) {}
-
-
-void MathFunctions::rotateVertex(Vertex& vertex, const Vertex& rotationPoint, const Matrix& transformationMatrix) const
-{
-	vertex -= rotationPoint.toVector();
-	vertex.transform (transformationMatrix, {0, 0, 0});
-	vertex += rotationPoint.toVector();
-}
-
-
-void MathFunctions::rotateObjects(int l, int m, int n, double angle, const QVector<LDObject*>& objects) const
-{
-	Vertex rotationPoint = getRotationPoint (objects);
-	double cosAngle = cos(angle);
-	double sinAngle = sin(angle);
-
-	// ref: http://en.wikipedia.org/wiki/Transformation_matrix#Rotation_2
-	Matrix transformationMatrix (
-	{
-		(l * l * (1 - cosAngle)) + cosAngle,
-		(m * l * (1 - cosAngle)) - (n * sinAngle),
-		(n * l * (1 - cosAngle)) + (m * sinAngle),
-
-		(l * m * (1 - cosAngle)) + (n * sinAngle),
-		(m * m * (1 - cosAngle)) + cosAngle,
-		(n * m * (1 - cosAngle)) - (l * sinAngle),
-
-		(l * n * (1 - cosAngle)) - (m * sinAngle),
-		(m * n * (1 - cosAngle)) + (l * sinAngle),
-		(n * n * (1 - cosAngle)) + cosAngle
-	});
-
-	// Apply the above matrix to everything
-	for (LDObject* obj : objects)
-	{
-		if (obj->numVertices())
-		{
-			for (int i = 0; i < obj->numVertices(); ++i)
-			{
-				Vertex v = obj->vertex (i);
-				rotateVertex(v, rotationPoint, transformationMatrix);
-				obj->setVertex (i, v);
-			}
-		}
-		else if (obj->hasMatrix())
-		{
-			LDMatrixObject* mo = dynamic_cast<LDMatrixObject*> (obj);
-
-			// Transform the position
-			Vertex v = mo->position();
-			rotateVertex(v, rotationPoint, transformationMatrix);
-			mo->setPosition (v);
-
-			// Transform the matrix
-			mo->setTransformationMatrix(transformationMatrix * mo->transformationMatrix());
-		}
-	}
-}
-
-
-Vertex MathFunctions::getRotationPoint(const QVector<LDObject*>& objs) const
-{
-	switch (RotationPoint (config::rotationPointType()))
-	{
-	case ObjectOrigin:
-		{
-			BoundingBox box;
-
-			// Calculate center vertex
-			for (LDObject* obj : objs)
-			{
-				if (obj->hasMatrix())
-				{
-					box << static_cast<LDMatrixObject*> (obj)->position();
-				}
-				else
-				{
-					for (int i = 0; i < obj->numVertices(); ++i)
-						box << obj->vertex(i);
-				}
-			}
-
-			return box.center();
-		}
-
-	case WorldOrigin:
-		return Vertex();
-
-	case CustomPoint:
-		return config::customRotationPoint();
-	}
-
-	return Vertex();
-}
--- a/src/mathfunctions.h	Sat Mar 24 12:58:01 2018 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,43 +0,0 @@
-/*
- *  LDForge: LDraw parts authoring CAD
- *  Copyright (C) 2013 - 2018 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 "main.h"
-#include "hierarchyelement.h"
-
-
-enum RotationPoint
-{
-	ObjectOrigin,
-	WorldOrigin,
-	CustomPoint,
-};
-
-
-class MathFunctions : public HierarchyElement
-{
-public:
-	MathFunctions(QObject* parent);
-
-	void rotateObjects(int l, int m, int n, double angle, const QVector<LDObject*>& objects) const;
-	Vertex getRotationPoint(const QVector<LDObject*>& objs) const;
-
-private:
-	void rotateVertex(Vertex& vertex, const Vertex& rotationPoint, const Matrix& transformationMatrix) const;
-};
-
--- a/src/toolsets/algorithmtoolset.cpp	Sat Mar 24 12:58:01 2018 +0200
+++ b/src/toolsets/algorithmtoolset.cpp	Sat Mar 24 13:03:02 2018 +0200
@@ -26,7 +26,6 @@
 #include "../lddocument.h"
 #include "../glrenderer.h"
 #include "../colors.h"
-#include "../mathfunctions.h"
 #include "../ldobjectiterator.h"
 #include "../documentmanager.h"
 #include "../linetypes/comment.h"
--- a/src/toolsets/movetoolset.cpp	Sat Mar 24 12:58:01 2018 +0200
+++ b/src/toolsets/movetoolset.cpp	Sat Mar 24 13:03:02 2018 +0200
@@ -17,12 +17,12 @@
  */
 
 #include "../lddocument.h"
-#include "../mathfunctions.h"
 #include "../mainwindow.h"
 #include "movetoolset.h"
 #include "ui_rotationpointdialog.h"
 #include "../grid.h"
 #include "../canvas.h"
+#include "../algorithms/geometry.h"
 
 MoveToolset::MoveToolset (MainWindow* parent) :
 	Toolset (parent) {}
@@ -121,32 +121,32 @@
 
 void MoveToolset::rotateXPos()
 {
-	math()->rotateObjects (1, 0, 0, getRotateActionAngle(), selectedObjects().toList().toVector());
+	rotateObjects(1, 0, 0, getRotateActionAngle(), selectedObjects().toList().toVector());
 }
 
 void MoveToolset::rotateYPos()
 {
-	math()->rotateObjects (0, 1, 0, getRotateActionAngle(), selectedObjects().toList().toVector());
+	rotateObjects(0, 1, 0, getRotateActionAngle(), selectedObjects().toList().toVector());
 }
 
 void MoveToolset::rotateZPos()
 {
-	math()->rotateObjects (0, 0, 1, getRotateActionAngle(), selectedObjects().toList().toVector());
+	rotateObjects(0, 0, 1, getRotateActionAngle(), selectedObjects().toList().toVector());
 }
 
 void MoveToolset::rotateXNeg()
 {
-	math()->rotateObjects (-1, 0, 0, getRotateActionAngle(), selectedObjects().toList().toVector());
+	rotateObjects(-1, 0, 0, getRotateActionAngle(), selectedObjects().toList().toVector());
 }
 
 void MoveToolset::rotateYNeg()
 {
-	math()->rotateObjects (0, -1, 0, getRotateActionAngle(), selectedObjects().toList().toVector());
+	rotateObjects(0, -1, 0, getRotateActionAngle(), selectedObjects().toList().toVector());
 }
 
 void MoveToolset::rotateZNeg()
 {
-	math()->rotateObjects (0, 0, -1, getRotateActionAngle(), selectedObjects().toList().toVector());
+	rotateObjects(0, 0, -1, getRotateActionAngle(), selectedObjects().toList().toVector());
 }
 
 void MoveToolset::configureRotationPoint()
@@ -155,7 +155,7 @@
 	Ui_RotPointUI ui;
 	ui.setupUi(dialog);
 
-	switch (RotationPoint(config::rotationPointType()))
+	switch (static_cast<RotationPoint>(config::rotationPointType()))
 	{
 	case ObjectOrigin:
 		ui.objectPoint->setChecked (true);

mercurial