Wed, 17 Feb 2016 00:43:17 +0200
Moved rotation point handling to ldObjectMath.cpp and encapsulated it into new class MathFunctions
--- a/src/editmodes/circleMode.cpp Tue Feb 16 19:59:43 2016 +0200 +++ b/src/editmodes/circleMode.cpp Wed Feb 17 00:43:17 2016 +0200 @@ -185,10 +185,10 @@ if (not objs.isEmpty()) { Axis relZ = renderer()->getRelativeZ();; - const int l (relZ == X ? 1 : 0); - const int m (relZ == Y ? 1 : 0); - const int n (relZ == Z ? 1 : 0); - RotateObjects (l, m, n, -m_angleOffset, objs); + int l = (relZ == X ? 1 : 0); + int m = (relZ == Y ? 1 : 0); + int n = (relZ == Z ? 1 : 0); + math()->rotateObjects (l, m, n, -m_angleOffset, objs); } finishDraw (objs); @@ -199,7 +199,7 @@ if (m_drawedVerts.isEmpty()) return 0.0; - const int divisions (m_window->ringToolHiRes() ? HighResolution : LowResolution); + int divisions = (m_window->ringToolHiRes() ? HighResolution : LowResolution); QPointF originspot (renderer()->convert3dTo2d (m_drawedVerts.first())); QLineF bearing (originspot, renderer()->mousePositionF()); QLineF bearing2 (originspot, QPointF (originspot.x(), 0.0));
--- a/src/hierarchyelement.cpp Tue Feb 16 19:59:43 2016 +0200 +++ b/src/hierarchyelement.cpp Wed Feb 17 00:43:17 2016 +0200 @@ -75,3 +75,9 @@ { return m_window->grid(); } + + +MathFunctions* HierarchyElement::math() const +{ + return m_window->mathFunctions(); +} \ No newline at end of file
--- a/src/hierarchyelement.h Tue Feb 16 19:59:43 2016 +0200 +++ b/src/hierarchyelement.h Wed Feb 17 00:43:17 2016 +0200 @@ -27,6 +27,7 @@ class DocumentManager; class PrimitiveManager; class Grid; +class MathFunctions; // // Objects that are to take part in the MainWindow's hierarchy multiple-inherit from this class to get a pointer back @@ -42,6 +43,7 @@ GuiUtilities* guiUtilities() const; PrimitiveManager* primitives(); Grid* grid() const; + MathFunctions* math() const; protected: MainWindow* m_window;
--- a/src/ldObjectMath.cpp Tue Feb 16 19:59:43 2016 +0200 +++ b/src/ldObjectMath.cpp Wed Feb 17 00:43:17 2016 +0200 @@ -20,38 +20,39 @@ #include "ldObject.h" #include "miscallenous.h" -// ============================================================================= -// -static void RotateVertex (Vertex& v, const Vertex& rotpoint, const Matrix& transformer) + +MathFunctions::MathFunctions(QObject* parent) : + HierarchyElement(parent) {} + + +void MathFunctions::rotateVertex(Vertex& vertex, const Vertex& rotationPoint, const Matrix& transformationMatrix) const { - v -= rotpoint; - v.transform (transformer, Origin); - v += rotpoint; + vertex -= rotationPoint; + vertex.transform (transformationMatrix, Origin); + vertex += rotationPoint; } -// ============================================================================= -// -void RotateObjects (const int l, const int m, const int n, double angle, LDObjectList const& objects) + +void MathFunctions::rotateObjects(int l, int m, int n, double angle, const LDObjectList& objects) const { - QList<Vertex*> queue; - const Vertex rotpoint = getRotationPoint (objects); - const double cosangle = cos (angle), - sinangle = sin (angle); + Vertex rotationPoint = getRotationPoint (objects); + double cosAngle = cos(angle); + double sinAngle = sin(angle); // ref: http://en.wikipedia.org/wiki/Transformation_matrix#Rotation_2 - Matrix transform ( + Matrix transformationMatrix ( { - (l * l * (1 - cosangle)) + cosangle, - (m * l * (1 - cosangle)) - (n * sinangle), - (n * l * (1 - cosangle)) + (m * sinangle), + (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 * 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 + (l * n * (1 - cosAngle)) - (m * sinAngle), + (m * n * (1 - cosAngle)) + (l * sinAngle), + (n * n * (1 - cosAngle)) + cosAngle }); // Apply the above matrix to everything @@ -62,7 +63,7 @@ for (int i = 0; i < obj->numVertices(); ++i) { Vertex v = obj->vertex (i); - RotateVertex (v, rotpoint, transform); + rotateVertex(v, rotationPoint, transformationMatrix); obj->setVertex (i, v); } } @@ -72,11 +73,45 @@ // Transform the position Vertex v = mo->position(); - RotateVertex (v, rotpoint, transform); + rotateVertex(v, rotationPoint, transformationMatrix); mo->setPosition (v); // Transform the matrix - mo->setTransform (transform * mo->transform()); + mo->setTransform(transformationMatrix * mo->transform()); } } } + + +Vertex MathFunctions::getRotationPoint(const LDObjectList& objs) const +{ + switch (RotationPoint (m_config->rotationPointType())) + { + case RotationPoint::ObjectOrigin: + { + BoundingBox box; + + // Calculate center vertex + for (LDObject* obj : objs) + { + if (obj->hasMatrix()) + box << static_cast<LDMatrixObject*> (obj)->position(); + else + box << obj; + } + + return box.center(); + } + + case RotationPoint::WorldOrigin: + return Vertex(); + + case RotationPoint::CustomPoint: + return m_config->customRotationPoint(); + + case RotationPoint::NumValues: + break; + } + + return Vertex(); +} \ No newline at end of file
--- a/src/ldObjectMath.h Tue Feb 16 19:59:43 2016 +0200 +++ b/src/ldObjectMath.h Wed Feb 17 00:43:17 2016 +0200 @@ -19,5 +19,25 @@ #pragma once #include "main.h" -void RotateObjects (const int l, const int m, const int n, - double angle, LDObjectList const& objects); + +enum class RotationPoint +{ + ObjectOrigin, + WorldOrigin, + CustomPoint, + NumValues +}; + + +class MathFunctions : public HierarchyElement +{ +public: + MathFunctions(QObject* parent); + + void rotateObjects(int l, int m, int n, double angle, const LDObjectList& objects) const; + Vertex getRotationPoint(const LDObjectList& objs) const; + +private: + void rotateVertex(Vertex& vertex, const Vertex& rotationPoint, const Matrix& transformationMatrix) const; +}; +
--- a/src/mainwindow.cpp Tue Feb 16 19:59:43 2016 +0200 +++ b/src/mainwindow.cpp Wed Feb 17 00:43:17 2016 +0200 @@ -57,6 +57,7 @@ #include "documentmanager.h" #include "ldobjectiterator.h" #include "grid.h" +#include "ldObjectMath.h" ConfigOption (bool ColorizeObjectsList = true) ConfigOption (QString QuickColorToolbar = "4:25:14:27:2:3:11:1:22:|:0:72:71:15") @@ -71,6 +72,7 @@ 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_settings (makeSettings (this)), @@ -85,7 +87,6 @@ m_tabs = new QTabBar; m_tabs->setTabsClosable (true); ui.verticalLayout->insertWidget (0, m_tabs); - createBlankDocument(); m_renderer->setDocument (m_currentDocument); @@ -192,6 +193,16 @@ MainWindow::~MainWindow() { g_win = nullptr; + delete m_guiUtilities; + delete m_primitives; + delete m_grid; + delete m_mathFunctions; + delete &ui; + delete m_settings; + delete m_documents; + + for (Toolset* toolset : m_toolsets) + delete toolset; } // --------------------------------------------------------------------------------------------------------------------- @@ -249,7 +260,6 @@ { QString file = it.toString(); QAction* recent = new QAction (GetIcon ("open-recent"), file, this); - connect (recent, SIGNAL (triggered()), this, SLOT (recentFileClicked())); ui.menuOpenRecent->insertAction (first, recent); m_recentFiles << recent; @@ -1339,6 +1349,11 @@ return m_grid; } +MathFunctions* MainWindow::mathFunctions() const +{ + return m_mathFunctions; +} + // --------------------------------------------------------------------------------------------------------------------- // ColorToolbarItem::ColorToolbarItem (LDColor color, QToolButton* toolButton) :
--- a/src/mainwindow.h Tue Feb 16 19:59:43 2016 +0200 +++ b/src/mainwindow.h Wed Feb 17 00:43:17 2016 +0200 @@ -39,6 +39,7 @@ class Configuration; class PrimitiveManager; class Grid; +class MathFunctions; class ColorToolbarItem { @@ -98,6 +99,7 @@ Grid* grid(); class GuiUtilities* guiUtilities(); void loadShortcuts(); + MathFunctions* mathFunctions() const; LDDocument* newDocument (bool cache = false); PrimitiveManager* primitives(); GLRenderer* renderer(); @@ -143,6 +145,7 @@ GLRenderer* m_renderer; PrimitiveManager* m_primitives; Grid* m_grid; + MathFunctions* m_mathFunctions; LDObjectList m_sel; QList<ColorToolbarItem> m_quickColors; QList<QToolButton*> m_colorButtons;
--- a/src/miscallenous.cpp Tue Feb 16 19:59:43 2016 +0200 +++ b/src/miscallenous.cpp Wed Feb 17 00:43:17 2016 +0200 @@ -47,40 +47,6 @@ } -Vertex getRotationPoint (const LDObjectList& objs) -{ - switch (RotationPoint (Config->rotationPointType())) - { - case RotationPoint::ObjectOrigin: - { - BoundingBox box; - - // Calculate center vertex - for (LDObject* obj : objs) - { - if (obj->hasMatrix()) - box << static_cast<LDMatrixObject*> (obj)->position(); - else - box << obj; - } - - return box.center(); - } - - case RotationPoint::WorldOrigin: - return Origin; - - case RotationPoint::CustomPoint: - return Config->customRotationPoint(); - - case RotationPoint::NumValues: - break; - } - - return Vertex(); -} - - QString joinStrings (QList<StringFormatArg> vals, QString delim) { QStringList list;
--- a/src/miscallenous.h Tue Feb 16 19:59:43 2016 +0200 +++ b/src/miscallenous.h Wed Feb 17 00:43:17 2016 +0200 @@ -29,20 +29,10 @@ using ApplyToMatrixFunction = std::function<void (int, double&)>; using ApplyToMatrixConstFunction = std::function<void (int, double)>; -enum class RotationPoint -{ - ObjectOrigin, - WorldOrigin, - CustomPoint, - NumValues -}; - void applyToMatrix (Matrix& a, ApplyToMatrixFunction func); void applyToMatrix (const Matrix& a, ApplyToMatrixConstFunction func); -void configureRotationPoint(); QString formatFileSize (qint64 size); int gcd (int a, int b); -Vertex getRotationPoint (const LDObjectList& objs); QString joinStrings (QList<StringFormatArg> vals, QString delim = " "); void roundToDecimals (double& a, int decimals); void simplify (int& numer, int& denom);
--- a/src/toolsets/movetoolset.cpp Tue Feb 16 19:59:43 2016 +0200 +++ b/src/toolsets/movetoolset.cpp Wed Feb 17 00:43:17 2016 +0200 @@ -108,32 +108,32 @@ void MoveToolset::rotateXPos() { - RotateObjects (1, 0, 0, getRotateActionAngle(), selectedObjects()); + math()->rotateObjects (1, 0, 0, getRotateActionAngle(), selectedObjects()); } void MoveToolset::rotateYPos() { - RotateObjects (0, 1, 0, getRotateActionAngle(), selectedObjects()); + math()->rotateObjects (0, 1, 0, getRotateActionAngle(), selectedObjects()); } void MoveToolset::rotateZPos() { - RotateObjects (0, 0, 1, getRotateActionAngle(), selectedObjects()); + math()->rotateObjects (0, 0, 1, getRotateActionAngle(), selectedObjects()); } void MoveToolset::rotateXNeg() { - RotateObjects (-1, 0, 0, getRotateActionAngle(), selectedObjects()); + math()->rotateObjects (-1, 0, 0, getRotateActionAngle(), selectedObjects()); } void MoveToolset::rotateYNeg() { - RotateObjects (0, -1, 0, getRotateActionAngle(), selectedObjects()); + math()->rotateObjects (0, -1, 0, getRotateActionAngle(), selectedObjects()); } void MoveToolset::rotateZNeg() { - RotateObjects (0, 0, -1, getRotateActionAngle(), selectedObjects()); + math()->rotateObjects (0, 0, -1, getRotateActionAngle(), selectedObjects()); } void MoveToolset::configureRotationPoint()