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 "../qtcompat.h"
27 
28 #include <QtCore>
29 
30 /*******************************************************************************
31  * Namespace / Forward Declarations
32  ******************************************************************************/
33 namespace librepcb {
34 
35 /*******************************************************************************
36  * Class Angle
37  ******************************************************************************/
38 
78 class Angle {
79  Q_DECLARE_TR_FUNCTIONS(Angle)
80 
81 public:
82  // Constructors / Destructor
83 
87  Angle() noexcept : Angle(0) {}
88 
94  Angle(const Angle& angle) noexcept : mMicrodegrees(angle.mMicrodegrees) {}
95 
101  explicit Angle(qint32 microdegrees) noexcept
102  : mMicrodegrees(microdegrees % 360000000) {}
103 
107  ~Angle() noexcept {}
108 
109  // Setters
110 
116  void setAngleMicroDeg(qint32 microdegrees) noexcept {
117  mMicrodegrees = microdegrees % 360000000;
118  }
119 
131  void setAngleDeg(qreal degrees) noexcept {
132  const qreal value = std::fmod(degrees * 1e6, 360e6);
133  // Note: Don't use qRound() because in Qt5 it is implemented strange.
134  setAngleMicroDeg((value >= 0.0) ? qint64(value + qreal(0.5))
135  : qint64(value - qreal(0.5)));
136  }
137 
148  void setAngleDeg(const QString& degrees) {
149  mMicrodegrees = degStringToMicrodeg(degrees) % 360000000;
150  }
151 
163  void setAngleRad(qreal radians) noexcept {
164  setAngleDeg(radians * (180.0 / M_PI));
165  }
166 
167  // Conversions
168 
174  qint32 toMicroDeg() const noexcept { return mMicrodegrees; }
175 
181  qreal toDeg() const noexcept {
182  return static_cast<qreal>(mMicrodegrees) / 1e6;
183  }
184 
192  QString toDegString() const noexcept;
193 
199  qreal toRad() const noexcept { return toDeg() / (180.0 / M_PI); }
200 
201  // General Methods
202 
210  Angle abs() const noexcept;
211 
219  Angle& makeAbs() noexcept;
220 
232  Angle inverted() const noexcept;
233 
245  Angle& invert() noexcept;
246 
259  Angle rounded(const Angle& interval) const noexcept;
260 
273  Angle& round(const Angle& interval) noexcept;
274 
282  Angle mappedTo0_360deg() const noexcept;
283 
291  Angle& mapTo0_360deg() noexcept;
292 
300  Angle mappedTo180deg() const noexcept;
301 
309  Angle& mapTo180deg() noexcept;
310 
311  // Static Methods
312 
320  static Angle fromDeg(qreal degrees) noexcept;
321 
339  static Angle fromDeg(const QString& degrees);
340 
348  static Angle fromRad(qreal radians) noexcept;
349 
350  // Static Methods to create often used angles
351  static Angle deg0() noexcept { return Angle(0); }
352  static Angle deg45() noexcept { return Angle(45000000); }
353  static Angle deg90() noexcept { return Angle(90000000); }
354  static Angle deg135() noexcept { return Angle(135000000); }
355  static Angle deg180() noexcept { return Angle(180000000); }
356  static Angle deg225() noexcept { return Angle(225000000); }
357  static Angle deg270() noexcept { return Angle(270000000); }
358  static Angle deg315() noexcept { return Angle(315000000); }
359 
360  // Operators
361  Angle& operator=(const Angle& rhs) {
363  return *this;
364  }
365  Angle& operator+=(const Angle& rhs) {
366  mMicrodegrees = (mMicrodegrees + rhs.mMicrodegrees) % 360000000;
367  return *this;
368  }
369  Angle& operator-=(const Angle& rhs) {
370  mMicrodegrees = (mMicrodegrees - rhs.mMicrodegrees) % 360000000;
371  return *this;
372  }
373  Angle operator+(const Angle& rhs) const {
374  return Angle(mMicrodegrees + rhs.mMicrodegrees);
375  }
376  Angle operator-() const { return Angle(-mMicrodegrees); }
377  Angle operator-(const Angle& rhs) const {
378  return Angle(mMicrodegrees - rhs.mMicrodegrees);
379  }
380  Angle operator*(const Angle& rhs) const {
381  return Angle(mMicrodegrees * rhs.mMicrodegrees);
382  }
383  Angle operator*(qint32 rhs) const { return Angle(mMicrodegrees * rhs); }
384  Angle operator/(const Angle& rhs) const {
385  return Angle(mMicrodegrees / rhs.mMicrodegrees);
386  }
387  Angle operator/(qint32 rhs) const { return Angle(mMicrodegrees / rhs); }
388  Angle operator%(const Angle& rhs) const {
389  return Angle(mMicrodegrees % rhs.mMicrodegrees);
390  }
391  bool operator>(const Angle& rhs) const {
392  return mMicrodegrees > rhs.mMicrodegrees;
393  }
394  bool operator>(qint32 rhs) const { return mMicrodegrees > rhs; }
395  bool operator<(const Angle& rhs) const {
396  return mMicrodegrees < rhs.mMicrodegrees;
397  }
398  bool operator<(qint32 rhs) const { return mMicrodegrees < rhs; }
399  bool operator>=(const Angle& rhs) const {
400  return mMicrodegrees >= rhs.mMicrodegrees;
401  }
402  bool operator>=(qint32 rhs) const { return mMicrodegrees >= rhs; }
403  bool operator<=(const Angle& rhs) const {
404  return mMicrodegrees <= rhs.mMicrodegrees;
405  }
406  bool operator<=(qint32 rhs) const { return mMicrodegrees <= rhs; }
407  bool operator==(const Angle& rhs) const {
408  return mMicrodegrees == rhs.mMicrodegrees;
409  }
410  bool operator==(qint32 rhs) const { return mMicrodegrees == rhs; }
411  bool operator!=(const Angle& rhs) const {
412  return mMicrodegrees != rhs.mMicrodegrees;
413  }
414  bool operator!=(qint32 rhs) const { return mMicrodegrees != rhs; }
415  explicit operator bool() const { return mMicrodegrees != 0; }
416 
417 private:
418  // Private Static Functions
419 
432  static qint32 degStringToMicrodeg(const QString& degrees);
433 
434  // Private Member Variables
435  qint32 mMicrodegrees;
436 };
437 
438 /*******************************************************************************
439  * Non-Member Functions
440  ******************************************************************************/
441 
442 inline QDataStream& operator<<(QDataStream& stream, const Angle& angle) {
443  stream << angle.toDeg();
444  return stream;
445 }
446 
447 inline QDebug operator<<(QDebug stream, const Angle& angle) {
448  stream << QString("Angle(%1°)").arg(angle.toDeg());
449  return stream;
450 }
451 
452 inline QtCompat::Hash qHash(const Angle& key,
453  QtCompat::Hash seed = 0) noexcept {
454  return ::qHash(key.toMicroDeg(), seed);
455 }
456 
457 /*******************************************************************************
458  * Class Angle3D
459  ******************************************************************************/
460 
461 using Angle3D = std::tuple<Angle, Angle, Angle>;
462 
463 QDebug operator<<(QDebug stream, const Angle3D& obj);
464 
465 /*******************************************************************************
466  * End of File
467  ******************************************************************************/
468 
469 } // namespace librepcb
470 
471 Q_DECLARE_METATYPE(librepcb::Angle)
472 
473 #endif
QtCompat::Hash qHash(const AttributeKey &key, QtCompat::Hash seed=0) noexcept
Definition: attributekey.h:119
bool operator==(const Angle &rhs) const
Definition: angle.h:407
static Angle deg180() noexcept
180 degrees
Definition: angle.h:355
QDataStream & operator<<(QDataStream &stream, const AttributeKey &obj)
Definition: attributekey.h:109
Angle mappedTo180deg() const noexcept
Get an Angle object which is mapped to [-180..+180[ degrees.
Definition: angle.cpp:104
bool operator==(qint32 rhs) const
Definition: angle.h:410
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:353
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:391
Angle & operator+=(const Angle &rhs)
Definition: angle.h:365
Angle & invert() noexcept
Invert the angle.
Definition: angle.cpp:66
qreal toRad() const noexcept
Get the angle in radians.
Definition: angle.h:199
bool operator<(qint32 rhs) const
Definition: angle.h:398
Definition: occmodel.cpp:77
void setAngleDeg(qreal degrees) noexcept
Set the angle in degrees.
Definition: angle.h:131
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:116
qreal toDeg() const noexcept
Get the Angle in degrees.
Definition: angle.h:181
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:373
Angle operator*(qint32 rhs) const
Definition: angle.h:383
~Angle() noexcept
Destructor.
Definition: angle.h:107
static Angle deg135() noexcept
135 degrees
Definition: angle.h:354
Angle() noexcept
Default Constructor.
Definition: angle.h:87
The Angle class is used to represent an angle (for example 12.75 degrees)
Definition: angle.h:78
QtCompat::Hash qHash(const Angle &key, QtCompat::Hash seed=0) noexcept
Definition: angle.h:452
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:361
void setAngleRad(qreal radians) noexcept
Set the angle in radians.
Definition: angle.h:163
Angle operator/(const Angle &rhs) const
Definition: angle.h:384
bool operator!=(qint32 rhs) const
Definition: angle.h:414
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:351
static Angle deg315() noexcept
315 degrees
Definition: angle.h:358
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:380
bool operator!=(const Angle &rhs) const
Definition: angle.h:411
qint32 mMicrodegrees
the angle in microdegrees
Definition: angle.h:435
bool operator>=(const Angle &rhs) const
Definition: angle.h:399
Angle operator/(qint32 rhs) const
Definition: angle.h:387
Angle(const Angle &angle) noexcept
Copy Constructor.
Definition: angle.h:94
static Angle deg225() noexcept
225 degrees
Definition: angle.h:356
Angle operator-() const
Definition: angle.h:376
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:406
Angle & mapTo180deg() noexcept
Map this Angle object to [-180..+180[ degrees.
Definition: angle.cpp:110
uint Hash
Return type of Qt&#39;s qHash() function.
Definition: qtcompat.h:58
bool operator<=(const Angle &rhs) const
Definition: angle.h:403
static Angle deg45() noexcept
45 degrees
Definition: angle.h:352
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:394
void setAngleDeg(const QString &degrees)
Set the angle in degrees, represented in a QString.
Definition: angle.h:148
static Angle deg270() noexcept
270 degrees
Definition: angle.h:357
Angle(qint32 microdegrees) noexcept
Constructor with an angle in microdegrees.
Definition: angle.h:101
Angle operator-(const Angle &rhs) const
Definition: angle.h:377
bool operator>=(qint32 rhs) const
Definition: angle.h:402
Angle inverted() const noexcept
Get an Angle object with inverted value.
Definition: angle.cpp:60
std::tuple< Angle, Angle, Angle > Angle3D
Definition: angle.h:461
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:174
Angle & operator-=(const Angle &rhs)
Definition: angle.h:369
Angle operator%(const Angle &rhs) const
Definition: angle.h:388
bool operator<(const Angle &rhs) const
Definition: angle.h:395