Improve preview of cylinder extrusion

Sat, 02 Jul 2022 19:03:57 +0300

author
Teemu Piippo <teemu.s.piippo@gmail.com>
date
Sat, 02 Jul 2022 19:03:57 +0300
changeset 315
23b47902d857
parent 314
4642ba1218e8
child 316
aab8e139a149

Improve preview of cylinder extrusion

src/layers/edittools.cpp file | annotate | diff | comparison | revisions
src/layers/edittools.h file | annotate | diff | comparison | revisions
--- a/src/layers/edittools.cpp	Fri Jul 01 23:51:16 2022 +0300
+++ b/src/layers/edittools.cpp	Sat Jul 02 19:03:57 2022 +0300
@@ -297,18 +297,30 @@
 			}
 		};
 		const glm::vec3 zvec = this->gridMatrix[2];
-		const glm::vec2 p1 = extremity(this->polygon[0] + zvec);
-		const glm::vec2 p2 = extremity(this->polygon[0] - zvec);
-		const glm::vec2 lateral = glm::normalize(glm::mat2{{0, 1}, {-1, 0}} * (p2 - p1));
-		painter->setPen(QPen{Qt::white, 3});
-		painter->drawLine(vecToQPoint(p1), vecToQPoint(p2));
-		constexpr float notchsize = 40.0f;
-		for (int a = -30; a <= 30; ++a) {
-			const glm::vec3 notch = this->polygon[0] + static_cast<float>(a) * zvec;
-			const QPointF s_notchcenter = this->renderer->modelToScreenCoordinates(notch);
-			const QPointF notch_s1 = s_notchcenter + notchsize * 0.5f * vecToQPoint(lateral);
-			const QPointF notch_s2 = s_notchcenter - notchsize * 0.5f * vecToQPoint(lateral);
-			painter->drawLine(notch_s1, notch_s2);
+		{
+			const glm::vec2 p1 = extremity(this->polygon[0] + zvec);
+			const glm::vec2 p2 = extremity(this->polygon[0] - zvec);
+			const glm::vec2 lateral = glm::normalize(glm::mat2{{0, 1}, {-1, 0}} * (p2 - p1));
+			painter->setPen(QPen{Qt::white, 3});
+			painter->drawLine(vecToQPoint(p1), vecToQPoint(p2));
+			constexpr float notchsize = 40.0f;
+			for (int a = -30; a <= 30; ++a) {
+				const glm::vec3 notch = this->polygon[0] + static_cast<float>(a) * zvec;
+				const QPointF s_notchcenter = this->renderer->modelToScreenCoordinates(notch);
+				const QPointF notch_s1 = s_notchcenter + notchsize * 0.5f * vecToQPoint(lateral);
+				const QPointF notch_s2 = s_notchcenter - notchsize * 0.5f * vecToQPoint(lateral);
+				painter->drawLine(notch_s1, notch_s2);
+			}
+		}
+		if (this->polygon.size() >= 3) {
+			const opt<float> height = this->cylinderHeight();
+			if (height.has_value()) {
+				const glm::vec3 heightvec = height.value_or(0) * zvec;
+				const glm::vec3 p = this->polygon[1] + 0.5f * heightvec;
+				QFont font{};
+				font.setBold(true);
+				drawBorderedText(painter, this->renderer->modelToScreenCoordinates(p), font, QString::number(*height));
+			}
 		}
 	}
 }
@@ -343,6 +355,27 @@
 	return result;
 }
 
+opt<float> EditTools::cylinderHeight() const
+{
+	if (this->polygon.size() < 3) {
+		return {};
+	}
+	else {
+		const Plane plane{
+			.normal = glm::normalize(this->renderer->cameraVector(this->localPosition)),
+			.anchor = this->polygon[0],
+		};
+		const opt<glm::vec3> p = this->renderer->screenToModelCoordinates(this->localPosition, plane);
+		if (p.has_value()) {
+			const glm::vec3 heightvec = glm::normalize(glm::vec3{gridMatrix[2]});
+			return std::round(glm::dot(*p - polygon[0], heightvec));
+		}
+		else {
+			return {};
+		}
+	}
+}
+
 EditingMode EditTools::currentEditingMode() const
 {
 	return this->mode;
@@ -449,39 +482,24 @@
 const std::vector<ModelAction> EditTools::circleModeActions() const
 {
 	std::vector<ModelAction> result;
-	if (this->numpoints == 3) {
+	if (this->numpoints >= 2) {
 		const glm::vec3 x = polygon[1] - polygon[0];
-		const opt<float> cyliheight = [&]() -> opt<float> {
-			const Plane plane{
-				.normal = glm::normalize(this->renderer->cameraVector(this->localPosition)),
-				.anchor = this->polygon[0],
-			};
-			const opt<glm::vec3> p = this->renderer->screenToModelCoordinates(this->localPosition, plane);
-			if (p.has_value()) {
-				const glm::vec3 heightvec = glm::normalize(glm::vec3{gridMatrix[2]});
-				return std::round(glm::dot(*p - polygon[0], heightvec));
-			}
-			else {
-				return {};
-			}
-		}();
-		if (cyliheight.has_value()) {
-			glm::mat4 transform{
-				glm::vec4{x, 0},
-				*cyliheight * this->gridMatrix[2],
-				glm::vec4{glm::cross(glm::vec3{-this->gridMatrix[2]}, x), 0},
-				glm::vec4{this->polygon[0], 1},
-			};
-			Colored<CircularPrimitive> circ{
-				CircularPrimitive{
-					.type = this->circleToolOptions.type,
-					.fraction = this->circleToolOptions.fraction,
-					.transformation = transform,
-				},
-				MAIN_COLOR
-			};
-			result.push_back(AppendToModel{.newElement = circ});
-		}
+		const opt<float> cyliheight = this->cylinderHeight().value_or(1);
+		glm::mat4 transform{
+			glm::vec4{x, 0},
+			*cyliheight * this->gridMatrix[2],
+			glm::vec4{glm::cross(glm::vec3{-this->gridMatrix[2]}, x), 0},
+			glm::vec4{this->polygon[0], 1},
+		};
+		Colored<CircularPrimitive> circ{
+			CircularPrimitive{
+				.type = this->circleToolOptions.type,
+				.fraction = this->circleToolOptions.fraction,
+				.transformation = transform,
+			},
+			MAIN_COLOR
+		};
+		result.push_back(AppendToModel{.newElement = circ});
 	}
 	return result;
 }
--- a/src/layers/edittools.h	Fri Jul 01 23:51:16 2022 +0300
+++ b/src/layers/edittools.h	Sat Jul 02 19:03:57 2022 +0300
@@ -79,4 +79,5 @@
 	void removeLastPoint();
 	[[nodiscard]] bool isCloseToExistingPoints() const;
 	[[nodiscard]] std::size_t calcNumPoints() const;
+	[[nodiscard]] opt<float> cylinderHeight() const;
 };

mercurial