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 
81 class Angle {
82  Q_DECLARE_TR_FUNCTIONS(Angle)
83 
84 public:
85  // Constructors / Destructor
86 
90  Angle() noexcept : Angle(0) {}
91 
97  Angle(const Angle& angle) noexcept : mMicrodegrees(angle.mMicrodegrees) {}
98 
104  explicit Angle(qint32 microdegrees) noexcept
105  : mMicrodegrees(microdegrees % 360000000) {}
106 
110  ~Angle() noexcept {}
111 
112  // Setters
113 
119  void setAngleMicroDeg(qint32 microdegrees) noexcept {
120  mMicrodegrees = microdegrees % 360000000;
121  }
122 
137  void setAngleDeg(qreal degrees) noexcept {
138  mMicrodegrees = fmod(degrees * 1e6, 360e6);
139  }
140 
151  void setAngleDeg(const QString& degrees) {
152  mMicrodegrees = degStringToMicrodeg(degrees) % 360000000;
153  }
154 
169  void setAngleRad(qreal radians) noexcept {
170  mMicrodegrees = fmod(radians * 180e6 / (qreal)M_PI, 360e6);
171  }
172 
173  // Conversions
174 
180  qint32 toMicroDeg() const noexcept { return mMicrodegrees; }
181 
187  qreal toDeg() const noexcept { return (qreal)mMicrodegrees / 1e6; }
188 
196  QString toDegString() const noexcept;
197 
203  qreal toRad() const noexcept {
204  return (qreal)mMicrodegrees * (qreal)M_PI / 180e6;
205  }
206 
207  // General Methods
208 
216  Angle abs() const noexcept;
217 
225  Angle& makeAbs() noexcept;
226 
234  Angle mappedTo0_360deg() const noexcept;
235 
243  Angle& mapTo0_360deg() noexcept;
244 
252  Angle mappedTo180deg() const noexcept;
253 
261  Angle& mapTo180deg() noexcept;
262 
263  // Static Methods
264 
272  static Angle fromDeg(qreal degrees) noexcept;
273 
291  static Angle fromDeg(const QString& degrees);
292 
300  static Angle fromRad(qreal radians) noexcept;
301 
302  // Static Methods to create often used angles
303  static Angle deg0() noexcept { return Angle(0); }
304  static Angle deg45() noexcept { return Angle(45000000); }
305  static Angle deg90() noexcept { return Angle(90000000); }
306  static Angle deg135() noexcept { return Angle(135000000); }
307  static Angle deg180() noexcept { return Angle(180000000); }
308  static Angle deg225() noexcept { return Angle(225000000); }
309  static Angle deg270() noexcept { return Angle(270000000); }
310  static Angle deg315() noexcept { return Angle(315000000); }
311 
312  // Operators
313  Angle& operator=(const Angle& rhs) {
315  return *this;
316  }
317  Angle& operator+=(const Angle& rhs) {
318  mMicrodegrees = (mMicrodegrees + rhs.mMicrodegrees) % 360000000;
319  return *this;
320  }
321  Angle& operator-=(const Angle& rhs) {
322  mMicrodegrees = (mMicrodegrees - rhs.mMicrodegrees) % 360000000;
323  return *this;
324  }
325  Angle operator+(const Angle& rhs) const {
326  return Angle(mMicrodegrees + rhs.mMicrodegrees);
327  }
328  Angle operator-() const { return Angle(-mMicrodegrees); }
329  Angle operator-(const Angle& rhs) const {
330  return Angle(mMicrodegrees - rhs.mMicrodegrees);
331  }
332  Angle operator*(const Angle& rhs) const {
333  return Angle(mMicrodegrees * rhs.mMicrodegrees);
334  }
335  Angle operator*(qint32 rhs) const { return Angle(mMicrodegrees * rhs); }
336  Angle operator/(const Angle& rhs) const {
337  return Angle(mMicrodegrees / rhs.mMicrodegrees);
338  }
339  Angle operator/(qint32 rhs) const { return Angle(mMicrodegrees / rhs); }
340  Angle operator%(const Angle& rhs) const {
341  return Angle(mMicrodegrees % rhs.mMicrodegrees);
342  }
343  bool operator>(const Angle& rhs) const {
344  return mMicrodegrees > rhs.mMicrodegrees;
345  }
346  bool operator>(qint32 rhs) const { return mMicrodegrees > rhs; }
347  bool operator<(const Angle& rhs) const {
348  return mMicrodegrees < rhs.mMicrodegrees;
349  }
350  bool operator<(qint32 rhs) const { return mMicrodegrees < rhs; }
351  bool operator>=(const Angle& rhs) const {
352  return mMicrodegrees >= rhs.mMicrodegrees;
353  }
354  bool operator>=(qint32 rhs) const { return mMicrodegrees >= rhs; }
355  bool operator<=(const Angle& rhs) const {
356  return mMicrodegrees <= rhs.mMicrodegrees;
357  }
358  bool operator<=(qint32 rhs) const { return mMicrodegrees <= rhs; }
359  bool operator==(const Angle& rhs) const {
360  return mMicrodegrees == rhs.mMicrodegrees;
361  }
362  bool operator==(qint32 rhs) const { return mMicrodegrees == rhs; }
363  bool operator!=(const Angle& rhs) const {
364  return mMicrodegrees != rhs.mMicrodegrees;
365  }
366  bool operator!=(qint32 rhs) const { return mMicrodegrees != rhs; }
367  explicit operator bool() const { return mMicrodegrees != 0; }
368 
369 private:
370  // Private Static Functions
371 
384  static qint32 degStringToMicrodeg(const QString& degrees);
385 
386  // Private Member Variables
387  qint32 mMicrodegrees;
388 };
389 
390 /*******************************************************************************
391  * Non-Member Functions
392  ******************************************************************************/
393 
394 template <>
397 }
398 
399 template <>
401  bool throwIfEmpty) {
402  QString str = sexpr.getStringOrToken(throwIfEmpty);
403  return Angle::fromDeg(str); // can throw
404 }
405 
406 inline QDataStream& operator<<(QDataStream& stream, const Angle& angle) {
407  stream << angle.toDeg();
408  return stream;
409 }
410 
411 inline QDebug operator<<(QDebug stream, const Angle& angle) {
412  stream << QString("Angle(%1°)").arg(angle.toDeg());
413  return stream;
414 }
415 
416 inline uint qHash(const Angle& key, uint seed = 0) noexcept {
417  return ::qHash(key.toMicroDeg(), seed);
418 }
419 
420 /*******************************************************************************
421  * End of File
422  ******************************************************************************/
423 
424 } // namespace librepcb
425 
426 #endif // LIBREPCB_ANGLE_H
static Angle deg180() noexcept
180 degrees
Definition: angle.h:307
bool operator>=(qint32 rhs) const
Definition: angle.h:354
Angle operator/(qint32 rhs) const
Definition: angle.h:339
QDataStream & operator<<(QDataStream &stream, const AttributeKey &obj)
Definition: attributekey.h:114
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:416
bool operator<(const Angle &rhs) const
Definition: angle.h:347
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:305
Angle & operator+=(const Angle &rhs)
Definition: angle.h:317
qreal toRad() const noexcept
Get the angle in radians.
Definition: angle.h:203
void setAngleDeg(qreal degrees) noexcept
Set the angle in degrees.
Definition: angle.h:137
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:119
qreal toDeg() const noexcept
Get the Angle in degrees.
Definition: angle.h:187
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:359
Angle operator-(const Angle &rhs) const
Definition: angle.h:329
bool operator!=(const Angle &rhs) const
Definition: angle.h:363
~Angle() noexcept
Destructor.
Definition: angle.h:110
static Angle deg135() noexcept
135 degrees
Definition: angle.h:306
Angle() noexcept
Default Constructor.
Definition: angle.h:90
The Angle class is used to represent an angle (for example 12.75 degrees)
Definition: angle.h:81
const QString & getStringOrToken(bool throwIfEmpty=false) const
Definition: sexpression.cpp:101
SExpression serializeToSExpression(const HAlign &obj)
Definition: alignment.h:79
Angle operator%(const Angle &rhs) const
Definition: angle.h:340
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:93
Angle & operator=(const Angle &rhs)
Definition: angle.h:313
static SExpression createToken(const QString &token)
Definition: sexpression.cpp:274
Angle operator/(const Angle &rhs) const
Definition: angle.h:336
void setAngleRad(qreal radians) noexcept
Set the angle in radians.
Definition: angle.h:169
bool operator<=(const Angle &rhs) const
Definition: angle.h:355
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:303
static Angle deg315() noexcept
315 degrees
Definition: angle.h:310
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:351
qint32 mMicrodegrees
the angle in microdegrees
Definition: angle.h:387
Angle operator-() const
Definition: angle.h:328
Angle(const Angle &angle) noexcept
Copy Constructor.
Definition: angle.h:97
static Angle deg225() noexcept
225 degrees
Definition: angle.h:308
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:346
Angle operator+(const Angle &rhs) const
Definition: angle.h:325
Angle & mapTo180deg() noexcept
Map this Angle object to [-180..+180[ degrees.
Definition: angle.cpp:72
Angle operator*(qint32 rhs) const
Definition: angle.h:335
static Angle deg45() noexcept
45 degrees
Definition: angle.h:304
bool operator<(qint32 rhs) const
Definition: angle.h:350
bool operator<=(qint32 rhs) const
Definition: angle.h:358
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:343
void setAngleDeg(const QString &degrees)
Set the angle in degrees, represented in a QString.
Definition: angle.h:151
bool operator==(qint32 rhs) const
Definition: angle.h:362
static Angle deg270() noexcept
270 degrees
Definition: angle.h:309
Angle(qint32 microdegrees) noexcept
Constructor with an angle in microdegrees.
Definition: angle.h:104
bool operator!=(qint32 rhs) const
Definition: angle.h:366
The SExpression class.
Definition: sexpression.h:60
qint32 toMicroDeg() const noexcept
Get the angle in microdegrees.
Definition: angle.h:180
uint qHash(const AttributeKey &key, uint seed=0) noexcept
Definition: attributekey.h:124
Angle & operator-=(const Angle &rhs)
Definition: angle.h:321
Angle operator*(const Angle &rhs) const
Definition: angle.h:332