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  const QPainterPath& toQPainterPathPx(bool close = false) const noexcept;
71 
72  // Transformations
73  Path& translate(const Point& offset) noexcept;
74  Path translated(const Point& offset) const noexcept;
75  Path& rotate(const Angle& angle, const Point& center = Point(0, 0)) noexcept;
76  Path rotated(const Angle& angle, const Point& center = Point(0, 0)) const
77  noexcept;
78  Path& mirror(Qt::Orientation orientation,
79  const Point& center = Point(0, 0)) noexcept;
80  Path mirrored(Qt::Orientation orientation,
81  const Point& center = Point(0, 0)) const noexcept;
82 
83  // General Methods
84  void addVertex(const Vertex& vertex) noexcept;
85  void addVertex(const Point& pos, const Angle& angle = Angle::deg0()) noexcept;
86  void insertVertex(int index, const Vertex& vertex) noexcept;
87  void insertVertex(int index, const Point& pos,
88  const Angle& angle = Angle::deg0()) noexcept;
89  bool close() noexcept;
90 
92  void serialize(SExpression& root) const override;
93 
94  // Operator Overloadings
95  bool operator==(const Path& rhs) const noexcept {
96  return mVertices == rhs.mVertices;
97  }
98  bool operator!=(const Path& rhs) const noexcept { return !(*this == rhs); }
99  Path& operator=(const Path& rhs) noexcept;
100 
101  // Static Methods
102  static Path line(const Point& p1, const Point& p2,
103  const Angle& angle = Angle::deg0()) noexcept;
104  static Path circle(const PositiveLength& diameter) noexcept;
105  static Path obround(const PositiveLength& width,
106  const PositiveLength& height) noexcept;
107  static Path obround(const Point& p1, const Point& p2,
108  const PositiveLength& width) noexcept;
109  static Path rect(const Point& p1, const Point& p2) noexcept;
110  static Path centeredRect(const PositiveLength& width,
111  const PositiveLength& height) noexcept;
112  static Path octagon(const PositiveLength& width,
113  const PositiveLength& height) noexcept;
114  static Path flatArc(const Point& p1, const Point& p2, const Angle& angle,
115  const PositiveLength& maxTolerance) noexcept;
116  static QPainterPath toQPainterPathPx(const QVector<Path>& paths) noexcept;
117 
118 private: // Methods
119  void invalidatePainterPath() const noexcept {
120  mPainterPathPx = QPainterPath();
121  }
122 
123 private: // Data
124  QVector<Vertex> mVertices;
125  mutable QPainterPath mPainterPathPx; // cached path for #toQPainterPathPx()
126 };
127 
128 /*******************************************************************************
129  * Non-Member Functions
130  ******************************************************************************/
131 
132 inline uint qHash(const Path& key, uint seed = 0) noexcept {
133 #if (QT_VERSION >= QT_VERSION_CHECK(5, 5, 0))
134  return qHashRange(key.getVertices().begin(), key.getVertices().end(), seed);
135 #else
136  uint hash = seed;
137  foreach (const Vertex& v, key.getVertices()) {
138  // from
139  // https://code.woboq.org/qt5/qtbase/src/corelib/tools/qhashfunctions.h.html
140  hash += seed ^ (qHash(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2));
141  }
142  return hash;
143 #endif
144 }
145 
146 /*******************************************************************************
147  * End of File
148  ******************************************************************************/
149 
150 } // namespace librepcb
151 
152 #endif // LIBREPCB_PATH_H
void invalidatePainterPath() const noexcept
Definition: path.h:119
The Vertex class.
Definition: vertex.h:43
Path & mirror(Qt::Orientation orientation, const Point &center=Point(0, 0)) noexcept
Definition: path.cpp:122
const QPainterPath & toQPainterPathPx(bool close=false) const noexcept
Definition: path.cpp:60
Path() noexcept
Definition: path.h:56
QVector< Vertex > mVertices
Definition: path.h:124
Path rotated(const Angle &angle, const Point &center=Point(0, 0)) const noexcept
Definition: path.cpp:118
The Angle class is used to represent an angle (for example 12.75 degrees)
Definition: angle.h:81
static Path line(const Point &p1, const Point &p2, const Angle &angle=Angle::deg0()) noexcept
Definition: path.cpp:187
static Path centeredRect(const PositiveLength &width, const PositiveLength &height) noexcept
Definition: path.cpp:240
The SerializableObject class is the base class for all classes which need to be serializable/deserial...
Definition: serializableobject.h:46
Path mirrored(Qt::Orientation orientation, const Point &center=Point(0, 0)) const noexcept
Definition: path.cpp:131
bool isClosed() const noexcept
Definition: path.cpp:52
bool operator!=(const Path &rhs) const noexcept
Definition: path.h:98
static Path rect(const Point &p1, const Point &p2) noexcept
Definition: path.cpp:230
static Path obround(const PositiveLength &width, const PositiveLength &height) noexcept
Definition: path.cpp:195
The Point class is used to represent a point/coordinate/vector, for example (1.2mm; 5...
Definition: point.h:81
static Angle deg0() noexcept
0 degrees
Definition: angle.h:303
bool close() noexcept
Definition: path.cpp:159
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:169
static Path circle(const PositiveLength &diameter) noexcept
Definition: path.cpp:191
Path(const QVector< Vertex > &vertices) noexcept
Definition: path.h:58
Path translated(const Point &offset) const noexcept
Definition: path.cpp:106
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:271
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:125
Path & operator=(const Path &rhs) noexcept
Definition: path.cpp:177
type_safe::constrained_type< Length, PositiveLengthConstraint, PositiveLengthVerifier > PositiveLength
Definition: length.h:783
Path & translate(const Point &offset) noexcept
Definition: path.cpp:98
Path & rotate(const Angle &angle, const Point &center=Point(0, 0)) noexcept
Definition: path.cpp:110
void insertVertex(int index, const Vertex &vertex) noexcept
Definition: path.cpp:149
static Path octagon(const PositiveLength &width, const PositiveLength &height) noexcept
Definition: path.cpp:253
The SExpression class.
Definition: sexpression.h:60
uint qHash(const AttributeKey &key, uint seed=0) noexcept
Definition: attributekey.h:119
void addVertex(const Vertex &vertex) noexcept
Definition: path.cpp:140