Simplify PolygonCache

Wed, 25 May 2022 17:42:02 +0300

author
Teemu Piippo <teemu@hecknology.net>
date
Wed, 25 May 2022 17:42:02 +0300
changeset 193
b4beff48bb7a
parent 192
e6faeffed1d1
child 194
be056e87c8ca

Simplify PolygonCache

src/documentmanager.cpp file | annotate | diff | comparison | revisions
src/documentmanager.h file | annotate | diff | comparison | revisions
src/gl/compiler.cpp file | annotate | diff | comparison | revisions
src/linetypes/subfilereference.cpp file | annotate | diff | comparison | revisions
src/polygoncache.cpp file | annotate | diff | comparison | revisions
src/polygoncache.h file | annotate | diff | comparison | revisions
--- a/src/documentmanager.cpp	Wed May 25 17:25:24 2022 +0300
+++ b/src/documentmanager.cpp	Wed May 25 17:42:02 2022 +0300
@@ -370,10 +370,21 @@
 	Model* model = this->getModelById(modelId);
 	if (model != nullptr)
 	{
-		this->polygonCaches.emplace(
-			std::piecewise_construct,
-			std::forward_as_tuple(modelId),
-			std::forward_as_tuple(model));
+		this->polygonCaches[modelId] = {};
+		connect(model, &Model::dataChanged, this, &DocumentManager::modelModified);
+		connect(model, &Model::rowsInserted, this, &DocumentManager::modelModified);
+		connect(model, &Model::rowsRemoved, this, &DocumentManager::modelModified);
+	}
+}
+
+void DocumentManager::modelModified()
+{
+	Model* model = qobject_cast<Model*>(this->sender());
+	for (auto it = this->openModels.begin(); it != this->openModels.end(); ++it) {
+		if (model == it->second.model.get()) {
+			this->polygonCaches[it->second.id].needRecache = true;
+			break;
+		}
 	}
 }
 
--- a/src/documentmanager.h	Wed May 25 17:25:24 2022 +0300
+++ b/src/documentmanager.h	Wed May 25 17:42:02 2022 +0300
@@ -75,6 +75,7 @@
 	void collectReferences(QSet<QString> &referenced, const QString& name, const Model* model);
 	void updateDependencies(ModelInfo* model);
 	void prune();
+	Q_SLOT void modelModified();
 	bool isReferencedByAnything(const ModelId modelId) const;
 	void makePolygonCacheForModel(const ModelId modelId);
 };
