RoboidControl-cpp/Angle16.cpp
2024-09-24 10:29:21 +02:00

97 lines
2.5 KiB
C++

// 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 <math.h>
#include <stdlib.h>
#include "Angle.h"
template <>
AngleOf<signed short>::AngleOf(int angle) {
signed long long_angle = (signed short)angle * 65536;
this->value = (signed short)(long_angle / 360);
}
template <>
AngleOf<signed short>::AngleOf(float angle) {
if (!isfinite(angle)) {
value = 0;
return;
}
// map float [-180..180) to integer [-32768..32767]
this->value = (signed short)(angle / 360.0F * 65536.0F);
}
// template <>
// AngleOf<signed short>::operator float() const {
// float f = ((this->value * 180) / 32768.0F);
// return f;
// }
template <>
float AngleOf<signed short>::ToFloat() const {
float f = ((this->value * 180) / 32768.0F);
return f;
}
template <>
AngleOf<signed short> AngleOf<signed short>::operator-() const {
AngleOf<signed short> angle = AngleOf();
angle.value = -this->value;
return angle;
}
template <>
AngleOf<signed short> AngleOf<signed short>::operator-(
const AngleOf<signed short>& a) const {
AngleOf<signed short> angle = AngleOf();
angle.value = this->value - a.value;
return angle;
}
template <>
AngleOf<signed short> AngleOf<signed short>::operator+(
const AngleOf<signed short>& a) const {
AngleOf<signed short> angle = AngleOf();
angle.value = this->value + a.value;
return angle;
}
// Not correct!!! just for syntactical compilation ATM
template <>
AngleOf<signed short> AngleOf<signed short>::CosineRuleSide(
float a,
float b,
AngleOf<signed short> gamma) {
float a2 = a * a;
float b2 = b * b;
float d = a2 + b2 -
2 * a * b * cosf(gamma.ToFloat() * Passer::LinearAlgebra::Deg2Rad);
// Catch edge cases where float inacuracies lead tot nans
if (d < 0)
return 0.0f;
float c = sqrtf(d);
return c;
}
// Not correct!!! just for syntactical compilation ATM
template <>
AngleOf<signed short> AngleOf<signed short>::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) * Passer::LinearAlgebra::Rad2Deg;
return gamma;
}
*/