Debian Codegraph @

scene/3d/spline_ik_3d.h

raw symbols SCIP
1 /**************************************************************************/
2 /* spline_ik_3d.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 "scene/3d/chain_ik_3d.h"
34 #include "scene/3d/path_3d.h"
35
36 class SplineIK3D : public ChainIK3D {
37 GDCLASS(SplineIK3D, ChainIK3D);
38
39 public:
40 struct SplineIK3DSetting : public ChainIK3DSetting {
41 NodePath path_3d;
42 bool tilt_enabled = true;
43 int tilt_fade_in = 1;
44 int tilt_fade_out = 1;
45
46 LocalVector<double> chain_length_accum;
47 LocalVector<double> twists;
48
49 // Find the nearest point.
50 int32_t find_nearest_point(const Vector3 &p_origin, double p_length_sq, const PackedVector3Array &p_points, bool p_is_path_closed, uint32_t p_first, double *r_ret) {
51 ERR_FAIL_COND_V(p_first >= p_points.size(), p_points.size() - 1);
52 uint32_t i = p_first;
53 if (p_is_path_closed) {
54 while (true) {
55 if (p_origin.distance_squared_to(p_points[i]) >= p_length_sq) {
56 break;
57 }
58 i++;
59 i = Math::posmod(i, p_points.size());
60 if (i == p_first) {
61 i = p_points.size();
62 break; // Can't found, use last.
63 }
64 }
65 } else {
66 while (i < p_points.size()) {
67 if (p_origin.distance_squared_to(p_points[i]) >= p_length_sq) {
68 break;
69 }
70 i++;
71 }
72 }
73 if (i == 0) {
74 *r_ret = 0.0;
75 return p_first;
76 } else if (i >= p_points.size()) {
77 *r_ret = 1.0;
78 return i;
79 }
80 *r_ret = Math::inverse_lerp((double)p_origin.distance_squared_to(p_points[i - 1]), (double)p_origin.distance_squared_to(p_points[i]), p_length_sq);
81 return i - 1;
82 }
83
84 // Make rotation as bone pose from chain coordinates.
85 void cache_current_joint_rotations(Skeleton3D *p_skeleton, bool p_use_tilt = false) {
86 Transform3D parent_gpose_tr;
87 int parent = p_skeleton->get_bone_parent(root_bone.bone);
88 if (parent >= 0) {
89 parent_gpose_tr = p_skeleton->get_bone_global_pose(parent);
90 }
91 Quaternion parent_gpose = parent_gpose_tr.basis.get_rotation_quaternion();
92
93 if (p_use_tilt) {
94 double parent_twist = 0.0;
95 for (uint32_t i = 0; i < joints.size(); i++) {
96 int HEAD = i;
97 IKModifier3DSolverInfo *solver_info = solver_info_list[HEAD];
98 if (!solver_info) {
99 continue;
100 }
101 solver_info->current_lrest = p_skeleton->get_bone_pose(joints[HEAD].bone).basis.get_rotation_quaternion();
102 solver_info->current_grest = parent_gpose * solver_info->current_lrest;
103 solver_info->current_grest.normalize();
104 Vector3 from = solver_info->forward_vector;
105 Vector3 to = solver_info->current_grest.xform_inv(solver_info->current_vector).normalized();
106
107 Basis b = get_swing(Quaternion(from, to), from);
108 b.rotate_local(from, twists[HEAD] - parent_twist);
109 parent_twist = twists[HEAD];
110 solver_info->current_lpose = solver_info->current_lrest * b.get_rotation_quaternion();
111
112 solver_info->current_gpose = parent_gpose * solver_info->current_lpose;
113 solver_info->current_gpose.normalize();
114 parent_gpose = solver_info->current_gpose;
115 }
116 } else {
117 for (uint32_t i = 0; i < joints.size(); i++) {
118 int HEAD = i;
119 IKModifier3DSolverInfo *solver_info = solver_info_list[HEAD];
120 if (!solver_info) {
121 continue;
122 }
123 solver_info->current_lrest = p_skeleton->get_bone_pose(joints[HEAD].bone).basis.get_rotation_quaternion();
124 solver_info->current_grest = parent_gpose * solver_info->current_lrest;
125 solver_info->current_grest.normalize();
126 Vector3 from = solver_info->forward_vector;
127 Vector3 to = solver_info->current_grest.xform_inv(solver_info->current_vector).normalized();
128
129 solver_info->current_lpose = solver_info->current_lrest * get_swing(Quaternion(from, to), from);
130
131 solver_info->current_gpose = parent_gpose * solver_info->current_lpose;
132 solver_info->current_gpose.normalize();
133 parent_gpose = solver_info->current_gpose;
134 }
135 }
136
137 // To update positions in preprocess of _process_joints().
138 cache_current_vectors(p_skeleton);
139 }
140 };
141
142 protected:
143 LocalVector<SplineIK3DSetting *> sp_settings; // For caching.
144
145 bool _get(const StringName &p_path, Variant &r_ret) const;
146 bool _set(const StringName &p_path, const Variant &p_value);
147 void _get_property_list(List<PropertyInfo> *p_list) const;
148 void _validate_dynamic_prop(PropertyInfo &p_property) const;
149
150 static void _bind_methods();
151
152 virtual void _init_joints(Skeleton3D *p_skeleton, int p_index) override;
153 virtual void _make_simulation_dirty(int p_index) override;
154 virtual void _update_bone_axis(Skeleton3D *p_skeleton, int p_index) override;
155
156 virtual void _process_ik(Skeleton3D *p_skeleton, double p_delta) override;
157 void _process_joints(double p_delta, Skeleton3D *p_skeleton, SplineIK3DSetting *p_setting, Ref<Curve3D> p_curve, const Transform3D &p_curve_space);
158
159 virtual void _set_joint_count(int p_index, int p_count) override;
160
161 public:
162 virtual PackedStringArray get_configuration_warnings() const override;
163 virtual void set_setting_count(int p_count) override {
164 _set_setting_count<SplineIK3DSetting>(p_count);
165 sp_settings = _cast_settings<SplineIK3DSetting>();
166 chain_settings = _cast_settings<ChainIK3DSetting>(); // Don't forget to sync super class settings.
167 }
168 virtual void clear_settings() override {
169 _set_setting_count<SplineIK3DSetting>(0);
170 sp_settings.clear();
171 chain_settings.clear(); // Don't forget to sync super class settings.
172 }
173
174 // Setting.
175 void set_path_3d(int p_index, const NodePath &p_path_3d);
176 NodePath get_path_3d(int p_index) const;
177 void set_tilt_enabled(int p_index, bool p_enabled);
178 bool is_tilt_enabled(int p_index) const;
179 void set_tilt_fade_in(int p_index, int p_size);
180 int get_tilt_fade_in(int p_index) const;
181 void set_tilt_fade_out(int p_index, int p_size);
182 int get_tilt_fade_out(int p_index) const;
183
184 // Helper.
185 double get_bezier_arc_length();
186
187 #ifdef TOOLS_ENABLED
188 virtual Vector3 get_bone_vector(int p_index, int p_joint) const override;
189 #endif // TOOLS_ENABLED
190
191 ~SplineIK3D();
192 };