Tue, 14 Jun 2022 23:04:49 +0300
Edit tools: get rid of the preview polygon and render the result-to-be
150
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
1 | #include "polygoncache.h" |
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
2 | #include "documentmanager.h" |
200 | 3 | #include "invert.h" |
150
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
4 | |
200 | 5 | Model* resolve(const QString& name, const ModelId callingModelId, DocumentManager* documents) |
6 | { | |
7 | return documents->findDependencyByName(callingModelId, name); | |
8 | } | |
9 | ||
210
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
205
diff
changeset
|
10 | static PolygonElement transformed( |
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
205
diff
changeset
|
11 | PolygonElement element, |
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
205
diff
changeset
|
12 | const glm::mat4& transform) |
200 | 13 | { |
211 | 14 | visitPoints([&transform](glm::vec3& p) { |
210
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
205
diff
changeset
|
15 | p = transform * glm::vec4{p, 1}; |
211 | 16 | }, element); |
210
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
205
diff
changeset
|
17 | return element; |
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
205
diff
changeset
|
18 | } |
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
205
diff
changeset
|
19 | |
211 | 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 | } | |
29 | } | |
30 | ||
210
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
205
diff
changeset
|
31 | static std::vector<WithId<PolygonElement>> getPolygonsAt( |
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
205
diff
changeset
|
32 | const Model* model, |
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
205
diff
changeset
|
33 | GetPolygonsContext* context) |
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
205
diff
changeset
|
34 | { |
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
205
diff
changeset
|
35 | std::vector<WithId<PolygonElement>> result; |
200 | 36 | for (int i = 0; i < model->size(); i += 1) |
37 | { | |
38 | const ModelElement& element = (*model)[i]; | |
39 | const ModelId id = model->idAt(i); | |
40 | std::visit<void>(overloaded{ | |
41 | [&](const Colored<LineSegment>& edge) { | |
210
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
205
diff
changeset
|
42 | result.push_back({{edge, edge.color}, id}); |
200 | 43 | }, |
44 | [&](const Colored<Triangle>& triangle) { | |
210
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
205
diff
changeset
|
45 | result.push_back({{triangle, triangle.color}, id}); |
200 | 46 | }, |
47 | [&](const Colored<Quadrilateral>& quad) { | |
210
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
205
diff
changeset
|
48 | result.push_back({{quad, quad.color}, id}); |
200 | 49 | }, |
50 | [&](const Colored<ConditionalEdge>& cedge) { | |
210
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
205
diff
changeset
|
51 | result.push_back({{cedge, cedge.color}, id}); |
200 | 52 | }, |
211 | 53 | [&result, &id, context](const Colored<SubfileReference>& ref) { |
200 | 54 | Model* dependency = context->documents->findDependencyByName(context->modelId, ref.name); |
55 | PolygonCache* cache = nullptr; | |
211 | 56 | if (dependency != nullptr) { |
57 | cache = findPolygonCacheForModel(dependency, context->documents); | |
200 | 58 | } |
59 | if (cache != nullptr) { | |
60 | const bool needInverting = glm::determinant(ref.transformation) < 0; | |
211 | 61 | const PolygonCache::vector_type* modelPolygons = getCachedPolygons( |
200 | 62 | cache, |
63 | dependency, | |
64 | context->documents); | |
211 | 65 | reserveMore(result, modelPolygons->size()); |
66 | for (WithId<PolygonElement> polygon : *modelPolygons) { | |
210
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
205
diff
changeset
|
67 | polygon = {transformed(polygon, ref.transformation), polygon.id}; |
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
205
diff
changeset
|
68 | if (needInverting != ref.inverted) { |
200 | 69 | gl::invert(polygon); |
70 | } | |
210
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
205
diff
changeset
|
71 | if (polygon.color == MAIN_COLOR) { |
200 | 72 | polygon.color = ref.color; |
73 | } | |
74 | polygon.id = id; | |
75 | result.push_back(polygon); | |
76 | } | |
77 | } | |
78 | }, | |
79 | [](const ModelElement&) {} | |
80 | }, element); | |
81 | } | |
82 | return result; | |
83 | } | |
150
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
84 | |
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
85 | /** |
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
86 | * @brief Gets a list of GL polygons that are used to represent this model. |
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
87 | * @details Will build polygons if polygons are outdated. |
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
88 | * @param documents Documents to use to resolve subfile references |
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
89 | * @return vector of GL polygons |
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
90 | */ |
211 | 91 | const PolygonCache::vector_type* getCachedPolygons( |
193 | 92 | PolygonCache *cache, |
93 | Model *model, | |
94 | DocumentManager *documents) | |
150
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
95 | { |
193 | 96 | if (cache->needRecache) |
150
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
97 | { |
193 | 98 | cache->cachedPolygons.clear(); |
99 | const std::optional<ModelId> modelId = documents->findIdForModel(model); | |
150
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
100 | if (modelId.has_value()) |
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
101 | { |
200 | 102 | GetPolygonsContext context{modelId.value(), documents}; |
103 | cache->cachedPolygons = getPolygonsAt(model, &context); | |
150
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
104 | } |
193 | 105 | cache->needRecache = false; |
150
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
106 | } |
211 | 107 | return &cache->cachedPolygons; |
150
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
108 | } |