...
This commit is contained in:
@@ -57,10 +57,17 @@ static constexpr MccHMSTag mcc_hms{};
|
||||
|
||||
class MccCoordinate
|
||||
{
|
||||
protected:
|
||||
double _angleInRads{0.0};
|
||||
int _precision{2};
|
||||
|
||||
public:
|
||||
enum norm_kind_t {
|
||||
NORM_KIND_0_360, // [0,360]
|
||||
NORM_KIND_0_180, // [0,180]
|
||||
NORM_KIND_90_90, // [-90,90]
|
||||
};
|
||||
|
||||
MccCoordinate() = default;
|
||||
|
||||
// by default angle is in radians
|
||||
@@ -102,6 +109,45 @@ public:
|
||||
}
|
||||
}
|
||||
|
||||
virtual ~MccCoordinate() = default;
|
||||
|
||||
// normalize coordinate
|
||||
template <norm_kind_t KIND>
|
||||
MccCoordinate& normalize()
|
||||
{
|
||||
_angleInRads = std::fmod(_angleInRads, std::numbers::pi * 2.0);
|
||||
|
||||
if constexpr (KIND == NORM_KIND_0_360) {
|
||||
if (_angleInRads < 0.0) {
|
||||
_angleInRads += 2.0 * std::numbers::pi;
|
||||
}
|
||||
} else if constexpr (KIND == NORM_KIND_0_180) {
|
||||
if (_angleInRads < -std::numbers::pi) {
|
||||
_angleInRads = 2.0 * std::numbers::pi + _angleInRads;
|
||||
} else if (_angleInRads < 0.0) {
|
||||
_angleInRads = -_angleInRads;
|
||||
}
|
||||
} else if constexpr (KIND == NORM_KIND_90_90) {
|
||||
if (_angleInRads >= 1.5 * std::numbers::pi) {
|
||||
_angleInRads = _angleInRads - 2.0 * std::numbers::pi;
|
||||
} else if (_angleInRads >= std::numbers::pi / 2.0) {
|
||||
_angleInRads = std::numbers::pi - _angleInRads;
|
||||
} else if (_angleInRads <= -1.5 * std::numbers::pi) {
|
||||
_angleInRads += 2.0 * std::numbers::pi;
|
||||
} else if (_angleInRads <= -std::numbers::pi / 2.0) {
|
||||
_angleInRads = -(std::numbers::pi + _angleInRads);
|
||||
}
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
MccCoordinate& normalize()
|
||||
{
|
||||
return normalize<NORM_KIND_0_360>();
|
||||
}
|
||||
|
||||
|
||||
operator double() const
|
||||
{
|
||||
return _angleInRads;
|
||||
@@ -132,7 +178,10 @@ public:
|
||||
friend MccCoordinate operator+(std::convertible_to<MccCoordinate> auto&& left,
|
||||
std::convertible_to<MccCoordinate> auto&& right)
|
||||
{
|
||||
return std::forward<decltype(left)>(left)._angleInRads + std::forward<decltype(right)>(right)._angleInRads;
|
||||
// return std::forward<decltype(left)>(left)._angleInRads +
|
||||
// std::forward<decltype(right)>(right)._angleInRads;
|
||||
return MccCoordinate{std::forward<decltype(left)>(left)}._angleInRads +
|
||||
MccCoordinate{std::forward<decltype(right)>(right)}._angleInRads;
|
||||
}
|
||||
// friend MccCoordinate operator+(const MccCoordinate& left, const MccCoordinate& right)
|
||||
// {
|
||||
@@ -143,7 +192,10 @@ public:
|
||||
friend MccCoordinate operator-(std::convertible_to<MccCoordinate> auto&& left,
|
||||
std::convertible_to<MccCoordinate> auto&& right)
|
||||
{
|
||||
return std::forward<decltype(left)>(left)._angleInRads - std::forward<decltype(right)>(right)._angleInRads;
|
||||
// return std::forward<decltype(left)>(left)._angleInRads -
|
||||
// std::forward<decltype(right)>(right)._angleInRads;
|
||||
return MccCoordinate{std::forward<decltype(left)>(left)}._angleInRads -
|
||||
MccCoordinate{std::forward<decltype(right)>(right)}._angleInRads;
|
||||
}
|
||||
// friend MccCoordinate operator-(const MccCoordinate& left, const MccCoordinate& right)
|
||||
// {
|
||||
@@ -220,5 +272,33 @@ public:
|
||||
}
|
||||
};
|
||||
|
||||
enum class MccNormCoordKind {
|
||||
NORM_KIND_0_360, // [0,360]
|
||||
NORM_KIND_0_180, // [0,180]
|
||||
NORM_KIND_90_90, // [-90,90]
|
||||
};
|
||||
|
||||
template <MccNormCoordKind KIND = MccNormCoordKind::NORM_KIND_0_360>
|
||||
class MccNormCoordinate : public MccCoordinate
|
||||
{
|
||||
public:
|
||||
static constexpr MccNormCoordKind normKind = KIND;
|
||||
|
||||
protected:
|
||||
static constexpr double pi2 = 2.0 * std::numbers::pi;
|
||||
static constexpr double normScale = KIND == MccNormCoordKind::NORM_KIND_0_360 ? std::numbers::pi * 2.0
|
||||
: KIND == MccNormCoordKind::NORM_KIND_0_180 ? std::numbers::pi
|
||||
: std::numbers::pi / 2.0;
|
||||
|
||||
void norm()
|
||||
{
|
||||
_angleInRads = std::fmod(_angleInRads, normScale);
|
||||
if constexpr (KIND == MccNormCoordKind::NORM_KIND_0_360) {
|
||||
_angleInRads += normScale;
|
||||
} else if constexpr (KIND == MccNormCoordKind::NORM_KIND_0_180) {
|
||||
_angleInRads = -_angleInRads;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace mcc
|
||||
|
||||
Reference in New Issue
Block a user