Reduce indent level

Sun, 03 Jul 2022 13:21:49 +0300

author
Teemu Piippo <teemu.s.piippo@gmail.com>
date
Sun, 03 Jul 2022 13:21:49 +0300
changeset 318
216f02b50b0a
parent 317
852021f38b66
child 319
9727e545b0bc

Reduce indent level

src/polygoncache.cpp file | annotate | diff | comparison | revisions
--- a/src/polygoncache.cpp	Sun Jul 03 12:20:26 2022 +0300
+++ b/src/polygoncache.cpp	Sun Jul 03 13:21:49 2022 +0300
@@ -29,6 +29,45 @@
 	}
 }
 
+constexpr bool n_xor(bool a)
+{
+	return a;
+}
+
+template<typename... Args>
+constexpr bool n_xor(bool a, Args&&... rest)
+{
+	return a xor n_xor(rest...);
+}
+
+template<typename Fn, typename Fn2>
+static void inlineSubfileReference(
+	const PolygonCache::vector_type& polygons,
+	const Colored<SubfileReference>& ref,
+	Fn&& add,
+	Fn2&& reserve)
+{
+	const bool needInverting = 0
+		^ (glm::determinant(ref.transformation) < 0)
+		^ (ref.inverted);
+	reserve(polygons.size());
+	for (const PolygonElement& cacheElement : polygons) {
+		PolygonElement polygon = transformed(cacheElement, ref.transformation);
+		if (needInverting) {
+			gl::invert(polygon);
+		}
+		if (polygon.color == MAIN_COLOR) {
+			polygon.color = ref.color;
+		}
+		add(polygon);
+	}
+}
+
+Model* findDependency(const SubfileReference& ref, GetPolygonsContext* context)
+{
+	return context->documents->findDependencyByName(context->modelId, ref.name);
+}
+
 template<typename Fn, typename Fn2>
 static void collectPolygons(
 	const ModelElement& element,
@@ -51,25 +90,13 @@
 			add({cedge, cedge.color});
 		},
 		[&add, context, &reserve](const Colored<SubfileReference>& ref) {
-			Model* dependency = context->documents->findDependencyByName(context->modelId, ref.name);
-			PolygonCache* cache = nullptr;
-			if (dependency != nullptr) {
-				cache = findPolygonCacheForModel(dependency, context->documents);
-			}
-			if (cache != nullptr) {
-				const bool needInverting = glm::determinant(ref.transformation) < 0;
+			Model* const dependency = findDependency(ref, context);
+			if (PolygonCache* cache = (dependency != nullptr)
+				? findPolygonCacheForModel(dependency, context->documents)
+				: nullptr
+			) {
 				recacheIfNeeded(cache, dependency, context->documents);
-				reserve(cache->polygons.size());
-				for (const PolygonElement& cacheElement : cache->polygons) {
-					PolygonElement polygon = transformed(cacheElement, ref.transformation);
-					if (needInverting != ref.inverted) {
-						gl::invert(polygon);
-					}
-					if (polygon.color == MAIN_COLOR) {
-						polygon.color = ref.color;
-					}
-					add(polygon);
-				}
+				inlineSubfileReference(cache->polygons, ref, add, reserve);
 			}
 		},
 		[&add](const Colored<CircularPrimitive>& circ) {

mercurial