diff --git a/Angle.cpp b/Angle.cpp index c88afdc..39f12df 100644 --- a/Angle.cpp +++ b/Angle.cpp @@ -8,116 +8,14 @@ const float Rad2Deg = 57.29578F; const float Deg2Rad = 0.0174532924F; -/* -float Angle::Normalize(float angle) { - if (!isfinite(angle)) - return angle; - while (angle <= -180) - angle += 360; - while (angle > 180) - angle -= 360; - return angle; -} -*/ - -//---------------------- +//===== Generic template AngleOf::AngleOf() : value(0) {} +template AngleOf::AngleOf(T rawValue) : value(rawValue) {} + template const AngleOf AngleOf::zero = AngleOf(); -// template -// const AngleOf AngleOf::deg90 = AngleOf::Degrees(90); -// template -// const AngleOf AngleOf::deg180 = AngleOf::Degrees(180); - -//===== AngleSingle, AngleOf - -template <> AngleOf AngleOf::Degrees(float degrees) { - if (isfinite(degrees)) { - while (degrees < -180) - degrees += 360; - while (degrees >= 180) - degrees -= 360; - } - - return Binary(degrees); -} - -template <> AngleOf AngleOf::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::InDegrees() const { return this->value; } - -template <> float AngleOf::InRadians() const { - return this->value * Deg2Rad; -} - -//===== Angle16, AngleOf - -template <> -AngleOf AngleOf::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 AngleOf::Radians(float radians) { - if (!isfinite(radians)) - return AngleOf::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::InDegrees() const { - float degrees = this->value / 65536.0f * 360.0f; - return degrees; -} - -template <> float AngleOf::InRadians() const { - float radians = this->value / 65536.0f * (2 * pi); - return radians; -} - -//===== Angle8, AngleOf - -template <> AngleOf AngleOf::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 AngleOf::Radians(float radians) { - if (!isfinite(radians)) - return AngleOf::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::InDegrees() const { - float degrees = this->value / 256.0f * 360.0f; - return degrees; -} - -template <> float AngleOf::InRadians() const { - float radians = this->value / 128.0f * pi; - return radians; -} - -//===== Generic template AngleOf AngleOf::Binary(T rawValue) { AngleOf angle = AngleOf(); @@ -372,4 +270,90 @@ AngleOf AngleOf::SineRuleAngle(float a, AngleOf beta, float b) { template class AngleOf; template class AngleOf; -template class AngleOf; \ No newline at end of file +template class AngleOf; + +//===== AngleSingle, AngleOf + +template <> AngleOf AngleOf::Degrees(float degrees) { + if (isfinite(degrees)) { + while (degrees < -180) + degrees += 360; + while (degrees >= 180) + degrees -= 360; + } + + return AngleOf(degrees); +} + +template <> AngleOf AngleOf::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::InDegrees() const { return this->value; } + +template <> float AngleOf::InRadians() const { + return this->value * Deg2Rad; +} + +//===== Angle16, AngleOf + +template <> +AngleOf AngleOf::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 AngleOf::Radians(float radians) { + if (!isfinite(radians)) + return AngleOf::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::InDegrees() const { + float degrees = this->value / 65536.0f * 360.0f; + return degrees; +} + +template <> float AngleOf::InRadians() const { + float radians = this->value / 65536.0f * (2 * pi); + return radians; +} + +//===== Angle8, AngleOf + +template <> AngleOf AngleOf::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 AngleOf::Radians(float radians) { + if (!isfinite(radians)) + return AngleOf::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::InDegrees() const { + float degrees = this->value / 256.0f * 360.0f; + return degrees; +} + +template <> float AngleOf::InRadians() const { + float radians = this->value / 128.0f * pi; + return radians; +} \ No newline at end of file diff --git a/Angle.h b/Angle.h index f8a215f..e8f1f63 100644 --- a/Angle.h +++ b/Angle.h @@ -32,14 +32,11 @@ public: /// @param degrees the angle in degrees /// @return The angle value static AngleOf Degrees(float degrees); - /// @brief Short-hand Deg alias for the Degrees function - constexpr static auto Deg = Degrees; /// @brief Creates an angle in radians /// @param radians the angle in radians /// @return The angle value static AngleOf 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 /// @param rawValue the raw value to use for the angle /// @return The the angle @@ -211,10 +208,9 @@ public: private: T value; - AngleOf(T value); + AngleOf(T rawValue); }; -// using Angle = AngleOf; using AngleSingle = AngleOf; using Angle16 = AngleOf; using Angle8 = AngleOf;