From 09d2b96e732ff1c6c6ac27665f842ccfaf6f6fc7 Mon Sep 17 00:00:00 2001
From: Roman Telezhynskyi <kroluku@gmail.com>
Date: Sun, 27 Mar 2016 22:53:41 +0300
Subject: [PATCH] Endge of empty sheet shouldn't depend on shift value.

--HG--
branch : develop
---
 src/libs/vlayout/vcontour.cpp     | 81 +++++++++++++++----------------
 src/libs/vlayout/vcontour.h       |  3 +-
 src/libs/vlayout/vlayoutpaper.cpp |  2 +-
 src/libs/vlayout/vposition.cpp    |  6 +--
 4 files changed, 44 insertions(+), 48 deletions(-)

diff --git a/src/libs/vlayout/vcontour.cpp b/src/libs/vlayout/vcontour.cpp
index 9ed94520f..44931d241 100644
--- a/src/libs/vlayout/vcontour.cpp
+++ b/src/libs/vlayout/vcontour.cpp
@@ -133,7 +133,7 @@ QVector<QPointF> VContour::UniteWithContour(const VLayoutDetail &detail, int glo
     }
     else
     {
-        if (globalI <= 0 || globalI > EdgesCount())
+        if (globalI <= 0 || globalI > GlobalEdgesCount())
         {
             return QVector<QPointF>();
         }
@@ -205,24 +205,11 @@ QVector<QPointF> VContour::UniteWithContour(const VLayoutDetail &detail, int glo
 }
 
 //---------------------------------------------------------------------------------------------------------------------
-int VContour::EdgesCount() const
+int VContour::GlobalEdgesCount() const
 {
     if (d->globalContour.isEmpty())
     {
-        if (d->shift == 0)
-        {
-            return 1;
-        }
-
-        const int n = qFloor(EmptySheetEdge().length()/d->shift);
-        if (n <= 0)
-        {
-            return 1;
-        }
-        else
-        {
-            return n;
-        }
+        return 10;
     }
     else
     {
@@ -236,36 +223,28 @@ QLineF VContour::GlobalEdge(int i) const
     if (d->globalContour.isEmpty()) //-V807
     {
         // Because sheet is blank we have one global edge for all cases - Ox axis.
-        const QLineF axis = EmptySheetEdge();
-        if (d->shift == 0)
-        {
-            return axis;
-        }
-
-        const int n = qFloor(axis.length()/d->shift);
-
-        if (i < 1 || i > n)
+        if (i < 1 || i > GlobalEdgesCount())
         { // Doesn't exist such edge
-            return QLineF();
+            return EmptySheetEdge();
         }
 
-        const qreal nShift = axis.length()/n;
+        const qreal nShift = EmptySheetEdge().length()/GlobalEdgesCount();
         return QLineF(nShift*(i-1), 0, nShift*i, 0);
     }
     else
     {
-        if (i < 1 || i > EdgesCount())
+        if (i < 1 || i > GlobalEdgesCount())
         { // Doesn't exist such edge
             return QLineF();
         }
         QLineF edge;
-        if (i < EdgesCount())
+        if (i < GlobalEdgesCount())
         {
             edge = QLineF(d->globalContour.at(i-1), d->globalContour.at(i));
         }
         else
         { // Closed countour
-            edge = QLineF(d->globalContour.at(EdgesCount()-1), d->globalContour.at(0));
+            edge = QLineF(d->globalContour.at(GlobalEdgesCount()-1), d->globalContour.at(0));
         }
         return edge;
     }
@@ -280,23 +259,39 @@ QVector<QPointF> VContour::CutEdge(const QLineF &edge) const
         points.append(edge.p1());
         points.append(edge.p2());
     }
-
-    const int n = qFloor(edge.length()/d->shift);
-
-    if (n <= 0)
-    {
-        points.append(edge.p1());
-        points.append(edge.p2());
-    }
     else
     {
-        const qreal nShift = edge.length()/n;
-        for (int i = 1; i <= n+1; ++i)
+        const int n = qFloor(edge.length()/d->shift);
+
+        if (n <= 0)
         {
-            QLineF l1 = edge;
-            l1.setLength(nShift*(i-1));
-            points.append(l1.p2());
+            points.append(edge.p1());
+            points.append(edge.p2());
         }
+        else
+        {
+            const qreal nShift = edge.length()/n;
+            for (int i = 1; i <= n+1; ++i)
+            {
+                QLineF l1 = edge;
+                l1.setLength(nShift*(i-1));
+                points.append(l1.p2());
+            }
+        }
+    }
+    return points;
+}
+
+//---------------------------------------------------------------------------------------------------------------------
+QVector<QPointF> VContour::CutEmptySheetEdge() const
+{
+    QVector<QPointF> points;
+    const qreal nShift = EmptySheetEdge().length()/GlobalEdgesCount();
+    for (int i = 1; i <= GlobalEdgesCount()+1; ++i)
+    {
+        QLineF l1 = EmptySheetEdge();
+        l1.setLength(nShift*(i-1));
+        points.append(l1.p2());
     }
     return points;
 }
diff --git a/src/libs/vlayout/vcontour.h b/src/libs/vlayout/vcontour.h
index 659fbbf22..3fe7bcb97 100644
--- a/src/libs/vlayout/vcontour.h
+++ b/src/libs/vlayout/vcontour.h
@@ -66,9 +66,10 @@ public:
     QVector<QPointF> UniteWithContour(const VLayoutDetail &detail, int globalI, int detJ, BestFrom type) const;
 
     QLineF EmptySheetEdge() const;
-    int    EdgesCount() const;
+    int    GlobalEdgesCount() const;
     QLineF GlobalEdge(int i) const;
     QVector<QPointF> CutEdge(const QLineF &edge) const;
+    QVector<QPointF> CutEmptySheetEdge() const;
 
     const QPointF &	at(int i) const;
 
diff --git a/src/libs/vlayout/vlayoutpaper.cpp b/src/libs/vlayout/vlayoutpaper.cpp
index 5f58a148a..593b06fc0 100644
--- a/src/libs/vlayout/vlayoutpaper.cpp
+++ b/src/libs/vlayout/vlayoutpaper.cpp
@@ -198,7 +198,7 @@ bool VLayoutPaper::AddToSheet(const VLayoutDetail &detail, volatile bool &stop)
     thread_pool->setExpiryTimeout(1000);
     QVector<VPosition *> threads;
 
-    for (int j=1; j <= d->globalContour.EdgesCount(); ++j)
+    for (int j=1; j <= d->globalContour.GlobalEdgesCount(); ++j)
     {
         for (int i=1; i<= detail.EdgesCount(); i++)
         {
diff --git a/src/libs/vlayout/vposition.cpp b/src/libs/vlayout/vposition.cpp
index db5f5007c..68dd79b6c 100644
--- a/src/libs/vlayout/vposition.cpp
+++ b/src/libs/vlayout/vposition.cpp
@@ -162,7 +162,7 @@ void VPosition::DrawDebug(const VContour &contour, const VLayoutDetail &detail,
     QPainterPath p;
     if (contour.GetContour().isEmpty())
     {
-        p = DrawContour(contour.CutEdge(contour.EmptySheetEdge()));
+        p = DrawContour(contour.CutEmptySheetEdge());
         p.translate(biasWidth/2, biasHeight/2);
         paint.drawPath(p);
     }
@@ -410,7 +410,7 @@ bool VPosition::CheckRotationEdges(VLayoutDetail &detail, int j, int dEdge, int
 //---------------------------------------------------------------------------------------------------------------------
 VPosition::CrossingType VPosition::Crossing(const VLayoutDetail &detail, const int &globalI, const int &detailI) const
 {
-    int globalEdgesCount = gContour.EdgesCount();
+    int globalEdgesCount = gContour.GlobalEdgesCount();
     if (globalEdgesCount == 0)
     {
         globalEdgesCount = 1;// For blank sheet
@@ -489,7 +489,7 @@ VPosition::InsideType VPosition::InsideContour(const VLayoutDetail &detail, cons
     }
     else
     {
-        const int polyCorners = gContour.EdgesCount();
+        const int polyCorners = gContour.GlobalEdgesCount();
         int j = polyCorners-1;
 
         QVector<qreal> constant;