From 07858f7fcbceb2f62f9d5894070d1865d313f64a Mon Sep 17 00:00:00 2001 From: Roman Telezhynskyi Date: Sat, 1 Aug 2020 11:55:56 +0300 Subject: [PATCH] Refactoring. --- src/libs/vgeometry/vgeometrydef.h | 13 ++--- src/libs/vlayout/vabstractpiece.cpp | 64 +----------------------- src/libs/vlayout/vabstractpiece.h | 75 +++++++++++++++++++++++++++++ 3 files changed, 83 insertions(+), 69 deletions(-) diff --git a/src/libs/vgeometry/vgeometrydef.h b/src/libs/vgeometry/vgeometrydef.h index e94279749..bd533ae30 100644 --- a/src/libs/vgeometry/vgeometrydef.h +++ b/src/libs/vgeometry/vgeometrydef.h @@ -99,16 +99,17 @@ Q_DECLARE_METATYPE(VLayoutPassmark) constexpr qreal accuracyPointOnLine = (0.117/*mm*/ / 25.4) * PrintDPI; -Q_REQUIRED_RESULT static inline bool VFuzzyComparePoints(const QPointF &p1, const QPointF &p2); -static inline bool VFuzzyComparePoints(const QPointF &p1, const QPointF &p2) +Q_REQUIRED_RESULT static inline bool VFuzzyComparePoints(const QPointF &p1, const QPointF &p2, + qreal accuracy = accuracyPointOnLine); +static inline bool VFuzzyComparePoints(const QPointF &p1, const QPointF &p2, qreal accuracy) { - return QLineF(p1, p2).length() <= accuracyPointOnLine; + return QLineF(p1, p2).length() <= accuracy; } -Q_REQUIRED_RESULT static inline bool VFuzzyOnAxis(qreal v1, qreal v2); -static inline bool VFuzzyOnAxis(qreal v1, qreal v2) +Q_REQUIRED_RESULT static inline bool VFuzzyOnAxis(qreal v1, qreal v2, qreal accuracy = accuracyPointOnLine); +static inline bool VFuzzyOnAxis(qreal v1, qreal v2, qreal accuracy) { - return qAbs(v1 - v2) <= accuracyPointOnLine; + return qAbs(v1 - v2) <= accuracy; } #endif // VGEOMETRYDEF_H diff --git a/src/libs/vlayout/vabstractpiece.cpp b/src/libs/vlayout/vabstractpiece.cpp index e051eefef..4ba2d8b78 100644 --- a/src/libs/vlayout/vabstractpiece.cpp +++ b/src/libs/vlayout/vabstractpiece.cpp @@ -1548,69 +1548,7 @@ bool VAbstractPiece::IsAllowanceValid(const QVector &base, const QVecto return false; // Wrong direction } - // Edges must not intersect - for (auto i = 0; i < base.count(); ++i) - { - int nextI = -1; - if (i < base.count()-1) - { - nextI = i + 1; - } - else - { - nextI = 0; - } - - QLineF baseSegment(base.at(i), base.at(nextI)); - if (baseSegment.isNull()) - { - continue; - } - - for (auto j = 0; j < allowance.count(); ++j) - { - int nextJ = -1; - if (j < allowance.count()-1) - { - nextJ = j + 1; - } - else - { - nextJ = 0; - } - - QLineF allowanceSegment(allowance.at(j), allowance.at(nextJ)); - if (allowanceSegment.isNull()) - { - continue; - } - - QPointF crosPoint; - const auto type = Intersects(baseSegment, allowanceSegment, &crosPoint); - - if (type == QLineF::BoundedIntersection - && not VFuzzyComparePoints(baseSegment.p1(), crosPoint) - && not VFuzzyComparePoints(baseSegment.p2(), crosPoint) - && not VGObject::IsPointOnLineviaPDP(allowanceSegment.p1(), baseSegment.p1(), baseSegment.p2()) - && not VGObject::IsPointOnLineviaPDP(allowanceSegment.p2(), baseSegment.p1(), baseSegment.p2())) - { - return false; - } - } - } - - // Just instersection edges is not enough. The base must be inside of the allowance. - QPolygonF allowancePolygon(allowance); - - for (auto &point : base) - { - if (not allowancePolygon.containsPoint(point, Qt::WindingFill)) - { - return false; - } - } - - return true; + return IsInsidePolygon(base, allowance); } //--------------------------------------------------------------------------------------------------------------------- diff --git a/src/libs/vlayout/vabstractpiece.h b/src/libs/vlayout/vabstractpiece.h index adff3056b..ef3386c8c 100644 --- a/src/libs/vlayout/vabstractpiece.h +++ b/src/libs/vlayout/vabstractpiece.h @@ -37,6 +37,7 @@ #include "../vmisc/diagnostic.h" #include "../vmisc/def.h" +#include "../vmisc/compatibility.h" #include "../vgeometry/vgobject.h" #include "vsapoint.h" #include "testpath.h" @@ -100,6 +101,9 @@ public: bool *needRollback = nullptr); static QLineF ParallelLine(const VSAPoint &p1, const VSAPoint &p2, qreal width); static bool IsAllowanceValid(const QVector &base, const QVector &allowance); + template + static bool IsInsidePolygon(const QVector &path, const QVector &polygon, + qreal accuracy = accuracyPointOnLine); template static QVector CorrectEquidistantPoints(const QVector &points, bool removeFirstAndLast = true); @@ -254,4 +258,75 @@ QVector VAbstractPiece::RemoveDublicates(const QVector &points, bool remov return p; } +//--------------------------------------------------------------------------------------------------------------------- +template +bool VAbstractPiece::IsInsidePolygon(const QVector &path, const QVector &polygon, qreal accuracy) +{ + // Edges must not intersect + for (auto i = 0; i < path.count(); ++i) + { + int nextI = -1; + if (i < path.count()-1) + { + nextI = i + 1; + } + else + { + nextI = 0; + } + + QLineF baseSegment(path.at(i), path.at(nextI)); + if (baseSegment.isNull()) + { + continue; + } + + for (auto j = 0; j < polygon.count(); ++j) + { + int nextJ = -1; + if (j < polygon.count()-1) + { + nextJ = j + 1; + } + else + { + nextJ = 0; + } + + QLineF allowanceSegment(polygon.at(j), polygon.at(nextJ)); + if (allowanceSegment.isNull()) + { + continue; + } + + QPointF crosPoint; + const auto type = Intersects(baseSegment, allowanceSegment, &crosPoint); + + if (type == QLineF::BoundedIntersection + && not VFuzzyComparePoints(baseSegment.p1(), crosPoint, accuracy) + && not VFuzzyComparePoints(baseSegment.p2(), crosPoint, accuracy) + && not VGObject::IsPointOnLineviaPDP(allowanceSegment.p1(), baseSegment.p1(), baseSegment.p2(), + accuracy) + && not VGObject::IsPointOnLineviaPDP(allowanceSegment.p2(), baseSegment.p1(), baseSegment.p2(), + accuracy)) + { + return false; + } + } + } + + // Just instersection edges is not enough. The base must be inside of the allowance. + QPolygonF allowancePolygon(polygon); + + for (auto &point : path) + { + if (not allowancePolygon.containsPoint(point, Qt::WindingFill)) + { + return false; + } + } + + return true; +} + #endif // VABSTRACTPIECE_H