1 #include "polygoncache.h" |
1 #include "polygoncache.h" |
2 #include "documentmanager.h" |
2 #include "documentmanager.h" |
3 |
3 |
4 PolygonCache::PolygonCache(Model *model) : |
4 static void getObjectPolygons( |
5 model{model} |
5 Model* model, |
6 { |
6 const int index, |
7 const auto mark = [this](){ this->needRecache = true; }; |
7 std::vector<gl::Polygon>& polygons_out, |
8 connect(model, &Model::dataChanged, mark); |
8 ldraw::GetPolygonsContext* context); |
9 connect(model, &Model::rowsInserted, mark); |
|
10 connect(model, &Model::rowsRemoved, mark); |
|
11 } |
|
12 |
9 |
13 /** |
10 /** |
14 * @brief Gets a list of GL polygons that are used to represent this model. |
11 * @brief Gets a list of GL polygons that are used to represent this model. |
15 * @details Will build polygons if polygons are outdated. |
12 * @details Will build polygons if polygons are outdated. |
16 * @param documents Documents to use to resolve subfile references |
13 * @param documents Documents to use to resolve subfile references |
17 * @return vector of GL polygons |
14 * @return vector of GL polygons |
18 */ |
15 */ |
19 const std::vector<gl::Polygon> &PolygonCache::getPolygons(DocumentManager* documents) |
16 const std::vector<gl::Polygon> &getCachedPolygons( |
|
17 PolygonCache *cache, |
|
18 Model *model, |
|
19 DocumentManager *documents) |
20 { |
20 { |
21 if (this->needRecache) |
21 if (cache->needRecache) |
22 { |
22 { |
23 this->cachedPolygons.clear(); |
23 cache->cachedPolygons.clear(); |
24 const std::optional<ModelId> modelId = documents->findIdForModel(this->model); |
24 const std::optional<ModelId> modelId = documents->findIdForModel(model); |
25 if (modelId.has_value()) |
25 if (modelId.has_value()) |
26 { |
26 { |
27 ldraw::GetPolygonsContext context{modelId.value(), documents}; |
27 ldraw::GetPolygonsContext context{modelId.value(), documents}; |
28 for (int i = 0; i < this->model->size(); i += 1) |
28 for (int i = 0; i < model->size(); i += 1) |
29 { |
29 { |
30 this->getObjectPolygons(i, this->cachedPolygons, &context); |
30 getObjectPolygons(model, i, cache->cachedPolygons, &context); |
31 } |
31 } |
32 } |
32 } |
33 this->needRecache = false; |
33 cache->needRecache = false; |
34 } |
34 } |
35 return this->cachedPolygons; |
35 return cache->cachedPolygons; |
36 } |
36 } |
37 |
37 |
38 /** |
38 /** |
39 * @brief Gets the GL polygons of the object at the specified position in the model |
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 |
40 * @param index Index of object in the model |
41 * @param polygons_out Vector to add polygons into |
41 * @param polygons_out Vector to add polygons into |
42 * @param context Context to use to resolve subfile references |
42 * @param context Context to use to resolve subfile references |
43 */ |
43 */ |
44 void PolygonCache::getObjectPolygons( |
44 static void getObjectPolygons( |
|
45 Model* model, |
45 const int index, |
46 const int index, |
46 std::vector<gl::Polygon>& polygons_out, |
47 std::vector<gl::Polygon>& polygons_out, |
47 ldraw::GetPolygonsContext* context) const |
48 ldraw::GetPolygonsContext* context) |
48 { |
49 { |
49 const ldraw::Object* object = (*this->model)[unsigned_cast(index)]; |
50 const ldraw::Object* object = (*model)[unsigned_cast(index)]; |
50 object->getPolygons(polygons_out, context); |
51 object->getPolygons(polygons_out, context); |
51 } |
52 } |