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_PATH_H
21 #define LIBREPCB_PATH_H
22 
23 /*******************************************************************************
24  * Includes
25  ******************************************************************************/
26 #include "vertex.h"
27 
28 #include <QtCore>
29 
30 /*******************************************************************************
31  * Namespace / Forward Declarations
32  ******************************************************************************/
33 namespace librepcb {
34 
35 /*******************************************************************************
36  * Class Path
37  ******************************************************************************/
38 
53 class Path final : public SerializableObject {
54 public:
55  // Constructors / Destructor
56  Path() noexcept : mVertices(), mPainterPathPx() {}
57  Path(const Path& other) noexcept;
58  explicit Path(const QVector<Vertex>& vertices) noexcept
59  : mVertices(vertices) {}
60  Path(const SExpression& node, const Version& fileFormat);
61  ~Path() noexcept {}
62 
63  // Getters
64  bool isClosed() const noexcept;
65  QVector<Vertex>& getVertices() noexcept {
67  return mVertices;
68  }
69  const QVector<Vertex>& getVertices() const noexcept { return mVertices; }
70  UnsignedLength getTotalStraightLength() const noexcept;
71  Path toClosedPath() const noexcept;
72  QVector<Path> toOutlineStrokes(const PositiveLength& width) const noexcept;
73  const QPainterPath& toQPainterPathPx() const noexcept;
74 
75  // Transformations
76  Path& translate(const Point& offset) noexcept;
77  Path translated(const Point& offset) const noexcept;
78  Path& mapToGrid(const PositiveLength& gridInterval) noexcept;
79  Path mappedToGrid(const PositiveLength& gridInterval) const noexcept;
80  Path& rotate(const Angle& angle, const Point& center = Point(0, 0)) noexcept;
81  Path rotated(const Angle& angle, const Point& center = Point(0, 0)) const
82  noexcept;
83  Path& mirror(Qt::Orientation orientation,
84  const Point& center = Point(0, 0)) noexcept;
85  Path mirrored(Qt::Orientation orientation,
86  const Point& center = Point(0, 0)) const noexcept;
87  Path& reverse() noexcept;
88  Path reversed() const noexcept;
89 
90  // General Methods
91  void addVertex(const Vertex& vertex) noexcept;
92  void addVertex(const Point& pos, const Angle& angle = Angle::deg0()) noexcept;
93  void insertVertex(int index, const Vertex& vertex) noexcept;
94  void insertVertex(int index, const Point& pos,
95  const Angle& angle = Angle::deg0()) noexcept;
96  bool close() noexcept;
97 
99  void serialize(SExpression& root) const override;
100 
101  // Operator Overloadings
102  bool operator==(const Path& rhs) const noexcept {
103  return mVertices == rhs.mVertices;
104  }
105  bool operator!=(const Path& rhs) const noexcept { return !(*this == rhs); }
106  Path& operator=(const Path& rhs) noexcept;
107 
108  // Static Methods
109  static Path line(const Point& p1, const Point& p2,
110  const Angle& angle = Angle::deg0()) noexcept;
111  static Path circle(const PositiveLength& diameter) noexcept;
112  static Path obround(const PositiveLength& width,
113  const PositiveLength& height) noexcept;
114  static Path obround(const Point& p1, const Point& p2,
115  const PositiveLength& width) noexcept;
116  static Path arcObround(const Point& p1, const Point& p2, const Angle& angle,
117  const PositiveLength& width) noexcept;
118  static Path rect(const Point& p1, const Point& p2) noexcept;
119  static Path centeredRect(const PositiveLength& width,
120  const PositiveLength& height) noexcept;
121  static Path octagon(const PositiveLength& width,
122  const PositiveLength& height) noexcept;
123  static Path flatArc(const Point& p1, const Point& p2, const Angle& angle,
124  const PositiveLength& maxTolerance) noexcept;
125 
136  static QPainterPath toQPainterPathPx(const QVector<Path>& paths,
137  bool area) noexcept;
138 
139 private: // Methods
140  void invalidatePainterPath() const noexcept {
141  mPainterPathPx = QPainterPath();
142  }
143 
144 private: // Data
145  QVector<Vertex> mVertices;
146  mutable QPainterPath mPainterPathPx; // cached path for #toQPainterPathPx()
147 };
148 
149 /*******************************************************************************
150  * Non-Member Functions
151  ******************************************************************************/
152 
153 inline uint qHash(const Path& key, uint seed = 0) noexcept {
154  return qHashRange(key.getVertices().begin(), key.getVertices().end(), seed);
155 }
156 
157 /*******************************************************************************
158  * End of File
159  ******************************************************************************/
160 
161 } // namespace librepcb
162 
163 #endif // LIBREPCB_PATH_H
Path toClosedPath() const noexcept
Definition: path.cpp:73
void invalidatePainterPath() const noexcept
Definition: path.h:140
The Version class represents a version number in the format "1.42.7".
Definition: version.h:60
The Vertex class.
Definition: vertex.h:43
Path & mirror(Qt::Orientation orientation, const Point &center=Point(0, 0)) noexcept
Definition: path.cpp:166
Definition: airwiresbuilder.cpp:32
Path() noexcept
Definition: path.h:56
QVector< Vertex > mVertices
Definition: path.h:145
Path rotated(const Angle &angle, const Point &center=Point(0, 0)) const noexcept
Definition: path.cpp:162
The Angle class is used to represent an angle (for example 12.75 degrees)
Definition: angle.h:78
static Path line(const Point &p1, const Point &p2, const Angle &angle=Angle::deg0()) noexcept
Definition: path.cpp:247
bool operator==(const Path &rhs) const noexcept
Definition: path.h:102
static Path centeredRect(const PositiveLength &width, const PositiveLength &height) noexcept
Definition: path.cpp:331
The SerializableObject class is the base class for all classes which need to be serializable/deserial...
Definition: serializableobject.h:43
Path mirrored(Qt::Orientation orientation, const Point &center=Point(0, 0)) const noexcept
Definition: path.cpp:175
bool isClosed() const noexcept
Definition: path.cpp:52
bool operator!=(const Path &rhs) const noexcept
Definition: path.h:105
static Path rect(const Point &p1, const Point &p2) noexcept
Definition: path.cpp:321
static Path obround(const PositiveLength &width, const PositiveLength &height) noexcept
Definition: path.cpp:255
Path & reverse() noexcept
Definition: path.cpp:180
The Point class is used to represent a point/coordinate/vector, for example (1.2mm; 5...
Definition: point.h:78
static Angle deg0() noexcept
0 degrees
Definition: angle.h:354
static Path arcObround(const Point &p1, const Point &p2, const Angle &angle, const PositiveLength &width) noexcept
Definition: path.cpp:290
bool close() noexcept
Definition: path.cpp:219
const QVector< Vertex > & getVertices() const noexcept
Definition: path.h:69
void serialize(SExpression &root) const override
Serialize the object into an existing S-Expression node.
Definition: path.cpp:229
static Path circle(const PositiveLength &diameter) noexcept
Definition: path.cpp:251
Path(const QVector< Vertex > &vertices) noexcept
Definition: path.h:58
Path translated(const Point &offset) const noexcept
Definition: path.cpp:138
QVector< Vertex > & getVertices() noexcept
Definition: path.h:65
static Path flatArc(const Point &p1, const Point &p2, const Angle &angle, const PositiveLength &maxTolerance) noexcept
Definition: path.cpp:362
Path & mapToGrid(const PositiveLength &gridInterval) noexcept
Definition: path.cpp:142
QVector< Path > toOutlineStrokes(const PositiveLength &width) const noexcept
Definition: path.cpp:79
The Path class represents a list of vertices connected by straight lines or circular arc segments...
Definition: path.h:53
~Path() noexcept
Definition: path.h:61
QPainterPath mPainterPathPx
Definition: path.h:146
UnsignedLength getTotalStraightLength() const noexcept
Definition: path.cpp:60
Path & operator=(const Path &rhs) noexcept
Definition: path.cpp:237
type_safe::constrained_type< Length, PositiveLengthConstraint, PositiveLengthVerifier > PositiveLength
Definition: length.h:792
Path & translate(const Point &offset) noexcept
Definition: path.cpp:130
Path & rotate(const Angle &angle, const Point &center=Point(0, 0)) noexcept
Definition: path.cpp:154
void insertVertex(int index, const Vertex &vertex) noexcept
Definition: path.cpp:209
static Path octagon(const PositiveLength &width, const PositiveLength &height) noexcept
Definition: path.cpp:344
The SExpression class.
Definition: sexpression.h:72
Path reversed() const noexcept
Definition: path.cpp:192
uint qHash(const AttributeKey &key, uint seed=0) noexcept
Definition: attributekey.h:120
void addVertex(const Vertex &vertex) noexcept
Definition: path.cpp:200
const QPainterPath & toQPainterPathPx() const noexcept
Definition: path.cpp:95
type_safe::constrained_type< Length, UnsignedLengthConstraint, UnsignedLengthVerifier > UnsignedLength
Definition: length.h:672
Path mappedToGrid(const PositiveLength &gridInterval) const noexcept
Definition: path.cpp:150