From d8dbd49a2f23b7637f05fc058f39bdf6ab706624 Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Fri, 31 May 2019 22:51:19 +1000 Subject: Cleanup: style, use braces in source/ Automated using clang-tidy. --- source/blender/ikplugin/intern/ikplugin_api.c | 24 +- source/blender/ikplugin/intern/iksolver_plugin.c | 146 ++++++++---- source/blender/ikplugin/intern/itasc_plugin.cpp | 274 +++++++++++++++-------- 3 files changed, 296 insertions(+), 148 deletions(-) (limited to 'source/blender/ikplugin') diff --git a/source/blender/ikplugin/intern/ikplugin_api.c b/source/blender/ikplugin/intern/ikplugin_api.c index 3a851df7058..f473bc02ce1 100644 --- a/source/blender/ikplugin/intern/ikplugin_api.c +++ b/source/blender/ikplugin/intern/ikplugin_api.c @@ -70,8 +70,9 @@ static IKPlugin ikplugin_tab[] = { static IKPlugin *get_plugin(bPose *pose) { if (!pose || pose->iksolver < 0 || - pose->iksolver >= ((sizeof(ikplugin_tab) / sizeof(IKPlugin)) - 1)) + pose->iksolver >= ((sizeof(ikplugin_tab) / sizeof(IKPlugin)) - 1)) { return NULL; + } return &ikplugin_tab[pose->iksolver]; } @@ -83,8 +84,9 @@ void BIK_initialize_tree(struct Depsgraph *depsgraph, Scene *scene, Object *ob, { IKPlugin *plugin = get_plugin(ob->pose); - if (plugin && plugin->initialize_tree_func) + if (plugin && plugin->initialize_tree_func) { plugin->initialize_tree_func(depsgraph, scene, ob, ctime); + } } void BIK_execute_tree( @@ -92,46 +94,52 @@ void BIK_execute_tree( { IKPlugin *plugin = get_plugin(ob->pose); - if (plugin && plugin->execute_tree_func) + if (plugin && plugin->execute_tree_func) { plugin->execute_tree_func(depsgraph, scene, ob, pchan, ctime); + } } void BIK_release_tree(struct Scene *scene, Object *ob, float ctime) { IKPlugin *plugin = get_plugin(ob->pose); - if (plugin && plugin->release_tree_func) + if (plugin && plugin->release_tree_func) { plugin->release_tree_func(scene, ob, ctime); + } } void BIK_clear_data(struct bPose *pose) { IKPlugin *plugin = get_plugin(pose); - if (plugin && plugin->remove_armature_func) + if (plugin && plugin->remove_armature_func) { plugin->remove_armature_func(pose); + } } void BIK_clear_cache(struct bPose *pose) { IKPlugin *plugin = get_plugin(pose); - if (plugin && plugin->clear_cache) + if (plugin && plugin->clear_cache) { plugin->clear_cache(pose); + } } void BIK_update_param(struct bPose *pose) { IKPlugin *plugin = get_plugin(pose); - if (plugin && plugin->update_param) + if (plugin && plugin->update_param) { plugin->update_param(pose); + } } void BIK_test_constraint(struct Object *ob, struct bConstraint *cons) { IKPlugin *plugin = get_plugin(ob->pose); - if (plugin && plugin->test_constraint) + if (plugin && plugin->test_constraint) { plugin->test_constraint(ob, cons); + } } diff --git a/source/blender/ikplugin/intern/iksolver_plugin.c b/source/blender/ikplugin/intern/iksolver_plugin.c index 9ae8855bc85..3c2642c542d 100644 --- a/source/blender/ikplugin/intern/iksolver_plugin.c +++ b/source/blender/ikplugin/intern/iksolver_plugin.c @@ -62,22 +62,28 @@ static void initialize_posetree(struct Object *UNUSED(ob), bPoseChannel *pchan_t for (con = pchan_tip->constraints.first; con; con = con->next) { if (con->type == CONSTRAINT_TYPE_KINEMATIC) { data = (bKinematicConstraint *)con->data; - if (data->flag & CONSTRAINT_IK_AUTO) + if (data->flag & CONSTRAINT_IK_AUTO) { break; - if (data->tar == NULL) + } + if (data->tar == NULL) { continue; - if (data->tar->type == OB_ARMATURE && data->subtarget[0] == 0) + } + if (data->tar->type == OB_ARMATURE && data->subtarget[0] == 0) { continue; - if ((con->flag & (CONSTRAINT_DISABLE | CONSTRAINT_OFF)) == 0 && (con->enforce != 0.0f)) + } + if ((con->flag & (CONSTRAINT_DISABLE | CONSTRAINT_OFF)) == 0 && (con->enforce != 0.0f)) { break; + } } } - if (con == NULL) + if (con == NULL) { return; + } /* exclude tip from chain? */ - if (!(data->flag & CONSTRAINT_IK_TIP)) + if (!(data->flag & CONSTRAINT_IK_TIP)) { pchan_tip = pchan_tip->parent; + } /* Find the chain's root & count the segments needed */ for (curchan = pchan_tip; curchan; curchan = curchan->parent) { @@ -87,11 +93,13 @@ static void initialize_posetree(struct Object *UNUSED(ob), bPoseChannel *pchan_t chanlist[segcount] = curchan; segcount++; - if (segcount == data->rootbone || segcount > 255) + if (segcount == data->rootbone || segcount > 255) { break; // 255 is weak + } } - if (!segcount) + if (!segcount) { return; + } /* setup the chain data */ @@ -99,13 +107,16 @@ static void initialize_posetree(struct Object *UNUSED(ob), bPoseChannel *pchan_t for (tree = pchan_root->iktree.first; tree; tree = tree->next) { for (target = tree->targets.first; target; target = target->next) { curchan = tree->pchan[target->tip]; - if (curchan->flag & POSE_CHAIN) + if (curchan->flag & POSE_CHAIN) { curchan->flag &= ~POSE_CHAIN; - else + } + else { break; + } } - if (target) + if (target) { break; + } } /* create a target */ @@ -143,26 +154,32 @@ static void initialize_posetree(struct Object *UNUSED(ob), bPoseChannel *pchan_t a = t = 0; while (a < size && t < tree->totchannel) { /* locate first matching channel */ - for (; t < tree->totchannel && tree->pchan[t] != chanlist[segcount - a - 1]; t++) + for (; t < tree->totchannel && tree->pchan[t] != chanlist[segcount - a - 1]; t++) { ; - if (t >= tree->totchannel) + } + if (t >= tree->totchannel) { break; + } for (; a < size && t < tree->totchannel && tree->pchan[t] == chanlist[segcount - a - 1]; - a++, t++) + a++, t++) { ; + } } segcount = segcount - a; target->tip = tree->totchannel + segcount - 1; if (segcount > 0) { - for (parent = a - 1; parent < tree->totchannel; parent++) - if (tree->pchan[parent] == chanlist[segcount - 1]->parent) + for (parent = a - 1; parent < tree->totchannel; parent++) { + if (tree->pchan[parent] == chanlist[segcount - 1]->parent) { break; + } + } /* shouldn't happen, but could with dependency cycles */ - if (parent == tree->totchannel) + if (parent == tree->totchannel) { parent = a - 1; + } /* resize array */ newsize = tree->totchannel + segcount; @@ -220,10 +237,12 @@ static void where_is_ik_bone(bPoseChannel *pchan, copy_m4_m3(ikmat, ik_mat); - if (pchan->parent) + if (pchan->parent) { mul_m4_m4m4(pchan->pose_mat, pchan->parent->pose_mat, pchan->chan_mat); - else + } + else { copy_m4_m4(pchan->pose_mat, pchan->chan_mat); + } #ifdef USE_NONUNIFORM_SCALE /* apply IK mat, but as if the bones have uniform scale since the IK solver @@ -278,8 +297,9 @@ static void execute_posetree(struct Depsgraph *depsgraph, bKinematicConstraint *data, *poleangledata = NULL; Bone *bone; - if (tree->totchannel == 0) + if (tree->totchannel == 0) { return; + } iktree = MEM_mallocN(sizeof(void *) * tree->totchannel, "ik tree"); @@ -290,12 +310,15 @@ static void execute_posetree(struct Depsgraph *depsgraph, /* set DoF flag */ flag = 0; - if (!(pchan->ikflag & BONE_IK_NO_XDOF) && !(pchan->ikflag & BONE_IK_NO_XDOF_TEMP)) + if (!(pchan->ikflag & BONE_IK_NO_XDOF) && !(pchan->ikflag & BONE_IK_NO_XDOF_TEMP)) { flag |= IK_XDOF; - if (!(pchan->ikflag & BONE_IK_NO_YDOF) && !(pchan->ikflag & BONE_IK_NO_YDOF_TEMP)) + } + if (!(pchan->ikflag & BONE_IK_NO_YDOF) && !(pchan->ikflag & BONE_IK_NO_YDOF_TEMP)) { flag |= IK_YDOF; - if (!(pchan->ikflag & BONE_IK_NO_ZDOF) && !(pchan->ikflag & BONE_IK_NO_ZDOF_TEMP)) + } + if (!(pchan->ikflag & BONE_IK_NO_ZDOF) && !(pchan->ikflag & BONE_IK_NO_ZDOF_TEMP)) { flag |= IK_ZDOF; + } if (tree->stretch && (pchan->ikstretch > 0.0f)) { flag |= IK_TRANS_YDOF; @@ -305,10 +328,12 @@ static void execute_posetree(struct Depsgraph *depsgraph, seg = iktree[a] = IK_CreateSegment(flag); /* find parent */ - if (a == 0) + if (a == 0) { parent = NULL; - else + } + else { parent = iktree[tree->parent[a]]; + } IK_SetParent(seg, parent); @@ -317,17 +342,21 @@ static void execute_posetree(struct Depsgraph *depsgraph, /* gather transformations for this IK segment */ - if (pchan->parent) + if (pchan->parent) { copy_m3_m4(R_parmat, pchan->parent->pose_mat); - else + } + else { unit_m3(R_parmat); + } /* bone offset */ - if (pchan->parent && (a > 0)) + if (pchan->parent && (a > 0)) { sub_v3_v3v3(start, pchan->pose_head, pchan->parent->pose_tail); - else + } + else { /* only root bone (a = 0) has no parent */ start[0] = start[1] = start[2] = 0.0f; + } /* change length based on bone size */ length = bone->length * len_v3(R_bonemat[1]); @@ -350,12 +379,15 @@ static void execute_posetree(struct Depsgraph *depsgraph, IK_SetTransform(seg, start, rest_basis, basis, length); - if (pchan->ikflag & BONE_IK_XLIMIT) + if (pchan->ikflag & BONE_IK_XLIMIT) { IK_SetLimit(seg, IK_X, pchan->limitmin[0], pchan->limitmax[0]); - if (pchan->ikflag & BONE_IK_YLIMIT) + } + if (pchan->ikflag & BONE_IK_YLIMIT) { IK_SetLimit(seg, IK_Y, pchan->limitmin[1], pchan->limitmax[1]); - if (pchan->ikflag & BONE_IK_ZLIMIT) + } + if (pchan->ikflag & BONE_IK_ZLIMIT) { IK_SetLimit(seg, IK_Z, pchan->limitmin[2], pchan->limitmax[2]); + } IK_SetStiffness(seg, IK_X, pchan->stiffness[0]); IK_SetStiffness(seg, IK_Y, pchan->stiffness[1]); @@ -384,8 +416,9 @@ static void execute_posetree(struct Depsgraph *depsgraph, * kind of nasty bugs... */ normalize_m4(rootmat); } - else + else { unit_m4(rootmat); + } copy_v3_v3(rootmat[3], pchan->pose_head); mul_m4_m4m4(imat, ob->obmat, rootmat); @@ -465,28 +498,33 @@ static void execute_posetree(struct Depsgraph *depsgraph, iktarget = iktree[target->tip]; if ((data->flag & CONSTRAINT_IK_POS) && data->weight != 0.0f) { - if (poleconstrain) + if (poleconstrain) { IK_SolverSetPoleVectorConstraint( solver, iktarget, goalpos, polepos, data->poleangle, (poleangledata == data)); + } IK_SolverAddGoal(solver, iktarget, goalpos, data->weight); } - if ((data->flag & CONSTRAINT_IK_ROT) && (data->orientweight != 0.0f)) - if ((data->flag & CONSTRAINT_IK_AUTO) == 0) + if ((data->flag & CONSTRAINT_IK_ROT) && (data->orientweight != 0.0f)) { + if ((data->flag & CONSTRAINT_IK_AUTO) == 0) { IK_SolverAddGoalOrientation(solver, iktarget, goalrot, data->orientweight); + } + } } /* solve */ IK_Solve(solver, 0.0f, tree->iterations); - if (poleangledata) + if (poleangledata) { poleangledata->poleangle = IK_SolverGetPoleAngle(solver); + } IK_FreeSolver(solver); /* gather basis changes */ tree->basis_change = MEM_mallocN(sizeof(float[3][3]) * tree->totchannel, "ik basis change"); - if (hasstretch) + if (hasstretch) { ikstretch = MEM_mallocN(sizeof(float) * tree->totchannel, "ik stretch"); + } for (a = 0; a < tree->totchannel; a++) { IK_GetBasisChange(iktree[a], tree->basis_change[a]); @@ -506,8 +544,9 @@ static void execute_posetree(struct Depsgraph *depsgraph, ikstretch[a] = (length == 0.0f) ? 1.0f : (trans[1] + length) / length; } - else + else { ikstretch[a] = 1.0; + } stretch = (parentstretch == 0.0f) ? 1.0f : ikstretch[a] / parentstretch; @@ -525,19 +564,23 @@ static void execute_posetree(struct Depsgraph *depsgraph, } MEM_freeN(iktree); - if (ikstretch) + if (ikstretch) { MEM_freeN(ikstretch); + } } static void free_posetree(PoseTree *tree) { BLI_freelistN(&tree->targets); - if (tree->pchan) + if (tree->pchan) { MEM_freeN(tree->pchan); - if (tree->parent) + } + if (tree->parent) { MEM_freeN(tree->parent); - if (tree->basis_change) + } + if (tree->basis_change) { MEM_freeN(tree->basis_change); + } MEM_freeN(tree); } @@ -552,8 +595,9 @@ void iksolver_initialize_tree(struct Depsgraph *UNUSED(depsgraph), bPoseChannel *pchan; for (pchan = ob->pose->chanbase.first; pchan; pchan = pchan->next) { - if (pchan->constflag & PCHAN_HAS_IK) // flag is set on editing constraints - initialize_posetree(ob, pchan); // will attach it to root! + if (pchan->constflag & PCHAN_HAS_IK) { // flag is set on editing constraints + initialize_posetree(ob, pchan); // will attach it to root! + } } ob->pose->flag &= ~POSE_WAS_REBUILT; } @@ -569,13 +613,15 @@ void iksolver_execute_tree(struct Depsgraph *depsgraph, int a; /* stop on the first tree that isn't a standard IK chain */ - if (tree->type != CONSTRAINT_TYPE_KINEMATIC) + if (tree->type != CONSTRAINT_TYPE_KINEMATIC) { return; + } /* 4. walk over the tree for regular solving */ for (a = 0; a < tree->totchannel; a++) { - if (!(tree->pchan[a]->flag & POSE_DONE)) // successive trees can set the flag + if (!(tree->pchan[a]->flag & POSE_DONE)) { // successive trees can set the flag BKE_pose_where_is_bone(depsgraph, scene, ob, tree->pchan[a], ctime, 1); + } /* Tell blender that this channel was controlled by IK, * it's cleared on each BKE_pose_where_is(). */ tree->pchan[a]->flag |= POSE_CHAIN; @@ -609,15 +655,17 @@ void iksolver_release_tree(struct Scene *UNUSED(scene), struct Object *ob, float void iksolver_clear_data(bPose *pose) { for (bPoseChannel *pchan = pose->chanbase.first; pchan; pchan = pchan->next) { - if ((pchan->flag & POSE_IKTREE) == 0) + if ((pchan->flag & POSE_IKTREE) == 0) { continue; + } while (pchan->iktree.first) { PoseTree *tree = pchan->iktree.first; /* stop on the first tree that isn't a standard IK chain */ - if (tree->type != CONSTRAINT_TYPE_KINEMATIC) + if (tree->type != CONSTRAINT_TYPE_KINEMATIC) { break; + } BLI_remlink(&pchan->iktree, tree); free_posetree(tree); diff --git a/source/blender/ikplugin/intern/itasc_plugin.cpp b/source/blender/ikplugin/intern/itasc_plugin.cpp index e05385feb17..13e13f0ee26 100644 --- a/source/blender/ikplugin/intern/itasc_plugin.cpp +++ b/source/blender/ikplugin/intern/itasc_plugin.cpp @@ -119,10 +119,12 @@ struct IK_Target { } ~IK_Target() { - if (constraint) + if (constraint) { delete constraint; - if (target) + } + if (target) { delete target; + } } }; @@ -196,22 +198,29 @@ struct IK_Scene { ~IK_Scene() { // delete scene first - if (scene) + if (scene) { delete scene; - for (std::vector::iterator it = targets.begin(); it != targets.end(); ++it) + } + for (std::vector::iterator it = targets.begin(); it != targets.end(); ++it) { delete (*it); + } targets.clear(); - if (channels) + if (channels) { delete[] channels; - if (solver) + } + if (solver) { delete solver; - if (armature) + } + if (armature) { delete armature; - if (base) + } + if (base) { delete base; + } // delete cache last - if (cache) + if (cache) { delete cache; + } } }; @@ -245,16 +254,18 @@ static int initialize_chain(Object *ob, bPoseChannel *pchan_tip, bConstraint *co data = (bKinematicConstraint *)con->data; /* exclude tip from chain? */ - if (!(data->flag & CONSTRAINT_IK_TIP)) + if (!(data->flag & CONSTRAINT_IK_TIP)) { pchan_tip = pchan_tip->parent; + } rootbone = data->rootbone; /* Find the chain's root & count the segments needed */ for (curchan = pchan_tip; curchan; curchan = curchan->parent) { pchan_root = curchan; - if (++segcount > 255) // 255 is weak + if (++segcount > 255) { // 255 is weak break; + } if (segcount == rootbone) { // reached this end of the chain but if the chain is overlapping with a @@ -266,17 +277,20 @@ static int initialize_chain(Object *ob, bPoseChannel *pchan_tip, bConstraint *co break; } - if (BLI_listbase_is_empty(&curchan->iktree) == false) + if (BLI_listbase_is_empty(&curchan->iktree) == false) { // Oh oh, there is already a chain starting from this channel and our chain is longer... // Should handle this by moving the previous chain up to the beginning of our chain // For now we just stop here break; + } } - if (!segcount) + if (!segcount) { return 0; + } // we reached a limit and still not the end of a previous chain, quit - if ((pchan_root->flag & POSE_CHAIN) && BLI_listbase_is_empty(&pchan_root->iktree)) + if ((pchan_root->flag & POSE_CHAIN) && BLI_listbase_is_empty(&pchan_root->iktree)) { return 0; + } // now that we know how many segment we have, set the flag for (rootbone = segcount, segcount = 0, curchan = pchan_tip; segcount < rootbone; @@ -323,26 +337,32 @@ static int initialize_chain(Object *ob, bPoseChannel *pchan_tip, bConstraint *co a = t = 0; while (a < size && t < tree->totchannel) { // locate first matching channel - for (; t < tree->totchannel && tree->pchan[t] != chanlist[segcount - a - 1]; t++) + for (; t < tree->totchannel && tree->pchan[t] != chanlist[segcount - a - 1]; t++) { ; - if (t >= tree->totchannel) + } + if (t >= tree->totchannel) { break; + } for (; a < size && t < tree->totchannel && tree->pchan[t] == chanlist[segcount - a - 1]; - a++, t++) + a++, t++) { ; + } } segcount = segcount - a; target->tip = tree->totchannel + segcount - 1; if (segcount > 0) { - for (parent = a - 1; parent < tree->totchannel; parent++) - if (tree->pchan[parent] == chanlist[segcount - 1]->parent) + for (parent = a - 1; parent < tree->totchannel; parent++) { + if (tree->pchan[parent] == chanlist[segcount - 1]->parent) { break; + } + } /* shouldn't happen, but could with dependency cycles */ - if (parent == tree->totchannel) + if (parent == tree->totchannel) { parent = a - 1; + } /* resize array */ newsize = tree->totchannel + segcount; @@ -387,16 +407,20 @@ static bool constraint_valid(bConstraint *con) { bKinematicConstraint *data = (bKinematicConstraint *)con->data; - if (data->flag & CONSTRAINT_IK_AUTO) + if (data->flag & CONSTRAINT_IK_AUTO) { return true; - if (con->flag & (CONSTRAINT_DISABLE | CONSTRAINT_OFF)) + } + if (con->flag & (CONSTRAINT_DISABLE | CONSTRAINT_OFF)) { return false; + } if (is_cartesian_constraint(con)) { /* cartesian space constraint */ - if (data->tar == NULL) + if (data->tar == NULL) { return false; - if (data->tar->type == OB_ARMATURE && data->subtarget[0] == 0) + } + if (data->tar->type == OB_ARMATURE && data->subtarget[0] == 0) { return false; + } } return true; } @@ -410,8 +434,9 @@ static int initialize_scene(Object *ob, bPoseChannel *pchan_tip) treecount = 0; for (con = (bConstraint *)pchan_tip->constraints.first; con; con = (bConstraint *)con->next) { if (con->type == CONSTRAINT_TYPE_KINEMATIC) { - if (constraint_valid(con)) + if (constraint_valid(con)) { treecount += initialize_chain(ob, pchan_tip, con); + } } } return treecount; @@ -419,13 +444,15 @@ static int initialize_scene(Object *ob, bPoseChannel *pchan_tip) static IK_Data *get_ikdata(bPose *pose) { - if (pose->ikdata) + if (pose->ikdata) { return (IK_Data *)pose->ikdata; + } pose->ikdata = MEM_callocN(sizeof(IK_Data), "iTaSC ikdata"); // here init ikdata if needed // now that we have scene, make sure the default param are initialized - if (!DefIKParam.iksolver) + if (!DefIKParam.iksolver) { BKE_pose_itasc_init(&DefIKParam); + } return (IK_Data *)pose->ikdata; } @@ -434,20 +461,26 @@ static double EulerAngleFromMatrix(const KDL::Rotation &R, int axis) double t = KDL::sqrt(R(0, 0) * R(0, 0) + R(0, 1) * R(0, 1)); if (t > 16.0 * KDL::epsilon) { - if (axis == 0) + if (axis == 0) { return -KDL::atan2(R(1, 2), R(2, 2)); - else if (axis == 1) + } + else if (axis == 1) { return KDL::atan2(-R(0, 2), t); - else + } + else { return -KDL::atan2(R(0, 1), R(0, 0)); + } } else { - if (axis == 0) + if (axis == 0) { return -KDL::atan2(-R(2, 1), R(1, 1)); - else if (axis == 1) + } + else if (axis == 1) { return KDL::atan2(-R(0, 2), t); - else + } + else { return 0.0f; + } } } @@ -717,8 +750,9 @@ static bool copypose_callback(const iTaSC::Timestamp ×tamp, bItasc *ikparam = (bItasc *)iktarget->owner->pose->ikparam; // we need default parameters - if (!ikparam) + if (!ikparam) { ikparam = &DefIKParam; + } if (iktarget->blenderConstraint->flag & CONSTRAINT_OFF) { if (iktarget->controlType & iTaSC::CopyPose::CTL_POSITION) { @@ -761,15 +795,17 @@ static void copypose_error(const iTaSC::ConstraintValues *values, if (iktarget->controlType & iTaSC::CopyPose::CTL_POSITION) { // update error - for (i = 0, error = 0.0, value = values->values; i < values->number; ++i, ++value) + for (i = 0, error = 0.0, value = values->values; i < values->number; ++i, ++value) { error += KDL::sqr(value->y - value->yd); + } iktarget->blenderConstraint->lin_error = (float)KDL::sqrt(error); values++; } if (iktarget->controlType & iTaSC::CopyPose::CTL_ROTATION) { // update error - for (i = 0, error = 0.0, value = values->values; i < values->number; ++i, ++value) + for (i = 0, error = 0.0, value = values->values; i < values->number; ++i, ++value) { error += KDL::sqr(value->y - value->yd); + } iktarget->blenderConstraint->rot_error = (float)KDL::sqrt(error); values++; } @@ -785,8 +821,9 @@ static bool distance_callback(const iTaSC::Timestamp ×tamp, iTaSC::ConstraintValues *values = _values; bItasc *ikparam = (bItasc *)iktarget->owner->pose->ikparam; // we need default parameters - if (!ikparam) + if (!ikparam) { ikparam = &DefIKParam; + } // update weight according to mode if (iktarget->blenderConstraint->flag & CONSTRAINT_OFF) { @@ -938,8 +975,9 @@ static int convert_channels(struct Depsgraph *depsgraph, // the constraint and channels must be applied before we build the iTaSC scene, // this is because some of the pose data (e.g. pose head) don't have corresponding // joint angles and can't be applied to the iTaSC armature dynamically - if (!(pchan->flag & POSE_DONE)) + if (!(pchan->flag & POSE_DONE)) { BKE_pose_where_is_bone(depsgraph, ikscene->blscene, ikscene->blArmature, pchan, ctime, 1); + } // tell blender that this channel was controlled by IK, // it's cleared on each BKE_pose_where_is() pchan->flag |= (POSE_DONE | POSE_CHAIN); @@ -1028,11 +1066,13 @@ static int convert_channels(struct Depsgraph *depsgraph, break; case IK_XDOF | IK_YDOF | IK_ZDOF: // spherical joint - if (pchan->ikflag & (BONE_IK_XLIMIT | BONE_IK_YLIMIT | BONE_IK_ZLIMIT)) + if (pchan->ikflag & (BONE_IK_XLIMIT | BONE_IK_YLIMIT | BONE_IK_ZLIMIT)) { // decompose in a Swing+RotY joint ikchan->jointType = IK_SWING | IK_YDOF; - else + } + else { ikchan->jointType = IK_REVOLUTE; + } ikchan->ndof = 3; break; } @@ -1113,8 +1153,9 @@ static void BKE_pose_rest(IK_Scene *ikscene) pchan = ikchan->pchan; bone = pchan->bone; - if (ikchan->jointType & IK_TRANSY) + if (ikchan->jointType & IK_TRANSY) { rot[ikchan->ndof - 1] = bone->length * scale; + } rot += ikchan->ndof; joint += ikchan->ndof; } @@ -1141,8 +1182,9 @@ static IK_Scene *convert_tree( double *rot; float start[3]; - if (tree->totchannel == 0) + if (tree->totchannel == 0) { return NULL; + } ikscene = new IK_Scene; ikscene->blscene = blscene; @@ -1160,9 +1202,10 @@ static IK_Scene *convert_tree( ikparam = &DefIKParam; } - if (ikparam->flag & ITASC_SIMULATION) + if (ikparam->flag & ITASC_SIMULATION) { // no cache in animation mode ikscene->cache = new iTaSC::Cache(); + } switch (ikparam->solver) { case ITASC_SOLVER_SDLS: @@ -1206,14 +1249,18 @@ static IK_Scene *convert_tree( { float R_parmat[3][3]; float iR_parmat[3][3]; - if (pchan->parent) + if (pchan->parent) { copy_m3_m4(R_parmat, pchan->parent->pose_mat); - else + } + else { unit_m3(R_parmat); - if (pchan->parent) + } + if (pchan->parent) { sub_v3_v3v3(start, pchan->pose_head, pchan->parent->pose_tail); - else + } + else { start[0] = start[1] = start[2] = 0.0f; + } invert_m3_m3(iR_parmat, R_parmat); normalize_m3(iR_parmat); mul_m3_v3(iR_parmat, start); @@ -1327,9 +1374,10 @@ static IK_Scene *convert_tree( weight[1] = (1.0 - min_ff(1.0 - ikstretch, 1.0f - 0.001f)); weights.push_back(weight[1]); } - if (!ret) + if (!ret) { // error making the armature?? break; + } // joint points to the segment that correspond to the bone per say ikchan->tail = joint; ikchan->head = parent; @@ -1339,36 +1387,42 @@ static IK_Scene *convert_tree( joint = bone->name; joint += ":RX"; if (pchan->ikflag & BONE_IK_XLIMIT) { - if (arm->addLimitConstraint(joint, 0, pchan->limitmin[0], pchan->limitmax[0]) < 0) + if (arm->addLimitConstraint(joint, 0, pchan->limitmin[0], pchan->limitmax[0]) < 0) { break; + } } if (pchan->ikflag & BONE_IK_ROTCTL) { - if (arm->addConstraint(joint, joint_callback, ikchan, false, false) < 0) + if (arm->addConstraint(joint, joint_callback, ikchan, false, false) < 0) { break; + } } } if ((ikchan->jointType & IK_YDOF) && (pchan->ikflag & (BONE_IK_YLIMIT | BONE_IK_ROTCTL))) { joint = bone->name; joint += ":RY"; if (pchan->ikflag & BONE_IK_YLIMIT) { - if (arm->addLimitConstraint(joint, 0, pchan->limitmin[1], pchan->limitmax[1]) < 0) + if (arm->addLimitConstraint(joint, 0, pchan->limitmin[1], pchan->limitmax[1]) < 0) { break; + } } if (pchan->ikflag & BONE_IK_ROTCTL) { - if (arm->addConstraint(joint, joint_callback, ikchan, false, false) < 0) + if (arm->addConstraint(joint, joint_callback, ikchan, false, false) < 0) { break; + } } } if ((ikchan->jointType & IK_ZDOF) && (pchan->ikflag & (BONE_IK_ZLIMIT | BONE_IK_ROTCTL))) { joint = bone->name; joint += ":RZ"; if (pchan->ikflag & BONE_IK_ZLIMIT) { - if (arm->addLimitConstraint(joint, 0, pchan->limitmin[2], pchan->limitmax[2]) < 0) + if (arm->addLimitConstraint(joint, 0, pchan->limitmin[2], pchan->limitmax[2]) < 0) { break; + } } if (pchan->ikflag & BONE_IK_ROTCTL) { - if (arm->addConstraint(joint, joint_callback, ikchan, false, false) < 0) + if (arm->addConstraint(joint, joint_callback, ikchan, false, false) < 0) { break; + } } } if ((ikchan->jointType & IK_SWING) && @@ -1376,23 +1430,27 @@ static IK_Scene *convert_tree( joint = bone->name; joint += ":SW"; if (pchan->ikflag & BONE_IK_XLIMIT) { - if (arm->addLimitConstraint(joint, 0, pchan->limitmin[0], pchan->limitmax[0]) < 0) + if (arm->addLimitConstraint(joint, 0, pchan->limitmin[0], pchan->limitmax[0]) < 0) { break; + } } if (pchan->ikflag & BONE_IK_ZLIMIT) { - if (arm->addLimitConstraint(joint, 1, pchan->limitmin[2], pchan->limitmax[2]) < 0) + if (arm->addLimitConstraint(joint, 1, pchan->limitmin[2], pchan->limitmax[2]) < 0) { break; + } } if (pchan->ikflag & BONE_IK_ROTCTL) { - if (arm->addConstraint(joint, joint_callback, ikchan, false, false) < 0) + if (arm->addConstraint(joint, joint_callback, ikchan, false, false) < 0) { break; + } } } if ((ikchan->jointType & IK_REVOLUTE) && (pchan->ikflag & BONE_IK_ROTCTL)) { joint = bone->name; joint += ":SJ"; - if (arm->addConstraint(joint, joint_callback, ikchan, false, false) < 0) + if (arm->addConstraint(joint, joint_callback, ikchan, false, false) < 0) { break; + } } // no error, so restore ret = true; @@ -1431,9 +1489,10 @@ static IK_Scene *convert_tree( iktarget->constraintName += ":C:"; iktarget->constraintName += target->con->name; numtarget++; - if (condata->poletar) + if (condata->poletar) { // this constraint has a polar target polarcon = target->con; + } } } // deal with polar target if any @@ -1452,8 +1511,9 @@ static IK_Scene *convert_tree( ret = scene->addObject(armname, ikscene->base); armname = ob->id.name; armname += ":AR"; - if (ret) + if (ret) { ret = scene->addObject(armname, ikscene->armature, ikscene->base); + } if (!ret) { delete ikscene; return NULL; @@ -1461,8 +1521,9 @@ static IK_Scene *convert_tree( // set the weight e_matrix &Wq = arm->getWq(); assert(Wq.cols() == (int)weights.size()); - for (int q = 0; q < Wq.cols(); q++) + for (int q = 0; q < Wq.cols(); q++) { Wq(q, q) = weights[q]; + } // get the inverse rest pose frame of the base to compute relative rest pose of end effectors // this is needed to handle the enforce parameter // ikscene->pchan[0] is the root channel of the tree @@ -1494,8 +1555,10 @@ static IK_Scene *convert_tree( // add the end effector // estimate the average bone length, used to clamp feedback error - for (bonecnt = 0, bonelen = 0.f, a = iktarget->channel; a >= 0; a = tree->parent[a], bonecnt++) + for (bonecnt = 0, bonelen = 0.f, a = iktarget->channel; a >= 0; + a = tree->parent[a], bonecnt++) { bonelen += ikscene->blScale * tree->pchan[a]->bone->length; + } bonelen /= bonecnt; // store the rest pose of the end effector to compute enforce target @@ -1511,54 +1574,65 @@ static IK_Scene *convert_tree( iktarget->target = new iTaSC::MovingFrame(initPose); iktarget->target->setCallback(target_callback, iktarget); ret = scene->addObject(iktarget->targetName, iktarget->target); - if (!ret) + if (!ret) { break; + } switch (condata->type) { case CONSTRAINT_IK_COPYPOSE: controltype = 0; if (condata->flag & CONSTRAINT_IK_ROT) { - if (!(condata->flag & CONSTRAINT_IK_NO_ROT_X)) + if (!(condata->flag & CONSTRAINT_IK_NO_ROT_X)) { controltype |= iTaSC::CopyPose::CTL_ROTATIONX; - if (!(condata->flag & CONSTRAINT_IK_NO_ROT_Y)) + } + if (!(condata->flag & CONSTRAINT_IK_NO_ROT_Y)) { controltype |= iTaSC::CopyPose::CTL_ROTATIONY; - if (!(condata->flag & CONSTRAINT_IK_NO_ROT_Z)) + } + if (!(condata->flag & CONSTRAINT_IK_NO_ROT_Z)) { controltype |= iTaSC::CopyPose::CTL_ROTATIONZ; + } } if (condata->flag & CONSTRAINT_IK_POS) { - if (!(condata->flag & CONSTRAINT_IK_NO_POS_X)) + if (!(condata->flag & CONSTRAINT_IK_NO_POS_X)) { controltype |= iTaSC::CopyPose::CTL_POSITIONX; - if (!(condata->flag & CONSTRAINT_IK_NO_POS_Y)) + } + if (!(condata->flag & CONSTRAINT_IK_NO_POS_Y)) { controltype |= iTaSC::CopyPose::CTL_POSITIONY; - if (!(condata->flag & CONSTRAINT_IK_NO_POS_Z)) + } + if (!(condata->flag & CONSTRAINT_IK_NO_POS_Z)) { controltype |= iTaSC::CopyPose::CTL_POSITIONZ; + } } if (controltype) { iktarget->constraint = new iTaSC::CopyPose(controltype, controltype, bonelen); // set the gain - if (controltype & iTaSC::CopyPose::CTL_POSITION) + if (controltype & iTaSC::CopyPose::CTL_POSITION) { iktarget->constraint->setControlParameter( iTaSC::CopyPose::ID_POSITION, iTaSC::ACT_ALPHA, condata->weight); - if (controltype & iTaSC::CopyPose::CTL_ROTATION) + } + if (controltype & iTaSC::CopyPose::CTL_ROTATION) { iktarget->constraint->setControlParameter( iTaSC::CopyPose::ID_ROTATION, iTaSC::ACT_ALPHA, condata->orientweight); + } iktarget->constraint->registerCallback(copypose_callback, iktarget); iktarget->errorCallback = copypose_error; iktarget->controlType = controltype; // add the constraint - if (condata->flag & CONSTRAINT_IK_TARGETAXIS) + if (condata->flag & CONSTRAINT_IK_TARGETAXIS) { ret = scene->addConstraintSet(iktarget->constraintName, iktarget->constraint, iktarget->targetName, armname, "", ikscene->channels[iktarget->channel].tail); - else + } + else { ret = scene->addConstraintSet(iktarget->constraintName, iktarget->constraint, armname, iktarget->targetName, ikscene->channels[iktarget->channel].tail); + } } break; case CONSTRAINT_IK_DISTANCE: @@ -1577,8 +1651,9 @@ static IK_Scene *convert_tree( ikscene->channels[iktarget->channel].tail); break; } - if (!ret) + if (!ret) { break; + } } if (!ret || !scene->addCache(ikscene->cache) || !scene->addSolver(ikscene->solver) || !scene->initialize()) { @@ -1609,12 +1684,15 @@ static void create_scene(struct Depsgraph *depsgraph, Scene *scene, Object *ob, while (tree) { BLI_remlink(&pchan->iktree, tree); BLI_freelistN(&tree->targets); - if (tree->pchan) + if (tree->pchan) { MEM_freeN(tree->pchan); - if (tree->parent) + } + if (tree->parent) { MEM_freeN(tree->parent); - if (tree->basis_change) + } + if (tree->basis_change) { MEM_freeN(tree->basis_change); + } MEM_freeN(tree); tree = (PoseTree *)pchan->iktree.first; } @@ -1631,8 +1709,9 @@ static int init_scene(Object *ob) if (ob->pose->ikdata) { for (scene = ((IK_Data *)ob->pose->ikdata)->first; scene != NULL; scene = scene->next) { - if (fabs(scene->blScale - scale) > KDL::epsilon) + if (fabs(scene->blScale - scale) > KDL::epsilon) { return 1; + } scene->channels[0].pchan->flag |= POSE_IKTREE; } } @@ -1660,8 +1739,9 @@ static void execute_scene(struct Depsgraph *depsgraph, else { // in animation mode, we must get the bone position from action and constraints for (i = 0, ikchan = ikscene->channels; i < ikscene->numchan; i++, ++ikchan) { - if (!(ikchan->pchan->flag & POSE_DONE)) + if (!(ikchan->pchan->flag & POSE_DONE)) { BKE_pose_where_is_bone(depsgraph, blscene, ikscene->blArmature, ikchan->pchan, ctime, 1); + } // tell blender that this channel was controlled by IK, // it's cleared on each BKE_pose_where_is() ikchan->pchan->flag |= (POSE_DONE | POSE_CHAIN); @@ -1671,12 +1751,14 @@ static void execute_scene(struct Depsgraph *depsgraph, // only run execute the scene if at least one of our target is enabled for (i = ikscene->targets.size(); i > 0; --i) { IK_Target *iktarget = ikscene->targets[i - 1]; - if (!(iktarget->blenderConstraint->flag & CONSTRAINT_OFF)) + if (!(iktarget->blenderConstraint->flag & CONSTRAINT_OFF)) { break; + } } - if (i == 0 && ikscene->armature->getNrOfConstraints() == 0) + if (i == 0 && ikscene->armature->getNrOfConstraints() == 0) { // all constraint disabled return; + } // compute timestep double timestamp = ctime * frtime + 2147483.648; @@ -1707,8 +1789,9 @@ static void execute_scene(struct Depsgraph *depsgraph, sts = cts = (iTaSC::CacheTS)(timestamp * 1000.0 + 0.5); if (ikscene->cache->getPreviousCacheItem(ikscene->armature, 0, &cts) == NULL || cts == 0) { // the cache is empty before this time, reiterate - if (ikparam->flag & ITASC_INITIAL_REITERATION) + if (ikparam->flag & ITASC_INITIAL_REITERATION) { reiterate = true; + } } else { // can take the cache as a start point. @@ -1759,21 +1842,24 @@ static void execute_scene(struct Depsgraph *depsgraph, float yaxis[3]; for (i = 0, ikchan = ikscene->channels; i < ikscene->numchan; ++i, ++ikchan) { if (i == 0) { - if (!arm->getRelativeFrame(frame, ikchan->tail)) + if (!arm->getRelativeFrame(frame, ikchan->tail)) { break; + } // this frame is relative to base, make it relative to object ikchan->frame = ikscene->baseFrame * frame; } else { - if (!arm->getRelativeFrame(frame, ikchan->tail, ikscene->channels[ikchan->parent].tail)) + if (!arm->getRelativeFrame(frame, ikchan->tail, ikscene->channels[ikchan->parent].tail)) { break; + } // combine with parent frame to get frame relative to object ikchan->frame = ikscene->channels[ikchan->parent].frame * frame; } // ikchan->frame is the tail frame relative to object // get bone length - if (!arm->getSegment(ikchan->tail, 3, joint, q_rest[0], q[0], tip)) + if (!arm->getSegment(ikchan->tail, 3, joint, q_rest[0], q[0], tip)) { break; + } if (joint->getType() == KDL::Joint::TransY) { // stretch bones have a TY joint, compute the scale scale = (float)(q[0] / q_rest[0]); @@ -1822,8 +1908,9 @@ void itasc_initialize_tree(struct Depsgraph *depsgraph, int count = 0; if (ob->pose->ikdata != NULL && !(ob->pose->flag & POSE_WAS_REBUILT)) { - if (!init_scene(ob)) + if (!init_scene(ob)) { return; + } } // first remove old scene itasc_clear_data(ob->pose); @@ -1831,13 +1918,15 @@ void itasc_initialize_tree(struct Depsgraph *depsgraph, // for blender but we'll start with the IK constraint alone for (pchan = (bPoseChannel *)ob->pose->chanbase.first; pchan; pchan = (bPoseChannel *)pchan->next) { - if (pchan->constflag & PCHAN_HAS_IK) + if (pchan->constflag & PCHAN_HAS_IK) { count += initialize_scene(ob, pchan); + } } // if at least one tree, create the scenes from the PoseTree stored in the channels // postpone until execute_tree: this way the pose constraint are included - if (count) + if (count) { create_scene(depsgraph, scene, ob, ctime); + } itasc_update_param(ob->pose); // make sure we don't rebuilt until the user changes something important ob->pose->flag &= ~POSE_WAS_REBUILT; @@ -1853,8 +1942,9 @@ void itasc_execute_tree(struct Depsgraph *depsgraph, IK_Data *ikdata = (IK_Data *)ob->pose->ikdata; bItasc *ikparam = (bItasc *)ob->pose->ikparam; // we need default parameters - if (!ikparam) + if (!ikparam) { ikparam = &DefIKParam; + } for (IK_Scene *ikscene = ikdata->first; ikscene; ikscene = ikscene->next) { if (ikscene->channels[0].pchan == pchan_root) { @@ -1889,9 +1979,10 @@ void itasc_clear_cache(struct bPose *pose) if (pose->ikdata) { IK_Data *ikdata = (IK_Data *)pose->ikdata; for (IK_Scene *scene = ikdata->first; scene; scene = scene->next) { - if (scene->cache) + if (scene->cache) { // clear all cache but leaving the timestamp 0 (=rest pose) scene->cache->clearCacheFrom(NULL, 1); + } } } } @@ -1930,8 +2021,9 @@ void itasc_test_constraint(struct Object *ob, struct bConstraint *cons) struct bKinematicConstraint *data = (struct bKinematicConstraint *)cons->data; /* only for IK constraint */ - if (cons->type != CONSTRAINT_TYPE_KINEMATIC || data == NULL) + if (cons->type != CONSTRAINT_TYPE_KINEMATIC || data == NULL) { return; + } switch (data->type) { case CONSTRAINT_IK_COPYPOSE: -- cgit v1.2.3