diff options
Diffstat (limited to 'src/core/Quat.cpp')
-rw-r--r-- | src/core/Quat.cpp | 206 |
1 files changed, 99 insertions, 107 deletions
diff --git a/src/core/Quat.cpp b/src/core/Quat.cpp index 8739be3..abfe056 100644 --- a/src/core/Quat.cpp +++ b/src/core/Quat.cpp @@ -1,7 +1,7 @@ #include "Quat.hpp" +#include "Basis.hpp" #include "Defs.hpp" #include "Vector3.hpp" -#include "Basis.hpp" #include <cmath> @@ -77,53 +77,47 @@ Vector3 Quat::get_euler_yxz() const { return m.get_euler_yxz(); } -real_t Quat::length() const -{ +real_t Quat::length() const { return ::sqrt(length_squared()); } -void Quat::normalize() -{ +void Quat::normalize() { *this /= length(); } -Quat Quat::normalized() const -{ +Quat Quat::normalized() const { return *this / length(); } -Quat Quat::inverse() const -{ - return Quat( -x, -y, -z, w ); +Quat Quat::inverse() const { + return Quat(-x, -y, -z, w); } -Quat Quat::slerp(const Quat& q, const real_t& t) const { - - Quat to1; - real_t omega, cosom, sinom, scale0, scale1; +Quat Quat::slerp(const Quat &q, const real_t &t) const { + Quat to1; + real_t omega, cosom, sinom, scale0, scale1; // calc cosine cosom = dot(q); // adjust signs (if necessary) - if ( cosom <0.0 ) { + if (cosom < 0.0) { cosom = -cosom; - to1.x = - q.x; - to1.y = - q.y; - to1.z = - q.z; - to1.w = - q.w; - } else { + to1.x = -q.x; + to1.y = -q.y; + to1.z = -q.z; + to1.w = -q.w; + } else { to1.x = q.x; to1.y = q.y; to1.z = q.z; to1.w = q.w; } - // calculate coefficients - if ( (1.0 - cosom) > CMP_EPSILON ) { + if ((1.0 - cosom) > CMP_EPSILON) { // standard case (slerp) omega = ::acos(cosom); sinom = ::sin(omega); @@ -137,14 +131,13 @@ Quat Quat::slerp(const Quat& q, const real_t& t) const { } // calculate final values return Quat( - scale0 * x + scale1 * to1.x, - scale0 * y + scale1 * to1.y, - scale0 * z + scale1 * to1.z, - scale0 * w + scale1 * to1.w - ); + scale0 * x + scale1 * to1.x, + scale0 * y + scale1 * to1.y, + scale0 * z + scale1 * to1.z, + scale0 * w + scale1 * to1.w); } -Quat Quat::slerpni(const Quat& q, const real_t& t) const { +Quat Quat::slerpni(const Quat &q, const real_t &t) const { const Quat &from = *this; @@ -152,162 +145,161 @@ Quat Quat::slerpni(const Quat& q, const real_t& t) const { if (::fabs(dot) > 0.9999) return from; - real_t theta = ::acos(dot), - sinT = 1.0 / ::sin(theta), - newFactor = ::sin(t * theta) * sinT, - invFactor = ::sin((1.0 - t) * theta) * sinT; + real_t theta = ::acos(dot), + sinT = 1.0 / ::sin(theta), + newFactor = ::sin(t * theta) * sinT, + invFactor = ::sin((1.0 - t) * theta) * sinT; return Quat(invFactor * from.x + newFactor * q.x, - invFactor * from.y + newFactor * q.y, - invFactor * from.z + newFactor * q.z, - invFactor * from.w + newFactor * q.w); + invFactor * from.y + newFactor * q.y, + invFactor * from.z + newFactor * q.z, + invFactor * from.w + newFactor * q.w); } -Quat Quat::cubic_slerp(const Quat& q, const Quat& prep, const Quat& postq,const real_t& t) const -{ +Quat Quat::cubic_slerp(const Quat &q, const Quat &prep, const Quat &postq, const real_t &t) const { //the only way to do slerp :| - real_t t2 = (1.0-t)*t*2; - Quat sp = this->slerp(q,t); - Quat sq = prep.slerpni(postq,t); - return sp.slerpni(sq,t2); + real_t t2 = (1.0 - t) * t * 2; + Quat sp = this->slerp(q, t); + Quat sq = prep.slerpni(postq, t); + return sp.slerpni(sq, t2); } -void Quat::get_axis_and_angle(Vector3& r_axis, real_t &r_angle) const { +void Quat::get_axis_and_angle(Vector3 &r_axis, real_t &r_angle) const { r_angle = 2 * ::acos(w); - r_axis.x = x / ::sqrt(1-w*w); - r_axis.y = y / ::sqrt(1-w*w); - r_axis.z = z / ::sqrt(1-w*w); + r_axis.x = x / ::sqrt(1 - w * w); + r_axis.y = y / ::sqrt(1 - w * w); + r_axis.z = z / ::sqrt(1 - w * w); } - - -Quat Quat::operator*(const Vector3& v) const -{ - return Quat( w * v.x + y * v.z - z * v.y, - w * v.y + z * v.x - x * v.z, - w * v.z + x * v.y - y * v.x, - -x * v.x - y * v.y - z * v.z); +Quat Quat::operator*(const Vector3 &v) const { + return Quat(w * v.x + y * v.z - z * v.y, + w * v.y + z * v.x - x * v.z, + w * v.z + x * v.y - y * v.x, + -x * v.x - y * v.y - z * v.z); } -Vector3 Quat::xform(const Vector3& v) const { +Vector3 Quat::xform(const Vector3 &v) const { Quat q = *this * v; q *= this->inverse(); - return Vector3(q.x,q.y,q.z); + return Vector3(q.x, q.y, q.z); } - -Quat::operator String() const -{ +Quat::operator String() const { return String(); // @Todo } - -Quat::Quat(const Vector3& axis, const real_t& angle) -{ +Quat::Quat(const Vector3 &axis, const real_t &angle) { real_t d = axis.length(); - if (d==0) - set(0,0,0,0); + if (d == 0) + set(0, 0, 0, 0); else { real_t sin_angle = ::sin(angle * 0.5); real_t cos_angle = ::cos(angle * 0.5); real_t s = sin_angle / d; set(axis.x * s, axis.y * s, axis.z * s, - cos_angle); + cos_angle); } } -Quat::Quat(const Vector3& v0, const Vector3& v1) // shortest arc +Quat::Quat(const Vector3 &v0, const Vector3 &v1) // shortest arc { Vector3 c = v0.cross(v1); - real_t d = v0.dot(v1); + real_t d = v0.dot(v1); if (d < -1.0 + CMP_EPSILON) { - x=0; - y=1; - z=0; - w=0; + x = 0; + y = 1; + z = 0; + w = 0; } else { - real_t s = ::sqrt((1.0 + d) * 2.0); + real_t s = ::sqrt((1.0 + d) * 2.0); real_t rs = 1.0 / s; - x=c.x*rs; - y=c.y*rs; - z=c.z*rs; - w=s * 0.5; + x = c.x * rs; + y = c.y * rs; + z = c.z * rs; + w = s * 0.5; } } - -real_t Quat::dot(const Quat& q) const { - return x * q.x+y * q.y+z * q.z+w * q.w; +real_t Quat::dot(const Quat &q) const { + return x * q.x + y * q.y + z * q.z + w * q.w; } real_t Quat::length_squared() const { return dot(*this); } -void Quat::operator+=(const Quat& q) { - x += q.x; y += q.y; z += q.z; w += q.w; +void Quat::operator+=(const Quat &q) { + x += q.x; + y += q.y; + z += q.z; + w += q.w; } -void Quat::operator-=(const Quat& q) { - x -= q.x; y -= q.y; z -= q.z; w -= q.w; +void Quat::operator-=(const Quat &q) { + x -= q.x; + y -= q.y; + z -= q.z; + w -= q.w; } -void Quat::operator*=(const Quat& q) { - x *= q.x; y *= q.y; z *= q.z; w *= q.w; +void Quat::operator*=(const Quat &q) { + x *= q.x; + y *= q.y; + z *= q.z; + w *= q.w; } - -void Quat::operator*=(const real_t& s) { - x *= s; y *= s; z *= s; w *= s; +void Quat::operator*=(const real_t &s) { + x *= s; + y *= s; + z *= s; + w *= s; } - -void Quat::operator/=(const real_t& s) { +void Quat::operator/=(const real_t &s) { *this *= 1.0 / s; } -Quat Quat::operator+(const Quat& q2) const { - const Quat& q1 = *this; - return Quat( q1.x+q2.x, q1.y+q2.y, q1.z+q2.z, q1.w+q2.w ); +Quat Quat::operator+(const Quat &q2) const { + const Quat &q1 = *this; + return Quat(q1.x + q2.x, q1.y + q2.y, q1.z + q2.z, q1.w + q2.w); } -Quat Quat::operator-(const Quat& q2) const { - const Quat& q1 = *this; - return Quat( q1.x-q2.x, q1.y-q2.y, q1.z-q2.z, q1.w-q2.w); +Quat Quat::operator-(const Quat &q2) const { + const Quat &q1 = *this; + return Quat(q1.x - q2.x, q1.y - q2.y, q1.z - q2.z, q1.w - q2.w); } -Quat Quat::operator*(const Quat& q2) const { +Quat Quat::operator*(const Quat &q2) const { Quat q1 = *this; q1 *= q2; return q1; } - Quat Quat::operator-() const { - const Quat& q2 = *this; - return Quat( -q2.x, -q2.y, -q2.z, -q2.w); + const Quat &q2 = *this; + return Quat(-q2.x, -q2.y, -q2.z, -q2.w); } -Quat Quat::operator*(const real_t& s) const { +Quat Quat::operator*(const real_t &s) const { return Quat(x * s, y * s, z * s, w * s); } -Quat Quat::operator/(const real_t& s) const { +Quat Quat::operator/(const real_t &s) const { return *this * (1.0 / s); } - -bool Quat::operator==(const Quat& p_quat) const { - return x==p_quat.x && y==p_quat.y && z==p_quat.z && w==p_quat.w; +bool Quat::operator==(const Quat &p_quat) const { + return x == p_quat.x && y == p_quat.y && z == p_quat.z && w == p_quat.w; } -bool Quat::operator!=(const Quat& p_quat) const { - return x!=p_quat.x || y!=p_quat.y || z!=p_quat.z || w!=p_quat.w; +bool Quat::operator!=(const Quat &p_quat) const { + return x != p_quat.x || y != p_quat.y || z != p_quat.z || w != p_quat.w; } -} +} // namespace godot |