Math/Angle: Adds conversion to euler angles and quaternions

This commit is contained in:
Lynix
2018-09-05 21:51:32 +02:00
parent 1114bb0fdd
commit 1ee75f2699
3 changed files with 132 additions and 9 deletions

View File

@@ -18,6 +18,9 @@ namespace Nz
{
struct SerializationContext;
template<typename T> class EulerAngles;
template<typename T> class Quaternion;
template<AngleUnit Unit, typename T>
class Angle
{
@@ -45,13 +48,20 @@ namespace Nz
template<typename U> Angle& Set(const Angle<Unit, U>& Angle);
Angle<AngleUnit::Degree, T> ToDegrees() const;
EulerAngles<T> ToEulerAngles() const;
Quaternion<T> ToQuaternion() const;
Angle<AngleUnit::Radian, T> ToRadians() const;
String ToString() const;
operator EulerAngles<T>() const;
operator Quaternion<T>() const;
Angle& operator=(const Angle&) = default;
Angle operator+(const Angle& other) const;
Angle operator-(const Angle& other) const;
Angle operator*(T scalar) const;
Angle operator/(T divider) const;
Angle& operator+=(const Angle& other);
Angle& operator-=(const Angle& other);

View File

@@ -319,6 +319,58 @@ namespace Nz
return Detail::AngleUtils<Unit>::ToString(angle);
}
/*!
* \brief Shortcut allowing implicit conversion to Euler angles
* \return Euler Angles representing a 2D rotation by this angle
*
* \see ToEulerAngles
*/
template<AngleUnit Unit, typename T>
Angle<Unit, T>::operator EulerAngles<T>() const
{
return ToEulerAngles();
}
/*!
* \brief Shortcut allowing implicit conversion to Quaternion
* \return Quaternion representing a 2D rotation by this angle
*
* \see ToQuaternion
*/
template<AngleUnit Unit, typename T>
Angle<Unit, T>::operator Quaternion<T>() const
{
return ToQuaternion();
}
/*!
* \brief Converts the angle to an Euler Angles representation
* \return A 2D rotation expressed in Euler angles
*
* This will assume two-dimensional usage, and will set the angle value (as degrees) as the roll value of the Euler Angles, leaving pitch and yaw to zero
*/
template<AngleUnit Unit, typename T>
EulerAngles<T> Angle<Unit, T>::ToEulerAngles() const
{
return EulerAngles<T>(0, 0, ToDegrees().angle);
}
/*!
* \brief Converts the angle to a Quaternion representation
* \return A 2D rotation expressed with Quaternion
*
* This will assume two-dimensional usage, as if the angle was first converted to Euler Angles and then to a Quaternion
*
* \see ToEulerAngles
*/
template<AngleUnit Unit, typename T>
Quaternion<T> Angle<Unit, T>::ToQuaternion() const
{
auto halfAngle = Angle(*this) / 2.f;
auto sincos = halfAngle.GetSinCos();
return Quaternion<T>(sincos.second, 0, 0, sincos.first);
}
/*!
* \brief Addition operator
* \return Adds two angles together
@@ -343,6 +395,30 @@ namespace Nz
return Angle(angle - other.angle);
}
/*!
* \brief Multiplication operator
* \return A copy of the angle, scaled by the multiplier
*
* \param scalar Multiplier
*/
template<AngleUnit Unit, typename T>
Angle<Unit, T> Angle<Unit, T>::operator*(T scalar) const
{
return Angle(angle * scalar);
}
/*!
* \brief Divides the angle by a scalar
* \return A copy of the angle, divided by the divider
*
* \param divider Divider
*/
template<AngleUnit Unit, typename T>
Angle<Unit, T> Angle<Unit, T>::operator/(T divider) const
{
return Angle(angle / divider);
}
/*!
* \brief Adds an angle by another
* \return A reference to the angle