LibrePCB Developers Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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  explicit Path(const SExpression& node);
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  Path toClosedPath() const noexcept;
71  QVector<Path> toOutlineStrokes(const PositiveLength& width) const noexcept;
72  const QPainterPath& toQPainterPathPx() const noexcept;
73 
74  // Transformations
75  Path& translate(const Point& offset) noexcept;
76  Path translated(const Point& offset) const noexcept;
77  Path& rotate(const Angle& angle, const Point& center = Point(0, 0)) noexcept;
78  Path rotated(const Angle& angle, const Point& center = Point(0, 0)) const
79  noexcept;
80  Path& mirror(Qt::Orientation orientation,
81  const Point& center = Point(0, 0)) noexcept;
82  Path mirrored(Qt::Orientation orientation,
83  const Point& center = Point(0, 0)) const noexcept;
84 
85  // General Methods
86  void addVertex(const Vertex& vertex) noexcept;
87  void addVertex(const Point& pos, const Angle& angle = Angle::deg0()) noexcept;
88  void insertVertex(int index, const Vertex& vertex) noexcept;
89  void insertVertex(int index, const Point& pos,
90  const Angle& angle = Angle::deg0()) noexcept;
91  bool close() noexcept;
92 
94  void serialize(SExpression& root) const override;
95 
96  // Operator Overloadings
97  bool operator==(const Path& rhs) const noexcept {
98  return mVertices == rhs.mVertices;
99  }
100  bool operator!=(const Path& rhs) const noexcept { return !(*this == rhs); }
101  Path& operator=(const Path& rhs) noexcept;
102 
103  // Static Methods
104  static Path line(const Point& p1, const Point& p2,
105  const Angle& angle = Angle::deg0()) noexcept;
106  static Path circle(const PositiveLength& diameter) noexcept;
107  static Path obround(const PositiveLength& width,
108  const PositiveLength& height) noexcept;
109  static Path obround(const Point& p1, const Point& p2,
110  const PositiveLength& width) noexcept;
111  static Path arcObround(const Point& p1, const Point& p2, const Angle& angle,
112  const PositiveLength& width) noexcept;
113  static Path rect(const Point& p1, const Point& p2) noexcept;
114  static Path centeredRect(const PositiveLength& width,
115  const PositiveLength& height) noexcept;
116  static Path octagon(const PositiveLength& width,
117  const PositiveLength& height) noexcept;
118  static Path flatArc(const Point& p1, const Point& p2, const Angle& angle,
119  const PositiveLength& maxTolerance) noexcept;
120 
131  static QPainterPath toQPainterPathPx(const QVector<Path>& paths,
132  bool area) noexcept;
133 
134 private: // Methods
135  void invalidatePainterPath() const noexcept {
136  mPainterPathPx = QPainterPath();
137  }
138 
139 private: // Data
140  QVector<Vertex> mVertices;
141  mutable QPainterPath mPainterPathPx; // cached path for #toQPainterPathPx()
142 };
143 
144 /*******************************************************************************
145  * Non-Member Functions
146  ******************************************************************************/
147 
148 inline uint qHash(const Path& key, uint seed = 0) noexcept {
149 #if (QT_VERSION >= QT_VERSION_CHECK(5, 5, 0))
150  return qHashRange(key.getVertices().begin(), key.getVertices().end(), seed);
151 #else
152  uint hash = seed;
153  foreach (const Vertex& v, key.getVertices()) {
154  // from
155  // https://code.woboq.org/qt5/qtbase/src/corelib/tools/qhashfunctions.h.html
156  hash += seed ^ (qHash(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2));
157  }
158  return hash;
159 #endif
160 }
161 
162 /*******************************************************************************
163  * End of File
164  ******************************************************************************/
165 
166 } // namespace librepcb
167 
168 #endif // LIBREPCB_PATH_H
Path toClosedPath() const noexcept
Definition: path.cpp:60
void invalidatePainterPath() const noexcept
Definition: path.h:135
The Vertex class.
Definition: vertex.h:43
Path & mirror(Qt::Orientation orientation, const Point &center=Point(0, 0)) noexcept
Definition: path.cpp:141
Path() noexcept
Definition: path.h:56
QVector< Vertex > mVertices
Definition: path.h:140
Path rotated(const Angle &angle, const Point &center=Point(0, 0)) const noexcept
Definition: path.cpp:137
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:206
static Path centeredRect(const PositiveLength &width, const PositiveLength &height) noexcept
Definition: path.cpp:290
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:150
bool isClosed() const noexcept
Definition: path.cpp:52
bool operator!=(const Path &rhs) const noexcept
Definition: path.h:100
static Path rect(const Point &p1, const Point &p2) noexcept
Definition: path.cpp:280
static Path obround(const PositiveLength &width, const PositiveLength &height) noexcept
Definition: path.cpp:214
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:300
static Path arcObround(const Point &p1, const Point &p2, const Angle &angle, const PositiveLength &width) noexcept
Definition: path.cpp:249
bool close() noexcept
Definition: path.cpp:178
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:188
static Path circle(const PositiveLength &diameter) noexcept
Definition: path.cpp:210
Path(const QVector< Vertex > &vertices) noexcept
Definition: path.h:58
Path translated(const Point &offset) const noexcept
Definition: path.cpp:125
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:321
QVector< Path > toOutlineStrokes(const PositiveLength &width) const noexcept
Definition: path.cpp:66
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:141
Path & operator=(const Path &rhs) noexcept
Definition: path.cpp:196
type_safe::constrained_type< Length, PositiveLengthConstraint, PositiveLengthVerifier > PositiveLength
Definition: length.h:780
Path & translate(const Point &offset) noexcept
Definition: path.cpp:117
Path & rotate(const Angle &angle, const Point &center=Point(0, 0)) noexcept
Definition: path.cpp:129
void insertVertex(int index, const Vertex &vertex) noexcept
Definition: path.cpp:168
static Path octagon(const PositiveLength &width, const PositiveLength &height) noexcept
Definition: path.cpp:303
The SExpression class.
Definition: sexpression.h:57
uint qHash(const AttributeKey &key, uint seed=0) noexcept
Definition: attributekey.h:119
void addVertex(const Vertex &vertex) noexcept
Definition: path.cpp:159
const QPainterPath & toQPainterPathPx() const noexcept
Definition: path.cpp:82