5 Model* resolve(const QString& name, const ModelId callingModelId, DocumentManager* documents) |
5 Model* resolve(const QString& name, const ModelId callingModelId, DocumentManager* documents) |
6 { |
6 { |
7 return documents->findDependencyByName(callingModelId, name); |
7 return documents->findDependencyByName(callingModelId, name); |
8 } |
8 } |
9 |
9 |
10 /** |
10 static PolygonElement transformed( |
11 * @brief Gets the GL polygons of the object at the specified position in the model |
11 PolygonElement element, |
12 * @param index Index of object in the model |
12 const glm::mat4& transform) |
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 { |
13 { |
18 std::vector<gl::Polygon> result; |
14 visitPoints(element, [&transform](glm::vec3& p) { |
|
15 p = transform * glm::vec4{p, 1}; |
|
16 }); |
|
17 return element; |
|
18 } |
|
19 |
|
20 static std::vector<WithId<PolygonElement>> getPolygonsAt( |
|
21 const Model* model, |
|
22 GetPolygonsContext* context) |
|
23 { |
|
24 std::vector<WithId<PolygonElement>> result; |
19 for (int i = 0; i < model->size(); i += 1) |
25 for (int i = 0; i < model->size(); i += 1) |
20 { |
26 { |
21 const ModelElement& element = (*model)[i]; |
27 const ModelElement& element = (*model)[i]; |
22 const ModelId id = model->idAt(i); |
28 const ModelId id = model->idAt(i); |
23 std::visit<void>(overloaded{ |
29 std::visit<void>(overloaded{ |
24 [&](const Colored<LineSegment>& edge) { |
30 [&](const Colored<LineSegment>& edge) { |
25 result.push_back(gl::edgeLine(edge, id)); |
31 result.push_back({{edge, edge.color}, id}); |
26 }, |
32 }, |
27 [&](const Colored<Triangle>& triangle) { |
33 [&](const Colored<Triangle>& triangle) { |
28 result.push_back(gl::triangle(triangle, id)); |
34 result.push_back({{triangle, triangle.color}, id}); |
29 }, |
35 }, |
30 [&](const Colored<Quadrilateral>& quad) { |
36 [&](const Colored<Quadrilateral>& quad) { |
31 result.push_back(gl::quadrilateral(quad, id)); |
37 result.push_back({{quad, quad.color}, id}); |
32 }, |
38 }, |
33 [&](const Colored<ConditionalEdge>& cedge) { |
39 [&](const Colored<ConditionalEdge>& cedge) { |
34 result.push_back(gl::conditionalEdge(cedge, id)); |
40 result.push_back({{cedge, cedge.color}, id}); |
35 }, |
41 }, |
36 [&](const Colored<SubfileReference>& ref) { |
42 [&](const Colored<SubfileReference>& ref) { |
37 Model* dependency = context->documents->findDependencyByName(context->modelId, ref.name); |
43 Model* dependency = context->documents->findDependencyByName(context->modelId, ref.name); |
38 PolygonCache* cache = nullptr; |
44 PolygonCache* cache = nullptr; |
39 if (dependency != nullptr) |
45 if (dependency != nullptr) |
43 cache = context->documents->getPolygonCacheForModel(dependencyModelId.value()); |
49 cache = context->documents->getPolygonCacheForModel(dependencyModelId.value()); |
44 } |
50 } |
45 } |
51 } |
46 if (cache != nullptr) { |
52 if (cache != nullptr) { |
47 const bool needInverting = glm::determinant(ref.transformation) < 0; |
53 const bool needInverting = glm::determinant(ref.transformation) < 0; |
48 const std::vector<gl::Polygon>& modelPolygons = getCachedPolygons( |
54 const auto& modelPolygons = getCachedPolygons( |
49 cache, |
55 cache, |
50 dependency, |
56 dependency, |
51 context->documents); |
57 context->documents); |
52 result.reserve(result.size() + modelPolygons.size()); |
58 result.reserve(result.size() + modelPolygons.size()); |
53 for (gl::Polygon polygon : modelPolygons) |
59 for (WithId<PolygonElement> polygon : modelPolygons) { |
54 { |
60 polygon = {transformed(polygon, ref.transformation), polygon.id}; |
55 for (unsigned int i = 0; i < polygon.numPolygonVertices(); i += 1) |
61 if (needInverting != ref.inverted) { |
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); |
62 gl::invert(polygon); |
63 } |
63 } |
64 if (polygon.color == MAIN_COLOR) |
64 if (polygon.color == MAIN_COLOR) { |
65 { |
|
66 polygon.color = ref.color; |
65 polygon.color = ref.color; |
67 } |
66 } |
68 polygon.id = id; |
67 polygon.id = id; |
69 result.push_back(polygon); |
68 result.push_back(polygon); |
70 } |
69 } |
80 * @brief Gets a list of GL polygons that are used to represent this model. |
79 * @brief Gets a list of GL polygons that are used to represent this model. |
81 * @details Will build polygons if polygons are outdated. |
80 * @details Will build polygons if polygons are outdated. |
82 * @param documents Documents to use to resolve subfile references |
81 * @param documents Documents to use to resolve subfile references |
83 * @return vector of GL polygons |
82 * @return vector of GL polygons |
84 */ |
83 */ |
85 const std::vector<gl::Polygon> &getCachedPolygons( |
84 const std::vector<WithId<PolygonElement>> &getCachedPolygons( |
86 PolygonCache *cache, |
85 PolygonCache *cache, |
87 Model *model, |
86 Model *model, |
88 DocumentManager *documents) |
87 DocumentManager *documents) |
89 { |
88 { |
90 if (cache->needRecache) |
89 if (cache->needRecache) |