// This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0.If a copy of the MPL was not distributed with this // file, You can obtain one at https ://mozilla.org/MPL/2.0/. #include "Angle.h" #include "FloatSingle.h" #include const float Rad2Deg = 57.29578F; const float Deg2Rad = 0.0174532924F; //===== AngleSingle, AngleOf template <> AngleOf Passer::LinearAlgebra::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; } //===== Generic template AngleOf::AngleOf() : value(0) {} template AngleOf::AngleOf(T rawValue) : value(rawValue) {} template const AngleOf AngleOf::zero = AngleOf(); template AngleOf AngleOf::Binary(T rawValue) { AngleOf angle = AngleOf(); angle.SetBinary(rawValue); return angle; } template T AngleOf::GetBinary() const { return this->value; } template void AngleOf::SetBinary(T rawValue) { this->value = rawValue; } template bool AngleOf::operator==(const AngleOf angle) const { return this->value == angle.value; } template bool AngleOf::operator>(AngleOf angle) const { return this->value > angle.value; } template bool AngleOf::operator>=(AngleOf angle) const { return this->value >= angle.value; } template bool AngleOf::operator<(AngleOf angle) const { return this->value < angle.value; } template bool AngleOf::operator<=(AngleOf angle) const { return this->value <= angle.value; } template signed int Passer::LinearAlgebra::AngleOf::Sign(AngleOf angle) { if (angle.value < 0) return -1; if (angle.value > 0) return 1; return 0; } template AngleOf Passer::LinearAlgebra::AngleOf::Abs(AngleOf angle) { if (Sign(angle) < 0) return -angle; else return angle; } template AngleOf AngleOf::operator-() const { AngleOf angle = Binary(-this->value); return angle; } template <> AngleOf AngleOf::operator-(const AngleOf &angle) const { AngleOf r = Binary(this->value - angle.value); r = Normalize(r); return r; } template AngleOf AngleOf::operator-(const AngleOf &angle) const { AngleOf r = Binary(this->value - angle.value); return r; } template <> AngleOf AngleOf::operator+(const AngleOf &angle) const { AngleOf r = Binary(this->value + angle.value); r = Normalize(r); return r; } template AngleOf AngleOf::operator+(const AngleOf &angle) const { AngleOf r = Binary(this->value + angle.value); return r; } template <> AngleOf AngleOf::operator+=(const AngleOf &angle) { this->value += angle.value; this->Normalize(); return *this; } template AngleOf AngleOf::operator+=(const AngleOf &angle) { this->value += angle.value; return *this; } // This defintion is not matching the declaration in the header file somehow // template // AngleOf operator*(const AngleOf &angle, float factor) { // return AngleOf::Degrees((float)angle.InDegrees() * factor); // } // This defintion is not matching the declaration in the header file somehow // template // AngleOf operator*(float factor, const AngleOf &angle) { // return AngleOf::Degrees((float)factor * angle.InDegrees()); // } template void AngleOf::Normalize() { float angleValue = this->InDegrees(); if (!isfinite(angleValue)) return; while (angleValue <= -180) angleValue += 360; while (angleValue > 180) angleValue -= 360; *this = AngleOf::Degrees(angleValue); } template AngleOf AngleOf::Normalize(AngleOf angle) { float angleValue = angle.InDegrees(); if (!isfinite(angleValue)) return angle; while (angleValue <= -180) angleValue += 360; while (angleValue > 180) angleValue -= 360; return AngleOf::Degrees(angleValue); } template AngleOf AngleOf::Clamp(AngleOf angle, AngleOf min, AngleOf max) { float r = Float::Clamp(angle.InDegrees(), min.InDegrees(), max.InDegrees()); return AngleOf::Degrees(r); } template AngleOf AngleOf::MoveTowards(AngleOf fromAngle, AngleOf toAngle, float maxDegrees) { 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)); if (Sign(d) < 0) d = -d; return fromAngle + d; } template float AngleOf::Cos(AngleOf angle) { return cosf(angle.InRadians()); } template float AngleOf::Sin(AngleOf angle) { return sinf(angle.InRadians()); } template float AngleOf::Tan(AngleOf angle) { return tanf(angle.InRadians()); } template AngleOf AngleOf::Acos(float f) { return AngleOf::Radians(acosf(f)); } template AngleOf AngleOf::Asin(float f) { return AngleOf::Radians(asinf(f)); } template AngleOf AngleOf::Atan(float f) { return AngleOf::Radians(atanf(f)); } template AngleOf Passer::LinearAlgebra::AngleOf::Atan2(float y, float x) { return AngleOf::Radians(atan2f(y, x)); } // template <> // float AngleOf::CosineRuleSide(float a, float b, AngleOf gamma) // { // float a2 = a * a; // float b2 = b * b; // float d = // a2 + b2 - // 2 * a * b * Cos(gamma); // cosf(gamma * // Passer::LinearAlgebra::Deg2Rad); // // Catch edge cases where float inacuracies lead tot nans // if (d < 0) // return 0.0f; // float c = sqrtf(d); // return c; // } template float AngleOf::CosineRuleSide(float a, float b, AngleOf gamma) { float a2 = a * a; float b2 = b * b; float d = a2 + b2 - 2 * a * b * Cos(gamma); // cosf(gamma * Passer::LinearAlgebra::Deg2Rad); // Catch edge cases where float inacuracies lead tot nans if (d < 0) return 0; float c = sqrtf(d); return c; } // template <> // AngleOf AngleOf::CosineRuleAngle(float a, float b, float c) { // float a2 = a * a; // float b2 = b * b; // float c2 = c * c; // float d = (a2 + b2 - c2) / (2 * a * b); // // Catch edge cases where float inacuracies lead tot nans // if (d >= 1) // return 0.0f; // if (d <= -1) // return 180.0f; // float gamma = acosf(d) * Rad2Deg; // return gamma; // } template AngleOf AngleOf::CosineRuleAngle(float a, float b, float c) { float a2 = a * a; float b2 = b * b; float c2 = c * c; float d = (a2 + b2 - c2) / (2 * a * b); // Catch edge cases where float inacuracies lead tot nans if (d >= 1) return AngleOf(); if (d <= -1) return AngleOf::Degrees(180); // float gamma = acosf(d) * Rad2Deg; AngleOf gamma = Acos(d); return gamma; } // template <> // AngleOf AngleOf::SineRuleAngle(float a, // AngleOf beta, // float b) { // float deg2rad = Deg2Rad; // float alpha = asinf(a * sinf(beta.InDegrees() * deg2rad) / b); // return alpha; // } template AngleOf AngleOf::SineRuleAngle(float a, AngleOf beta, float b) { // float deg2rad = Deg2Rad; // float alpha = asinf(a * sinf(beta.InDegrees() * deg2rad) / b); AngleOf alpha = Asin(a * Sin(beta) / b); return alpha; } template class Passer::LinearAlgebra::AngleOf; template class Passer::LinearAlgebra::AngleOf; template class Passer::LinearAlgebra::AngleOf;