From 3f837d5cc52a4dd18df05efcf348bb99bb33a708 Mon Sep 17 00:00:00 2001 From: Aaron Franke Date: Sat, 9 Feb 2019 00:24:02 -0500 Subject: [Core] Rename Matrix3 file to Basis The code already referred to "Basis", it's just the file name that was different for some reason. --- core/math/basis.cpp | 848 ++++++++++++++++++++++++++++++++++++++++++++++++++ core/math/basis.h | 344 ++++++++++++++++++++ core/math/matrix3.cpp | 848 -------------------------------------------------- core/math/matrix3.h | 344 -------------------- core/math/quat.cpp | 2 +- core/math/transform.h | 2 +- core/math/vector3.cpp | 2 +- core/math/vector3.h | 2 +- core/variant.h | 2 +- 9 files changed, 1197 insertions(+), 1197 deletions(-) create mode 100644 core/math/basis.cpp create mode 100644 core/math/basis.h delete mode 100644 core/math/matrix3.cpp delete mode 100644 core/math/matrix3.h (limited to 'core') diff --git a/core/math/basis.cpp b/core/math/basis.cpp new file mode 100644 index 0000000000..8e4eacd9a6 --- /dev/null +++ b/core/math/basis.cpp @@ -0,0 +1,848 @@ +/*************************************************************************/ +/* basis.cpp */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2019 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 "basis.h" + +#include "core/math/math_funcs.h" +#include "core/os/copymem.h" +#include "core/print_string.h" + +#define cofac(row1, col1, row2, col2) \ + (elements[row1][col1] * elements[row2][col2] - elements[row1][col2] * elements[row2][col1]) + +void Basis::from_z(const Vector3 &p_z) { + + if (Math::abs(p_z.z) > Math_SQRT12) { + + // choose p in y-z plane + real_t a = p_z[1] * p_z[1] + p_z[2] * p_z[2]; + real_t k = 1.0 / Math::sqrt(a); + elements[0] = Vector3(0, -p_z[2] * k, p_z[1] * k); + elements[1] = Vector3(a * k, -p_z[0] * elements[0][2], p_z[0] * elements[0][1]); + } else { + + // choose p in x-y plane + real_t a = p_z.x * p_z.x + p_z.y * p_z.y; + real_t k = 1.0 / Math::sqrt(a); + elements[0] = Vector3(-p_z.y * k, p_z.x * k, 0); + elements[1] = Vector3(-p_z.z * elements[0].y, p_z.z * elements[0].x, a * k); + } + elements[2] = p_z; +} + +void Basis::invert() { + + real_t co[3] = { + cofac(1, 1, 2, 2), cofac(1, 2, 2, 0), cofac(1, 0, 2, 1) + }; + real_t det = elements[0][0] * co[0] + + elements[0][1] * co[1] + + elements[0][2] * co[2]; +#ifdef MATH_CHECKS + ERR_FAIL_COND(det == 0); +#endif + real_t s = 1.0 / det; + + set(co[0] * s, cofac(0, 2, 2, 1) * s, cofac(0, 1, 1, 2) * s, + co[1] * s, cofac(0, 0, 2, 2) * s, cofac(0, 2, 1, 0) * s, + co[2] * s, cofac(0, 1, 2, 0) * s, cofac(0, 0, 1, 1) * s); +} + +void Basis::orthonormalize() { +#ifdef MATH_CHECKS + ERR_FAIL_COND(determinant() == 0); +#endif + // Gram-Schmidt Process + + Vector3 x = get_axis(0); + Vector3 y = get_axis(1); + Vector3 z = get_axis(2); + + x.normalize(); + y = (y - x * (x.dot(y))); + y.normalize(); + z = (z - x * (x.dot(z)) - y * (y.dot(z))); + z.normalize(); + + set_axis(0, x); + set_axis(1, y); + set_axis(2, z); +} + +Basis Basis::orthonormalized() const { + + Basis c = *this; + c.orthonormalize(); + return c; +} + +bool Basis::is_orthogonal() const { + Basis id; + Basis m = (*this) * transposed(); + + return is_equal_approx(id, m); +} + +bool Basis::is_diagonal() const { + return ( + Math::is_equal_approx(elements[0][1], 0) && Math::is_equal_approx(elements[0][2], 0) && + Math::is_equal_approx(elements[1][0], 0) && Math::is_equal_approx(elements[1][2], 0) && + Math::is_equal_approx(elements[2][0], 0) && Math::is_equal_approx(elements[2][1], 0)); +} + +bool Basis::is_rotation() const { + return Math::is_equal_approx(determinant(), 1) && is_orthogonal(); +} + +bool Basis::is_symmetric() const { + + if (!Math::is_equal_approx(elements[0][1], elements[1][0])) + return false; + if (!Math::is_equal_approx(elements[0][2], elements[2][0])) + return false; + if (!Math::is_equal_approx(elements[1][2], elements[2][1])) + return false; + + return true; +} + +Basis Basis::diagonalize() { + +//NOTE: only implemented for symmetric matrices +//with the Jacobi iterative method method +#ifdef MATH_CHECKS + ERR_FAIL_COND_V(!is_symmetric(), Basis()); +#endif + const int ite_max = 1024; + + real_t off_matrix_norm_2 = elements[0][1] * elements[0][1] + elements[0][2] * elements[0][2] + elements[1][2] * elements[1][2]; + + int ite = 0; + Basis acc_rot; + while (off_matrix_norm_2 > CMP_EPSILON2 && ite++ < ite_max) { + real_t el01_2 = elements[0][1] * elements[0][1]; + real_t el02_2 = elements[0][2] * elements[0][2]; + real_t el12_2 = elements[1][2] * elements[1][2]; + // Find the pivot element + int i, j; + if (el01_2 > el02_2) { + if (el12_2 > el01_2) { + i = 1; + j = 2; + } else { + i = 0; + j = 1; + } + } else { + if (el12_2 > el02_2) { + i = 1; + j = 2; + } else { + i = 0; + j = 2; + } + } + + // Compute the rotation angle + real_t angle; + if (Math::is_equal_approx(elements[j][j], elements[i][i])) { + angle = Math_PI / 4; + } else { + angle = 0.5 * Math::atan(2 * elements[i][j] / (elements[j][j] - elements[i][i])); + } + + // Compute the rotation matrix + Basis rot; + rot.elements[i][i] = rot.elements[j][j] = Math::cos(angle); + rot.elements[i][j] = -(rot.elements[j][i] = Math::sin(angle)); + + // Update the off matrix norm + off_matrix_norm_2 -= elements[i][j] * elements[i][j]; + + // Apply the rotation + *this = rot * *this * rot.transposed(); + acc_rot = rot * acc_rot; + } + + return acc_rot; +} + +Basis Basis::inverse() const { + + Basis inv = *this; + inv.invert(); + return inv; +} + +void Basis::transpose() { + + SWAP(elements[0][1], elements[1][0]); + SWAP(elements[0][2], elements[2][0]); + SWAP(elements[1][2], elements[2][1]); +} + +Basis Basis::transposed() const { + + Basis tr = *this; + tr.transpose(); + return tr; +} + +// Multiplies the matrix from left by the scaling matrix: M -> S.M +// See the comment for Basis::rotated for further explanation. +void Basis::scale(const Vector3 &p_scale) { + + elements[0][0] *= p_scale.x; + elements[0][1] *= p_scale.x; + elements[0][2] *= p_scale.x; + elements[1][0] *= p_scale.y; + elements[1][1] *= p_scale.y; + elements[1][2] *= p_scale.y; + elements[2][0] *= p_scale.z; + elements[2][1] *= p_scale.z; + elements[2][2] *= p_scale.z; +} + +Basis Basis::scaled(const Vector3 &p_scale) const { + Basis m = *this; + m.scale(p_scale); + return m; +} + +void Basis::scale_local(const Vector3 &p_scale) { + // performs a scaling in object-local coordinate system: + // M -> (M.S.Minv).M = M.S. + *this = scaled_local(p_scale); +} + +Basis Basis::scaled_local(const Vector3 &p_scale) const { + Basis b; + b.set_diagonal(p_scale); + + return (*this) * b; +} + +Vector3 Basis::get_scale_abs() const { + + return Vector3( + Vector3(elements[0][0], elements[1][0], elements[2][0]).length(), + Vector3(elements[0][1], elements[1][1], elements[2][1]).length(), + Vector3(elements[0][2], elements[1][2], elements[2][2]).length()); +} + +Vector3 Basis::get_scale_local() const { + real_t det_sign = determinant() > 0 ? 1 : -1; + return det_sign * Vector3(elements[0].length(), elements[1].length(), elements[2].length()); +} + +// get_scale works with get_rotation, use get_scale_abs if you need to enforce positive signature. +Vector3 Basis::get_scale() const { + // FIXME: We are assuming M = R.S (R is rotation and S is scaling), and use polar decomposition to extract R and S. + // A polar decomposition is M = O.P, where O is an orthogonal matrix (meaning rotation and reflection) and + // P is a positive semi-definite matrix (meaning it contains absolute values of scaling along its diagonal). + // + // Despite being different from what we want to achieve, we can nevertheless make use of polar decomposition + // here as follows. We can split O into a rotation and a reflection as O = R.Q, and obtain M = R.S where + // we defined S = Q.P. Now, R is a proper rotation matrix and S is a (signed) scaling matrix, + // which can involve negative scalings. However, there is a catch: unlike the polar decomposition of M = O.P, + // the decomposition of O into a rotation and reflection matrix as O = R.Q is not unique. + // Therefore, we are going to do this decomposition by sticking to a particular convention. + // This may lead to confusion for some users though. + // + // The convention we use here is to absorb the sign flip into the scaling matrix. + // The same convention is also used in other similar functions such as get_rotation_axis_angle, get_rotation, ... + // + // A proper way to get rid of this issue would be to store the scaling values (or at least their signs) + // as a part of Basis. However, if we go that path, we need to disable direct (write) access to the + // matrix elements. + // + // The rotation part of this decomposition is returned by get_rotation* functions. + real_t det_sign = determinant() > 0 ? 1 : -1; + return det_sign * Vector3( + Vector3(elements[0][0], elements[1][0], elements[2][0]).length(), + Vector3(elements[0][1], elements[1][1], elements[2][1]).length(), + Vector3(elements[0][2], elements[1][2], elements[2][2]).length()); +} + +// Decomposes a Basis into a rotation-reflection matrix (an element of the group O(3)) and a positive scaling matrix as B = O.S. +// Returns the rotation-reflection matrix via reference argument, and scaling information is returned as a Vector3. +// This (internal) function is too specific and named too ugly to expose to users, and probably there's no need to do so. +Vector3 Basis::rotref_posscale_decomposition(Basis &rotref) const { +#ifdef MATH_CHECKS + ERR_FAIL_COND_V(determinant() == 0, Vector3()); + + Basis m = transposed() * (*this); + ERR_FAIL_COND_V(!m.is_diagonal(), Vector3()); +#endif + Vector3 scale = get_scale(); + Basis inv_scale = Basis().scaled(scale.inverse()); // this will also absorb the sign of scale + rotref = (*this) * inv_scale; + +#ifdef MATH_CHECKS + ERR_FAIL_COND_V(!rotref.is_orthogonal(), Vector3()); +#endif + return scale.abs(); +} + +// Multiplies the matrix from left by the rotation matrix: M -> R.M +// Note that this does *not* rotate the matrix itself. +// +// The main use of Basis is as Transform.basis, which is used a the transformation matrix +// of 3D object. Rotate here refers to rotation of the object (which is R * (*this)), +// not the matrix itself (which is R * (*this) * R.transposed()). +Basis Basis::rotated(const Vector3 &p_axis, real_t p_phi) const { + return Basis(p_axis, p_phi) * (*this); +} + +void Basis::rotate(const Vector3 &p_axis, real_t p_phi) { + *this = rotated(p_axis, p_phi); +} + +void Basis::rotate_local(const Vector3 &p_axis, real_t p_phi) { + // performs a rotation in object-local coordinate system: + // M -> (M.R.Minv).M = M.R. + *this = rotated_local(p_axis, p_phi); +} +Basis Basis::rotated_local(const Vector3 &p_axis, real_t p_phi) const { + + return (*this) * Basis(p_axis, p_phi); +} + +Basis Basis::rotated(const Vector3 &p_euler) const { + return Basis(p_euler) * (*this); +} + +void Basis::rotate(const Vector3 &p_euler) { + *this = rotated(p_euler); +} + +Basis Basis::rotated(const Quat &p_quat) const { + return Basis(p_quat) * (*this); +} + +void Basis::rotate(const Quat &p_quat) { + *this = rotated(p_quat); +} + +Vector3 Basis::get_rotation_euler() 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. + Basis m = orthonormalized(); + real_t det = m.determinant(); + if (det < 0) { + // Ensure that the determinant is 1, such that result is a proper rotation matrix which can be represented by Euler angles. + m.scale(Vector3(-1, -1, -1)); + } + + return m.get_euler(); +} + +Quat Basis::get_rotation_quat() 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. + Basis m = orthonormalized(); + real_t det = m.determinant(); + if (det < 0) { + // Ensure that the determinant is 1, such that result is a proper rotation matrix which can be represented by Euler angles. + m.scale(Vector3(-1, -1, -1)); + } + + return m.get_quat(); +} + +void Basis::get_rotation_axis_angle(Vector3 &p_axis, real_t &p_angle) 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. + Basis m = orthonormalized(); + real_t det = m.determinant(); + if (det < 0) { + // Ensure that the determinant is 1, such that result is a proper rotation matrix which can be represented by Euler angles. + m.scale(Vector3(-1, -1, -1)); + } + + m.get_axis_angle(p_axis, p_angle); +} + +void Basis::get_rotation_axis_angle_local(Vector3 &p_axis, real_t &p_angle) 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. + Basis m = transposed(); + m.orthonormalize(); + real_t det = m.determinant(); + if (det < 0) { + // Ensure that the determinant is 1, such that result is a proper rotation matrix which can be represented by Euler angles. + m.scale(Vector3(-1, -1, -1)); + } + + m.get_axis_angle(p_axis, p_angle); + p_angle = -p_angle; +} + +// get_euler_xyz returns a vector containing the Euler angles in the format +// (a1,a2,a3), where a3 is the angle of the first rotation, and a1 is the last +// (following the convention they are commonly defined in the literature). +// +// The current implementation uses XYZ convention (Z is the first rotation), +// so euler.z is the angle of the (first) rotation around Z axis and so on, +// +// And thus, assuming the matrix is a rotation matrix, this function returns +// the angles in the decomposition R = X(a1).Y(a2).Z(a3) where Z(a) rotates +// around the z-axis by a and so on. +Vector3 Basis::get_euler_xyz() const { + + // Euler angles in XYZ convention. + // See https://en.wikipedia.org/wiki/Euler_angles#Rotation_matrix + // + // rot = cy*cz -cy*sz sy + // cz*sx*sy+cx*sz cx*cz-sx*sy*sz -cy*sx + // -cx*cz*sy+sx*sz cz*sx+cx*sy*sz cx*cy + + Vector3 euler; +#ifdef MATH_CHECKS + ERR_FAIL_COND_V(!is_rotation(), euler); +#endif + real_t sy = elements[0][2]; + if (sy < 1.0) { + if (sy > -1.0) { + // is this a pure Y rotation? + if (elements[1][0] == 0.0 && elements[0][1] == 0.0 && elements[1][2] == 0 && elements[2][1] == 0 && elements[1][1] == 1) { + // return the simplest form (human friendlier in editor and scripts) + euler.x = 0; + euler.y = atan2(elements[0][2], elements[0][0]); + euler.z = 0; + } else { + euler.x = Math::atan2(-elements[1][2], elements[2][2]); + euler.y = Math::asin(sy); + euler.z = Math::atan2(-elements[0][1], elements[0][0]); + } + } else { + euler.x = -Math::atan2(elements[0][1], elements[1][1]); + euler.y = -Math_PI / 2.0; + euler.z = 0.0; + } + } else { + euler.x = Math::atan2(elements[0][1], elements[1][1]); + euler.y = Math_PI / 2.0; + euler.z = 0.0; + } + return euler; +} + +// set_euler_xyz 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. +// The current implementation uses XYZ convention (Z is the first rotation). +void Basis::set_euler_xyz(const Vector3 &p_euler) { + + real_t c, s; + + c = Math::cos(p_euler.x); + s = Math::sin(p_euler.x); + Basis xmat(1.0, 0.0, 0.0, 0.0, c, -s, 0.0, s, c); + + c = Math::cos(p_euler.y); + s = Math::sin(p_euler.y); + Basis ymat(c, 0.0, s, 0.0, 1.0, 0.0, -s, 0.0, c); + + c = Math::cos(p_euler.z); + s = Math::sin(p_euler.z); + Basis zmat(c, -s, 0.0, s, c, 0.0, 0.0, 0.0, 1.0); + + //optimizer will optimize away all this anyway + *this = xmat * (ymat * zmat); +} + +// get_euler_yxz returns a vector containing the Euler angles in the YXZ convention, +// as in first-Z, then-X, last-Y. The angles for X, Y, and Z rotations are returned +// as the x, y, and z components of a Vector3 respectively. +Vector3 Basis::get_euler_yxz() const { + + // Euler angles in YXZ convention. + // See https://en.wikipedia.org/wiki/Euler_angles#Rotation_matrix + // + // rot = cy*cz+sy*sx*sz cz*sy*sx-cy*sz cx*sy + // cx*sz cx*cz -sx + // cy*sx*sz-cz*sy cy*cz*sx+sy*sz cy*cx + + Vector3 euler; +#ifdef MATH_CHECKS + ERR_FAIL_COND_V(!is_rotation(), euler); +#endif + real_t m12 = elements[1][2]; + + if (m12 < 1) { + if (m12 > -1) { + // is this a pure X rotation? + if (elements[1][0] == 0 && elements[0][1] == 0 && elements[0][2] == 0 && elements[2][0] == 0 && elements[0][0] == 1) { + // return the simplest form (human friendlier in editor and scripts) + euler.x = atan2(-m12, elements[1][1]); + euler.y = 0; + euler.z = 0; + } else { + euler.x = asin(-m12); + euler.y = atan2(elements[0][2], elements[2][2]); + euler.z = atan2(elements[1][0], elements[1][1]); + } + } else { // m12 == -1 + euler.x = Math_PI * 0.5; + euler.y = -atan2(-elements[0][1], elements[0][0]); + euler.z = 0; + } + } else { // m12 == 1 + euler.x = -Math_PI * 0.5; + euler.y = -atan2(-elements[0][1], elements[0][0]); + euler.z = 0; + } + + return euler; +} + +// set_euler_yxz 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. +// The current implementation uses YXZ convention (Z is the first rotation). +void Basis::set_euler_yxz(const Vector3 &p_euler) { + + real_t c, s; + + c = Math::cos(p_euler.x); + s = Math::sin(p_euler.x); + Basis xmat(1.0, 0.0, 0.0, 0.0, c, -s, 0.0, s, c); + + c = Math::cos(p_euler.y); + s = Math::sin(p_euler.y); + Basis ymat(c, 0.0, s, 0.0, 1.0, 0.0, -s, 0.0, c); + + c = Math::cos(p_euler.z); + s = Math::sin(p_euler.z); + Basis zmat(c, -s, 0.0, s, c, 0.0, 0.0, 0.0, 1.0); + + //optimizer will optimize away all this anyway + *this = ymat * xmat * zmat; +} + +bool Basis::is_equal_approx(const Basis &a, const Basis &b) const { + + for (int i = 0; i < 3; i++) { + for (int j = 0; j < 3; j++) { + if (!Math::is_equal_approx(a.elements[i][j], b.elements[i][j])) + return false; + } + } + + return true; +} + +bool Basis::operator==(const Basis &p_matrix) const { + + for (int i = 0; i < 3; i++) { + for (int j = 0; j < 3; j++) { + if (elements[i][j] != p_matrix.elements[i][j]) + return false; + } + } + + return true; +} + +bool Basis::operator!=(const Basis &p_matrix) const { + + return (!(*this == p_matrix)); +} + +Basis::operator String() const { + + String mtx; + for (int i = 0; i < 3; i++) { + + for (int j = 0; j < 3; j++) { + + if (i != 0 || j != 0) + mtx += ", "; + + mtx += rtos(elements[i][j]); + } + } + + return mtx; +} + +Quat Basis::get_quat() const { +#ifdef MATH_CHECKS + ERR_FAIL_COND_V(!is_rotation(), Quat()); +#endif + real_t trace = elements[0][0] + elements[1][1] + elements[2][2]; + real_t temp[4]; + + if (trace > 0.0) { + real_t s = Math::sqrt(trace + 1.0); + temp[3] = (s * 0.5); + s = 0.5 / s; + + temp[0] = ((elements[2][1] - elements[1][2]) * s); + temp[1] = ((elements[0][2] - elements[2][0]) * s); + temp[2] = ((elements[1][0] - elements[0][1]) * s); + } else { + int i = elements[0][0] < elements[1][1] ? + (elements[1][1] < elements[2][2] ? 2 : 1) : + (elements[0][0] < elements[2][2] ? 2 : 0); + int j = (i + 1) % 3; + int k = (i + 2) % 3; + + real_t s = Math::sqrt(elements[i][i] - elements[j][j] - elements[k][k] + 1.0); + temp[i] = s * 0.5; + s = 0.5 / s; + + temp[3] = (elements[k][j] - elements[j][k]) * s; + temp[j] = (elements[j][i] + elements[i][j]) * s; + temp[k] = (elements[k][i] + elements[i][k]) * s; + } + + return Quat(temp[0], temp[1], temp[2], temp[3]); +} + +static const Basis _ortho_bases[24] = { + Basis(1, 0, 0, 0, 1, 0, 0, 0, 1), + Basis(0, -1, 0, 1, 0, 0, 0, 0, 1), + Basis(-1, 0, 0, 0, -1, 0, 0, 0, 1), + Basis(0, 1, 0, -1, 0, 0, 0, 0, 1), + Basis(1, 0, 0, 0, 0, -1, 0, 1, 0), + Basis(0, 0, 1, 1, 0, 0, 0, 1, 0), + Basis(-1, 0, 0, 0, 0, 1, 0, 1, 0), + Basis(0, 0, -1, -1, 0, 0, 0, 1, 0), + Basis(1, 0, 0, 0, -1, 0, 0, 0, -1), + Basis(0, 1, 0, 1, 0, 0, 0, 0, -1), + Basis(-1, 0, 0, 0, 1, 0, 0, 0, -1), + Basis(0, -1, 0, -1, 0, 0, 0, 0, -1), + Basis(1, 0, 0, 0, 0, 1, 0, -1, 0), + Basis(0, 0, -1, 1, 0, 0, 0, -1, 0), + Basis(-1, 0, 0, 0, 0, -1, 0, -1, 0), + Basis(0, 0, 1, -1, 0, 0, 0, -1, 0), + Basis(0, 0, 1, 0, 1, 0, -1, 0, 0), + Basis(0, -1, 0, 0, 0, 1, -1, 0, 0), + Basis(0, 0, -1, 0, -1, 0, -1, 0, 0), + Basis(0, 1, 0, 0, 0, -1, -1, 0, 0), + Basis(0, 0, 1, 0, -1, 0, 1, 0, 0), + Basis(0, 1, 0, 0, 0, 1, 1, 0, 0), + Basis(0, 0, -1, 0, 1, 0, 1, 0, 0), + Basis(0, -1, 0, 0, 0, -1, 1, 0, 0) +}; + +int Basis::get_orthogonal_index() const { + + //could be sped up if i come up with a way + Basis orth = *this; + for (int i = 0; i < 3; i++) { + for (int j = 0; j < 3; j++) { + + real_t v = orth[i][j]; + if (v > 0.5) + v = 1.0; + else if (v < -0.5) + v = -1.0; + else + v = 0; + + orth[i][j] = v; + } + } + + for (int i = 0; i < 24; i++) { + + if (_ortho_bases[i] == orth) + return i; + } + + return 0; +} + +void Basis::set_orthogonal_index(int p_index) { + + //there only exist 24 orthogonal bases in r3 + ERR_FAIL_INDEX(p_index, 24); + + *this = _ortho_bases[p_index]; +} + +void Basis::get_axis_angle(Vector3 &r_axis, real_t &r_angle) const { +#ifdef MATH_CHECKS + ERR_FAIL_COND(!is_rotation()); +#endif + real_t angle, x, y, z; // variables for result + real_t epsilon = 0.01; // margin to allow for rounding errors + real_t epsilon2 = 0.1; // margin to distinguish between 0 and 180 degrees + + if ((Math::abs(elements[1][0] - elements[0][1]) < epsilon) && (Math::abs(elements[2][0] - elements[0][2]) < epsilon) && (Math::abs(elements[2][1] - elements[1][2]) < epsilon)) { + // singularity found + // first check for identity matrix which must have +1 for all terms + // in leading diagonaland zero in other terms + if ((Math::abs(elements[1][0] + elements[0][1]) < epsilon2) && (Math::abs(elements[2][0] + elements[0][2]) < epsilon2) && (Math::abs(elements[2][1] + elements[1][2]) < epsilon2) && (Math::abs(elements[0][0] + elements[1][1] + elements[2][2] - 3) < epsilon2)) { + // this singularity is identity matrix so angle = 0 + r_axis = Vector3(0, 1, 0); + r_angle = 0; + return; + } + // otherwise this singularity is angle = 180 + angle = Math_PI; + real_t xx = (elements[0][0] + 1) / 2; + real_t yy = (elements[1][1] + 1) / 2; + real_t zz = (elements[2][2] + 1) / 2; + real_t xy = (elements[1][0] + elements[0][1]) / 4; + real_t xz = (elements[2][0] + elements[0][2]) / 4; + real_t yz = (elements[2][1] + elements[1][2]) / 4; + if ((xx > yy) && (xx > zz)) { // elements[0][0] is the largest diagonal term + if (xx < epsilon) { + x = 0; + y = 0.7071; + z = 0.7071; + } else { + x = Math::sqrt(xx); + y = xy / x; + z = xz / x; + } + } else if (yy > zz) { // elements[1][1] is the largest diagonal term + if (yy < epsilon) { + x = 0.7071; + y = 0; + z = 0.7071; + } else { + y = Math::sqrt(yy); + x = xy / y; + z = yz / y; + } + } else { // elements[2][2] is the largest diagonal term so base result on this + if (zz < epsilon) { + x = 0.7071; + y = 0.7071; + z = 0; + } else { + z = Math::sqrt(zz); + x = xz / z; + y = yz / z; + } + } + r_axis = Vector3(x, y, z); + r_angle = angle; + return; + } + // as we have reached here there are no singularities so we can handle normally + real_t s = Math::sqrt((elements[1][2] - elements[2][1]) * (elements[1][2] - elements[2][1]) + (elements[2][0] - elements[0][2]) * (elements[2][0] - elements[0][2]) + (elements[0][1] - elements[1][0]) * (elements[0][1] - elements[1][0])); // s=|axis||sin(angle)|, used to normalise + + angle = Math::acos((elements[0][0] + elements[1][1] + elements[2][2] - 1) / 2); + if (angle < 0) s = -s; + x = (elements[2][1] - elements[1][2]) / s; + y = (elements[0][2] - elements[2][0]) / s; + z = (elements[1][0] - elements[0][1]) / s; + + r_axis = Vector3(x, y, z); + r_angle = angle; +} + +void Basis::set_quat(const Quat &p_quat) { + + real_t d = p_quat.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; + 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)); +} + +void Basis::set_axis_angle(const Vector3 &p_axis, real_t p_phi) { +// Rotation matrix from axis and angle, see https://en.wikipedia.org/wiki/Rotation_matrix#Rotation_matrix_from_axis_angle +#ifdef MATH_CHECKS + ERR_FAIL_COND(!p_axis.is_normalized()); +#endif + Vector3 axis_sq(p_axis.x * p_axis.x, p_axis.y * p_axis.y, p_axis.z * p_axis.z); + + real_t cosine = Math::cos(p_phi); + real_t sine = Math::sin(p_phi); + + elements[0][0] = axis_sq.x + cosine * (1.0 - axis_sq.x); + elements[0][1] = p_axis.x * p_axis.y * (1.0 - cosine) - p_axis.z * sine; + elements[0][2] = p_axis.z * p_axis.x * (1.0 - cosine) + p_axis.y * sine; + + elements[1][0] = p_axis.x * p_axis.y * (1.0 - cosine) + p_axis.z * sine; + elements[1][1] = axis_sq.y + cosine * (1.0 - axis_sq.y); + elements[1][2] = p_axis.y * p_axis.z * (1.0 - cosine) - p_axis.x * sine; + + elements[2][0] = p_axis.z * p_axis.x * (1.0 - cosine) - p_axis.y * sine; + elements[2][1] = p_axis.y * p_axis.z * (1.0 - cosine) + p_axis.x * sine; + elements[2][2] = axis_sq.z + cosine * (1.0 - axis_sq.z); +} + +void Basis::set_axis_angle_scale(const Vector3 &p_axis, real_t p_phi, const Vector3 &p_scale) { + set_diagonal(p_scale); + rotate(p_axis, p_phi); +} + +void Basis::set_euler_scale(const Vector3 &p_euler, const Vector3 &p_scale) { + set_diagonal(p_scale); + rotate(p_euler); +} + +void Basis::set_quat_scale(const Quat &p_quat, const Vector3 &p_scale) { + set_diagonal(p_scale); + rotate(p_quat); +} + +void Basis::set_diagonal(const Vector3 p_diag) { + elements[0][0] = p_diag.x; + elements[0][1] = 0; + elements[0][2] = 0; + + elements[1][0] = 0; + elements[1][1] = p_diag.y; + elements[1][2] = 0; + + elements[2][0] = 0; + elements[2][1] = 0; + elements[2][2] = p_diag.z; +} + +Basis Basis::slerp(const Basis &target, const real_t &t) const { +// TODO: implement this directly without using quaternions to make it more efficient +#ifdef MATH_CHECKS + ERR_FAIL_COND_V(!is_rotation(), Basis()); + ERR_FAIL_COND_V(!target.is_rotation(), Basis()); +#endif + + Quat from(*this); + Quat to(target); + + return Basis(from.slerp(to, t)); +} diff --git a/core/math/basis.h b/core/math/basis.h new file mode 100644 index 0000000000..128e56b494 --- /dev/null +++ b/core/math/basis.h @@ -0,0 +1,344 @@ +/*************************************************************************/ +/* basis.h */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2019 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. */ +/*************************************************************************/ + +// Circular dependency between Vector3 and Basis :/ +#include "core/math/vector3.h" + +#ifndef BASIS_H +#define BASIS_H + +#include "core/math/quat.h" + +/** + @author Juan Linietsky +*/ + +class Basis { +public: + Vector3 elements[3]; + + _FORCE_INLINE_ const Vector3 &operator[](int axis) const { + + return elements[axis]; + } + _FORCE_INLINE_ Vector3 &operator[](int axis) { + + return elements[axis]; + } + + void invert(); + void transpose(); + + Basis inverse() const; + Basis transposed() const; + + _FORCE_INLINE_ real_t determinant() const; + + void from_z(const Vector3 &p_z); + + _FORCE_INLINE_ Vector3 get_axis(int p_axis) const { + // get actual basis axis (elements is transposed for performance) + return Vector3(elements[0][p_axis], elements[1][p_axis], elements[2][p_axis]); + } + _FORCE_INLINE_ void set_axis(int p_axis, const Vector3 &p_value) { + // get actual basis axis (elements is transposed for performance) + elements[0][p_axis] = p_value.x; + elements[1][p_axis] = p_value.y; + elements[2][p_axis] = p_value.z; + } + + void rotate(const Vector3 &p_axis, real_t p_phi); + Basis rotated(const Vector3 &p_axis, real_t p_phi) const; + + void rotate_local(const Vector3 &p_axis, real_t p_phi); + Basis rotated_local(const Vector3 &p_axis, real_t p_phi) const; + + 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; + + 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; + Vector3 get_rotation() const { return get_rotation_euler(); }; + + Vector3 rotref_posscale_decomposition(Basis &rotref) const; + + Vector3 get_euler_xyz() const; + void set_euler_xyz(const Vector3 &p_euler); + Vector3 get_euler_yxz() const; + void set_euler_yxz(const Vector3 &p_euler); + + Quat get_quat() const; + void set_quat(const Quat &p_quat); + + Vector3 get_euler() const { return get_euler_yxz(); } + void set_euler(const Vector3 &p_euler) { set_euler_yxz(p_euler); } + + void get_axis_angle(Vector3 &r_axis, real_t &r_angle) const; + void set_axis_angle(const Vector3 &p_axis, real_t p_phi); + + void scale(const Vector3 &p_scale); + Basis scaled(const Vector3 &p_scale) const; + + void scale_local(const Vector3 &p_scale); + Basis scaled_local(const Vector3 &p_scale) const; + + Vector3 get_scale() const; + Vector3 get_scale_abs() const; + Vector3 get_scale_local() const; + + 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); + + // transposed dot products + _FORCE_INLINE_ real_t tdotx(const Vector3 &v) const { + return elements[0][0] * v[0] + elements[1][0] * v[1] + elements[2][0] * v[2]; + } + _FORCE_INLINE_ real_t tdoty(const Vector3 &v) const { + return elements[0][1] * v[0] + elements[1][1] * v[1] + elements[2][1] * v[2]; + } + _FORCE_INLINE_ real_t tdotz(const Vector3 &v) const { + return elements[0][2] * v[0] + elements[1][2] * v[1] + elements[2][2] * v[2]; + } + + bool is_equal_approx(const Basis &a, const Basis &b) const; + + bool operator==(const Basis &p_matrix) const; + bool operator!=(const Basis &p_matrix) const; + + _FORCE_INLINE_ Vector3 xform(const Vector3 &p_vector) const; + _FORCE_INLINE_ Vector3 xform_inv(const Vector3 &p_vector) const; + _FORCE_INLINE_ void operator*=(const Basis &p_matrix); + _FORCE_INLINE_ Basis operator*(const Basis &p_matrix) const; + _FORCE_INLINE_ void operator+=(const Basis &p_matrix); + _FORCE_INLINE_ Basis operator+(const Basis &p_matrix) const; + _FORCE_INLINE_ void operator-=(const Basis &p_matrix); + _FORCE_INLINE_ Basis operator-(const Basis &p_matrix) const; + _FORCE_INLINE_ void operator*=(real_t p_val); + _FORCE_INLINE_ Basis operator*(real_t p_val) const; + + int get_orthogonal_index() const; + void set_orthogonal_index(int p_index); + + void set_diagonal(const Vector3 p_diag); + + bool is_orthogonal() const; + bool is_diagonal() const; + bool is_rotation() const; + + Basis slerp(const Basis &target, const real_t &t) const; + + operator String() const; + + /* create / set */ + + _FORCE_INLINE_ void set(real_t xx, real_t xy, real_t xz, real_t yx, real_t yy, real_t yz, real_t zx, real_t zy, real_t zz) { + + elements[0][0] = xx; + elements[0][1] = xy; + elements[0][2] = xz; + elements[1][0] = yx; + elements[1][1] = yy; + elements[1][2] = yz; + elements[2][0] = zx; + elements[2][1] = zy; + elements[2][2] = zz; + } + _FORCE_INLINE_ void set(const Vector3 &p_x, const Vector3 &p_y, const Vector3 &p_z) { + + set_axis(0, p_x); + set_axis(1, p_y); + set_axis(2, p_z); + } + _FORCE_INLINE_ Vector3 get_column(int i) const { + + return Vector3(elements[0][i], elements[1][i], elements[2][i]); + } + + _FORCE_INLINE_ Vector3 get_row(int i) const { + + return Vector3(elements[i][0], elements[i][1], elements[i][2]); + } + _FORCE_INLINE_ Vector3 get_main_diagonal() const { + return Vector3(elements[0][0], elements[1][1], elements[2][2]); + } + + _FORCE_INLINE_ void set_row(int i, const Vector3 &p_row) { + elements[i][0] = p_row.x; + elements[i][1] = p_row.y; + elements[i][2] = p_row.z; + } + + _FORCE_INLINE_ void set_zero() { + elements[0].zero(); + elements[1].zero(); + elements[2].zero(); + } + + _FORCE_INLINE_ Basis transpose_xform(const Basis &m) const { + return Basis( + elements[0].x * m[0].x + elements[1].x * m[1].x + elements[2].x * m[2].x, + elements[0].x * m[0].y + elements[1].x * m[1].y + elements[2].x * m[2].y, + elements[0].x * m[0].z + elements[1].x * m[1].z + elements[2].x * m[2].z, + elements[0].y * m[0].x + elements[1].y * m[1].x + elements[2].y * m[2].x, + elements[0].y * m[0].y + elements[1].y * m[1].y + elements[2].y * m[2].y, + elements[0].y * m[0].z + elements[1].y * m[1].z + elements[2].y * m[2].z, + elements[0].z * m[0].x + elements[1].z * m[1].x + elements[2].z * m[2].x, + elements[0].z * m[0].y + elements[1].z * m[1].y + elements[2].z * m[2].y, + elements[0].z * m[0].z + elements[1].z * m[1].z + elements[2].z * m[2].z); + } + Basis(real_t xx, real_t xy, real_t xz, real_t yx, real_t yy, real_t yz, real_t zx, real_t zy, real_t zz) { + + set(xx, xy, xz, yx, yy, yz, zx, zy, zz); + } + + void orthonormalize(); + Basis orthonormalized() const; + + bool is_symmetric() const; + Basis diagonalize(); + + operator Quat() const { return get_quat(); } + + 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 Vector3 &p_euler) { set_euler(p_euler); } + Basis(const Vector3 &p_euler, const Vector3 &p_scale) { set_euler_scale(p_euler, p_scale); } + + Basis(const Vector3 &p_axis, real_t p_phi) { set_axis_angle(p_axis, p_phi); } + Basis(const Vector3 &p_axis, real_t p_phi, const Vector3 &p_scale) { set_axis_angle_scale(p_axis, p_phi, p_scale); } + + _FORCE_INLINE_ Basis(const Vector3 &row0, const Vector3 &row1, const Vector3 &row2) { + elements[0] = row0; + elements[1] = row1; + elements[2] = row2; + } + + _FORCE_INLINE_ Basis() { + + elements[0][0] = 1; + elements[0][1] = 0; + elements[0][2] = 0; + elements[1][0] = 0; + elements[1][1] = 1; + elements[1][2] = 0; + elements[2][0] = 0; + elements[2][1] = 0; + elements[2][2] = 1; + } +}; + +_FORCE_INLINE_ void Basis::operator*=(const Basis &p_matrix) { + + set( + p_matrix.tdotx(elements[0]), p_matrix.tdoty(elements[0]), p_matrix.tdotz(elements[0]), + p_matrix.tdotx(elements[1]), p_matrix.tdoty(elements[1]), p_matrix.tdotz(elements[1]), + p_matrix.tdotx(elements[2]), p_matrix.tdoty(elements[2]), p_matrix.tdotz(elements[2])); +} + +_FORCE_INLINE_ Basis Basis::operator*(const Basis &p_matrix) const { + + return Basis( + p_matrix.tdotx(elements[0]), p_matrix.tdoty(elements[0]), p_matrix.tdotz(elements[0]), + p_matrix.tdotx(elements[1]), p_matrix.tdoty(elements[1]), p_matrix.tdotz(elements[1]), + p_matrix.tdotx(elements[2]), p_matrix.tdoty(elements[2]), p_matrix.tdotz(elements[2])); +} + +_FORCE_INLINE_ void Basis::operator+=(const Basis &p_matrix) { + + elements[0] += p_matrix.elements[0]; + elements[1] += p_matrix.elements[1]; + elements[2] += p_matrix.elements[2]; +} + +_FORCE_INLINE_ Basis Basis::operator+(const Basis &p_matrix) const { + + Basis ret(*this); + ret += p_matrix; + return ret; +} + +_FORCE_INLINE_ void Basis::operator-=(const Basis &p_matrix) { + + elements[0] -= p_matrix.elements[0]; + elements[1] -= p_matrix.elements[1]; + elements[2] -= p_matrix.elements[2]; +} + +_FORCE_INLINE_ Basis Basis::operator-(const Basis &p_matrix) const { + + Basis ret(*this); + ret -= p_matrix; + return ret; +} + +_FORCE_INLINE_ void Basis::operator*=(real_t p_val) { + + elements[0] *= p_val; + elements[1] *= p_val; + elements[2] *= p_val; +} + +_FORCE_INLINE_ Basis Basis::operator*(real_t p_val) const { + + Basis ret(*this); + ret *= p_val; + return ret; +} + +Vector3 Basis::xform(const Vector3 &p_vector) const { + + return Vector3( + elements[0].dot(p_vector), + elements[1].dot(p_vector), + elements[2].dot(p_vector)); +} + +Vector3 Basis::xform_inv(const Vector3 &p_vector) const { + + return Vector3( + (elements[0][0] * p_vector.x) + (elements[1][0] * p_vector.y) + (elements[2][0] * p_vector.z), + (elements[0][1] * p_vector.x) + (elements[1][1] * p_vector.y) + (elements[2][1] * p_vector.z), + (elements[0][2] * p_vector.x) + (elements[1][2] * p_vector.y) + (elements[2][2] * p_vector.z)); +} + +real_t Basis::determinant() const { + + return elements[0][0] * (elements[1][1] * elements[2][2] - elements[2][1] * elements[1][2]) - + elements[1][0] * (elements[0][1] * elements[2][2] - elements[2][1] * elements[0][2]) + + elements[2][0] * (elements[0][1] * elements[1][2] - elements[1][1] * elements[0][2]); +} +#endif // BASIS_H diff --git a/core/math/matrix3.cpp b/core/math/matrix3.cpp deleted file mode 100644 index 0aa67078fb..0000000000 --- a/core/math/matrix3.cpp +++ /dev/null @@ -1,848 +0,0 @@ -/*************************************************************************/ -/* matrix3.cpp */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2019 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 "matrix3.h" - -#include "core/math/math_funcs.h" -#include "core/os/copymem.h" -#include "core/print_string.h" - -#define cofac(row1, col1, row2, col2) \ - (elements[row1][col1] * elements[row2][col2] - elements[row1][col2] * elements[row2][col1]) - -void Basis::from_z(const Vector3 &p_z) { - - if (Math::abs(p_z.z) > Math_SQRT12) { - - // choose p in y-z plane - real_t a = p_z[1] * p_z[1] + p_z[2] * p_z[2]; - real_t k = 1.0 / Math::sqrt(a); - elements[0] = Vector3(0, -p_z[2] * k, p_z[1] * k); - elements[1] = Vector3(a * k, -p_z[0] * elements[0][2], p_z[0] * elements[0][1]); - } else { - - // choose p in x-y plane - real_t a = p_z.x * p_z.x + p_z.y * p_z.y; - real_t k = 1.0 / Math::sqrt(a); - elements[0] = Vector3(-p_z.y * k, p_z.x * k, 0); - elements[1] = Vector3(-p_z.z * elements[0].y, p_z.z * elements[0].x, a * k); - } - elements[2] = p_z; -} - -void Basis::invert() { - - real_t co[3] = { - cofac(1, 1, 2, 2), cofac(1, 2, 2, 0), cofac(1, 0, 2, 1) - }; - real_t det = elements[0][0] * co[0] + - elements[0][1] * co[1] + - elements[0][2] * co[2]; -#ifdef MATH_CHECKS - ERR_FAIL_COND(det == 0); -#endif - real_t s = 1.0 / det; - - set(co[0] * s, cofac(0, 2, 2, 1) * s, cofac(0, 1, 1, 2) * s, - co[1] * s, cofac(0, 0, 2, 2) * s, cofac(0, 2, 1, 0) * s, - co[2] * s, cofac(0, 1, 2, 0) * s, cofac(0, 0, 1, 1) * s); -} - -void Basis::orthonormalize() { -#ifdef MATH_CHECKS - ERR_FAIL_COND(determinant() == 0); -#endif - // Gram-Schmidt Process - - Vector3 x = get_axis(0); - Vector3 y = get_axis(1); - Vector3 z = get_axis(2); - - x.normalize(); - y = (y - x * (x.dot(y))); - y.normalize(); - z = (z - x * (x.dot(z)) - y * (y.dot(z))); - z.normalize(); - - set_axis(0, x); - set_axis(1, y); - set_axis(2, z); -} - -Basis Basis::orthonormalized() const { - - Basis c = *this; - c.orthonormalize(); - return c; -} - -bool Basis::is_orthogonal() const { - Basis id; - Basis m = (*this) * transposed(); - - return is_equal_approx(id, m); -} - -bool Basis::is_diagonal() const { - return ( - Math::is_equal_approx(elements[0][1], 0) && Math::is_equal_approx(elements[0][2], 0) && - Math::is_equal_approx(elements[1][0], 0) && Math::is_equal_approx(elements[1][2], 0) && - Math::is_equal_approx(elements[2][0], 0) && Math::is_equal_approx(elements[2][1], 0)); -} - -bool Basis::is_rotation() const { - return Math::is_equal_approx(determinant(), 1) && is_orthogonal(); -} - -bool Basis::is_symmetric() const { - - if (!Math::is_equal_approx(elements[0][1], elements[1][0])) - return false; - if (!Math::is_equal_approx(elements[0][2], elements[2][0])) - return false; - if (!Math::is_equal_approx(elements[1][2], elements[2][1])) - return false; - - return true; -} - -Basis Basis::diagonalize() { - -//NOTE: only implemented for symmetric matrices -//with the Jacobi iterative method method -#ifdef MATH_CHECKS - ERR_FAIL_COND_V(!is_symmetric(), Basis()); -#endif - const int ite_max = 1024; - - real_t off_matrix_norm_2 = elements[0][1] * elements[0][1] + elements[0][2] * elements[0][2] + elements[1][2] * elements[1][2]; - - int ite = 0; - Basis acc_rot; - while (off_matrix_norm_2 > CMP_EPSILON2 && ite++ < ite_max) { - real_t el01_2 = elements[0][1] * elements[0][1]; - real_t el02_2 = elements[0][2] * elements[0][2]; - real_t el12_2 = elements[1][2] * elements[1][2]; - // Find the pivot element - int i, j; - if (el01_2 > el02_2) { - if (el12_2 > el01_2) { - i = 1; - j = 2; - } else { - i = 0; - j = 1; - } - } else { - if (el12_2 > el02_2) { - i = 1; - j = 2; - } else { - i = 0; - j = 2; - } - } - - // Compute the rotation angle - real_t angle; - if (Math::is_equal_approx(elements[j][j], elements[i][i])) { - angle = Math_PI / 4; - } else { - angle = 0.5 * Math::atan(2 * elements[i][j] / (elements[j][j] - elements[i][i])); - } - - // Compute the rotation matrix - Basis rot; - rot.elements[i][i] = rot.elements[j][j] = Math::cos(angle); - rot.elements[i][j] = -(rot.elements[j][i] = Math::sin(angle)); - - // Update the off matrix norm - off_matrix_norm_2 -= elements[i][j] * elements[i][j]; - - // Apply the rotation - *this = rot * *this * rot.transposed(); - acc_rot = rot * acc_rot; - } - - return acc_rot; -} - -Basis Basis::inverse() const { - - Basis inv = *this; - inv.invert(); - return inv; -} - -void Basis::transpose() { - - SWAP(elements[0][1], elements[1][0]); - SWAP(elements[0][2], elements[2][0]); - SWAP(elements[1][2], elements[2][1]); -} - -Basis Basis::transposed() const { - - Basis tr = *this; - tr.transpose(); - return tr; -} - -// Multiplies the matrix from left by the scaling matrix: M -> S.M -// See the comment for Basis::rotated for further explanation. -void Basis::scale(const Vector3 &p_scale) { - - elements[0][0] *= p_scale.x; - elements[0][1] *= p_scale.x; - elements[0][2] *= p_scale.x; - elements[1][0] *= p_scale.y; - elements[1][1] *= p_scale.y; - elements[1][2] *= p_scale.y; - elements[2][0] *= p_scale.z; - elements[2][1] *= p_scale.z; - elements[2][2] *= p_scale.z; -} - -Basis Basis::scaled(const Vector3 &p_scale) const { - Basis m = *this; - m.scale(p_scale); - return m; -} - -void Basis::scale_local(const Vector3 &p_scale) { - // performs a scaling in object-local coordinate system: - // M -> (M.S.Minv).M = M.S. - *this = scaled_local(p_scale); -} - -Basis Basis::scaled_local(const Vector3 &p_scale) const { - Basis b; - b.set_diagonal(p_scale); - - return (*this) * b; -} - -Vector3 Basis::get_scale_abs() const { - - return Vector3( - Vector3(elements[0][0], elements[1][0], elements[2][0]).length(), - Vector3(elements[0][1], elements[1][1], elements[2][1]).length(), - Vector3(elements[0][2], elements[1][2], elements[2][2]).length()); -} - -Vector3 Basis::get_scale_local() const { - real_t det_sign = determinant() > 0 ? 1 : -1; - return det_sign * Vector3(elements[0].length(), elements[1].length(), elements[2].length()); -} - -// get_scale works with get_rotation, use get_scale_abs if you need to enforce positive signature. -Vector3 Basis::get_scale() const { - // FIXME: We are assuming M = R.S (R is rotation and S is scaling), and use polar decomposition to extract R and S. - // A polar decomposition is M = O.P, where O is an orthogonal matrix (meaning rotation and reflection) and - // P is a positive semi-definite matrix (meaning it contains absolute values of scaling along its diagonal). - // - // Despite being different from what we want to achieve, we can nevertheless make use of polar decomposition - // here as follows. We can split O into a rotation and a reflection as O = R.Q, and obtain M = R.S where - // we defined S = Q.P. Now, R is a proper rotation matrix and S is a (signed) scaling matrix, - // which can involve negative scalings. However, there is a catch: unlike the polar decomposition of M = O.P, - // the decomposition of O into a rotation and reflection matrix as O = R.Q is not unique. - // Therefore, we are going to do this decomposition by sticking to a particular convention. - // This may lead to confusion for some users though. - // - // The convention we use here is to absorb the sign flip into the scaling matrix. - // The same convention is also used in other similar functions such as get_rotation_axis_angle, get_rotation, ... - // - // A proper way to get rid of this issue would be to store the scaling values (or at least their signs) - // as a part of Basis. However, if we go that path, we need to disable direct (write) access to the - // matrix elements. - // - // The rotation part of this decomposition is returned by get_rotation* functions. - real_t det_sign = determinant() > 0 ? 1 : -1; - return det_sign * Vector3( - Vector3(elements[0][0], elements[1][0], elements[2][0]).length(), - Vector3(elements[0][1], elements[1][1], elements[2][1]).length(), - Vector3(elements[0][2], elements[1][2], elements[2][2]).length()); -} - -// Decomposes a Basis into a rotation-reflection matrix (an element of the group O(3)) and a positive scaling matrix as B = O.S. -// Returns the rotation-reflection matrix via reference argument, and scaling information is returned as a Vector3. -// This (internal) function is too specific and named too ugly to expose to users, and probably there's no need to do so. -Vector3 Basis::rotref_posscale_decomposition(Basis &rotref) const { -#ifdef MATH_CHECKS - ERR_FAIL_COND_V(determinant() == 0, Vector3()); - - Basis m = transposed() * (*this); - ERR_FAIL_COND_V(!m.is_diagonal(), Vector3()); -#endif - Vector3 scale = get_scale(); - Basis inv_scale = Basis().scaled(scale.inverse()); // this will also absorb the sign of scale - rotref = (*this) * inv_scale; - -#ifdef MATH_CHECKS - ERR_FAIL_COND_V(!rotref.is_orthogonal(), Vector3()); -#endif - return scale.abs(); -} - -// Multiplies the matrix from left by the rotation matrix: M -> R.M -// Note that this does *not* rotate the matrix itself. -// -// The main use of Basis is as Transform.basis, which is used a the transformation matrix -// of 3D object. Rotate here refers to rotation of the object (which is R * (*this)), -// not the matrix itself (which is R * (*this) * R.transposed()). -Basis Basis::rotated(const Vector3 &p_axis, real_t p_phi) const { - return Basis(p_axis, p_phi) * (*this); -} - -void Basis::rotate(const Vector3 &p_axis, real_t p_phi) { - *this = rotated(p_axis, p_phi); -} - -void Basis::rotate_local(const Vector3 &p_axis, real_t p_phi) { - // performs a rotation in object-local coordinate system: - // M -> (M.R.Minv).M = M.R. - *this = rotated_local(p_axis, p_phi); -} -Basis Basis::rotated_local(const Vector3 &p_axis, real_t p_phi) const { - - return (*this) * Basis(p_axis, p_phi); -} - -Basis Basis::rotated(const Vector3 &p_euler) const { - return Basis(p_euler) * (*this); -} - -void Basis::rotate(const Vector3 &p_euler) { - *this = rotated(p_euler); -} - -Basis Basis::rotated(const Quat &p_quat) const { - return Basis(p_quat) * (*this); -} - -void Basis::rotate(const Quat &p_quat) { - *this = rotated(p_quat); -} - -Vector3 Basis::get_rotation_euler() 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. - Basis m = orthonormalized(); - real_t det = m.determinant(); - if (det < 0) { - // Ensure that the determinant is 1, such that result is a proper rotation matrix which can be represented by Euler angles. - m.scale(Vector3(-1, -1, -1)); - } - - return m.get_euler(); -} - -Quat Basis::get_rotation_quat() 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. - Basis m = orthonormalized(); - real_t det = m.determinant(); - if (det < 0) { - // Ensure that the determinant is 1, such that result is a proper rotation matrix which can be represented by Euler angles. - m.scale(Vector3(-1, -1, -1)); - } - - return m.get_quat(); -} - -void Basis::get_rotation_axis_angle(Vector3 &p_axis, real_t &p_angle) 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. - Basis m = orthonormalized(); - real_t det = m.determinant(); - if (det < 0) { - // Ensure that the determinant is 1, such that result is a proper rotation matrix which can be represented by Euler angles. - m.scale(Vector3(-1, -1, -1)); - } - - m.get_axis_angle(p_axis, p_angle); -} - -void Basis::get_rotation_axis_angle_local(Vector3 &p_axis, real_t &p_angle) 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. - Basis m = transposed(); - m.orthonormalize(); - real_t det = m.determinant(); - if (det < 0) { - // Ensure that the determinant is 1, such that result is a proper rotation matrix which can be represented by Euler angles. - m.scale(Vector3(-1, -1, -1)); - } - - m.get_axis_angle(p_axis, p_angle); - p_angle = -p_angle; -} - -// get_euler_xyz returns a vector containing the Euler angles in the format -// (a1,a2,a3), where a3 is the angle of the first rotation, and a1 is the last -// (following the convention they are commonly defined in the literature). -// -// The current implementation uses XYZ convention (Z is the first rotation), -// so euler.z is the angle of the (first) rotation around Z axis and so on, -// -// And thus, assuming the matrix is a rotation matrix, this function returns -// the angles in the decomposition R = X(a1).Y(a2).Z(a3) where Z(a) rotates -// around the z-axis by a and so on. -Vector3 Basis::get_euler_xyz() const { - - // Euler angles in XYZ convention. - // See https://en.wikipedia.org/wiki/Euler_angles#Rotation_matrix - // - // rot = cy*cz -cy*sz sy - // cz*sx*sy+cx*sz cx*cz-sx*sy*sz -cy*sx - // -cx*cz*sy+sx*sz cz*sx+cx*sy*sz cx*cy - - Vector3 euler; -#ifdef MATH_CHECKS - ERR_FAIL_COND_V(!is_rotation(), euler); -#endif - real_t sy = elements[0][2]; - if (sy < 1.0) { - if (sy > -1.0) { - // is this a pure Y rotation? - if (elements[1][0] == 0.0 && elements[0][1] == 0.0 && elements[1][2] == 0 && elements[2][1] == 0 && elements[1][1] == 1) { - // return the simplest form (human friendlier in editor and scripts) - euler.x = 0; - euler.y = atan2(elements[0][2], elements[0][0]); - euler.z = 0; - } else { - euler.x = Math::atan2(-elements[1][2], elements[2][2]); - euler.y = Math::asin(sy); - euler.z = Math::atan2(-elements[0][1], elements[0][0]); - } - } else { - euler.x = -Math::atan2(elements[0][1], elements[1][1]); - euler.y = -Math_PI / 2.0; - euler.z = 0.0; - } - } else { - euler.x = Math::atan2(elements[0][1], elements[1][1]); - euler.y = Math_PI / 2.0; - euler.z = 0.0; - } - return euler; -} - -// set_euler_xyz 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. -// The current implementation uses XYZ convention (Z is the first rotation). -void Basis::set_euler_xyz(const Vector3 &p_euler) { - - real_t c, s; - - c = Math::cos(p_euler.x); - s = Math::sin(p_euler.x); - Basis xmat(1.0, 0.0, 0.0, 0.0, c, -s, 0.0, s, c); - - c = Math::cos(p_euler.y); - s = Math::sin(p_euler.y); - Basis ymat(c, 0.0, s, 0.0, 1.0, 0.0, -s, 0.0, c); - - c = Math::cos(p_euler.z); - s = Math::sin(p_euler.z); - Basis zmat(c, -s, 0.0, s, c, 0.0, 0.0, 0.0, 1.0); - - //optimizer will optimize away all this anyway - *this = xmat * (ymat * zmat); -} - -// get_euler_yxz returns a vector containing the Euler angles in the YXZ convention, -// as in first-Z, then-X, last-Y. The angles for X, Y, and Z rotations are returned -// as the x, y, and z components of a Vector3 respectively. -Vector3 Basis::get_euler_yxz() const { - - // Euler angles in YXZ convention. - // See https://en.wikipedia.org/wiki/Euler_angles#Rotation_matrix - // - // rot = cy*cz+sy*sx*sz cz*sy*sx-cy*sz cx*sy - // cx*sz cx*cz -sx - // cy*sx*sz-cz*sy cy*cz*sx+sy*sz cy*cx - - Vector3 euler; -#ifdef MATH_CHECKS - ERR_FAIL_COND_V(!is_rotation(), euler); -#endif - real_t m12 = elements[1][2]; - - if (m12 < 1) { - if (m12 > -1) { - // is this a pure X rotation? - if (elements[1][0] == 0 && elements[0][1] == 0 && elements[0][2] == 0 && elements[2][0] == 0 && elements[0][0] == 1) { - // return the simplest form (human friendlier in editor and scripts) - euler.x = atan2(-m12, elements[1][1]); - euler.y = 0; - euler.z = 0; - } else { - euler.x = asin(-m12); - euler.y = atan2(elements[0][2], elements[2][2]); - euler.z = atan2(elements[1][0], elements[1][1]); - } - } else { // m12 == -1 - euler.x = Math_PI * 0.5; - euler.y = -atan2(-elements[0][1], elements[0][0]); - euler.z = 0; - } - } else { // m12 == 1 - euler.x = -Math_PI * 0.5; - euler.y = -atan2(-elements[0][1], elements[0][0]); - euler.z = 0; - } - - return euler; -} - -// set_euler_yxz 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. -// The current implementation uses YXZ convention (Z is the first rotation). -void Basis::set_euler_yxz(const Vector3 &p_euler) { - - real_t c, s; - - c = Math::cos(p_euler.x); - s = Math::sin(p_euler.x); - Basis xmat(1.0, 0.0, 0.0, 0.0, c, -s, 0.0, s, c); - - c = Math::cos(p_euler.y); - s = Math::sin(p_euler.y); - Basis ymat(c, 0.0, s, 0.0, 1.0, 0.0, -s, 0.0, c); - - c = Math::cos(p_euler.z); - s = Math::sin(p_euler.z); - Basis zmat(c, -s, 0.0, s, c, 0.0, 0.0, 0.0, 1.0); - - //optimizer will optimize away all this anyway - *this = ymat * xmat * zmat; -} - -bool Basis::is_equal_approx(const Basis &a, const Basis &b) const { - - for (int i = 0; i < 3; i++) { - for (int j = 0; j < 3; j++) { - if (!Math::is_equal_approx(a.elements[i][j], b.elements[i][j])) - return false; - } - } - - return true; -} - -bool Basis::operator==(const Basis &p_matrix) const { - - for (int i = 0; i < 3; i++) { - for (int j = 0; j < 3; j++) { - if (elements[i][j] != p_matrix.elements[i][j]) - return false; - } - } - - return true; -} - -bool Basis::operator!=(const Basis &p_matrix) const { - - return (!(*this == p_matrix)); -} - -Basis::operator String() const { - - String mtx; - for (int i = 0; i < 3; i++) { - - for (int j = 0; j < 3; j++) { - - if (i != 0 || j != 0) - mtx += ", "; - - mtx += rtos(elements[i][j]); - } - } - - return mtx; -} - -Quat Basis::get_quat() const { -#ifdef MATH_CHECKS - ERR_FAIL_COND_V(!is_rotation(), Quat()); -#endif - real_t trace = elements[0][0] + elements[1][1] + elements[2][2]; - real_t temp[4]; - - if (trace > 0.0) { - real_t s = Math::sqrt(trace + 1.0); - temp[3] = (s * 0.5); - s = 0.5 / s; - - temp[0] = ((elements[2][1] - elements[1][2]) * s); - temp[1] = ((elements[0][2] - elements[2][0]) * s); - temp[2] = ((elements[1][0] - elements[0][1]) * s); - } else { - int i = elements[0][0] < elements[1][1] ? - (elements[1][1] < elements[2][2] ? 2 : 1) : - (elements[0][0] < elements[2][2] ? 2 : 0); - int j = (i + 1) % 3; - int k = (i + 2) % 3; - - real_t s = Math::sqrt(elements[i][i] - elements[j][j] - elements[k][k] + 1.0); - temp[i] = s * 0.5; - s = 0.5 / s; - - temp[3] = (elements[k][j] - elements[j][k]) * s; - temp[j] = (elements[j][i] + elements[i][j]) * s; - temp[k] = (elements[k][i] + elements[i][k]) * s; - } - - return Quat(temp[0], temp[1], temp[2], temp[3]); -} - -static const Basis _ortho_bases[24] = { - Basis(1, 0, 0, 0, 1, 0, 0, 0, 1), - Basis(0, -1, 0, 1, 0, 0, 0, 0, 1), - Basis(-1, 0, 0, 0, -1, 0, 0, 0, 1), - Basis(0, 1, 0, -1, 0, 0, 0, 0, 1), - Basis(1, 0, 0, 0, 0, -1, 0, 1, 0), - Basis(0, 0, 1, 1, 0, 0, 0, 1, 0), - Basis(-1, 0, 0, 0, 0, 1, 0, 1, 0), - Basis(0, 0, -1, -1, 0, 0, 0, 1, 0), - Basis(1, 0, 0, 0, -1, 0, 0, 0, -1), - Basis(0, 1, 0, 1, 0, 0, 0, 0, -1), - Basis(-1, 0, 0, 0, 1, 0, 0, 0, -1), - Basis(0, -1, 0, -1, 0, 0, 0, 0, -1), - Basis(1, 0, 0, 0, 0, 1, 0, -1, 0), - Basis(0, 0, -1, 1, 0, 0, 0, -1, 0), - Basis(-1, 0, 0, 0, 0, -1, 0, -1, 0), - Basis(0, 0, 1, -1, 0, 0, 0, -1, 0), - Basis(0, 0, 1, 0, 1, 0, -1, 0, 0), - Basis(0, -1, 0, 0, 0, 1, -1, 0, 0), - Basis(0, 0, -1, 0, -1, 0, -1, 0, 0), - Basis(0, 1, 0, 0, 0, -1, -1, 0, 0), - Basis(0, 0, 1, 0, -1, 0, 1, 0, 0), - Basis(0, 1, 0, 0, 0, 1, 1, 0, 0), - Basis(0, 0, -1, 0, 1, 0, 1, 0, 0), - Basis(0, -1, 0, 0, 0, -1, 1, 0, 0) -}; - -int Basis::get_orthogonal_index() const { - - //could be sped up if i come up with a way - Basis orth = *this; - for (int i = 0; i < 3; i++) { - for (int j = 0; j < 3; j++) { - - real_t v = orth[i][j]; - if (v > 0.5) - v = 1.0; - else if (v < -0.5) - v = -1.0; - else - v = 0; - - orth[i][j] = v; - } - } - - for (int i = 0; i < 24; i++) { - - if (_ortho_bases[i] == orth) - return i; - } - - return 0; -} - -void Basis::set_orthogonal_index(int p_index) { - - //there only exist 24 orthogonal bases in r3 - ERR_FAIL_INDEX(p_index, 24); - - *this = _ortho_bases[p_index]; -} - -void Basis::get_axis_angle(Vector3 &r_axis, real_t &r_angle) const { -#ifdef MATH_CHECKS - ERR_FAIL_COND(!is_rotation()); -#endif - real_t angle, x, y, z; // variables for result - real_t epsilon = 0.01; // margin to allow for rounding errors - real_t epsilon2 = 0.1; // margin to distinguish between 0 and 180 degrees - - if ((Math::abs(elements[1][0] - elements[0][1]) < epsilon) && (Math::abs(elements[2][0] - elements[0][2]) < epsilon) && (Math::abs(elements[2][1] - elements[1][2]) < epsilon)) { - // singularity found - // first check for identity matrix which must have +1 for all terms - // in leading diagonaland zero in other terms - if ((Math::abs(elements[1][0] + elements[0][1]) < epsilon2) && (Math::abs(elements[2][0] + elements[0][2]) < epsilon2) && (Math::abs(elements[2][1] + elements[1][2]) < epsilon2) && (Math::abs(elements[0][0] + elements[1][1] + elements[2][2] - 3) < epsilon2)) { - // this singularity is identity matrix so angle = 0 - r_axis = Vector3(0, 1, 0); - r_angle = 0; - return; - } - // otherwise this singularity is angle = 180 - angle = Math_PI; - real_t xx = (elements[0][0] + 1) / 2; - real_t yy = (elements[1][1] + 1) / 2; - real_t zz = (elements[2][2] + 1) / 2; - real_t xy = (elements[1][0] + elements[0][1]) / 4; - real_t xz = (elements[2][0] + elements[0][2]) / 4; - real_t yz = (elements[2][1] + elements[1][2]) / 4; - if ((xx > yy) && (xx > zz)) { // elements[0][0] is the largest diagonal term - if (xx < epsilon) { - x = 0; - y = 0.7071; - z = 0.7071; - } else { - x = Math::sqrt(xx); - y = xy / x; - z = xz / x; - } - } else if (yy > zz) { // elements[1][1] is the largest diagonal term - if (yy < epsilon) { - x = 0.7071; - y = 0; - z = 0.7071; - } else { - y = Math::sqrt(yy); - x = xy / y; - z = yz / y; - } - } else { // elements[2][2] is the largest diagonal term so base result on this - if (zz < epsilon) { - x = 0.7071; - y = 0.7071; - z = 0; - } else { - z = Math::sqrt(zz); - x = xz / z; - y = yz / z; - } - } - r_axis = Vector3(x, y, z); - r_angle = angle; - return; - } - // as we have reached here there are no singularities so we can handle normally - real_t s = Math::sqrt((elements[1][2] - elements[2][1]) * (elements[1][2] - elements[2][1]) + (elements[2][0] - elements[0][2]) * (elements[2][0] - elements[0][2]) + (elements[0][1] - elements[1][0]) * (elements[0][1] - elements[1][0])); // s=|axis||sin(angle)|, used to normalise - - angle = Math::acos((elements[0][0] + elements[1][1] + elements[2][2] - 1) / 2); - if (angle < 0) s = -s; - x = (elements[2][1] - elements[1][2]) / s; - y = (elements[0][2] - elements[2][0]) / s; - z = (elements[1][0] - elements[0][1]) / s; - - r_axis = Vector3(x, y, z); - r_angle = angle; -} - -void Basis::set_quat(const Quat &p_quat) { - - real_t d = p_quat.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; - 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)); -} - -void Basis::set_axis_angle(const Vector3 &p_axis, real_t p_phi) { -// Rotation matrix from axis and angle, see https://en.wikipedia.org/wiki/Rotation_matrix#Rotation_matrix_from_axis_angle -#ifdef MATH_CHECKS - ERR_FAIL_COND(!p_axis.is_normalized()); -#endif - Vector3 axis_sq(p_axis.x * p_axis.x, p_axis.y * p_axis.y, p_axis.z * p_axis.z); - - real_t cosine = Math::cos(p_phi); - real_t sine = Math::sin(p_phi); - - elements[0][0] = axis_sq.x + cosine * (1.0 - axis_sq.x); - elements[0][1] = p_axis.x * p_axis.y * (1.0 - cosine) - p_axis.z * sine; - elements[0][2] = p_axis.z * p_axis.x * (1.0 - cosine) + p_axis.y * sine; - - elements[1][0] = p_axis.x * p_axis.y * (1.0 - cosine) + p_axis.z * sine; - elements[1][1] = axis_sq.y + cosine * (1.0 - axis_sq.y); - elements[1][2] = p_axis.y * p_axis.z * (1.0 - cosine) - p_axis.x * sine; - - elements[2][0] = p_axis.z * p_axis.x * (1.0 - cosine) - p_axis.y * sine; - elements[2][1] = p_axis.y * p_axis.z * (1.0 - cosine) + p_axis.x * sine; - elements[2][2] = axis_sq.z + cosine * (1.0 - axis_sq.z); -} - -void Basis::set_axis_angle_scale(const Vector3 &p_axis, real_t p_phi, const Vector3 &p_scale) { - set_diagonal(p_scale); - rotate(p_axis, p_phi); -} - -void Basis::set_euler_scale(const Vector3 &p_euler, const Vector3 &p_scale) { - set_diagonal(p_scale); - rotate(p_euler); -} - -void Basis::set_quat_scale(const Quat &p_quat, const Vector3 &p_scale) { - set_diagonal(p_scale); - rotate(p_quat); -} - -void Basis::set_diagonal(const Vector3 p_diag) { - elements[0][0] = p_diag.x; - elements[0][1] = 0; - elements[0][2] = 0; - - elements[1][0] = 0; - elements[1][1] = p_diag.y; - elements[1][2] = 0; - - elements[2][0] = 0; - elements[2][1] = 0; - elements[2][2] = p_diag.z; -} - -Basis Basis::slerp(const Basis &target, const real_t &t) const { -// TODO: implement this directly without using quaternions to make it more efficient -#ifdef MATH_CHECKS - ERR_FAIL_COND_V(!is_rotation(), Basis()); - ERR_FAIL_COND_V(!target.is_rotation(), Basis()); -#endif - - Quat from(*this); - Quat to(target); - - return Basis(from.slerp(to, t)); -} diff --git a/core/math/matrix3.h b/core/math/matrix3.h deleted file mode 100644 index e7d6ab4522..0000000000 --- a/core/math/matrix3.h +++ /dev/null @@ -1,344 +0,0 @@ -/*************************************************************************/ -/* matrix3.h */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2019 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. */ -/*************************************************************************/ - -// Circular dependency between Vector3 and Basis :/ -#include "core/math/vector3.h" - -#ifndef MATRIX3_H -#define MATRIX3_H - -#include "core/math/quat.h" - -/** - @author Juan Linietsky -*/ - -class Basis { -public: - Vector3 elements[3]; - - _FORCE_INLINE_ const Vector3 &operator[](int axis) const { - - return elements[axis]; - } - _FORCE_INLINE_ Vector3 &operator[](int axis) { - - return elements[axis]; - } - - void invert(); - void transpose(); - - Basis inverse() const; - Basis transposed() const; - - _FORCE_INLINE_ real_t determinant() const; - - void from_z(const Vector3 &p_z); - - _FORCE_INLINE_ Vector3 get_axis(int p_axis) const { - // get actual basis axis (elements is transposed for performance) - return Vector3(elements[0][p_axis], elements[1][p_axis], elements[2][p_axis]); - } - _FORCE_INLINE_ void set_axis(int p_axis, const Vector3 &p_value) { - // get actual basis axis (elements is transposed for performance) - elements[0][p_axis] = p_value.x; - elements[1][p_axis] = p_value.y; - elements[2][p_axis] = p_value.z; - } - - void rotate(const Vector3 &p_axis, real_t p_phi); - Basis rotated(const Vector3 &p_axis, real_t p_phi) const; - - void rotate_local(const Vector3 &p_axis, real_t p_phi); - Basis rotated_local(const Vector3 &p_axis, real_t p_phi) const; - - 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; - - 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; - Vector3 get_rotation() const { return get_rotation_euler(); }; - - Vector3 rotref_posscale_decomposition(Basis &rotref) const; - - Vector3 get_euler_xyz() const; - void set_euler_xyz(const Vector3 &p_euler); - Vector3 get_euler_yxz() const; - void set_euler_yxz(const Vector3 &p_euler); - - Quat get_quat() const; - void set_quat(const Quat &p_quat); - - Vector3 get_euler() const { return get_euler_yxz(); } - void set_euler(const Vector3 &p_euler) { set_euler_yxz(p_euler); } - - void get_axis_angle(Vector3 &r_axis, real_t &r_angle) const; - void set_axis_angle(const Vector3 &p_axis, real_t p_phi); - - void scale(const Vector3 &p_scale); - Basis scaled(const Vector3 &p_scale) const; - - void scale_local(const Vector3 &p_scale); - Basis scaled_local(const Vector3 &p_scale) const; - - Vector3 get_scale() const; - Vector3 get_scale_abs() const; - Vector3 get_scale_local() const; - - 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); - - // transposed dot products - _FORCE_INLINE_ real_t tdotx(const Vector3 &v) const { - return elements[0][0] * v[0] + elements[1][0] * v[1] + elements[2][0] * v[2]; - } - _FORCE_INLINE_ real_t tdoty(const Vector3 &v) const { - return elements[0][1] * v[0] + elements[1][1] * v[1] + elements[2][1] * v[2]; - } - _FORCE_INLINE_ real_t tdotz(const Vector3 &v) const { - return elements[0][2] * v[0] + elements[1][2] * v[1] + elements[2][2] * v[2]; - } - - bool is_equal_approx(const Basis &a, const Basis &b) const; - - bool operator==(const Basis &p_matrix) const; - bool operator!=(const Basis &p_matrix) const; - - _FORCE_INLINE_ Vector3 xform(const Vector3 &p_vector) const; - _FORCE_INLINE_ Vector3 xform_inv(const Vector3 &p_vector) const; - _FORCE_INLINE_ void operator*=(const Basis &p_matrix); - _FORCE_INLINE_ Basis operator*(const Basis &p_matrix) const; - _FORCE_INLINE_ void operator+=(const Basis &p_matrix); - _FORCE_INLINE_ Basis operator+(const Basis &p_matrix) const; - _FORCE_INLINE_ void operator-=(const Basis &p_matrix); - _FORCE_INLINE_ Basis operator-(const Basis &p_matrix) const; - _FORCE_INLINE_ void operator*=(real_t p_val); - _FORCE_INLINE_ Basis operator*(real_t p_val) const; - - int get_orthogonal_index() const; - void set_orthogonal_index(int p_index); - - void set_diagonal(const Vector3 p_diag); - - bool is_orthogonal() const; - bool is_diagonal() const; - bool is_rotation() const; - - Basis slerp(const Basis &target, const real_t &t) const; - - operator String() const; - - /* create / set */ - - _FORCE_INLINE_ void set(real_t xx, real_t xy, real_t xz, real_t yx, real_t yy, real_t yz, real_t zx, real_t zy, real_t zz) { - - elements[0][0] = xx; - elements[0][1] = xy; - elements[0][2] = xz; - elements[1][0] = yx; - elements[1][1] = yy; - elements[1][2] = yz; - elements[2][0] = zx; - elements[2][1] = zy; - elements[2][2] = zz; - } - _FORCE_INLINE_ void set(const Vector3 &p_x, const Vector3 &p_y, const Vector3 &p_z) { - - set_axis(0, p_x); - set_axis(1, p_y); - set_axis(2, p_z); - } - _FORCE_INLINE_ Vector3 get_column(int i) const { - - return Vector3(elements[0][i], elements[1][i], elements[2][i]); - } - - _FORCE_INLINE_ Vector3 get_row(int i) const { - - return Vector3(elements[i][0], elements[i][1], elements[i][2]); - } - _FORCE_INLINE_ Vector3 get_main_diagonal() const { - return Vector3(elements[0][0], elements[1][1], elements[2][2]); - } - - _FORCE_INLINE_ void set_row(int i, const Vector3 &p_row) { - elements[i][0] = p_row.x; - elements[i][1] = p_row.y; - elements[i][2] = p_row.z; - } - - _FORCE_INLINE_ void set_zero() { - elements[0].zero(); - elements[1].zero(); - elements[2].zero(); - } - - _FORCE_INLINE_ Basis transpose_xform(const Basis &m) const { - return Basis( - elements[0].x * m[0].x + elements[1].x * m[1].x + elements[2].x * m[2].x, - elements[0].x * m[0].y + elements[1].x * m[1].y + elements[2].x * m[2].y, - elements[0].x * m[0].z + elements[1].x * m[1].z + elements[2].x * m[2].z, - elements[0].y * m[0].x + elements[1].y * m[1].x + elements[2].y * m[2].x, - elements[0].y * m[0].y + elements[1].y * m[1].y + elements[2].y * m[2].y, - elements[0].y * m[0].z + elements[1].y * m[1].z + elements[2].y * m[2].z, - elements[0].z * m[0].x + elements[1].z * m[1].x + elements[2].z * m[2].x, - elements[0].z * m[0].y + elements[1].z * m[1].y + elements[2].z * m[2].y, - elements[0].z * m[0].z + elements[1].z * m[1].z + elements[2].z * m[2].z); - } - Basis(real_t xx, real_t xy, real_t xz, real_t yx, real_t yy, real_t yz, real_t zx, real_t zy, real_t zz) { - - set(xx, xy, xz, yx, yy, yz, zx, zy, zz); - } - - void orthonormalize(); - Basis orthonormalized() const; - - bool is_symmetric() const; - Basis diagonalize(); - - operator Quat() const { return get_quat(); } - - 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 Vector3 &p_euler) { set_euler(p_euler); } - Basis(const Vector3 &p_euler, const Vector3 &p_scale) { set_euler_scale(p_euler, p_scale); } - - Basis(const Vector3 &p_axis, real_t p_phi) { set_axis_angle(p_axis, p_phi); } - Basis(const Vector3 &p_axis, real_t p_phi, const Vector3 &p_scale) { set_axis_angle_scale(p_axis, p_phi, p_scale); } - - _FORCE_INLINE_ Basis(const Vector3 &row0, const Vector3 &row1, const Vector3 &row2) { - elements[0] = row0; - elements[1] = row1; - elements[2] = row2; - } - - _FORCE_INLINE_ Basis() { - - elements[0][0] = 1; - elements[0][1] = 0; - elements[0][2] = 0; - elements[1][0] = 0; - elements[1][1] = 1; - elements[1][2] = 0; - elements[2][0] = 0; - elements[2][1] = 0; - elements[2][2] = 1; - } -}; - -_FORCE_INLINE_ void Basis::operator*=(const Basis &p_matrix) { - - set( - p_matrix.tdotx(elements[0]), p_matrix.tdoty(elements[0]), p_matrix.tdotz(elements[0]), - p_matrix.tdotx(elements[1]), p_matrix.tdoty(elements[1]), p_matrix.tdotz(elements[1]), - p_matrix.tdotx(elements[2]), p_matrix.tdoty(elements[2]), p_matrix.tdotz(elements[2])); -} - -_FORCE_INLINE_ Basis Basis::operator*(const Basis &p_matrix) const { - - return Basis( - p_matrix.tdotx(elements[0]), p_matrix.tdoty(elements[0]), p_matrix.tdotz(elements[0]), - p_matrix.tdotx(elements[1]), p_matrix.tdoty(elements[1]), p_matrix.tdotz(elements[1]), - p_matrix.tdotx(elements[2]), p_matrix.tdoty(elements[2]), p_matrix.tdotz(elements[2])); -} - -_FORCE_INLINE_ void Basis::operator+=(const Basis &p_matrix) { - - elements[0] += p_matrix.elements[0]; - elements[1] += p_matrix.elements[1]; - elements[2] += p_matrix.elements[2]; -} - -_FORCE_INLINE_ Basis Basis::operator+(const Basis &p_matrix) const { - - Basis ret(*this); - ret += p_matrix; - return ret; -} - -_FORCE_INLINE_ void Basis::operator-=(const Basis &p_matrix) { - - elements[0] -= p_matrix.elements[0]; - elements[1] -= p_matrix.elements[1]; - elements[2] -= p_matrix.elements[2]; -} - -_FORCE_INLINE_ Basis Basis::operator-(const Basis &p_matrix) const { - - Basis ret(*this); - ret -= p_matrix; - return ret; -} - -_FORCE_INLINE_ void Basis::operator*=(real_t p_val) { - - elements[0] *= p_val; - elements[1] *= p_val; - elements[2] *= p_val; -} - -_FORCE_INLINE_ Basis Basis::operator*(real_t p_val) const { - - Basis ret(*this); - ret *= p_val; - return ret; -} - -Vector3 Basis::xform(const Vector3 &p_vector) const { - - return Vector3( - elements[0].dot(p_vector), - elements[1].dot(p_vector), - elements[2].dot(p_vector)); -} - -Vector3 Basis::xform_inv(const Vector3 &p_vector) const { - - return Vector3( - (elements[0][0] * p_vector.x) + (elements[1][0] * p_vector.y) + (elements[2][0] * p_vector.z), - (elements[0][1] * p_vector.x) + (elements[1][1] * p_vector.y) + (elements[2][1] * p_vector.z), - (elements[0][2] * p_vector.x) + (elements[1][2] * p_vector.y) + (elements[2][2] * p_vector.z)); -} - -real_t Basis::determinant() const { - - return elements[0][0] * (elements[1][1] * elements[2][2] - elements[2][1] * elements[1][2]) - - elements[1][0] * (elements[0][1] * elements[2][2] - elements[2][1] * elements[0][2]) + - elements[2][0] * (elements[0][1] * elements[1][2] - elements[1][1] * elements[0][2]); -} -#endif diff --git a/core/math/quat.cpp b/core/math/quat.cpp index c1e45f36f0..6833d5de55 100644 --- a/core/math/quat.cpp +++ b/core/math/quat.cpp @@ -30,7 +30,7 @@ #include "quat.h" -#include "core/math/matrix3.h" +#include "core/math/basis.h" #include "core/print_string.h" // set_euler_xyz expects a vector containing the Euler angles in the format diff --git a/core/math/transform.h b/core/math/transform.h index 9b323a6f0f..2f43f6b035 100644 --- a/core/math/transform.h +++ b/core/math/transform.h @@ -32,7 +32,7 @@ #define TRANSFORM_H #include "core/math/aabb.h" -#include "core/math/matrix3.h" +#include "core/math/basis.h" #include "core/math/plane.h" /** diff --git a/core/math/vector3.cpp b/core/math/vector3.cpp index b2e89ac7b8..1c28934422 100644 --- a/core/math/vector3.cpp +++ b/core/math/vector3.cpp @@ -30,7 +30,7 @@ #include "vector3.h" -#include "core/math/matrix3.h" +#include "core/math/basis.h" void Vector3::rotate(const Vector3 &p_axis, real_t p_phi) { diff --git a/core/math/vector3.h b/core/math/vector3.h index b0eef35635..8d6e093c4c 100644 --- a/core/math/vector3.h +++ b/core/math/vector3.h @@ -151,7 +151,7 @@ struct Vector3 { }; // Should be included after class definition, otherwise we get circular refs -#include "core/math/matrix3.h" +#include "core/math/basis.h" Vector3 Vector3::cross(const Vector3 &p_b) const { diff --git a/core/variant.h b/core/variant.h index 0377c78ea8..6ddaf17406 100644 --- a/core/variant.h +++ b/core/variant.h @@ -41,8 +41,8 @@ #include "core/dvector.h" #include "core/io/ip_address.h" #include "core/math/aabb.h" +#include "core/math/basis.h" #include "core/math/face3.h" -#include "core/math/matrix3.h" #include "core/math/plane.h" #include "core/math/quat.h" #include "core/math/transform.h" -- cgit v1.2.3