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