Add vertex map

Tue, 27 Jul 2021 13:23:34 +0300

author
Teemu Piippo <teemu@hecknology.net>
date
Tue, 27 Jul 2021 13:23:34 +0300
changeset 117
121a40d5e34c
parent 116
aad3e897bc32
child 118
8e1c9f18ae15

Add vertex map

CMakeLists.txt file | annotate | diff | comparison | revisions
src/document.cpp file | annotate | diff | comparison | revisions
src/document.h file | annotate | diff | comparison | revisions
src/linetypes/propertygenerics.h file | annotate | diff | comparison | revisions
src/model.h file | annotate | diff | comparison | revisions
src/vertexmap.cpp file | annotate | diff | comparison | revisions
src/vertexmap.h file | annotate | diff | comparison | revisions
--- 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
--- 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);
--- 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;
 };
--- 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<ldraw::Property property>
 	inline const char* PROPERTY_NAME = PropertyTraits<property>::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<Property>(static_cast<int>(Property::Point0) + n);
 	}
 
--- 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<typename R>
 	Get2Result<R> get2(ldraw::Id<R> id) const;
+	template<typename R, typename Fn>
+	void apply(Fn f);
+	template<typename R, typename Fn>
+	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<typename R, typename Fn>
+void Model::apply(Fn f) const
+{
+	for (const ModelObjectPointer& object : this->body)
+	{
+		const R* subobject = dynamic_cast<const R*>(object.get());
+		if (subobject != nullptr)
+		{
+			f(subobject);
+		}
+	}
+}
+
+/**
+ * \brief Call @c f for all objects of type \c R
+ */
+template<typename R, typename Fn>
+void Model::apply(Fn f)
+{
+	for (ModelObjectPointer& object : this->body)
+	{
+		R* subobject = dynamic_cast<R*>(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
--- /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<ldraw::Object>([&](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);
+		}
+	});
+}
--- /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 <set>
+#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<unsigned int, std::set<ldraw::id_t>> map;
+};
+
+#endif // VERTEXMAP_H

mercurial