--- a/src/gl/compiler.cpp	Wed May 25 17:25:24 2022 +0300
+++ b/src/gl/compiler.cpp	Wed May 25 17:42:02 2022 +0300
@@ -218,10 +218,10 @@
 	std::optional<ModelId> modelId = context->findIdForModel(model);
 	if (modelId.has_value())
 	{
-		PolygonCache* polygonCache= context->getPolygonCacheForModel(modelId.value());
-		if (polygonCache != nullptr)
+		PolygonCache* cache = context->getPolygonCacheForModel(modelId.value());
+		if (cache != nullptr)
 		{
-			for (const gl::Polygon& polygon : polygonCache->getPolygons(context))
+			for (const gl::Polygon& polygon : getCachedPolygons(cache, model, context))
 			{
 				fn(polygon);
 			}
--- a/src/linetypes/subfilereference.cpp	Wed May 25 17:25:24 2022 +0300
+++ b/src/linetypes/subfilereference.cpp	Wed May 25 17:42:02 2022 +0300
@@ -43,19 +43,22 @@
 ) const
 {
 	Model* dependency = this->resolve(context->modelId, context->documents);
-	PolygonCache* referencedModelPolygonBuilder = nullptr;
+	PolygonCache* cache = nullptr;
 	if (dependency != nullptr)
 	{
 		const auto dependencyModelId = context->documents->findIdForModel(dependency);
 		if (dependencyModelId.has_value())
 		{
-			referencedModelPolygonBuilder = context->documents->getPolygonCacheForModel(dependencyModelId.value());
+			cache = context->documents->getPolygonCacheForModel(dependencyModelId.value());
 		}
 	}
-	if (referencedModelPolygonBuilder != nullptr)
+	if (cache != nullptr)
 	{
 		const bool needInverting = glm::determinant(this->transformation) < 0;
-		const std::vector<gl::Polygon> modelPolygons = referencedModelPolygonBuilder->getPolygons(context->documents);
+		const std::vector<gl::Polygon> modelPolygons = getCachedPolygons(
+			cache,
+			dependency,
+			context->documents);
 		polygons.reserve(polygons.size() + modelPolygons.size());
 		for (gl::Polygon polygon : modelPolygons)
 		{
--- a/src/polygoncache.cpp	Wed May 25 17:25:24 2022 +0300
+++ b/src/polygoncache.cpp	Wed May 25 17:42:02 2022 +0300
@@ -1,14 +1,11 @@
 #include "polygoncache.h"
 #include "documentmanager.h"
 
-PolygonCache::PolygonCache(Model *model) :
-	model{model}
-{
-	const auto mark = [this](){ this->needRecache = true; };
-	connect(model, &Model::dataChanged, mark);
-	connect(model, &Model::rowsInserted, mark);
-	connect(model, &Model::rowsRemoved, mark);
-}
+static void getObjectPolygons(
+	Model* model,
+	const int index,
+	std::vector<gl::Polygon>& polygons_out,
+	ldraw::GetPolygonsContext* context);
 
 /**
  * @brief Gets a list of GL polygons that are used to represent this model.
@@ -16,23 +13,26 @@
  * @param documents Documents to use to resolve subfile references
  * @return vector of GL polygons
  */
-const std::vector<gl::Polygon> &PolygonCache::getPolygons(DocumentManager* documents)
+const std::vector<gl::Polygon> &getCachedPolygons(
+	PolygonCache *cache,
+	Model *model,
+	DocumentManager *documents)
 {
-	if (this->needRecache)
+	if (cache->needRecache)
 	{
-		this->cachedPolygons.clear();
-		const std::optional<ModelId> modelId = documents->findIdForModel(this->model);
+		cache->cachedPolygons.clear();
+		const std::optional<ModelId> modelId = documents->findIdForModel(model);
 		if (modelId.has_value())
 		{
 			ldraw::GetPolygonsContext context{modelId.value(), documents};
-			for (int i = 0; i < this->model->size(); i += 1)
+			for (int i = 0; i < model->size(); i += 1)
 			{
-				this->getObjectPolygons(i, this->cachedPolygons, &context);
+				getObjectPolygons(model, i, cache->cachedPolygons, &context);
 			}
 		}
-		this->needRecache = false;
+		cache->needRecache = false;
 	}
-	return this->cachedPolygons;
+	return cache->cachedPolygons;
 }
 
 /**
@@ -41,11 +41,12 @@
  * @param polygons_out Vector to add polygons into
  * @param context Context to use to resolve subfile references
  */
-void PolygonCache::getObjectPolygons(
+static void getObjectPolygons(
+	Model* model,
 	const int index,
 	std::vector<gl::Polygon>& polygons_out,
-	ldraw::GetPolygonsContext* context) const
+	ldraw::GetPolygonsContext* context)
 {
-	const ldraw::Object* object = (*this->model)[unsigned_cast(index)];
+	const ldraw::Object* object = (*model)[unsigned_cast(index)];
 	object->getPolygons(polygons_out, context);
 }
--- a/src/polygoncache.h	Wed May 25 17:25:24 2022 +0300
+++ b/src/polygoncache.h	Wed May 25 17:42:02 2022 +0300
@@ -2,18 +2,13 @@
 #include "main.h"
 #include "model.h"
 
-class PolygonCache : QObject
+struct PolygonCache
 {
-	Q_OBJECT
-public:
-	PolygonCache(Model* model);
-	const std::vector<gl::Polygon>& getPolygons(class DocumentManager* documents);
-private:
-	void getObjectPolygons(
-		const int index,
-		std::vector<gl::Polygon>& polygons_out,
-		ldraw::GetPolygonsContext* context) const;
-	Model* const model;
 	std::vector<gl::Polygon> cachedPolygons;
 	bool needRecache = true;
 };
+
+const std::vector<gl::Polygon>& getCachedPolygons(
+	PolygonCache* cache,
+	Model* model,
+	class DocumentManager* documents);

mercurial