Debian Codegraph @

core/math/transform_interpolator.h

raw symbols SCIP
1 /**************************************************************************/
2 /* transform_interpolator.h */
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 #pragma once
32
33 #include "core/math/math_defs.h"
34 #include "core/math/vector3.h"
35
36 // Keep all the functions for fixed timestep interpolation together.
37 // There are two stages involved:
38 // Finding a method, for determining the interpolation method between two
39 // keyframes (which are physics ticks).
40 // And applying that pre-determined method.
41
42 // Pre-determining the method makes sense because it is expensive and often
43 // several frames may occur between each physics tick, which will make it cheaper
44 // than performing every frame.
45
46 struct Transform2D;
47 struct Transform3D;
48 struct Basis;
49 struct Quaternion;
50
51 class TransformInterpolator {
52 public:
53 enum Method {
54 INTERP_LERP,
55 INTERP_SLERP,
56 INTERP_SCALED_SLERP,
57 };
58
59 private:
60 _FORCE_INLINE_ static bool _sign(real_t p_val) { return p_val >= 0; }
61 static real_t _vec3_sum(const Vector3 &p_pt) { return p_pt.x + p_pt.y + p_pt.z; }
62 static real_t _vec3_normalize(Vector3 &p_vec);
63 _FORCE_INLINE_ static bool _vec3_is_equal_approx(const Vector3 &p_a, const Vector3 &p_b, real_t p_tolerance) {
64 return Math::is_equal_approx(p_a.x, p_b.x, p_tolerance) && Math::is_equal_approx(p_a.y, p_b.y, p_tolerance) && Math::is_equal_approx(p_a.z, p_b.z, p_tolerance);
65 }
66 static Vector3 _basis_orthonormalize(Basis &r_basis);
67 static Method _test_basis(Basis p_basis, bool r_needed_normalize, Quaternion &r_quat);
68 static Basis _basis_slerp_unchecked(Basis p_from, Basis p_to, real_t p_fraction);
69 static Quaternion _quat_slerp_unchecked(const Quaternion &p_from, const Quaternion &p_to, real_t p_fraction);
70 static Quaternion _basis_to_quat_unchecked(const Basis &p_basis);
71 static bool _basis_is_orthogonal(const Basis &p_basis, real_t p_epsilon = 0.01f);
72 static bool _basis_is_orthogonal_any_scale(const Basis &p_basis);
73
74 static void interpolate_basis_linear(const Basis &p_prev, const Basis &p_curr, Basis &r_result, real_t p_fraction);
75 static void interpolate_basis_scaled_slerp(Basis p_prev, Basis p_curr, Basis &r_result, real_t p_fraction);
76
77 public:
78 static void interpolate_transform_2d(const Transform2D &p_prev, const Transform2D &p_curr, Transform2D &r_result, real_t p_fraction);
79
80 // Generic functions, use when you don't know what method should be used, e.g. from GDScript.
81 // These will be slower.
82 static void interpolate_transform_3d(const Transform3D &p_prev, const Transform3D &p_curr, Transform3D &r_result, real_t p_fraction);
83 static void interpolate_basis(const Basis &p_prev, const Basis &p_curr, Basis &r_result, real_t p_fraction);
84
85 // Optimized function when you know ahead of time the method.
86 static void interpolate_transform_3d_via_method(const Transform3D &p_prev, const Transform3D &p_curr, Transform3D &r_result, real_t p_fraction, Method p_method);
87 static void interpolate_basis_via_method(const Basis &p_prev, const Basis &p_curr, Basis &r_result, real_t p_fraction, Method p_method);
88
89 static real_t checksum_transform_3d(const Transform3D &p_transform);
90
91 static Method find_method(const Basis &p_a, const Basis &p_b);
92 };