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}) include_directories(include ${ATHENA_INCLUDE_DIR})
set(SOURCES set(SOURCES
src/CAxisAngle.cpp
src/CVector3f.cpp src/CVector3f.cpp
src/Math.cpp src/Math.cpp
src/CQuaternion.cpp src/CQuaternion.cpp

View File

@ -12,12 +12,15 @@ struct alignas(16) CAxisAngle : CVector3f
ZE_DECLARE_ALIGNED_ALLOCATOR(); ZE_DECLARE_ALIGNED_ALLOCATOR();
CAxisAngle() = default; 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) {} CAxisAngle(const CVector3f& axisAngle) : CVector3f(axisAngle) {}
float angle() { return magnitude(); } float angle() const { return magnitude(); }
const CVector3f& getVector() { return *this; } const CVector3f& getVector() const { return *this; }
static const CAxisAngle sIdentity;
}; };
} }

View File

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

View File

@ -7,23 +7,33 @@
namespace zeus 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: float angle = 0.f;
/**
* @brief CRelAngle CRelAngle() = default;
* @param angles In degrees CRelAngle(float angle) : angle(angle) {}
*/ float asDegrees() const { return radToDeg(angle); }
CRelAngle(const CVector3f& angles) float asRadians() const { return angle; }
float arcCosine() const { return std::acos(angle); }
static CRelAngle FromDegrees(float angle)
{ {
x = degToRad(angles.x); CRelAngle ret;
y = degToRad(angles.y); ret.angle = degToRad(angle);
z = degToRad(angles.z); 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(); ZE_DECLARE_ALIGNED_ALLOCATOR();
CUnitVector3f() : CVector3f(0, 1, 0) {} 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) CUnitVector3f(const CVector3f& vec, bool doNormalize = false) : CVector3f(vec)
{ {
if (doNormalize && canBeNormalized()) 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); } 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 CQuaternion CQuaternion::log() const
{ {
float a = std::acos(w); float a = std::acos(w);