Thu, 09 Jun 2022 13:32:55 +0300
more refactoring, dosn't build yet
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 | { |
210
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
205
diff
changeset
|
14 | visitPoints(element, [&transform](glm::vec3& p) { |
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}; |
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
205
diff
changeset
|
16 | }); |
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 | |
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
205
diff
changeset
|
20 | static std::vector<WithId<PolygonElement>> getPolygonsAt( |
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
205
diff
changeset
|
21 | const Model* model, |
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
205
diff
changeset
|
22 | GetPolygonsContext* context) |
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
205
diff
changeset
|
23 | { |
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
205
diff
changeset
|
24 | std::vector<WithId<PolygonElement>> result; |
200 | 25 | for (int i = 0; i < model->size(); i += 1) |
26 | { | |
27 | const ModelElement& element = (*model)[i]; | |
28 | const ModelId id = model->idAt(i); | |
29 | std::visit<void>(overloaded{ | |
30 | [&](const Colored<LineSegment>& edge) { | |
210
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
205
diff
changeset
|
31 | result.push_back({{edge, edge.color}, id}); |
200 | 32 | }, |
33 | [&](const Colored<Triangle>& triangle) { | |
210
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
205
diff
changeset
|
34 | result.push_back({{triangle, triangle.color}, id}); |
200 | 35 | }, |
36 | [&](const Colored<Quadrilateral>& quad) { | |
210
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
205
diff
changeset
|
37 | result.push_back({{quad, quad.color}, id}); |
200 | 38 | }, |
39 | [&](const Colored<ConditionalEdge>& cedge) { | |
210
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
205
diff
changeset
|
40 | result.push_back({{cedge, cedge.color}, id}); |
200 | 41 | }, |
42 | [&](const Colored<SubfileReference>& ref) { | |
43 | Model* dependency = context->documents->findDependencyByName(context->modelId, ref.name); | |
44 | PolygonCache* cache = nullptr; | |
45 | if (dependency != nullptr) | |
46 | { | |
47 | const auto dependencyModelId = context->documents->findIdForModel(dependency); | |
48 | if (dependencyModelId.has_value()) { | |
49 | cache = context->documents->getPolygonCacheForModel(dependencyModelId.value()); | |
50 | } | |
51 | } | |
52 | if (cache != nullptr) { | |
53 | const bool needInverting = glm::determinant(ref.transformation) < 0; | |
210
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
205
diff
changeset
|
54 | const auto& modelPolygons = getCachedPolygons( |
200 | 55 | cache, |
56 | dependency, | |
57 | context->documents); | |
58 | result.reserve(result.size() + modelPolygons.size()); | |
210
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
205
diff
changeset
|
59 | for (WithId<PolygonElement> polygon : modelPolygons) { |
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
205
diff
changeset
|
60 | 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
|
61 | if (needInverting != ref.inverted) { |
200 | 62 | gl::invert(polygon); |
63 | } | |
210
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
205
diff
changeset
|
64 | if (polygon.color == MAIN_COLOR) { |
200 | 65 | polygon.color = ref.color; |
66 | } | |
67 | polygon.id = id; | |
68 | result.push_back(polygon); | |
69 | } | |
70 | } | |
71 | }, | |
72 | [](const ModelElement&) {} | |
73 | }, element); | |
74 | } | |
75 | return result; | |
76 | } | |
150
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
77 | |
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
78 | /** |
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
79 | * @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
|
80 | * @details Will build polygons if polygons are outdated. |
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
81 | * @param documents Documents to use to resolve subfile references |
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
82 | * @return vector of GL polygons |
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
83 | */ |
210
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
205
diff
changeset
|
84 | const std::vector<WithId<PolygonElement>> &getCachedPolygons( |
193 | 85 | PolygonCache *cache, |
86 | Model *model, | |
87 | DocumentManager *documents) | |
150
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
88 | { |
193 | 89 | if (cache->needRecache) |
150
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
90 | { |
193 | 91 | cache->cachedPolygons.clear(); |
92 | const std::optional<ModelId> modelId = documents->findIdForModel(model); | |
150
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
93 | if (modelId.has_value()) |
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
94 | { |
200 | 95 | GetPolygonsContext context{modelId.value(), documents}; |
96 | cache->cachedPolygons = getPolygonsAt(model, &context); | |
150
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
97 | } |
193 | 98 | cache->needRecache = false; |
150
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
99 | } |
193 | 100 | return cache->cachedPolygons; |
150
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
101 | } |