src/polygoncache.cpp

changeset 211
b27b90fb993f
parent 210
232e7634cc8a
child 232
8efa3a33172e
equal deleted inserted replaced
210:232e7634cc8a 211:b27b90fb993f
9 9
10 static PolygonElement transformed( 10 static PolygonElement transformed(
11 PolygonElement element, 11 PolygonElement element,
12 const glm::mat4& transform) 12 const glm::mat4& transform)
13 { 13 {
14 visitPoints(element, [&transform](glm::vec3& p) { 14 visitPoints([&transform](glm::vec3& p) {
15 p = transform * glm::vec4{p, 1}; 15 p = transform * glm::vec4{p, 1};
16 }); 16 }, element);
17 return element; 17 return element;
18 }
19
20 PolygonCache* findPolygonCacheForModel(Model* model, DocumentManager* context)
21 {
22 std::optional<ModelId> modelId = context->findIdForModel(model);
23 if (modelId.has_value()) {
24 return context->getPolygonCacheForModel(modelId.value());
25 }
26 else {
27 return nullptr;
28 }
18 } 29 }
19 30
20 static std::vector<WithId<PolygonElement>> getPolygonsAt( 31 static std::vector<WithId<PolygonElement>> getPolygonsAt(
21 const Model* model, 32 const Model* model,
22 GetPolygonsContext* context) 33 GetPolygonsContext* context)
37 result.push_back({{quad, quad.color}, id}); 48 result.push_back({{quad, quad.color}, id});
38 }, 49 },
39 [&](const Colored<ConditionalEdge>& cedge) { 50 [&](const Colored<ConditionalEdge>& cedge) {
40 result.push_back({{cedge, cedge.color}, id}); 51 result.push_back({{cedge, cedge.color}, id});
41 }, 52 },
42 [&](const Colored<SubfileReference>& ref) { 53 [&result, &id, context](const Colored<SubfileReference>& ref) {
43 Model* dependency = context->documents->findDependencyByName(context->modelId, ref.name); 54 Model* dependency = context->documents->findDependencyByName(context->modelId, ref.name);
44 PolygonCache* cache = nullptr; 55 PolygonCache* cache = nullptr;
45 if (dependency != nullptr) 56 if (dependency != nullptr) {
46 { 57 cache = findPolygonCacheForModel(dependency, context->documents);
47 const auto dependencyModelId = context->documents->findIdForModel(dependency);
48 if (dependencyModelId.has_value()) {
49 cache = context->documents->getPolygonCacheForModel(dependencyModelId.value());
50 }
51 } 58 }
52 if (cache != nullptr) { 59 if (cache != nullptr) {
53 const bool needInverting = glm::determinant(ref.transformation) < 0; 60 const bool needInverting = glm::determinant(ref.transformation) < 0;
54 const auto& modelPolygons = getCachedPolygons( 61 const PolygonCache::vector_type* modelPolygons = getCachedPolygons(
55 cache, 62 cache,
56 dependency, 63 dependency,
57 context->documents); 64 context->documents);
58 result.reserve(result.size() + modelPolygons.size()); 65 reserveMore(result, modelPolygons->size());
59 for (WithId<PolygonElement> polygon : modelPolygons) { 66 for (WithId<PolygonElement> polygon : *modelPolygons) {
60 polygon = {transformed(polygon, ref.transformation), polygon.id}; 67 polygon = {transformed(polygon, ref.transformation), polygon.id};
61 if (needInverting != ref.inverted) { 68 if (needInverting != ref.inverted) {
62 gl::invert(polygon); 69 gl::invert(polygon);
63 } 70 }
64 if (polygon.color == MAIN_COLOR) { 71 if (polygon.color == MAIN_COLOR) {
79 * @brief Gets a list of GL polygons that are used to represent this model. 86 * @brief Gets a list of GL polygons that are used to represent this model.
80 * @details Will build polygons if polygons are outdated. 87 * @details Will build polygons if polygons are outdated.
81 * @param documents Documents to use to resolve subfile references 88 * @param documents Documents to use to resolve subfile references
82 * @return vector of GL polygons 89 * @return vector of GL polygons
83 */ 90 */
84 const std::vector<WithId<PolygonElement>> &getCachedPolygons( 91 const PolygonCache::vector_type* getCachedPolygons(
85 PolygonCache *cache, 92 PolygonCache *cache,
86 Model *model, 93 Model *model,
87 DocumentManager *documents) 94 DocumentManager *documents)
88 { 95 {
89 if (cache->needRecache) 96 if (cache->needRecache)
95 GetPolygonsContext context{modelId.value(), documents}; 102 GetPolygonsContext context{modelId.value(), documents};
96 cache->cachedPolygons = getPolygonsAt(model, &context); 103 cache->cachedPolygons = getPolygonsAt(model, &context);
97 } 104 }
98 cache->needRecache = false; 105 cache->needRecache = false;
99 } 106 }
100 return cache->cachedPolygons; 107 return &cache->cachedPolygons;
101 } 108 }

mercurial