Circular primitive type is now an enum class

Wed, 12 Apr 2023 01:53:42 +0300

author
Teemu Piippo <teemu.s.piippo@gmail.com>
date
Wed, 12 Apr 2023 01:53:42 +0300
changeset 379
8d88adffb779
parent 378
01537fbe096e
child 380
16f6717a218b

Circular primitive type is now an enum class

src/circularprimitive.h file | annotate | diff | comparison | revisions
src/layers/edittools.cpp file | annotate | diff | comparison | revisions
src/layers/edittools.h file | annotate | diff | comparison | revisions
src/model.cpp file | annotate | diff | comparison | revisions
src/model.h file | annotate | diff | comparison | revisions
src/ui/circletooloptionswidget.cpp file | annotate | diff | comparison | revisions
src/ui/circletooloptionswidget.h file | annotate | diff | comparison | revisions
--- a/src/circularprimitive.h	Tue Apr 11 22:51:47 2023 +0300
+++ b/src/circularprimitive.h	Wed Apr 12 01:53:42 2023 +0300
@@ -5,7 +5,7 @@
 #include "src/ldrawalgorithm.h"
 
 template<typename Fn>
-void rasterize(const CircularPrimitive& circ, Fn&& fn)
+void circular_element_to_polygons(const circular_primitive& circ, Fn&& fn)
 {
 	std::vector<ModelElement> result;
 	const auto xform = [&circ](const glm::vec2& p, float y){
@@ -14,19 +14,19 @@
 	const glm::vec3 primitiveOrigin = xform({0, 0}, 0);
 	const bool invertedMatrix = (glm::determinant(circ.transformation) < 0) != circ.inverted;
 	switch(circ.type) {
-	case CircularPrimitive::Circle:
+	case circular_primitive_type_e::circle:
 		ldraw::circle(circ.fraction.segments, circ.fraction.divisions, [&]
 		(const glm::vec2&, const glm::vec2& p1, const glm::vec2& p2){
 			fn(LineSegment{xform(p1, 0), xform(p2, 0)}, EDGE_COLOR);
 		});
 		break;
-	case CircularPrimitive::Disc:
+	case circular_primitive_type_e::disc:
 		ldraw::circle(circ.fraction.segments, circ.fraction.divisions, [&]
 		(const glm::vec2&, const glm::vec2& p1, const glm::vec2& p2){
 			fn(Triangle{primitiveOrigin, xform(p1, 0), xform(p2, 0)}, MAIN_COLOR);
 		});
 		break;
-	case CircularPrimitive::Cylinder:
+	case circular_primitive_type_e::cylinder:
 		ldraw::circle(circ.fraction.segments, circ.fraction.divisions, [&]
 		(const glm::vec2&, const glm::vec2& p1, const glm::vec2& p2){
 			Quadrilateral quad{xform(p1, 1), xform(p2, 1), xform(p2, 0), xform(p1, 0)};
@@ -36,36 +36,36 @@
 			fn(quad, MAIN_COLOR);
 		});
 		break;
-	case CircularPrimitive::CylinderOpen:
-		rasterize(CircularPrimitive{
-			.type = CircularPrimitive::Cylinder,
+	case circular_primitive_type_e::cylinder_open:
+		circular_element_to_polygons(circular_primitive{
+			.type = circular_primitive_type_e::cylinder,
 			.fraction = circ.fraction,
 			.transformation = circ.transformation,
 		}, fn);
-		rasterize(CircularPrimitive{
-			.type = CircularPrimitive::Circle,
+		circular_element_to_polygons(circular_primitive{
+			.type = circular_primitive_type_e::circle,
 			.fraction = circ.fraction,
 			.transformation = circ.transformation,
 		}, fn);
-		rasterize(CircularPrimitive{
-			.type = CircularPrimitive::Circle,
+		circular_element_to_polygons(circular_primitive{
+			.type = circular_primitive_type_e::circle,
 			.fraction = circ.fraction,
 			.transformation = glm::translate(circ.transformation, {0, 1, 0}),
 		}, fn);
 		break;
-	case CircularPrimitive::CylinderClosed:
-		rasterize(CircularPrimitive{
-			.type = CircularPrimitive::CylinderOpen,
+	case circular_primitive_type_e::cylinder_closed:
+		circular_element_to_polygons(circular_primitive{
+			.type = circular_primitive_type_e::cylinder_open,
 			.fraction = circ.fraction,
 			.transformation = circ.transformation,
 		}, fn);
-		rasterize(CircularPrimitive{
-			.type = CircularPrimitive::Disc,
+		circular_element_to_polygons(circular_primitive{
+			.type = circular_primitive_type_e::disc,
 			.fraction = circ.fraction,
 			.transformation = circ.transformation,
 		}, fn);
 		break;
-	case CircularPrimitive::DiscNegative:
+	case circular_primitive_type_e::disc_negative:
 		{
 			unsigned int i = 0;
 			ldraw::circle(circ.fraction.segments, circ.fraction.divisions, [&]
@@ -82,7 +82,7 @@
 			});
 		}
 		break;
-	case CircularPrimitive::Chord:
+	case circular_primitive_type_e::chord:
 		for (unsigned int i = 1; i < circ.fraction.segments; ++i) {
 			const glm::vec2& p1 = ldraw::rimpoint(circ.fraction.divisions, i);
 			const glm::vec2& p2 = ldraw::rimpoint(circ.fraction.divisions, i + 1);
--- a/src/layers/edittools.cpp	Tue Apr 11 22:51:47 2023 +0300
+++ b/src/layers/edittools.cpp	Wed Apr 12 01:53:42 2023 +0300
@@ -117,9 +117,9 @@
 		else if (const Colored<Quadrilateral>* quad = std::get_if<Colored<Quadrilateral>>(&newElement)) {
 			result.push_back({quad->element.p1, quad->element.p2, quad->element.p3, quad->element.p4});
 		}
-		else if (const Colored<CircularPrimitive>* circ = std::get_if<Colored<CircularPrimitive>>(&newElement)) {
+		else if (const Colored<circular_primitive>* circ = std::get_if<Colored<circular_primitive>>(&newElement)) {
 			// rasterize the circle down to polygons, and append them to the result.
-			rasterize(circ->element, [&](const PlainPolygonElement& poly, const ColorIndex color){
+			circular_element_to_polygons(circ->element, [&](const PlainPolygonElement& poly, const ColorIndex color){
 				AppendToModel append{elementFromPolygonAndColor(poly, color)};
 				const auto& subpoints = polygonsToBeInserted(append);
 				std::copy(subpoints.begin(), subpoints.end(), std::back_inserter(result));
@@ -335,8 +335,8 @@
 			glm::vec4{glm::cross(glm::vec3{-this->gridMatrix[2]}, x), 0},
 			glm::vec4{this->inputPolygon[0], 1},
 		};
-		Colored<CircularPrimitive> circ{
-			CircularPrimitive{
+		Colored<circular_primitive> circ{
+			circular_primitive{
 				.type = this->circleToolOptions.type,
 				.fraction = this->circleToolOptions.fraction,
 				.transformation = transform,
--- a/src/layers/edittools.h	Tue Apr 11 22:51:47 2023 +0300
+++ b/src/layers/edittools.h	Wed Apr 12 01:53:42 2023 +0300
@@ -48,7 +48,7 @@
 	opt<glm::vec3> worldPosition;
 	CircleToolOptions circleToolOptions = {
 		.fraction = {16, 16},
-		.type = CircularPrimitive::Circle,
+		.type = circular_primitive_type_e::circle,
 	};
 public:
 	explicit EditTools(QObject *parent = nullptr);
--- a/src/model.cpp	Tue Apr 11 22:51:47 2023 +0300
+++ b/src/model.cpp	Wed Apr 12 01:53:42 2023 +0300
@@ -36,12 +36,12 @@
 static_assert(gcd(16, 4) == 4);
 static_assert(gcd(272, 192) == 16);
 
-static constexpr const char* circularPrimitiveTypeString(const CircularPrimitive& circ)
+static constexpr const char* circularPrimitiveTypeString(const circular_primitive& circ)
 {
-	return circularPrimitiveStems[circ.type];
+	return circularPrimitiveStems[static_cast<unsigned int>(circ.type)];
 }
 
-static QString circularPrimitiveFilePath(const CircularPrimitive& circ)
+static QString circularPrimitiveFilePath(const circular_primitive& circ)
 {
 	QString result;
 	if (circ.fraction.divisions != 16) {
@@ -78,7 +78,7 @@
 		[](const Colored<ConditionalEdge>&) {
 			return ":/icons/linetype-conditionaledge.png";
 		},
-		[](const Colored<CircularPrimitive>&) {
+		[](const Colored<circular_primitive>&) {
 			return ":/icons/linetype-circularprimitive.png";
 		},
 		[](const Comment&) {
@@ -148,7 +148,7 @@
 				.arg(vertexToString(cedge.element.c1))
 				.arg(vertexToString(cedge.element.c2));
 		},
-		[](const Colored<CircularPrimitive>& circ) {
+		[](const Colored<circular_primitive>& circ) {
 			return QStringLiteral("1 %1 %2 %3")
 				.arg(circ.color.index)
 				.arg(transformToString(circ.element.transformation))
--- a/src/model.h	Tue Apr 11 22:51:47 2023 +0300
+++ b/src/model.h	Wed Apr 12 01:53:42 2023 +0300
@@ -62,25 +62,27 @@
 	return p.segments * q.divisions < q.segments / p.divisions;
 }
 
-struct CircularPrimitive
+enum class circular_primitive_type_e
 {
-	enum Type
-	{
-		Circle,
-		Disc,
-		Cylinder,
-		CylinderOpen,
-		CylinderClosed,
-		DiscNegative,
-		Chord,
-	} type;
-	static constexpr int NUM_TYPES = Chord + 1;
+	circle,
+	disc,
+	cylinder,
+	cylinder_open,
+	cylinder_closed,
+	disc_negative,
+	chord,
+};
+
+struct circular_primitive
+{
+	static constexpr int NUM_TYPES = static_cast<int>(circular_primitive_type_e::chord) + 1;
+	circular_primitive_type_e type;
 	CircularFraction fraction;
 	glm::mat4 transformation;
 	bool inverted = false;
 };
 
-constexpr char circularPrimitiveStems[CircularPrimitive::NUM_TYPES][5] = {
+constexpr char circularPrimitiveStems[circular_primitive::NUM_TYPES][5] = {
 	"edge",
 	"disc",
 	"cyli",
@@ -90,12 +92,12 @@
 	"chrd",
 };
 
-Q_DECLARE_METATYPE(CircularPrimitive::Type)
+Q_DECLARE_METATYPE(circular_primitive_type_e)
 
 struct CircleToolOptions
 {
 	CircularFraction fraction;
-	CircularPrimitive::Type type;
+	circular_primitive_type_e type;
 };
 
 using ModelElement = std::variant<
@@ -104,7 +106,7 @@
 	Colored<Triangle>,
 	Colored<Quadrilateral>,
 	Colored<ConditionalEdge>,
-	Colored<CircularPrimitive>,
+	Colored<circular_primitive>,
 	Comment,
 	Empty,
 	ParseError>;
--- a/src/ui/circletooloptionswidget.cpp	Tue Apr 11 22:51:47 2023 +0300
+++ b/src/ui/circletooloptionswidget.cpp	Wed Apr 12 01:53:42 2023 +0300
@@ -1,21 +1,21 @@
 #include "src/ui/circletooloptionswidget.h"
 
-static QString circularPrimitiveTypeName(CircularPrimitive::Type type)
+static QString circularPrimitiveTypeName(circular_primitive_type_e type)
 {
 	switch (type) {
-	case CircularPrimitive::Circle:
+	case circular_primitive_type_e::circle:
 		return CircleToolOptionsWidget::tr("Circle");
-	case CircularPrimitive::Disc:
+	case circular_primitive_type_e::disc:
 		return CircleToolOptionsWidget::tr("Disc");
-	case CircularPrimitive::Cylinder:
+	case circular_primitive_type_e::cylinder:
 		return CircleToolOptionsWidget::tr("Cylinder");
-	case CircularPrimitive::CylinderOpen:
+	case circular_primitive_type_e::cylinder_open:
 		return CircleToolOptionsWidget::tr("Cylinder open");
-	case CircularPrimitive::CylinderClosed:
+	case circular_primitive_type_e::cylinder_closed:
 		return CircleToolOptionsWidget::tr("Cylinder closed");
-	case CircularPrimitive::DiscNegative:
+	case circular_primitive_type_e::disc_negative:
 		return CircleToolOptionsWidget::tr("Disc negative");
-	case CircularPrimitive::Chord:
+	case circular_primitive_type_e::chord:
 		return CircleToolOptionsWidget::tr("Chord");
 	}
 	return "";
@@ -25,9 +25,9 @@
 	QWidget{parent}
 {
 	this->ui.setupUi(this);
-	for (int i = 0; i < CircularPrimitive::NUM_TYPES; ++i) {
-		const auto type = static_cast<CircularPrimitive::Type>(i);
-		this->ui.type->addItem(circularPrimitiveTypeName(type), type);
+	for (int i = 0; i < circular_primitive::NUM_TYPES; ++i) {
+		const auto type = static_cast<circular_primitive_type_e>(i);
+		this->ui.type->addItem(circularPrimitiveTypeName(type), QVariant::fromValue(type));
 	}
 	connect(ui.segments, qOverload<int>(&QSpinBox::valueChanged), this, &CircleToolOptionsWidget::handleInputChange);
 	connect(ui.divisions, &QComboBox::currentTextChanged, this, &CircleToolOptionsWidget::handleInputChange);
@@ -55,9 +55,9 @@
 	}
 }
 
-CircularPrimitive::Type CircleToolOptionsWidget::type() const
+circular_primitive_type_e CircleToolOptionsWidget::type() const
 {
-	return this->ui.type->currentData().value<CircularPrimitive::Type>();
+	return this->ui.type->currentData().value<circular_primitive_type_e>();
 }
 
 void CircleToolOptionsWidget::handleInputChange()
--- a/src/ui/circletooloptionswidget.h	Tue Apr 11 22:51:47 2023 +0300
+++ b/src/ui/circletooloptionswidget.h	Wed Apr 12 01:53:42 2023 +0300
@@ -12,7 +12,7 @@
 	virtual ~CircleToolOptionsWidget();
 	unsigned int segments() const;
 	unsigned int divisions() const;
-	CircularPrimitive::Type type() const;
+	circular_primitive_type_e type() const;
 Q_SIGNALS:
 	void optionsChanged(const CircleToolOptions& options);
 private:

mercurial