diff --git a/Angle.cpp b/Angle.cpp index 4ad5113..7bf8bc2 100644 --- a/Angle.cpp +++ b/Angle.cpp @@ -3,8 +3,8 @@ // file, You can obtain one at https ://mozilla.org/MPL/2.0/. #include "Angle.h" -#include #include "FloatSingle.h" +#include const float Rad2Deg = 57.29578F; const float Deg2Rad = 0.0174532924F; @@ -23,16 +23,13 @@ float Angle::Normalize(float angle) { //---------------------- -template -AngleOf::AngleOf() : value(0) {} +template AngleOf::AngleOf() : value(0) {} -template -AngleOf::AngleOf(T angle) : value(angle) {} +template AngleOf::AngleOf(T angle) : value(angle) {} //===== AngleSingle, AngleOf -template <> -AngleOf AngleOf::Degrees(float angle) { +template <> AngleOf AngleOf::Degrees(float angle) { if (isfinite(angle)) { while (angle < -180) angle += 360; @@ -43,8 +40,7 @@ AngleOf AngleOf::Degrees(float angle) { return AngleOf(angle); } -template <> -AngleOf AngleOf::Radians(float angle) { +template <> AngleOf AngleOf::Radians(float angle) { if (isfinite(angle)) { while (angle <= -pi) angle += 2 * pi; @@ -55,27 +51,27 @@ AngleOf AngleOf::Radians(float angle) { return AngleOf(angle * Rad2Deg); } -template <> -float AngleOf::InDegrees() const { - return this->value; +template AngleOf AngleOf::Binary(T x) { + AngleOf angle = AngleOf(); + angle.value = x; + return angle; } -template <> -float AngleOf::InRadians() const { +template <> float AngleOf::InDegrees() const { return this->value; } + +template <> float AngleOf::InRadians() const { return this->value * Deg2Rad; } //===== Angle16, AngleOf -template <> -AngleOf AngleOf::Degrees(float angle) { +template <> AngleOf AngleOf::Degrees(float angle) { // map float [-180..180) to integer [-32768..32767] signed short value = (signed short)(angle / 360.0F * 65536.0F); return AngleOf(value); } -template <> -AngleOf AngleOf::Radians(float angle) { +template <> AngleOf AngleOf::Radians(float angle) { if (!isfinite(angle)) return AngleOf(0); @@ -84,29 +80,25 @@ AngleOf AngleOf::Radians(float angle) { return AngleOf(value); } -template <> -float AngleOf::InDegrees() const { +template <> float AngleOf::InDegrees() const { float degrees = this->value / 65536.0f * 360.0f; return degrees; } -template <> -float AngleOf::InRadians() const { +template <> float AngleOf::InRadians() const { float radians = this->value / 65536.0f * (2 * pi); return radians; } //===== Angle8, AngleOf -template <> -AngleOf AngleOf::Degrees(float angle) { +template <> AngleOf AngleOf::Degrees(float angle) { // map float [-180..180) to integer [-128..127) signed char value = (signed char)(angle / 360.0F * 256.0F); return AngleOf(value); } -template <> -AngleOf AngleOf::Radians(float angle) { +template <> AngleOf AngleOf::Radians(float angle) { if (!isfinite(angle)) return AngleOf(0); @@ -115,14 +107,12 @@ AngleOf AngleOf::Radians(float angle) { return AngleOf(value); } -template <> -float AngleOf::InDegrees() const { +template <> float AngleOf::InDegrees() const { float degrees = this->value / 256.0f * 360.0f; return degrees; } -template <> -float AngleOf::InRadians() const { +template <> float AngleOf::InRadians() const { float radians = this->value / 128.0f * pi; return radians; } @@ -136,28 +126,23 @@ float AngleOf::InRadians() const { // template // const AngleOf AngleOf::deg180 = AngleOf::Degrees(180); -template -bool AngleOf::operator==(const AngleOf a) const { +template bool AngleOf::operator==(const AngleOf a) const { return this->value == a.value; } -template -bool AngleOf::operator>(AngleOf a) const { +template bool AngleOf::operator>(AngleOf a) const { return this->value > a.value; } -template -bool AngleOf::operator>=(AngleOf a) const { +template bool AngleOf::operator>=(AngleOf a) const { return this->value >= a.value; } -template -bool AngleOf::operator<(AngleOf a) const { +template bool AngleOf::operator<(AngleOf a) const { return this->value < a.value; } -template -bool AngleOf::operator<=(AngleOf a) const { +template bool AngleOf::operator<=(AngleOf a) const { return this->value <= a.value; } @@ -178,51 +163,47 @@ AngleOf Passer::LinearAlgebra::AngleOf::Abs(AngleOf a) { return a; } -template -AngleOf AngleOf::operator-() const { +template AngleOf AngleOf::operator-() const { AngleOf angle = AngleOf(-this->value); return angle; } template <> -AngleOf AngleOf::operator-(const AngleOf& a) const { +AngleOf AngleOf::operator-(const AngleOf &a) const { AngleOf angle = AngleOf(this->value - a.value); angle = Normalize(angle); return angle; } template -AngleOf AngleOf::operator-(const AngleOf& a) const { +AngleOf AngleOf::operator-(const AngleOf &a) const { AngleOf angle = AngleOf(this->value - a.value); return angle; } template <> -AngleOf AngleOf::operator+(const AngleOf& a) const { +AngleOf AngleOf::operator+(const AngleOf &a) const { AngleOf angle = AngleOf(this->value + a.value); angle = Normalize(angle); return angle; } template -AngleOf AngleOf::operator+(const AngleOf& a) const { +AngleOf AngleOf::operator+(const AngleOf &a) const { AngleOf angle = AngleOf(this->value + a.value); return angle; } -template <> -AngleOf AngleOf::operator+=(const AngleOf& a) { +template <> AngleOf AngleOf::operator+=(const AngleOf &a) { this->value += a.value; this->Normalize(); return *this; } -template -AngleOf AngleOf::operator+=(const AngleOf& a) { +template AngleOf AngleOf::operator+=(const AngleOf &a) { this->value += a.value; return *this; } -template -void AngleOf::Normalize() { +template void AngleOf::Normalize() { float angleValue = this->InDegrees(); if (!isfinite(angleValue)) return; @@ -234,8 +215,7 @@ void AngleOf::Normalize() { *this = AngleOf::Degrees(angleValue); } -template -AngleOf AngleOf::Normalize(AngleOf angle) { +template AngleOf AngleOf::Normalize(AngleOf angle) { float angleValue = angle.InDegrees(); if (!isfinite(angleValue)) return angle; @@ -254,10 +234,9 @@ AngleOf AngleOf::Clamp(AngleOf angle, AngleOf min, AngleOf max) { } template -AngleOf AngleOf::MoveTowards(AngleOf fromAngle, - AngleOf toAngle, +AngleOf AngleOf::MoveTowards(AngleOf fromAngle, AngleOf toAngle, float maxDegrees) { - maxDegrees = fmaxf(0, maxDegrees); // filter out negative distances + maxDegrees = fmaxf(0, maxDegrees); // filter out negative distances AngleOf d = toAngle - fromAngle; float dDegrees = Abs(d).InDegrees(); d = AngleOf::Degrees(Float::Clamp(dDegrees, 0, maxDegrees)); @@ -267,29 +246,23 @@ AngleOf AngleOf::MoveTowards(AngleOf fromAngle, return fromAngle + d; } -template -float AngleOf::Cos(AngleOf a) { +template float AngleOf::Cos(AngleOf a) { return cosf(a.InRadians()); } -template -float AngleOf::Sin(AngleOf a) { +template float AngleOf::Sin(AngleOf a) { return sinf(a.InRadians()); } -template -float AngleOf::Tan(AngleOf a) { +template float AngleOf::Tan(AngleOf a) { return tanf(a.InRadians()); } -template -AngleOf AngleOf::Acos(float f) { +template AngleOf AngleOf::Acos(float f) { return AngleOf::Radians(acosf(f)); } -template -AngleOf AngleOf::Asin(float f) { +template AngleOf AngleOf::Asin(float f) { return AngleOf::Radians(asinf(f)); } -template -AngleOf AngleOf::Atan(float f) { +template AngleOf AngleOf::Atan(float f) { return AngleOf::Radians(atanf(f)); } @@ -321,7 +294,7 @@ float AngleOf::CosineRuleSide(float a, float b, AngleOf gamma) { float b2 = b * b; float d = a2 + b2 - - 2 * a * b * Cos(gamma); // cosf(gamma * Passer::LinearAlgebra::Deg2Rad); + 2 * a * b * Cos(gamma); // cosf(gamma * Passer::LinearAlgebra::Deg2Rad); // Catch edge cases where float inacuracies lead tot nans if (d < 0) return 0; diff --git a/Angle.h b/Angle.h index 69e564b..89ae2fe 100644 --- a/Angle.h +++ b/Angle.h @@ -13,13 +13,13 @@ static float pi = 3.1415927410125732421875F; static float Rad2Deg = 360.0f / (pi * 2); static float Deg2Rad = (pi * 2) / 360.0f; -template -class AngleOf { - public: +template class AngleOf { +public: AngleOf(); static AngleOf Degrees(float f); static AngleOf Radians(float f); + static AngleOf Binary(T x); // const static AngleOf zero; // const static AngleOf deg90; @@ -29,6 +29,7 @@ class AngleOf { float InRadians() const; inline T GetBinary() const { return this->value; } + inline void SetBinary(T x) { this->value = x; } bool operator==(const AngleOf a) const; bool operator>(AngleOf a) const; @@ -40,14 +41,14 @@ class AngleOf { static AngleOf Abs(AngleOf a); AngleOf operator-() const; - AngleOf operator-(const AngleOf& a) const; - AngleOf operator+(const AngleOf& a) const; - AngleOf operator+=(const AngleOf& a); + AngleOf operator-(const AngleOf &a) const; + AngleOf operator+(const AngleOf &a) const; + AngleOf operator+=(const AngleOf &a); - friend AngleOf operator*(const AngleOf& a, float f) { + friend AngleOf operator*(const AngleOf &a, float f) { return AngleOf::Degrees((float)a.InDegrees() * f); } - friend AngleOf operator*(float f, const AngleOf& a) { + friend AngleOf operator*(float f, const AngleOf &a) { return AngleOf::Degrees((float)f * a.InDegrees()); } @@ -58,8 +59,7 @@ class AngleOf { // AngleOf r = Normalize(b.InDegrees() - a.InDegrees()); // return r; // }; - static AngleOf MoveTowards(AngleOf fromAngle, - AngleOf toAngle, + static AngleOf MoveTowards(AngleOf fromAngle, AngleOf toAngle, float maxAngle); static float Cos(AngleOf a); @@ -76,7 +76,7 @@ class AngleOf { static AngleOf SineRuleAngle(float a, AngleOf beta, float c); - private: +private: T value; AngleOf(T value); @@ -91,8 +91,8 @@ using AngleSingle = AngleOf; using Angle16 = AngleOf; using Angle8 = AngleOf; -} // namespace LinearAlgebra -} // namespace Passer +} // namespace LinearAlgebra +} // namespace Passer using namespace Passer::LinearAlgebra; #endif \ No newline at end of file