LibrePCB Developers Documentation
angle.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_ANGLE_H
21 #define LIBREPCB_CORE_ANGLE_H
22 
23 /*******************************************************************************
24  * Includes
25  ******************************************************************************/
26 #include <QtCore>
27 
28 /*******************************************************************************
29  * Namespace / Forward Declarations
30  ******************************************************************************/
31 namespace librepcb {
32 
33 /*******************************************************************************
34  * Class Angle
35  ******************************************************************************/
36 
76 class Angle {
77  Q_DECLARE_TR_FUNCTIONS(Angle)
78 
79 public:
80  // Constructors / Destructor
81 
85  Angle() noexcept : Angle(0) {}
86 
92  Angle(const Angle& angle) noexcept : mMicrodegrees(angle.mMicrodegrees) {}
93 
99  explicit Angle(qint32 microdegrees) noexcept
100  : mMicrodegrees(microdegrees % 360000000) {}
101 
105  ~Angle() noexcept {}
106 
107  // Setters
108 
114  void setAngleMicroDeg(qint32 microdegrees) noexcept {
115  mMicrodegrees = microdegrees % 360000000;
116  }
117 
129  void setAngleDeg(qreal degrees) noexcept {
130  setAngleMicroDeg(qRound(std::fmod(degrees * 1e6, 360e6)));
131  }
132 
143  void setAngleDeg(const QString& degrees) {
144  mMicrodegrees = degStringToMicrodeg(degrees) % 360000000;
145  }
146 
158  void setAngleRad(qreal radians) noexcept {
159  setAngleDeg(radians * (180.0 / M_PI));
160  }
161 
162  // Conversions
163 
169  qint32 toMicroDeg() const noexcept { return mMicrodegrees; }
170 
176  qreal toDeg() const noexcept {
177  return static_cast<qreal>(mMicrodegrees) / 1e6;
178  }
179 
187  QString toDegString() const noexcept;
188 
194  qreal toRad() const noexcept { return toDeg() / (180.0 / M_PI); }
195 
196  // General Methods
197 
205  Angle abs() const noexcept;
206 
214  Angle& makeAbs() noexcept;
215 
227  Angle inverted() const noexcept;
228 
240  Angle& invert() noexcept;
241 
254  Angle rounded(const Angle& interval) const noexcept;
255 
268  Angle& round(const Angle& interval) noexcept;
269 
277  Angle mappedTo0_360deg() const noexcept;
278 
286  Angle& mapTo0_360deg() noexcept;
287 
295  Angle mappedTo180deg() const noexcept;
296 
304  Angle& mapTo180deg() noexcept;
305 
306  // Static Methods
307 
315  static Angle fromDeg(qreal degrees) noexcept;
316 
334  static Angle fromDeg(const QString& degrees);
335 
343  static Angle fromRad(qreal radians) noexcept;
344 
345  // Static Methods to create often used angles
346  static Angle deg0() noexcept { return Angle(0); }
347  static Angle deg45() noexcept { return Angle(45000000); }
348  static Angle deg90() noexcept { return Angle(90000000); }
349  static Angle deg135() noexcept { return Angle(135000000); }
350  static Angle deg180() noexcept { return Angle(180000000); }
351  static Angle deg225() noexcept { return Angle(225000000); }
352  static Angle deg270() noexcept { return Angle(270000000); }
353  static Angle deg315() noexcept { return Angle(315000000); }
354 
355  // Operators
356  Angle& operator=(const Angle& rhs) {
358  return *this;
359  }
360  Angle& operator+=(const Angle& rhs) {
361  mMicrodegrees = (mMicrodegrees + rhs.mMicrodegrees) % 360000000;
362  return *this;
363  }
364  Angle& operator-=(const Angle& rhs) {
365  mMicrodegrees = (mMicrodegrees - rhs.mMicrodegrees) % 360000000;
366  return *this;
367  }
368  Angle operator+(const Angle& rhs) const {
369  return Angle(mMicrodegrees + rhs.mMicrodegrees);
370  }
371  Angle operator-() const { return Angle(-mMicrodegrees); }
372  Angle operator-(const Angle& rhs) const {
373  return Angle(mMicrodegrees - rhs.mMicrodegrees);
374  }
375  Angle operator*(const Angle& rhs) const {
376  return Angle(mMicrodegrees * rhs.mMicrodegrees);
377  }
378  Angle operator*(qint32 rhs) const { return Angle(mMicrodegrees * rhs); }
379  Angle operator/(const Angle& rhs) const {
380  return Angle(mMicrodegrees / rhs.mMicrodegrees);
381  }
382  Angle operator/(qint32 rhs) const { return Angle(mMicrodegrees / rhs); }
383  Angle operator%(const Angle& rhs) const {
384  return Angle(mMicrodegrees % rhs.mMicrodegrees);
385  }
386  bool operator>(const Angle& rhs) const {
387  return mMicrodegrees > rhs.mMicrodegrees;
388  }
389  bool operator>(qint32 rhs) const { return mMicrodegrees > rhs; }
390  bool operator<(const Angle& rhs) const {
391  return mMicrodegrees < rhs.mMicrodegrees;
392  }
393  bool operator<(qint32 rhs) const { return mMicrodegrees < rhs; }
394  bool operator>=(const Angle& rhs) const {
395  return mMicrodegrees >= rhs.mMicrodegrees;
396  }
397  bool operator>=(qint32 rhs) const { return mMicrodegrees >= rhs; }
398  bool operator<=(const Angle& rhs) const {
399  return mMicrodegrees <= rhs.mMicrodegrees;
400  }
401  bool operator<=(qint32 rhs) const { return mMicrodegrees <= rhs; }
402  bool operator==(const Angle& rhs) const {
403  return mMicrodegrees == rhs.mMicrodegrees;
404  }
405  bool operator==(qint32 rhs) const { return mMicrodegrees == rhs; }
406  bool operator!=(const Angle& rhs) const {
407  return mMicrodegrees != rhs.mMicrodegrees;
408  }
409  bool operator!=(qint32 rhs) const { return mMicrodegrees != rhs; }
410  explicit operator bool() const { return mMicrodegrees != 0; }
411 
412 private:
413  // Private Static Functions
414 
427  static qint32 degStringToMicrodeg(const QString& degrees);
428 
429  // Private Member Variables
430  qint32 mMicrodegrees;
431 };
432 
433 /*******************************************************************************
434  * Non-Member Functions
435  ******************************************************************************/
436 
437 inline QDataStream& operator<<(QDataStream& stream, const Angle& angle) {
438  stream << angle.toDeg();
439  return stream;
440 }
441 
442 inline QDebug operator<<(QDebug stream, const Angle& angle) {
443  stream << QString("Angle(%1°)").arg(angle.toDeg());
444  return stream;
445 }
446 
447 inline uint qHash(const Angle& key, uint seed = 0) noexcept {
448  return ::qHash(key.toMicroDeg(), seed);
449 }
450 
451 /*******************************************************************************
452  * Class Angle3D
453  ******************************************************************************/
454 
455 using Angle3D = std::tuple<Angle, Angle, Angle>;
456 
457 QDebug operator<<(QDebug stream, const Angle3D& obj);
458 
459 /*******************************************************************************
460  * End of File
461  ******************************************************************************/
462 
463 } // namespace librepcb
464 
465 Q_DECLARE_METATYPE(librepcb::Angle)
466 
467 #endif
bool operator==(const Angle &rhs) const
Definition: angle.h:402
static Angle deg180() noexcept
180 degrees
Definition: angle.h:350
QDataStream & operator<<(QDataStream &stream, const AttributeKey &obj)
Definition: attributekey.h:108
Angle mappedTo180deg() const noexcept
Get an Angle object which is mapped to [-180..+180[ degrees.
Definition: angle.cpp:104
uint qHash(const Angle &key, uint seed=0) noexcept
Definition: angle.h:447
bool operator==(qint32 rhs) const
Definition: angle.h:405
QString toDegString() const noexcept
Get the angle in degrees as a QString.
Definition: angle.cpp:45
static Angle deg90() noexcept
90 degrees
Definition: angle.h:348
Angle & round(const Angle &interval) noexcept
Round the angle to a given interval.
Definition: angle.cpp:81
bool operator>(const Angle &rhs) const
Definition: angle.h:386
Angle & operator+=(const Angle &rhs)
Definition: angle.h:360
Angle & invert() noexcept
Invert the angle.
Definition: angle.cpp:66
qreal toRad() const noexcept
Get the angle in radians.
Definition: angle.h:194
bool operator<(qint32 rhs) const
Definition: angle.h:393
Definition: occmodel.cpp:76
void setAngleDeg(qreal degrees) noexcept
Set the angle in degrees.
Definition: angle.h:129
Angle & makeAbs() noexcept
Make the angle absolute (mMicrodegrees >= 0)
Definition: angle.cpp:55
void setAngleMicroDeg(qint32 microdegrees) noexcept
Set the angle in microdegrees.
Definition: angle.h:114
qreal toDeg() const noexcept
Get the Angle in degrees.
Definition: angle.h:176
static Angle fromDeg(qreal degrees) noexcept
Get an Angle object with a specific angle.
Definition: angle.cpp:120
Angle operator+(const Angle &rhs) const
Definition: angle.h:368
Angle operator*(qint32 rhs) const
Definition: angle.h:378
~Angle() noexcept
Destructor.
Definition: angle.h:105
static Angle deg135() noexcept
135 degrees
Definition: angle.h:349
Angle() noexcept
Default Constructor.
Definition: angle.h:85
The Angle class is used to represent an angle (for example 12.75 degrees)
Definition: angle.h:76
Angle mappedTo0_360deg() const noexcept
Get an Angle object which is mapped to [0..360[ degrees.
Definition: angle.cpp:93
Angle & operator=(const Angle &rhs)
Definition: angle.h:356
void setAngleRad(qreal radians) noexcept
Set the angle in radians.
Definition: angle.h:158
Angle operator/(const Angle &rhs) const
Definition: angle.h:379
bool operator!=(qint32 rhs) const
Definition: angle.h:409
Angle & mapTo0_360deg() noexcept
Map this Angle object to [0..360[ degrees.
Definition: angle.cpp:99
static Angle deg0() noexcept
0 degrees
Definition: angle.h:346
static Angle deg315() noexcept
315 degrees
Definition: angle.h:353
static qint32 degStringToMicrodeg(const QString &degrees)
Convert an angle from a QString (in degrees) to an integer (in microdegrees)
Definition: angle.cpp:140
Angle operator*(const Angle &rhs) const
Definition: angle.h:375
bool operator!=(const Angle &rhs) const
Definition: angle.h:406
qint32 mMicrodegrees
the angle in microdegrees
Definition: angle.h:430
bool operator>=(const Angle &rhs) const
Definition: angle.h:394
Angle operator/(qint32 rhs) const
Definition: angle.h:382
Angle(const Angle &angle) noexcept
Copy Constructor.
Definition: angle.h:92
static Angle deg225() noexcept
225 degrees
Definition: angle.h:351
Angle operator-() const
Definition: angle.h:371
static Angle fromRad(qreal radians) noexcept
Get an Angle object with a specific angle.
Definition: angle.cpp:132
bool operator<=(qint32 rhs) const
Definition: angle.h:401
Angle & mapTo180deg() noexcept
Map this Angle object to [-180..+180[ degrees.
Definition: angle.cpp:110
bool operator<=(const Angle &rhs) const
Definition: angle.h:398
static Angle deg45() noexcept
45 degrees
Definition: angle.h:347
Angle abs() const noexcept
Get an Angle object with absolute value (mMicrodegrees >= 0)
Definition: angle.cpp:49
bool operator>(qint32 rhs) const
Definition: angle.h:389
void setAngleDeg(const QString &degrees)
Set the angle in degrees, represented in a QString.
Definition: angle.h:143
static Angle deg270() noexcept
270 degrees
Definition: angle.h:352
Angle(qint32 microdegrees) noexcept
Constructor with an angle in microdegrees.
Definition: angle.h:99
Angle operator-(const Angle &rhs) const
Definition: angle.h:372
bool operator>=(qint32 rhs) const
Definition: angle.h:397
Angle inverted() const noexcept
Get an Angle object with inverted value.
Definition: angle.cpp:60
std::tuple< Angle, Angle, Angle > Angle3D
Definition: angle.h:455
Angle rounded(const Angle &interval) const noexcept
Get an Angle object rounded to a given interval.
Definition: angle.cpp:75
qint32 toMicroDeg() const noexcept
Get the angle in microdegrees.
Definition: angle.h:169
uint qHash(const AttributeKey &key, uint seed=0) noexcept
Definition: attributekey.h:118
Angle & operator-=(const Angle &rhs)
Definition: angle.h:364
Angle operator%(const Angle &rhs) const
Definition: angle.h:383
bool operator<(const Angle &rhs) const
Definition: angle.h:390