123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249 |
- #include "quat.h"
- #include "core/math/matrix3.h"
- #include "core/print_string.h"
- void Quat::set_euler_xyz(const Vector3 &p_euler) {
- real_t half_a1 = p_euler.x * 0.5;
- real_t half_a2 = p_euler.y * 0.5;
- real_t half_a3 = p_euler.z * 0.5;
-
-
-
- real_t cos_a1 = Math::cos(half_a1);
- real_t sin_a1 = Math::sin(half_a1);
- real_t cos_a2 = Math::cos(half_a2);
- real_t sin_a2 = Math::sin(half_a2);
- real_t cos_a3 = Math::cos(half_a3);
- real_t sin_a3 = Math::sin(half_a3);
- set(sin_a1 * cos_a2 * cos_a3 + sin_a2 * sin_a3 * cos_a1,
- -sin_a1 * sin_a3 * cos_a2 + sin_a2 * cos_a1 * cos_a3,
- sin_a1 * sin_a2 * cos_a3 + sin_a3 * cos_a1 * cos_a2,
- -sin_a1 * sin_a2 * sin_a3 + cos_a1 * cos_a2 * cos_a3);
- }
- Vector3 Quat::get_euler_xyz() const {
- Basis m(*this);
- return m.get_euler_xyz();
- }
- void Quat::set_euler_yxz(const Vector3 &p_euler) {
- real_t half_a1 = p_euler.y * 0.5;
- real_t half_a2 = p_euler.x * 0.5;
- real_t half_a3 = p_euler.z * 0.5;
-
-
-
- real_t cos_a1 = Math::cos(half_a1);
- real_t sin_a1 = Math::sin(half_a1);
- real_t cos_a2 = Math::cos(half_a2);
- real_t sin_a2 = Math::sin(half_a2);
- real_t cos_a3 = Math::cos(half_a3);
- real_t sin_a3 = Math::sin(half_a3);
- set(sin_a1 * cos_a2 * sin_a3 + cos_a1 * sin_a2 * cos_a3,
- sin_a1 * cos_a2 * cos_a3 - cos_a1 * sin_a2 * sin_a3,
- -sin_a1 * sin_a2 * cos_a3 + cos_a1 * cos_a2 * sin_a3,
- sin_a1 * sin_a2 * sin_a3 + cos_a1 * cos_a2 * cos_a3);
- }
- Vector3 Quat::get_euler_yxz() const {
- #ifdef MATH_CHECKS
- ERR_FAIL_COND_V(!is_normalized(), Vector3(0, 0, 0));
- #endif
- Basis m(*this);
- return m.get_euler_yxz();
- }
- void Quat::operator*=(const Quat &q) {
- set(w * q.x + x * q.w + y * q.z - z * q.y,
- w * q.y + y * q.w + z * q.x - x * q.z,
- w * q.z + z * q.w + x * q.y - y * q.x,
- w * q.w - x * q.x - y * q.y - z * q.z);
- }
- Quat Quat::operator*(const Quat &q) const {
- Quat r = *this;
- r *= q;
- return r;
- }
- real_t Quat::length() const {
- return Math::sqrt(length_squared());
- }
- void Quat::normalize() {
- *this /= length();
- }
- Quat Quat::normalized() const {
- return *this / length();
- }
- bool Quat::is_normalized() const {
- return Math::is_equal_approx(length_squared(), 1.0);
- }
- Quat Quat::inverse() const {
- #ifdef MATH_CHECKS
- ERR_FAIL_COND_V(!is_normalized(), Quat());
- #endif
- return Quat(-x, -y, -z, w);
- }
- Quat Quat::slerp(const Quat &q, const real_t &t) const {
- #ifdef MATH_CHECKS
- ERR_FAIL_COND_V(!is_normalized(), Quat());
- ERR_FAIL_COND_V(!q.is_normalized(), Quat());
- #endif
- Quat to1;
- real_t omega, cosom, sinom, scale0, scale1;
-
- cosom = dot(q);
-
- 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;
- }
-
- if ((1.0 - cosom) > CMP_EPSILON) {
-
- omega = Math::acos(cosom);
- sinom = Math::sin(omega);
- scale0 = Math::sin((1.0 - t) * omega) / sinom;
- scale1 = Math::sin(t * omega) / sinom;
- } else {
-
-
- scale0 = 1.0 - t;
- scale1 = t;
- }
-
- return Quat(
- 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 {
- #ifdef MATH_CHECKS
- ERR_FAIL_COND_V(!is_normalized(), Quat());
- ERR_FAIL_COND_V(!q.is_normalized(), Quat());
- #endif
- const Quat &from = *this;
- real_t dot = from.dot(q);
- if (Math::absf(dot) > 0.9999) return from;
- real_t theta = Math::acos(dot),
- sinT = 1.0 / Math::sin(theta),
- newFactor = Math::sin(t * theta) * sinT,
- invFactor = Math::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);
- }
- Quat Quat::cubic_slerp(const Quat &q, const Quat &prep, const Quat &postq, const real_t &t) const {
- #ifdef MATH_CHECKS
- ERR_FAIL_COND_V(!is_normalized(), Quat());
- ERR_FAIL_COND_V(!q.is_normalized(), Quat());
- #endif
-
- 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);
- }
- Quat::operator String() const {
- return String::num(x) + ", " + String::num(y) + ", " + String::num(z) + ", " + String::num(w);
- }
- void Quat::set_axis_angle(const Vector3 &axis, const real_t &angle) {
- #ifdef MATH_CHECKS
- ERR_FAIL_COND(!axis.is_normalized());
- #endif
- real_t d = axis.length();
- if (d == 0)
- set(0, 0, 0, 0);
- else {
- real_t sin_angle = Math::sin(angle * 0.5);
- real_t cos_angle = Math::cos(angle * 0.5);
- real_t s = sin_angle / d;
- set(axis.x * s, axis.y * s, axis.z * s,
- cos_angle);
- }
- }
|