# HG changeset patch # User Teemu Piippo # Date 1627381414 -10800 # Node ID 121a40d5e34cfc57cdf999c2cb27df50cdf84ce7 # Parent aad3e897bc323bb40a8f18477bc0b2ec18a65207 Add vertex map diff -r aad3e897bc32 -r 121a40d5e34c CMakeLists.txt --- a/CMakeLists.txt Tue Jul 27 12:44:54 2021 +0300 +++ b/CMakeLists.txt Tue Jul 27 13:23:34 2021 +0300 @@ -25,7 +25,7 @@ source_group("3.2 Widgets" REGULAR_EXPRESSION "src/(ui|widgets)/.+\\.(cpp|h|ui)") source_group("3.1 Settings editor" REGULAR_EXPRESSION "src/settingseditor/.+\\.(cpp|h|ui)") source_group("3 User interface" REGULAR_EXPRESSION "src/(mainwindow|document|documentmanager|uiutilities)\\.(cpp|h|ui)") -source_group("2 Model handling" REGULAR_EXPRESSION "src/(model|modeleditcontext|libraries|colors|parser)\\.(cpp|h|ui)") +source_group("2 Model handling" REGULAR_EXPRESSION "src/(model|modeleditcontext|libraries|colors|parser|vertexmap)\\.(cpp|h|ui)") source_group("6 Editing tools" REGULAR_EXPRESSION "src/tools/.+\\.(cpp|h|ui)") set (LDFORGE_SOURCES @@ -42,6 +42,7 @@ src/parser.cpp src/uiutilities.cpp src/version.cpp + src/vertexmap.cpp src/gl/axesprogram.cpp src/gl/basicshaderprogram.cpp src/gl/compiler.cpp @@ -93,6 +94,7 @@ src/uiutilities.h src/utility.h src/version.h + src/vertexmap.h src/gl/axesprogram.h src/gl/basicshaderprogram.h src/gl/common.h diff -r aad3e897bc32 -r 121a40d5e34c src/document.cpp --- a/src/document.cpp Tue Jul 27 12:44:54 2021 +0300 +++ b/src/document.cpp Tue Jul 27 13:23:34 2021 +0300 @@ -33,7 +33,8 @@ colorTable{colorTable}, renderer{new Canvas{model, documents, colorTable, this}}, ui{*new Ui::Document}, - objectEditor{model, ldraw::NULL_ID, this} + objectEditor{model, ldraw::NULL_ID, this}, + vertexMap{model} { this->ui.setupUi(this); this->ui.listView->setModel(model); diff -r aad3e897bc32 -r 121a40d5e34c src/document.h --- a/src/document.h Tue Jul 27 12:44:54 2021 +0300 +++ b/src/document.h Tue Jul 27 13:23:34 2021 +0300 @@ -22,6 +22,7 @@ #include "ui/canvas.h" #include "ui/objecteditor.h" #include "model.h" +#include "vertexmap.h" namespace Ui { @@ -56,4 +57,5 @@ Canvas* renderer; Ui::Document& ui; ObjectEditor objectEditor; + VertexMap vertexMap; }; diff -r aad3e897bc32 -r 121a40d5e34c src/linetypes/propertygenerics.h --- a/src/linetypes/propertygenerics.h Tue Jul 27 12:44:54 2021 +0300 +++ b/src/linetypes/propertygenerics.h Tue Jul 27 13:23:34 2021 +0300 @@ -71,6 +71,8 @@ template inline const char* PROPERTY_NAME = PropertyTraits::name; + constexpr int MAX_POINTS = 4; + struct PropertyKeyValue { Property key; @@ -79,7 +81,7 @@ constexpr Property pointProperty(int n) { - Q_ASSERT(n >= 0 and n < 4); + Q_ASSERT(n >= 0 and n < MAX_POINTS); return static_cast(static_cast(Property::Point0) + n); } diff -r aad3e897bc32 -r 121a40d5e34c src/model.h --- a/src/model.h Tue Jul 27 12:44:54 2021 +0300 +++ b/src/model.h Tue Jul 27 13:23:34 2021 +0300 @@ -58,6 +58,10 @@ }; template Get2Result get2(ldraw::Id id) const; + template + void apply(Fn f); + template + void apply(Fn f) const; Q_SIGNALS: void objectAdded(ldraw::id_t id, int position); private: @@ -94,6 +98,38 @@ }; /** + * \brief Call @c f for all objects of type \c R + */ +template +void Model::apply(Fn f) const +{ + for (const ModelObjectPointer& object : this->body) + { + const R* subobject = dynamic_cast(object.get()); + if (subobject != nullptr) + { + f(subobject); + } + } +} + +/** + * \brief Call @c f for all objects of type \c R + */ +template +void Model::apply(Fn f) +{ + for (ModelObjectPointer& object : this->body) + { + R* subobject = dynamic_cast(object.get()); + if (subobject != nullptr) + { + f(subobject); + } + } +} + +/** * \brief Checks type of object behind id * Checks whether the specified id refers to an object of the specified type. * \returns id casted to subclass if appropriate, null id otherwise diff -r aad3e897bc32 -r 121a40d5e34c src/vertexmap.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/vertexmap.cpp Tue Jul 27 13:23:34 2021 +0300 @@ -0,0 +1,39 @@ +#include "vertexmap.h" +#include "linetypes/polygonobject.h" + +VertexMap::VertexMap(const Model *model) : + model{model} +{ + connect( + model, + &Model::dataChanged, + this, + &VertexMap::build + ); + connect( + model, + &Model::rowsInserted, + this, + &VertexMap::build + ); + connect( + model, + &Model::rowsRemoved, + this, + &VertexMap::build + ); + this->build(); +} + +void VertexMap::build() +{ + this->map.clear(); + this->model->apply([&](const ldraw::Object* object) + { + for (int i = 0; i < object->numPoints(); i += 1) + { + const glm::vec3& point = object->getPoint(i); + this->map[qHash(point)].insert(object->id); + } + }); +} diff -r aad3e897bc32 -r 121a40d5e34c src/vertexmap.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/vertexmap.h Tue Jul 27 13:23:34 2021 +0300 @@ -0,0 +1,18 @@ +#ifndef VERTEXMAP_H +#define VERTEXMAP_H +#include +#include "main.h" +#include "model.h" + +class VertexMap : public QObject +{ + Q_OBJECT +public: + VertexMap(const Model *model); + Q_SLOT void build(); +private: + const Model* const model; + std::map> map; +}; + +#endif // VERTEXMAP_H