RoboidControl-cpp/SwingTwist.h
2024-09-26 10:10:28 +02:00

72 lines
2.2 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/.
#ifndef SWINGTWIST_H
#define SWINGTWIST_H
#include "Angle.h"
#include "Direction.h"
#include "Quaternion.h"
#include "Spherical.h"
namespace Passer {
namespace LinearAlgebra {
template <typename T>
class SwingTwistOf {
public:
DirectionOf<T> swing;
AngleOf<T> twist;
SwingTwistOf<T>();
SwingTwistOf<T>(DirectionOf<T> swing, AngleOf<T> twist);
SwingTwistOf<T>(AngleOf<T> horizontal, AngleOf<T> vertical, AngleOf<T> twist);
static SwingTwistOf<T> Degrees(float horizontal,
float vertical = 0,
float twist = 0);
Quaternion ToQuaternion() const;
static SwingTwistOf<T> FromQuaternion(Quaternion q);
SphericalOf<T> ToAngleAxis() const;
static SwingTwistOf<T> FromAngleAxis(SphericalOf<T> aa);
const static SwingTwistOf<T> identity;
/// <summary>
/// Rotate a vector using this rotation
/// </summary>
/// <param name="vector">The vector to rotate</param>
/// <returns>The rotated vector</returns>
SphericalOf<T> operator*(const SphericalOf<T>& vector) const;
/// <summary>
/// Multiply this rotation with another rotation
/// </summary>
/// <param name="rotation">The swing/twist rotation to multiply with</param>
/// <returns>The resulting swing/twist rotation</returns>
/// The result will be this rotation rotated according to
/// the give rotation.
SwingTwistOf<T> operator*(const SwingTwistOf<T>& rotation) const;
SwingTwistOf<T> operator*=(const SwingTwistOf<T>& rotation);
static SwingTwistOf<T> Inverse(SwingTwistOf<T> rotation);
/// <summary>
/// Convert an angle/axis representation to a swingt
/// </summary>
/// <param name="angle">The angle</param>
/// <param name="axis">The axis</param>
/// <returns>The resulting quaternion</returns>
static SwingTwistOf<T> AngleAxis(float angle, const SphericalOf<T>& axis);
};
using SwingTwistSingle = SwingTwistOf<float>;
using SwingTwist16 = SwingTwistOf<signed short>;
} // namespace LinearAlgebra
} // namespace Passer
using namespace Passer::LinearAlgebra;
#endif