zeus/src/CVector3f.cpp

63 lines
1.6 KiB
C++
Raw Normal View History

2016-03-04 15:03:26 -08:00
#include "zeus/CVector3f.hpp"
2017-03-14 00:02:09 -07:00
#include "zeus/CVector3d.hpp"
2015-04-19 13:39:16 -07:00
#include <memory.h>
#include <cmath>
2017-12-29 00:06:22 -08:00
#include <cassert>
2016-03-04 15:03:26 -08:00
#include "zeus/Math.hpp"
2015-04-19 13:39:16 -07:00
2018-12-07 17:16:50 -08:00
namespace zeus {
2017-02-11 19:48:21 -08:00
const CVector3f CVector3f::skOne(1.f);
const CVector3f CVector3f::skNegOne(-1.f);
2015-04-19 13:39:16 -07:00
const CVector3f CVector3f::skZero;
2017-02-11 19:48:21 -08:00
const CVector3f CVector3f::skForward(0.f, 1.f, 0.f);
const CVector3f CVector3f::skBack(0.f, -1.f, 0.f);
const CVector3f CVector3f::skLeft(-1.f, 0.f, 0.f);
const CVector3f CVector3f::skRight(1.f, 0.f, 0.f);
const CVector3f CVector3f::skUp(0.f, 0.f, 1.f);
const CVector3f CVector3f::skDown(0.f, 0.f, -1.f);
const CVector3f CVector3f::skRadToDegVec(180.0f / M_PIF);
const CVector3f CVector3f::skDegToRadVec(M_PIF / 180.0f);
2017-03-17 16:30:14 -07:00
const CVector3d CVector3d::skZero(0.0, 0.0, 0.0);
2015-04-19 13:39:16 -07:00
2018-12-07 17:16:50 -08:00
CVector3f::CVector3f(const CVector3d& vec) : mSimd(vec.mSimd) {}
2016-03-04 15:03:26 -08:00
2018-12-07 17:16:50 -08:00
float CVector3f::getAngleDiff(const CVector3f& a, const CVector3f& b) {
float mag1 = a.magnitude();
float mag2 = b.magnitude();
2015-04-19 13:39:16 -07:00
2018-12-07 17:16:50 -08:00
if (!mag1 || !mag2)
return 0.f;
2015-04-19 13:39:16 -07:00
2018-12-07 17:16:50 -08:00
float dot = a.dot(b);
float theta = std::acos(dot / (mag1 * mag2));
return theta;
2015-04-19 13:39:16 -07:00
}
2018-12-07 17:16:50 -08:00
CVector3f CVector3f::slerp(const CVector3f& a, const CVector3f& b, float t) {
if (t <= 0.0f)
return a;
if (t >= 1.0f)
return b;
2015-04-19 13:39:16 -07:00
2018-12-07 17:16:50 -08:00
CVector3f ret;
2015-04-19 13:39:16 -07:00
2018-12-07 17:16:50 -08:00
float mag = std::sqrt(a.dot(a) * b.dot(b));
2015-04-19 13:39:16 -07:00
2018-12-07 17:16:50 -08:00
float prod = a.dot(b) / mag;
2015-04-19 13:39:16 -07:00
2018-12-07 17:16:50 -08:00
if (std::fabs(prod) < 1.0f) {
const double sign = (prod < 0.0f) ? -1.0f : 1.0f;
2015-04-19 13:39:16 -07:00
2018-12-07 17:16:50 -08:00
const double theta = acos(sign * prod);
const double s1 = sin(sign * t * theta);
const double d = 1.0 / sin(theta);
const double s0 = sin((1.0 - t) * theta);
2015-04-19 13:39:16 -07:00
2018-12-07 17:16:50 -08:00
ret = (a * s0 + b * s1) * d;
2015-04-19 13:39:16 -07:00
2018-12-07 17:16:50 -08:00
return ret;
}
return a;
2015-04-19 13:39:16 -07:00
}
2018-12-07 21:23:50 -08:00
} // namespace zeus