Tue, 07 Jun 2022 01:37:26 +0300
Continue giant refactor
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 | ||
10 | /** | |
11 | * @brief Gets the GL polygons of the object at the specified position in the model | |
12 | * @param index Index of object in the model | |
13 | * @param polygons_out Vector to add polygons into | |
14 | * @param context Context to use to resolve subfile references | |
15 | */ | |
16 | static std::vector<gl::Polygon> getPolygonsAt(const Model* model, GetPolygonsContext* context) | |
17 | { | |
18 | std::vector<gl::Polygon> result; | |
19 | for (int i = 0; i < model->size(); i += 1) | |
20 | { | |
21 | const ModelElement& element = (*model)[i]; | |
22 | const ModelId id = model->idAt(i); | |
23 | std::visit<void>(overloaded{ | |
24 | [&](const Colored<LineSegment>& edge) { | |
25 | result.push_back(gl::edgeLine(edge, id)); | |
26 | }, | |
27 | [&](const Colored<Triangle>& triangle) { | |
28 | result.push_back(gl::triangle(triangle, id)); | |
29 | }, | |
30 | [&](const Colored<Quadrilateral>& quad) { | |
31 | result.push_back(gl::quadrilateral(quad, id)); | |
32 | }, | |
33 | [&](const Colored<ConditionalEdge>& cedge) { | |
34 | result.push_back(gl::conditionalEdge(cedge, id)); | |
35 | }, | |
36 | [&](const Colored<SubfileReference>& ref) { | |
37 | Model* dependency = context->documents->findDependencyByName(context->modelId, ref.name); | |
38 | PolygonCache* cache = nullptr; | |
39 | if (dependency != nullptr) | |
40 | { | |
41 | const auto dependencyModelId = context->documents->findIdForModel(dependency); | |
42 | if (dependencyModelId.has_value()) { | |
43 | cache = context->documents->getPolygonCacheForModel(dependencyModelId.value()); | |
44 | } | |
45 | } | |
46 | if (cache != nullptr) { | |
47 | const bool needInverting = glm::determinant(ref.transformation) < 0; | |
48 | const std::vector<gl::Polygon>& modelPolygons = getCachedPolygons( | |
49 | cache, | |
50 | dependency, | |
51 | context->documents); | |
52 | result.reserve(result.size() + modelPolygons.size()); | |
53 | for (gl::Polygon polygon : modelPolygons) | |
54 | { | |
55 | for (unsigned int i = 0; i < polygon.numPolygonVertices(); i += 1) | |
56 | { | |
57 | const glm::vec4 homogenousVertex {polygon.vertices[i], 1}; | |
58 | polygon.vertices[i] = ref.transformation * homogenousVertex; | |
59 | } | |
60 | if (needInverting != ref.inverted) | |
61 | { | |
62 | gl::invert(polygon); | |
63 | } | |
64 | if (polygon.color == ldraw::MAIN_COLOR) | |
65 | { | |
66 | polygon.color = ref.color; | |
67 | } | |
68 | polygon.id = id; | |
69 | result.push_back(polygon); | |
70 | } | |
71 | } | |
72 | }, | |
73 | [](const ModelElement&) {} | |
74 | }, element); | |
75 | } | |
76 | return result; | |
77 | } | |
150
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 | /** |
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
80 | * @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
|
81 | * @details Will build polygons if polygons are outdated. |
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
82 | * @param documents Documents to use to resolve subfile references |
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
83 | * @return vector of GL polygons |
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
84 | */ |
193 | 85 | const std::vector<gl::Polygon> &getCachedPolygons( |
86 | PolygonCache *cache, | |
87 | Model *model, | |
88 | DocumentManager *documents) | |
150
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
89 | { |
193 | 90 | if (cache->needRecache) |
150
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
91 | { |
193 | 92 | cache->cachedPolygons.clear(); |
93 | const std::optional<ModelId> modelId = documents->findIdForModel(model); | |
150
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
94 | if (modelId.has_value()) |
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
95 | { |
200 | 96 | GetPolygonsContext context{modelId.value(), documents}; |
97 | cache->cachedPolygons = getPolygonsAt(model, &context); | |
150
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
98 | } |
193 | 99 | cache->needRecache = false; |
150
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
100 | } |
193 | 101 | return cache->cachedPolygons; |
150
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
102 | } |