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_POINT_H
21 #define LIBREPCB_POINT_H
22 
23 /*******************************************************************************
24  * Includes
25  ******************************************************************************/
26 #include "../fileio/serializableobject.h"
27 #include "length.h"
28 
29 #include <QtCore>
30 
31 /*******************************************************************************
32  * Namespace / Forward Declarations
33  ******************************************************************************/
34 namespace librepcb {
35 
36 class Angle;
37 
38 /*******************************************************************************
39  * Class Point
40  ******************************************************************************/
41 
78 class Point final : public SerializableObject {
79 public:
80  // Constructors / Destructor
81 
87  Point() noexcept : Point(Length(0), Length(0)) {}
88 
94  Point(const Point& point) noexcept : mX(point.mX), mY(point.mY) {}
95 
102  explicit Point(const Length& x, const Length& y) noexcept : mX(x), mY(y) {}
103 
104  Point(const SExpression& node, const Version& fileFormat);
105 
109  ~Point() noexcept {}
110 
111  // Setters
112 
118  void setX(const Length& x) noexcept { mX = x; }
119 
125  void setY(const Length& y) noexcept { mY = y; }
126 
135  void setXmm(const QString& mm) { mX.setLengthMm(mm); }
136 
145  void setYmm(const QString& mm) { mY.setLengthMm(mm); }
146 
150  void setPointNm(LengthBase_t nmX, LengthBase_t nmY) noexcept {
151  mX.setLengthNm(nmX);
152  mY.setLengthNm(nmY);
153  }
154 
158  void setPointMm(const QPointF& millimeters) {
159  mX.setLengthMm(millimeters.x());
160  mY.setLengthMm(millimeters.y());
161  }
162 
166  void setPointInch(const QPointF& inches) {
167  mX.setLengthInch(inches.x());
168  mY.setLengthInch(inches.y());
169  }
170 
174  void setPointMil(const QPointF& mils) {
175  mX.setLengthMil(mils.x());
176  mX.setLengthMil(mils.y());
177  }
178 
184  void setPointPx(const QPointF& pixels) {
185  mX.setLengthPx(pixels.x());
186  mY.setLengthPx(-pixels.y());
187  } // invert Y!
188 
189  // Getters
190 
196  const Length& getX() const noexcept { return mX; }
197 
203  const Length& getY() const noexcept { return mY; }
204 
211  UnsignedLength getLength() const noexcept {
212  LengthBase_t length = static_cast<LengthBase_t>(
213  qSqrt(mX.toNm() * mX.toNm() + mY.toNm() * mY.toNm()));
214  Q_ASSERT(length >= 0);
215  return UnsignedLength(length);
216  }
217 
223  bool isOrigin() const noexcept { return ((mX == 0) && (mY == 0)); }
224 
225  // Conversions
226 
236  QPointF toMmQPointF() const noexcept { return QPointF(mX.toMm(), mY.toMm()); }
237 
247  QPointF toInchQPointF() const noexcept {
248  return QPointF(mX.toInch(), mY.toInch());
249  }
250 
260  QPointF toMilQPointF() const noexcept {
261  return QPointF(mX.toMil(), mY.toMil());
262  }
263 
276  QPointF toPxQPointF() const noexcept {
277  return QPointF(mX.toPx(), -mY.toPx());
278  } // invert Y!
279 
280  // General Methods
281 
290  Point abs() const noexcept;
291 
299  Point& makeAbs() noexcept;
300 
310  Point mappedToGrid(const PositiveLength& gridInterval) const noexcept;
311 
321  Point& mapToGrid(const PositiveLength& gridInterval) noexcept;
322 
332  bool isOnGrid(const PositiveLength& gridInterval) const noexcept;
333 
349  Point rotated(const Angle& angle, const Point& center = Point(0, 0)) const
350  noexcept;
351 
367  Point& rotate(const Angle& angle, const Point& center = Point(0, 0)) noexcept;
368 
381  Point mirrored(Qt::Orientation orientation,
382  const Point& center = Point(0, 0)) const noexcept;
383 
395  Point& mirror(Qt::Orientation orientation,
396  const Point& center = Point(0, 0)) noexcept;
397 
399  void serialize(SExpression& root) const override;
400 
401  // Static Functions
402 
404  static Point fromMm(qreal millimetersX, qreal millimetersY);
405  static Point fromMm(const QPointF& millimeters);
406 
408  static Point fromInch(qreal inchesX, qreal inchesY);
409  static Point fromInch(const QPointF& inches);
410 
412  static Point fromMil(qreal milsX, qreal milsY);
413  static Point fromMil(const QPointF& mils);
414 
419  static Point fromPx(qreal pixelsX, qreal pixelsY);
420  static Point fromPx(const QPointF& pixels);
421 
422  // Operators
423  Point& operator=(const Point& rhs) {
424  mX = rhs.mX;
425  mY = rhs.mY;
426  return *this;
427  }
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  }
444  mX *= rhs;
445  mY *= rhs;
446  return *this;
447  }
448  Point& operator/=(const Point& rhs) {
449  mX /= rhs.mX;
450  mY /= rhs.mY;
451  return *this;
452  }
454  mX /= rhs;
455  mY /= rhs;
456  return *this;
457  }
458  Point operator+(const Point& rhs) const {
459  return Point(mX + rhs.mX, mY + rhs.mY);
460  }
461  Point operator-() const { return Point(-mX, -mY); }
462  Point operator-(const Point& rhs) const {
463  return Point(mX - rhs.mX, mY - rhs.mY);
464  }
465  Point operator*(const Length& rhs) const { return Point(mX * rhs, mY * rhs); }
466  Point operator*(LengthBase_t rhs) const { return Point(mX * rhs, mY * rhs); }
467  Point operator/(const Length& rhs) const { return Point(mX / rhs, mY / rhs); }
468  Point operator/(LengthBase_t rhs) const { return Point(mX / rhs, mY / rhs); }
469  Point operator%(const Length& rhs) const { return Point(mX % rhs, mY % rhs); }
470  bool operator==(const Point& rhs) const {
471  return (mX == rhs.mX) && (mY == rhs.mY);
472  }
473  bool operator!=(const Point& rhs) const {
474  return (mX != rhs.mX) || (mY != rhs.mY);
475  }
476 
478 
492  bool operator<(const Point& rhs) const noexcept {
493  return (mX < rhs.mX) || ((mX == rhs.mX) && (mY < rhs.mY));
494  }
495  bool operator<=(const Point& rhs) const noexcept {
496  return (mX < rhs.mX) || ((mX == rhs.mX) && (mY <= rhs.mY));
497  }
498  bool operator>(const Point& rhs) const noexcept {
499  return (mX > rhs.mX) || ((mX == rhs.mX) && (mY > rhs.mY));
500  }
501  bool operator>=(const Point& rhs) const noexcept {
502  return (mX > rhs.mX) || ((mX == rhs.mX) && (mY >= rhs.mY));
503  }
505 
506 private:
509 };
510 
511 /*******************************************************************************
512  * Non-Member Functions
513  ******************************************************************************/
514 
515 QDataStream& operator<<(QDataStream& stream, const Point& point);
516 QDebug operator<<(QDebug stream, const Point& point);
517 
518 inline uint qHash(const Point& key, uint seed = 0) noexcept {
519  return ::qHash(qMakePair(key.getX(), key.getY()), seed);
520 }
521 
522 /*******************************************************************************
523  * End of File
524  ******************************************************************************/
525 
526 } // namespace librepcb
527 
528 Q_DECLARE_METATYPE(librepcb::Point)
529 
530 #endif // LIBREPCB_POINT_H
bool isOrigin() const noexcept
Check if the position represents the origin (X == 0 and Y == 0)
Definition: point.h:223
qreal toPx() const noexcept
Get the length in pixels (for QGraphics* objects)
Definition: length.h:273
const Length & getX() const noexcept
Get the X coordinate.
Definition: point.h:196
bool operator<=(const Point &rhs) const noexcept
Less/Greater comparison operator overloadings.
Definition: point.h:495
The Version class represents a version number in the format "1.42.7".
Definition: version.h:60
Point operator-() const
Definition: point.h:461
bool operator>(const Point &rhs) const noexcept
Less/Greater comparison operator overloadings.
Definition: point.h:498
QDataStream & operator<<(QDataStream &stream, const AttributeKey &obj)
Definition: attributekey.h:110
void setYmm(const QString &mm)
Set the Y coordinate from a string in millimeters.
Definition: point.h:145
QPointF toPxQPointF() const noexcept
Get the point as a QPointF object in pixels (for QGraphics* objects)
Definition: point.h:276
void setPointPx(const QPointF &pixels)
Definition: point.h:184
Point(const Point &point) noexcept
Copy Constructor.
Definition: point.h:94
Point & operator*=(LengthBase_t rhs)
Definition: point.h:443
static Point fromMil(qreal milsX, qreal milsY)
Definition: point.cpp:163
const Length & getY() const noexcept
Get the Y coordinate.
Definition: point.h:203
void setXmm(const QString &mm)
Set the X coordinate from a string in millimeters.
Definition: point.h:135
Definition: airwiresbuilder.cpp:32
bool operator==(const Point &rhs) const
Definition: point.h:470
Point operator/(LengthBase_t rhs) const
Definition: point.h:468
bool operator!=(const Point &rhs) const
Definition: point.h:473
void setLengthNm(LengthBase_t nanometers) noexcept
Set the length in nanometers.
Definition: length.h:123
Point & mapToGrid(const PositiveLength &gridInterval) noexcept
Map this Point object to a specific grid interval.
Definition: point.cpp:68
Point operator+(const Point &rhs) const
Definition: point.h:458
qint64 LengthBase_t
This type is the ONLY base type to store all lengths (always in nanometers)!
Definition: length.h:61
qreal toMm() const noexcept
Get the length in millimeters.
Definition: length.h:230
LengthBase_t toNm() const noexcept
Get the length in nanometers.
Definition: length.h:213
The Angle class is used to represent an angle (for example 12.75 degrees)
Definition: angle.h:78
Length mY
the Y coordinate
Definition: point.h:508
Point & operator+=(const Point &rhs)
Definition: point.h:428
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:119
~Point() noexcept
Destructor.
Definition: point.h:109
Point mappedToGrid(const PositiveLength &gridInterval) const noexcept
Get a Point object which is mapped to a specific grid interval.
Definition: point.cpp:62
Point & operator=(const Point &rhs)
Definition: point.h:423
bool isOnGrid(const PositiveLength &gridInterval) const noexcept
Check whether the Point lies on the grid.
Definition: point.cpp:74
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
The SerializableObject class is the base class for all classes which need to be serializable/deserial...
Definition: serializableobject.h:43
Point operator/(const Length &rhs) const
Definition: point.h:467
Point & operator-=(const Point &rhs)
Definition: point.h:433
The Point class is used to represent a point/coordinate/vector, for example (1.2mm; 5...
Definition: point.h:78
qreal toMil() const noexcept
Get the length in mils (1/1000 inches)
Definition: length.h:261
Length mX
the X coordinate
Definition: point.h:507
void setLengthPx(qreal pixels)
Set the length in pixels (from QGraphics* objects)
Definition: length.h:204
void setPointNm(LengthBase_t nmX, LengthBase_t nmY) noexcept
Definition: point.h:150
void setPointInch(const QPointF &inches)
Definition: point.h:166
static Point fromInch(qreal inchesX, qreal inchesY)
Definition: point.cpp:152
QPointF toInchQPointF() const noexcept
Get the point as a QPointF object in inches.
Definition: point.h:247
void setY(const Length &y) noexcept
Set the Y coordinate.
Definition: point.h:125
Point operator%(const Length &rhs) const
Definition: point.h:469
Point operator*(LengthBase_t rhs) const
Definition: point.h:466
Point operator-(const Point &rhs) const
Definition: point.h:462
Point(const Length &x, const Length &y) noexcept
Constructor for passing two Length objects.
Definition: point.h:102
Point() noexcept
Default Constructor.
Definition: point.h:87
bool operator>=(const Point &rhs) const noexcept
Less/Greater comparison operator overloadings.
Definition: point.h:501
void setX(const Length &x) noexcept
Set the X coordinate.
Definition: point.h:118
void setPointMil(const QPointF &mils)
Definition: point.h:174
Point & operator*=(const Point &rhs)
Definition: point.h:438
Point & operator/=(const Point &rhs)
Definition: point.h:448
Point abs() const noexcept
Get a Point object with both coordinates in absolute values (X,Y >= 0)
Definition: point.cpp:50
void setLengthInch(qreal inches)
Set the length in inches.
Definition: length.h:172
Point operator*(const Length &rhs) const
Definition: point.h:465
void setPointMm(const QPointF &millimeters)
Definition: point.h:158
QPointF toMilQPointF() const noexcept
Get the point as a QPointF object in mils (1/1000 inches)
Definition: point.h:260
QPointF toMmQPointF() const noexcept
Get the point as a QPointF object in millimeters.
Definition: point.h:236
type_safe::constrained_type< Length, PositiveLengthConstraint, PositiveLengthVerifier > PositiveLength
Definition: length.h:792
static Point fromPx(qreal pixelsX, qreal pixelsY)
Definition: point.cpp:174
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:112
void setLengthMil(qreal mils)
Set the length in mils (1/1000 inch)
Definition: length.h:187
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
static Point fromMm(qreal millimetersX, qreal millimetersY)
Definition: point.cpp:141
The Length class is used to represent a length (for example 12.75 millimeters)
Definition: length.h:82
uint qHash(const Point &key, uint seed=0) noexcept
Definition: point.h:518
qreal toInch() const noexcept
Get the length in inches.
Definition: length.h:252
void setLengthMm(qreal millimeters)
Set the length in millimeters.
Definition: length.h:140
Point & operator/=(LengthBase_t rhs)
Definition: point.h:453
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:211
Point & makeAbs() noexcept
Make both coordinates absolute (X,Y >= 0)
Definition: point.cpp:56
bool operator<(const Point &rhs) const noexcept
Less/Greater comparison operator overloadings.
Definition: point.h:492
The SExpression class.
Definition: sexpression.h:72
void serialize(SExpression &root) const override
Serialize the object into an existing S-Expression node.
Definition: point.cpp:134
uint qHash(const AttributeKey &key, uint seed=0) noexcept
Definition: attributekey.h:120
type_safe::constrained_type< Length, UnsignedLengthConstraint, UnsignedLengthVerifier > UnsignedLength
Definition: length.h:672