LibrePCB Developers Documentation
clipperhelpers.h
Go to the documentation of this file.
1 /*
2  * LibrePCB - Professional EDA for everyone!
3  * Copyright (C) 2013 LibrePCB Developers, see AUTHORS.md for contributors.
4  * https://librepcb.org/
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #ifndef LIBREPCB_CORE_CLIPPERHELPERS_H
21 #define LIBREPCB_CORE_CLIPPERHELPERS_H
22 
23 /*******************************************************************************
24  * Includes
25  ******************************************************************************/
26 #include "../geometry/path.h"
27 
28 #include <polyclipping/clipper.hpp>
29 
30 #include <QtCore>
31 
32 #include <memory>
33 
34 /*******************************************************************************
35  * Namespace / Forward Declarations
36  ******************************************************************************/
37 namespace librepcb {
38 
39 /*******************************************************************************
40  * Class ClipperHelpers
41  ******************************************************************************/
42 
46 class ClipperHelpers final {
47  Q_DECLARE_TR_FUNCTIONS(ClipperHelpers)
48 
49 public:
50  // Disable instantiation
51  ClipperHelpers() = delete;
52  ~ClipperHelpers() = delete;
53 
54  // General Methods
55  static bool allPointsInside(const ClipperLib::Path& points,
56  const ClipperLib::Path& path);
57  static bool anyPointsInside(const ClipperLib::Path& points,
58  const ClipperLib::Path& path);
59  static bool anyPointsInside(const ClipperLib::Paths& points,
60  const ClipperLib::Path& path);
61  static void unite(ClipperLib::Paths& paths,
62  ClipperLib::PolyFillType fillType);
63  static void unite(ClipperLib::Paths& subject, const ClipperLib::Paths& clip,
64  ClipperLib::PolyFillType subjectFillType,
65  ClipperLib::PolyFillType clipFillType);
66  static std::unique_ptr<ClipperLib::PolyTree> uniteToTree(
67  const ClipperLib::Paths& paths, ClipperLib::PolyFillType fillType);
68  static std::unique_ptr<ClipperLib::PolyTree> uniteToTree(
69  const ClipperLib::Paths& paths, const ClipperLib::Paths& clip,
70  ClipperLib::PolyFillType subjectFillType,
71  ClipperLib::PolyFillType clipFillType);
72  static void intersect(ClipperLib::Paths& subject,
73  const ClipperLib::Paths& clip,
74  ClipperLib::PolyFillType subjectFillType,
75  ClipperLib::PolyFillType clipFillType);
76  static std::unique_ptr<ClipperLib::PolyTree> intersectToTree(
77  const ClipperLib::Paths& subject, const ClipperLib::Paths& clip,
78  ClipperLib::PolyFillType subjectFillType,
79  ClipperLib::PolyFillType clipFillType, bool closed = true);
80  static std::unique_ptr<ClipperLib::PolyTree> intersectToTree(
81  const QList<ClipperLib::Paths>& paths);
82  static void subtract(ClipperLib::Paths& subject,
83  const ClipperLib::Paths& clip,
84  ClipperLib::PolyFillType subjectFillType,
85  ClipperLib::PolyFillType clipFillType);
86  static std::unique_ptr<ClipperLib::PolyTree> subtractToTree(
87  const ClipperLib::Paths& subject, const ClipperLib::Paths& clip,
88  ClipperLib::PolyFillType subjectFillType,
89  ClipperLib::PolyFillType clipFillType, bool closed = true);
90  static void offset(ClipperLib::Paths& paths, const Length& offset,
91  const PositiveLength& maxArcTolerance,
92  ClipperLib::JoinType joinType = ClipperLib::jtRound);
93  static std::unique_ptr<ClipperLib::PolyTree> offsetToTree(
94  const ClipperLib::Paths& paths, const Length& offset,
95  const PositiveLength& maxArcTolerance);
96  static ClipperLib::Paths treeToPaths(const ClipperLib::PolyTree& tree);
97  static ClipperLib::Paths flattenTree(const ClipperLib::PolyNode& node);
98 
99  // Type Conversions
100  static QVector<Path> convert(const ClipperLib::Paths& paths) noexcept;
101  static Path convert(const ClipperLib::Path& path) noexcept;
102  static Point convert(const ClipperLib::IntPoint& point) noexcept;
103  static ClipperLib::Paths convert(
104  const QVector<Path>& paths,
105  const PositiveLength& maxArcTolerance) noexcept;
106  static ClipperLib::Path convert(
107  const Path& path, const PositiveLength& maxArcTolerance) noexcept;
108  static ClipperLib::IntPoint convert(const Point& point) noexcept;
109 
110 private: // Internal Helper Methods
111  static ClipperLib::Path convertHolesToCutIns(const ClipperLib::Path& outline,
112  const ClipperLib::Paths& holes);
113  static ClipperLib::Paths prepareHoles(
114  const ClipperLib::Paths& holes) noexcept;
115  static ClipperLib::Path rotateCutInHole(
116  const ClipperLib::Path& hole) noexcept;
117  static void addCutInToPath(ClipperLib::Path& outline,
118  const ClipperLib::Path& hole);
119  static int insertConnectionPointToPath(ClipperLib::Path& path,
120  const ClipperLib::IntPoint& p);
121  static bool calcIntersectionPos(const ClipperLib::IntPoint& p1,
122  const ClipperLib::IntPoint& p2,
123  const ClipperLib::cInt& x,
124  ClipperLib::cInt& y) noexcept;
125 };
126 
127 /*******************************************************************************
128  * End of File
129  ******************************************************************************/
130 
131 } // namespace librepcb
132 
133 #endif
static std::unique_ptr< ClipperLib::PolyTree > subtractToTree(const ClipperLib::Paths &subject, const ClipperLib::Paths &clip, ClipperLib::PolyFillType subjectFillType, ClipperLib::PolyFillType clipFillType, bool closed=true)
Definition: clipperhelpers.cpp:232
static bool anyPointsInside(const ClipperLib::Path &points, const ClipperLib::Path &path)
Definition: clipperhelpers.cpp:52
Definition: occmodel.cpp:76
static std::unique_ptr< ClipperLib::PolyTree > uniteToTree(const ClipperLib::Paths &paths, ClipperLib::PolyFillType fillType)
Definition: clipperhelpers.cpp:113
static ClipperLib::Paths prepareHoles(const ClipperLib::Paths &holes) noexcept
Definition: clipperhelpers.cpp:389
static void unite(ClipperLib::Paths &paths, ClipperLib::PolyFillType fillType)
Definition: clipperhelpers.cpp:86
static void addCutInToPath(ClipperLib::Path &outline, const ClipperLib::Path &hole)
Definition: clipperhelpers.cpp:424
static std::unique_ptr< ClipperLib::PolyTree > intersectToTree(const ClipperLib::Paths &subject, const ClipperLib::Paths &clip, ClipperLib::PolyFillType subjectFillType, ClipperLib::PolyFillType clipFillType, bool closed=true)
Definition: clipperhelpers.cpp:164
The Point class is used to represent a point/coordinate/vector, for example (1.2mm; 5...
Definition: point.h:78
static bool allPointsInside(const ClipperLib::Path &points, const ClipperLib::Path &path)
Definition: clipperhelpers.cpp:36
static ClipperLib::Paths flattenTree(const ClipperLib::PolyNode &node)
Definition: clipperhelpers.cpp:294
static QVector< Path > convert(const ClipperLib::Paths &paths) noexcept
Definition: clipperhelpers.cpp:318
static ClipperLib::Path rotateCutInHole(const ClipperLib::Path &hole) noexcept
Definition: clipperhelpers.cpp:409
static int insertConnectionPointToPath(ClipperLib::Path &path, const ClipperLib::IntPoint &p)
Definition: clipperhelpers.cpp:430
static void subtract(ClipperLib::Paths &subject, const ClipperLib::Paths &clip, ClipperLib::PolyFillType subjectFillType, ClipperLib::PolyFillType clipFillType)
Definition: clipperhelpers.cpp:217
static ClipperLib::Path convertHolesToCutIns(const ClipperLib::Path &outline, const ClipperLib::Paths &holes)
Definition: clipperhelpers.cpp:373
The Path class represents a list of vertices connected by straight lines or circular arc segments...
Definition: path.h:57
type_safe::constrained_type< Length, PositiveLengthConstraint, PositiveLengthVerifier > PositiveLength
Definition: length.h:785
The ClipperHelpers class.
Definition: clipperhelpers.h:46
static void offset(ClipperLib::Paths &paths, const Length &offset, const PositiveLength &maxArcTolerance, ClipperLib::JoinType joinType=ClipperLib::jtRound)
Definition: clipperhelpers.cpp:251
static void intersect(ClipperLib::Paths &subject, const ClipperLib::Paths &clip, ClipperLib::PolyFillType subjectFillType, ClipperLib::PolyFillType clipFillType)
Definition: clipperhelpers.cpp:148
static bool calcIntersectionPos(const ClipperLib::IntPoint &p1, const ClipperLib::IntPoint &p2, const ClipperLib::cInt &x, ClipperLib::cInt &y) noexcept
Definition: clipperhelpers.cpp:457
The Length class is used to represent a length (for example 12.75 millimeters)
Definition: length.h:82
static ClipperLib::Paths treeToPaths(const ClipperLib::PolyTree &tree)
Definition: clipperhelpers.cpp:281
static std::unique_ptr< ClipperLib::PolyTree > offsetToTree(const ClipperLib::Paths &paths, const Length &offset, const PositiveLength &maxArcTolerance)
Definition: clipperhelpers.cpp:264