From 8acd13a456050ded00f0f264ff0aa91a304f6c54 Mon Sep 17 00:00:00 2001 From: Marcel Admiraal Date: Wed, 20 Jan 2021 07:02:02 +0000 Subject: Rename Quat to Quaternion --- core/core_constants.cpp | 2 +- core/io/marshalls.cpp | 8 +- core/io/packed_data_container.cpp | 2 +- core/io/resource_format_binary.cpp | 12 +- core/math/basis.cpp | 38 +++--- core/math/basis.h | 20 ++-- core/math/math_fieldwise.cpp | 4 +- core/math/quat.cpp | 232 ------------------------------------ core/math/quat.h | 238 ------------------------------------- core/math/quaternion.cpp | 232 ++++++++++++++++++++++++++++++++++++ core/math/quaternion.h | 238 +++++++++++++++++++++++++++++++++++++ core/math/transform_3d.cpp | 6 +- core/variant/method_ptrcall.h | 2 +- core/variant/type_info.h | 2 +- core/variant/typed_array.h | 4 +- core/variant/variant.cpp | 81 +++++++------ core/variant/variant.h | 10 +- core/variant/variant_call.cpp | 30 ++--- core/variant/variant_construct.cpp | 18 +-- core/variant/variant_internal.h | 32 ++--- core/variant/variant_op.cpp | 42 +++---- core/variant/variant_parser.cpp | 10 +- core/variant/variant_setget.cpp | 32 ++--- 23 files changed, 647 insertions(+), 648 deletions(-) delete mode 100644 core/math/quat.cpp delete mode 100644 core/math/quat.h create mode 100644 core/math/quaternion.cpp create mode 100644 core/math/quaternion.h (limited to 'core') diff --git a/core/core_constants.cpp b/core/core_constants.cpp index 30c2ed80ac..7fc09fc3a6 100644 --- a/core/core_constants.cpp +++ b/core/core_constants.cpp @@ -580,7 +580,7 @@ void register_global_constants() { BIND_CORE_ENUM_CONSTANT_CUSTOM("TYPE_VECTOR3I", Variant::VECTOR3I); BIND_CORE_ENUM_CONSTANT_CUSTOM("TYPE_TRANSFORM2D", Variant::TRANSFORM2D); BIND_CORE_ENUM_CONSTANT_CUSTOM("TYPE_PLANE", Variant::PLANE); - BIND_CORE_ENUM_CONSTANT_CUSTOM("TYPE_QUAT", Variant::QUAT); + BIND_CORE_ENUM_CONSTANT_CUSTOM("TYPE_QUATERNION", Variant::QUATERNION); BIND_CORE_ENUM_CONSTANT_CUSTOM("TYPE_AABB", Variant::AABB); BIND_CORE_ENUM_CONSTANT_CUSTOM("TYPE_BASIS", Variant::BASIS); BIND_CORE_ENUM_CONSTANT_CUSTOM("TYPE_TRANSFORM3D", Variant::TRANSFORM3D); diff --git a/core/io/marshalls.cpp b/core/io/marshalls.cpp index de32ffbcdc..b1988d6f2a 100644 --- a/core/io/marshalls.cpp +++ b/core/io/marshalls.cpp @@ -279,9 +279,9 @@ Error decode_variant(Variant &r_variant, const uint8_t *p_buffer, int p_len, int } } break; - case Variant::QUAT: { + case Variant::QUATERNION: { ERR_FAIL_COND_V(len < 4 * 4, ERR_INVALID_DATA); - Quat val; + Quaternion val; val.x = decode_float(&buf[0]); val.y = decode_float(&buf[4]); val.z = decode_float(&buf[8]); @@ -1099,9 +1099,9 @@ Error encode_variant(const Variant &p_variant, uint8_t *r_buffer, int &r_len, bo r_len += 4 * 4; } break; - case Variant::QUAT: { + case Variant::QUATERNION: { if (buf) { - Quat q = p_variant; + Quaternion q = p_variant; encode_float(q.x, &buf[0]); encode_float(q.y, &buf[4]); encode_float(q.z, &buf[8]); diff --git a/core/io/packed_data_container.cpp b/core/io/packed_data_container.cpp index 7565e8e01a..cf6a0b6027 100644 --- a/core/io/packed_data_container.cpp +++ b/core/io/packed_data_container.cpp @@ -227,7 +227,7 @@ uint32_t PackedDataContainer::_pack(const Variant &p_data, Vector &tmpd case Variant::VECTOR3: case Variant::TRANSFORM2D: case Variant::PLANE: - case Variant::QUAT: + case Variant::QUATERNION: case Variant::AABB: case Variant::BASIS: case Variant::TRANSFORM3D: diff --git a/core/io/resource_format_binary.cpp b/core/io/resource_format_binary.cpp index 59474a5bc5..385f15c0cf 100644 --- a/core/io/resource_format_binary.cpp +++ b/core/io/resource_format_binary.cpp @@ -51,7 +51,7 @@ enum { VARIANT_RECT2 = 11, VARIANT_VECTOR3 = 12, VARIANT_PLANE = 13, - VARIANT_QUAT = 14, + VARIANT_QUATERNION = 14, VARIANT_AABB = 15, VARIANT_MATRIX3 = 16, VARIANT_TRANSFORM = 17, @@ -199,8 +199,8 @@ Error ResourceLoaderBinary::parse_variant(Variant &r_v) { v.d = f->get_real(); r_v = v; } break; - case VARIANT_QUAT: { - Quat v; + case VARIANT_QUATERNION: { + Quaternion v; v.x = f->get_real(); v.y = f->get_real(); v.z = f->get_real(); @@ -1371,9 +1371,9 @@ void ResourceFormatSaverBinaryInstance::write_variant(FileAccess *f, const Varia f->store_real(val.d); } break; - case Variant::QUAT: { - f->store_32(VARIANT_QUAT); - Quat val = p_property; + case Variant::QUATERNION: { + f->store_32(VARIANT_QUATERNION); + Quaternion val = p_property; f->store_real(val.x); f->store_real(val.y); f->store_real(val.z); diff --git a/core/math/basis.cpp b/core/math/basis.cpp index 037378b9d7..7489da34d9 100644 --- a/core/math/basis.cpp +++ b/core/math/basis.cpp @@ -345,12 +345,12 @@ void Basis::rotate(const Vector3 &p_euler) { *this = rotated(p_euler); } -Basis Basis::rotated(const Quat &p_quat) const { - return Basis(p_quat) * (*this); +Basis Basis::rotated(const Quaternion &p_quaternion) const { + return Basis(p_quaternion) * (*this); } -void Basis::rotate(const Quat &p_quat) { - *this = rotated(p_quat); +void Basis::rotate(const Quaternion &p_quaternion) { + *this = rotated(p_quaternion); } Vector3 Basis::get_rotation_euler() const { @@ -367,7 +367,7 @@ Vector3 Basis::get_rotation_euler() const { return m.get_euler(); } -Quat Basis::get_rotation_quat() const { +Quaternion Basis::get_rotation_quaternion() const { // Assumes that the matrix can be decomposed into a proper rotation and scaling matrix as M = R.S, // and returns the Euler angles corresponding to the rotation part, complementing get_scale(). // See the comment in get_scale() for further information. @@ -378,7 +378,7 @@ Quat Basis::get_rotation_quat() const { m.scale(Vector3(-1, -1, -1)); } - return m.get_quat(); + return m.get_quaternion(); } void Basis::get_rotation_axis_angle(Vector3 &p_axis, real_t &p_angle) const { @@ -770,9 +770,9 @@ Basis::operator String() const { return mtx; } -Quat Basis::get_quat() const { +Quaternion Basis::get_quaternion() const { #ifdef MATH_CHECKS - ERR_FAIL_COND_V_MSG(!is_rotation(), Quat(), "Basis must be normalized in order to be casted to a Quaternion. Use get_rotation_quat() or call orthonormalized() instead."); + ERR_FAIL_COND_V_MSG(!is_rotation(), Quaternion(), "Basis must be normalized in order to be casted to a Quaternion. Use get_rotation_quaternion() or call orthonormalized() instead."); #endif /* Allow getting a quaternion from an unnormalized transform */ Basis m = *this; @@ -803,7 +803,7 @@ Quat Basis::get_quat() const { temp[k] = (m.elements[k][i] + m.elements[i][k]) * s; } - return Quat(temp[0], temp[1], temp[2], temp[3]); + return Quaternion(temp[0], temp[1], temp[2], temp[3]); } static const Basis _ortho_bases[24] = { @@ -945,13 +945,13 @@ void Basis::get_axis_angle(Vector3 &r_axis, real_t &r_angle) const { r_angle = angle; } -void Basis::set_quat(const Quat &p_quat) { - real_t d = p_quat.length_squared(); +void Basis::set_quaternion(const Quaternion &p_quaternion) { + real_t d = p_quaternion.length_squared(); real_t s = 2.0 / d; - real_t xs = p_quat.x * s, ys = p_quat.y * s, zs = p_quat.z * s; - real_t wx = p_quat.w * xs, wy = p_quat.w * ys, wz = p_quat.w * zs; - real_t xx = p_quat.x * xs, xy = p_quat.x * ys, xz = p_quat.x * zs; - real_t yy = p_quat.y * ys, yz = p_quat.y * zs, zz = p_quat.z * zs; + real_t xs = p_quaternion.x * s, ys = p_quaternion.y * s, zs = p_quaternion.z * s; + real_t wx = p_quaternion.w * xs, wy = p_quaternion.w * ys, wz = p_quaternion.w * zs; + real_t xx = p_quaternion.x * xs, xy = p_quaternion.x * ys, xz = p_quaternion.x * zs; + real_t yy = p_quaternion.y * ys, yz = p_quaternion.y * zs, zz = p_quaternion.z * zs; set(1.0 - (yy + zz), xy - wz, xz + wy, xy + wz, 1.0 - (xx + zz), yz - wx, xz - wy, yz + wx, 1.0 - (xx + yy)); @@ -997,9 +997,9 @@ void Basis::set_euler_scale(const Vector3 &p_euler, const Vector3 &p_scale) { rotate(p_euler); } -void Basis::set_quat_scale(const Quat &p_quat, const Vector3 &p_scale) { +void Basis::set_quaternion_scale(const Quaternion &p_quaternion, const Vector3 &p_scale) { set_diagonal(p_scale); - rotate(p_quat); + rotate(p_quaternion); } void Basis::set_diagonal(const Vector3 &p_diag) { @@ -1018,8 +1018,8 @@ void Basis::set_diagonal(const Vector3 &p_diag) { Basis Basis::slerp(const Basis &p_to, const real_t &p_weight) const { //consider scale - Quat from(*this); - Quat to(p_to); + Quaternion from(*this); + Quaternion to(p_to); Basis b(from.slerp(to, p_weight)); b.elements[0] *= Math::lerp(elements[0].length(), p_to.elements[0].length(), p_weight); diff --git a/core/math/basis.h b/core/math/basis.h index 56f6227313..3736047dd3 100644 --- a/core/math/basis.h +++ b/core/math/basis.h @@ -31,7 +31,7 @@ #ifndef BASIS_H #define BASIS_H -#include "core/math/quat.h" +#include "core/math/quaternion.h" #include "core/math/vector3.h" class Basis { @@ -79,13 +79,13 @@ public: void rotate(const Vector3 &p_euler); Basis rotated(const Vector3 &p_euler) const; - void rotate(const Quat &p_quat); - Basis rotated(const Quat &p_quat) const; + void rotate(const Quaternion &p_quaternion); + Basis rotated(const Quaternion &p_quaternion) const; Vector3 get_rotation_euler() const; void get_rotation_axis_angle(Vector3 &p_axis, real_t &p_angle) const; void get_rotation_axis_angle_local(Vector3 &p_axis, real_t &p_angle) const; - Quat get_rotation_quat() const; + Quaternion get_rotation_quaternion() const; Vector3 get_rotation() const { return get_rotation_euler(); }; Vector3 rotref_posscale_decomposition(Basis &rotref) const; @@ -108,8 +108,8 @@ public: Vector3 get_euler_zyx() const; void set_euler_zyx(const Vector3 &p_euler); - Quat get_quat() const; - void set_quat(const Quat &p_quat); + Quaternion get_quaternion() const; + void set_quaternion(const Quaternion &p_quaternion); Vector3 get_euler() const { return get_euler_yxz(); } void set_euler(const Vector3 &p_euler) { set_euler_yxz(p_euler); } @@ -132,7 +132,7 @@ public: void set_axis_angle_scale(const Vector3 &p_axis, real_t p_phi, const Vector3 &p_scale); void set_euler_scale(const Vector3 &p_euler, const Vector3 &p_scale); - void set_quat_scale(const Quat &p_quat, const Vector3 &p_scale); + void set_quaternion_scale(const Quaternion &p_quaternion, const Vector3 &p_scale); // transposed dot products _FORCE_INLINE_ real_t tdotx(const Vector3 &v) const { @@ -240,10 +240,10 @@ public: #endif Basis diagonalize(); - operator Quat() const { return get_quat(); } + operator Quaternion() const { return get_quaternion(); } - Basis(const Quat &p_quat) { set_quat(p_quat); }; - Basis(const Quat &p_quat, const Vector3 &p_scale) { set_quat_scale(p_quat, p_scale); } + Basis(const Quaternion &p_quaternion) { set_quaternion(p_quaternion); }; + Basis(const Quaternion &p_quaternion, const Vector3 &p_scale) { set_quaternion_scale(p_quaternion, p_scale); } Basis(const Vector3 &p_euler) { set_euler(p_euler); } Basis(const Vector3 &p_euler, const Vector3 &p_scale) { set_euler_scale(p_euler, p_scale); } diff --git a/core/math/math_fieldwise.cpp b/core/math/math_fieldwise.cpp index f2baef1a59..570c57e254 100644 --- a/core/math/math_fieldwise.cpp +++ b/core/math/math_fieldwise.cpp @@ -88,8 +88,8 @@ Variant fieldwise_assign(const Variant &p_target, const Variant &p_source, const return target; } - case Variant::QUAT: { - SETUP_TYPE(Quat) + case Variant::QUATERNION: { + SETUP_TYPE(Quaternion) /**/ TRY_TRANSFER_FIELD("x", x) else TRY_TRANSFER_FIELD("y", y) diff --git a/core/math/quat.cpp b/core/math/quat.cpp deleted file mode 100644 index 3982a0b993..0000000000 --- a/core/math/quat.cpp +++ /dev/null @@ -1,232 +0,0 @@ -/*************************************************************************/ -/* quat.cpp */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ -/* */ -/* Permission is hereby granted, free of charge, to any person obtaining */ -/* a copy of this software and associated documentation files (the */ -/* "Software"), to deal in the Software without restriction, including */ -/* without limitation the rights to use, copy, modify, merge, publish, */ -/* distribute, sublicense, and/or sell copies of the Software, and to */ -/* permit persons to whom the Software is furnished to do so, subject to */ -/* the following conditions: */ -/* */ -/* The above copyright notice and this permission notice shall be */ -/* included in all copies or substantial portions of the Software. */ -/* */ -/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ -/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ -/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ -/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ -/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ -/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ -/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -/*************************************************************************/ - -#include "quat.h" - -#include "core/math/basis.h" -#include "core/string/print_string.h" - -// get_euler_xyz returns a vector containing the Euler angles in the format -// (ax,ay,az), where ax is the angle of rotation around x axis, -// and similar for other axes. -// This implementation uses XYZ convention (Z is the first rotation). -Vector3 Quat::get_euler_xyz() const { - Basis m(*this); - return m.get_euler_xyz(); -} - -// get_euler_yxz returns a vector containing the Euler angles in the format -// (ax,ay,az), where ax is the angle of rotation around x axis, -// and similar for other axes. -// This implementation uses YXZ convention (Z is the first rotation). -Vector3 Quat::get_euler_yxz() const { -#ifdef MATH_CHECKS - ERR_FAIL_COND_V_MSG(!is_normalized(), Vector3(0, 0, 0), "The quaternion must be normalized."); -#endif - Basis m(*this); - return m.get_euler_yxz(); -} - -void Quat::operator*=(const Quat &p_q) { - real_t xx = w * p_q.x + x * p_q.w + y * p_q.z - z * p_q.y; - real_t yy = w * p_q.y + y * p_q.w + z * p_q.x - x * p_q.z; - real_t zz = w * p_q.z + z * p_q.w + x * p_q.y - y * p_q.x; - w = w * p_q.w - x * p_q.x - y * p_q.y - z * p_q.z; - x = xx; - y = yy; - z = zz; -} - -Quat Quat::operator*(const Quat &p_q) const { - Quat r = *this; - r *= p_q; - return r; -} - -bool Quat::is_equal_approx(const Quat &p_quat) const { - return Math::is_equal_approx(x, p_quat.x) && Math::is_equal_approx(y, p_quat.y) && Math::is_equal_approx(z, p_quat.z) && Math::is_equal_approx(w, p_quat.w); -} - -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, (real_t)UNIT_EPSILON); //use less epsilon -} - -Quat Quat::inverse() const { -#ifdef MATH_CHECKS - ERR_FAIL_COND_V_MSG(!is_normalized(), Quat(), "The quaternion must be normalized."); -#endif - return Quat(-x, -y, -z, w); -} - -Quat Quat::slerp(const Quat &p_to, const real_t &p_weight) const { -#ifdef MATH_CHECKS - ERR_FAIL_COND_V_MSG(!is_normalized(), Quat(), "The start quaternion must be normalized."); - ERR_FAIL_COND_V_MSG(!p_to.is_normalized(), Quat(), "The end quaternion must be normalized."); -#endif - Quat to1; - real_t omega, cosom, sinom, scale0, scale1; - - // calc cosine - cosom = dot(p_to); - - // adjust signs (if necessary) - if (cosom < 0.0) { - cosom = -cosom; - to1.x = -p_to.x; - to1.y = -p_to.y; - to1.z = -p_to.z; - to1.w = -p_to.w; - } else { - to1.x = p_to.x; - to1.y = p_to.y; - to1.z = p_to.z; - to1.w = p_to.w; - } - - // calculate coefficients - - if ((1.0 - cosom) > CMP_EPSILON) { - // standard case (slerp) - omega = Math::acos(cosom); - sinom = Math::sin(omega); - scale0 = Math::sin((1.0 - p_weight) * omega) / sinom; - scale1 = Math::sin(p_weight * omega) / sinom; - } else { - // "from" and "to" quaternions are very close - // ... so we can do a linear interpolation - scale0 = 1.0 - p_weight; - scale1 = p_weight; - } - // 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); -} - -Quat Quat::slerpni(const Quat &p_to, const real_t &p_weight) const { -#ifdef MATH_CHECKS - ERR_FAIL_COND_V_MSG(!is_normalized(), Quat(), "The start quaternion must be normalized."); - ERR_FAIL_COND_V_MSG(!p_to.is_normalized(), Quat(), "The end quaternion must be normalized."); -#endif - const Quat &from = *this; - - real_t dot = from.dot(p_to); - - if (Math::absf(dot) > 0.9999) { - return from; - } - - real_t theta = Math::acos(dot), - sinT = 1.0 / Math::sin(theta), - newFactor = Math::sin(p_weight * theta) * sinT, - invFactor = Math::sin((1.0 - p_weight) * theta) * sinT; - - return Quat(invFactor * from.x + newFactor * p_to.x, - invFactor * from.y + newFactor * p_to.y, - invFactor * from.z + newFactor * p_to.z, - invFactor * from.w + newFactor * p_to.w); -} - -Quat Quat::cubic_slerp(const Quat &p_b, const Quat &p_pre_a, const Quat &p_post_b, const real_t &p_weight) const { -#ifdef MATH_CHECKS - ERR_FAIL_COND_V_MSG(!is_normalized(), Quat(), "The start quaternion must be normalized."); - ERR_FAIL_COND_V_MSG(!p_b.is_normalized(), Quat(), "The end quaternion must be normalized."); -#endif - //the only way to do slerp :| - real_t t2 = (1.0 - p_weight) * p_weight * 2; - Quat sp = this->slerp(p_b, p_weight); - Quat sq = p_pre_a.slerpni(p_post_b, p_weight); - return sp.slerpni(sq, t2); -} - -Quat::operator String() const { - return String::num(x) + ", " + String::num(y) + ", " + String::num(z) + ", " + String::num(w); -} - -Quat::Quat(const Vector3 &p_axis, real_t p_angle) { -#ifdef MATH_CHECKS - ERR_FAIL_COND_MSG(!p_axis.is_normalized(), "The axis Vector3 must be normalized."); -#endif - real_t d = p_axis.length(); - if (d == 0) { - x = 0; - y = 0; - z = 0; - w = 0; - } else { - real_t sin_angle = Math::sin(p_angle * 0.5); - real_t cos_angle = Math::cos(p_angle * 0.5); - real_t s = sin_angle / d; - x = p_axis.x * s; - y = p_axis.y * s; - z = p_axis.z * s; - w = cos_angle; - } -} - -// Euler constructor expects a vector containing the Euler angles in the format -// (ax, ay, az), where ax is the angle of rotation around x axis, -// and similar for other axes. -// This implementation uses YXZ convention (Z is the first rotation). -Quat::Quat(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; - - // R = Y(a1).X(a2).Z(a3) convention for Euler angles. - // Conversion to quaternion as listed in https://ntrs.nasa.gov/archive/nasa/casi.ntrs.nasa.gov/19770024290.pdf (page A-6) - // a3 is the angle of the first rotation, following the notation in this reference. - - 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); - - x = sin_a1 * cos_a2 * sin_a3 + cos_a1 * sin_a2 * cos_a3; - y = sin_a1 * cos_a2 * cos_a3 - cos_a1 * sin_a2 * sin_a3; - z = -sin_a1 * sin_a2 * cos_a3 + cos_a1 * cos_a2 * sin_a3; - w = sin_a1 * sin_a2 * sin_a3 + cos_a1 * cos_a2 * cos_a3; -} diff --git a/core/math/quat.h b/core/math/quat.h deleted file mode 100644 index d9b130c050..0000000000 --- a/core/math/quat.h +++ /dev/null @@ -1,238 +0,0 @@ -/*************************************************************************/ -/* quat.h */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ -/* */ -/* Permission is hereby granted, free of charge, to any person obtaining */ -/* a copy of this software and associated documentation files (the */ -/* "Software"), to deal in the Software without restriction, including */ -/* without limitation the rights to use, copy, modify, merge, publish, */ -/* distribute, sublicense, and/or sell copies of the Software, and to */ -/* permit persons to whom the Software is furnished to do so, subject to */ -/* the following conditions: */ -/* */ -/* The above copyright notice and this permission notice shall be */ -/* included in all copies or substantial portions of the Software. */ -/* */ -/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ -/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ -/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ -/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ -/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ -/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ -/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -/*************************************************************************/ - -#ifndef QUAT_H -#define QUAT_H - -#include "core/math/math_defs.h" -#include "core/math/math_funcs.h" -#include "core/math/vector3.h" -#include "core/string/ustring.h" - -class Quat { -public: - union { - struct { - real_t x; - real_t y; - real_t z; - real_t w; - }; - real_t components[4] = { 0, 0, 0, 1.0 }; - }; - - _FORCE_INLINE_ real_t &operator[](int idx) { - return components[idx]; - } - _FORCE_INLINE_ const real_t &operator[](int idx) const { - return components[idx]; - } - _FORCE_INLINE_ real_t length_squared() const; - bool is_equal_approx(const Quat &p_quat) const; - real_t length() const; - void normalize(); - Quat normalized() const; - bool is_normalized() const; - Quat inverse() const; - _FORCE_INLINE_ real_t dot(const Quat &p_q) const; - - Vector3 get_euler_xyz() const; - Vector3 get_euler_yxz() const; - Vector3 get_euler() const { return get_euler_yxz(); }; - - Quat slerp(const Quat &p_to, const real_t &p_weight) const; - Quat slerpni(const Quat &p_to, const real_t &p_weight) const; - Quat cubic_slerp(const Quat &p_b, const Quat &p_pre_a, const Quat &p_post_b, const real_t &p_weight) const; - - _FORCE_INLINE_ void get_axis_angle(Vector3 &r_axis, real_t &r_angle) const { - r_angle = 2 * Math::acos(w); - real_t r = ((real_t)1) / Math::sqrt(1 - w * w); - r_axis.x = x * r; - r_axis.y = y * r; - r_axis.z = z * r; - } - - void operator*=(const Quat &p_q); - Quat operator*(const Quat &p_q) const; - - 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); - } - - _FORCE_INLINE_ Vector3 xform(const Vector3 &v) const { -#ifdef MATH_CHECKS - ERR_FAIL_COND_V_MSG(!is_normalized(), v, "The quaternion must be normalized."); -#endif - Vector3 u(x, y, z); - Vector3 uv = u.cross(v); - return v + ((uv * w) + u.cross(uv)) * ((real_t)2); - } - - _FORCE_INLINE_ Vector3 xform_inv(const Vector3 &v) const { - return inverse().xform(v); - } - - _FORCE_INLINE_ void operator+=(const Quat &p_q); - _FORCE_INLINE_ void operator-=(const Quat &p_q); - _FORCE_INLINE_ void operator*=(const real_t &s); - _FORCE_INLINE_ void operator/=(const real_t &s); - _FORCE_INLINE_ Quat operator+(const Quat &q2) const; - _FORCE_INLINE_ Quat operator-(const Quat &q2) const; - _FORCE_INLINE_ Quat operator-() const; - _FORCE_INLINE_ Quat operator*(const real_t &s) const; - _FORCE_INLINE_ Quat operator/(const real_t &s) const; - - _FORCE_INLINE_ bool operator==(const Quat &p_quat) const; - _FORCE_INLINE_ bool operator!=(const Quat &p_quat) const; - - operator String() const; - - _FORCE_INLINE_ Quat() {} - - _FORCE_INLINE_ Quat(real_t p_x, real_t p_y, real_t p_z, real_t p_w) : - x(p_x), - y(p_y), - z(p_z), - w(p_w) { - } - - Quat(const Vector3 &p_axis, real_t p_angle); - - Quat(const Vector3 &p_euler); - - Quat(const Quat &p_q) : - x(p_q.x), - y(p_q.y), - z(p_q.z), - w(p_q.w) { - } - - Quat &operator=(const Quat &p_q) { - x = p_q.x; - y = p_q.y; - z = p_q.z; - w = p_q.w; - return *this; - } - - Quat(const Vector3 &v0, const Vector3 &v1) // shortest arc - { - Vector3 c = v0.cross(v1); - real_t d = v0.dot(v1); - - if (d < -1.0 + CMP_EPSILON) { - x = 0; - y = 1; - z = 0; - w = 0; - } else { - real_t s = Math::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; - } - } -}; - -real_t Quat::dot(const Quat &p_q) const { - return x * p_q.x + y * p_q.y + z * p_q.z + w * p_q.w; -} - -real_t Quat::length_squared() const { - return dot(*this); -} - -void Quat::operator+=(const Quat &p_q) { - x += p_q.x; - y += p_q.y; - z += p_q.z; - w += p_q.w; -} - -void Quat::operator-=(const Quat &p_q) { - x -= p_q.x; - y -= p_q.y; - z -= p_q.z; - w -= p_q.w; -} - -void Quat::operator*=(const real_t &s) { - x *= s; - y *= s; - z *= s; - w *= 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 { - const Quat &q2 = *this; - return Quat(-q2.x, -q2.y, -q2.z, -q2.w); -} - -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 { - 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; -} - -_FORCE_INLINE_ Quat operator*(const real_t &p_real, const Quat &p_quat) { - return p_quat * p_real; -} - -#endif // QUAT_H diff --git a/core/math/quaternion.cpp b/core/math/quaternion.cpp new file mode 100644 index 0000000000..8de3d0cc2a --- /dev/null +++ b/core/math/quaternion.cpp @@ -0,0 +1,232 @@ +/*************************************************************************/ +/* quaternion.cpp */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/*************************************************************************/ + +#include "quaternion.h" + +#include "core/math/basis.h" +#include "core/string/print_string.h" + +// get_euler_xyz returns a vector containing the Euler angles in the format +// (ax,ay,az), where ax is the angle of rotation around x axis, +// and similar for other axes. +// This implementation uses XYZ convention (Z is the first rotation). +Vector3 Quaternion::get_euler_xyz() const { + Basis m(*this); + return m.get_euler_xyz(); +} + +// get_euler_yxz returns a vector containing the Euler angles in the format +// (ax,ay,az), where ax is the angle of rotation around x axis, +// and similar for other axes. +// This implementation uses YXZ convention (Z is the first rotation). +Vector3 Quaternion::get_euler_yxz() const { +#ifdef MATH_CHECKS + ERR_FAIL_COND_V_MSG(!is_normalized(), Vector3(0, 0, 0), "The quaternion must be normalized."); +#endif + Basis m(*this); + return m.get_euler_yxz(); +} + +void Quaternion::operator*=(const Quaternion &p_q) { + real_t xx = w * p_q.x + x * p_q.w + y * p_q.z - z * p_q.y; + real_t yy = w * p_q.y + y * p_q.w + z * p_q.x - x * p_q.z; + real_t zz = w * p_q.z + z * p_q.w + x * p_q.y - y * p_q.x; + w = w * p_q.w - x * p_q.x - y * p_q.y - z * p_q.z; + x = xx; + y = yy; + z = zz; +} + +Quaternion Quaternion::operator*(const Quaternion &p_q) const { + Quaternion r = *this; + r *= p_q; + return r; +} + +bool Quaternion::is_equal_approx(const Quaternion &p_quaternion) const { + return Math::is_equal_approx(x, p_quaternion.x) && Math::is_equal_approx(y, p_quaternion.y) && Math::is_equal_approx(z, p_quaternion.z) && Math::is_equal_approx(w, p_quaternion.w); +} + +real_t Quaternion::length() const { + return Math::sqrt(length_squared()); +} + +void Quaternion::normalize() { + *this /= length(); +} + +Quaternion Quaternion::normalized() const { + return *this / length(); +} + +bool Quaternion::is_normalized() const { + return Math::is_equal_approx(length_squared(), 1, (real_t)UNIT_EPSILON); //use less epsilon +} + +Quaternion Quaternion::inverse() const { +#ifdef MATH_CHECKS + ERR_FAIL_COND_V_MSG(!is_normalized(), Quaternion(), "The quaternion must be normalized."); +#endif + return Quaternion(-x, -y, -z, w); +} + +Quaternion Quaternion::slerp(const Quaternion &p_to, const real_t &p_weight) const { +#ifdef MATH_CHECKS + ERR_FAIL_COND_V_MSG(!is_normalized(), Quaternion(), "The start quaternion must be normalized."); + ERR_FAIL_COND_V_MSG(!p_to.is_normalized(), Quaternion(), "The end quaternion must be normalized."); +#endif + Quaternion to1; + real_t omega, cosom, sinom, scale0, scale1; + + // calc cosine + cosom = dot(p_to); + + // adjust signs (if necessary) + if (cosom < 0.0) { + cosom = -cosom; + to1.x = -p_to.x; + to1.y = -p_to.y; + to1.z = -p_to.z; + to1.w = -p_to.w; + } else { + to1.x = p_to.x; + to1.y = p_to.y; + to1.z = p_to.z; + to1.w = p_to.w; + } + + // calculate coefficients + + if ((1.0 - cosom) > CMP_EPSILON) { + // standard case (slerp) + omega = Math::acos(cosom); + sinom = Math::sin(omega); + scale0 = Math::sin((1.0 - p_weight) * omega) / sinom; + scale1 = Math::sin(p_weight * omega) / sinom; + } else { + // "from" and "to" quaternions are very close + // ... so we can do a linear interpolation + scale0 = 1.0 - p_weight; + scale1 = p_weight; + } + // calculate final values + return Quaternion( + scale0 * x + scale1 * to1.x, + scale0 * y + scale1 * to1.y, + scale0 * z + scale1 * to1.z, + scale0 * w + scale1 * to1.w); +} + +Quaternion Quaternion::slerpni(const Quaternion &p_to, const real_t &p_weight) const { +#ifdef MATH_CHECKS + ERR_FAIL_COND_V_MSG(!is_normalized(), Quaternion(), "The start quaternion must be normalized."); + ERR_FAIL_COND_V_MSG(!p_to.is_normalized(), Quaternion(), "The end quaternion must be normalized."); +#endif + const Quaternion &from = *this; + + real_t dot = from.dot(p_to); + + if (Math::absf(dot) > 0.9999) { + return from; + } + + real_t theta = Math::acos(dot), + sinT = 1.0 / Math::sin(theta), + newFactor = Math::sin(p_weight * theta) * sinT, + invFactor = Math::sin((1.0 - p_weight) * theta) * sinT; + + return Quaternion(invFactor * from.x + newFactor * p_to.x, + invFactor * from.y + newFactor * p_to.y, + invFactor * from.z + newFactor * p_to.z, + invFactor * from.w + newFactor * p_to.w); +} + +Quaternion Quaternion::cubic_slerp(const Quaternion &p_b, const Quaternion &p_pre_a, const Quaternion &p_post_b, const real_t &p_weight) const { +#ifdef MATH_CHECKS + ERR_FAIL_COND_V_MSG(!is_normalized(), Quaternion(), "The start quaternion must be normalized."); + ERR_FAIL_COND_V_MSG(!p_b.is_normalized(), Quaternion(), "The end quaternion must be normalized."); +#endif + //the only way to do slerp :| + real_t t2 = (1.0 - p_weight) * p_weight * 2; + Quaternion sp = this->slerp(p_b, p_weight); + Quaternion sq = p_pre_a.slerpni(p_post_b, p_weight); + return sp.slerpni(sq, t2); +} + +Quaternion::operator String() const { + return String::num(x) + ", " + String::num(y) + ", " + String::num(z) + ", " + String::num(w); +} + +Quaternion::Quaternion(const Vector3 &p_axis, real_t p_angle) { +#ifdef MATH_CHECKS + ERR_FAIL_COND_MSG(!p_axis.is_normalized(), "The axis Vector3 must be normalized."); +#endif + real_t d = p_axis.length(); + if (d == 0) { + x = 0; + y = 0; + z = 0; + w = 0; + } else { + real_t sin_angle = Math::sin(p_angle * 0.5); + real_t cos_angle = Math::cos(p_angle * 0.5); + real_t s = sin_angle / d; + x = p_axis.x * s; + y = p_axis.y * s; + z = p_axis.z * s; + w = cos_angle; + } +} + +// Euler constructor expects a vector containing the Euler angles in the format +// (ax, ay, az), where ax is the angle of rotation around x axis, +// and similar for other axes. +// This implementation uses YXZ convention (Z is the first rotation). +Quaternion::Quaternion(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; + + // R = Y(a1).X(a2).Z(a3) convention for Euler angles. + // Conversion to quaternion as listed in https://ntrs.nasa.gov/archive/nasa/casi.ntrs.nasa.gov/19770024290.pdf (page A-6) + // a3 is the angle of the first rotation, following the notation in this reference. + + 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); + + x = sin_a1 * cos_a2 * sin_a3 + cos_a1 * sin_a2 * cos_a3; + y = sin_a1 * cos_a2 * cos_a3 - cos_a1 * sin_a2 * sin_a3; + z = -sin_a1 * sin_a2 * cos_a3 + cos_a1 * cos_a2 * sin_a3; + w = sin_a1 * sin_a2 * sin_a3 + cos_a1 * cos_a2 * cos_a3; +} diff --git a/core/math/quaternion.h b/core/math/quaternion.h new file mode 100644 index 0000000000..796214b79e --- /dev/null +++ b/core/math/quaternion.h @@ -0,0 +1,238 @@ +/*************************************************************************/ +/* quaternion.h */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/*************************************************************************/ + +#ifndef QUAT_H +#define QUAT_H + +#include "core/math/math_defs.h" +#include "core/math/math_funcs.h" +#include "core/math/vector3.h" +#include "core/string/ustring.h" + +class Quaternion { +public: + union { + struct { + real_t x; + real_t y; + real_t z; + real_t w; + }; + real_t components[4] = { 0, 0, 0, 1.0 }; + }; + + _FORCE_INLINE_ real_t &operator[](int idx) { + return components[idx]; + } + _FORCE_INLINE_ const real_t &operator[](int idx) const { + return components[idx]; + } + _FORCE_INLINE_ real_t length_squared() const; + bool is_equal_approx(const Quaternion &p_quaternion) const; + real_t length() const; + void normalize(); + Quaternion normalized() const; + bool is_normalized() const; + Quaternion inverse() const; + _FORCE_INLINE_ real_t dot(const Quaternion &p_q) const; + + Vector3 get_euler_xyz() const; + Vector3 get_euler_yxz() const; + Vector3 get_euler() const { return get_euler_yxz(); }; + + Quaternion slerp(const Quaternion &p_to, const real_t &p_weight) const; + Quaternion slerpni(const Quaternion &p_to, const real_t &p_weight) const; + Quaternion cubic_slerp(const Quaternion &p_b, const Quaternion &p_pre_a, const Quaternion &p_post_b, const real_t &p_weight) const; + + _FORCE_INLINE_ void get_axis_angle(Vector3 &r_axis, real_t &r_angle) const { + r_angle = 2 * Math::acos(w); + real_t r = ((real_t)1) / Math::sqrt(1 - w * w); + r_axis.x = x * r; + r_axis.y = y * r; + r_axis.z = z * r; + } + + void operator*=(const Quaternion &p_q); + Quaternion operator*(const Quaternion &p_q) const; + + Quaternion operator*(const Vector3 &v) const { + return Quaternion(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); + } + + _FORCE_INLINE_ Vector3 xform(const Vector3 &v) const { +#ifdef MATH_CHECKS + ERR_FAIL_COND_V_MSG(!is_normalized(), v, "The quaternion must be normalized."); +#endif + Vector3 u(x, y, z); + Vector3 uv = u.cross(v); + return v + ((uv * w) + u.cross(uv)) * ((real_t)2); + } + + _FORCE_INLINE_ Vector3 xform_inv(const Vector3 &v) const { + return inverse().xform(v); + } + + _FORCE_INLINE_ void operator+=(const Quaternion &p_q); + _FORCE_INLINE_ void operator-=(const Quaternion &p_q); + _FORCE_INLINE_ void operator*=(const real_t &s); + _FORCE_INLINE_ void operator/=(const real_t &s); + _FORCE_INLINE_ Quaternion operator+(const Quaternion &q2) const; + _FORCE_INLINE_ Quaternion operator-(const Quaternion &q2) const; + _FORCE_INLINE_ Quaternion operator-() const; + _FORCE_INLINE_ Quaternion operator*(const real_t &s) const; + _FORCE_INLINE_ Quaternion operator/(const real_t &s) const; + + _FORCE_INLINE_ bool operator==(const Quaternion &p_quaternion) const; + _FORCE_INLINE_ bool operator!=(const Quaternion &p_quaternion) const; + + operator String() const; + + _FORCE_INLINE_ Quaternion() {} + + _FORCE_INLINE_ Quaternion(real_t p_x, real_t p_y, real_t p_z, real_t p_w) : + x(p_x), + y(p_y), + z(p_z), + w(p_w) { + } + + Quaternion(const Vector3 &p_axis, real_t p_angle); + + Quaternion(const Vector3 &p_euler); + + Quaternion(const Quaternion &p_q) : + x(p_q.x), + y(p_q.y), + z(p_q.z), + w(p_q.w) { + } + + Quaternion &operator=(const Quaternion &p_q) { + x = p_q.x; + y = p_q.y; + z = p_q.z; + w = p_q.w; + return *this; + } + + Quaternion(const Vector3 &v0, const Vector3 &v1) // shortest arc + { + Vector3 c = v0.cross(v1); + real_t d = v0.dot(v1); + + if (d < -1.0 + CMP_EPSILON) { + x = 0; + y = 1; + z = 0; + w = 0; + } else { + real_t s = Math::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; + } + } +}; + +real_t Quaternion::dot(const Quaternion &p_q) const { + return x * p_q.x + y * p_q.y + z * p_q.z + w * p_q.w; +} + +real_t Quaternion::length_squared() const { + return dot(*this); +} + +void Quaternion::operator+=(const Quaternion &p_q) { + x += p_q.x; + y += p_q.y; + z += p_q.z; + w += p_q.w; +} + +void Quaternion::operator-=(const Quaternion &p_q) { + x -= p_q.x; + y -= p_q.y; + z -= p_q.z; + w -= p_q.w; +} + +void Quaternion::operator*=(const real_t &s) { + x *= s; + y *= s; + z *= s; + w *= s; +} + +void Quaternion::operator/=(const real_t &s) { + *this *= 1.0 / s; +} + +Quaternion Quaternion::operator+(const Quaternion &q2) const { + const Quaternion &q1 = *this; + return Quaternion(q1.x + q2.x, q1.y + q2.y, q1.z + q2.z, q1.w + q2.w); +} + +Quaternion Quaternion::operator-(const Quaternion &q2) const { + const Quaternion &q1 = *this; + return Quaternion(q1.x - q2.x, q1.y - q2.y, q1.z - q2.z, q1.w - q2.w); +} + +Quaternion Quaternion::operator-() const { + const Quaternion &q2 = *this; + return Quaternion(-q2.x, -q2.y, -q2.z, -q2.w); +} + +Quaternion Quaternion::operator*(const real_t &s) const { + return Quaternion(x * s, y * s, z * s, w * s); +} + +Quaternion Quaternion::operator/(const real_t &s) const { + return *this * (1.0 / s); +} + +bool Quaternion::operator==(const Quaternion &p_quaternion) const { + return x == p_quaternion.x && y == p_quaternion.y && z == p_quaternion.z && w == p_quaternion.w; +} + +bool Quaternion::operator!=(const Quaternion &p_quaternion) const { + return x != p_quaternion.x || y != p_quaternion.y || z != p_quaternion.z || w != p_quaternion.w; +} + +_FORCE_INLINE_ Quaternion operator*(const real_t &p_real, const Quaternion &p_quaternion) { + return p_quaternion * p_real; +} + +#endif // QUAT_H diff --git a/core/math/transform_3d.cpp b/core/math/transform_3d.cpp index 2611d6accf..210f0b81bb 100644 --- a/core/math/transform_3d.cpp +++ b/core/math/transform_3d.cpp @@ -112,15 +112,15 @@ Transform3D Transform3D::interpolate_with(const Transform3D &p_transform, real_t /* not sure if very "efficient" but good enough? */ Vector3 src_scale = basis.get_scale(); - Quat src_rot = basis.get_rotation_quat(); + Quaternion src_rot = basis.get_rotation_quaternion(); Vector3 src_loc = origin; Vector3 dst_scale = p_transform.basis.get_scale(); - Quat dst_rot = p_transform.basis.get_rotation_quat(); + Quaternion dst_rot = p_transform.basis.get_rotation_quaternion(); Vector3 dst_loc = p_transform.origin; Transform3D interp; - interp.basis.set_quat_scale(src_rot.slerp(dst_rot, p_c).normalized(), src_scale.lerp(dst_scale, p_c)); + interp.basis.set_quaternion_scale(src_rot.slerp(dst_rot, p_c).normalized(), src_scale.lerp(dst_scale, p_c)); interp.origin = src_loc.lerp(dst_loc, p_c); return interp; diff --git a/core/variant/method_ptrcall.h b/core/variant/method_ptrcall.h index f863962111..d4ec5e570c 100644 --- a/core/variant/method_ptrcall.h +++ b/core/variant/method_ptrcall.h @@ -122,7 +122,7 @@ MAKE_PTRARG_BY_REFERENCE(Vector3); MAKE_PTRARG_BY_REFERENCE(Vector3i); MAKE_PTRARG(Transform2D); MAKE_PTRARG_BY_REFERENCE(Plane); -MAKE_PTRARG(Quat); +MAKE_PTRARG(Quaternion); MAKE_PTRARG_BY_REFERENCE(AABB); MAKE_PTRARG_BY_REFERENCE(Basis); MAKE_PTRARG_BY_REFERENCE(Transform3D); diff --git a/core/variant/type_info.h b/core/variant/type_info.h index 5712c90f4c..76cb065d10 100644 --- a/core/variant/type_info.h +++ b/core/variant/type_info.h @@ -146,7 +146,7 @@ MAKE_TYPE_INFO(Rect2i, Variant::RECT2I) MAKE_TYPE_INFO(Vector3i, Variant::VECTOR3I) MAKE_TYPE_INFO(Transform2D, Variant::TRANSFORM2D) MAKE_TYPE_INFO(Plane, Variant::PLANE) -MAKE_TYPE_INFO(Quat, Variant::QUAT) +MAKE_TYPE_INFO(Quaternion, Variant::QUATERNION) MAKE_TYPE_INFO(AABB, Variant::AABB) MAKE_TYPE_INFO(Basis, Variant::BASIS) MAKE_TYPE_INFO(Transform3D, Variant::TRANSFORM3D) diff --git a/core/variant/typed_array.h b/core/variant/typed_array.h index 95c0578543..900dcf7689 100644 --- a/core/variant/typed_array.h +++ b/core/variant/typed_array.h @@ -98,7 +98,7 @@ MAKE_TYPED_ARRAY(Vector3, Variant::VECTOR3) MAKE_TYPED_ARRAY(Vector3i, Variant::VECTOR3I) MAKE_TYPED_ARRAY(Transform2D, Variant::TRANSFORM2D) MAKE_TYPED_ARRAY(Plane, Variant::PLANE) -MAKE_TYPED_ARRAY(Quat, Variant::QUAT) +MAKE_TYPED_ARRAY(Quaternion, Variant::QUATERNION) MAKE_TYPED_ARRAY(AABB, Variant::AABB) MAKE_TYPED_ARRAY(Basis, Variant::BASIS) MAKE_TYPED_ARRAY(Transform3D, Variant::TRANSFORM3D) @@ -196,7 +196,7 @@ MAKE_TYPED_ARRAY_INFO(Vector3, Variant::VECTOR3) MAKE_TYPED_ARRAY_INFO(Vector3i, Variant::VECTOR3I) MAKE_TYPED_ARRAY_INFO(Transform2D, Variant::TRANSFORM2D) MAKE_TYPED_ARRAY_INFO(Plane, Variant::PLANE) -MAKE_TYPED_ARRAY_INFO(Quat, Variant::QUAT) +MAKE_TYPED_ARRAY_INFO(Quaternion, Variant::QUATERNION) MAKE_TYPED_ARRAY_INFO(AABB, Variant::AABB) MAKE_TYPED_ARRAY_INFO(Basis, Variant::BASIS) MAKE_TYPED_ARRAY_INFO(Transform3D, Variant::TRANSFORM3D) diff --git a/core/variant/variant.cpp b/core/variant/variant.cpp index d77b7ef140..2bde08742c 100644 --- a/core/variant/variant.cpp +++ b/core/variant/variant.cpp @@ -91,8 +91,8 @@ String Variant::get_type_name(Variant::Type p_type) { case AABB: { return "AABB"; } break; - case QUAT: { - return "Quat"; + case QUATERNION: { + return "Quaternion"; } break; case BASIS: { @@ -300,7 +300,7 @@ bool Variant::can_convert(Variant::Type p_type_from, Variant::Type p_type_to) { } break; - case QUAT: { + case QUATERNION: { static const Type valid[] = { BASIS, NIL @@ -311,7 +311,7 @@ bool Variant::can_convert(Variant::Type p_type_from, Variant::Type p_type_to) { } break; case BASIS: { static const Type valid[] = { - QUAT, + QUATERNION, VECTOR3, NIL }; @@ -322,7 +322,7 @@ bool Variant::can_convert(Variant::Type p_type_from, Variant::Type p_type_to) { case TRANSFORM3D: { static const Type valid[] = { TRANSFORM2D, - QUAT, + QUATERNION, BASIS, NIL }; @@ -607,7 +607,7 @@ bool Variant::can_convert_strict(Variant::Type p_type_from, Variant::Type p_type } break; - case QUAT: { + case QUATERNION: { static const Type valid[] = { BASIS, NIL @@ -618,7 +618,7 @@ bool Variant::can_convert_strict(Variant::Type p_type_from, Variant::Type p_type } break; case BASIS: { static const Type valid[] = { - QUAT, + QUATERNION, VECTOR3, NIL }; @@ -629,7 +629,7 @@ bool Variant::can_convert_strict(Variant::Type p_type_from, Variant::Type p_type case TRANSFORM3D: { static const Type valid[] = { TRANSFORM2D, - QUAT, + QUATERNION, BASIS, NIL }; @@ -873,8 +873,8 @@ bool Variant::is_zero() const { case AABB: { return *_data._aabb == ::AABB(); } break; - case QUAT: { - return *reinterpret_cast(_data._mem) == Quat(); + case QUATERNION: { + return *reinterpret_cast(_data._mem) == Quaternion(); } break; case BASIS: { @@ -1092,8 +1092,8 @@ void Variant::reference(const Variant &p_variant) { case AABB: { _data._aabb = memnew(::AABB(*p_variant._data._aabb)); } break; - case QUAT: { - memnew_placement(_data._mem, Quat(*reinterpret_cast(p_variant._data._mem))); + case QUATERNION: { + memnew_placement(_data._mem, Quaternion(*reinterpret_cast(p_variant._data._mem))); } break; case BASIS: { @@ -1254,8 +1254,8 @@ void Variant::zero() { case PLANE: *reinterpret_cast(this->_data._mem) = Plane(); break; - case QUAT: - *reinterpret_cast(this->_data._mem) = Quat(); + case QUATERNION: + *reinterpret_cast(this->_data._mem) = Quaternion(); break; case COLOR: *reinterpret_cast(this->_data._mem) = Color(); @@ -1275,7 +1275,7 @@ void Variant::_clear_internal() { // no point, they don't allocate memory VECTOR3, PLANE, - QUAT, + QUATERNION, COLOR, VECTOR2, RECT2 @@ -1653,11 +1653,10 @@ String Variant::stringify(List &stack) const { return "(" + operator Vector3i() + ")"; case PLANE: return operator Plane(); - //case QUAT: case AABB: return operator ::AABB(); - case QUAT: - return "(" + operator Quat() + ")"; + case QUATERNION: + return "(" + operator Quaternion() + ")"; case BASIS: { Basis mat3 = operator Basis(); @@ -1956,8 +1955,8 @@ Variant::operator ::AABB() const { Variant::operator Basis() const { if (type == BASIS) { return *_data._basis; - } else if (type == QUAT) { - return *reinterpret_cast(_data._mem); + } else if (type == QUATERNION) { + return *reinterpret_cast(_data._mem); } else if (type == VECTOR3) { return Basis(*reinterpret_cast(_data._mem)); } else if (type == TRANSFORM3D) { // unexposed in Variant::can_convert? @@ -1967,15 +1966,15 @@ Variant::operator Basis() const { } } -Variant::operator Quat() const { - if (type == QUAT) { - return *reinterpret_cast(_data._mem); +Variant::operator Quaternion() const { + if (type == QUATERNION) { + return *reinterpret_cast(_data._mem); } else if (type == BASIS) { return *_data._basis; } else if (type == TRANSFORM3D) { return _data._transform3d->basis; } else { - return Quat(); + return Quaternion(); } } @@ -1984,8 +1983,8 @@ Variant::operator Transform3D() const { return *_data._transform3d; } else if (type == BASIS) { return Transform3D(*_data._basis, Vector3()); - } else if (type == QUAT) { - return Transform3D(Basis(*reinterpret_cast(_data._mem)), Vector3()); + } else if (type == QUATERNION) { + return Transform3D(Basis(*reinterpret_cast(_data._mem)), Vector3()); } else if (type == TRANSFORM2D) { const Transform2D &t = *_data._transform2d; Transform3D m; @@ -2495,9 +2494,9 @@ Variant::Variant(const Basis &p_matrix) { _data._basis = memnew(Basis(p_matrix)); } -Variant::Variant(const Quat &p_quat) { - type = QUAT; - memnew_placement(_data._mem, Quat(p_quat)); +Variant::Variant(const Quaternion &p_quaternion) { + type = QUATERNION; + memnew_placement(_data._mem, Quaternion(p_quaternion)); } Variant::Variant(const Transform3D &p_transform) { @@ -2739,8 +2738,8 @@ void Variant::operator=(const Variant &p_variant) { case AABB: { *_data._aabb = *(p_variant._data._aabb); } break; - case QUAT: { - *reinterpret_cast(_data._mem) = *reinterpret_cast(p_variant._data._mem); + case QUATERNION: { + *reinterpret_cast(_data._mem) = *reinterpret_cast(p_variant._data._mem); } break; case BASIS: { *_data._basis = *(p_variant._data._basis); @@ -2916,11 +2915,11 @@ uint32_t Variant::hash() const { return hash; } break; - case QUAT: { - uint32_t hash = hash_djb2_one_float(reinterpret_cast(_data._mem)->x); - hash = hash_djb2_one_float(reinterpret_cast(_data._mem)->y, hash); - hash = hash_djb2_one_float(reinterpret_cast(_data._mem)->z, hash); - return hash_djb2_one_float(reinterpret_cast(_data._mem)->w, hash); + case QUATERNION: { + uint32_t hash = hash_djb2_one_float(reinterpret_cast(_data._mem)->x); + hash = hash_djb2_one_float(reinterpret_cast(_data._mem)->y, hash); + hash = hash_djb2_one_float(reinterpret_cast(_data._mem)->z, hash); + return hash_djb2_one_float(reinterpret_cast(_data._mem)->w, hash); } break; case BASIS: { @@ -3127,7 +3126,7 @@ uint32_t Variant::hash() const { (hash_compare_scalar((p_lhs).y, (p_rhs).y)) && \ (hash_compare_scalar((p_lhs).z, (p_rhs).z)) -#define hash_compare_quat(p_lhs, p_rhs) \ +#define hash_compare_quaternion(p_lhs, p_rhs) \ (hash_compare_scalar((p_lhs).x, (p_rhs).x)) && \ (hash_compare_scalar((p_lhs).y, (p_rhs).y)) && \ (hash_compare_scalar((p_lhs).z, (p_rhs).z)) && \ @@ -3235,11 +3234,11 @@ bool Variant::hash_compare(const Variant &p_variant) const { } break; - case QUAT: { - const Quat *l = reinterpret_cast(_data._mem); - const Quat *r = reinterpret_cast(p_variant._data._mem); + case QUATERNION: { + const Quaternion *l = reinterpret_cast(_data._mem); + const Quaternion *r = reinterpret_cast(p_variant._data._mem); - return hash_compare_quat(*l, *r); + return hash_compare_quaternion(*l, *r); } break; case BASIS: { diff --git a/core/variant/variant.h b/core/variant/variant.h index 4945e967eb..75316da63f 100644 --- a/core/variant/variant.h +++ b/core/variant/variant.h @@ -37,7 +37,7 @@ #include "core/math/color.h" #include "core/math/face3.h" #include "core/math/plane.h" -#include "core/math/quat.h" +#include "core/math/quaternion.h" #include "core/math/transform_2d.h" #include "core/math/transform_3d.h" #include "core/math/vector3.h" @@ -88,7 +88,7 @@ public: VECTOR3I, TRANSFORM2D, PLANE, - QUAT, + QUATERNION, AABB, BASIS, TRANSFORM3D, @@ -225,7 +225,7 @@ private: false, //VECTOR3I, true, //TRANSFORM2D, false, //PLANE, - false, //QUAT, + false, //QUATERNION, true, //AABB, true, //BASIS, true, //TRANSFORM, @@ -320,7 +320,7 @@ public: operator Vector3i() const; operator Plane() const; operator ::AABB() const; - operator Quat() const; + operator Quaternion() const; operator Basis() const; operator Transform2D() const; operator Transform3D() const; @@ -392,7 +392,7 @@ public: Variant(const Vector3i &p_vector3i); Variant(const Plane &p_plane); Variant(const ::AABB &p_aabb); - Variant(const Quat &p_quat); + Variant(const Quaternion &p_quat); Variant(const Basis &p_matrix); Variant(const Transform2D &p_transform); Variant(const Transform3D &p_transform); diff --git a/core/variant/variant_call.cpp b/core/variant/variant_call.cpp index a93a166d33..72dd888dff 100644 --- a/core/variant/variant_call.cpp +++ b/core/variant/variant_call.cpp @@ -1544,19 +1544,19 @@ static void _register_variant_builtin_methods() { bind_methodv(Plane, intersects_ray, &Plane::intersects_ray_bind, sarray("from", "dir"), varray()); bind_methodv(Plane, intersects_segment, &Plane::intersects_segment_bind, sarray("from", "to"), varray()); - /* Quat */ - - bind_method(Quat, length, sarray(), varray()); - bind_method(Quat, length_squared, sarray(), varray()); - bind_method(Quat, normalized, sarray(), varray()); - bind_method(Quat, is_normalized, sarray(), varray()); - bind_method(Quat, is_equal_approx, sarray("to"), varray()); - bind_method(Quat, inverse, sarray(), varray()); - bind_method(Quat, dot, sarray("with"), varray()); - bind_method(Quat, slerp, sarray("to", "weight"), varray()); - bind_method(Quat, slerpni, sarray("to", "weight"), varray()); - bind_method(Quat, cubic_slerp, sarray("b", "pre_a", "post_b", "weight"), varray()); - bind_method(Quat, get_euler, sarray(), varray()); + /* Quaternion */ + + bind_method(Quaternion, length, sarray(), varray()); + bind_method(Quaternion, length_squared, sarray(), varray()); + bind_method(Quaternion, normalized, sarray(), varray()); + bind_method(Quaternion, is_normalized, sarray(), varray()); + bind_method(Quaternion, is_equal_approx, sarray("to"), varray()); + bind_method(Quaternion, inverse, sarray(), varray()); + bind_method(Quaternion, dot, sarray("with"), varray()); + bind_method(Quaternion, slerp, sarray("to", "weight"), varray()); + bind_method(Quaternion, slerpni, sarray("to", "weight"), varray()); + bind_method(Quaternion, cubic_slerp, sarray("b", "pre_a", "post_b", "weight"), varray()); + bind_method(Quaternion, get_euler, sarray(), varray()); /* Color */ @@ -1668,7 +1668,7 @@ static void _register_variant_builtin_methods() { bind_method(Basis, get_orthogonal_index, sarray(), varray()); bind_method(Basis, slerp, sarray("to", "weight"), varray()); bind_method(Basis, is_equal_approx, sarray("b"), varray()); - bind_method(Basis, get_rotation_quat, sarray(), varray()); + bind_method(Basis, get_rotation_quaternion, sarray(), varray()); /* AABB */ @@ -2047,7 +2047,7 @@ static void _register_variant_builtin_methods() { _VariantCall::add_variant_constant(Variant::PLANE, "PLANE_XZ", Plane(Vector3(0, 1, 0), 0)); _VariantCall::add_variant_constant(Variant::PLANE, "PLANE_XY", Plane(Vector3(0, 0, 1), 0)); - _VariantCall::add_variant_constant(Variant::QUAT, "IDENTITY", Quat(0, 0, 0, 1)); + _VariantCall::add_variant_constant(Variant::QUATERNION, "IDENTITY", Quaternion(0, 0, 0, 1)); } void Variant::_register_variant_methods() { diff --git a/core/variant/variant_construct.cpp b/core/variant/variant_construct.cpp index 699496ce19..f66f33ef93 100644 --- a/core/variant/variant_construct.cpp +++ b/core/variant/variant_construct.cpp @@ -62,7 +62,7 @@ MAKE_PTRCONSTRUCT(Vector3); MAKE_PTRCONSTRUCT(Vector3i); MAKE_PTRCONSTRUCT(Transform2D); MAKE_PTRCONSTRUCT(Plane); -MAKE_PTRCONSTRUCT(Quat); +MAKE_PTRCONSTRUCT(Quaternion); MAKE_PTRCONSTRUCT(AABB); MAKE_PTRCONSTRUCT(Basis); MAKE_PTRCONSTRUCT(Transform3D); @@ -659,13 +659,13 @@ void Variant::_register_variant_constructors() { add_constructor>(sarray("point1", "point2", "point3")); add_constructor>(sarray("a", "b", "c", "d")); - add_constructor>(sarray()); - add_constructor>(sarray("from")); - add_constructor>(sarray("from")); - add_constructor>(sarray("euler")); - add_constructor>(sarray("axis", "angle")); - add_constructor>(sarray("arc_from", "arc_to")); - add_constructor>(sarray("x", "y", "z", "w")); + add_constructor>(sarray()); + add_constructor>(sarray("from")); + add_constructor>(sarray("from")); + add_constructor>(sarray("euler")); + add_constructor>(sarray("axis", "angle")); + add_constructor>(sarray("arc_from", "arc_to")); + add_constructor>(sarray("x", "y", "z", "w")); add_constructor>(sarray()); add_constructor>(sarray("from")); @@ -673,7 +673,7 @@ void Variant::_register_variant_constructors() { add_constructor>(sarray()); add_constructor>(sarray("from")); - add_constructor>(sarray("from")); + add_constructor>(sarray("from")); add_constructor>(sarray("euler")); add_constructor>(sarray("axis", "phi")); add_constructor>(sarray("x_axis", "y_axis", "z_axis")); diff --git a/core/variant/variant_internal.h b/core/variant/variant_internal.h index 1eaa211b37..9e5811a082 100644 --- a/core/variant/variant_internal.h +++ b/core/variant/variant_internal.h @@ -138,8 +138,8 @@ public: _FORCE_INLINE_ static const Transform2D *get_transform2d(const Variant *v) { return v->_data._transform2d; } _FORCE_INLINE_ static Plane *get_plane(Variant *v) { return reinterpret_cast(v->_data._mem); } _FORCE_INLINE_ static const Plane *get_plane(const Variant *v) { return reinterpret_cast(v->_data._mem); } - _FORCE_INLINE_ static Quat *get_quat(Variant *v) { return reinterpret_cast(v->_data._mem); } - _FORCE_INLINE_ static const Quat *get_quat(const Variant *v) { return reinterpret_cast(v->_data._mem); } + _FORCE_INLINE_ static Quaternion *get_quaternion(Variant *v) { return reinterpret_cast(v->_data._mem); } + _FORCE_INLINE_ static const Quaternion *get_quaternion(const Variant *v) { return reinterpret_cast(v->_data._mem); } _FORCE_INLINE_ static ::AABB *get_aabb(Variant *v) { return v->_data._aabb; } _FORCE_INLINE_ static const ::AABB *get_aabb(const Variant *v) { return v->_data._aabb; } _FORCE_INLINE_ static Basis *get_basis(Variant *v) { return v->_data._basis; } @@ -324,8 +324,8 @@ public: return get_transform(v); case Variant::TRANSFORM2D: return get_transform2d(v); - case Variant::QUAT: - return get_quat(v); + case Variant::QUATERNION: + return get_quaternion(v); case Variant::PLANE: return get_plane(v); case Variant::BASIS: @@ -402,8 +402,8 @@ public: return get_transform(v); case Variant::TRANSFORM2D: return get_transform2d(v); - case Variant::QUAT: - return get_quat(v); + case Variant::QUATERNION: + return get_quaternion(v); case Variant::PLANE: return get_plane(v); case Variant::BASIS: @@ -602,9 +602,9 @@ struct VariantGetInternalPtr { }; template <> -struct VariantGetInternalPtr { - static Quat *get_ptr(Variant *v) { return VariantInternal::get_quat(v); } - static const Quat *get_ptr(const Variant *v) { return VariantInternal::get_quat(v); } +struct VariantGetInternalPtr { + static Quaternion *get_ptr(Variant *v) { return VariantInternal::get_quaternion(v); } + static const Quaternion *get_ptr(const Variant *v) { return VariantInternal::get_quaternion(v); } }; template <> @@ -831,9 +831,9 @@ struct VariantInternalAccessor { }; template <> -struct VariantInternalAccessor { - static _FORCE_INLINE_ const Quat &get(const Variant *v) { return *VariantInternal::get_quat(v); } - static _FORCE_INLINE_ void set(Variant *v, const Quat &p_value) { *VariantInternal::get_quat(v) = p_value; } +struct VariantInternalAccessor { + static _FORCE_INLINE_ const Quaternion &get(const Variant *v) { return *VariantInternal::get_quaternion(v); } + static _FORCE_INLINE_ void set(Variant *v, const Quaternion &p_value) { *VariantInternal::get_quaternion(v) = p_value; } }; template <> @@ -1067,8 +1067,8 @@ struct VariantInitializer { }; template <> -struct VariantInitializer { - static _FORCE_INLINE_ void init(Variant *v) { VariantInternal::init_generic(v); } +struct VariantInitializer { + static _FORCE_INLINE_ void init(Variant *v) { VariantInternal::init_generic(v); } }; template <> @@ -1241,8 +1241,8 @@ struct VariantZeroAssigner { }; template <> -struct VariantZeroAssigner { - static _FORCE_INLINE_ void zero(Variant *v) { *VariantInternal::get_quat(v) = Quat(); } +struct VariantZeroAssigner { + static _FORCE_INLINE_ void zero(Variant *v) { *VariantInternal::get_quaternion(v) = Quaternion(); } }; template <> diff --git a/core/variant/variant_op.cpp b/core/variant/variant_op.cpp index cce0177e20..10d0a83014 100644 --- a/core/variant/variant_op.cpp +++ b/core/variant/variant_op.cpp @@ -1395,7 +1395,7 @@ void Variant::_register_variant_operators() { register_op>(Variant::OP_ADD, Variant::VECTOR2I, Variant::VECTOR2I); register_op>(Variant::OP_ADD, Variant::VECTOR3, Variant::VECTOR3); register_op>(Variant::OP_ADD, Variant::VECTOR3I, Variant::VECTOR3I); - register_op>(Variant::OP_ADD, Variant::QUAT, Variant::QUAT); + register_op>(Variant::OP_ADD, Variant::QUATERNION, Variant::QUATERNION); register_op>(Variant::OP_ADD, Variant::COLOR, Variant::COLOR); register_op(Variant::OP_ADD, Variant::ARRAY, Variant::ARRAY); register_op>(Variant::OP_ADD, Variant::PACKED_BYTE_ARRAY, Variant::PACKED_BYTE_ARRAY); @@ -1416,7 +1416,7 @@ void Variant::_register_variant_operators() { register_op>(Variant::OP_SUBTRACT, Variant::VECTOR2I, Variant::VECTOR2I); register_op>(Variant::OP_SUBTRACT, Variant::VECTOR3, Variant::VECTOR3); register_op>(Variant::OP_SUBTRACT, Variant::VECTOR3I, Variant::VECTOR3I); - register_op>(Variant::OP_SUBTRACT, Variant::QUAT, Variant::QUAT); + register_op>(Variant::OP_SUBTRACT, Variant::QUATERNION, Variant::QUATERNION); register_op>(Variant::OP_SUBTRACT, Variant::COLOR, Variant::COLOR); register_op>(Variant::OP_MULTIPLY, Variant::INT, Variant::INT); @@ -1449,9 +1449,9 @@ void Variant::_register_variant_operators() { register_op>(Variant::OP_MULTIPLY, Variant::VECTOR3I, Variant::INT); register_op>(Variant::OP_MULTIPLY, Variant::VECTOR3I, Variant::FLOAT); - register_op>(Variant::OP_MULTIPLY, Variant::QUAT, Variant::QUAT); - register_op>(Variant::OP_MULTIPLY, Variant::QUAT, Variant::INT); - register_op>(Variant::OP_MULTIPLY, Variant::QUAT, Variant::FLOAT); + register_op>(Variant::OP_MULTIPLY, Variant::QUATERNION, Variant::QUATERNION); + register_op>(Variant::OP_MULTIPLY, Variant::QUATERNION, Variant::INT); + register_op>(Variant::OP_MULTIPLY, Variant::QUATERNION, Variant::FLOAT); register_op>(Variant::OP_MULTIPLY, Variant::COLOR, Variant::COLOR); register_op>(Variant::OP_MULTIPLY, Variant::COLOR, Variant::INT); @@ -1477,13 +1477,13 @@ void Variant::_register_variant_operators() { register_op>(Variant::OP_MULTIPLY, Variant::BASIS, Variant::VECTOR3); register_op>(Variant::OP_MULTIPLY, Variant::VECTOR3, Variant::BASIS); - register_op>(Variant::OP_MULTIPLY, Variant::QUAT, Variant::QUAT); - register_op>(Variant::OP_MULTIPLY, Variant::QUAT, Variant::INT); - register_op>(Variant::OP_MULTIPLY, Variant::INT, Variant::QUAT); - register_op>(Variant::OP_MULTIPLY, Variant::QUAT, Variant::FLOAT); - register_op>(Variant::OP_MULTIPLY, Variant::FLOAT, Variant::QUAT); - register_op>(Variant::OP_MULTIPLY, Variant::QUAT, Variant::VECTOR3); - register_op>(Variant::OP_MULTIPLY, Variant::VECTOR3, Variant::QUAT); + register_op>(Variant::OP_MULTIPLY, Variant::QUATERNION, Variant::QUATERNION); + register_op>(Variant::OP_MULTIPLY, Variant::QUATERNION, Variant::INT); + register_op>(Variant::OP_MULTIPLY, Variant::INT, Variant::QUATERNION); + register_op>(Variant::OP_MULTIPLY, Variant::QUATERNION, Variant::FLOAT); + register_op>(Variant::OP_MULTIPLY, Variant::FLOAT, Variant::QUATERNION); + register_op>(Variant::OP_MULTIPLY, Variant::QUATERNION, Variant::VECTOR3); + register_op>(Variant::OP_MULTIPLY, Variant::VECTOR3, Variant::QUATERNION); register_op>(Variant::OP_MULTIPLY, Variant::COLOR, Variant::COLOR); register_op>(Variant::OP_MULTIPLY, Variant::COLOR, Variant::INT); @@ -1516,8 +1516,8 @@ void Variant::_register_variant_operators() { register_op>(Variant::OP_DIVIDE, Variant::VECTOR3I, Variant::FLOAT); register_op>(Variant::OP_DIVIDE, Variant::VECTOR3I, Variant::INT); - register_op>(Variant::OP_DIVIDE, Variant::QUAT, Variant::FLOAT); - register_op>(Variant::OP_DIVIDE, Variant::QUAT, Variant::INT); + register_op>(Variant::OP_DIVIDE, Variant::QUATERNION, Variant::FLOAT); + register_op>(Variant::OP_DIVIDE, Variant::QUATERNION, Variant::INT); register_op>(Variant::OP_DIVIDE, Variant::COLOR, Variant::COLOR); register_op>(Variant::OP_DIVIDE, Variant::COLOR, Variant::FLOAT); @@ -1544,7 +1544,7 @@ void Variant::_register_variant_operators() { register_op>(Variant::OP_MODULE, Variant::STRING, Variant::VECTOR3I); register_op>(Variant::OP_MODULE, Variant::STRING, Variant::TRANSFORM2D); register_op>(Variant::OP_MODULE, Variant::STRING, Variant::PLANE); - register_op>(Variant::OP_MODULE, Variant::STRING, Variant::QUAT); + register_op>(Variant::OP_MODULE, Variant::STRING, Variant::QUATERNION); register_op>(Variant::OP_MODULE, Variant::STRING, Variant::AABB); register_op>(Variant::OP_MODULE, Variant::STRING, Variant::BASIS); register_op>(Variant::OP_MODULE, Variant::STRING, Variant::TRANSFORM3D); @@ -1574,7 +1574,7 @@ void Variant::_register_variant_operators() { register_op>(Variant::OP_NEGATE, Variant::VECTOR2I, Variant::NIL); register_op>(Variant::OP_NEGATE, Variant::VECTOR3, Variant::NIL); register_op>(Variant::OP_NEGATE, Variant::VECTOR3I, Variant::NIL); - register_op>(Variant::OP_NEGATE, Variant::QUAT, Variant::NIL); + register_op>(Variant::OP_NEGATE, Variant::QUATERNION, Variant::NIL); register_op>(Variant::OP_NEGATE, Variant::PLANE, Variant::NIL); register_op>(Variant::OP_NEGATE, Variant::COLOR, Variant::NIL); @@ -1584,7 +1584,7 @@ void Variant::_register_variant_operators() { register_op>(Variant::OP_POSITIVE, Variant::VECTOR2I, Variant::NIL); register_op>(Variant::OP_POSITIVE, Variant::VECTOR3, Variant::NIL); register_op>(Variant::OP_POSITIVE, Variant::VECTOR3I, Variant::NIL); - register_op>(Variant::OP_POSITIVE, Variant::QUAT, Variant::NIL); + register_op>(Variant::OP_POSITIVE, Variant::QUATERNION, Variant::NIL); register_op>(Variant::OP_POSITIVE, Variant::PLANE, Variant::NIL); register_op>(Variant::OP_POSITIVE, Variant::COLOR, Variant::NIL); @@ -1612,7 +1612,7 @@ void Variant::_register_variant_operators() { register_op>(Variant::OP_EQUAL, Variant::VECTOR3I, Variant::VECTOR3I); register_op>(Variant::OP_EQUAL, Variant::TRANSFORM2D, Variant::TRANSFORM2D); register_op>(Variant::OP_EQUAL, Variant::PLANE, Variant::PLANE); - register_op>(Variant::OP_EQUAL, Variant::QUAT, Variant::QUAT); + register_op>(Variant::OP_EQUAL, Variant::QUATERNION, Variant::QUATERNION); register_op>(Variant::OP_EQUAL, Variant::AABB, Variant::AABB); register_op>(Variant::OP_EQUAL, Variant::BASIS, Variant::BASIS); register_op>(Variant::OP_EQUAL, Variant::TRANSFORM3D, Variant::TRANSFORM3D); @@ -1658,7 +1658,7 @@ void Variant::_register_variant_operators() { register_op>(Variant::OP_NOT_EQUAL, Variant::VECTOR3I, Variant::VECTOR3I); register_op>(Variant::OP_NOT_EQUAL, Variant::TRANSFORM2D, Variant::TRANSFORM2D); register_op>(Variant::OP_NOT_EQUAL, Variant::PLANE, Variant::PLANE); - register_op>(Variant::OP_NOT_EQUAL, Variant::QUAT, Variant::QUAT); + register_op>(Variant::OP_NOT_EQUAL, Variant::QUATERNION, Variant::QUATERNION); register_op>(Variant::OP_NOT_EQUAL, Variant::AABB, Variant::AABB); register_op>(Variant::OP_NOT_EQUAL, Variant::BASIS, Variant::BASIS); register_op>(Variant::OP_NOT_EQUAL, Variant::TRANSFORM3D, Variant::TRANSFORM3D); @@ -1849,7 +1849,7 @@ void Variant::_register_variant_operators() { register_op>(Variant::OP_IN, Variant::VECTOR3I, Variant::DICTIONARY); register_op>(Variant::OP_IN, Variant::TRANSFORM2D, Variant::DICTIONARY); register_op>(Variant::OP_IN, Variant::PLANE, Variant::DICTIONARY); - register_op>(Variant::OP_IN, Variant::QUAT, Variant::DICTIONARY); + register_op>(Variant::OP_IN, Variant::QUATERNION, Variant::DICTIONARY); register_op>(Variant::OP_IN, Variant::AABB, Variant::DICTIONARY); register_op>(Variant::OP_IN, Variant::BASIS, Variant::DICTIONARY); register_op>(Variant::OP_IN, Variant::TRANSFORM3D, Variant::DICTIONARY); @@ -1886,7 +1886,7 @@ void Variant::_register_variant_operators() { register_op>(Variant::OP_IN, Variant::VECTOR3I, Variant::ARRAY); register_op>(Variant::OP_IN, Variant::TRANSFORM2D, Variant::ARRAY); register_op>(Variant::OP_IN, Variant::PLANE, Variant::ARRAY); - register_op>(Variant::OP_IN, Variant::QUAT, Variant::ARRAY); + register_op>(Variant::OP_IN, Variant::QUATERNION, Variant::ARRAY); register_op>(Variant::OP_IN, Variant::AABB, Variant::ARRAY); register_op>(Variant::OP_IN, Variant::BASIS, Variant::ARRAY); register_op>(Variant::OP_IN, Variant::TRANSFORM3D, Variant::ARRAY); diff --git a/core/variant/variant_parser.cpp b/core/variant/variant_parser.cpp index 62643e1d31..b58f60c149 100644 --- a/core/variant/variant_parser.cpp +++ b/core/variant/variant_parser.cpp @@ -614,7 +614,7 @@ Error VariantParser::parse_value(Token &token, Variant &value, Stream *p_stream, } value = Plane(args[0], args[1], args[2], args[3]); - } else if (id == "Quat") { + } else if (id == "Quaternion" || id == "Quat") { // "Quat" kept for compatibility Vector args; Error err = _parse_construct(p_stream, args, line, r_err_str); if (err) { @@ -626,7 +626,7 @@ Error VariantParser::parse_value(Token &token, Variant &value, Stream *p_stream, return ERR_PARSE_ERROR; } - value = Quat(args[0], args[1], args[2], args[3]); + value = Quaternion(args[0], args[1], args[2], args[3]); } else if (id == "AABB" || id == "Rect3") { Vector args; Error err = _parse_construct(p_stream, args, line, r_err_str); @@ -1454,9 +1454,9 @@ Error VariantWriter::write(const Variant &p_variant, StoreStringFunc p_store_str p_store_string_func(p_store_string_ud, "AABB( " + rtosfix(aabb.position.x) + ", " + rtosfix(aabb.position.y) + ", " + rtosfix(aabb.position.z) + ", " + rtosfix(aabb.size.x) + ", " + rtosfix(aabb.size.y) + ", " + rtosfix(aabb.size.z) + " )"); } break; - case Variant::QUAT: { - Quat quat = p_variant; - p_store_string_func(p_store_string_ud, "Quat( " + rtosfix(quat.x) + ", " + rtosfix(quat.y) + ", " + rtosfix(quat.z) + ", " + rtosfix(quat.w) + " )"); + case Variant::QUATERNION: { + Quaternion quaternion = p_variant; + p_store_string_func(p_store_string_ud, "Quaternion( " + rtosfix(quaternion.x) + ", " + rtosfix(quaternion.y) + ", " + rtosfix(quaternion.z) + ", " + rtosfix(quaternion.w) + " )"); } break; case Variant::TRANSFORM2D: { diff --git a/core/variant/variant_setget.cpp b/core/variant/variant_setget.cpp index c3f667d9a7..4f4a80e807 100644 --- a/core/variant/variant_setget.cpp +++ b/core/variant/variant_setget.cpp @@ -279,10 +279,10 @@ SETGET_NUMBER_STRUCT_CUSTOM(Plane, double, z, normal.z) SETGET_STRUCT(Plane, Vector3, normal) SETGET_NUMBER_STRUCT(Plane, double, d) -SETGET_NUMBER_STRUCT(Quat, double, x) -SETGET_NUMBER_STRUCT(Quat, double, y) -SETGET_NUMBER_STRUCT(Quat, double, z) -SETGET_NUMBER_STRUCT(Quat, double, w) +SETGET_NUMBER_STRUCT(Quaternion, double, x) +SETGET_NUMBER_STRUCT(Quaternion, double, y) +SETGET_NUMBER_STRUCT(Quaternion, double, z) +SETGET_NUMBER_STRUCT(Quaternion, double, w) SETGET_STRUCT_FUNC_INDEX(Basis, Vector3, x, set_axis, get_axis, 0) SETGET_STRUCT_FUNC_INDEX(Basis, Vector3, y, set_axis, get_axis, 1) @@ -374,10 +374,10 @@ void register_named_setters_getters() { REGISTER_MEMBER(Plane, d); REGISTER_MEMBER(Plane, normal); - REGISTER_MEMBER(Quat, x); - REGISTER_MEMBER(Quat, y); - REGISTER_MEMBER(Quat, z); - REGISTER_MEMBER(Quat, w); + REGISTER_MEMBER(Quaternion, x); + REGISTER_MEMBER(Quaternion, y); + REGISTER_MEMBER(Quaternion, z); + REGISTER_MEMBER(Quaternion, w); REGISTER_MEMBER(Basis, x); REGISTER_MEMBER(Basis, y); @@ -975,7 +975,7 @@ INDEXED_SETGET_STRUCT_BULTIN_NUMERIC(Vector2, double, real_t, 2) INDEXED_SETGET_STRUCT_BULTIN_NUMERIC(Vector2i, int64_t, int32_t, 2) INDEXED_SETGET_STRUCT_BULTIN_NUMERIC(Vector3, double, real_t, 3) INDEXED_SETGET_STRUCT_BULTIN_NUMERIC(Vector3i, int64_t, int32_t, 3) -INDEXED_SETGET_STRUCT_BULTIN_NUMERIC(Quat, double, real_t, 4) +INDEXED_SETGET_STRUCT_BULTIN_NUMERIC(Quaternion, double, real_t, 4) INDEXED_SETGET_STRUCT_BULTIN_NUMERIC(Color, double, float, 4) INDEXED_SETGET_STRUCT_BULTIN_ACCESSOR(Transform2D, Vector2, .elements, 3) @@ -1037,7 +1037,7 @@ void register_indexed_setters_getters() { REGISTER_INDEXED_MEMBER(Vector2i); REGISTER_INDEXED_MEMBER(Vector3); REGISTER_INDEXED_MEMBER(Vector3i); - REGISTER_INDEXED_MEMBER(Quat); + REGISTER_INDEXED_MEMBER(Quaternion); REGISTER_INDEXED_MEMBER(Color); REGISTER_INDEXED_MEMBER(Transform2D); REGISTER_INDEXED_MEMBER(Basis); @@ -2135,10 +2135,10 @@ void Variant::blend(const Variant &a, const Variant &b, float c, Variant &r_dst) r_dst = ::AABB(ra->position + rb->position * c, ra->size + rb->size * c); } return; - case QUAT: { - Quat empty_rot; - const Quat *qa = reinterpret_cast(a._data._mem); - const Quat *qb = reinterpret_cast(b._data._mem); + case QUATERNION: { + Quaternion empty_rot; + const Quaternion *qa = reinterpret_cast(a._data._mem); + const Quaternion *qb = reinterpret_cast(b._data._mem); r_dst = *qa * empty_rot.slerp(*qb, c); } return; @@ -2295,8 +2295,8 @@ void Variant::interpolate(const Variant &a, const Variant &b, float c, Variant & r_dst = a; } return; - case QUAT: { - r_dst = reinterpret_cast(a._data._mem)->slerp(*reinterpret_cast(b._data._mem), c); + case QUATERNION: { + r_dst = reinterpret_cast(a._data._mem)->slerp(*reinterpret_cast(b._data._mem), c); } return; case AABB: { -- cgit v1.2.3