Cleanup: remove redundant doxygen \file argument
[blender.git] / source / blender / depsgraph / intern / builder / deg_builder_nodes_rig.cc
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 2013 Blender Foundation.
17  * All rights reserved.
18  */
19
20 /** \file \ingroup depsgraph
21  *
22  * Methods for constructing depsgraph's nodes
23  */
24
25 #include "intern/builder/deg_builder_nodes.h"
26
27 #include <stdio.h>
28 #include <stdlib.h>
29
30 #include "MEM_guardedalloc.h"
31
32 #include "BLI_utildefines.h"
33 #include "BLI_blenlib.h"
34 #include "BLI_string.h"
35
36 extern "C" {
37 #include "DNA_anim_types.h"
38 #include "DNA_armature_types.h"
39 #include "DNA_constraint_types.h"
40 #include "DNA_object_types.h"
41 #include "DNA_scene_types.h"
42
43 #include "BKE_action.h"
44 #include "BKE_armature.h"
45 #include "BKE_constraint.h"
46 } /* extern "C" */
47
48 #include "DEG_depsgraph.h"
49 #include "DEG_depsgraph_build.h"
50
51 #include "intern/builder/deg_builder.h"
52 #include "intern/eval/deg_eval_copy_on_write.h"
53 #include "intern/node/deg_node.h"
54 #include "intern/node/deg_node_component.h"
55 #include "intern/node/deg_node_operation.h"
56 #include "intern/depsgraph_type.h"
57
58 namespace DEG {
59
60 void DepsgraphNodeBuilder::build_pose_constraints(
61         Object *object,
62         bPoseChannel *pchan,
63         int pchan_index,
64         bool is_object_visible)
65 {
66         /* Pull indirect dependencies via constraints. */
67         BuilderWalkUserData data;
68         data.builder = this;
69         data.is_parent_visible = is_object_visible;
70         BKE_constraints_id_loop(&pchan->constraints, constraint_walk, &data);
71         /* Create node for constraint stack. */
72         add_operation_node(&object->id,
73                            NodeType::BONE,
74                            pchan->name,
75                            OperationCode::BONE_CONSTRAINTS,
76                            function_bind(BKE_pose_constraints_evaluate,
77                                          _1,
78                                          get_cow_datablock(scene_),
79                                          get_cow_datablock(object),
80                                          pchan_index));
81 }
82
83 /* IK Solver Eval Steps */
84 void DepsgraphNodeBuilder::build_ik_pose(Object *object,
85                                          bPoseChannel *pchan,
86                                          bConstraint *con)
87 {
88         bKinematicConstraint *data = (bKinematicConstraint *)con->data;
89
90         /* Find the chain's root. */
91         bPoseChannel *rootchan = BKE_armature_ik_solver_find_root(pchan, data);
92         if (rootchan == NULL) {
93                 return;
94         }
95
96         if (has_operation_node(&object->id, NodeType::EVAL_POSE, rootchan->name,
97                                OperationCode::POSE_IK_SOLVER))
98         {
99                 return;
100         }
101
102         int rootchan_index = BLI_findindex(&object->pose->chanbase, rootchan);
103         BLI_assert(rootchan_index != -1);
104         /* Operation node for evaluating/running IK Solver. */
105         add_operation_node(&object->id,
106                            NodeType::EVAL_POSE,
107                            rootchan->name,
108                            OperationCode::POSE_IK_SOLVER,
109                            function_bind(BKE_pose_iktree_evaluate,
110                                          _1,
111                                          get_cow_datablock(scene_),
112                                          get_cow_datablock(object),
113                                          rootchan_index));
114 }
115
116 /* Spline IK Eval Steps */
117 void DepsgraphNodeBuilder::build_splineik_pose(Object *object,
118                                                bPoseChannel *pchan,
119                                                bConstraint *con)
120 {
121         bSplineIKConstraint *data = (bSplineIKConstraint *)con->data;
122
123         /* Find the chain's root. */
124         bPoseChannel *rootchan = BKE_armature_splineik_solver_find_root(pchan, data);
125
126         /* Operation node for evaluating/running Spline IK Solver.
127          * Store the "root bone" of this chain in the solver, so it knows where to
128          * start. */
129         int rootchan_index = BLI_findindex(&object->pose->chanbase, rootchan);
130         BLI_assert(rootchan_index != -1);
131         add_operation_node(&object->id,
132                            NodeType::EVAL_POSE,
133                            rootchan->name,
134                            OperationCode::POSE_SPLINE_IK_SOLVER,
135                            function_bind(BKE_pose_splineik_evaluate,
136                                          _1,
137                                          get_cow_datablock(scene_),
138                                          get_cow_datablock(object),
139                                          rootchan_index));
140 }
141
142 /* Pose/Armature Bones Graph */
143 void DepsgraphNodeBuilder::build_rig(Object *object, bool is_object_visible)
144 {
145         bArmature *armature = (bArmature *)object->data;
146         Scene *scene_cow = get_cow_datablock(scene_);
147         Object *object_cow = get_cow_datablock(object);
148         OperationNode *op_node;
149         /* Animation and/or drivers linking posebones to base-armature used to
150          * define them.
151          *
152          * NOTE: AnimData here is really used to control animated deform properties,
153          *       which ideally should be able to be unique across different
154          *       instances. Eventually, we need some type of proxy/isolation
155          *       mechanism in-between here to ensure that we can use same rig
156          *       multiple times in same scene. */
157         /* Armature. */
158         build_armature(armature);
159         /* Rebuild pose if not up to date. */
160         if (object->pose == NULL || (object->pose->flag & POSE_RECALC)) {
161                 /* By definition, no need to tag depsgraph as dirty from here, so we can pass NULL bmain. */
162                 BKE_pose_rebuild(NULL, object, armature, true);
163         }
164         /* Speed optimization for animation lookups. */
165         if (object->pose != NULL) {
166                 BKE_pose_channels_hash_make(object->pose);
167                 if (object->pose->flag & POSE_CONSTRAINTS_NEED_UPDATE_FLAGS) {
168                         BKE_pose_update_constraint_flags(object->pose);
169                 }
170         }
171         /**
172          * Pose Rig Graph
173          * ==============
174          *
175          * Pose Component:
176          * - Mainly used for referencing Bone components.
177          * - This is where the evaluation operations for init/exec/cleanup
178          *   (ik) solvers live, and are later hooked up (so that they can be
179          *   interleaved during runtime) with bone-operations they depend on/affect.
180          * - init_pose_eval() and cleanup_pose_eval() are absolute first and last
181          *   steps of pose eval process. ALL bone operations must be performed
182          *   between these two...
183          *
184          * Bone Component:
185          * - Used for representing each bone within the rig
186          * - Acts to encapsulate the evaluation operations (base matrix + parenting,
187          *   and constraint stack) so that they can be easily found.
188          * - Everything else which depends on bone-results hook up to the component
189          *   only so that we can redirect those to point at either the the post-IK/
190          *   post-constraint/post-matrix steps, as needed. */
191         /* Pose eval context. */
192         op_node = add_operation_node(&object->id,
193                                      NodeType::EVAL_POSE,
194                                      OperationCode::POSE_INIT,
195                                      function_bind(BKE_pose_eval_init,
196                                                    _1,
197                                                    scene_cow,
198                                                    object_cow));
199         op_node->set_as_entry();
200
201         op_node = add_operation_node(&object->id,
202                                      NodeType::EVAL_POSE,
203                                      OperationCode::POSE_INIT_IK,
204                                      function_bind(BKE_pose_eval_init_ik,
205                                                    _1,
206                                                    scene_cow,
207                                                    object_cow));
208
209         add_operation_node(&object->id,
210                            NodeType::EVAL_POSE,
211                            OperationCode::POSE_CLEANUP,
212                            function_bind(BKE_pose_eval_cleanup,
213                                          _1,
214                                          scene_cow,
215                                          object_cow));
216
217         op_node = add_operation_node(&object->id,
218                                      NodeType::EVAL_POSE,
219                                      OperationCode::POSE_DONE,
220                                      function_bind(BKE_pose_eval_done,
221                                                    _1,
222                                                    object_cow));
223         op_node->set_as_exit();
224         /* Bones. */
225         int pchan_index = 0;
226         LISTBASE_FOREACH (bPoseChannel *, pchan, &object->pose->chanbase) {
227                 /* Node for bone evaluation. */
228                 op_node = add_operation_node(&object->id,
229                                              NodeType::BONE,
230                                              pchan->name,
231                                              OperationCode::BONE_LOCAL);
232                 op_node->set_as_entry();
233
234                 add_operation_node(&object->id,
235                                    NodeType::BONE,
236                                    pchan->name,
237                                    OperationCode::BONE_POSE_PARENT,
238                                    function_bind(BKE_pose_eval_bone, _1,
239                                                  scene_cow,
240                                                  object_cow,
241                                                  pchan_index));
242
243                 /* NOTE: Dedicated noop for easier relationship construction. */
244                 add_operation_node(&object->id,
245                                    NodeType::BONE,
246                                    pchan->name,
247                                    OperationCode::BONE_READY);
248
249                 op_node = add_operation_node(&object->id,
250                                              NodeType::BONE,
251                                              pchan->name,
252                                              OperationCode::BONE_DONE,
253                                              function_bind(BKE_pose_bone_done,
254                                                            _1,
255                                                            object_cow,
256                                                            pchan_index));
257
258                 /* B-Bone shape computation - the real last step if present. */
259                 if (pchan->bone != NULL && pchan->bone->segments > 1) {
260                         op_node = add_operation_node(&object->id,
261                                                      NodeType::BONE,
262                                                      pchan->name,
263                                                      OperationCode::BONE_SEGMENTS,
264                                                      function_bind(BKE_pose_eval_bbone_segments, _1,
265                                                                    object_cow,
266                                                                    pchan_index));
267                 }
268
269                 op_node->set_as_exit();
270
271                 /* Custom properties. */
272                 if (pchan->prop != NULL) {
273                         add_operation_node(&object->id,
274                                            NodeType::PARAMETERS,
275                                            OperationCode::PARAMETERS_EVAL,
276                                            NULL,
277                                            pchan->name);
278                 }
279                 /* Build constraints. */
280                 if (pchan->constraints.first != NULL) {
281                         build_pose_constraints(
282                                 object, pchan, pchan_index, is_object_visible);
283                 }
284                 /**
285                  * IK Solvers.
286                  *
287                  * - These require separate processing steps are pose-level
288                  *   to be executed between chains of bones (i.e. once the
289                  *   base transforms of a bunch of bones is done)
290                  *
291                  * Unsolved Issues:
292                  * - Care is needed to ensure that multi-headed trees work out the same
293                  *   as in ik-tree building
294                  * - Animated chain-lengths are a problem. */
295                 LISTBASE_FOREACH (bConstraint *, con, &pchan->constraints) {
296                         switch (con->type) {
297                                 case CONSTRAINT_TYPE_KINEMATIC:
298                                         build_ik_pose(object, pchan, con);
299                                         break;
300
301                                 case CONSTRAINT_TYPE_SPLINEIK:
302                                         build_splineik_pose(object, pchan, con);
303                                         break;
304
305                                 default:
306                                         break;
307                         }
308                 }
309                 /* Custom shape. */
310                 if (pchan->custom != NULL) {
311                         /* TODO(sergey): Use own visibility. */
312                         build_object(
313                                 -1,
314                                 pchan->custom,
315                                 DEG_ID_LINKED_INDIRECTLY,
316                                 is_object_visible);
317                 }
318                 pchan_index++;
319         }
320 }
321
322 void DepsgraphNodeBuilder::build_proxy_rig(Object *object)
323 {
324         bArmature *armature = (bArmature *)object->data;
325         OperationNode *op_node;
326         Object *object_cow = get_cow_datablock(object);
327         /* Sanity check. */
328         BLI_assert(object->pose != NULL);
329         /* Armature. */
330         build_armature(armature);
331         /* speed optimization for animation lookups */
332         BKE_pose_channels_hash_make(object->pose);
333         if (object->pose->flag & POSE_CONSTRAINTS_NEED_UPDATE_FLAGS) {
334                 BKE_pose_update_constraint_flags(object->pose);
335         }
336         op_node = add_operation_node(&object->id,
337                                      NodeType::EVAL_POSE,
338                                      OperationCode::POSE_INIT,
339                                      function_bind(BKE_pose_eval_proxy_init,
340                                                    _1,
341                                                    object_cow));
342         op_node->set_as_entry();
343
344         int pchan_index = 0;
345         LISTBASE_FOREACH (bPoseChannel *, pchan, &object->pose->chanbase) {
346                 op_node = add_operation_node(&object->id,
347                                              NodeType::BONE,
348                                              pchan->name,
349                                              OperationCode::BONE_LOCAL);
350                 op_node->set_as_entry();
351                 /* Bone is ready for solvers. */
352                 add_operation_node(&object->id,
353                                    NodeType::BONE,
354                                    pchan->name,
355                                    OperationCode::BONE_READY);
356                 /* Bone is fully evaluated. */
357                 op_node = add_operation_node(
358                         &object->id,
359                         NodeType::BONE,
360                         pchan->name,
361                         OperationCode::BONE_DONE,
362                         function_bind(BKE_pose_eval_proxy_copy_bone,
363                                       _1,
364                                       object_cow,
365                                       pchan_index));
366                 op_node->set_as_exit();
367
368                 /* Custom properties. */
369                 if (pchan->prop != NULL) {
370                         add_operation_node(&object->id,
371                                            NodeType::PARAMETERS,
372                                            OperationCode::PARAMETERS_EVAL,
373                                            NULL,
374                                            pchan->name);
375                 }
376
377                 pchan_index++;
378         }
379         op_node = add_operation_node(&object->id,
380                                      NodeType::EVAL_POSE,
381                                      OperationCode::POSE_CLEANUP,
382                                      function_bind(BKE_pose_eval_proxy_cleanup,
383                                                    _1,
384                                                    object_cow));
385         op_node = add_operation_node(&object->id,
386                                      NodeType::EVAL_POSE,
387                                      OperationCode::POSE_DONE,
388                                      function_bind(BKE_pose_eval_proxy_done,
389                                                    _1,
390                                                    object_cow));
391         op_node->set_as_exit();
392 }
393
394 }  // namespace DEG