From faad0419cf554d03d29445a883119aa67c466c4d Mon Sep 17 00:00:00 2001 From: Roman Telezhynskyi Date: Fri, 11 Nov 2016 17:55:02 +0200 Subject: [PATCH] Using class VSAPoint for drawing base seam allowance. --HG-- branch : feature --- src/libs/vlayout/vabstractpiece.cpp | 96 +++-------------------------- src/libs/vlayout/vabstractpiece.h | 93 ++++++++++++++++++++++++++-- src/libs/vpatterndb/vpiece.cpp | 32 ++++++---- src/libs/vpatterndb/vpiece.h | 4 ++ 4 files changed, 121 insertions(+), 104 deletions(-) diff --git a/src/libs/vlayout/vabstractpiece.cpp b/src/libs/vlayout/vabstractpiece.cpp index 897d97976..c0d052653 100644 --- a/src/libs/vlayout/vabstractpiece.cpp +++ b/src/libs/vlayout/vabstractpiece.cpp @@ -32,9 +32,6 @@ #include #include #include -#include - -#include "../vgeometry/vgobject.h" //--------------------------------------------------------------------------------------------------------------------- VAbstractPiece::VAbstractPiece() @@ -110,17 +107,15 @@ void VAbstractPiece::SetSAWidth(qreal value) } //--------------------------------------------------------------------------------------------------------------------- -QVector VAbstractPiece::Equidistant(const QVector &points, qreal width) +QVector VAbstractPiece::Equidistant(const QVector &points, qreal width) { - QVector ekvPoints; - - if (width <= 0) + if (width < 0) { - qDebug()<<"Width <= 0."; + qDebug()<<"Width < 0."; return QVector(); } - QVector p = CorrectEquidistantPoints(points); + QVector p = CorrectEquidistantPoints(points); if ( p.size() < 3 ) { qDebug()<<"Not enough points for building the equidistant."; @@ -132,6 +127,7 @@ QVector VAbstractPiece::Equidistant(const QVector &points, qre p.append(p.at(0));// Should be always closed } + QVector ekvPoints; for (qint32 i = 0; i < p.size(); ++i ) { if ( i == 0) @@ -326,90 +322,16 @@ QVector VAbstractPiece::CheckLoops(const QVector &points) return ekvPoints; } -//--------------------------------------------------------------------------------------------------------------------- -/** - * @brief CorrectEquidistantPoints clear equivalent points and remove point on line from equdistant. - * @param points list of points equdistant. - * @return corrected list. - */ -QVector VAbstractPiece::CorrectEquidistantPoints(const QVector &points, bool removeFirstAndLast) -{ - if (points.size()<4)//Better don't check if only three points. We can destroy equidistant. - { - qDebug()<<"Only three points."; - return points; - } - - //Clear equivalent points - QVector correctPoints = RemoveDublicates(points, removeFirstAndLast); - - if (correctPoints.size()<3) - { - return correctPoints; - } - - //Remove point on line - for (qint32 i = 1; i VAbstractPiece::RemoveDublicates(const QVector &points, bool removeFirstAndLast) -{ - QVector p = points; - - if (removeFirstAndLast) - { - if (not p.isEmpty() && p.size() > 1) - { - // Path can't be closed - if (p.first() == p.last()) - { - #if QT_VERSION < QT_VERSION_CHECK(5, 1, 0) - p.remove(p.size() - 1); - #else - p.removeLast(); - #endif - } - } - } - - for (int i = 0; i < p.size()-1; ++i) - { - if (p.at(i) == p.at(i+1)) - { - if (not removeFirstAndLast && (i == p.size()-1)) - { - continue; - } - - p.erase(p.begin() + i + 1); - --i; - continue; - } - } - - return p; -} - //--------------------------------------------------------------------------------------------------------------------- /** * @brief EkvPoint return vector of points of equidistant two lines. Last point of two lines must be equal. * @param width width of equidistant. * @return vector of points. */ -QVector VAbstractPiece::EkvPoint(const QPointF &p1Line1, const QPointF &p2Line1, - const QPointF &p1Line2, const QPointF &p2Line2, qreal width) +QVector VAbstractPiece::EkvPoint(const VSAPoint &p1Line1, const VSAPoint &p2Line1, + const VSAPoint &p1Line2, const VSAPoint &p2Line2, qreal width) { - if (width <= 0) + if (width < 0) { return QVector(); } @@ -496,7 +418,7 @@ QVector VAbstractPiece::EkvPoint(const QPointF &p1Line1, const QPointF } //--------------------------------------------------------------------------------------------------------------------- -QLineF VAbstractPiece::ParallelLine(const QPointF &p1, const QPointF &p2, qreal width) +QLineF VAbstractPiece::ParallelLine(const VSAPoint &p1, const VSAPoint &p2, qreal width) { const QLineF paralel = QLineF(SingleParallelPoint(p1, p2, 90, width), SingleParallelPoint(p2, p1, -90, width)); diff --git a/src/libs/vlayout/vabstractpiece.h b/src/libs/vlayout/vabstractpiece.h index fc473c8d5..579f46965 100644 --- a/src/libs/vlayout/vabstractpiece.h +++ b/src/libs/vlayout/vabstractpiece.h @@ -32,8 +32,10 @@ #include #include #include +#include #include "../vmisc/diagnostic.h" +#include "../vgeometry/vgobject.h" template class QVector; @@ -125,22 +127,101 @@ public: qreal GetSAWidth() const; void SetSAWidth(qreal value); - static QVector Equidistant(const QVector &points, qreal width); + static QVector Equidistant(const QVector &points, qreal width); static qreal SumTrapezoids(const QVector &points); static QVector CheckLoops(const QVector &points); - static QVector CorrectEquidistantPoints(const QVector &points, bool removeFirstAndLast = true); + + template + static QVector CorrectEquidistantPoints(const QVector &points, bool removeFirstAndLast = true); protected: - static QVector RemoveDublicates(const QVector &points, bool removeFirstAndLast = true); + template + static QVector RemoveDublicates(const QVector &points, bool removeFirstAndLast = true); private: QSharedDataPointer d; - static QVector EkvPoint(const QPointF &p1Line1, const QPointF &p2Line1, - const QPointF &p1Line2, const QPointF &p2Line2, qreal width); - static QLineF ParallelLine(const QPointF &p1, const QPointF &p2, qreal width); + static QVector EkvPoint(const VSAPoint &p1Line1, const VSAPoint &p2Line1, + const VSAPoint &p1Line2, const VSAPoint &p2Line2, qreal width); + static QLineF ParallelLine(const VSAPoint &p1, const VSAPoint &p2, qreal width); static QPointF SingleParallelPoint(const QPointF &p1, const QPointF &p2, qreal angle, qreal width); static int BisectorAngle(const QPointF &p1, const QPointF &p2, const QPointF &p3); }; +//--------------------------------------------------------------------------------------------------------------------- +/** + * @brief CorrectEquidistantPoints clear equivalent points and remove point on line from equdistant. + * @param points list of points equdistant. + * @return corrected list. + */ +template +QVector VAbstractPiece::CorrectEquidistantPoints(const QVector &points, bool removeFirstAndLast) +{ + if (points.size()<4)//Better don't check if only three points. We can destroy equidistant. + { + qDebug()<<"Only three points."; + return points; + } + + //Clear equivalent points + QVector correctPoints = RemoveDublicates(points, removeFirstAndLast); + + if (correctPoints.size()<3) + { + return correctPoints; + } + + //Remove point on line + for (qint32 i = 1; i +QVector VAbstractPiece::RemoveDublicates(const QVector &points, bool removeFirstAndLast) +{ + QVector p = points; + + if (removeFirstAndLast) + { + if (not p.isEmpty() && p.size() > 1) + { + // Path can't be closed + if (p.first() == p.last()) + { + #if QT_VERSION < QT_VERSION_CHECK(5, 1, 0) + p.remove(p.size() - 1); + #else + p.removeLast(); + #endif + } + } + } + + for (int i = 0; i < p.size()-1; ++i) + { + if (p.at(i) == p.at(i+1)) + { + if (not removeFirstAndLast && (i == p.size()-1)) + { + continue; + } + + p.erase(p.begin() + i + 1); + --i; + continue; + } + } + + return p; +} + #endif // VABSTRACTPIECE_H diff --git a/src/libs/vpatterndb/vpiece.cpp b/src/libs/vpatterndb/vpiece.cpp index da0183809..57c52d277 100644 --- a/src/libs/vpatterndb/vpiece.cpp +++ b/src/libs/vpatterndb/vpiece.cpp @@ -205,12 +205,13 @@ QVector VPiece::SeamAllowancePoints(const VContainer *data) const { SCASSERT(data != nullptr); - QVector pointsEkv; + if (not IsSeamAllowance()) { - return pointsEkv; + return QVector(); } + QVector pointsEkv; for (int i = 0; i< CountNodes(); ++i) { switch (at(i).GetTypeTool()) @@ -218,7 +219,7 @@ QVector VPiece::SeamAllowancePoints(const VContainer *data) const case (Tool::NodePoint): { const QSharedPointer point = data->GeometricObject(at(i).GetId()); - pointsEkv.append(*point); + pointsEkv.append(VSAPoint(point->toQPointF())); } break; case (Tool::NodeArc): @@ -226,11 +227,7 @@ QVector VPiece::SeamAllowancePoints(const VContainer *data) const case (Tool::NodeSplinePath): { const QSharedPointer curve = data->GeometricObject(at(i).GetId()); - - const QPointF begin = StartSegment(data, i, at(i).GetReverse()); - const QPointF end = EndSegment(data, i, at(i).GetReverse()); - - pointsEkv << curve->GetSegmentPoints(begin, end, at(i).GetReverse()); + CurveSeamAllowanceSegment(pointsEkv, data, curve, i, at(i).GetReverse()); } break; default: @@ -239,9 +236,7 @@ QVector VPiece::SeamAllowancePoints(const VContainer *data) const } } - pointsEkv = CheckLoops(CorrectEquidistantPoints(pointsEkv));//A path can contains loops - pointsEkv = Equidistant(pointsEkv, ToPixel(GetSAWidth(), *data->GetPatternUnit())); - return pointsEkv; + return Equidistant(pointsEkv, ToPixel(GetSAWidth(), *data->GetPatternUnit())); } //--------------------------------------------------------------------------------------------------------------------- @@ -375,6 +370,21 @@ int VPiece::indexOfNode(const quint32 &id) const return indexOfNode(d->m_nodes, id); } +//--------------------------------------------------------------------------------------------------------------------- +void VPiece::CurveSeamAllowanceSegment(QVector &pointsEkv, const VContainer *data, + const QSharedPointer &curve, int i, bool reverse) const +{ + const QPointF begin = StartSegment(data, i, reverse); + const QPointF end = EndSegment(data, i, reverse); + + const QVector points = curve->GetSegmentPoints(begin, end, reverse); + + for(int i = 0; i < points.size(); ++i) + { + pointsEkv.append(VSAPoint(points.at(i))); + } +} + //--------------------------------------------------------------------------------------------------------------------- QPointF VPiece::StartSegment(const VContainer *data, const int &i, bool reverse) const { diff --git a/src/libs/vpatterndb/vpiece.h b/src/libs/vpatterndb/vpiece.h index ad5834580..11723c989 100644 --- a/src/libs/vpatterndb/vpiece.h +++ b/src/libs/vpatterndb/vpiece.h @@ -40,6 +40,8 @@ class VPieceNode; class QPointF; class VContainer; template class QVector; +template class QSharedPointer; +class VAbstractCurve; class VPiece : public VAbstractPiece { @@ -83,6 +85,8 @@ public: private: QSharedDataPointer d; + void CurveSeamAllowanceSegment(QVector &pointsEkv, const VContainer *data, + const QSharedPointer &curve, int i, bool reverse) const; QPointF StartSegment(const VContainer *data, const int &i, bool reverse) const; QPointF EndSegment(const VContainer *data, const int &i, bool reverse) const;