src/polygoncache.cpp

changeset 210
232e7634cc8a
parent 205
1a4342d80de7
child 211
b27b90fb993f
--- a/src/polygoncache.cpp	Thu Jun 09 11:51:42 2022 +0300
+++ b/src/polygoncache.cpp	Thu Jun 09 13:32:55 2022 +0300
@@ -7,31 +7,37 @@
 	return documents->findDependencyByName(callingModelId, name);
 }
 
-/**
- * @brief Gets the GL polygons of the object at the specified position in the model
- * @param index Index of object in the model
- * @param polygons_out Vector to add polygons into
- * @param context Context to use to resolve subfile references
- */
-static std::vector<gl::Polygon> getPolygonsAt(const Model* model, GetPolygonsContext* context)
+static PolygonElement transformed(
+	PolygonElement element,
+	const glm::mat4& transform)
 {
-	std::vector<gl::Polygon> result;
+	visitPoints(element, [&transform](glm::vec3& p) {
+		p = transform * glm::vec4{p, 1};
+	});
+	return element;
+}
+
+static std::vector<WithId<PolygonElement>> getPolygonsAt(
+	const Model* model,
+	GetPolygonsContext* context)
+{
+	std::vector<WithId<PolygonElement>> result;
 	for (int i = 0; i < model->size(); i += 1)
 	{
 		const ModelElement& element = (*model)[i];
 		const ModelId id = model->idAt(i);
 		std::visit<void>(overloaded{
 			[&](const Colored<LineSegment>& edge) {
-				result.push_back(gl::edgeLine(edge, id));
+				result.push_back({{edge, edge.color}, id});
 			},
 			[&](const Colored<Triangle>& triangle) {
-				result.push_back(gl::triangle(triangle, id));
+				result.push_back({{triangle, triangle.color}, id});
 			},
 			[&](const Colored<Quadrilateral>& quad) {
-				result.push_back(gl::quadrilateral(quad, id));
+				result.push_back({{quad, quad.color}, id});
 			},
 			[&](const Colored<ConditionalEdge>& cedge) {
-				result.push_back(gl::conditionalEdge(cedge, id));
+				result.push_back({{cedge, cedge.color}, id});
 			},
 			[&](const Colored<SubfileReference>& ref) {
 				Model* dependency = context->documents->findDependencyByName(context->modelId, ref.name);
@@ -45,24 +51,17 @@
 				}
 				if (cache != nullptr) {
 					const bool needInverting = glm::determinant(ref.transformation) < 0;
-					const std::vector<gl::Polygon>& modelPolygons = getCachedPolygons(
+					const auto& modelPolygons = getCachedPolygons(
 						cache,
 						dependency,
 						context->documents);
 					result.reserve(result.size() + modelPolygons.size());
-					for (gl::Polygon polygon : modelPolygons)
-					{
-						for (unsigned int i = 0; i < polygon.numPolygonVertices(); i += 1)
-						{
-							const glm::vec4 homogenousVertex {polygon.vertices[i], 1};
-							polygon.vertices[i] = ref.transformation * homogenousVertex;
-						}
-						if (needInverting != ref.inverted)
-						{
+					for (WithId<PolygonElement> polygon : modelPolygons) {
+						polygon = {transformed(polygon, ref.transformation), polygon.id};
+						if (needInverting != ref.inverted) {
 							gl::invert(polygon);
 						}
-						if (polygon.color == MAIN_COLOR)
-						{
+						if (polygon.color == MAIN_COLOR) {
 							polygon.color = ref.color;
 						}
 						polygon.id = id;
@@ -82,7 +81,7 @@
  * @param documents Documents to use to resolve subfile references
  * @return vector of GL polygons
  */
-const std::vector<gl::Polygon> &getCachedPolygons(
+const std::vector<WithId<PolygonElement>> &getCachedPolygons(
 	PolygonCache *cache,
 	Model *model,
 	DocumentManager *documents)

mercurial