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 
81 class Point final : public SerializableObject {
82 public:
83  // Constructors / Destructor
84 
90  Point() noexcept : Point(Length(0), Length(0)) {}
91 
97  Point(const Point& point) noexcept : mX(point.mX), mY(point.mY) {}
98 
105  explicit Point(const Length& x, const Length& y) noexcept : mX(x), mY(y) {}
106 
107  explicit Point(const SExpression& node);
108 
112  ~Point() noexcept {}
113 
114  // Setters
115 
121  void setX(const Length& x) noexcept { mX = x; }
122 
128  void setY(const Length& y) noexcept { mY = y; }
129 
138  void setXmm(const QString& mm) { mX.setLengthMm(mm); }
139 
148  void setYmm(const QString& mm) { mY.setLengthMm(mm); }
149 
153  void setPointNm(LengthBase_t nmX, LengthBase_t nmY) noexcept {
154  mX.setLengthNm(nmX);
155  mY.setLengthNm(nmY);
156  }
157 
161  void setPointMm(const QPointF& millimeters) {
162  mX.setLengthMm(millimeters.x());
163  mY.setLengthMm(millimeters.y());
164  }
165 
169  void setPointInch(const QPointF& inches) {
170  mX.setLengthInch(inches.x());
171  mY.setLengthInch(inches.y());
172  }
173 
177  void setPointMil(const QPointF& mils) {
178  mX.setLengthMil(mils.x());
179  mX.setLengthMil(mils.y());
180  }
181 
187  void setPointPx(const QPointF& pixels) {
188  mX.setLengthPx(pixels.x());
189  mY.setLengthPx(-pixels.y());
190  } // invert Y!
191 
192  // Getters
193 
199  const Length& getX() const noexcept { return mX; }
200 
206  const Length& getY() const noexcept { return mY; }
207 
214  Length getLength() const noexcept {
215  return Length(qSqrt(mX.toNm() * mX.toNm() + mY.toNm() * mY.toNm()));
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 
338  Point rotated(const Angle& angle, const Point& center = Point(0, 0)) const
339  noexcept;
340 
356  Point& rotate(const Angle& angle, const Point& center = Point(0, 0)) noexcept;
357 
370  Point mirrored(Qt::Orientation orientation,
371  const Point& center = Point(0, 0)) const noexcept;
372 
384  Point& mirror(Qt::Orientation orientation,
385  const Point& center = Point(0, 0)) noexcept;
386 
388  void serialize(SExpression& root) const override;
389 
390  // Static Functions
391 
393  static Point fromMm(qreal millimetersX, qreal millimetersY);
394  static Point fromMm(const QPointF& millimeters);
395 
397  static Point fromInch(qreal inchesX, qreal inchesY);
398  static Point fromInch(const QPointF& inches);
399 
401  static Point fromMil(qreal milsX, qreal milsY);
402  static Point fromMil(const QPointF& mils);
403 
408  static Point fromPx(qreal pixelsX, qreal pixelsY);
409  static Point fromPx(const QPointF& pixels);
410 
411  // Operators
412  Point& operator=(const Point& rhs) {
413  mX = rhs.mX;
414  mY = rhs.mY;
415  return *this;
416  }
417  Point& operator+=(const Point& rhs) {
418  mX += rhs.mX;
419  mY += rhs.mY;
420  return *this;
421  }
422  Point& operator-=(const Point& rhs) {
423  mX -= rhs.mX;
424  mY -= rhs.mY;
425  return *this;
426  }
427  Point& operator*=(const Point& rhs) {
428  mX *= rhs.mX;
429  mY *= rhs.mY;
430  return *this;
431  }
433  mX *= rhs;
434  mY *= rhs;
435  return *this;
436  }
437  Point& operator/=(const Point& rhs) {
438  mX /= rhs.mX;
439  mY /= rhs.mY;
440  return *this;
441  }
443  mX /= rhs;
444  mY /= rhs;
445  return *this;
446  }
447  Point operator+(const Point& rhs) const {
448  return Point(mX + rhs.mX, mY + rhs.mY);
449  }
450  Point operator-() const { return Point(-mX, -mY); }
451  Point operator-(const Point& rhs) const {
452  return Point(mX - rhs.mX, mY - rhs.mY);
453  }
454  Point operator*(const Length& rhs) const { return Point(mX * rhs, mY * rhs); }
455  Point operator*(LengthBase_t rhs) const { return Point(mX * rhs, mY * rhs); }
456  Point operator/(const Length& rhs) const { return Point(mX / rhs, mY / rhs); }
457  Point operator/(LengthBase_t rhs) const { return Point(mX / rhs, mY / rhs); }
458  Point operator%(const Length& rhs) const { return Point(mX % rhs, mY % rhs); }
459  bool operator==(const Point& rhs) const {
460  return (mX == rhs.mX) && (mY == rhs.mY);
461  }
462  bool operator!=(const Point& rhs) const {
463  return (mX != rhs.mX) || (mY != rhs.mY);
464  }
465 
466 private:
469 };
470 
471 /*******************************************************************************
472  * Non-Member Functions
473  ******************************************************************************/
474 
475 QDataStream& operator<<(QDataStream& stream, const Point& point);
476 QDebug operator<<(QDebug stream, const Point& point);
477 
478 inline uint qHash(const Point& key, uint seed = 0) noexcept {
479  return ::qHash(qMakePair(key.getX(), key.getY()), seed);
480 }
481 
482 /*******************************************************************************
483  * End of File
484  ******************************************************************************/
485 
486 } // namespace librepcb
487 
488 Q_DECLARE_METATYPE(librepcb::Point)
489 
490 #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:276
Point operator+(const Point &rhs) const
Definition: point.h:447
const Length & getX() const noexcept
Get the X coordinate.
Definition: point.h:199
QDataStream & operator<<(QDataStream &stream, const AttributeKey &obj)
Definition: attributekey.h:114
void setYmm(const QString &mm)
Set the Y coordinate from a string in millimeters.
Definition: point.h:148
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:187
Point(const Point &point) noexcept
Copy Constructor.
Definition: point.h:97
Point & operator*=(LengthBase_t rhs)
Definition: point.h:432
static Point fromMil(qreal milsX, qreal milsY)
Definition: point.cpp:159
const Length & getY() const noexcept
Get the Y coordinate.
Definition: point.h:206
void setXmm(const QString &mm)
Set the X coordinate from a string in millimeters.
Definition: point.h:138
void setLengthNm(LengthBase_t nanometers) noexcept
Set the length in nanometers.
Definition: length.h:126
Point operator*(LengthBase_t rhs) const
Definition: point.h:455
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:233
LengthBase_t toNm() const noexcept
Get the length in nanometers.
Definition: length.h:216
Point operator/(const Length &rhs) const
Definition: point.h:456
The Angle class is used to represent an angle (for example 12.75 degrees)
Definition: angle.h:81
Length mY
the Y coordinate
Definition: point.h:468
Point & operator+=(const Point &rhs)
Definition: point.h:417
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:112
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:46
Point operator%(const Length &rhs) const
Definition: point.h:458
Point & operator-=(const Point &rhs)
Definition: point.h:422
The Point class is used to represent a point/coordinate/vector, for example (1.2mm; 5...
Definition: point.h:81
Point operator-(const Point &rhs) const
Definition: point.h:451
qreal toMil() const noexcept
Get the length in mils (1/1000 inches)
Definition: length.h:264
Length mX
the X coordinate
Definition: point.h:467
void setLengthPx(qreal pixels)
Set the length in pixels (from QGraphics* objects)
Definition: length.h:207
void setPointNm(LengthBase_t nmX, LengthBase_t nmY) noexcept
Definition: point.h:153
void setPointInch(const QPointF &inches)
Definition: point.h:169
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:247
void setY(const Length &y) noexcept
Set the Y coordinate.
Definition: point.h:128
Point(const Length &x, const Length &y) noexcept
Constructor for passing two Length objects.
Definition: point.h:105
Point() noexcept
Default Constructor.
Definition: point.h:90
void setX(const Length &x) noexcept
Set the X coordinate.
Definition: point.h:121
void setPointMil(const QPointF &mils)
Definition: point.h:177
Point & operator*=(const Point &rhs)
Definition: point.h:427
Point & operator/=(const Point &rhs)
Definition: point.h:437
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:175
Point operator-() const
Definition: point.h:450
bool operator!=(const Point &rhs) const
Definition: point.h:462
void setPointMm(const QPointF &millimeters)
Definition: point.h:161
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:783
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:214
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:190
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:85
uint qHash(const Point &key, uint seed=0) noexcept
Definition: point.h:478
qreal toInch() const noexcept
Get the length in inches.
Definition: length.h:255
void setLengthMm(qreal millimeters)
Set the length in millimeters.
Definition: length.h:143
bool operator==(const Point &rhs) const
Definition: point.h:459
Point & operator/=(LengthBase_t rhs)
Definition: point.h:442
Point operator/(LengthBase_t rhs) const
Definition: point.h:457
Point operator*(const Length &rhs) const
Definition: point.h:454
Point & makeAbs() noexcept
Make both coordinates absolute (X,Y >= 0)
Definition: point.cpp:56
The SExpression class.
Definition: sexpression.h:60
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:124