Sat, 31 Mar 2018 18:41:24 +0300
start rework
CMakeLists.txt | file | annotate | diff | comparison | revisions | |
src/glcompiler.cpp | file | annotate | diff | comparison | revisions | |
src/model.cpp | file | annotate | diff | comparison | revisions | |
src/model.h | file | annotate | diff | comparison | revisions | |
src/types/matrix.cpp | file | annotate | diff | comparison | revisions | |
src/types/matrix.h | file | annotate | diff | comparison | revisions | |
src/widgets/vertexobjecteditor.cpp | file | annotate | diff | comparison | revisions | |
src/widgets/vertexobjecteditor.h | file | annotate | diff | comparison | revisions |
--- a/CMakeLists.txt Fri Mar 30 14:30:51 2018 +0300 +++ b/CMakeLists.txt Sat Mar 31 18:41:24 2018 +0300 @@ -150,6 +150,7 @@ src/generics/functions.h src/generics/migrate.h src/generics/range.h + src/generics/resources.h src/generics/reverse.h src/generics/ring.h src/generics/transform.h
--- a/src/glcompiler.cpp Fri Mar 30 14:30:51 2018 +0300 +++ b/src/glcompiler.cpp Sat Mar 31 18:41:24 2018 +0300 @@ -344,6 +344,28 @@ unstage(index); } +LDPolygon* GLCompiler::makePolygon(const QModelIndex& index) +{ + LDObjectType ot = type(); + int num = (ot == LDObjectType::EdgeLine) ? 2 + : (ot == LDObjectType::Triangle) ? 3 + : (ot == LDObjectType::Quadrilateral) ? 4 + : (ot == LDObjectType::ConditionalEdge) ? 5 + : 0; + + if (num == 0) + return nullptr; + + LDPolygon* data = new LDPolygon; + data->num = num; + data->color = color().index(); + + for (int i = 0; i < data->numVertices(); ++i) + data->vertices[i] = vertex (i); + + return data; +} + /* * Compiles a single object. */ @@ -413,6 +435,10 @@ if (m_renderer->model()->winding() == Clockwise) ::invertPolygon(poly); + // Apply transformation matrix + for (int i = 0; i < poly.numVertices(); i += 1) + poly.vertices[i] = m_renderer->model()->map(poly.vertices[i]); + VboClass surface; int vertexCount;
--- a/src/model.cpp Fri Mar 30 14:30:51 2018 +0300 +++ b/src/model.cpp Sat Mar 31 18:41:24 2018 +0300 @@ -18,6 +18,7 @@ #include "model.h" #include "linetypes/modelobject.h" +#include "linetypes/comment.h" #include "documentmanager.h" #include "generics/migrate.h" #include "editHistory.h" @@ -355,11 +356,118 @@ return {}; } + case LDObjectTypeRole: + return object->type(); + + case Vertex0Role: + case Vertex1Role: + case Vertex2Role: + case Vertex3Role: + return this->map(object->vertex(role - Vertex0Role)); + + case ColorRole: + return object->color(); + + case PositionRole: + if (object->type() == LDObjectType::SubfileReference) + return static_cast<LDSubfileReference*>(object)->position(); + else + return Vertex {}; + + case TransformationMatrixRole: + if (object->type() == LDObjectType::SubfileReference) + { + return static_cast<LDSubfileReference*>(object)->transformationMatrix() + * this->transformationMatrix().toGenericMatrix<3, 3>(); + } + else + { + return Matrix {}; + } + + case ReferenceNameRole: + if (object->type() == LDObjectType::SubfileReference) + return static_cast<LDSubfileReference*>(object)->referenceName(); + else + return ""; + + case CommentTextRole: + if (object->type() == LDObjectType::Comment) + return static_cast<LDComment*>(object)->text(); + else + return ""; + default: return {}; } } +bool Model::setData(const QModelIndex& index, const QVariant& value, int role) +{ + LDObject* object = this->objects()[index.row()]; + + switch (role) + { + case Vertex0Role: + case Vertex1Role: + case Vertex2Role: + case Vertex3Role: + object->setVertex(role - Vertex0Role, this->unmap(value.value<Vertex>())); + return true; + + case ColorRole: + object->setColor(value.value<LDColor>()); + return true; + + case PositionRole: + if (object->type() == LDObjectType::SubfileReference) + { + LDSubfileReference* reference = static_cast<LDSubfileReference*>(object); + reference->setPosition(this->unmap(value.value<Vertex>())); + return true; + } + else + { + return false; + } + + case TransformationMatrixRole: + if (object->type() == LDObjectType::SubfileReference) + { + LDSubfileReference* reference = static_cast<LDSubfileReference*>(object); + reference->setTransformationMatrix( + value.value<Matrix>() + * this->invertedTransformationmatrix.toGenericMatrix<3, 3>() + ); + return true; + } + else + { + return false; + } + + case ReferenceNameRole: + if (object->type() == LDObjectType::SubfileReference) + return static_cast<LDSubfileReference*>(object)->referenceName(); + else + return ""; + + case CommentTextRole: + if (object->type() == LDObjectType::Comment) + { + static_cast<LDComment*>(object)->setText(value.toString()); + return true; + } + else + { + return false; + } + + default: + return false; + } +} + /* bool Model::removeRows(int row, int count, const QModelIndex& parent) { @@ -425,6 +533,31 @@ emit windingChanged(this->_winding); } +const GLRotationMatrix& Model::transformationMatrix() const +{ + return this->storedTransformationmatrix; +} + +void Model::setTransformationMatrix(const GLRotationMatrix& matrix) +{ + if (matrix != this->storedTransformationmatrix) + { + this->storedTransformationmatrix = matrix; + this->invertedTransformationmatrix = matrix.inverted(); + emit this->transformationMatrixChanged(matrix); + } +} + +Vertex Model::map(const Vertex& global) const +{ + return global.transformed(this->transformationMatrix()); +} + +Vertex Model::unmap(const Vertex& local) const +{ + return local.transformed(this->invertedTransformationmatrix); +} + int countof(Model& model) { return model.size();
--- a/src/model.h Fri Mar 30 14:30:51 2018 +0300 +++ b/src/model.h Sat Mar 31 18:41:24 2018 +0300 @@ -78,6 +78,20 @@ Q_OBJECT public: + enum + { + LDObjectTypeRole = Qt::UserRole, + Vertex0Role, + Vertex1Role, + Vertex2Role, + Vertex3Role, + ColorRole, + PositionRole, + TransformationMatrixRole, + ReferenceNameRole, + CommentTextRole, + }; + Model(class DocumentManager* manager); Model(const Model& other) = delete; ~Model(); @@ -109,6 +123,10 @@ QModelIndex objectByPickingColor(const QColor& color) const; Winding winding() const; void setWinding(Winding winding); + const GLRotationMatrix& transformationMatrix() const; + void setTransformationMatrix(const GLRotationMatrix& matrix); + Vertex map(const Vertex& global) const; + Vertex unmap(const Vertex& local) const; bool moveRows( const QModelIndex& sourceParent, @@ -120,12 +138,14 @@ int rowCount(const QModelIndex& parent) const override; QVariant data(const QModelIndex& index, int role) const override; + bool setData(const QModelIndex& index, const QVariant& value, int role); signals: void objectAdded(const QModelIndex& object); void aboutToRemoveObject(const QModelIndex& index); void objectModified(LDObject* object); void windingChanged(Winding newWinding); + void transformationMatrixChanged(const Matrix& newMatrix); protected: template<typename T, typename... Args> T* constructObject(Args&& ...args); @@ -137,6 +157,8 @@ mutable int _triangleCount = 0; mutable bool _needsTriangleRecount; Winding _winding = NoWinding; + GLRotationMatrix storedTransformationmatrix; + GLRotationMatrix invertedTransformationmatrix; private: void installObject(int row, LDObject* object);
--- a/src/types/matrix.cpp Fri Mar 30 14:30:51 2018 +0300 +++ b/src/types/matrix.cpp Sat Mar 31 18:41:24 2018 +0300 @@ -51,6 +51,13 @@ } } +Matrix::Matrix(const QGenericMatrix<3, 3>& other) +{ + for (int i = 0; i < 3; i += 1) + for (int j = 0; j < 3; j += 1) + (*this)(i, j) = other(i, j); +} + /* * Returns a string representation of the matrix */
--- a/src/types/matrix.h Fri Mar 30 14:30:51 2018 +0300 +++ b/src/types/matrix.h Sat Mar 31 18:41:24 2018 +0300 @@ -19,6 +19,7 @@ #pragma once #include <QString> #include <QMetaType> +#include <QGenericMatrix> /* * A mathematical 3 × 3 matrix @@ -31,6 +32,7 @@ Matrix(); Matrix (const std::initializer_list<double>& values); + Matrix(const QGenericMatrix<3, 3>& other); Matrix (double fillval); double* begin();
--- a/src/widgets/vertexobjecteditor.cpp Fri Mar 30 14:30:51 2018 +0300 +++ b/src/widgets/vertexobjecteditor.cpp Sat Mar 31 18:41:24 2018 +0300 @@ -18,14 +18,20 @@ #include <QDoubleSpinBox> #include "../linetypes/modelobject.h" +#include "../model.h" #include "vertexobjecteditor.h" #include "ui_vertexobjecteditor.h" #include "../dialogs/colorselector.h" #include "../guiutilities.h" -VertexObjectEditor::VertexObjectEditor(LDObject* object, QWidget *parent) : +VertexObjectEditor::VertexObjectEditor( + LDObject* object, + Model* model, + QWidget *parent +) : QDialog {parent}, object {object}, + model {model}, ui {*new Ui_VertexObjectEditor}, vertexGrid {new QGridLayout} { @@ -61,6 +67,7 @@ for (Axis axis : {X, Y, Z}) { QDoubleSpinBox* spinbox = this->spinboxAt(i, axis); + Vertex vertex = this->model->map(this->object->vertex(i)); if (spinbox) spinbox->setValue(this->object->vertex(i)[axis]); @@ -93,7 +100,7 @@ vertex.setCoordinate(axis, spinbox->value()); } - this->object->setVertex(i, vertex); + this->object->setVertex(i, this->model->unmap(vertex)); } this->object->setColor(this->currentColor);
--- a/src/widgets/vertexobjecteditor.h Fri Mar 30 14:30:51 2018 +0300 +++ b/src/widgets/vertexobjecteditor.h Sat Mar 31 18:41:24 2018 +0300 @@ -20,12 +20,18 @@ #include <QDialog> #include "../main.h" +class Model; + class VertexObjectEditor : public QDialog { Q_OBJECT public: - VertexObjectEditor(LDObject* object = nullptr, QWidget* parent = nullptr); + VertexObjectEditor( + LDObject* object, + Model* model, + QWidget* parent = nullptr + ); ~VertexObjectEditor(); void accept() override; @@ -36,5 +42,6 @@ class Ui_VertexObjectEditor& ui; class QGridLayout* vertexGrid; LDObject* const object; + Model* const model; LDColor currentColor; };