| 1 | /**************************************************************************/ |
| 2 | /* transform_interpolator.cpp */ |
| 3 | /**************************************************************************/ |
| 4 | /* This file is part of: */ |
| 5 | /* GODOT ENGINE */ |
| 6 | /* https://godotengine.org */ |
| 7 | /**************************************************************************/ |
| 8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
| 9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
| 10 | /* */ |
| 11 | /* Permission is hereby granted, free of charge, to any person obtaining */ |
| 12 | /* a copy of this software and associated documentation files (the */ |
| 13 | /* "Software"), to deal in the Software without restriction, including */ |
| 14 | /* without limitation the rights to use, copy, modify, merge, publish, */ |
| 15 | /* distribute, sublicense, and/or sell copies of the Software, and to */ |
| 16 | /* permit persons to whom the Software is furnished to do so, subject to */ |
| 17 | /* the following conditions: */ |
| 18 | /* */ |
| 19 | /* The above copyright notice and this permission notice shall be */ |
| 20 | /* included in all copies or substantial portions of the Software. */ |
| 21 | /* */ |
| 22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
| 23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
| 24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
| 25 | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
| 26 | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
| 27 | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
| 28 | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
| 29 | /**************************************************************************/ |
| 30 | |
| 31 | #include "transform_interpolator.h" |
| 32 | |
| 33 | #include "core/math/transform_2d.h" |
| 34 | #include "core/math/transform_3d.h" |
| 35 | |
| 36 | void TransformInterpolator::interpolate_transform_2d(const Transform2D &p_prev, const Transform2D &p_curr, Transform2D &r_result, real_t p_fraction) { |
| 37 | // Special case for physics interpolation, if flipping, don't interpolate basis. |
| 38 | // If the determinant polarity changes, the handedness of the coordinate system changes. |
| 39 | if (_sign(p_prev.determinant()) != _sign(p_curr.determinant())) { |
| 40 | r_result.columns[0] = p_curr.columns[0]; |
| 41 | r_result.columns[1] = p_curr.columns[1]; |
| 42 | r_result.set_origin(p_prev.get_origin().lerp(p_curr.get_origin(), p_fraction)); |
| 43 | return; |
| 44 | } |
| 45 | |
| 46 | r_result = p_prev.interpolate_with(p_curr, p_fraction); |
| 47 | } |
| 48 | |
| 49 | void TransformInterpolator::interpolate_transform_3d(const Transform3D &p_prev, const Transform3D &p_curr, Transform3D &r_result, real_t p_fraction) { |
| 50 | r_result.origin = p_prev.origin + ((p_curr.origin - p_prev.origin) * p_fraction); |
| 51 | interpolate_basis(p_prev.basis, p_curr.basis, r_result.basis, p_fraction); |
| 52 | } |
| 53 | |
| 54 | void TransformInterpolator::interpolate_basis(const Basis &p_prev, const Basis &p_curr, Basis &r_result, real_t p_fraction) { |
| 55 | Method method = find_method(p_prev, p_curr); |
| 56 | interpolate_basis_via_method(p_prev, p_curr, r_result, p_fraction, method); |
| 57 | } |
| 58 | |
| 59 | void TransformInterpolator::interpolate_transform_3d_via_method(const Transform3D &p_prev, const Transform3D &p_curr, Transform3D &r_result, real_t p_fraction, Method p_method) { |
| 60 | r_result.origin = p_prev.origin + ((p_curr.origin - p_prev.origin) * p_fraction); |
| 61 | interpolate_basis_via_method(p_prev.basis, p_curr.basis, r_result.basis, p_fraction, p_method); |
| 62 | } |
| 63 | |
| 64 | void TransformInterpolator::interpolate_basis_via_method(const Basis &p_prev, const Basis &p_curr, Basis &r_result, real_t p_fraction, Method p_method) { |
| 65 | switch (p_method) { |
| 66 | default: { |
| 67 | interpolate_basis_linear(p_prev, p_curr, r_result, p_fraction); |
| 68 | } break; |
| 69 | case INTERP_SLERP: { |
| 70 | r_result = _basis_slerp_unchecked(p_prev, p_curr, p_fraction); |
| 71 | } break; |
| 72 | case INTERP_SCALED_SLERP: { |
| 73 | interpolate_basis_scaled_slerp(p_prev, p_curr, r_result, p_fraction); |
| 74 | } break; |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | Quaternion TransformInterpolator::_basis_to_quat_unchecked(const Basis &p_basis) { |
| 79 | Basis m = p_basis; |
| 80 | real_t trace = m.rows[0][0] + m.rows[1][1] + m.rows[2][2]; |
| 81 | real_t temp[4]; |
| 82 | |
| 83 | if (trace > 0.0) { |
| 84 | real_t s = Math::sqrt(trace + 1.0f); |
| 85 | temp[3] = (s * 0.5f); |
| 86 | s = 0.5f / s; |
| 87 | |
| 88 | temp[0] = ((m.rows[2][1] - m.rows[1][2]) * s); |
| 89 | temp[1] = ((m.rows[0][2] - m.rows[2][0]) * s); |
| 90 | temp[2] = ((m.rows[1][0] - m.rows[0][1]) * s); |
| 91 | } else { |
| 92 | int i = m.rows[0][0] < m.rows[1][1] |
| 93 | ? (m.rows[1][1] < m.rows[2][2] ? 2 : 1) |
| 94 | : (m.rows[0][0] < m.rows[2][2] ? 2 : 0); |
| 95 | int j = (i + 1) % 3; |
| 96 | int k = (i + 2) % 3; |
| 97 | |
| 98 | real_t s = Math::sqrt(m.rows[i][i] - m.rows[j][j] - m.rows[k][k] + 1.0f); |
| 99 | temp[i] = s * 0.5f; |
| 100 | s = 0.5f / s; |
| 101 | |
| 102 | temp[3] = (m.rows[k][j] - m.rows[j][k]) * s; |
| 103 | temp[j] = (m.rows[j][i] + m.rows[i][j]) * s; |
| 104 | temp[k] = (m.rows[k][i] + m.rows[i][k]) * s; |
| 105 | } |
| 106 | |
| 107 | return Quaternion(temp[0], temp[1], temp[2], temp[3]); |
| 108 | } |
| 109 | |
| 110 | Quaternion TransformInterpolator::_quat_slerp_unchecked(const Quaternion &p_from, const Quaternion &p_to, real_t p_fraction) { |
| 111 | Quaternion to1; |
| 112 | real_t omega, cosom, sinom, scale0, scale1; |
| 113 | |
| 114 | // Calculate cosine. |
| 115 | cosom = p_from.dot(p_to); |
| 116 | |
| 117 | // Adjust signs (if necessary) |
| 118 | if (cosom < 0.0f) { |
| 119 | cosom = -cosom; |
| 120 | to1.x = -p_to.x; |
| 121 | to1.y = -p_to.y; |
| 122 | to1.z = -p_to.z; |
| 123 | to1.w = -p_to.w; |
| 124 | } else { |
| 125 | to1.x = p_to.x; |
| 126 | to1.y = p_to.y; |
| 127 | to1.z = p_to.z; |
| 128 | to1.w = p_to.w; |
| 129 | } |
| 130 | |
| 131 | // Calculate coefficients. |
| 132 | |
| 133 | // This check could possibly be removed as we dealt with this |
| 134 | // case in the find_method() function, but is left for safety, it probably |
| 135 | // isn't a bottleneck. |
| 136 | if ((1.0f - cosom) > (real_t)CMP_EPSILON) { |
| 137 | // standard case (slerp) |
| 138 | omega = Math::acos(cosom); |
| 139 | sinom = Math::sin(omega); |
| 140 | scale0 = Math::sin((1.0f - p_fraction) * omega) / sinom; |
| 141 | scale1 = Math::sin(p_fraction * omega) / sinom; |
| 142 | } else { |
| 143 | // "from" and "to" quaternions are very close |
| 144 | // ... so we can do a linear interpolation |
| 145 | scale0 = 1.0f - p_fraction; |
| 146 | scale1 = p_fraction; |
| 147 | } |
| 148 | // Calculate final values. |
| 149 | return Quaternion( |
| 150 | scale0 * p_from.x + scale1 * to1.x, |
| 151 | scale0 * p_from.y + scale1 * to1.y, |
| 152 | scale0 * p_from.z + scale1 * to1.z, |
| 153 | scale0 * p_from.w + scale1 * to1.w); |
| 154 | } |
| 155 | |
| 156 | Basis TransformInterpolator::_basis_slerp_unchecked(Basis p_from, Basis p_to, real_t p_fraction) { |
| 157 | Quaternion from = _basis_to_quat_unchecked(p_from); |
| 158 | Quaternion to = _basis_to_quat_unchecked(p_to); |
| 159 | |
| 160 | Basis b(_quat_slerp_unchecked(from, to, p_fraction)); |
| 161 | return b; |
| 162 | } |
| 163 | |
| 164 | void TransformInterpolator::interpolate_basis_scaled_slerp(Basis p_prev, Basis p_curr, Basis &r_result, real_t p_fraction) { |
| 165 | // Normalize both and find lengths. |
| 166 | Vector3 lengths_prev = _basis_orthonormalize(p_prev); |
| 167 | Vector3 lengths_curr = _basis_orthonormalize(p_curr); |
| 168 | |
| 169 | r_result = _basis_slerp_unchecked(p_prev, p_curr, p_fraction); |
| 170 | |
| 171 | // Now the result is unit length basis, we need to scale. |
| 172 | Vector3 lengths_lerped = lengths_prev + ((lengths_curr - lengths_prev) * p_fraction); |
| 173 | |
| 174 | // Keep a note that the column / row order of the basis is weird, |
| 175 | // so keep an eye for bugs with this. |
| 176 | r_result[0] *= lengths_lerped; |
| 177 | r_result[1] *= lengths_lerped; |
| 178 | r_result[2] *= lengths_lerped; |
| 179 | } |
| 180 | |
| 181 | void TransformInterpolator::interpolate_basis_linear(const Basis &p_prev, const Basis &p_curr, Basis &r_result, real_t p_fraction) { |
| 182 | // Interpolate basis. |
| 183 | r_result = p_prev.lerp(p_curr, p_fraction); |
| 184 | |
| 185 | // It turns out we need to guard against zero scale basis. |
| 186 | // This is kind of silly, as we should probably fix the bugs elsewhere in Godot that can't deal with |
| 187 | // zero scale, but until that time... |
| 188 | for (int n = 0; n < 3; n++) { |
| 189 | Vector3 &axis = r_result[n]; |
| 190 | |
| 191 | // Not ok, this could cause errors due to bugs elsewhere, |
| 192 | // so we will bodge set this to a small value. |
| 193 | const real_t smallest = 0.0001f; |
| 194 | const real_t smallest_squared = smallest * smallest; |
| 195 | if (axis.length_squared() < smallest_squared) { |
| 196 | // Setting a different component to the smallest |
| 197 | // helps prevent the situation where all the axes are pointing in the same direction, |
| 198 | // which could be a problem for e.g. cross products... |
| 199 | axis[n] = smallest; |
| 200 | } |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | // Returns length. |
| 205 | real_t TransformInterpolator::_vec3_normalize(Vector3 &p_vec) { |
| 206 | real_t lengthsq = p_vec.length_squared(); |
| 207 | if (lengthsq == 0.0f) { |
| 208 | p_vec.x = p_vec.y = p_vec.z = 0.0f; |
| 209 | return 0.0f; |
| 210 | } |
| 211 | real_t length = Math::sqrt(lengthsq); |
| 212 | p_vec.x /= length; |
| 213 | p_vec.y /= length; |
| 214 | p_vec.z /= length; |
| 215 | return length; |
| 216 | } |
| 217 | |
| 218 | // Returns lengths. |
| 219 | Vector3 TransformInterpolator::_basis_orthonormalize(Basis &r_basis) { |
| 220 | // Gram-Schmidt Process. |
| 221 | |
| 222 | Vector3 x = r_basis.get_column(0); |
| 223 | Vector3 y = r_basis.get_column(1); |
| 224 | Vector3 z = r_basis.get_column(2); |
| 225 | |
| 226 | Vector3 lengths; |
| 227 | |
| 228 | lengths.x = _vec3_normalize(x); |
| 229 | y = (y - x * (x.dot(y))); |
| 230 | lengths.y = _vec3_normalize(y); |
| 231 | z = (z - x * (x.dot(z)) - y * (y.dot(z))); |
| 232 | lengths.z = _vec3_normalize(z); |
| 233 | |
| 234 | r_basis.set_column(0, x); |
| 235 | r_basis.set_column(1, y); |
| 236 | r_basis.set_column(2, z); |
| 237 | |
| 238 | return lengths; |
| 239 | } |
| 240 | |
| 241 | TransformInterpolator::Method TransformInterpolator::_test_basis(Basis p_basis, bool r_needed_normalize, Quaternion &r_quat) { |
| 242 | // Axis lengths. |
| 243 | Vector3 al = Vector3(p_basis.get_column(0).length_squared(), |
| 244 | p_basis.get_column(1).length_squared(), |
| 245 | p_basis.get_column(2).length_squared()); |
| 246 | |
| 247 | // Non unit scale? |
| 248 | if (r_needed_normalize || !_vec3_is_equal_approx(al, Vector3(1.0, 1.0, 1.0), (real_t)0.001f)) { |
| 249 | // If the basis is not normalized (at least approximately), it will fail the checks needed for slerp. |
| 250 | // So we try to detect a scaled (but not sheared) basis, which we *can* slerp by normalizing first, |
| 251 | // and lerping the scales separately. |
| 252 | |
| 253 | // If any of the axes are really small, it is unlikely to be a valid rotation, or is scaled too small to deal with float error. |
| 254 | const real_t sl_epsilon = 0.00001f; |
| 255 | if ((al.x < sl_epsilon) || |
| 256 | (al.y < sl_epsilon) || |
| 257 | (al.z < sl_epsilon)) { |
| 258 | return INTERP_LERP; |
| 259 | } |
| 260 | |
| 261 | // Normalize the basis. |
| 262 | Basis norm_basis = p_basis; |
| 263 | |
| 264 | al.x = Math::sqrt(al.x); |
| 265 | al.y = Math::sqrt(al.y); |
| 266 | al.z = Math::sqrt(al.z); |
| 267 | |
| 268 | norm_basis.set_column(0, norm_basis.get_column(0) / al.x); |
| 269 | norm_basis.set_column(1, norm_basis.get_column(1) / al.y); |
| 270 | norm_basis.set_column(2, norm_basis.get_column(2) / al.z); |
| 271 | |
| 272 | // This doesn't appear necessary, as the later checks will catch it. |
| 273 | // if (!_basis_is_orthogonal_any_scale(norm_basis)) { |
| 274 | // return INTERP_LERP; |
| 275 | // } |
| 276 | |
| 277 | p_basis = norm_basis; |
| 278 | |
| 279 | // Orthonormalize not necessary as normal normalization(!) works if the |
| 280 | // axes are orthonormal. |
| 281 | // p_basis.orthonormalize(); |
| 282 | |
| 283 | // If we needed to normalize one of the two bases, we will need to normalize both, |
| 284 | // regardless of whether the 2nd needs it, just to make sure it takes the path to return |
| 285 | // INTERP_SCALED_LERP on the 2nd call of _test_basis. |
| 286 | r_needed_normalize = true; |
| 287 | } |
| 288 | |
| 289 | // Apply less stringent tests than the built in slerp, the standard Godot slerp |
| 290 | // is too susceptible to float error to be useful. |
| 291 | real_t det = p_basis.determinant(); |
| 292 | if (!Math::is_equal_approx(det, 1, (real_t)0.01f)) { |
| 293 | return INTERP_LERP; |
| 294 | } |
| 295 | |
| 296 | if (!_basis_is_orthogonal(p_basis)) { |
| 297 | return INTERP_LERP; |
| 298 | } |
| 299 | |
| 300 | // TODO: This could possibly be less stringent too, check this. |
| 301 | r_quat = _basis_to_quat_unchecked(p_basis); |
| 302 | if (!r_quat.is_normalized()) { |
| 303 | return INTERP_LERP; |
| 304 | } |
| 305 | |
| 306 | return r_needed_normalize ? INTERP_SCALED_SLERP : INTERP_SLERP; |
| 307 | } |
| 308 | |
| 309 | // This check doesn't seem to be needed but is preserved in case of bugs. |
| 310 | bool TransformInterpolator::_basis_is_orthogonal_any_scale(const Basis &p_basis) { |
| 311 | Vector3 cross = p_basis.get_column(0).cross(p_basis.get_column(1)); |
| 312 | real_t l = _vec3_normalize(cross); |
| 313 | // Too small numbers, revert to lerp. |
| 314 | if (l < 0.001f) { |
| 315 | return false; |
| 316 | } |
| 317 | |
| 318 | const real_t epsilon = 0.9995f; |
| 319 | |
| 320 | real_t dot = cross.dot(p_basis.get_column(2)); |
| 321 | if (dot < epsilon) { |
| 322 | return false; |
| 323 | } |
| 324 | |
| 325 | cross = p_basis.get_column(1).cross(p_basis.get_column(2)); |
| 326 | l = _vec3_normalize(cross); |
| 327 | // Too small numbers, revert to lerp. |
| 328 | if (l < 0.001f) { |
| 329 | return false; |
| 330 | } |
| 331 | |
| 332 | dot = cross.dot(p_basis.get_column(0)); |
| 333 | if (dot < epsilon) { |
| 334 | return false; |
| 335 | } |
| 336 | |
| 337 | return true; |
| 338 | } |
| 339 | |
| 340 | bool TransformInterpolator::_basis_is_orthogonal(const Basis &p_basis, real_t p_epsilon) { |
| 341 | Basis identity; |
| 342 | Basis m = p_basis * p_basis.transposed(); |
| 343 | |
| 344 | // Less stringent tests than the standard Godot slerp. |
| 345 | if (!_vec3_is_equal_approx(m[0], identity[0], p_epsilon) || !_vec3_is_equal_approx(m[1], identity[1], p_epsilon) || !_vec3_is_equal_approx(m[2], identity[2], p_epsilon)) { |
| 346 | return false; |
| 347 | } |
| 348 | return true; |
| 349 | } |
| 350 | |
| 351 | real_t TransformInterpolator::checksum_transform_3d(const Transform3D &p_transform) { |
| 352 | // just a really basic checksum, this can probably be improved |
| 353 | real_t sum = _vec3_sum(p_transform.origin); |
| 354 | sum -= _vec3_sum(p_transform.basis.rows[0]); |
| 355 | sum += _vec3_sum(p_transform.basis.rows[1]); |
| 356 | sum -= _vec3_sum(p_transform.basis.rows[2]); |
| 357 | return sum; |
| 358 | } |
| 359 | |
| 360 | TransformInterpolator::Method TransformInterpolator::find_method(const Basis &p_a, const Basis &p_b) { |
| 361 | bool needed_normalize = false; |
| 362 | |
| 363 | Quaternion q0; |
| 364 | Method method = _test_basis(p_a, needed_normalize, q0); |
| 365 | if (method == INTERP_LERP) { |
| 366 | return method; |
| 367 | } |
| 368 | |
| 369 | Quaternion q1; |
| 370 | method = _test_basis(p_b, needed_normalize, q1); |
| 371 | if (method == INTERP_LERP) { |
| 372 | return method; |
| 373 | } |
| 374 | |
| 375 | // Are they close together? |
| 376 | // Apply the same test that will revert to lerp as is present in the slerp routine. |
| 377 | // Calculate cosine. |
| 378 | real_t cosom = Math::abs(q0.dot(q1)); |
| 379 | if ((1.0f - cosom) <= (real_t)CMP_EPSILON) { |
| 380 | return INTERP_LERP; |
| 381 | } |
| 382 | |
| 383 | return method; |
| 384 | } |