src/polygoncache.cpp

changeset 200
ca23936b455b
parent 193
b4beff48bb7a
child 205
1a4342d80de7
equal deleted inserted replaced
199:6988973515d2 200:ca23936b455b
1 #include "polygoncache.h" 1 #include "polygoncache.h"
2 #include "documentmanager.h" 2 #include "documentmanager.h"
3 #include "invert.h"
3 4
4 static void getObjectPolygons( 5 Model* resolve(const QString& name, const ModelId callingModelId, DocumentManager* documents)
5 Model* model, 6 {
6 const int index, 7 return documents->findDependencyByName(callingModelId, name);
7 std::vector<gl::Polygon>& polygons_out, 8 }
8 ldraw::GetPolygonsContext* context); 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 }
9 78
10 /** 79 /**
11 * @brief Gets a list of GL polygons that are used to represent this model. 80 * @brief Gets a list of GL polygons that are used to represent this model.
12 * @details Will build polygons if polygons are outdated. 81 * @details Will build polygons if polygons are outdated.
13 * @param documents Documents to use to resolve subfile references 82 * @param documents Documents to use to resolve subfile references
22 { 91 {
23 cache->cachedPolygons.clear(); 92 cache->cachedPolygons.clear();
24 const std::optional<ModelId> modelId = documents->findIdForModel(model); 93 const std::optional<ModelId> modelId = documents->findIdForModel(model);
25 if (modelId.has_value()) 94 if (modelId.has_value())
26 { 95 {
27 ldraw::GetPolygonsContext context{modelId.value(), documents}; 96 GetPolygonsContext context{modelId.value(), documents};
28 for (int i = 0; i < model->size(); i += 1) 97 cache->cachedPolygons = getPolygonsAt(model, &context);
29 {
30 getObjectPolygons(model, i, cache->cachedPolygons, &context);
31 }
32 } 98 }
33 cache->needRecache = false; 99 cache->needRecache = false;
34 } 100 }
35 return cache->cachedPolygons; 101 return cache->cachedPolygons;
36 } 102 }
37
38 /**
39 * @brief Gets the GL polygons of the object at the specified position in the model
40 * @param index Index of object in the model
41 * @param polygons_out Vector to add polygons into
42 * @param context Context to use to resolve subfile references
43 */
44 static void getObjectPolygons(
45 Model* model,
46 const int index,
47 std::vector<gl::Polygon>& polygons_out,
48 ldraw::GetPolygonsContext* context)
49 {
50 const ldraw::Object* object = (*model)[unsigned_cast(index)];
51 object->getPolygons(polygons_out, context);
52 }

mercurial