LibrePCB Developers Documentation
point.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_POINT_H
21#define LIBREPCB_CORE_POINT_H
22
23/*******************************************************************************
24 * Includes
25 ******************************************************************************/
26#include "../qtcompat.h"
27#include "length.h"
28
29#include <QtCore>
30
31/*******************************************************************************
32 * Namespace / Forward Declarations
33 ******************************************************************************/
34namespace librepcb {
35
36class Angle;
37class SExpression;
38
39/*******************************************************************************
40 * Class Point
41 ******************************************************************************/
42
79class Point final {
80public:
81 // Constructors / Destructor
82
88 Point() noexcept : Point(Length(0), Length(0)) {}
89
95 Point(const Point& point) noexcept : mX(point.mX), mY(point.mY) {}
96
103 explicit Point(const Length& x, const Length& y) noexcept : mX(x), mY(y) {}
104
105 explicit Point(const SExpression& node);
106
110 ~Point() noexcept {}
111
112 // Setters
113
119 void setX(const Length& x) noexcept { mX = x; }
120
126 void setY(const Length& y) noexcept { mY = y; }
127
136 void setXmm(const QString& mm) { mX.setLengthMm(mm); }
137
146 void setYmm(const QString& mm) { mY.setLengthMm(mm); }
147
151 void setPointNm(LengthBase_t nmX, LengthBase_t nmY) noexcept {
152 mX.setLengthNm(nmX);
153 mY.setLengthNm(nmY);
154 }
155
159 void setPointMm(const QPointF& millimeters) {
160 mX.setLengthMm(millimeters.x());
161 mY.setLengthMm(millimeters.y());
162 }
163
167 void setPointInch(const QPointF& inches) {
168 mX.setLengthInch(inches.x());
169 mY.setLengthInch(inches.y());
170 }
171
175 void setPointMil(const QPointF& mils) {
176 mX.setLengthMil(mils.x());
177 mX.setLengthMil(mils.y());
178 }
179
185 void setPointPx(const QPointF& pixels) {
186 mX.setLengthPx(pixels.x());
187 mY.setLengthPx(-pixels.y());
188 } // invert Y!
189
190 // Getters
191
197 const Length& getX() const noexcept { return mX; }
198
204 const Length& getY() const noexcept { return mY; }
205
212 UnsignedLength getLength() const noexcept {
213 LengthBase_t length =
214 static_cast<LengthBase_t>(std::hypot(mX.toNm(), mY.toNm()));
215 Q_ASSERT(length >= 0);
216 return UnsignedLength(length);
217 }
218
224 bool isOrigin() const noexcept { return ((mX == 0) && (mY == 0)); }
225
226 // Conversions
227
237 QPointF toMmQPointF() const noexcept { return QPointF(mX.toMm(), mY.toMm()); }
238
248 QPointF toInchQPointF() const noexcept {
249 return QPointF(mX.toInch(), mY.toInch());
250 }
251
261 QPointF toMilQPointF() const noexcept {
262 return QPointF(mX.toMil(), mY.toMil());
263 }
264
277 QPointF toPxQPointF() const noexcept {
278 return QPointF(mX.toPx(), -mY.toPx());
279 } // invert Y!
280
281 // General Methods
282
291 Point abs() const noexcept;
292
300 Point& makeAbs() noexcept;
301
311 Point mappedToGrid(const PositiveLength& gridInterval) const noexcept;
312
322 Point& mapToGrid(const PositiveLength& gridInterval) noexcept;
323
333 bool isOnGrid(const PositiveLength& gridInterval) const noexcept;
334
350 Point rotated(const Angle& angle,
351 const Point& center = Point(0, 0)) const noexcept;
352
368 Point& rotate(const Angle& angle, const Point& center = Point(0, 0)) noexcept;
369
382 Point mirrored(Qt::Orientation orientation,
383 const Point& center = Point(0, 0)) const noexcept;
384
396 Point& mirror(Qt::Orientation orientation,
397 const Point& center = Point(0, 0)) noexcept;
398
404 void serialize(SExpression& root) const;
405
406 // Static Functions
407
409 static Point fromMm(qreal millimetersX, qreal millimetersY);
410 static Point fromMm(const QPointF& millimeters);
411
413 static Point fromInch(qreal inchesX, qreal inchesY);
414 static Point fromInch(const QPointF& inches);
415
417 static Point fromMil(qreal milsX, qreal milsY);
418 static Point fromMil(const QPointF& mils);
419
424 static Point fromPx(qreal pixelsX, qreal pixelsY);
425 static Point fromPx(const QPointF& pixels);
426
427 // Operators
428 Point& operator=(const Point& rhs) {
429 mX = rhs.mX;
430 mY = rhs.mY;
431 return *this;
432 }
433 Point& operator+=(const Point& rhs) {
434 mX += rhs.mX;
435 mY += rhs.mY;
436 return *this;
437 }
438 Point& operator-=(const Point& rhs) {
439 mX -= rhs.mX;
440 mY -= rhs.mY;
441 return *this;
442 }
443 Point& operator*=(const Point& rhs) {
444 mX *= rhs.mX;
445 mY *= rhs.mY;
446 return *this;
447 }
449 mX *= rhs;
450 mY *= rhs;
451 return *this;
452 }
453 Point& operator/=(const Point& rhs) {
454 mX /= rhs.mX;
455 mY /= rhs.mY;
456 return *this;
457 }
459 mX /= rhs;
460 mY /= rhs;
461 return *this;
462 }
463 Point operator+(const Point& rhs) const {
464 return Point(mX + rhs.mX, mY + rhs.mY);
465 }
466 Point operator-() const { return Point(-mX, -mY); }
467 Point operator-(const Point& rhs) const {
468 return Point(mX - rhs.mX, mY - rhs.mY);
469 }
470 Point operator*(const Length& rhs) const { return Point(mX * rhs, mY * rhs); }
471 Point operator*(LengthBase_t rhs) const { return Point(mX * rhs, mY * rhs); }
472 Point operator/(const Length& rhs) const { return Point(mX / rhs, mY / rhs); }
473 Point operator/(LengthBase_t rhs) const { return Point(mX / rhs, mY / rhs); }
474 Point operator%(const Length& rhs) const { return Point(mX % rhs, mY % rhs); }
475 bool operator==(const Point& rhs) const {
476 return (mX == rhs.mX) && (mY == rhs.mY);
477 }
478 bool operator!=(const Point& rhs) const {
479 return (mX != rhs.mX) || (mY != rhs.mY);
480 }
481
483
497 bool operator<(const Point& rhs) const noexcept {
498 return (mX < rhs.mX) || ((mX == rhs.mX) && (mY < rhs.mY));
499 }
500 bool operator<=(const Point& rhs) const noexcept {
501 return (mX < rhs.mX) || ((mX == rhs.mX) && (mY <= rhs.mY));
502 }
503 bool operator>(const Point& rhs) const noexcept {
504 return (mX > rhs.mX) || ((mX == rhs.mX) && (mY > rhs.mY));
505 }
506 bool operator>=(const Point& rhs) const noexcept {
507 return (mX > rhs.mX) || ((mX == rhs.mX) && (mY >= rhs.mY));
508 }
510
511private:
514};
515
516/*******************************************************************************
517 * Non-Member Functions
518 ******************************************************************************/
519
520QDataStream& operator<<(QDataStream& stream, const Point& point);
521QDebug operator<<(QDebug stream, const Point& point);
522
523inline QtCompat::Hash qHash(const Point& key,
524 QtCompat::Hash seed = 0) noexcept {
525 return ::qHash(qMakePair(key.getX(), key.getY()), seed);
526}
527
528/*******************************************************************************
529 * End of File
530 ******************************************************************************/
531
532} // namespace librepcb
533
534Q_DECLARE_METATYPE(librepcb::Point)
535
536#endif
The Angle class is used to represent an angle (for example 12.75 degrees)
Definition: angle.h:78
The Length class is used to represent a length (for example 12.75 millimeters)
Definition: length.h:83
void setLengthMil(qreal mils)
Set the length in mils (1/1000 inch)
Definition: length.h:188
void setLengthPx(qreal pixels)
Set the length in pixels (from QGraphics* objects)
Definition: length.h:205
qreal toMm() const noexcept
Get the length in millimeters.
Definition: length.h:240
qreal toInch() const noexcept
Get the length in inches.
Definition: length.h:262
void setLengthNm(LengthBase_t nanometers) noexcept
Set the length in nanometers.
Definition: length.h:124
void setLengthInch(qreal inches)
Set the length in inches.
Definition: length.h:173
qreal toPx() const noexcept
Get the length in pixels (for QGraphics* objects)
Definition: length.h:283
void setLengthMm(qreal millimeters)
Set the length in millimeters.
Definition: length.h:141
qreal toMil() const noexcept
Get the length in mils (1/1000 inches)
Definition: length.h:271
LengthBase_t toNm() const noexcept
Get the length in nanometers.
Definition: length.h:214
The Point class is used to represent a point/coordinate/vector, for example (1.2mm; 5....
Definition: point.h:79
Length mX
the X coordinate
Definition: point.h:512
bool isOrigin() const noexcept
Check if the position represents the origin (X == 0 and Y == 0)
Definition: point.h:224
Point & makeAbs() noexcept
Make both coordinates absolute (X,Y >= 0)
Definition: point.cpp:56
const Length & getX() const noexcept
Get the X coordinate.
Definition: point.h:197
Point(const Point &point) noexcept
Copy Constructor.
Definition: point.h:95
static Point fromPx(qreal pixelsX, qreal pixelsY)
Definition: point.cpp:176
void setPointNm(LengthBase_t nmX, LengthBase_t nmY) noexcept
Definition: point.h:151
void setYmm(const QString &mm)
Set the Y coordinate from a string in millimeters.
Definition: point.h:146
Point operator*(const Length &rhs) const
Definition: point.h:470
Point operator-() const
Definition: point.h:466
bool operator<(const Point &rhs) const noexcept
Less/Greater comparison operator overloadings.
Definition: point.h:497
Point operator%(const Length &rhs) const
Definition: point.h:474
void setY(const Length &y) noexcept
Set the Y coordinate.
Definition: point.h:126
QPointF toMilQPointF() const noexcept
Get the point as a QPointF object in mils (1/1000 inches)
Definition: point.h:261
bool operator<=(const Point &rhs) const noexcept
Definition: point.h:500
QPointF toInchQPointF() const noexcept
Get the point as a QPointF object in inches.
Definition: point.h:248
Point operator*(LengthBase_t rhs) const
Definition: point.h:471
QPointF toPxQPointF() const noexcept
Get the point as a QPointF object in pixels (for QGraphics* objects)
Definition: point.h:277
Point abs() const noexcept
Get a Point object with both coordinates in absolute values (X,Y >= 0)
Definition: point.cpp:50
Point & mapToGrid(const PositiveLength &gridInterval) noexcept
Map this Point object to a specific grid interval.
Definition: point.cpp:68
Point rotated(const Angle &angle, const Point &center=Point(0, 0)) const noexcept
Get the point rotated by a specific angle with respect to a specific center.
Definition: point.cpp:78
Point() noexcept
Default Constructor.
Definition: point.h:88
Length mY
the Y coordinate
Definition: point.h:513
Point(const Length &x, const Length &y) noexcept
Constructor for passing two Length objects.
Definition: point.h:103
Point operator-(const Point &rhs) const
Definition: point.h:467
Point & operator*=(const Point &rhs)
Definition: point.h:443
static Point fromInch(qreal inchesX, qreal inchesY)
Definition: point.cpp:154
Point & operator-=(const Point &rhs)
Definition: point.h:438
void setX(const Length &x) noexcept
Set the X coordinate.
Definition: point.h:119
void setPointMil(const QPointF &mils)
Definition: point.h:175
const Length & getY() const noexcept
Get the Y coordinate.
Definition: point.h:204
Point mirrored(Qt::Orientation orientation, const Point &center=Point(0, 0)) const noexcept
Get the point mirrored horizontally or vertically around a specific center.
Definition: point.cpp:114
void setXmm(const QString &mm)
Set the X coordinate from a string in millimeters.
Definition: point.h:136
~Point() noexcept
Destructor.
Definition: point.h:110
bool operator>(const Point &rhs) const noexcept
Definition: point.h:503
Point operator+(const Point &rhs) const
Definition: point.h:463
UnsignedLength getLength() const noexcept
Get the length of the vector if X and Y represents a vector (e.g. the distance of this Point from the...
Definition: point.h:212
void serialize(SExpression &root) const
Serialize into librepcb::SExpression node.
Definition: point.cpp:136
bool isOnGrid(const PositiveLength &gridInterval) const noexcept
Check whether the Point lies on the grid.
Definition: point.cpp:74
static Point fromMm(qreal millimetersX, qreal millimetersY)
Definition: point.cpp:143
QPointF toMmQPointF() const noexcept
Get the point as a QPointF object in millimeters.
Definition: point.h:237
Point operator/(const Length &rhs) const
Definition: point.h:472
Point mappedToGrid(const PositiveLength &gridInterval) const noexcept
Get a Point object which is mapped to a specific grid interval.
Definition: point.cpp:62
void setPointInch(const QPointF &inches)
Definition: point.h:167
Point operator/(LengthBase_t rhs) const
Definition: point.h:473
bool operator!=(const Point &rhs) const
Definition: point.h:478
static Point fromMil(qreal milsX, qreal milsY)
Definition: point.cpp:165
Point & operator*=(LengthBase_t rhs)
Definition: point.h:448
Point & operator/=(LengthBase_t rhs)
Definition: point.h:458
Point & operator+=(const Point &rhs)
Definition: point.h:433
void setPointMm(const QPointF &millimeters)
Definition: point.h:159
Point & operator/=(const Point &rhs)
Definition: point.h:453
Point & mirror(Qt::Orientation orientation, const Point &center=Point(0, 0)) noexcept
Mirror the point horizontally or vertically around a specific center.
Definition: point.cpp:121
void setPointPx(const QPointF &pixels)
Definition: point.h:185
bool operator>=(const Point &rhs) const noexcept
Definition: point.h:506
bool operator==(const Point &rhs) const
Definition: point.h:475
Point & rotate(const Angle &angle, const Point &center=Point(0, 0)) noexcept
Rotate the point by a specific angle with respect to a specific center.
Definition: point.cpp:84
uint Hash
Return type of Qt's qHash() function.
Definition: qtcompat.h:58
The SExpression class.
Definition: sexpression.h:69
Definition: occmodel.cpp:77
type_safe::constrained_type< Length, PositiveLengthConstraint, PositiveLengthVerifier > PositiveLength
Definition: length.h:812
QtCompat::Hash qHash(const Point &key, QtCompat::Hash seed=0) noexcept
Definition: point.h:523
QtCompat::Hash qHash(const AttributeKey &key, QtCompat::Hash seed=0) noexcept
Definition: attributekey.h:119
type_safe::constrained_type< Length, UnsignedLengthConstraint, UnsignedLengthVerifier > UnsignedLength
Definition: length.h:696
qint64 LengthBase_t
This type is the ONLY base type to store all lengths (always in nanometers)!
Definition: length.h:62
QDataStream & operator<<(QDataStream &stream, const AttributeKey &obj)
Definition: attributekey.h:109