Proper CRelAngle implementation

This commit is contained in:
Phillip Stephens 2016-09-01 12:38:16 -07:00
parent fb91979596
commit 1bba8594d5
7 changed files with 43 additions and 41 deletions

View File

@ -7,6 +7,7 @@ endif()
include_directories(include ${ATHENA_INCLUDE_DIR})
set(SOURCES
src/CAxisAngle.cpp
src/CVector3f.cpp
src/Math.cpp
src/CQuaternion.cpp

View File

@ -12,12 +12,15 @@ struct alignas(16) CAxisAngle : CVector3f
ZE_DECLARE_ALIGNED_ALLOCATOR();
CAxisAngle() = default;
CAxisAngle(const CUnitVector3f& axis, float distance) : CVector3f(distance * axis) {}
CAxisAngle(float x, float y, float z) : CVector3f(x, y, z) {}
CAxisAngle(const CUnitVector3f& axis, float angle) : CVector3f(angle * axis) {}
CAxisAngle(const CVector3f& axisAngle) : CVector3f(axisAngle) {}
float angle() { return magnitude(); }
const CVector3f& getVector() { return *this; }
float angle() const { return magnitude(); }
const CVector3f& getVector() const { return *this; }
static const CAxisAngle sIdentity;
};
}

View File

@ -7,6 +7,7 @@
#include "zeus/CVector4f.hpp"
#include "zeus/CMatrix3f.hpp"
#include "zeus/Math.hpp"
#include "zeus/CRelAngle.hpp"
#if ZE_ATHENA_TYPES
#include <athena/IStreamReader.hpp>
#endif
@ -162,18 +163,16 @@ public:
* @param angle The magnitude of the rotation in radians
* @return
*/
static inline CQuaternion fromAxisAngle(const CVector3f& axis, float angle)
static inline CQuaternion fromAxisAngle(const CUnitVector3f& axis, const CRelAngle& angle)
{
return CQuaternion(std::cos(angle / 2.f), axis * std::sin(angle / 2.f));
return CQuaternion(std::cos(angle.asRadians() / 2.f), axis * std::sin(angle.asRadians() / 2.f));
}
void rotateX(float angle) { *this *= fromAxisAngle({1.0f, 0.0f, 0.0f}, angle); }
void rotateY(float angle) { *this *= fromAxisAngle({0.0f, 1.0f, 0.0f}, angle); }
void rotateZ(float angle) { *this *= fromAxisAngle({0.0f, 0.0f, 1.0f}, angle); }
void rotateX(const CRelAngle& angle) { *this *= fromAxisAngle({1.0f, 0.0f, 0.0f}, angle); }
void rotateY(const CRelAngle& angle) { *this *= fromAxisAngle({0.0f, 1.0f, 0.0f}, angle); }
void rotateZ(const CRelAngle& angle) { *this *= fromAxisAngle({0.0f, 0.0f, 1.0f}, angle); }
CAxisAngle toAxisAngle();
static inline CVector3f rotate(const CQuaternion& rotation, const CVector3f& v)
static inline CVector3f rotate(const CQuaternion& rotation, const CAxisAngle& v)
{
CQuaternion q = rotation * v;
q *= rotation.inverse();

View File

@ -7,23 +7,33 @@
namespace zeus
{
/**
* @brief The CRelAngle class represents relative angles in radians
* @brief The CRelAngle class represents relative angle in radians
*/
class alignas(16) CRelAngle : public CVector3f
struct CRelAngle
{
public:
/**
* @brief CRelAngle
* @param angles In degrees
*/
CRelAngle(const CVector3f& angles)
float angle = 0.f;
CRelAngle() = default;
CRelAngle(float angle) : angle(angle) {}
float asDegrees() const { return radToDeg(angle); }
float asRadians() const { return angle; }
float arcCosine() const { return std::acos(angle); }
static CRelAngle FromDegrees(float angle)
{
x = degToRad(angles.x);
y = degToRad(angles.y);
z = degToRad(angles.z);
CRelAngle ret;
ret.angle = degToRad(angle);
return ret;
}
CRelAngle(float x, float y, float z) : CRelAngle(CVector3f{x, y, z}) {}
operator float() { return angle; }
static CRelAngle FromRadians(float angle) { return CRelAngle(angle); }
bool operator <(const CRelAngle& other) const { return angle < other.angle; }
CRelAngle& operator +=(const CRelAngle& other) { angle += other.angle; return *this; }
CRelAngle& operator +=(float r) { angle += r; return *this; }
CRelAngle& operator *=(const CRelAngle& other) { angle *= other.angle; return *this; }
CRelAngle& operator *=(float r) { angle *= r; return *this;}
};
}

View File

@ -11,7 +11,7 @@ public:
ZE_DECLARE_ALIGNED_ALLOCATOR();
CUnitVector3f() : CVector3f(0, 1, 0) {}
CUnitVector3f(float x, float y, float z) : CVector3f(x, y, z) {}
CUnitVector3f(const CVector3f& vec, bool doNormalize = false) : CVector3f(vec)
{
if (doNormalize && canBeNormalized())

6
src/CAxisAngle.cpp Normal file
View File

@ -0,0 +1,6 @@
#include "zeus/CAxisAngle.hpp"
namespace zeus
{
const CAxisAngle CAxisAngle::sIdentity = {};
}

View File

@ -120,23 +120,6 @@ void CQuaternion::invert()
CQuaternion CQuaternion::inverse() const { return CQuaternion(w, -x, -y, -z); }
CAxisAngle CQuaternion::toAxisAngle()
{
// CAxisAngle ret;
// ret.angle = std::acos(r);
// float thetaInv = 1.0f/std::sin(ret.angle);
// ret.axis.x = v.x * thetaInv;
// ret.axis.y = v.y * thetaInv;
// ret.axis.z = v.z * thetaInv;
// ret.angle *= 2.f;
// return ret;
return CAxisAngle();
}
CQuaternion CQuaternion::log() const
{
float a = std::acos(w);