From 166f2fbcdb3dbbdaedd2d723d32e16c2e530d356 Mon Sep 17 00:00:00 2001 From: dismine Date: Mon, 12 Jan 2015 22:35:32 +0200 Subject: [PATCH] Save each frame to png file for debuging creation layout. --HG-- branch : feature --- src/app/mainwindow.cpp | 2 +- src/libs/vlayout/vlayoutdef.h | 8 ++ src/libs/vlayout/vlayoutdetail.cpp | 41 +++++++++ src/libs/vlayout/vlayoutdetail.h | 1 + src/libs/vlayout/vlayoutgenerator.cpp | 11 +++ src/libs/vlayout/vlayoutpaper.cpp | 122 ++++++++++++++++++++++++++ src/libs/vlayout/vlayoutpaper.h | 8 ++ src/libs/vlayout/vlayoutpaper_p.h | 9 +- 8 files changed, 198 insertions(+), 4 deletions(-) diff --git a/src/app/mainwindow.cpp b/src/app/mainwindow.cpp index 3bf1a2cf4..01097d8de 100644 --- a/src/app/mainwindow.cpp +++ b/src/app/mainwindow.cpp @@ -1923,7 +1923,7 @@ void MainWindow::ActionLayout(bool checked) det.SetSeamAllowencePoints(idetail.value().SeamAllowancePoints(pattern)); det.setSeamAllowance(idetail.value().getSeamAllowance()); det.setName(idetail.value().getName()); - det.SetLayoutAllowencePoints(); + det.setWidth(qApp->toPixel(idetail.value().getWidth())); listDetails.append(det); } diff --git a/src/libs/vlayout/vlayoutdef.h b/src/libs/vlayout/vlayoutdef.h index ecadc2977..30f7593ab 100644 --- a/src/libs/vlayout/vlayoutdef.h +++ b/src/libs/vlayout/vlayoutdef.h @@ -40,4 +40,12 @@ enum class LayoutErrors : char EmptyPaperError }; +#define LAYOUT_DEBUG // Enable debug mode + +#ifdef LAYOUT_DEBUG +# define SHOW_VERTICES // Show contour vertices +# define SHOW_DIRECTION // Show contour direction +# define ARRANGED_DETAILS // Show already arranged details +#endif//LAYOUT_DEBUG + #endif // VLAYOUTDEF_H diff --git a/src/libs/vlayout/vlayoutdetail.cpp b/src/libs/vlayout/vlayoutdetail.cpp index 7edee057b..4d427d6ba 100644 --- a/src/libs/vlayout/vlayoutdetail.cpp +++ b/src/libs/vlayout/vlayoutdetail.cpp @@ -29,6 +29,7 @@ #include "vlayoutdetail.h" #include "vlayoutdetail_p.h" +#include #include //--------------------------------------------------------------------------------------------------------------------- @@ -316,3 +317,43 @@ QVector VLayoutDetail::Map(const QVector &points) const } return p; } + +//--------------------------------------------------------------------------------------------------------------------- +QPainterPath VLayoutDetail::ContourPath() const +{ + QPainterPath path; + + // contour + path.moveTo(d->contour[0]); + for (qint32 i = 1; i < d->contour.count(); ++i) + { + path.lineTo(d->contour.at(i)); + } + path.lineTo(d->contour.at(0)); + + // seam allowence + if (getSeamAllowance() == true) + { + QPainterPath ekv; + QVector p; + if (getClosed() == true) + { + p = Equidistant(d->seamAllowence, EquidistantType::CloseEquidistant, getWidth()); + } + else + { + p = Equidistant(d->seamAllowence, EquidistantType::OpenEquidistant, getWidth()); + } + + ekv.moveTo(p.at(0)); + for (qint32 i = 1; i < p.count(); ++i) + { + ekv.lineTo(p.at(i)); + } + + path.addPath(ekv); + path.setFillRule(Qt::WindingFill); + } + + return path; +} diff --git a/src/libs/vlayout/vlayoutdetail.h b/src/libs/vlayout/vlayoutdetail.h index 9021516f5..922f3abfa 100644 --- a/src/libs/vlayout/vlayoutdetail.h +++ b/src/libs/vlayout/vlayoutdetail.h @@ -71,6 +71,7 @@ public: bool isNull() const; qint64 Square() const; + QPainterPath ContourPath() const; private: QSharedDataPointer d; diff --git a/src/libs/vlayout/vlayoutgenerator.cpp b/src/libs/vlayout/vlayoutgenerator.cpp index 396fedabe..d494a09e0 100644 --- a/src/libs/vlayout/vlayoutgenerator.cpp +++ b/src/libs/vlayout/vlayoutgenerator.cpp @@ -31,6 +31,8 @@ #include "vlayoutdetail.h" #include +#include +#include //--------------------------------------------------------------------------------------------------------------------- VLayoutGenerator::VLayoutGenerator(QObject *parent) @@ -74,6 +76,14 @@ void VLayoutGenerator::Generate() stopGeneration = false; papers.clear(); state = LayoutErrors::NoError; + +#ifdef LAYOUT_DEBUG + const QString path = QDir::homePath()+QStringLiteral("/LayoutDebug"); + QDir debugDir(path); + debugDir.removeRecursively(); + debugDir.mkpath(path); +#endif + emit Start(); if (bank->Prepare()) @@ -89,6 +99,7 @@ void VLayoutGenerator::Generate() VLayoutPaper paper(paperHeight, paperWidth); paper.SetShift(shift); + paper.SetPaperIndex(papers.count()); if (bank->LeftArrange() > 0) { const int index = bank->GetTiket(); diff --git a/src/libs/vlayout/vlayoutpaper.cpp b/src/libs/vlayout/vlayoutpaper.cpp index 1a9efb9ab..6c3b475f0 100644 --- a/src/libs/vlayout/vlayoutpaper.cpp +++ b/src/libs/vlayout/vlayoutpaper.cpp @@ -32,6 +32,9 @@ #include #include #include +#include +#include +#include class BestResult { @@ -171,6 +174,12 @@ void VLayoutPaper::SetShift(unsigned int shift) d->shift = shift; } +//--------------------------------------------------------------------------------------------------------------------- +void VLayoutPaper::SetPaperIndex(quint32 index) +{ + d->paperIndex = index; +} + //--------------------------------------------------------------------------------------------------------------------- bool VLayoutPaper::ArrangeDetail(const VLayoutDetail &detail) { @@ -185,6 +194,8 @@ bool VLayoutPaper::ArrangeDetail(const VLayoutDetail &detail) return false;//Not enough edges } + d->frame = 0; + if (Count() == 0) { return AddToBlankSheet(detail); @@ -223,6 +234,7 @@ bool VLayoutPaper::AddToBlankSheet(const VLayoutDetail &detail) workDetail.GetMatrix()); } } + ++d->frame; for (int angle = 0; angle < 360; ++angle) { @@ -238,6 +250,7 @@ bool VLayoutPaper::AddToBlankSheet(const VLayoutDetail &detail) workDetail.GetMatrix()); } } + ++d->frame; } } } @@ -273,6 +286,7 @@ bool VLayoutPaper::AddToSheet(const VLayoutDetail &detail) continue; // Outside of sheet. } } + ++d->frame; } } @@ -288,6 +302,10 @@ bool VLayoutPaper::CheckCombineEdges(VLayoutDetail &detail, int j, int &dEdge) c CombineEdges(detail, globalEdge, dEdge); +#ifdef LAYOUT_DEBUG + DrawDebug(detail); +#endif + switch (Crossing(detail, j, dEdge)) { case CrossingType::EdgeError: @@ -363,6 +381,10 @@ bool VLayoutPaper::CheckRotationEdges(VLayoutDetail &detail, int j, int dEdge, i RotateEdges(detail, globalEdge, dEdge, angle); +#ifdef LAYOUT_DEBUG + DrawDebug(detail); +#endif + switch (Crossing(detail, j, dEdge)) { case CrossingType::EdgeError: @@ -735,3 +757,103 @@ bool VLayoutPaper::SaveResult(const BestResult &bestResult, const VLayoutDetail return bestResult.ValideResult(); // Do we have the best result? } + +//--------------------------------------------------------------------------------------------------------------------- +void VLayoutPaper::DrawDebug(const VLayoutDetail &detail) const +{ + QImage frameImage ( d->paperWidth, d->paperHeight, QImage::Format_RGB32 ); + frameImage.fill(Qt::white); + QPainter paint; + paint.setPen(QPen(Qt::black, 1, Qt::SolidLine, Qt::FlatCap, Qt::MiterJoin)); + paint.begin(&frameImage); + + if (d->globalContour.isEmpty()) + { + paint.drawPath(DrawContour(CutEdge(QLineF(0, 0, d->paperWidth, 0)))); + } + else + { + paint.drawPath(DrawContour(d->globalContour)); + } + + paint.setPen(QPen(Qt::darkGreen, 1, Qt::SolidLine, Qt::FlatCap, Qt::MiterJoin)); + paint.drawPath(DrawContour(detail.GetLayoutAllowencePoints())); + +#ifdef ARRANGED_DETAILS + paint.setPen(QPen(Qt::blue, 1, Qt::SolidLine, Qt::FlatCap, Qt::MiterJoin)); + paint.drawPath(DrawDetails()); +#endif + + paint.end(); + const QString path = QDir::homePath()+QStringLiteral("/LayoutDebug/")+QString("%1_%2.png").arg(d->paperIndex) + .arg(d->frame); + frameImage.save (path); +} + +//--------------------------------------------------------------------------------------------------------------------- +QPainterPath VLayoutPaper::ShowDirection(const QLineF &edge) const +{ + QLineF arrow = edge; + arrow.setLength(edge.length()/2.0); + + //Reverse line because we want start arrow from this point + arrow = QLineF(arrow.p2(), arrow.p1()); + const qreal angle = arrow.angle();//we each time change line angle, better save original angle value + arrow.setLength(14);//arrow length in pixels + + QPainterPath path; + arrow.setAngle(angle-35); + path.moveTo(arrow.p1()); + path.lineTo(arrow.p2()); + + arrow.setAngle(angle+35); + path.moveTo(arrow.p1()); + path.lineTo(arrow.p2()); + return path; +} + +//--------------------------------------------------------------------------------------------------------------------- +QPainterPath VLayoutPaper::DrawContour(const QVector &points) const +{ + QPainterPath path; + path.setFillRule(Qt::WindingFill); + if (points.count() >= 2) + { + for (qint32 i = 0; i < points.count()-1; ++i) + { + path.moveTo(points.at(i)); + path.lineTo(points.at(i+1)); + } + path.lineTo(points.at(0)); + +#ifdef SHOW_DIRECTION + for (qint32 i = 0; i < points.count()-1; ++i) + { + path.addPath(ShowDirection(QLineF(points.at(i), points.at(i+1)))); + } +#endif + +#ifdef SHOW_VERTICES + for (qint32 i = 0; i < points.count(); ++i) + { + path.addRect(points.at(i).x()-3, points.at(i).y()-3, 6, 6); + } +#endif + } + return path; +} + +//--------------------------------------------------------------------------------------------------------------------- +QPainterPath VLayoutPaper::DrawDetails() const +{ + QPainterPath path; + path.setFillRule(Qt::WindingFill); + if (Count() > 0) + { + for (int i = 0; i < d->details.size(); ++i) + { + path.addPath(d->details.at(i).ContourPath()); + } + } + return path; +} diff --git a/src/libs/vlayout/vlayoutpaper.h b/src/libs/vlayout/vlayoutpaper.h index d47fc9543..2317839eb 100644 --- a/src/libs/vlayout/vlayoutpaper.h +++ b/src/libs/vlayout/vlayoutpaper.h @@ -37,6 +37,7 @@ class QPointF; class QLineF; class QPolygonF; class BestResult; +class QPainterPath; class VLayoutPaper { @@ -56,6 +57,8 @@ public: unsigned int GetShift() const; void SetShift(unsigned int shift); + void SetPaperIndex(quint32 index); + bool ArrangeDetail(const VLayoutDetail &detail); int Count() const; private: @@ -98,6 +101,11 @@ private: QVector CutEdge(const QLineF &edge) const; bool SaveResult(const BestResult &bestResult, const VLayoutDetail &detail); + + void DrawDebug(const VLayoutDetail &detail) const; + QPainterPath ShowDirection(const QLineF &edge) const; + QPainterPath DrawContour(const QVector &points) const; + QPainterPath DrawDetails() const; }; #endif // VLAYOUTPAPER_H diff --git a/src/libs/vlayout/vlayoutpaper_p.h b/src/libs/vlayout/vlayoutpaper_p.h index 523e7aec3..62e37923c 100644 --- a/src/libs/vlayout/vlayoutpaper_p.h +++ b/src/libs/vlayout/vlayoutpaper_p.h @@ -44,17 +44,18 @@ class VLayoutPaperData : public QSharedData { public: VLayoutPaperData() - :details(QVector()), globalContour(QVector()), paperHeight(0), paperWidth(0), shift(0) + :details(QVector()), globalContour(QVector()), paperHeight(0), paperWidth(0), shift(0), + paperIndex(0), frame(0) {} VLayoutPaperData(int height, int width) :details(QVector()), globalContour(QVector()), paperHeight(height), paperWidth(width), - shift(0) + shift(0), paperIndex(0), frame(0) {} VLayoutPaperData(const VLayoutPaperData &paper) :QSharedData(paper), details(paper.details), globalContour(paper.globalContour), paperHeight(paper.paperHeight), - paperWidth(paper.paperWidth), shift(paper.shift) + paperWidth(paper.paperWidth), shift(paper.shift), paperIndex(paper.paperIndex), frame(paper.frame) {} ~VLayoutPaperData() {} @@ -72,6 +73,8 @@ public: int paperWidth; unsigned int shift; + quint32 paperIndex; + quint32 frame; }; #ifdef Q_CC_GNU