LibrePCB Developers Documentation
Loading...
Searching...
No Matches
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 ******************************************************************************/
31namespace librepcb {
32
33/*******************************************************************************
34 * Class Angle
35 ******************************************************************************/
36
76class Angle {
77 Q_DECLARE_TR_FUNCTIONS(Angle)
78
79public:
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 const qreal value = std::fmod(degrees * 1e6, 360e6);
131 // Note: Don't use qRound() because in Qt5 it is implemented strange.
132 setAngleMicroDeg((value >= 0.0) ? qint64(value + qreal(0.5))
133 : qint64(value - qreal(0.5)));
134 }
135
146 void setAngleDeg(const QString& degrees) {
147 mMicrodegrees = degStringToMicrodeg(degrees) % 360000000;
148 }
149
161 void setAngleRad(qreal radians) noexcept {
162 setAngleDeg(radians * (180.0 / M_PI));
163 }
164
165 // Conversions
166
172 qint32 toMicroDeg() const noexcept { return mMicrodegrees; }
173
179 qreal toDeg() const noexcept {
180 return static_cast<qreal>(mMicrodegrees) / 1e6;
181 }
182
190 QString toDegString() const noexcept;
191
197 qreal toRad() const noexcept { return toDeg() / (180.0 / M_PI); }
198
199 // General Methods
200
208 Angle abs() const noexcept;
209
217 Angle& makeAbs() noexcept;
218
230 Angle inverted() const noexcept;
231
243 Angle& invert() noexcept;
244
257 Angle rounded(const Angle& interval) const noexcept;
258
271 Angle& round(const Angle& interval) noexcept;
272
280 Angle mappedTo0_360deg() const noexcept;
281
289 Angle& mapTo0_360deg() noexcept;
290
298 Angle mappedTo180deg() const noexcept;
299
307 Angle& mapTo180deg() noexcept;
308
309 // Static Methods
310
318 static Angle fromDeg(qreal degrees) noexcept;
319
337 static Angle fromDeg(const QString& degrees);
338
346 static Angle fromRad(qreal radians) noexcept;
347
348 // Static Methods to create often used angles
349 static Angle deg0() noexcept { return Angle(0); }
350 static Angle deg45() noexcept { return Angle(45000000); }
351 static Angle deg90() noexcept { return Angle(90000000); }
352 static Angle deg135() noexcept { return Angle(135000000); }
353 static Angle deg180() noexcept { return Angle(180000000); }
354 static Angle deg225() noexcept { return Angle(225000000); }
355 static Angle deg270() noexcept { return Angle(270000000); }
356 static Angle deg315() noexcept { return Angle(315000000); }
357
358 // Operators
359 Angle& operator=(const Angle& rhs) {
361 return *this;
362 }
363 Angle& operator+=(const Angle& rhs) {
364 mMicrodegrees = (mMicrodegrees + rhs.mMicrodegrees) % 360000000;
365 return *this;
366 }
367 Angle& operator-=(const Angle& rhs) {
368 mMicrodegrees = (mMicrodegrees - rhs.mMicrodegrees) % 360000000;
369 return *this;
370 }
371 Angle operator+(const Angle& rhs) const {
372 return Angle(mMicrodegrees + rhs.mMicrodegrees);
373 }
374 Angle operator-() const { return Angle(-mMicrodegrees); }
375 Angle operator-(const Angle& rhs) const {
376 return Angle(mMicrodegrees - rhs.mMicrodegrees);
377 }
378 Angle operator*(const Angle& rhs) const {
379 return Angle(mMicrodegrees * rhs.mMicrodegrees);
380 }
381 Angle operator*(qint32 rhs) const { return Angle(mMicrodegrees * rhs); }
382 Angle operator/(const Angle& rhs) const {
383 return Angle(mMicrodegrees / rhs.mMicrodegrees);
384 }
385 Angle operator/(qint32 rhs) const { return Angle(mMicrodegrees / rhs); }
386 Angle operator%(const Angle& rhs) const {
387 return Angle(mMicrodegrees % rhs.mMicrodegrees);
388 }
389 bool operator>(const Angle& rhs) const {
390 return mMicrodegrees > rhs.mMicrodegrees;
391 }
392 bool operator>(qint32 rhs) const { return mMicrodegrees > rhs; }
393 bool operator<(const Angle& rhs) const {
394 return mMicrodegrees < rhs.mMicrodegrees;
395 }
396 bool operator<(qint32 rhs) const { return mMicrodegrees < rhs; }
397 bool operator>=(const Angle& rhs) const {
398 return mMicrodegrees >= rhs.mMicrodegrees;
399 }
400 bool operator>=(qint32 rhs) const { return mMicrodegrees >= rhs; }
401 bool operator<=(const Angle& rhs) const {
402 return mMicrodegrees <= rhs.mMicrodegrees;
403 }
404 bool operator<=(qint32 rhs) const { return mMicrodegrees <= rhs; }
405 bool operator==(const Angle& rhs) const {
406 return mMicrodegrees == rhs.mMicrodegrees;
407 }
408 bool operator==(qint32 rhs) const { return mMicrodegrees == rhs; }
409 bool operator!=(const Angle& rhs) const {
410 return mMicrodegrees != rhs.mMicrodegrees;
411 }
412 bool operator!=(qint32 rhs) const { return mMicrodegrees != rhs; }
413 explicit operator bool() const { return mMicrodegrees != 0; }
414
415private:
416 // Private Static Functions
417
430 static qint32 degStringToMicrodeg(const QString& degrees);
431
432 // Private Member Variables
434};
435
436/*******************************************************************************
437 * Non-Member Functions
438 ******************************************************************************/
439
440inline QDataStream& operator<<(QDataStream& stream, const Angle& angle) {
441 stream << angle.toDeg();
442 return stream;
443}
444
445inline QDebug operator<<(QDebug stream, const Angle& angle) {
446 stream << QString("Angle(%1°)").arg(angle.toDeg());
447 return stream;
448}
449
450inline std::size_t qHash(const Angle& key, std::size_t seed = 0) noexcept {
451 return ::qHash(key.toMicroDeg(), seed);
452}
453
454/*******************************************************************************
455 * Class Angle3D
456 ******************************************************************************/
457
458using Angle3D = std::tuple<Angle, Angle, Angle>;
459
460QDebug operator<<(QDebug stream, const Angle3D& obj);
461
462/*******************************************************************************
463 * End of File
464 ******************************************************************************/
465
466} // namespace librepcb
467
468Q_DECLARE_METATYPE(librepcb::Angle)
469
470#endif
The Angle class is used to represent an angle (for example 12.75 degrees)
Definition angle.h:76
static Angle deg0() noexcept
0 degrees
Definition angle.h:349
Angle operator-(const Angle &rhs) const
Definition angle.h:375
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:382
bool operator>=(const Angle &rhs) const
Definition angle.h:397
static Angle deg180() noexcept
180 degrees
Definition angle.h:353
Angle & operator-=(const Angle &rhs)
Definition angle.h:367
qint32 toMicroDeg() const noexcept
Get the angle in microdegrees.
Definition angle.h:172
bool operator!=(qint32 rhs) const
Definition angle.h:412
void setAngleDeg(qreal degrees) noexcept
Set the angle in degrees.
Definition angle.h:129
Angle & operator+=(const Angle &rhs)
Definition angle.h:363
bool operator>(const Angle &rhs) const
Definition angle.h:389
static Angle deg315() noexcept
315 degrees
Definition angle.h:356
bool operator<=(qint32 rhs) const
Definition angle.h:404
bool operator<(qint32 rhs) const
Definition angle.h:396
Angle & operator=(const Angle &rhs)
Definition angle.h:359
~Angle() noexcept
Destructor.
Definition angle.h:105
Angle & makeAbs() noexcept
Make the angle absolute (mMicrodegrees >= 0)
Definition angle.cpp:55
Angle operator+(const Angle &rhs) const
Definition angle.h:371
static Angle deg45() noexcept
45 degrees
Definition angle.h:350
Angle operator/(qint32 rhs) const
Definition angle.h:385
bool operator==(qint32 rhs) const
Definition angle.h:408
bool operator>(qint32 rhs) const
Definition angle.h:392
qreal toRad() const noexcept
Get the angle in radians.
Definition angle.h:197
static Angle deg135() noexcept
135 degrees
Definition angle.h:352
void setAngleMicroDeg(qint32 microdegrees) noexcept
Set the angle in microdegrees.
Definition angle.h:114
Angle & mapTo180deg() noexcept
Map this Angle object to [-180..+180[ degrees.
Definition angle.cpp:110
static Angle deg270() noexcept
270 degrees
Definition angle.h:355
void setAngleRad(qreal radians) noexcept
Set the angle in radians.
Definition angle.h:161
Angle abs() const noexcept
Get an Angle object with absolute value (mMicrodegrees >= 0)
Definition angle.cpp:49
static Angle deg90() noexcept
90 degrees
Definition angle.h:351
Angle & round(const Angle &interval) noexcept
Round the angle to a given interval.
Definition angle.cpp:81
Angle inverted() const noexcept
Get an Angle object with inverted value.
Definition angle.cpp:60
bool operator>=(qint32 rhs) const
Definition angle.h:400
Angle & mapTo0_360deg() noexcept
Map this Angle object to [0..360[ degrees.
Definition angle.cpp:99
static Angle fromRad(qreal radians) noexcept
Get an Angle object with a specific angle.
Definition angle.cpp:132
Angle operator-() const
Definition angle.h:374
Angle(const Angle &angle) noexcept
Copy Constructor.
Definition angle.h:92
bool operator==(const Angle &rhs) const
Definition angle.h:405
bool operator!=(const Angle &rhs) const
Definition angle.h:409
Angle operator*(qint32 rhs) const
Definition angle.h:381
Angle operator*(const Angle &rhs) const
Definition angle.h:378
Angle & invert() noexcept
Invert the angle.
Definition angle.cpp:66
Angle rounded(const Angle &interval) const noexcept
Get an Angle object rounded to a given interval.
Definition angle.cpp:75
qreal toDeg() const noexcept
Get the Angle in degrees.
Definition angle.h:179
Angle operator%(const Angle &rhs) const
Definition angle.h:386
Angle(qint32 microdegrees) noexcept
Constructor with an angle in microdegrees.
Definition angle.h:99
QString toDegString() const noexcept
Get the angle in degrees as a QString.
Definition angle.cpp:45
static qint32 degStringToMicrodeg(const QString &degrees)
Convert an angle from a QString (in degrees) to an integer (in microdegrees)
Definition angle.cpp:140
Angle() noexcept
Default Constructor.
Definition angle.h:85
bool operator<(const Angle &rhs) const
Definition angle.h:393
Angle mappedTo180deg() const noexcept
Get an Angle object which is mapped to [-180..+180[ degrees.
Definition angle.cpp:104
qint32 mMicrodegrees
the angle in microdegrees
Definition angle.h:433
static Angle deg225() noexcept
225 degrees
Definition angle.h:354
bool operator<=(const Angle &rhs) const
Definition angle.h:401
void setAngleDeg(const QString &degrees)
Set the angle in degrees, represented in a QString.
Definition angle.h:146
Angle mappedTo0_360deg() const noexcept
Get an Angle object which is mapped to [0..360[ degrees.
Definition angle.cpp:93
Definition occmodel.cpp:76
std::size_t qHash(const AttributeKey &key, std::size_t seed=0) noexcept
Definition attributekey.h:118
std::tuple< Angle, Angle, Angle > Angle3D
Definition angle.h:458
QDataStream & operator<<(QDataStream &stream, const AttributeKey &obj)
Definition attributekey.h:108