# HG changeset patch # User Teemu Piippo # Date 1653489722 -10800 # Node ID b4beff48bb7adff3f0027029b1b8ed84c09826c5 # Parent e6faeffed1d1de82ae332daa665b8a4d6278f394 Simplify PolygonCache diff -r e6faeffed1d1 -r b4beff48bb7a src/documentmanager.cpp --- 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(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; + } } } diff -r e6faeffed1d1 -r b4beff48bb7a src/documentmanager.h --- 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 &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); }; diff -r e6faeffed1d1 -r b4beff48bb7a src/gl/compiler.cpp --- 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 = 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); } diff -r e6faeffed1d1 -r b4beff48bb7a src/linetypes/subfilereference.cpp --- 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 modelPolygons = referencedModelPolygonBuilder->getPolygons(context->documents); + const std::vector modelPolygons = getCachedPolygons( + cache, + dependency, + context->documents); polygons.reserve(polygons.size() + modelPolygons.size()); for (gl::Polygon polygon : modelPolygons) { diff -r e6faeffed1d1 -r b4beff48bb7a src/polygoncache.cpp --- 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& 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 &PolygonCache::getPolygons(DocumentManager* documents) +const std::vector &getCachedPolygons( + PolygonCache *cache, + Model *model, + DocumentManager *documents) { - if (this->needRecache) + if (cache->needRecache) { - this->cachedPolygons.clear(); - const std::optional modelId = documents->findIdForModel(this->model); + cache->cachedPolygons.clear(); + const std::optional 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& 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); } diff -r e6faeffed1d1 -r b4beff48bb7a src/polygoncache.h --- 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& getPolygons(class DocumentManager* documents); -private: - void getObjectPolygons( - const int index, - std::vector& polygons_out, - ldraw::GetPolygonsContext* context) const; - Model* const model; std::vector cachedPolygons; bool needRecache = true; }; + +const std::vector& getCachedPolygons( + PolygonCache* cache, + Model* model, + class DocumentManager* documents);