LibrePCB Developers Documentation
Loading...
Searching...
No Matches
transform.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_TRANSFORM_H
21#define LIBREPCB_CORE_TRANSFORM_H
22
23/*******************************************************************************
24 * Includes
25 ******************************************************************************/
26#include "../geometry/path.h"
27#include "../types/angle.h"
28#include "../types/point.h"
29
30#include <QtCore>
31
32/*******************************************************************************
33 * Namespace / Forward Declarations
34 ******************************************************************************/
35namespace librepcb {
36
37class Layer;
38
39/*******************************************************************************
40 * Class Transform
41 ******************************************************************************/
42
59class Transform {
60public:
61 // Constructors / Destructor
62
70 Transform(const Point& position = Point(0, 0),
71 const Angle& rotation = Angle(0), bool mirrored = false) noexcept
72 : mPosition(position), mRotation(rotation), mMirrored(mirrored) {}
73
82 template <typename T>
83 explicit Transform(const T& obj)
84 : mPosition(obj.getPosition()),
86 mMirrored(obj.getMirrored()) {}
87
93 Transform(const Transform& other) noexcept
94 : mPosition(other.mPosition),
95 mRotation(other.mRotation),
96 mMirrored(other.mMirrored) {}
97
101 ~Transform() noexcept {}
102
103 // Getters
104 const Point& getPosition() const noexcept { return mPosition; }
105 const Angle& getRotation() const noexcept { return mRotation; }
106 bool getMirrored() const noexcept { return mMirrored; }
107
108 // Setters
109 void setPosition(const Point& position) noexcept { mPosition = position; }
110 void setRotation(const Angle& rotation) noexcept { mRotation = rotation; }
111 void setMirrored(bool mirrored) noexcept { mMirrored = mirrored; }
112
113 // General Methods
114
122 bool map(bool mirror) const noexcept;
123
134 Angle mapMirrorable(const Angle& angle) const noexcept;
135
146 Angle mapNonMirrorable(const Angle& angle) const noexcept;
147
156 Point map(const Point& point) const noexcept;
157
166 Path map(const Path& path) const noexcept;
167
176 NonEmptyPath map(const NonEmptyPath& path) const noexcept;
177
185 const Layer& map(const Layer& layer) const noexcept;
186
196 template <typename T>
197 T map(const T& container) const noexcept {
198 T copy = container;
199 for (auto& item : copy) {
200 item = map(item);
201 }
202 return copy;
203 }
204
214 template <typename T>
215 T mapPx(const T& obj) const noexcept {
216 QTransform t;
217 t.translate(mPosition.toPxQPointF().x(), mPosition.toPxQPointF().y());
218 t.rotate(-mRotation.toDeg());
219 if (mMirrored) {
220 t.scale(-1, 1);
221 }
222 return t.map(obj);
223 }
224
225 // Operator Overloadings
226 bool operator==(const Transform& rhs) const noexcept {
227 return (mPosition == rhs.mPosition) && (mRotation == rhs.mRotation) &&
228 (mMirrored == rhs.mMirrored);
229 }
230 bool operator!=(const Transform& rhs) const noexcept {
231 return !(*this == rhs);
232 }
233 Transform& operator=(const Transform& rhs) noexcept {
234 mPosition = rhs.mPosition;
235 mRotation = rhs.mRotation;
236 mMirrored = rhs.mMirrored;
237 return *this;
238 }
239
240private: // Data
244};
245
246/*******************************************************************************
247 * End of File
248 ******************************************************************************/
249
250} // namespace librepcb
251
252#endif
The Angle class is used to represent an angle (for example 12.75 degrees)
Definition angle.h:76
qreal toDeg() const noexcept
Get the Angle in degrees.
Definition angle.h:179
The Layer class provides all supported geometry layers.
Definition layer.h:42
The Path class represents a list of vertices connected by straight lines or circular arc segments.
Definition path.h:57
The Point class is used to represent a point/coordinate/vector, for example (1.2mm; 5....
Definition point.h:78
QPointF toPxQPointF() const noexcept
Get the point as a QPointF object in pixels (for QGraphics* objects)
Definition point.h:276
Helper class to perform coordinate transformation with various types.
Definition transform.h:59
bool getMirrored() const noexcept
Definition transform.h:106
const Angle & getRotation() const noexcept
Definition transform.h:105
Transform & operator=(const Transform &rhs) noexcept
Definition transform.h:233
Transform(const Point &position=Point(0, 0), const Angle &rotation=Angle(0), bool mirrored=false) noexcept
(Default) construdtor
Definition transform.h:70
Transform(const Transform &other) noexcept
Copy constructor.
Definition transform.h:93
bool operator!=(const Transform &rhs) const noexcept
Definition transform.h:230
Angle mapMirrorable(const Angle &angle) const noexcept
Map a given angle to the transformed coordinate system.
Definition transform.cpp:40
bool operator==(const Transform &rhs) const noexcept
Definition transform.h:226
bool map(bool mirror) const noexcept
Map a given mirror state to the transformed coordinate system.
Definition transform.cpp:36
T mapPx(const T &obj) const noexcept
Map a given Qt object in pixels to the transformed coordinate system.
Definition transform.h:215
Angle mapNonMirrorable(const Angle &angle) const noexcept
Map a given angle to the transformed coordinate system.
Definition transform.cpp:44
void setMirrored(bool mirrored) noexcept
Definition transform.h:111
Angle mRotation
Definition transform.h:242
const Point & getPosition() const noexcept
Definition transform.h:104
void setRotation(const Angle &rotation) noexcept
Definition transform.h:110
~Transform() noexcept
Destructor.
Definition transform.h:101
T map(const T &container) const noexcept
Map all items of a container to the transformed coordinate system.
Definition transform.h:197
bool mMirrored
Definition transform.h:243
Point mPosition
Definition transform.h:241
void setPosition(const Point &position) noexcept
Definition transform.h:109
Transform(const T &obj)
Constructor to copy the transformation of an object.
Definition transform.h:83
Definition occmodel.cpp:76
type_safe::constrained_type< Path, NonEmptyPathConstraint, NonEmptyPathVerifier > NonEmptyPath
Definition path.h:231