--- a/src/polygoncache.cpp Thu Jun 09 13:32:55 2022 +0300 +++ b/src/polygoncache.cpp Thu Jun 09 19:11:27 2022 +0300 @@ -11,12 +11,23 @@ PolygonElement element, const glm::mat4& transform) { - visitPoints(element, [&transform](glm::vec3& p) { + visitPoints([&transform](glm::vec3& p) { p = transform * glm::vec4{p, 1}; - }); + }, element); return element; } +PolygonCache* findPolygonCacheForModel(Model* model, DocumentManager* context) +{ + std::optional<ModelId> modelId = context->findIdForModel(model); + if (modelId.has_value()) { + return context->getPolygonCacheForModel(modelId.value()); + } + else { + return nullptr; + } +} + static std::vector<WithId<PolygonElement>> getPolygonsAt( const Model* model, GetPolygonsContext* context) @@ -39,24 +50,20 @@ [&](const Colored<ConditionalEdge>& cedge) { result.push_back({{cedge, cedge.color}, id}); }, - [&](const Colored<SubfileReference>& ref) { + [&result, &id, context](const Colored<SubfileReference>& ref) { Model* dependency = context->documents->findDependencyByName(context->modelId, ref.name); PolygonCache* cache = nullptr; - if (dependency != nullptr) - { - const auto dependencyModelId = context->documents->findIdForModel(dependency); - if (dependencyModelId.has_value()) { - cache = context->documents->getPolygonCacheForModel(dependencyModelId.value()); - } + if (dependency != nullptr) { + cache = findPolygonCacheForModel(dependency, context->documents); } if (cache != nullptr) { const bool needInverting = glm::determinant(ref.transformation) < 0; - const auto& modelPolygons = getCachedPolygons( + const PolygonCache::vector_type* modelPolygons = getCachedPolygons( cache, dependency, context->documents); - result.reserve(result.size() + modelPolygons.size()); - for (WithId<PolygonElement> polygon : modelPolygons) { + reserveMore(result, modelPolygons->size()); + for (WithId<PolygonElement> polygon : *modelPolygons) { polygon = {transformed(polygon, ref.transformation), polygon.id}; if (needInverting != ref.inverted) { gl::invert(polygon); @@ -81,7 +88,7 @@ * @param documents Documents to use to resolve subfile references * @return vector of GL polygons */ -const std::vector<WithId<PolygonElement>> &getCachedPolygons( +const PolygonCache::vector_type* getCachedPolygons( PolygonCache *cache, Model *model, DocumentManager *documents) @@ -97,5 +104,5 @@ } cache->needRecache = false; } - return cache->cachedPolygons; + return &cache->cachedPolygons; }