LibrePCB Developers Documentation
path.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_PATH_H
21#define LIBREPCB_CORE_PATH_H
22
23/*******************************************************************************
24 * Includes
25 ******************************************************************************/
26#include "../exceptions.h"
27#include "../qtcompat.h"
28#include "vertex.h"
29
30#include <type_safe/constrained_type.hpp>
31
32#include <QtCore>
33#include <QtGui>
34
35/*******************************************************************************
36 * Namespace / Forward Declarations
37 ******************************************************************************/
38namespace librepcb {
39
40/*******************************************************************************
41 * Class Path
42 ******************************************************************************/
43
58class Path final {
59 Q_DECLARE_TR_FUNCTIONS(Path)
60
61public:
62 // Constructors / Destructor
63 Path() noexcept : mVertices(), mPainterPathPx() {}
64 Path(const Path& other) noexcept;
65 explicit Path(const QVector<Vertex>& vertices) noexcept
66 : mVertices(vertices) {}
67 explicit Path(const SExpression& node);
68 ~Path() noexcept {}
69
70 // Getters
71 bool isClosed() const noexcept;
72 bool isCurved() const noexcept;
73 QVector<Vertex>& getVertices() noexcept {
75 return mVertices;
76 }
77 const QVector<Vertex>& getVertices() const noexcept { return mVertices; }
79 qreal calcAreaOfStraightSegments() const noexcept;
80 Point calcNearestPointBetweenVertices(const Point& p) const noexcept;
81 Path cleaned() const noexcept;
82 Path toClosedPath() const noexcept;
83 Path toOpenPath() const noexcept;
84 QVector<Path> toOutlineStrokes(const PositiveLength& width) const noexcept;
85 const QPainterPath& toQPainterPathPx() const noexcept;
86
87 // Transformations
88 Path& translate(const Point& offset) noexcept;
89 Path translated(const Point& offset) const noexcept;
90 Path& mapToGrid(const PositiveLength& gridInterval) noexcept;
91 Path mappedToGrid(const PositiveLength& gridInterval) const noexcept;
92 Path& rotate(const Angle& angle, const Point& center = Point(0, 0)) noexcept;
93 Path rotated(const Angle& angle,
94 const Point& center = Point(0, 0)) const noexcept;
95 Path& mirror(Qt::Orientation orientation,
96 const Point& center = Point(0, 0)) noexcept;
97 Path mirrored(Qt::Orientation orientation,
98 const Point& center = Point(0, 0)) const noexcept;
99 Path& reverse() noexcept;
100 Path reversed() const noexcept;
101 Path& flattenArcs(const PositiveLength& maxTolerance) noexcept;
102 Path flattenedArcs(const PositiveLength& maxTolerance) const noexcept;
103
104 // General Methods
105 void addVertex(const Vertex& vertex) noexcept;
106 void addVertex(const Point& pos, const Angle& angle = Angle::deg0()) noexcept;
107 void insertVertex(int index, const Vertex& vertex) noexcept;
108 void insertVertex(int index, const Point& pos,
109 const Angle& angle = Angle::deg0()) noexcept;
110 bool clean() noexcept;
111 bool close() noexcept;
112 bool open() noexcept;
113
119 void serialize(SExpression& root) const;
120
121 // Operator Overloadings
122 Path& operator=(const Path& rhs) noexcept;
123 bool operator==(const Path& rhs) const noexcept {
124 return mVertices == rhs.mVertices;
125 }
126 bool operator!=(const Path& rhs) const noexcept { return !(*this == rhs); }
127
138 bool operator<(const Path& rhs) const noexcept;
139
140 // Static Methods
141 static Path line(const Point& p1, const Point& p2,
142 const Angle& angle = Angle::deg0()) noexcept;
143 static Path circle(const PositiveLength& diameter) noexcept;
144 static Path obround(const PositiveLength& width,
145 const PositiveLength& height) noexcept;
146 static Path obround(const Point& p1, const Point& p2,
147 const PositiveLength& width) noexcept;
148 static Path arcObround(const Point& p1, const Point& p2, const Angle& angle,
149 const PositiveLength& width) noexcept;
150 static Path rect(const Point& p1, const Point& p2) noexcept;
151 static Path centeredRect(
152 const PositiveLength& width, const PositiveLength& height,
153 const UnsignedLength& cornerRadius = UnsignedLength(0)) noexcept;
154 static Path octagon(
155 const PositiveLength& width, const PositiveLength& height,
156 const UnsignedLength& cornerRadius = UnsignedLength(0)) noexcept;
157 static Path flatArc(const Point& p1, const Point& p2, const Angle& angle,
158 const PositiveLength& maxTolerance) noexcept;
159
170 static QPainterPath toQPainterPathPx(const QVector<Path>& paths,
171 bool area) noexcept;
172
173private: // Methods
174 void invalidatePainterPath() const noexcept {
175 mPainterPathPx = QPainterPath();
176 }
177
178private: // Data
179 QVector<Vertex> mVertices;
180 mutable QPainterPath mPainterPathPx; // cached path for #toQPainterPathPx()
181};
182
183/*******************************************************************************
184 * Non-Member Functions
185 ******************************************************************************/
186
187inline QtCompat::Hash qHash(const Path& key, QtCompat::Hash seed = 0) noexcept {
188 return qHashRange(key.getVertices().begin(), key.getVertices().end(), seed);
189}
190
191/*******************************************************************************
192 * Class NonEmptyPath
193 ******************************************************************************/
194
196 template <typename Value, typename Predicate>
197 static constexpr auto verify(Value&& val, const Predicate& p) ->
198 typename std::decay<Value>::type {
199 return p(val)
200 ? std::forward<Value>(val)
201 : (throw RuntimeError(__FILE__, __LINE__,
202 Path::tr("Path doesn't contain vertices!")),
203 std::forward<Value>(val));
204 }
205};
206
208 bool operator()(const Path& p) const noexcept {
209 return p.getVertices().count() > 0;
210 }
211};
212
220using NonEmptyPath = type_safe::constrained_type<Path, NonEmptyPathConstraint,
222
224 QtCompat::Hash seed = 0) noexcept {
225 return ::qHash(*key, seed);
226}
227
228inline NonEmptyPath makeNonEmptyPath(const Point& pos) noexcept {
229 return NonEmptyPath(Path({Vertex(pos)}));
230}
231
232/*******************************************************************************
233 * Class StraightAreaPath
234 ******************************************************************************/
235
237 template <typename Value, typename Predicate>
238 static constexpr auto verify(Value&& val, const Predicate& p) ->
239 typename std::decay<Value>::type {
240 return p(val) ? std::forward<Value>(val)
241 : (throw RuntimeError(
242 __FILE__, __LINE__,
243 Path::tr("Path is not fillable or contains arcs!")),
244 std::forward<Value>(val));
245 }
246};
247
249 bool operator()(const Path& p) const noexcept {
250 return (p.getVertices().count() >= 4) && p.isClosed() && (!p.isCurved());
251 }
252};
253
263 type_safe::constrained_type<Path, StraightAreaPathConstraint,
265
267 QtCompat::Hash seed = 0) noexcept {
268 return ::qHash(*key, seed);
269}
270
271/*******************************************************************************
272 * End of File
273 ******************************************************************************/
274
275} // namespace librepcb
276
277Q_DECLARE_METATYPE(librepcb::Path)
278
279#endif
The Angle class is used to represent an angle (for example 12.75 degrees)
Definition: angle.h:78
static Angle deg0() noexcept
0 degrees
Definition: angle.h:351
The Path class represents a list of vertices connected by straight lines or circular arc segments.
Definition: path.h:58
Path reversed() const noexcept
Definition: path.cpp:252
Path toOpenPath() const noexcept
Definition: path.cpp:130
QVector< Path > toOutlineStrokes(const PositiveLength &width) const noexcept
Definition: path.cpp:136
Path & mirror(Qt::Orientation orientation, const Point &center=Point(0, 0)) noexcept
Definition: path.cpp:226
Path flattenedArcs(const PositiveLength &maxTolerance) const noexcept
Definition: path.cpp:276
const QPainterPath & toQPainterPathPx() const noexcept
Definition: path.cpp:156
static Path octagon(const PositiveLength &width, const PositiveLength &height, const UnsignedLength &cornerRadius=UnsignedLength(0)) noexcept
Definition: path.cpp:485
const QVector< Vertex > & getVertices() const noexcept
Definition: path.h:77
Path & reverse() noexcept
Definition: path.cpp:240
QVector< Vertex > & getVertices() noexcept
Definition: path.h:73
bool operator<(const Path &rhs) const noexcept
The "<" operator to compare two librepcb::Path objects.
Definition: path.cpp:353
QVector< Vertex > mVertices
Definition: path.h:179
static Path rect(const Point &p1, const Point &p2) noexcept
Definition: path.cpp:445
Path translated(const Point &offset) const noexcept
Definition: path.cpp:198
Path & flattenArcs(const PositiveLength &maxTolerance) noexcept
Definition: path.cpp:256
Path mirrored(Qt::Orientation orientation, const Point &center=Point(0, 0)) const noexcept
Definition: path.cpp:235
static Path arcObround(const Point &p1, const Point &p2, const Angle &angle, const PositiveLength &width) noexcept
Definition: path.cpp:410
bool clean() noexcept
Definition: path.cpp:303
bool close() noexcept
Definition: path.cpp:316
Path & mapToGrid(const PositiveLength &gridInterval) noexcept
Definition: path.cpp:202
static Path centeredRect(const PositiveLength &width, const PositiveLength &height, const UnsignedLength &cornerRadius=UnsignedLength(0)) noexcept
Definition: path.cpp:455
Path cleaned() const noexcept
Definition: path.cpp:118
void insertVertex(int index, const Vertex &vertex) noexcept
Definition: path.cpp:293
static Path flatArc(const Point &p1, const Point &p2, const Angle &angle, const PositiveLength &maxTolerance) noexcept
Definition: path.cpp:536
Point calcNearestPointBetweenVertices(const Point &p) const noexcept
Definition: path.cpp:101
Path mappedToGrid(const PositiveLength &gridInterval) const noexcept
Definition: path.cpp:210
Path() noexcept
Definition: path.h:63
static Path circle(const PositiveLength &diameter) noexcept
Definition: path.cpp:371
Path(const QVector< Vertex > &vertices) noexcept
Definition: path.h:65
static Path line(const Point &p1, const Point &p2, const Angle &angle=Angle::deg0()) noexcept
Definition: path.cpp:367
void invalidatePainterPath() const noexcept
Definition: path.h:174
qreal calcAreaOfStraightSegments() const noexcept
Definition: path.cpp:86
Path & rotate(const Angle &angle, const Point &center=Point(0, 0)) noexcept
Definition: path.cpp:214
QPainterPath mPainterPathPx
Definition: path.h:180
static Path obround(const PositiveLength &width, const PositiveLength &height) noexcept
Definition: path.cpp:375
void addVertex(const Vertex &vertex) noexcept
Definition: path.cpp:284
~Path() noexcept
Definition: path.h:68
Path rotated(const Angle &angle, const Point &center=Point(0, 0)) const noexcept
Definition: path.cpp:222
void serialize(SExpression &root) const
Serialize into librepcb::SExpression node.
Definition: path.cpp:335
Path & translate(const Point &offset) noexcept
Definition: path.cpp:190
bool isCurved() const noexcept
Definition: path.cpp:63
UnsignedLength getTotalStraightLength() const noexcept
Definition: path.cpp:73
bool open() noexcept
Definition: path.cpp:326
bool operator!=(const Path &rhs) const noexcept
Definition: path.h:126
bool isClosed() const noexcept
Definition: path.cpp:55
Path toClosedPath() const noexcept
Definition: path.cpp:124
The Point class is used to represent a point/coordinate/vector, for example (1.2mm; 5....
Definition: point.h:79
uint Hash
Return type of Qt's qHash() function.
Definition: qtcompat.h:58
The RuntimeError class.
Definition: exceptions.h:218
The SExpression class.
Definition: sexpression.h:69
The Vertex class.
Definition: vertex.h:44
Definition: occmodel.cpp:77
type_safe::constrained_type< Length, PositiveLengthConstraint, PositiveLengthVerifier > PositiveLength
Definition: length.h:812
type_safe::constrained_type< Path, NonEmptyPathConstraint, NonEmptyPathVerifier > NonEmptyPath
Definition: path.h:221
QtCompat::Hash qHash(const AttributeKey &key, QtCompat::Hash seed=0) noexcept
Definition: attributekey.h:119
type_safe::constrained_type< Length, UnsignedLengthConstraint, UnsignedLengthVerifier > UnsignedLength
Definition: length.h:696
type_safe::constrained_type< Path, StraightAreaPathConstraint, StraightAreaPathVerifier > StraightAreaPath
Definition: path.h:264
NonEmptyPath makeNonEmptyPath(const Point &pos) noexcept
Definition: path.h:228
QtCompat::Hash qHash(const StraightAreaPath &key, QtCompat::Hash seed=0) noexcept
Definition: path.h:266
Definition: path.h:207
bool operator()(const Path &p) const noexcept
Definition: path.h:208
Definition: path.h:195
static constexpr auto verify(Value &&val, const Predicate &p) -> typename std::decay< Value >::type
Definition: path.h:197
bool operator()(const Path &p) const noexcept
Definition: path.h:249
static constexpr auto verify(Value &&val, const Predicate &p) -> typename std::decay< Value >::type
Definition: path.h:238