LibrePCB Developers Documentation
Loading...
Searching...
No Matches
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 "vertex.h"
28
29#include <type_safe/constrained_type.hpp>
30
31#include <QtCore>
32#include <QtGui>
33
34/*******************************************************************************
35 * Namespace / Forward Declarations
36 ******************************************************************************/
37namespace librepcb {
38
39/*******************************************************************************
40 * Class Path
41 ******************************************************************************/
42
57class Path final {
58 Q_DECLARE_TR_FUNCTIONS(Path)
59
60public:
61 // Constructors / Destructor
62 Path() noexcept : mVertices(), mPainterPathPx() {}
63 Path(const Path& other) noexcept;
64 explicit Path(const QVector<Vertex>& vertices) noexcept
65 : mVertices(vertices) {}
66 explicit Path(const SExpression& node);
67 ~Path() noexcept {}
68
69 // Getters
70 bool isClosed() const noexcept;
71 bool isCurved() const noexcept;
72 bool isZeroLength() 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 donut(const PositiveLength& outerDiameter,
145 const PositiveLength& innerDiameter) noexcept;
146 static Path obround(const PositiveLength& width,
147 const PositiveLength& height) noexcept;
148 static Path obround(const Point& p1, const Point& p2,
149 const PositiveLength& width) noexcept;
150 static Path arcObround(const Point& p1, const Point& p2, const Angle& angle,
151 const PositiveLength& width) noexcept;
152 static Path rect(const Point& p1, const Point& p2) noexcept;
153 static Path centeredRect(
154 const PositiveLength& width, const PositiveLength& height,
155 const UnsignedLength& cornerRadius = UnsignedLength(0)) noexcept;
156 static Path chamferedRect(const PositiveLength& width,
157 const PositiveLength& height,
158 const UnsignedLength& chamferSize, bool topLeft,
159 bool topRight, bool bottomLeft,
160 bool bottomRight) noexcept;
161 static Path trapezoid(const PositiveLength& width,
162 const PositiveLength& height, const Length& dw,
163 const Length dh) noexcept;
164 static Path octagon(
165 const PositiveLength& width, const PositiveLength& height,
166 const UnsignedLength& cornerRadius = UnsignedLength(0)) noexcept;
167 static Path flatArc(const Point& p1, const Point& p2, const Angle& angle,
168 const PositiveLength& maxTolerance) noexcept;
169
180 static QPainterPath toQPainterPathPx(const QVector<Path>& paths,
181 bool area) noexcept;
182
183private: // Methods
184 void invalidatePainterPath() const noexcept {
185 mPainterPathPx = QPainterPath();
186 }
187
188private: // Data
189 QVector<Vertex> mVertices;
190 mutable QPainterPath mPainterPathPx; // cached path for #toQPainterPathPx()
191};
192
193/*******************************************************************************
194 * Non-Member Functions
195 ******************************************************************************/
196
197inline std::size_t qHash(const Path& key, std::size_t seed = 0) noexcept {
198 return qHashRange(key.getVertices().begin(), key.getVertices().end(), seed);
199}
200
201/*******************************************************************************
202 * Class NonEmptyPath
203 ******************************************************************************/
204
206 template <typename Value, typename Predicate>
207 static constexpr auto verify(Value&& val, const Predicate& p) ->
208 typename std::decay<Value>::type {
209 return p(val)
210 ? std::forward<Value>(val)
211 : (throw RuntimeError(__FILE__, __LINE__,
212 Path::tr("Path doesn't contain vertices!")),
213 std::forward<Value>(val));
214 }
215};
216
218 bool operator()(const Path& p) const noexcept {
219 return p.getVertices().count() > 0;
220 }
221};
222
230using NonEmptyPath = type_safe::constrained_type<Path, NonEmptyPathConstraint,
232
233inline std::size_t qHash(const NonEmptyPath& key,
234 std::size_t seed = 0) noexcept {
235 return ::qHash(*key, seed);
236}
237
238inline NonEmptyPath makeNonEmptyPath(const Point& pos) noexcept {
239 return NonEmptyPath(Path({Vertex(pos)}));
240}
241
242/*******************************************************************************
243 * Class StraightAreaPath
244 ******************************************************************************/
245
247 template <typename Value, typename Predicate>
248 static constexpr auto verify(Value&& val, const Predicate& p) ->
249 typename std::decay<Value>::type {
250 return p(val) ? std::forward<Value>(val)
251 : (throw RuntimeError(
252 __FILE__, __LINE__,
253 Path::tr("Path is not fillable or contains arcs!")),
254 std::forward<Value>(val));
255 }
256};
257
259 bool operator()(const Path& p) const noexcept {
260 return (p.getVertices().count() >= 4) && p.isClosed() && (!p.isCurved());
261 }
262};
263
273 type_safe::constrained_type<Path, StraightAreaPathConstraint,
275
276inline std::size_t qHash(const StraightAreaPath& key,
277 std::size_t seed = 0) noexcept {
278 return ::qHash(*key, seed);
279}
280
281/*******************************************************************************
282 * End of File
283 ******************************************************************************/
284
285} // namespace librepcb
286
287Q_DECLARE_METATYPE(librepcb::Path)
288
289#endif
The Angle class is used to represent an angle (for example 12.75 degrees)
Definition angle.h:76
static Angle deg0() noexcept
0 degrees
Definition angle.h:349
The Length class is used to represent a length (for example 12.75 millimeters)
Definition length.h:82
The Path class represents a list of vertices connected by straight lines or circular arc segments.
Definition path.h:57
Path reversed() const noexcept
Definition path.cpp:265
Path toOpenPath() const noexcept
Definition path.cpp:143
static Path trapezoid(const PositiveLength &width, const PositiveLength &height, const Length &dw, const Length dh) noexcept
Definition path.cpp:547
QVector< Path > toOutlineStrokes(const PositiveLength &width) const noexcept
Definition path.cpp:149
Path & mirror(Qt::Orientation orientation, const Point &center=Point(0, 0)) noexcept
Definition path.cpp:239
Path flattenedArcs(const PositiveLength &maxTolerance) const noexcept
Definition path.cpp:289
const QPainterPath & toQPainterPathPx() const noexcept
Definition path.cpp:169
static Path octagon(const PositiveLength &width, const PositiveLength &height, const UnsignedLength &cornerRadius=UnsignedLength(0)) noexcept
Definition path.cpp:567
const QVector< Vertex > & getVertices() const noexcept
Definition path.h:77
Path & reverse() noexcept
Definition path.cpp:253
static Path donut(const PositiveLength &outerDiameter, const PositiveLength &innerDiameter) noexcept
Definition path.cpp:382
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:366
QVector< Vertex > mVertices
Definition path.h:189
static Path rect(const Point &p1, const Point &p2) noexcept
Definition path.cpp:469
Path translated(const Point &offset) const noexcept
Definition path.cpp:211
Path & flattenArcs(const PositiveLength &maxTolerance) noexcept
Definition path.cpp:269
Path mirrored(Qt::Orientation orientation, const Point &center=Point(0, 0)) const noexcept
Definition path.cpp:248
static Path arcObround(const Point &p1, const Point &p2, const Angle &angle, const PositiveLength &width) noexcept
Definition path.cpp:434
bool clean() noexcept
Definition path.cpp:316
bool close() noexcept
Definition path.cpp:329
Path & mapToGrid(const PositiveLength &gridInterval) noexcept
Definition path.cpp:215
static Path chamferedRect(const PositiveLength &width, const PositiveLength &height, const UnsignedLength &chamferSize, bool topLeft, bool topRight, bool bottomLeft, bool bottomRight) noexcept
Definition path.cpp:509
static Path centeredRect(const PositiveLength &width, const PositiveLength &height, const UnsignedLength &cornerRadius=UnsignedLength(0)) noexcept
Definition path.cpp:479
Path cleaned() const noexcept
Definition path.cpp:131
void insertVertex(int index, const Vertex &vertex) noexcept
Definition path.cpp:306
static Path flatArc(const Point &p1, const Point &p2, const Angle &angle, const PositiveLength &maxTolerance) noexcept
Definition path.cpp:618
Point calcNearestPointBetweenVertices(const Point &p) const noexcept
Definition path.cpp:114
bool isZeroLength() const noexcept
Definition path.cpp:73
Path mappedToGrid(const PositiveLength &gridInterval) const noexcept
Definition path.cpp:223
Path() noexcept
Definition path.h:62
static Path circle(const PositiveLength &diameter) noexcept
Definition path.cpp:378
Path(const QVector< Vertex > &vertices) noexcept
Definition path.h:64
static Path line(const Point &p1, const Point &p2, const Angle &angle=Angle::deg0()) noexcept
Definition path.cpp:374
void invalidatePainterPath() const noexcept
Definition path.h:184
qreal calcAreaOfStraightSegments() const noexcept
Definition path.cpp:99
Path & rotate(const Angle &angle, const Point &center=Point(0, 0)) noexcept
Definition path.cpp:227
QPainterPath mPainterPathPx
Definition path.h:190
static Path obround(const PositiveLength &width, const PositiveLength &height) noexcept
Definition path.cpp:399
void addVertex(const Vertex &vertex) noexcept
Definition path.cpp:297
~Path() noexcept
Definition path.h:67
Path rotated(const Angle &angle, const Point &center=Point(0, 0)) const noexcept
Definition path.cpp:235
void serialize(SExpression &root) const
Serialize into librepcb::SExpression node.
Definition path.cpp:348
Path & translate(const Point &offset) noexcept
Definition path.cpp:203
bool isCurved() const noexcept
Definition path.cpp:63
UnsignedLength getTotalStraightLength() const noexcept
Definition path.cpp:86
bool open() noexcept
Definition path.cpp:339
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:137
The Point class is used to represent a point/coordinate/vector, for example (1.2mm; 5....
Definition point.h:78
The RuntimeError class.
Definition exceptions.h:218
The SExpression class.
Definition sexpression.h:69
The Vertex class.
Definition vertex.h:43
Definition occmodel.cpp:76
std::size_t qHash(const AttributeKey &key, std::size_t seed=0) noexcept
Definition attributekey.h:118
type_safe::constrained_type< Length, PositiveLengthConstraint, PositiveLengthVerifier > PositiveLength
Definition length.h:810
type_safe::constrained_type< Path, NonEmptyPathConstraint, NonEmptyPathVerifier > NonEmptyPath
Definition path.h:231
type_safe::constrained_type< Length, UnsignedLengthConstraint, UnsignedLengthVerifier > UnsignedLength
Definition length.h:694
type_safe::constrained_type< Path, StraightAreaPathConstraint, StraightAreaPathVerifier > StraightAreaPath
Definition path.h:274
NonEmptyPath makeNonEmptyPath(const Point &pos) noexcept
Definition path.h:238
bool operator()(const Path &p) const noexcept
Definition path.h:218
Definition path.h:205
static constexpr auto verify(Value &&val, const Predicate &p) -> typename std::decay< Value >::type
Definition path.h:207
bool operator()(const Path &p) const noexcept
Definition path.h:259
static constexpr auto verify(Value &&val, const Predicate &p) -> typename std::decay< Value >::type
Definition path.h:248