# HG changeset patch # User Teemu Piippo # Date 1681253622 -10800 # Node ID 8d88adffb779d744cbe5fa27fac9b2f129e04514 # Parent 01537fbe096efbcf4a45cdb65132bdfd597b4ff9 Circular primitive type is now an enum class diff -r 01537fbe096e -r 8d88adffb779 src/circularprimitive.h --- 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 -void rasterize(const CircularPrimitive& circ, Fn&& fn) +void circular_element_to_polygons(const circular_primitive& circ, Fn&& fn) { std::vector 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); diff -r 01537fbe096e -r 8d88adffb779 src/layers/edittools.cpp --- 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* quad = std::get_if>(&newElement)) { result.push_back({quad->element.p1, quad->element.p2, quad->element.p3, quad->element.p4}); } - else if (const Colored* circ = std::get_if>(&newElement)) { + else if (const Colored* circ = std::get_if>(&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 circ{ - CircularPrimitive{ + Colored circ{ + circular_primitive{ .type = this->circleToolOptions.type, .fraction = this->circleToolOptions.fraction, .transformation = transform, diff -r 01537fbe096e -r 8d88adffb779 src/layers/edittools.h --- 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 worldPosition; CircleToolOptions circleToolOptions = { .fraction = {16, 16}, - .type = CircularPrimitive::Circle, + .type = circular_primitive_type_e::circle, }; public: explicit EditTools(QObject *parent = nullptr); diff -r 01537fbe096e -r 8d88adffb779 src/model.cpp --- 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(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&) { return ":/icons/linetype-conditionaledge.png"; }, - [](const Colored&) { + [](const Colored&) { return ":/icons/linetype-circularprimitive.png"; }, [](const Comment&) { @@ -148,7 +148,7 @@ .arg(vertexToString(cedge.element.c1)) .arg(vertexToString(cedge.element.c2)); }, - [](const Colored& circ) { + [](const Colored& circ) { return QStringLiteral("1 %1 %2 %3") .arg(circ.color.index) .arg(transformToString(circ.element.transformation)) diff -r 01537fbe096e -r 8d88adffb779 src/model.h --- 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(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, Colored, Colored, - Colored, + Colored, Comment, Empty, ParseError>; diff -r 01537fbe096e -r 8d88adffb779 src/ui/circletooloptionswidget.cpp --- 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(i); - this->ui.type->addItem(circularPrimitiveTypeName(type), type); + for (int i = 0; i < circular_primitive::NUM_TYPES; ++i) { + const auto type = static_cast(i); + this->ui.type->addItem(circularPrimitiveTypeName(type), QVariant::fromValue(type)); } connect(ui.segments, qOverload(&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(); + return this->ui.type->currentData().value(); } void CircleToolOptionsWidget::handleInputChange() diff -r 01537fbe096e -r 8d88adffb779 src/ui/circletooloptionswidget.h --- 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: