LibrePCB Developers Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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_ANGLE_H
21 #define LIBREPCB_ANGLE_H
22 
23 /*******************************************************************************
24  * Includes
25  ******************************************************************************/
26 #include "../fileio/sexpression.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 
134  void setAngleDeg(qreal degrees) noexcept {
135  mMicrodegrees = fmod(degrees * 1e6, 360e6);
136  }
137 
148  void setAngleDeg(const QString& degrees) {
149  mMicrodegrees = degStringToMicrodeg(degrees) % 360000000;
150  }
151 
166  void setAngleRad(qreal radians) noexcept {
167  mMicrodegrees = fmod(radians * 180e6 / (qreal)M_PI, 360e6);
168  }
169 
170  // Conversions
171 
177  qint32 toMicroDeg() const noexcept { return mMicrodegrees; }
178 
184  qreal toDeg() const noexcept { return (qreal)mMicrodegrees / 1e6; }
185 
193  QString toDegString() const noexcept;
194 
200  qreal toRad() const noexcept {
201  return (qreal)mMicrodegrees * (qreal)M_PI / 180e6;
202  }
203 
204  // General Methods
205 
213  Angle abs() const noexcept;
214 
222  Angle& makeAbs() noexcept;
223 
231  Angle mappedTo0_360deg() const noexcept;
232 
240  Angle& mapTo0_360deg() noexcept;
241 
249  Angle mappedTo180deg() const noexcept;
250 
258  Angle& mapTo180deg() noexcept;
259 
260  // Static Methods
261 
269  static Angle fromDeg(qreal degrees) noexcept;
270 
288  static Angle fromDeg(const QString& degrees);
289 
297  static Angle fromRad(qreal radians) noexcept;
298 
299  // Static Methods to create often used angles
300  static Angle deg0() noexcept { return Angle(0); }
301  static Angle deg45() noexcept { return Angle(45000000); }
302  static Angle deg90() noexcept { return Angle(90000000); }
303  static Angle deg135() noexcept { return Angle(135000000); }
304  static Angle deg180() noexcept { return Angle(180000000); }
305  static Angle deg225() noexcept { return Angle(225000000); }
306  static Angle deg270() noexcept { return Angle(270000000); }
307  static Angle deg315() noexcept { return Angle(315000000); }
308 
309  // Operators
310  Angle& operator=(const Angle& rhs) {
312  return *this;
313  }
314  Angle& operator+=(const Angle& rhs) {
315  mMicrodegrees = (mMicrodegrees + rhs.mMicrodegrees) % 360000000;
316  return *this;
317  }
318  Angle& operator-=(const Angle& rhs) {
319  mMicrodegrees = (mMicrodegrees - rhs.mMicrodegrees) % 360000000;
320  return *this;
321  }
322  Angle operator+(const Angle& rhs) const {
323  return Angle(mMicrodegrees + rhs.mMicrodegrees);
324  }
325  Angle operator-() const { return Angle(-mMicrodegrees); }
326  Angle operator-(const Angle& rhs) const {
327  return Angle(mMicrodegrees - rhs.mMicrodegrees);
328  }
329  Angle operator*(const Angle& rhs) const {
330  return Angle(mMicrodegrees * rhs.mMicrodegrees);
331  }
332  Angle operator*(qint32 rhs) const { return Angle(mMicrodegrees * rhs); }
333  Angle operator/(const Angle& rhs) const {
334  return Angle(mMicrodegrees / rhs.mMicrodegrees);
335  }
336  Angle operator/(qint32 rhs) const { return Angle(mMicrodegrees / rhs); }
337  Angle operator%(const Angle& rhs) const {
338  return Angle(mMicrodegrees % rhs.mMicrodegrees);
339  }
340  bool operator>(const Angle& rhs) const {
341  return mMicrodegrees > rhs.mMicrodegrees;
342  }
343  bool operator>(qint32 rhs) const { return mMicrodegrees > rhs; }
344  bool operator<(const Angle& rhs) const {
345  return mMicrodegrees < rhs.mMicrodegrees;
346  }
347  bool operator<(qint32 rhs) const { return mMicrodegrees < rhs; }
348  bool operator>=(const Angle& rhs) const {
349  return mMicrodegrees >= rhs.mMicrodegrees;
350  }
351  bool operator>=(qint32 rhs) const { return mMicrodegrees >= rhs; }
352  bool operator<=(const Angle& rhs) const {
353  return mMicrodegrees <= rhs.mMicrodegrees;
354  }
355  bool operator<=(qint32 rhs) const { return mMicrodegrees <= rhs; }
356  bool operator==(const Angle& rhs) const {
357  return mMicrodegrees == rhs.mMicrodegrees;
358  }
359  bool operator==(qint32 rhs) const { return mMicrodegrees == rhs; }
360  bool operator!=(const Angle& rhs) const {
361  return mMicrodegrees != rhs.mMicrodegrees;
362  }
363  bool operator!=(qint32 rhs) const { return mMicrodegrees != rhs; }
364  explicit operator bool() const { return mMicrodegrees != 0; }
365 
366 private:
367  // Private Static Functions
368 
381  static qint32 degStringToMicrodeg(const QString& degrees);
382 
383  // Private Member Variables
384  qint32 mMicrodegrees;
385 };
386 
387 /*******************************************************************************
388  * Non-Member Functions
389  ******************************************************************************/
390 
391 template <>
394 }
395 
396 template <>
398  bool throwIfEmpty) {
399  QString str = sexpr.getStringOrToken(throwIfEmpty);
400  return Angle::fromDeg(str); // can throw
401 }
402 
403 inline QDataStream& operator<<(QDataStream& stream, const Angle& angle) {
404  stream << angle.toDeg();
405  return stream;
406 }
407 
408 inline QDebug operator<<(QDebug stream, const Angle& angle) {
409  stream << QString("Angle(%1°)").arg(angle.toDeg());
410  return stream;
411 }
412 
413 inline uint qHash(const Angle& key, uint seed = 0) noexcept {
414  return ::qHash(key.toMicroDeg(), seed);
415 }
416 
417 /*******************************************************************************
418  * End of File
419  ******************************************************************************/
420 
421 } // namespace librepcb
422 
423 #endif // LIBREPCB_ANGLE_H
static Angle deg180() noexcept
180 degrees
Definition: angle.h:304
bool operator>=(qint32 rhs) const
Definition: angle.h:351
Angle operator/(qint32 rhs) const
Definition: angle.h:336
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:66
uint qHash(const Angle &key, uint seed=0) noexcept
Definition: angle.h:413
bool operator<(const Angle &rhs) const
Definition: angle.h:344
QString toDegString() const noexcept
Get the angle in degrees as a QString.
Definition: angle.cpp:40
static Angle deg90() noexcept
90 degrees
Definition: angle.h:302
Angle & operator+=(const Angle &rhs)
Definition: angle.h:314
qreal toRad() const noexcept
Get the angle in radians.
Definition: angle.h:200
void setAngleDeg(qreal degrees) noexcept
Set the angle in degrees.
Definition: angle.h:134
Angle & makeAbs() noexcept
Make the angle absolute (mMicrodegrees >= 0)
Definition: angle.cpp:50
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:184
static Angle fromDeg(qreal degrees) noexcept
Get an Angle object with a specific angle.
Definition: angle.cpp:82
bool operator==(const Angle &rhs) const
Definition: angle.h:356
Angle operator-(const Angle &rhs) const
Definition: angle.h:326
bool operator!=(const Angle &rhs) const
Definition: angle.h:360
~Angle() noexcept
Destructor.
Definition: angle.h:107
static Angle deg135() noexcept
135 degrees
Definition: angle.h:303
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
const QString & getStringOrToken(bool throwIfEmpty=false) const
Definition: sexpression.cpp:101
SExpression serializeToSExpression(const HAlign &obj)
Definition: alignment.h:76
Angle operator%(const Angle &rhs) const
Definition: angle.h:337
Angle mappedTo0_360deg() const noexcept
Get an Angle object which is mapped to [0..360[ degrees.
Definition: angle.cpp:55
HAlign deserializeFromSExpression(const SExpression &sexpr, bool throwIfEmpty)
Definition: alignment.h:90
Angle & operator=(const Angle &rhs)
Definition: angle.h:310
static SExpression createToken(const QString &token)
Definition: sexpression.cpp:280
Angle operator/(const Angle &rhs) const
Definition: angle.h:333
void setAngleRad(qreal radians) noexcept
Set the angle in radians.
Definition: angle.h:166
bool operator<=(const Angle &rhs) const
Definition: angle.h:352
Angle & mapTo0_360deg() noexcept
Map this Angle object to [0..360[ degrees.
Definition: angle.cpp:61
static Angle deg0() noexcept
0 degrees
Definition: angle.h:300
static Angle deg315() noexcept
315 degrees
Definition: angle.h:307
static qint32 degStringToMicrodeg(const QString &degrees)
Convert an angle from a QString (in degrees) to an integer (in microdegrees)
Definition: angle.cpp:102
bool operator>=(const Angle &rhs) const
Definition: angle.h:348
qint32 mMicrodegrees
the angle in microdegrees
Definition: angle.h:384
Angle operator-() const
Definition: angle.h:325
Angle(const Angle &angle) noexcept
Copy Constructor.
Definition: angle.h:94
static Angle deg225() noexcept
225 degrees
Definition: angle.h:305
static Angle fromRad(qreal radians) noexcept
Get an Angle object with a specific angle.
Definition: angle.cpp:94
bool operator>(qint32 rhs) const
Definition: angle.h:343
Angle operator+(const Angle &rhs) const
Definition: angle.h:322
Angle & mapTo180deg() noexcept
Map this Angle object to [-180..+180[ degrees.
Definition: angle.cpp:72
Angle operator*(qint32 rhs) const
Definition: angle.h:332
static Angle deg45() noexcept
45 degrees
Definition: angle.h:301
bool operator<(qint32 rhs) const
Definition: angle.h:347
bool operator<=(qint32 rhs) const
Definition: angle.h:355
Angle abs() const noexcept
Get an Angle object with absolute value (mMicrodegrees >= 0)
Definition: angle.cpp:44
bool operator>(const Angle &rhs) const
Definition: angle.h:340
void setAngleDeg(const QString &degrees)
Set the angle in degrees, represented in a QString.
Definition: angle.h:148
bool operator==(qint32 rhs) const
Definition: angle.h:359
static Angle deg270() noexcept
270 degrees
Definition: angle.h:306
Angle(qint32 microdegrees) noexcept
Constructor with an angle in microdegrees.
Definition: angle.h:101
bool operator!=(qint32 rhs) const
Definition: angle.h:363
The SExpression class.
Definition: sexpression.h:57
qint32 toMicroDeg() const noexcept
Get the angle in microdegrees.
Definition: angle.h:177
uint qHash(const AttributeKey &key, uint seed=0) noexcept
Definition: attributekey.h:119
Angle & operator-=(const Angle &rhs)
Definition: angle.h:318
Angle operator*(const Angle &rhs) const
Definition: angle.h:329