1 /*
2     Scan Tailor - Interactive post-processing tool for scanned pages.
3     Copyright (C)  Joseph Artsimovich <joseph.artsimovich@gmail.com>
4 
5     This program is free software: you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation, either version 3 of the License, or
8     (at your option) any later version.
9 
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14 
15     You should have received a copy of the GNU General Public License
16     along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #ifndef IMAGEPROC_POLYGONUTILS_H_
20 #define IMAGEPROC_POLYGONUTILS_H_
21 
22 #include <vector>
23 
24 class QPolygonF;
25 class QPointF;
26 class QLineF;
27 
28 namespace imageproc {
29 class PolygonUtils {
30  public:
31   /**
32    * \brief Adjust vertices to more round coordinates.
33    *
34    * This method exists to workaround bugs in QPainterPath and QPolygonF
35    * composition operations.  It turns out rounding vertex coordinates
36    * solves many of those bugs.  We don't round to integer values, we
37    * only make very minor adjustments.
38    */
39   static QPolygonF round(const QPolygonF& poly);
40 
41   /**
42    * \brief Test if two polygons are logically equal.
43    *
44    * By logical equality we mean that the following differences don't matter:
45    * \li Direction (clockwise vs counter-clockwise).
46    * \li Closed vs unclosed.
47    * \li Tiny differences in vertex coordinates.
48    *
49    * \return true if polygons are logically equal, false otherwise.
50    */
51   static bool fuzzyCompare(const QPolygonF& poly1, const QPolygonF& poly2);
52 
53   static QPolygonF convexHull(std::vector<QPointF> point_cloud);
54 
55  private:
56   class Before;
57 
58   static QPointF roundPoint(const QPointF& p);
59 
60   static double roundValue(double val);
61 
62   static std::vector<QLineF> extractAndNormalizeEdges(const QPolygonF& poly);
63 
64   static void maybeAddNormalizedEdge(std::vector<QLineF>& edges, const QPointF& p1, const QPointF& p2);
65 
66   static bool fuzzyCompareImpl(const std::vector<QLineF>& lines1, const std::vector<QLineF>& lines2);
67 
68   static bool fuzzyCompareImpl(const QLineF& line1, const QLineF& line2);
69 
70   static bool fuzzyCompareImpl(const QPointF& p1, const QPointF& p2);
71 
72   static const double ROUNDING_MULTIPLIER;
73   static const double ROUNDING_RECIP_MULTIPLIER;
74 };
75 }  // namespace imageproc
76 #endif  // ifndef IMAGEPROC_POLYGONUTILS_H_
77