Merge commit '2b5f5cd175b20fa01e04b696dfe0cfbe2c4ad9da'

This commit is contained in:
Pascal Serrarens 2024-12-28 15:29:46 +01:00
commit 770ce327d2
2 changed files with 92 additions and 112 deletions

196
Angle.cpp
View File

@ -8,116 +8,14 @@
const float Rad2Deg = 57.29578F; const float Rad2Deg = 57.29578F;
const float Deg2Rad = 0.0174532924F; const float Deg2Rad = 0.0174532924F;
/*
float Angle::Normalize(float angle) {
if (!isfinite(angle))
return angle;
while (angle <= -180) //===== Generic
angle += 360;
while (angle > 180)
angle -= 360;
return angle;
}
*/
//----------------------
template <typename T> AngleOf<T>::AngleOf() : value(0) {} template <typename T> AngleOf<T>::AngleOf() : value(0) {}
template <typename T> AngleOf<T>::AngleOf(T rawValue) : value(rawValue) {}
template <typename T> const AngleOf<T> AngleOf<T>::zero = AngleOf<T>(); template <typename T> const AngleOf<T> AngleOf<T>::zero = AngleOf<T>();
// template <typename T>
// const AngleOf<T> AngleOf<T>::deg90 = AngleOf<T>::Degrees(90);
// template <typename T>
// const AngleOf<T> AngleOf<T>::deg180 = AngleOf<T>::Degrees(180);
//===== AngleSingle, AngleOf<float>
template <> AngleOf<float> AngleOf<float>::Degrees(float degrees) {
if (isfinite(degrees)) {
while (degrees < -180)
degrees += 360;
while (degrees >= 180)
degrees -= 360;
}
return Binary(degrees);
}
template <> AngleOf<float> AngleOf<float>::Radians(float radians) {
if (isfinite(radians)) {
while (radians <= -pi)
radians += 2 * pi;
while (radians > pi)
radians -= 2 * pi;
}
return Binary(radians * Rad2Deg);
}
template <> float AngleOf<float>::InDegrees() const { return this->value; }
template <> float AngleOf<float>::InRadians() const {
return this->value * Deg2Rad;
}
//===== Angle16, AngleOf<signed short>
template <>
AngleOf<signed short> AngleOf<signed short>::Degrees(float degrees) {
// map float [-180..180) to integer [-32768..32767]
signed short value = (signed short)roundf(degrees / 360.0F * 65536.0F);
return Binary(value);
}
template <>
AngleOf<signed short> AngleOf<signed short>::Radians(float radians) {
if (!isfinite(radians))
return AngleOf<signed short>::zero;
// map float [-PI..PI) to integer [-32768..32767]
signed short value = (signed short)roundf(radians / pi * 32768.0F);
return Binary(value);
}
template <> float AngleOf<signed short>::InDegrees() const {
float degrees = this->value / 65536.0f * 360.0f;
return degrees;
}
template <> float AngleOf<signed short>::InRadians() const {
float radians = this->value / 65536.0f * (2 * pi);
return radians;
}
//===== Angle8, AngleOf<signed char>
template <> AngleOf<signed char> AngleOf<signed char>::Degrees(float degrees) {
// map float [-180..180) to integer [-128..127)
signed char value = (signed char)roundf(degrees / 360.0F * 256.0F);
return Binary(value);
}
template <> AngleOf<signed char> AngleOf<signed char>::Radians(float radians) {
if (!isfinite(radians))
return AngleOf<signed char>::zero;
// map float [-pi..pi) to integer [-128..127)
signed char value = (signed char)roundf(radians / pi * 128.0f);
return Binary(value);
}
template <> float AngleOf<signed char>::InDegrees() const {
float degrees = this->value / 256.0f * 360.0f;
return degrees;
}
template <> float AngleOf<signed char>::InRadians() const {
float radians = this->value / 128.0f * pi;
return radians;
}
//===== Generic
template <typename T> AngleOf<T> AngleOf<T>::Binary(T rawValue) { template <typename T> AngleOf<T> AngleOf<T>::Binary(T rawValue) {
AngleOf<T> angle = AngleOf<T>(); AngleOf<T> angle = AngleOf<T>();
@ -372,4 +270,90 @@ AngleOf<T> AngleOf<T>::SineRuleAngle(float a, AngleOf<T> beta, float b) {
template class AngleOf<float>; template class AngleOf<float>;
template class AngleOf<signed char>; template class AngleOf<signed char>;
template class AngleOf<signed short>; template class AngleOf<signed short>;
//===== AngleSingle, AngleOf<float>
template <> AngleOf<float> AngleOf<float>::Degrees(float degrees) {
if (isfinite(degrees)) {
while (degrees < -180)
degrees += 360;
while (degrees >= 180)
degrees -= 360;
}
return AngleOf<float>(degrees);
}
template <> AngleOf<float> AngleOf<float>::Radians(float radians) {
if (isfinite(radians)) {
while (radians <= -pi)
radians += 2 * pi;
while (radians > pi)
radians -= 2 * pi;
}
return Binary(radians * Rad2Deg);
}
template <> float AngleOf<float>::InDegrees() const { return this->value; }
template <> float AngleOf<float>::InRadians() const {
return this->value * Deg2Rad;
}
//===== Angle16, AngleOf<signed short>
template <>
AngleOf<signed short> AngleOf<signed short>::Degrees(float degrees) {
// map float [-180..180) to integer [-32768..32767]
signed short value = (signed short)roundf(degrees / 360.0F * 65536.0F);
return Binary(value);
}
template <>
AngleOf<signed short> AngleOf<signed short>::Radians(float radians) {
if (!isfinite(radians))
return AngleOf<signed short>::zero;
// map float [-PI..PI) to integer [-32768..32767]
signed short value = (signed short)roundf(radians / pi * 32768.0F);
return Binary(value);
}
template <> float AngleOf<signed short>::InDegrees() const {
float degrees = this->value / 65536.0f * 360.0f;
return degrees;
}
template <> float AngleOf<signed short>::InRadians() const {
float radians = this->value / 65536.0f * (2 * pi);
return radians;
}
//===== Angle8, AngleOf<signed char>
template <> AngleOf<signed char> AngleOf<signed char>::Degrees(float degrees) {
// map float [-180..180) to integer [-128..127)
signed char value = (signed char)roundf(degrees / 360.0F * 256.0F);
return Binary(value);
}
template <> AngleOf<signed char> AngleOf<signed char>::Radians(float radians) {
if (!isfinite(radians))
return AngleOf<signed char>::zero;
// map float [-pi..pi) to integer [-128..127)
signed char value = (signed char)roundf(radians / pi * 128.0f);
return Binary(value);
}
template <> float AngleOf<signed char>::InDegrees() const {
float degrees = this->value / 256.0f * 360.0f;
return degrees;
}
template <> float AngleOf<signed char>::InRadians() const {
float radians = this->value / 128.0f * pi;
return radians;
}

View File

@ -32,14 +32,11 @@ public:
/// @param degrees the angle in degrees /// @param degrees the angle in degrees
/// @return The angle value /// @return The angle value
static AngleOf<T> Degrees(float degrees); static AngleOf<T> Degrees(float degrees);
/// @brief Short-hand Deg alias for the Degrees function
constexpr static auto Deg = Degrees;
/// @brief Creates an angle in radians /// @brief Creates an angle in radians
/// @param radians the angle in radians /// @param radians the angle in radians
/// @return The angle value /// @return The angle value
static AngleOf<T> Radians(float radians); static AngleOf<T> Radians(float radians);
/// @brief Short-hand Rad alias for the Radians function
constexpr static auto Rad = Radians;
/// @brief Creates an angle from a raw value /// @brief Creates an angle from a raw value
/// @param rawValue the raw value to use for the angle /// @param rawValue the raw value to use for the angle
/// @return The the angle /// @return The the angle
@ -211,10 +208,9 @@ public:
private: private:
T value; T value;
AngleOf<T>(T value); AngleOf<T>(T rawValue);
}; };
// using Angle = AngleOf<float>;
using AngleSingle = AngleOf<float>; using AngleSingle = AngleOf<float>;
using Angle16 = AngleOf<signed short>; using Angle16 = AngleOf<signed short>;
using Angle8 = AngleOf<signed char>; using Angle8 = AngleOf<signed char>;