swift
Loading...
Searching...
No Matches
angle.cpp
1// SPDX-FileCopyrightText: Copyright (C) 2013 swift Project Community / Contributors
2// SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3
4#include "misc/pq/angle.h"
5
6#include <cmath>
7
8#include "misc/iconlist.h"
9#include "misc/icons.h"
10#include "misc/math/constants.h"
11#include "misc/math/mathutils.h"
12
13using namespace swift::misc::math;
14
15namespace swift::misc::physical_quantities
16{
17 CAngle::CAngle(int degrees, int minutes, double seconds)
18 : CPhysicalQuantity(degrees + minutes / 100.0 + seconds / 10000.0, CAngleUnit::sexagesimalDeg())
19 {
20 Q_ASSERT_X((degrees >= 0 && minutes >= 0 && seconds >= 0) || (degrees <= 0 && minutes <= 0 && seconds <= 0),
21 Q_FUNC_INFO, "Same sign required");
22 }
23
24 CAngle::CAngle(int degrees, double minutes)
25 : CPhysicalQuantity(degrees + minutes / 100.0, CAngleUnit::sexagesimalDeg())
26 {
27 Q_ASSERT_X((degrees >= 0 && minutes >= 0) || (degrees <= 0 && minutes <= 0), Q_FUNC_INFO, "Same sign required");
28 }
29
30 void CAngle::unifySign(int degrees, int &minutes, double &seconds)
31 {
32 minutes = std::copysign(minutes, degrees == 0 ? minutes : degrees);
33 seconds = std::copysign(seconds, degrees == 0 ? minutes : degrees);
34 }
35
36 void CAngle::unifySign(int degrees, int &minutes)
37 {
38 if (degrees == 0)
39 {
40 return;
41 }
42 minutes = std::copysign(minutes, degrees);
43 }
44
45 CIcons::IconIndex CAngle::toIcon() const { return CIcons::StandardIconArrowMediumNorth16; }
46
48 {
49 double dms = this->value(CAngleUnit::sexagesimalDeg());
50
51 if (range180Degrees)
52 {
53 dms = std::fmod(dms + 180.0, 360.0);
54 dms += (dms < 0) ? 180.0 : -180.0;
55 }
56
58 if (dms < 0)
59 {
60 values.sign = -1;
61 dms *= -1.0;
62 }
63
64 QString str = QStringLiteral("%1").arg(dms, 14, 'f', 10, '0'); // 000.0000000000
65 values.deg = QStringView { str }.mid(0, 3).toInt();
66 values.min = QStringView { str }.mid(4, 2).toInt();
67 values.sec = QStringView { str }.mid(6, 2).toInt();
68 values.fractionalSec = QStringView { str }.mid(8, 6).toInt() / 1000000.0;
69 return values;
70 }
71
72 double CAngle::piFactor() const { return math::CMathUtils::round(this->value(CAngleUnit::rad()) / c_pi, 6); }
73
74 const double &CAngle::PI() { return c_pi; }
75
76 double CAngle::sin() const { return std::sin(this->value(CAngleUnit::rad())); }
77
78 double CAngle::cos() const { return std::cos(this->value(CAngleUnit::rad())); }
79
80 double CAngle::tan() const { return std::tan(this->value(CAngleUnit::rad())); }
81
83 {
84 const double v = normalizeDegrees180(this->value(CAngleUnit::deg()));
85 const CAngleUnit u = this->getUnit();
86 *this = CAngle(v, CAngleUnit::deg());
87 this->switchUnit(u);
88 }
89
91 {
92 const double v = normalizeDegrees360(this->value(CAngleUnit::deg()));
93 const CAngleUnit u = this->getUnit();
94 *this = CAngle(v, CAngleUnit::deg());
95 this->switchUnit(u);
96 }
97
99 {
100 CAngle copy(*this);
102 return copy;
103 }
104
106 {
107 CAngle copy(*this);
109 return copy;
110 }
111
112 double CAngle::normalizeDegrees180(double degrees, int roundDigits)
113 {
114 double d = CMathUtils::normalizeDegrees360(degrees + 180.0) - 180.0;
115 if (d <= -180.0)
116 {
117 d = 180.0;
118 } // -180 -> 180
119 return roundDigits < 0 ? d : CMathUtils::round(d, roundDigits);
120 }
121
122 double CAngle::normalizeDegrees360(double degrees, int roundDigits)
123 {
124 const double d = CMathUtils::normalizeDegrees360(degrees);
125 return roundDigits < 0 ? d : CMathUtils::round(d, roundDigits);
126 }
127} // namespace swift::misc::physical_quantities
IconIndex
Index for each icon, allows to send them via DBus, efficiently store them, etc.
Definition icons.h:32
static double round(double value, int digits)
Utility round method.
Definition mathutils.cpp:18
CAngle()
Default constructor.
Definition angle.h:26
double piFactor() const
Value as factor of PI (e.g. 0.5PI).
Definition angle.cpp:72
CAngle normalizedToPlusMinus180Degrees() const
As [-179.99, 180.0] normalized angle.
Definition angle.cpp:98
static void unifySign(int degrees, int &minutes, double &seconds)
Minutes and secods will get same sign as degrees.
Definition angle.cpp:30
DegMinSecFractionalSec asSexagesimalDegMinSec(bool range180Degrees=false) const
As individual values.
Definition angle.cpp:47
double cos() const
Cosine of angle.
Definition angle.cpp:78
double sin() const
Sine of angle.
Definition angle.cpp:76
double tan() const
Tangent of angle.
Definition angle.cpp:80
void normalizeTo360Degrees()
Normalize to 0-360, [0, 359,99].
Definition angle.cpp:90
static const double & PI()
PI as convenience method.
Definition angle.cpp:74
static double normalizeDegrees180(double degrees, int roundDigits=-1)
Normalize: -180< degrees ≤180.
Definition angle.cpp:112
void normalizeToPlusMinus180Degrees()
Normalize to +- 180deg, [-179.99, 180.0].
Definition angle.cpp:82
CAngle normalizedTo360Degrees() const
As [0, 359.99] normalized angle.
Definition angle.cpp:105
swift::misc::CIcons::IconIndex toIcon() const
As icon, not implemented by all classes.
Definition angle.cpp:45
static double normalizeDegrees360(double degrees, int roundDigits=-1)
Normalize: 0≤ degrees <360.
Definition angle.cpp:122
Specialized class for angles (degrees, radian).
Definition units.h:231
static CAngleUnit rad()
Radians.
Definition units.h:268
static CAngleUnit deg()
Degrees.
Definition units.h:276
static CAngleUnit sexagesimalDeg()
Sexagesimal degree (degrees, minutes, seconds, decimal seconds).
Definition units.h:285
static double round(double value, int digits)
Utility round method.
Definition mathutils.cpp:18
static double normalizeDegrees360(double degrees)
Normalize: 0≤ degrees <360.
Definition mathutils.cpp:65
constexpr double c_pi
PI.
Definition constants.h:14
QString arg(Args &&... args) const const
QStringView mid(qsizetype start, qsizetype length) const const
int toInt(bool *ok, int base) const const