Wed, 25 Mar 2020 16:07:20 +0200
added PolygonObject and refactored away a lot of boilerplate
--- a/src/geometry.h Thu Mar 19 21:06:06 2020 +0200 +++ b/src/geometry.h Wed Mar 25 16:07:20 2020 +0200 @@ -26,7 +26,7 @@ template<int N> struct Polygon { - glm::vec3 points[N]; + std::array<glm::vec3, N> points; }; template<int N>
--- a/src/linetypes/conditionaledge.cpp Thu Mar 19 21:06:06 2020 +0200 +++ b/src/linetypes/conditionaledge.cpp Wed Mar 25 16:07:20 2020 +0200 @@ -1,65 +1,10 @@ #include "conditionaledge.h" -ldraw::ConditionalEdge::ConditionalEdge( - const glm::vec3& point_1, - const glm::vec3& point_2, - const glm::vec3& controlPoint_1, - const glm::vec3& controlPoint_2, - const Color color_index) : - Edge{point_1, point_2, color_index}, - controlPoint_1{controlPoint_1}, - controlPoint_2{controlPoint_2} -{ -} - -ldraw::ConditionalEdge::ConditionalEdge(const std::array<glm::vec3, 4>& vertices, const Color color) : - Edge{vertices[0], vertices[1], color}, - controlPoint_1{vertices[2]}, - controlPoint_2{vertices[3]} -{ -} - -QVariant ldraw::ConditionalEdge::getProperty(Property property) const -{ - switch (property) - { - case Property::ControlPoint0: - return QVariant::fromValue(controlPoint_1); - case Property::ControlPoint1: - return QVariant::fromValue(controlPoint_2); - default: - return Edge::getProperty(property); - } -} - -void ldraw::ConditionalEdge::setProperty(SetPropertyResult* result, const PropertyKeyValue& pair) -{ - LDRAW_OBJECT_HANDLE_SET_PROPERTY(ControlPoint0, {this->controlPoint_1 = value;}) - LDRAW_OBJECT_HANDLE_SET_PROPERTY(ControlPoint1, {this->controlPoint_2 = value;}) - BaseClass::setProperty(result, pair); -} - QString ldraw::ConditionalEdge::textRepresentation() const { - return Edge::textRepresentation() + utility::format("%1 %2", - utility::vertexToStringParens(controlPoint_1), - utility::vertexToStringParens(controlPoint_2)); -} - -int ldraw::ConditionalEdge::numPoints() const -{ - return 4; + return utility::format("%1 %2 %3 %4", + utility::vertexToStringParens(this->points[0]), + utility::vertexToStringParens(this->points[1]), + utility::vertexToStringParens(this->points[2]), + utility::vertexToStringParens(this->points[3])); } - -const glm::vec3& ldraw::ConditionalEdge::getPoint(int index) const -{ - switch(index) - { - case 3: - return this->controlPoint_1; - case 4: - return this->controlPoint_2; - default: - return ldraw::Edge::getPoint(index); - } -}
--- a/src/linetypes/conditionaledge.h Thu Mar 19 21:06:06 2020 +0200 +++ b/src/linetypes/conditionaledge.h Wed Mar 25 16:07:20 2020 +0200 @@ -6,23 +6,9 @@ class ConditionalEdge; } -class ldraw::ConditionalEdge : public Edge +class ldraw::ConditionalEdge : public PolygonObject<4> { public: - ConditionalEdge() = default; - ConditionalEdge( - const glm::vec3& point_1, - const glm::vec3& point_2, - const glm::vec3& controlPoint_1, - const glm::vec3& controlPoint_2, - const Color colorIndex = ldraw::edgeColor); - ConditionalEdge(const std::array<glm::vec3, 4>& vertices, const Color color); - QVariant getProperty(Property property) const override; + using PolygonObject<4>::PolygonObject; QString textRepresentation() const override; - int numPoints() const override; - const glm::vec3& getPoint(int index) const override; - glm::vec3 controlPoint_1 = {}; - glm::vec3 controlPoint_2 = {}; -protected: - void setProperty(SetPropertyResult* result, const PropertyKeyValue& pair) override; };
--- a/src/linetypes/edge.cpp Thu Mar 19 21:06:06 2020 +0200 +++ b/src/linetypes/edge.cpp Wed Mar 25 16:07:20 2020 +0200 @@ -1,43 +1,11 @@ #include "edge.h" -ldraw::Edge::Edge( - const glm::vec3& point_1, - const glm::vec3& point_2, - const Color color_index) : - ColoredObject{color_index}, - point_1{point_1}, - point_2{point_2} {} - -ldraw::Edge::Edge(const std::array<glm::vec3, 2>& vertices, const Color color) : - ColoredObject{color}, - point_1{vertices[0]}, - point_2{vertices[1]} -{ -} - -QVariant ldraw::Edge::getProperty(Property property) const -{ - switch (property) - { - case Property::Point0: - return QVariant::fromValue(point_1); - case Property::Point1: - return QVariant::fromValue(point_2); - default: - return BaseClass::getProperty(property); - } -} - -void ldraw::Edge::setProperty(SetPropertyResult* result, const PropertyKeyValue& pair) -{ - LDRAW_OBJECT_HANDLE_SET_PROPERTY(Point0, {this->point_1 = value;}) - LDRAW_OBJECT_HANDLE_SET_PROPERTY(Point1, {this->point_2 = value;}) - BaseClass::setProperty(result, pair); -} - QString ldraw::Edge::textRepresentation() const { - return utility::format("%1 %2", utility::vertexToStringParens(point_1), utility::vertexToStringParens(point_2)); + return utility::format( + "%1 %2", + utility::vertexToStringParens(this->points[0]), + utility::vertexToStringParens(this->points[1])); } void ldraw::Edge::getPolygons( @@ -45,23 +13,5 @@ GetPolygonsContext* context) const { Q_UNUSED(context) - polygons.push_back(gl::edgeLine(this->point_1, this->point_2, this->colorIndex, this->id)); -} - -int ldraw::Edge::numPoints() const -{ - return 2; + polygons.push_back(gl::edgeLine(this->points[0], this->points[1], this->colorIndex, this->id)); } - -const glm::vec3& ldraw::Edge::getPoint(int index) const -{ - switch (index) - { - case 0: - return this->point_1; - case 1: - return this->point_2; - default: - return ldraw::ColoredObject::getPoint(index); - } -}
--- a/src/linetypes/edge.h Thu Mar 19 21:06:06 2020 +0200 +++ b/src/linetypes/edge.h Wed Mar 25 16:07:20 2020 +0200 @@ -6,21 +6,10 @@ class Edge; } -class ldraw::Edge : public ColoredObject +class ldraw::Edge : public PolygonObject<2> { public: - using BaseClass = ColoredObject; - Edge() = default; - Edge(const glm::vec3& point_1, const glm::vec3& point_2, - const Color colorIndex = ldraw::edgeColor); - Edge(const std::array<glm::vec3, 2>& vertices, const Color color); - QVariant getProperty(Property property) const override; + using PolygonObject::PolygonObject; QString textRepresentation() const override; void getPolygons(std::vector<gl::Polygon>& polygons, GetPolygonsContext* context) const override; - int numPoints() const override; - const glm::vec3& getPoint(int index) const override; - glm::vec3 point_1 = {}; - glm::vec3 point_2 = {}; -protected: - void setProperty(SetPropertyResult* result, const PropertyKeyValue& pair) override; };
--- a/src/linetypes/object.h Thu Mar 19 21:06:06 2020 +0200 +++ b/src/linetypes/object.h Wed Mar 25 16:07:20 2020 +0200 @@ -14,6 +14,8 @@ class ColoredObject; class Empty; class UnhandledProperty; + template<int N> + class PolygonObject; } class DocumentManager; @@ -34,8 +36,6 @@ Point1, // Second vertex in a polygon or edge line Point2, // Third vertex in a polygon Point3, // Fourth vertex in a quadrilateral - ControlPoint0, // First control point in a conditional edge line - ControlPoint1, // Second control point in a conditional edge line Transformation, // 4x4 transformation matrix of a subfile reference ReferenceName, // Subfile reference name IsInverted, // Whether or not the object has been inverted with BFC INVERTNEXT @@ -77,8 +77,6 @@ LDFORGE_DEFINE_PROPERTY_TYPE(Point1, glm::vec3) LDFORGE_DEFINE_PROPERTY_TYPE(Point2, glm::vec3) LDFORGE_DEFINE_PROPERTY_TYPE(Point3, glm::vec3) -LDFORGE_DEFINE_PROPERTY_TYPE(ControlPoint0, glm::vec3) -LDFORGE_DEFINE_PROPERTY_TYPE(ControlPoint1, glm::vec3) LDFORGE_DEFINE_PROPERTY_TYPE(Transformation, glm::mat4) LDFORGE_DEFINE_PROPERTY_TYPE(ReferenceName, QString) LDFORGE_DEFINE_PROPERTY_TYPE(IsInverted, bool) @@ -155,6 +153,64 @@ void setProperty(SetPropertyResult* result, const PropertyKeyValue& pair) override; }; +template<int N> +class ldraw::PolygonObject : public ColoredObject +{ +public: + PolygonObject(const std::array<glm::vec3, N>& points, const Color color) : + ColoredObject{color}, + points{points} {} + int numPoints() const override + { + return N; + } + const glm::vec3& getPoint(int index) const override + { + Q_ASSERT(index >= 0 and index < N); + return this->points[index]; + } + QVariant getProperty(const Property id) const override + { + switch (id) + { + case Property::Point0: + return QVariant::fromValue(points[0]); + case Property::Point1: + return QVariant::fromValue(points[1]); + case Property::Point2: + if (N >= 3) + { + return QVariant::fromValue(points[2]); + } + break; + case Property::Point3: + if (N >= 4) + { + return QVariant::fromValue(points[3]); + } + break; + default: + break; + } + return ColoredObject::getProperty(id); + } + void setProperty(SetPropertyResult* result, const PropertyKeyValue& pair) + { + LDRAW_OBJECT_HANDLE_SET_PROPERTY(Point0, {points[0] = value;}) + LDRAW_OBJECT_HANDLE_SET_PROPERTY(Point1, {points[1] = value;}) + if constexpr (N >= 3) + { + LDRAW_OBJECT_HANDLE_SET_PROPERTY(Point2, {points[2] = value;}) + } + if constexpr (N >= 4) + { + LDRAW_OBJECT_HANDLE_SET_PROPERTY(Point3, {points[2] = value;}) + } + ColoredObject::setProperty(result, pair); + } + std::array<std::enable_if_t<(N > 0 and N <= 4), glm::vec3>, N> points; +}; + /** * @brief Represents an empty line. */
--- a/src/linetypes/quadrilateral.cpp Thu Mar 19 21:06:06 2020 +0200 +++ b/src/linetypes/quadrilateral.cpp Wed Mar 25 16:07:20 2020 +0200 @@ -1,61 +1,12 @@ #include "quadrilateral.h" -ldraw::Quadrilateral::Quadrilateral -( - const glm::vec3& point_1, - const glm::vec3& point_2, - const glm::vec3& point_3, - const glm::vec3& point_4, - Color color_index -) : - ColoredObject{color_index}, - points{point_1, point_2, point_3, point_4} -{ -} - -ldraw::Quadrilateral::Quadrilateral -( - const std::array<glm::vec3, 4>& vertices, - const Color color -) : - ColoredObject{color}, - points{vertices[0], vertices[1], vertices[2], vertices[3]} -{ -} - -QVariant ldraw::Quadrilateral::getProperty(const Property id) const -{ - switch (id) - { - case Property::Point0: - return QVariant::fromValue(points[0]); - case Property::Point1: - return QVariant::fromValue(points[1]); - case Property::Point2: - return QVariant::fromValue(points[2]); - case Property::Point3: - return QVariant::fromValue(points[3]); - default: - return ColoredObject::getProperty(id); - } -} - -void ldraw::Quadrilateral::setProperty(SetPropertyResult* result, const PropertyKeyValue& pair) -{ - LDRAW_OBJECT_HANDLE_SET_PROPERTY(Point0, {points[0] = value;}) - LDRAW_OBJECT_HANDLE_SET_PROPERTY(Point1, {points[1] = value;}) - LDRAW_OBJECT_HANDLE_SET_PROPERTY(Point2, {points[2] = value;}) - LDRAW_OBJECT_HANDLE_SET_PROPERTY(Point3, {points[3] = value;}) - ColoredObject::setProperty(result, pair); -} - QString ldraw::Quadrilateral::textRepresentation() const { return utility::format("%1 %2 %3 %4", - utility::vertexToStringParens(points[0]), - utility::vertexToStringParens(points[1]), - utility::vertexToStringParens(points[2]), - utility::vertexToStringParens(points[3])); + utility::vertexToStringParens(this->points[0]), + utility::vertexToStringParens(this->points[1]), + utility::vertexToStringParens(this->points[2]), + utility::vertexToStringParens(this->points[3])); } void ldraw::Quadrilateral::getPolygons( @@ -69,7 +20,7 @@ this->points[2], this->points[3], this->colorIndex, - this->id)); + this->id)); } void ldraw::Quadrilateral::invert() @@ -78,20 +29,3 @@ // -> 2 1 0 3 std::swap(this->points[0], this->points[2]); } - -int ldraw::Quadrilateral::numPoints() const -{ - return 4; -} - -const glm::vec3& ldraw::Quadrilateral::getPoint(int index) const -{ - if (index >= 0 and index < 4) - { - return points[index]; - } - else - { - return ldraw::ColoredObject::getPoint(index); - } -}
--- a/src/linetypes/quadrilateral.h Thu Mar 19 21:06:06 2020 +0200 +++ b/src/linetypes/quadrilateral.h Wed Mar 25 16:07:20 2020 +0200 @@ -6,24 +6,11 @@ class Quadrilateral; } -class ldraw::Quadrilateral : public ColoredObject +class ldraw::Quadrilateral : public PolygonObject<4> { public: - Quadrilateral() = default; - Quadrilateral( - const glm::vec3 &point_1, - const glm::vec3 &point_2, - const glm::vec3 &point_3, - const glm::vec3 &point_4, - Color colorIndex = ldraw::mainColor); - Quadrilateral(const std::array<glm::vec3, 4>& vertices, const Color color); - QVariant getProperty(Property id) const override; + using PolygonObject<4>::PolygonObject; QString textRepresentation() const override; void getPolygons(std::vector<gl::Polygon>& polygons, GetPolygonsContext* context) const override; void invert() override; - int numPoints() const override; - const glm::vec3& getPoint(int index) const override; - glm::vec3 points[4] = {{}}; -protected: - void setProperty(SetPropertyResult* result, const PropertyKeyValue& pair) override; };
--- a/src/linetypes/triangle.cpp Thu Mar 19 21:06:06 2020 +0200 +++ b/src/linetypes/triangle.cpp Wed Mar 25 16:07:20 2020 +0200 @@ -1,50 +1,5 @@ #include "triangle.h" -ldraw::Triangle::Triangle( - const glm::vec3& point_1, - const glm::vec3& point_2, - const glm::vec3& point_3, - Color color_index) : - ColoredObject{color_index}, - points{point_1, point_2, point_3} -{ -} - -ldraw::Triangle::Triangle(const std::array<glm::vec3, 3>& vertices, const Color color) : - ColoredObject{color}, - points{vertices[0], vertices[1], vertices[2]} -{ -} - -ldraw::Triangle::Triangle(const glm::vec3 (&vertices)[3], const Color color) : - ColoredObject{color}, - points{vertices[0], vertices[1], vertices[2]} -{ -} - -QVariant ldraw::Triangle::getProperty(const Property id) const -{ - switch (id) - { - case Property::Point0: - return QVariant::fromValue(points[0]); - case Property::Point1: - return QVariant::fromValue(points[1]); - case Property::Point2: - return QVariant::fromValue(points[2]); - default: - return ColoredObject::getProperty(id); - } -} - -void ldraw::Triangle::setProperty(SetPropertyResult* result, const PropertyKeyValue& pair) -{ - LDRAW_OBJECT_HANDLE_SET_PROPERTY(Point0, {points[0] = value;}) - LDRAW_OBJECT_HANDLE_SET_PROPERTY(Point1, {points[1] = value;}) - LDRAW_OBJECT_HANDLE_SET_PROPERTY(Point2, {points[2] = value;}) - ColoredObject::setProperty(result, pair); -} - QString ldraw::Triangle::textRepresentation() const { return utility::format("%1 %2 %3", @@ -63,7 +18,7 @@ this->points[1], this->points[2], this->colorIndex, - this->id)); + this->id)); } void ldraw::Triangle::invert() @@ -72,20 +27,3 @@ // -> 1 0 2 std::swap(this->points[0], this->points[1]); } - -int ldraw::Triangle::numPoints() const -{ - return 3; -} - -const glm::vec3& ldraw::Triangle::getPoint(int index) const -{ - if (index >= 0 and index < 3) - { - return this->points[index]; - } - else - { - return ldraw::ColoredObject::getPoint(index); - } -}
--- a/src/linetypes/triangle.h Thu Mar 19 21:06:06 2020 +0200 +++ b/src/linetypes/triangle.h Wed Mar 25 16:07:20 2020 +0200 @@ -6,25 +6,12 @@ class Triangle; } -class ldraw::Triangle : public ColoredObject +class ldraw::Triangle : public PolygonObject<3> { public: - Triangle() = default; - Triangle( - const glm::vec3 &point_1, - const glm::vec3 &point_2, - const glm::vec3 &point_3, - Color colorIndex = ldraw::mainColor); - Triangle(const std::array<glm::vec3, 3>& vertices, const Color color); - Triangle(const glm::vec3 (&vertices)[3], const Color color); - QVariant getProperty(Property id) const override; + using PolygonObject<3>::PolygonObject; QString textRepresentation() const override; void getPolygons(std::vector<gl::Polygon>& polygons, GetPolygonsContext* context) const override; void invert() override; - int numPoints() const override; - const glm::vec3& getPoint(int index) const override; - glm::vec3 points[3] = {{}}; -protected: - void setProperty(SetPropertyResult* result, const PropertyKeyValue& pair) override; };
--- a/src/modeleditcontext.cpp Thu Mar 19 21:06:06 2020 +0200 +++ b/src/modeleditcontext.cpp Wed Mar 25 16:07:20 2020 +0200 @@ -70,7 +70,7 @@ return this->storedModel; } -static std::array<geom::Triangle, 2> splitTriangles(ldraw::Diagonal diagonal, const glm::vec3(&points)[4]) +static std::array<geom::Triangle, 2> splitTriangles(ldraw::Diagonal diagonal, const std::array<glm::vec3, 4>& points) { std::array<geom::Triangle, 2> result; switch (diagonal)