LibrePCB Developers Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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  explicit Point(const SExpression& node);
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  Length getLength() const noexcept {
212  return Length(qSqrt(mX.toNm() * mX.toNm() + mY.toNm() * mY.toNm()));
213  }
214 
220  bool isOrigin() const noexcept { return ((mX == 0) && (mY == 0)); }
221 
222  // Conversions
223 
233  QPointF toMmQPointF() const noexcept { return QPointF(mX.toMm(), mY.toMm()); }
234 
244  QPointF toInchQPointF() const noexcept {
245  return QPointF(mX.toInch(), mY.toInch());
246  }
247 
257  QPointF toMilQPointF() const noexcept {
258  return QPointF(mX.toMil(), mY.toMil());
259  }
260 
273  QPointF toPxQPointF() const noexcept {
274  return QPointF(mX.toPx(), -mY.toPx());
275  } // invert Y!
276 
277  // General Methods
278 
287  Point abs() const noexcept;
288 
296  Point& makeAbs() noexcept;
297 
307  Point mappedToGrid(const PositiveLength& gridInterval) const noexcept;
308 
318  Point& mapToGrid(const PositiveLength& gridInterval) noexcept;
319 
335  Point rotated(const Angle& angle, const Point& center = Point(0, 0)) const
336  noexcept;
337 
353  Point& rotate(const Angle& angle, const Point& center = Point(0, 0)) noexcept;
354 
367  Point mirrored(Qt::Orientation orientation,
368  const Point& center = Point(0, 0)) const noexcept;
369 
381  Point& mirror(Qt::Orientation orientation,
382  const Point& center = Point(0, 0)) noexcept;
383 
385  void serialize(SExpression& root) const override;
386 
387  // Static Functions
388 
390  static Point fromMm(qreal millimetersX, qreal millimetersY);
391  static Point fromMm(const QPointF& millimeters);
392 
394  static Point fromInch(qreal inchesX, qreal inchesY);
395  static Point fromInch(const QPointF& inches);
396 
398  static Point fromMil(qreal milsX, qreal milsY);
399  static Point fromMil(const QPointF& mils);
400 
405  static Point fromPx(qreal pixelsX, qreal pixelsY);
406  static Point fromPx(const QPointF& pixels);
407 
408  // Operators
409  Point& operator=(const Point& rhs) {
410  mX = rhs.mX;
411  mY = rhs.mY;
412  return *this;
413  }
414  Point& operator+=(const Point& rhs) {
415  mX += rhs.mX;
416  mY += rhs.mY;
417  return *this;
418  }
419  Point& operator-=(const Point& rhs) {
420  mX -= rhs.mX;
421  mY -= rhs.mY;
422  return *this;
423  }
424  Point& operator*=(const Point& rhs) {
425  mX *= rhs.mX;
426  mY *= rhs.mY;
427  return *this;
428  }
430  mX *= rhs;
431  mY *= rhs;
432  return *this;
433  }
434  Point& operator/=(const Point& rhs) {
435  mX /= rhs.mX;
436  mY /= rhs.mY;
437  return *this;
438  }
440  mX /= rhs;
441  mY /= rhs;
442  return *this;
443  }
444  Point operator+(const Point& rhs) const {
445  return Point(mX + rhs.mX, mY + rhs.mY);
446  }
447  Point operator-() const { return Point(-mX, -mY); }
448  Point operator-(const Point& rhs) const {
449  return Point(mX - rhs.mX, mY - rhs.mY);
450  }
451  Point operator*(const Length& rhs) const { return Point(mX * rhs, mY * rhs); }
452  Point operator*(LengthBase_t rhs) const { return Point(mX * rhs, mY * rhs); }
453  Point operator/(const Length& rhs) const { return Point(mX / rhs, mY / rhs); }
454  Point operator/(LengthBase_t rhs) const { return Point(mX / rhs, mY / rhs); }
455  Point operator%(const Length& rhs) const { return Point(mX % rhs, mY % rhs); }
456  bool operator==(const Point& rhs) const {
457  return (mX == rhs.mX) && (mY == rhs.mY);
458  }
459  bool operator!=(const Point& rhs) const {
460  return (mX != rhs.mX) || (mY != rhs.mY);
461  }
462 
463 private:
466 };
467 
468 /*******************************************************************************
469  * Non-Member Functions
470  ******************************************************************************/
471 
472 QDataStream& operator<<(QDataStream& stream, const Point& point);
473 QDebug operator<<(QDebug stream, const Point& point);
474 
475 inline uint qHash(const Point& key, uint seed = 0) noexcept {
476  return ::qHash(qMakePair(key.getX(), key.getY()), seed);
477 }
478 
479 /*******************************************************************************
480  * End of File
481  ******************************************************************************/
482 
483 } // namespace librepcb
484 
485 Q_DECLARE_METATYPE(librepcb::Point)
486 
487 #endif // LIBREPCB_POINT_H
bool isOrigin() const noexcept
Check if the position represents the origin (X == 0 and Y == 0)
Definition: point.h:220
qreal toPx() const noexcept
Get the length in pixels (for QGraphics* objects)
Definition: length.h:273
Point operator+(const Point &rhs) const
Definition: point.h:444
const Length & getX() const noexcept
Get the X coordinate.
Definition: point.h:196
QDataStream & operator<<(QDataStream &stream, const AttributeKey &obj)
Definition: attributekey.h:109
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:273
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:429
static Point fromMil(qreal milsX, qreal milsY)
Definition: point.cpp:159
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
void setLengthNm(LengthBase_t nanometers) noexcept
Set the length in nanometers.
Definition: length.h:123
Point operator*(LengthBase_t rhs) const
Definition: point.h:452
Point & mapToGrid(const PositiveLength &gridInterval) noexcept
Map this Point object to a specific grid interval.
Definition: point.cpp:68
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
Point operator/(const Length &rhs) const
Definition: point.h:453
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:465
Point & operator+=(const Point &rhs)
Definition: point.h:414
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:115
~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 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:74
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:455
Point & operator-=(const Point &rhs)
Definition: point.h:419
The Point class is used to represent a point/coordinate/vector, for example (1.2mm; 5...
Definition: point.h:78
Point operator-(const Point &rhs) const
Definition: point.h:448
qreal toMil() const noexcept
Get the length in mils (1/1000 inches)
Definition: length.h:261
Length mX
the X coordinate
Definition: point.h:464
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:148
QPointF toInchQPointF() const noexcept
Get the point as a QPointF object in inches.
Definition: point.h:244
void setY(const Length &y) noexcept
Set the Y coordinate.
Definition: point.h:125
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
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:424
Point & operator/=(const Point &rhs)
Definition: point.h:434
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
Definition: point.h:447
bool operator!=(const Point &rhs) const
Definition: point.h:459
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:257
QPointF toMmQPointF() const noexcept
Get the point as a QPointF object in millimeters.
Definition: point.h:233
type_safe::constrained_type< Length, PositiveLengthConstraint, PositiveLengthVerifier > PositiveLength
Definition: length.h:780
static Point fromPx(qreal pixelsX, qreal pixelsY)
Definition: point.cpp:170
Length 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 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:108
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:80
static Point fromMm(qreal millimetersX, qreal millimetersY)
Definition: point.cpp:137
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:475
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
bool operator==(const Point &rhs) const
Definition: point.h:456
Point & operator/=(LengthBase_t rhs)
Definition: point.h:439
Point operator/(LengthBase_t rhs) const
Definition: point.h:454
Point operator*(const Length &rhs) const
Definition: point.h:451
Point & makeAbs() noexcept
Make both coordinates absolute (X,Y >= 0)
Definition: point.cpp:56
The SExpression class.
Definition: sexpression.h:57
void serialize(SExpression &root) const override
Serialize the object into an existing S-Expression node.
Definition: point.cpp:130
uint qHash(const AttributeKey &key, uint seed=0) noexcept
Definition: attributekey.h:119