2015-01-10 14:47:46 +01:00
|
|
|
/************************************************************************
|
|
|
|
**
|
|
|
|
** @file vlayoutpaper.cpp
|
|
|
|
** @author Roman Telezhynskyi <dismine(at)gmail.com>
|
|
|
|
** @date 7 1, 2015
|
|
|
|
**
|
|
|
|
** @brief
|
|
|
|
** @copyright
|
|
|
|
** This source code is part of the Valentine project, a pattern making
|
|
|
|
** program, whose allow create and modeling patterns of clothing.
|
|
|
|
** Copyright (C) 2015 Valentina project
|
|
|
|
** <https://bitbucket.org/dismine/valentina> All Rights Reserved.
|
|
|
|
**
|
|
|
|
** Valentina is free software: you can redistribute it and/or modify
|
|
|
|
** it under the terms of the GNU General Public License as published by
|
|
|
|
** the Free Software Foundation, either version 3 of the License, or
|
|
|
|
** (at your option) any later version.
|
|
|
|
**
|
|
|
|
** Valentina is distributed in the hope that it will be useful,
|
|
|
|
** but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
** GNU General Public License for more details.
|
|
|
|
**
|
|
|
|
** You should have received a copy of the GNU General Public License
|
|
|
|
** along with Valentina. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
**
|
|
|
|
*************************************************************************/
|
|
|
|
|
|
|
|
#include "vlayoutpaper.h"
|
|
|
|
#include "vlayoutpaper_p.h"
|
2015-01-21 19:56:59 +01:00
|
|
|
#include "vbestsquare.h"
|
2015-01-10 14:47:46 +01:00
|
|
|
|
|
|
|
#include <climits>
|
|
|
|
#include <QPointF>
|
2015-01-12 16:23:25 +01:00
|
|
|
#include <QtMath>
|
2015-01-12 21:35:32 +01:00
|
|
|
#include <QImage>
|
|
|
|
#include <QDir>
|
|
|
|
#include <QPainter>
|
2015-01-13 11:38:51 +01:00
|
|
|
#include <QGraphicsItem>
|
2015-01-16 13:54:37 +01:00
|
|
|
#include <QCoreApplication>
|
2015-01-10 14:47:46 +01:00
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
VLayoutPaper::VLayoutPaper()
|
|
|
|
:d(new VLayoutPaperData)
|
|
|
|
{}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
VLayoutPaper::VLayoutPaper(int height, int width)
|
|
|
|
:d(new VLayoutPaperData(height, width))
|
|
|
|
{}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
VLayoutPaper::VLayoutPaper(const VLayoutPaper &paper)
|
|
|
|
:d (paper.d)
|
|
|
|
{}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
VLayoutPaper &VLayoutPaper::operator=(const VLayoutPaper &paper)
|
|
|
|
{
|
|
|
|
if ( &paper == this )
|
|
|
|
{
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
d = paper.d;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
VLayoutPaper::~VLayoutPaper()
|
|
|
|
{}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
int VLayoutPaper::GetHeight() const
|
|
|
|
{
|
2015-01-21 19:56:59 +01:00
|
|
|
return d->globalContour.GetHeight();
|
2015-01-10 14:47:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VLayoutPaper::SetHeight(int height)
|
|
|
|
{
|
2015-01-21 19:56:59 +01:00
|
|
|
d->globalContour.SetHeight(height);
|
2015-01-10 14:47:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
int VLayoutPaper::GetWidth() const
|
|
|
|
{
|
2015-01-21 19:56:59 +01:00
|
|
|
return d->globalContour.GetWidth();
|
2015-01-10 14:47:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VLayoutPaper::SetWidth(int width)
|
|
|
|
{
|
2015-01-21 19:56:59 +01:00
|
|
|
d->globalContour.SetWidth(width);
|
2015-01-10 14:47:46 +01:00
|
|
|
}
|
|
|
|
|
2015-01-17 15:00:46 +01:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
qreal VLayoutPaper::GetLayoutWidth() const
|
|
|
|
{
|
|
|
|
return d->layoutWidth;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VLayoutPaper::SetLayoutWidth(qreal width)
|
|
|
|
{
|
|
|
|
if (width >= 0)
|
|
|
|
{
|
|
|
|
d->layoutWidth = width;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-12 16:23:25 +01:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
unsigned int VLayoutPaper::GetShift() const
|
|
|
|
{
|
2015-01-21 19:56:59 +01:00
|
|
|
return d->globalContour.GetShift();
|
2015-01-12 16:23:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VLayoutPaper::SetShift(unsigned int shift)
|
|
|
|
{
|
2015-01-21 19:56:59 +01:00
|
|
|
d->globalContour.SetShift(shift);
|
2015-01-12 16:23:25 +01:00
|
|
|
}
|
|
|
|
|
2015-01-12 21:35:32 +01:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VLayoutPaper::SetPaperIndex(quint32 index)
|
|
|
|
{
|
|
|
|
d->paperIndex = index;
|
|
|
|
}
|
|
|
|
|
2015-01-10 14:47:46 +01:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2015-01-16 15:45:56 +01:00
|
|
|
bool VLayoutPaper::ArrangeDetail(const VLayoutDetail &detail, bool &stop)
|
2015-01-10 14:47:46 +01:00
|
|
|
{
|
|
|
|
// First need set size of paper
|
2015-01-21 19:56:59 +01:00
|
|
|
if (d->globalContour.GetHeight() <= 0 || d->globalContour.GetWidth() <= 0)
|
2015-01-10 14:47:46 +01:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (detail.EdgesCount() < 3)
|
|
|
|
{
|
|
|
|
return false;//Not enough edges
|
|
|
|
}
|
|
|
|
|
2015-01-12 21:35:32 +01:00
|
|
|
d->frame = 0;
|
|
|
|
|
2015-01-21 15:33:06 +01:00
|
|
|
return AddToSheet(detail, stop);
|
2015-01-10 14:47:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
int VLayoutPaper::Count() const
|
|
|
|
{
|
|
|
|
return d->details.count();
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2015-01-16 15:45:56 +01:00
|
|
|
bool VLayoutPaper::AddToSheet(const VLayoutDetail &detail, bool &stop)
|
2015-01-10 14:47:46 +01:00
|
|
|
{
|
2015-01-21 19:56:59 +01:00
|
|
|
VBestSquare bestResult;
|
2015-01-10 14:47:46 +01:00
|
|
|
|
2015-01-21 19:56:59 +01:00
|
|
|
for (int j=1; j <= d->globalContour.EdgesCount(); ++j)
|
2015-01-10 14:47:46 +01:00
|
|
|
{
|
2015-01-20 21:58:30 +01:00
|
|
|
for (int i=1; i<= detail.EdgesCount(); i++)
|
2015-01-10 14:47:46 +01:00
|
|
|
{
|
2015-01-16 13:54:37 +01:00
|
|
|
QCoreApplication::processEvents();
|
|
|
|
|
2015-01-16 15:45:56 +01:00
|
|
|
if (stop)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-01-20 21:58:30 +01:00
|
|
|
// We should use copy of the detail.
|
|
|
|
VLayoutDetail workDetail = detail;
|
|
|
|
|
2015-01-10 14:47:46 +01:00
|
|
|
int dEdge = i;// For mirror detail edge will be different
|
2015-01-12 16:59:15 +01:00
|
|
|
if (CheckCombineEdges(workDetail, j, dEdge))
|
2015-01-10 14:47:46 +01:00
|
|
|
{
|
2015-01-20 13:50:24 +01:00
|
|
|
#ifdef LAYOUT_DEBUG
|
|
|
|
# ifdef SHOW_CANDIDATE_BEST
|
2015-01-21 19:56:59 +01:00
|
|
|
DrawDebug(d->globalContour, workDetail, d->frame+2, d->paperIndex, d->details.count(),
|
|
|
|
d->details);
|
2015-01-20 13:50:24 +01:00
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
|
2015-01-21 19:56:59 +01:00
|
|
|
SaveCandidate(bestResult, workDetail, j, dEdge, BestFrom::Combine);
|
2015-01-10 14:47:46 +01:00
|
|
|
}
|
2015-01-20 13:50:24 +01:00
|
|
|
d->frame = d->frame + 3;
|
2015-01-20 21:58:30 +01:00
|
|
|
|
|
|
|
for (int angle = 0; angle <= 360; angle = angle+20)
|
|
|
|
{
|
|
|
|
QCoreApplication::processEvents();
|
|
|
|
|
|
|
|
if (stop)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We should use copy of the detail.
|
|
|
|
VLayoutDetail workDetail = detail;
|
|
|
|
|
|
|
|
if (CheckRotationEdges(workDetail, j, i, angle))
|
|
|
|
{
|
|
|
|
#ifdef LAYOUT_DEBUG
|
|
|
|
# ifdef SHOW_CANDIDATE_BEST
|
|
|
|
++d->frame;
|
2015-01-21 19:56:59 +01:00
|
|
|
DrawDebug(d->globalContour, workDetail, d->frame, d->paperIndex, d->details.count(),
|
|
|
|
d->details);
|
2015-01-20 21:58:30 +01:00
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
|
2015-01-21 19:56:59 +01:00
|
|
|
SaveCandidate(bestResult, workDetail, j, i, BestFrom::Rotation);
|
2015-01-20 21:58:30 +01:00
|
|
|
}
|
|
|
|
++d->frame;
|
|
|
|
}
|
2015-01-10 14:47:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-12 16:23:25 +01:00
|
|
|
return SaveResult(bestResult, detail);
|
2015-01-10 14:47:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2015-01-12 16:59:15 +01:00
|
|
|
bool VLayoutPaper::CheckCombineEdges(VLayoutDetail &detail, int j, int &dEdge) const
|
2015-01-10 14:47:46 +01:00
|
|
|
{
|
2015-01-21 19:56:59 +01:00
|
|
|
const QLineF globalEdge = d->globalContour.GlobalEdge(j);
|
2015-01-10 14:47:46 +01:00
|
|
|
bool flagMirror = false;
|
|
|
|
bool flagSquare = false;
|
|
|
|
|
|
|
|
CombineEdges(detail, globalEdge, dEdge);
|
|
|
|
|
2015-01-12 21:35:32 +01:00
|
|
|
#ifdef LAYOUT_DEBUG
|
2015-01-18 19:56:01 +01:00
|
|
|
# ifdef SHOW_COMBINE
|
2015-01-21 19:56:59 +01:00
|
|
|
DrawDebug(d->globalContour, detail, d->frame, d->paperIndex, d->details.count(), d->details);
|
2015-01-18 19:56:01 +01:00
|
|
|
# endif
|
2015-01-12 21:35:32 +01:00
|
|
|
#endif
|
|
|
|
|
2015-01-19 11:36:27 +01:00
|
|
|
CrossingType type = CrossingType::Intersection;
|
|
|
|
if (SheetContains(detail.BoundingRect()))
|
|
|
|
{
|
|
|
|
type = Crossing(detail, j, dEdge);
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (type)
|
2015-01-10 14:47:46 +01:00
|
|
|
{
|
|
|
|
case CrossingType::EdgeError:
|
|
|
|
return false;
|
|
|
|
case CrossingType::Intersection:
|
|
|
|
detail.Mirror(globalEdge);
|
|
|
|
flagMirror = true;
|
|
|
|
break;
|
|
|
|
case CrossingType::NoIntersection:
|
|
|
|
{
|
|
|
|
switch (InsideContour(detail, dEdge))
|
|
|
|
{
|
|
|
|
case InsideType::EdgeError:
|
|
|
|
return false;
|
|
|
|
case InsideType::Inside:
|
|
|
|
detail.Mirror(globalEdge);
|
|
|
|
flagMirror = true;
|
|
|
|
break;
|
|
|
|
case InsideType::Outside:
|
|
|
|
flagSquare = true;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (flagMirror)
|
|
|
|
{
|
2015-01-16 13:54:37 +01:00
|
|
|
#ifdef LAYOUT_DEBUG
|
2015-01-18 19:56:01 +01:00
|
|
|
#ifdef SHOW_MIRROR
|
2015-01-21 19:56:59 +01:00
|
|
|
DrawDebug(d->globalContour, detail, d->frame+1, d->paperIndex, d->details.count(), d->details);
|
2015-01-18 19:56:01 +01:00
|
|
|
#endif
|
2015-01-16 13:54:37 +01:00
|
|
|
#endif
|
|
|
|
|
2015-01-10 14:47:46 +01:00
|
|
|
dEdge = detail.EdgeByPoint(globalEdge.p2());
|
|
|
|
if (dEdge <= 0)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-01-19 11:36:27 +01:00
|
|
|
CrossingType type = CrossingType::Intersection;
|
|
|
|
if (SheetContains(detail.BoundingRect()))
|
|
|
|
{
|
|
|
|
type = Crossing(detail, j, dEdge);
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (type)
|
2015-01-10 14:47:46 +01:00
|
|
|
{
|
|
|
|
case CrossingType::EdgeError:
|
|
|
|
return false;
|
|
|
|
case CrossingType::Intersection:
|
|
|
|
flagSquare = false;
|
|
|
|
break;
|
|
|
|
case CrossingType::NoIntersection:
|
|
|
|
{
|
|
|
|
switch (InsideContour(detail, dEdge))
|
|
|
|
{
|
|
|
|
case InsideType::EdgeError:
|
|
|
|
return false;
|
|
|
|
case InsideType::Inside:
|
|
|
|
flagSquare = false;
|
|
|
|
break;
|
|
|
|
case InsideType::Outside:
|
|
|
|
flagSquare = true;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return flagSquare;
|
|
|
|
}
|
|
|
|
|
2015-01-12 16:59:15 +01:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
bool VLayoutPaper::CheckRotationEdges(VLayoutDetail &detail, int j, int dEdge, int angle) const
|
|
|
|
{
|
2015-01-21 19:56:59 +01:00
|
|
|
const QLineF globalEdge = d->globalContour.GlobalEdge(j);
|
2015-01-12 16:59:15 +01:00
|
|
|
bool flagSquare = false;
|
|
|
|
|
|
|
|
RotateEdges(detail, globalEdge, dEdge, angle);
|
|
|
|
|
2015-01-12 21:35:32 +01:00
|
|
|
#ifdef LAYOUT_DEBUG
|
2015-01-17 14:14:28 +01:00
|
|
|
#ifdef SHOW_ROTATION
|
2015-01-21 19:56:59 +01:00
|
|
|
DrawDebug(d->globalContour, detail, d->frame, d->paperIndex, d->details.count(), d->details);
|
2015-01-17 14:14:28 +01:00
|
|
|
#endif
|
2015-01-12 21:35:32 +01:00
|
|
|
#endif
|
|
|
|
|
2015-01-19 11:36:27 +01:00
|
|
|
CrossingType type = CrossingType::Intersection;
|
|
|
|
if (SheetContains(detail.BoundingRect()))
|
|
|
|
{
|
|
|
|
type = Crossing(detail, j, dEdge);
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (type)
|
2015-01-12 16:59:15 +01:00
|
|
|
{
|
|
|
|
case CrossingType::EdgeError:
|
|
|
|
return false;
|
|
|
|
case CrossingType::Intersection:
|
|
|
|
flagSquare = false;
|
|
|
|
break;
|
|
|
|
case CrossingType::NoIntersection:
|
|
|
|
{
|
|
|
|
switch (InsideContour(detail, dEdge))
|
|
|
|
{
|
|
|
|
case InsideType::EdgeError:
|
|
|
|
return false;
|
|
|
|
case InsideType::Inside:
|
|
|
|
flagSquare = false;
|
|
|
|
break;
|
|
|
|
case InsideType::Outside:
|
|
|
|
flagSquare = true;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return flagSquare;
|
|
|
|
}
|
|
|
|
|
2015-01-10 14:47:46 +01:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2015-01-18 19:56:01 +01:00
|
|
|
VLayoutPaper::CrossingType VLayoutPaper::Crossing(const VLayoutDetail &detail, const int &globalI,
|
|
|
|
const int &detailI) const
|
2015-01-10 14:47:46 +01:00
|
|
|
{
|
2015-01-21 19:56:59 +01:00
|
|
|
int globalEdgesCount = d->globalContour.EdgesCount();
|
2015-01-10 14:47:46 +01:00
|
|
|
if (globalEdgesCount == 0)
|
|
|
|
{
|
|
|
|
globalEdgesCount = 1;// For blank sheet
|
|
|
|
}
|
|
|
|
|
|
|
|
const int detailEdgesCount = detail.EdgesCount();
|
|
|
|
if (detailEdgesCount < 3)
|
|
|
|
{
|
|
|
|
return CrossingType::EdgeError;
|
|
|
|
}
|
|
|
|
|
|
|
|
for(int i = 1; i <= globalEdgesCount; i++)
|
|
|
|
{
|
2015-01-21 19:56:59 +01:00
|
|
|
const QLineF globalEdge = d->globalContour.GlobalEdge(i);
|
2015-01-10 14:47:46 +01:00
|
|
|
if (globalEdge.isNull()) // Got null edge
|
|
|
|
{
|
|
|
|
return CrossingType::EdgeError;
|
|
|
|
}
|
|
|
|
|
|
|
|
for(int j = 1; j <= detailEdgesCount; j++)
|
|
|
|
{
|
|
|
|
if (i == globalI && j == detailI)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
const QLineF detailEdge = detail.Edge(j);
|
|
|
|
if (detailEdge.isNull()) // Got null edge
|
|
|
|
{
|
|
|
|
return CrossingType::EdgeError;
|
|
|
|
}
|
|
|
|
|
|
|
|
QPointF xPoint;
|
|
|
|
QLineF::IntersectType type = globalEdge.intersect(detailEdge, &xPoint);
|
|
|
|
|
|
|
|
if (type == QLineF::BoundedIntersection)
|
|
|
|
{
|
2015-01-21 19:56:59 +01:00
|
|
|
if (TrueIntersection(d->globalContour.GlobalEdge(globalI), detail.Edge(detailI), xPoint))
|
2015-01-16 15:22:42 +01:00
|
|
|
{
|
|
|
|
return CrossingType::Intersection;
|
|
|
|
}
|
2015-01-10 14:47:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return CrossingType::NoIntersection;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2015-01-18 19:56:01 +01:00
|
|
|
VLayoutPaper::InsideType VLayoutPaper::InsideContour(const VLayoutDetail &detail, const int &detailI) const
|
2015-01-10 14:47:46 +01:00
|
|
|
{
|
|
|
|
if (detail.EdgesCount() < 3)
|
|
|
|
{
|
|
|
|
return InsideType::EdgeError;
|
|
|
|
}
|
|
|
|
|
2015-01-11 14:11:56 +01:00
|
|
|
const QVector<QPointF> lPoints = detail.GetLayoutAllowencePoints();
|
2015-01-10 14:47:46 +01:00
|
|
|
|
|
|
|
const QLineF detailEdge = detail.Edge(detailI);
|
|
|
|
if (detailEdge.isNull()) // Got null edge
|
|
|
|
{
|
|
|
|
return InsideType::EdgeError;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (d->details.isEmpty())
|
|
|
|
{
|
2015-01-21 19:56:59 +01:00
|
|
|
const QLineF globalEdge = d->globalContour.GlobalEdge(1);
|
2015-01-10 14:47:46 +01:00
|
|
|
for(int i = 0; i < lPoints.count(); i++)
|
|
|
|
{
|
|
|
|
if (CheckSide(globalEdge, lPoints.at(i)) < 0)
|
|
|
|
{
|
|
|
|
return InsideType::Inside;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-01-21 19:56:59 +01:00
|
|
|
const int polyCorners = d->globalContour.EdgesCount();
|
2015-01-18 19:56:01 +01:00
|
|
|
int j = polyCorners-1;
|
|
|
|
|
|
|
|
QVector<qreal> constant;
|
|
|
|
QVector<qreal> multiple;
|
|
|
|
|
|
|
|
for(int i=0; i<polyCorners; i++)
|
2015-01-10 14:47:46 +01:00
|
|
|
{
|
2015-01-18 19:56:01 +01:00
|
|
|
const qreal xi = d->globalContour.at(i).x();
|
|
|
|
const qreal xj = d->globalContour.at(j).x();
|
|
|
|
const qreal yi = d->globalContour.at(i).y();
|
|
|
|
const qreal yj = d->globalContour.at(j).y();
|
|
|
|
if(qFuzzyCompare(yj, yi))
|
|
|
|
{
|
|
|
|
constant.insert(i, xi);
|
|
|
|
multiple.insert(i, 0);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
constant.insert(i, xi - (yi*xj)/(yj-yi) + (yi*xi)/(yj-yi));
|
|
|
|
multiple.insert(i, (xj-xi)/(yj-yi));
|
|
|
|
}
|
|
|
|
|
|
|
|
j=i;
|
|
|
|
}
|
|
|
|
|
2015-01-19 11:14:54 +01:00
|
|
|
for(int m = 1; m <= detail.EdgesCount(); ++m)
|
2015-01-18 19:56:01 +01:00
|
|
|
{
|
2015-01-19 11:14:54 +01:00
|
|
|
if (m == detailI)
|
2015-01-10 14:47:46 +01:00
|
|
|
{
|
2015-01-19 11:14:54 +01:00
|
|
|
continue;
|
2015-01-10 14:47:46 +01:00
|
|
|
}
|
|
|
|
|
2015-01-19 11:14:54 +01:00
|
|
|
const QLineF detailEdge = detail.Edge(m);
|
|
|
|
if (detailEdge.isNull()) // Got null edge
|
2015-01-10 14:47:46 +01:00
|
|
|
{
|
2015-01-19 11:14:54 +01:00
|
|
|
return InsideType::EdgeError;
|
|
|
|
}
|
2015-01-18 19:56:01 +01:00
|
|
|
|
2015-01-19 11:14:54 +01:00
|
|
|
const QVector<QPointF> p = Triplet(detailEdge);
|
|
|
|
for (int n=0; n<p.size(); ++n )
|
|
|
|
{
|
2015-01-19 19:01:42 +01:00
|
|
|
int j = polyCorners-1;
|
|
|
|
bool oddNodes = false;
|
|
|
|
|
|
|
|
for (int i=0; i<polyCorners; i++)
|
2015-01-18 19:56:01 +01:00
|
|
|
{
|
2015-01-19 19:01:42 +01:00
|
|
|
const qreal yi = d->globalContour.at(i).y();
|
|
|
|
const qreal yj = d->globalContour.at(j).y();
|
2015-01-18 19:56:01 +01:00
|
|
|
|
2015-01-19 19:01:42 +01:00
|
|
|
if (((yi < p.at(n).y() && yj >= p.at(n).y()) || (yj < p.at(n).y() && yi >= p.at(n).y())))
|
2015-01-18 19:56:01 +01:00
|
|
|
{
|
2015-01-19 19:01:42 +01:00
|
|
|
oddNodes ^= (p.at(n).y() * multiple.at(i) + constant.at(i) < p.at(n).x());
|
2015-01-19 11:14:54 +01:00
|
|
|
}
|
|
|
|
|
2015-01-19 19:01:42 +01:00
|
|
|
j=i;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (oddNodes)
|
|
|
|
{
|
|
|
|
return InsideType::Inside;
|
2015-01-10 14:47:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return InsideType::Outside;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
qreal VLayoutPaper::CheckSide(const QLineF &edge, const QPointF &p) const
|
|
|
|
{
|
|
|
|
return (edge.x2() - edge.x1()) * (p.y() - edge.y1()) - (edge.y2() - edge.y1()) * (p.x() - edge.x1());
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
bool VLayoutPaper::SheetContains(const QRectF &rect) const
|
|
|
|
{
|
2015-01-21 19:56:59 +01:00
|
|
|
const QRectF bRect(0, 0, d->globalContour.GetWidth(), d->globalContour.GetHeight());
|
2015-01-10 14:47:46 +01:00
|
|
|
return bRect.contains(rect);
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2015-01-18 19:56:01 +01:00
|
|
|
void VLayoutPaper::CombineEdges(VLayoutDetail &detail, const QLineF &globalEdge, const int &dEdge) const
|
2015-01-10 14:47:46 +01:00
|
|
|
{
|
|
|
|
QLineF detailEdge = detail.Edge(dEdge);
|
|
|
|
|
|
|
|
// Find distance between two edges for two begin vertex.
|
|
|
|
const qreal dx = globalEdge.x2() - detailEdge.x2();
|
|
|
|
const qreal dy = globalEdge.y2() - detailEdge.y2();
|
|
|
|
|
|
|
|
detailEdge.translate(dx, dy); // Use values for translate detail edge.
|
|
|
|
|
|
|
|
const qreal angle_between = globalEdge.angleTo(detailEdge); // Seek angle between two edges.
|
|
|
|
|
|
|
|
// Now we move detail to position near to global contour edge.
|
|
|
|
detail.Translate(dx, dy);
|
2015-01-16 13:54:37 +01:00
|
|
|
detail.Rotate(detailEdge.p2(), -angle_between);
|
2015-01-10 14:47:46 +01:00
|
|
|
}
|
|
|
|
|
2015-01-12 16:59:15 +01:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VLayoutPaper::RotateEdges(VLayoutDetail &detail, const QLineF &globalEdge, int dEdge, int angle) const
|
|
|
|
{
|
|
|
|
QLineF detailEdge = detail.Edge(dEdge);
|
|
|
|
|
|
|
|
// Find distance between two edges for two begin vertex.
|
|
|
|
const qreal dx = globalEdge.x2() - detailEdge.x2();
|
|
|
|
const qreal dy = globalEdge.y2() - detailEdge.y2();
|
|
|
|
|
|
|
|
detailEdge.translate(dx, dy); // Use values for translate detail edge.
|
|
|
|
|
|
|
|
// Now we move detail to position near to global contour edge.
|
|
|
|
detail.Translate(dx, dy);
|
|
|
|
detail.Rotate(globalEdge.p2(), angle);
|
|
|
|
}
|
|
|
|
|
2015-01-10 14:47:46 +01:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2015-01-21 19:56:59 +01:00
|
|
|
bool VLayoutPaper::SaveResult(const VBestSquare &bestResult, const VLayoutDetail &detail)
|
2015-01-12 16:23:25 +01:00
|
|
|
{
|
|
|
|
if (bestResult.ValideResult())
|
|
|
|
{
|
|
|
|
VLayoutDetail workDetail = detail;
|
|
|
|
workDetail.SetMatrix(bestResult.Matrix());// Don't forget set matrix
|
2015-01-18 19:56:01 +01:00
|
|
|
workDetail.SetMirror(bestResult.Mirror());
|
2015-01-21 19:56:59 +01:00
|
|
|
const QVector<QPointF> newGContour = d->globalContour.UniteWithContour(workDetail, bestResult.GContourEdge(),
|
|
|
|
bestResult.DetailEdge(),
|
|
|
|
bestResult.Type());
|
2015-01-12 16:23:25 +01:00
|
|
|
if (newGContour.isEmpty())
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
d->details.append(workDetail);
|
2015-01-21 19:56:59 +01:00
|
|
|
d->globalContour.SetContour(newGContour);
|
2015-01-18 19:56:01 +01:00
|
|
|
|
|
|
|
#ifdef LAYOUT_DEBUG
|
|
|
|
# ifdef SHOW_BEST
|
2015-01-21 19:56:59 +01:00
|
|
|
DrawDebug(d->globalContour, workDetail, UINT_MAX, d->paperIndex, d->details.count(), d->details);
|
2015-01-18 19:56:01 +01:00
|
|
|
# endif
|
|
|
|
#endif
|
2015-01-12 16:23:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return bestResult.ValideResult(); // Do we have the best result?
|
|
|
|
}
|
2015-01-12 21:35:32 +01:00
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2015-01-21 19:56:59 +01:00
|
|
|
void VLayoutPaper::SaveCandidate(VBestSquare &bestResult, const VLayoutDetail &detail, int globalI, int detJ,
|
|
|
|
BestFrom type)
|
2015-01-12 21:35:32 +01:00
|
|
|
{
|
2015-01-21 19:56:59 +01:00
|
|
|
QVector<QPointF> newGContour = d->globalContour.UniteWithContour(detail, globalI, detJ, type);
|
|
|
|
newGContour.append(newGContour.first());
|
|
|
|
const QRectF rec = QPolygonF(newGContour).boundingRect();
|
|
|
|
bestResult.NewResult(static_cast<qint64>(rec.width()*rec.height()), globalI, detJ,
|
|
|
|
detail.GetMatrix(), detail.IsMirror(), type);
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VLayoutPaper::DrawDebug(const VContour &contour, const VLayoutDetail &detail, int frame, quint32 paperIndex,
|
|
|
|
int detailsCount, const QVector<VLayoutDetail> &details)
|
|
|
|
{
|
|
|
|
QImage frameImage(contour.GetWidth()*2, contour.GetHeight()*2, QImage::Format_RGB32);
|
2015-01-12 21:35:32 +01:00
|
|
|
frameImage.fill(Qt::white);
|
|
|
|
QPainter paint;
|
|
|
|
paint.begin(&frameImage);
|
|
|
|
|
2015-01-20 13:50:24 +01:00
|
|
|
paint.setPen(QPen(Qt::darkRed, 10, Qt::SolidLine, Qt::FlatCap, Qt::MiterJoin));
|
2015-01-21 19:56:59 +01:00
|
|
|
paint.drawRect(QRectF(contour.GetWidth()/2, contour.GetHeight()/2, contour.GetWidth(), contour.GetHeight()));
|
2015-01-16 13:54:37 +01:00
|
|
|
|
|
|
|
paint.setPen(QPen(Qt::black, 3, Qt::SolidLine, Qt::FlatCap, Qt::MiterJoin));
|
|
|
|
QPainterPath p;
|
2015-01-21 19:56:59 +01:00
|
|
|
if (contour.GetContour().isEmpty())
|
2015-01-12 21:35:32 +01:00
|
|
|
{
|
2015-01-21 19:56:59 +01:00
|
|
|
p = DrawContour(contour.CutEdge(QLineF(0, 0, contour.GetWidth(), 0)));
|
|
|
|
p.translate(contour.GetWidth()/2, contour.GetHeight()/2);
|
2015-01-16 13:54:37 +01:00
|
|
|
paint.drawPath(p);
|
2015-01-12 21:35:32 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-01-21 19:56:59 +01:00
|
|
|
p = DrawContour(contour.GetContour());
|
|
|
|
p.translate(contour.GetWidth()/2, contour.GetHeight()/2);
|
2015-01-16 13:54:37 +01:00
|
|
|
paint.drawPath(p);
|
2015-01-12 21:35:32 +01:00
|
|
|
}
|
|
|
|
|
2015-01-18 19:56:01 +01:00
|
|
|
#ifdef SHOW_CANDIDATE
|
2015-01-16 13:54:37 +01:00
|
|
|
paint.setPen(QPen(Qt::darkGreen, 3, Qt::SolidLine, Qt::FlatCap, Qt::MiterJoin));
|
|
|
|
p = DrawContour(detail.GetLayoutAllowencePoints());
|
2015-01-21 19:56:59 +01:00
|
|
|
p.translate(contour.GetWidth()/2, contour.GetHeight()/2);
|
2015-01-16 13:54:37 +01:00
|
|
|
paint.drawPath(p);
|
2015-01-18 19:56:01 +01:00
|
|
|
#endif
|
2015-01-12 21:35:32 +01:00
|
|
|
|
|
|
|
#ifdef ARRANGED_DETAILS
|
|
|
|
paint.setPen(QPen(Qt::blue, 1, Qt::SolidLine, Qt::FlatCap, Qt::MiterJoin));
|
2015-01-21 19:56:59 +01:00
|
|
|
p = DrawDetails(details);
|
|
|
|
p.translate(contour.GetWidth()/2, contour.GetHeight()/2);
|
2015-01-16 13:54:37 +01:00
|
|
|
paint.drawPath(p);
|
2015-01-19 11:36:27 +01:00
|
|
|
#else
|
|
|
|
Q_UNUSED(detail)
|
2015-01-12 21:35:32 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
paint.end();
|
2015-01-21 19:56:59 +01:00
|
|
|
const QString path = QDir::homePath()+QStringLiteral("/LayoutDebug/")+QString("%1_%2_%3.png").arg(paperIndex)
|
|
|
|
.arg(detailsCount).arg(frame);
|
2015-01-12 21:35:32 +01:00
|
|
|
frameImage.save (path);
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2015-01-21 19:56:59 +01:00
|
|
|
QPainterPath VLayoutPaper::ShowDirection(const QLineF &edge)
|
2015-01-12 21:35:32 +01:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2015-01-21 19:56:59 +01:00
|
|
|
QPainterPath VLayoutPaper::DrawContour(const QVector<QPointF> &points)
|
2015-01-12 21:35:32 +01:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-01-16 13:54:37 +01:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2015-01-21 19:56:59 +01:00
|
|
|
QPainterPath VLayoutPaper::DrawDetails(const QVector<VLayoutDetail> &details)
|
2015-01-12 21:35:32 +01:00
|
|
|
{
|
|
|
|
QPainterPath path;
|
|
|
|
path.setFillRule(Qt::WindingFill);
|
2015-01-21 19:56:59 +01:00
|
|
|
if (details.count() > 0)
|
2015-01-12 21:35:32 +01:00
|
|
|
{
|
2015-01-21 19:56:59 +01:00
|
|
|
for (int i = 0; i < details.size(); ++i)
|
2015-01-12 21:35:32 +01:00
|
|
|
{
|
2015-01-21 19:56:59 +01:00
|
|
|
path.addPath(details.at(i).ContourPath());
|
2015-01-12 21:35:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return path;
|
|
|
|
}
|
2015-01-13 11:38:51 +01:00
|
|
|
|
2015-01-16 15:22:42 +01:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
bool VLayoutPaper::TrueIntersection(const QLineF &gEdge, const QLineF &dEdge, const QPointF &p) const
|
|
|
|
{
|
|
|
|
const QPointF pX = RoundedPoint(p);
|
|
|
|
const QPointF gP1 = RoundedPoint(gEdge.p1());
|
|
|
|
const QPointF gP2 = RoundedPoint(gEdge.p2());
|
|
|
|
const QPointF dP1 = RoundedPoint(dEdge.p1());
|
|
|
|
const QPointF dP2 = RoundedPoint(dEdge.p2());
|
2015-01-18 21:21:20 +01:00
|
|
|
return !(pX == gP1 || pX == gP2 || pX == dP1 || pX == dP2);
|
2015-01-16 15:22:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
QPointF VLayoutPaper::RoundedPoint(const QPointF &p) const
|
|
|
|
{
|
|
|
|
return QPointF(qRound(p.x()), qRound(p.y()));
|
|
|
|
}
|
|
|
|
|
2015-01-19 11:14:54 +01:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
QVector<QPointF> VLayoutPaper::Triplet(const QLineF &edge) const
|
|
|
|
{
|
|
|
|
QVector<QPointF> p;
|
|
|
|
QLineF line = edge;
|
|
|
|
line.setLength(edge.length()/2);
|
|
|
|
|
|
|
|
p.append(edge.p1());
|
|
|
|
p.append(line.p2());
|
|
|
|
p.append(edge.p2());
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2015-01-13 11:38:51 +01:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
QGraphicsItem *VLayoutPaper::GetItem() const
|
|
|
|
{
|
2015-01-21 19:56:59 +01:00
|
|
|
QGraphicsRectItem *paper = new QGraphicsRectItem(QRectF(0, 0, d->globalContour.GetWidth(),
|
|
|
|
d->globalContour.GetHeight()));
|
2015-01-13 11:38:51 +01:00
|
|
|
paper->setPen(QPen(Qt::black, 1));
|
|
|
|
paper->setBrush(QBrush(Qt::white));
|
|
|
|
for (int i=0; i < d->details.count(); ++i)
|
|
|
|
{
|
|
|
|
QGraphicsItem *item = d->details.at(i).GetItem();
|
|
|
|
item->setParentItem(paper);
|
|
|
|
}
|
|
|
|
return paper;
|
|
|
|
}
|