use BLI_strncpy and BLI_snprintf when the size of the string is known.
[blender.git] / source / blender / editors / armature / editarmature_retarget.c
index d50f2d0..9ec27b6 100644 (file)
@@ -1,6 +1,4 @@
-/**
- * $Id$
- *
+/*
  * ***** BEGIN GPL LICENSE BLOCK *****
  *
  * This program is free software; you can redistribute it and/or
  * autoarmature.c: Interface for automagically manipulating armature (retarget, created, ...)
  */
 
+/** \file blender/editors/armature/editarmature_retarget.c
+ *  \ingroup edarmature
+ */
+
+
 #include <ctype.h>
 #include <stdlib.h>
 #include <string.h>
@@ -41,6 +44,7 @@
 #include "BLI_blenlib.h"
 #include "BLI_math.h"
 #include "BLI_editVert.h"
+#include "BLI_utildefines.h"
 #include "BLI_ghash.h"
 #include "BLI_graph.h"
 #include "BLI_rand.h"
@@ -97,7 +101,7 @@ typedef enum
        ARC_USED = 2
 } ArcUsageFlags;
 
-RigGraph *GLOBAL_RIGG = NULL;
+static RigGraph *GLOBAL_RIGG = NULL;
 
 /*******************************************************************************************************/
 
@@ -111,7 +115,7 @@ float rollBoneByQuat(EditBone *bone, float old_up_axis[3], float qrot[4]);
 
 /*********************************** EDITBONE UTILS ****************************************************/
 
-int countEditBoneChildren(ListBase *list, EditBone *parent)
+static int countEditBoneChildren(ListBase *list, EditBone *parent)
 {
        EditBone *ebone;
        int count = 0;
@@ -127,7 +131,7 @@ int countEditBoneChildren(ListBase *list, EditBone *parent)
        return count;
 }
 
-EditBone* nextEditBoneChild(ListBase *list, EditBone *parent, int n)
+static EditBone* nextEditBoneChild(ListBase *list, EditBone *parent, int n)
 {
        EditBone *ebone;
        
@@ -146,21 +150,21 @@ EditBone* nextEditBoneChild(ListBase *list, EditBone *parent, int n)
        return NULL;
 }
 
-void getEditBoneRollUpAxis(EditBone *bone, float roll, float up_axis[3])
+static void getEditBoneRollUpAxis(EditBone *bone, float roll, float up_axis[3])
 {
        float mat[3][3], nor[3];
 
        sub_v3_v3v3(nor, bone->tail, bone->head);
        
        vec_roll_to_mat3(nor, roll, mat);
-       VECCOPY(up_axis, mat[2]);
+       copy_v3_v3(up_axis, mat[2]);
 }
 
-float rollBoneByQuatAligned(EditBone *bone, float old_up_axis[3], float qrot[4], float qroll[4], float aligned_axis[3])
+static float rollBoneByQuatAligned(EditBone *bone, float old_up_axis[3], float qrot[4], float qroll[4], float aligned_axis[3])
 {
        float nor[3], new_up_axis[3], x_axis[3], z_axis[3];
        
-       VECCOPY(new_up_axis, old_up_axis);
+       copy_v3_v3(new_up_axis, old_up_axis);
        mul_qt_v3(qrot, new_up_axis);
        
        sub_v3_v3v3(nor, bone->tail, bone->head);
@@ -185,16 +189,16 @@ float rollBoneByQuatAligned(EditBone *bone, float old_up_axis[3], float qrot[4],
        if (angle_normalized_v3v3(x_axis, new_up_axis) < angle_normalized_v3v3(z_axis, new_up_axis))
        {
                rotation_between_vecs_to_quat(qroll, new_up_axis, x_axis); /* set roll rotation quat */
-               return ED_rollBoneToVector(bone, x_axis);
+               return ED_rollBoneToVector(bone, x_axis, FALSE);
        }
        else
        {
                rotation_between_vecs_to_quat(qroll, new_up_axis, z_axis); /* set roll rotation quat */
-               return ED_rollBoneToVector(bone, z_axis);
+               return ED_rollBoneToVector(bone, z_axis, FALSE);
        }
 }
 
-float rollBoneByQuatJoint(RigEdge *edge, RigEdge *previous, float qrot[4], float qroll[4], float up_axis[3])
+static float rollBoneByQuatJoint(RigEdge *edge, RigEdge *previous, float qrot[4], float qroll[4], float up_axis[3])
 {
        if (previous == NULL)
        {
@@ -232,7 +236,7 @@ float rollBoneByQuatJoint(RigEdge *edge, RigEdge *previous, float qrot[4], float
                
                mul_qt_v3(qroll, normal);
                        
-               VECCOPY(new_up_axis, edge->up_axis);
+               copy_v3_v3(new_up_axis, edge->up_axis);
                mul_qt_v3(qrot, new_up_axis);
                
                normalize_v3(new_up_axis);
@@ -240,7 +244,7 @@ float rollBoneByQuatJoint(RigEdge *edge, RigEdge *previous, float qrot[4], float
                /* real qroll between normal and up_axis */
                rotation_between_vecs_to_quat(qroll, new_up_axis, normal);
 
-               return ED_rollBoneToVector(edge->bone, normal);
+               return ED_rollBoneToVector(edge->bone, normal, FALSE);
        }
 }
 
@@ -248,17 +252,15 @@ float rollBoneByQuat(EditBone *bone, float old_up_axis[3], float qrot[4])
 {
        float new_up_axis[3];
        
-       VECCOPY(new_up_axis, old_up_axis);
+       copy_v3_v3(new_up_axis, old_up_axis);
        mul_qt_v3(qrot, new_up_axis);
        
-       normalize_v3(new_up_axis);
-       
-       return ED_rollBoneToVector(bone, new_up_axis);
+       return ED_rollBoneToVector(bone, new_up_axis, FALSE);
 }
 
 /************************************ DESTRUCTORS ******************************************************/
 
-void RIG_freeRigArc(BArc *arc)
+static void RIG_freeRigArc(BArc *arc)
 {
        BLI_freelistN(&((RigArc*)arc)->edges);
 }
@@ -306,7 +308,7 @@ void RIG_freeRigGraph(BGraph *rg)
 
 /************************************* ALLOCATORS ******************************************************/
 
-static RigGraph *newRigGraph()
+static RigGraph *newRigGraph(void)
 {
        RigGraph *rg;
        int totthread;
@@ -365,7 +367,7 @@ static RigNode *newRigNodeHead(RigGraph *rg, RigArc *arc, float p[3])
        node = MEM_callocN(sizeof(RigNode), "rig node");
        BLI_addtail(&rg->nodes, node);
 
-       VECCOPY(node->p, p);
+       copy_v3_v3(node->p, p);
        node->degree = 1;
        node->arcs = NULL;
        
@@ -387,7 +389,7 @@ static RigNode *newRigNode(RigGraph *rg, float p[3])
        node = MEM_callocN(sizeof(RigNode), "rig node");
        BLI_addtail(&rg->nodes, node);
 
-       VECCOPY(node->p, p);
+       copy_v3_v3(node->p, p);
        node->degree = 0;
        node->arcs = NULL;
        
@@ -410,12 +412,12 @@ static void RIG_appendEdgeToArc(RigArc *arc, RigEdge *edge)
 
        if (edge->prev == NULL)
        {
-               VECCOPY(edge->head, arc->head->p);
+               copy_v3_v3(edge->head, arc->head->p);
        }
        else
        {
                RigEdge *last_edge = edge->prev;
-               VECCOPY(edge->head, last_edge->tail);
+               copy_v3_v3(edge->head, last_edge->tail);
                RIG_calculateEdgeAngles(last_edge, edge);
        }
        
@@ -432,7 +434,7 @@ static void RIG_addEdgeToArc(RigArc *arc, float tail[3], EditBone *bone)
 
        edge = MEM_callocN(sizeof(RigEdge), "rig edge");
 
-       VECCOPY(edge->tail, tail);
+       copy_v3_v3(edge->tail, tail);
        edge->bone = bone;
        
        if (bone)
@@ -448,7 +450,7 @@ static void renameTemplateBone(char *name, char *template_name, ListBase *editbo
 {
        int i, j;
        
-       for (i = 0, j = 0; template_name[i] != '\0' && i < 31 && j < 31; i++)
+       for (i = 0, j = 0; template_name[i] != '\0' && i < (MAXBONENAME-1) && j < (MAXBONENAME-1); i++)
        {
                if (template_name[i] == '&')
                {
@@ -483,14 +485,14 @@ static void renameTemplateBone(char *name, char *template_name, ListBase *editbo
 static RigControl *cloneControl(RigGraph *rg, RigGraph *src_rg, RigControl *src_ctrl, GHash *ptr_hash, char *side_string, char *num_string)
 {
        RigControl *ctrl;
-       char name[32];
+       char name[MAXBONENAME];
        
        ctrl = newRigControl(rg);
        
-       VECCOPY(ctrl->head, src_ctrl->head);
-       VECCOPY(ctrl->tail, src_ctrl->tail);
-       VECCOPY(ctrl->up_axis, src_ctrl->up_axis);
-       VECCOPY(ctrl->offset, src_ctrl->offset);
+       copy_v3_v3(ctrl->head, src_ctrl->head);
+       copy_v3_v3(ctrl->tail, src_ctrl->tail);
+       copy_v3_v3(ctrl->up_axis, src_ctrl->up_axis);
+       copy_v3_v3(ctrl->offset, src_ctrl->offset);
        
        ctrl->tail_mode = src_ctrl->tail_mode;
        ctrl->flag = src_ctrl->flag;
@@ -529,9 +531,9 @@ static RigArc *cloneArc(RigGraph *rg, RigGraph *src_rg, RigArc *src_arc, GHash *
        
                edge = MEM_callocN(sizeof(RigEdge), "rig edge");
 
-               VECCOPY(edge->head, src_edge->head);
-               VECCOPY(edge->tail, src_edge->tail);
-               VECCOPY(edge->up_axis, src_edge->up_axis);
+               copy_v3_v3(edge->head, src_edge->head);
+               copy_v3_v3(edge->tail, src_edge->tail);
+               copy_v3_v3(edge->up_axis, src_edge->up_axis);
                
                edge->length = src_edge->length;
                edge->angle = src_edge->angle;
@@ -539,7 +541,7 @@ static RigArc *cloneArc(RigGraph *rg, RigGraph *src_rg, RigArc *src_arc, GHash *
                
                if (src_edge->bone != NULL)
                {
-                       char name[32];
+                       char name[MAXBONENAME];
                        renameTemplateBone(name, src_edge->bone->name, rg->editbones, side_string, num_string);
                        edge->bone = duplicateEditBoneObjects(src_edge->bone, name, rg->editbones, src_rg->ob, rg->ob);
                        edge->bone->flag &= ~(BONE_TIPSEL|BONE_SELECTED|BONE_ROOTSEL);
@@ -688,8 +690,8 @@ static void RIG_addControlBone(RigGraph *rg, EditBone *bone)
 {
        RigControl *ctrl = newRigControl(rg);
        ctrl->bone = bone;
-       VECCOPY(ctrl->head, bone->head);
-       VECCOPY(ctrl->tail, bone->tail);
+       copy_v3_v3(ctrl->head, bone->head);
+       copy_v3_v3(ctrl->tail, bone->tail);
        getEditBoneRollUpAxis(bone, bone->roll, ctrl->up_axis);
        ctrl->tail_mode = TL_NONE;
        
@@ -706,7 +708,7 @@ static int RIG_parentControl(RigControl *ctrl, EditBone *link)
                sub_v3_v3v3(offset, ctrl->bone->head, link->head);
 
                /* if root matches, check for direction too */          
-               if (dot_v3v3(offset, offset) < 0.0001)
+               if (dot_v3v3(offset, offset) < 0.0001f)
                {
                        float vbone[3], vparent[3];
                        
@@ -724,7 +726,7 @@ static int RIG_parentControl(RigControl *ctrl, EditBone *link)
                                cross_v3_v3v3(nor, vbone, vparent);
                                
                                len = dot_v3v3(nor, nor);
-                               if (len < 0.0001)
+                               if (len < 0.0001f)
                                {
                                        flag |= RIG_CTRL_FIT_BONE;
                                }
@@ -763,7 +765,7 @@ static int RIG_parentControl(RigControl *ctrl, EditBone *link)
                ctrl->link = link;
                ctrl->flag = flag;
                
-               VECCOPY(ctrl->offset, offset);
+               copy_v3_v3(ctrl->offset, offset);
                
                return 1;
        }
@@ -857,8 +859,8 @@ static void RIG_reconnectControlBones(RigGraph *rg)
                                                {
                                                        int fit = 0;
                                                        
-                                                       fit = len_v3v3(ctrl->bone->head, edge->bone->head) < 0.0001;
-                                                       fit = fit || len_v3v3(ctrl->bone->tail, edge->bone->tail) < 0.0001;
+                                                       fit = len_v3v3(ctrl->bone->head, edge->bone->head) < 0.0001f;
+                                                       fit = fit || len_v3v3(ctrl->bone->tail, edge->bone->tail) < 0.0001f;
                                                        
                                                        if (fit)
                                                        {
@@ -1014,13 +1016,13 @@ static void RIG_reconnectControlBones(RigGraph *rg)
                                /* don't link with parent */
                                if (bone->parent != ctrl->bone)
                                {
-                                       if (len_v3v3(ctrl->bone->tail, bone->head) < 0.01)
+                                       if (len_v3v3(ctrl->bone->tail, bone->head) < 0.01f)
                                        {
                                                ctrl->tail_mode = TL_HEAD;
                                                ctrl->link_tail = bone;
                                                break;
                                        }
-                                       else if (len_v3v3(ctrl->bone->tail, bone->tail) < 0.01)
+                                       else if (len_v3v3(ctrl->bone->tail, bone->tail) < 0.01f)
                                        {
                                                ctrl->tail_mode = TL_TAIL;
                                                ctrl->link_tail = bone;
@@ -1120,14 +1122,14 @@ static void RIG_removeUneededOffsets(RigGraph *rg)
                
                if (first_edge->bone == NULL)
                {
-                       if (first_edge->bone == NULL && len_v3v3(first_edge->tail, arc->head->p) <= 0.001)
+                       if (first_edge->bone == NULL && len_v3v3(first_edge->tail, arc->head->p) <= 0.001f)
                        {
                                BLI_remlink(&arc->edges, first_edge);
                                MEM_freeN(first_edge);
                        }
                        else if (arc->head->degree == 1)
                        {
-                               RigNode *new_node = (RigNode*)BLI_FindNodeByPosition((BGraph*)rg, first_edge->tail, 0.001);
+                               RigNode *new_node = (RigNode*)BLI_FindNodeByPosition((BGraph*)rg, first_edge->tail, 0.001f);
                                
                                if (new_node)
                                {
@@ -1144,7 +1146,7 @@ static void RIG_removeUneededOffsets(RigGraph *rg)
                                                BLI_remlink(&arc->edges, first_edge);
                                                MEM_freeN(first_edge);
                                                
-                                               VECCOPY(arc->head->p, next_edge->head);
+                                               copy_v3_v3(arc->head->p, next_edge->head);
                                        }
                                }
                        }
@@ -1220,7 +1222,7 @@ static void RIG_removeUneededOffsets(RigGraph *rg)
                                                        BLI_remlink(&arc->edges, first_edge);
                                                        MEM_freeN(first_edge);
                                                        
-                                                       VECCOPY(arc->head->p, next_edge->head);
+                                                       copy_v3_v3(arc->head->p, next_edge->head);
                                                        
                                                        /* remove null edge in other arcs too */
                                                        for (other_arc = rg->arcs.first; other_arc; other_arc = other_arc->next)
@@ -1250,14 +1252,14 @@ static void RIG_removeUneededOffsets(RigGraph *rg)
                
                if (last_edge->bone == NULL)
                {
-                       if (len_v3v3(last_edge->head, arc->tail->p) <= 0.001)
+                       if (len_v3v3(last_edge->head, arc->tail->p) <= 0.001f)
                        {
                                BLI_remlink(&arc->edges, last_edge);
                                MEM_freeN(last_edge);
                        }
                        else if (arc->tail->degree == 1)
                        {
-                               RigNode *new_node = (RigNode*)BLI_FindNodeByPosition((BGraph*)rg, last_edge->head, 0.001);
+                               RigNode *new_node = (RigNode*)BLI_FindNodeByPosition((BGraph*)rg, last_edge->head, 0.001f);
                                
                                if (new_node)
                                {
@@ -1282,7 +1284,7 @@ static void RIG_removeUneededOffsets(RigGraph *rg)
                                                BLI_remlink(&arc->edges, last_edge);
                                                MEM_freeN(last_edge);
                                                
-                                               VECCOPY(arc->tail->p, previous_edge->tail);
+                                               copy_v3_v3(arc->tail->p, previous_edge->tail);
                                                previous_edge->angle = 0;
                                        }
                                }
@@ -1415,9 +1417,9 @@ static void RIG_findHead(RigGraph *rg)
 
 /*******************************************************************************************************/
 
-void RIG_printNode(RigNode *node, char name[])
+static void RIG_printNode(RigNode *node, const char name[])
 {
-       printf("%s %p %i <%0.3f, %0.3f, %0.3f>\n", name, node, node->degree, node->p[0], node->p[1], node->p[2]);
+       printf("%s %p %i <%0.3f, %0.3f, %0.3f>\n", name, (void *)node, node->degree, node->p[0], node->p[1], node->p[2]);
        
        if (node->symmetry_flag & SYM_TOPOLOGICAL)
        {
@@ -1444,20 +1446,20 @@ void RIG_printArcBones(RigArc *arc)
        printf("\n");
 }
 
-void RIG_printCtrl(RigControl *ctrl, char *indent)
+static void RIG_printCtrl(RigControl *ctrl, char *indent)
 {
        char text[128];
        
        printf("%sBone: %s\n", indent, ctrl->bone->name);
        printf("%sLink: %s\n", indent, ctrl->link ? ctrl->link->name : "!NONE!");
        
-       sprintf(text, "%soffset", indent);
+       BLI_snprintf(text, sizeof(text), "%soffset", indent);
        print_v3(text, ctrl->offset);
        
        printf("%sFlag: %i\n", indent, ctrl->flag);
 }
 
-void RIG_printLinkedCtrl(RigGraph *rg, EditBone *bone, int tabs)
+static void RIG_printLinkedCtrl(RigGraph *rg, EditBone *bone, int tabs)
 {
        RigControl *ctrl;
        char indent[64];
@@ -1532,13 +1534,12 @@ RigGraph *RIG_graphFromArmature(const bContext *C, Object *ob, bArmature *arm)
        Scene *scene = CTX_data_scene(C);
        EditBone *ebone;
        RigGraph *rg;
-       
+
        rg = newRigGraph();
        
        if (obedit == ob)
        {
-               bArmature *arm = obedit->data;
-               rg->editbones = arm->edbo;
+               rg->editbones = ((bArmature *)obedit->data)->edbo;
        }
        else
        {
@@ -1579,13 +1580,13 @@ RigGraph *RIG_graphFromArmature(const bContext *C, Object *ob, bArmature *arm)
        return rg;
 }
 
-RigGraph *armatureSelectedToGraph(bContext *C, Object *ob, bArmature *arm)
+static RigGraph *armatureSelectedToGraph(bContext *C, Object *ob, bArmature *arm)
 {
        Object *obedit = CTX_data_edit_object(C);
        Scene *scene = CTX_data_scene(C);
        EditBone *ebone;
        RigGraph *rg;
-       
+
        rg = newRigGraph();
        
        if (obedit == ob)
@@ -1658,7 +1659,8 @@ static EditBone *add_editbonetolist(char *name, ListBase *list)
 }
 #endif
 
-void generateMissingArcsFromNode(RigGraph *rigg, ReebNode *node, int multi_level_limit)
+#if 0 /* UNUSED */
+static void generateMissingArcsFromNode(RigGraph *rigg, ReebNode *node, int multi_level_limit)
 {
        while (node->multi_level > multi_level_limit && node->link_up)
        {
@@ -1691,9 +1693,9 @@ void generateMissingArcsFromNode(RigGraph *rigg, ReebNode *node, int multi_level
        }
 }
 
-void generateMissingArcs(RigGraph *rigg)
+static void generateMissingArcs(RigGraph *rigg)
 {
-       ReebGraph *reebg = rigg->link_mesh;
+       ReebGraph *reebg;
        int multi_level_limit = 5;
        
        for (reebg = rigg->link_mesh; reebg; reebg = reebg->link_up)
@@ -1710,6 +1712,7 @@ void generateMissingArcs(RigGraph *rigg)
                }
        }
 }
+#endif
 
 /************************************ RETARGETTING *****************************************************/
 
@@ -1752,7 +1755,7 @@ static void finalizeControl(RigGraph *rigg, RigControl *ctrl, float resize)
                        sub_v3_v3v3(v1, ctrl->bone->tail, ctrl->bone->head);
                        sub_v3_v3v3(v2, tail_vec, ctrl->bone->head);
                        
-                       VECCOPY(ctrl->bone->tail, tail_vec);
+                       copy_v3_v3(ctrl->bone->tail, tail_vec);
                        
                        rotation_between_vecs_to_quat(qtail, v1, v2);
                        mul_qt_qtqt(ctrl->qrot, qtail, ctrl->qrot);
@@ -1788,7 +1791,7 @@ static void repositionControl(RigGraph *rigg, RigControl *ctrl, float head[3], f
 {
        float parent_offset[3], tail_offset[3];
        
-       VECCOPY(parent_offset, ctrl->offset);
+       copy_v3_v3(parent_offset, ctrl->offset);
        mul_v3_fl(parent_offset, resize);
        mul_qt_v3(qrot, parent_offset);
        
@@ -1796,7 +1799,7 @@ static void repositionControl(RigGraph *rigg, RigControl *ctrl, float head[3], f
 
        ctrl->flag |= RIG_CTRL_HEAD_DONE;
 
-       QUATCOPY(ctrl->qrot, qrot); 
+       copy_qt_qt(ctrl->qrot, qrot);
 
        if (ctrl->tail_mode == TL_NONE)
        {
@@ -1833,8 +1836,8 @@ static void repositionBone(bContext *C, RigGraph *rigg, RigEdge *edge, float vec
        
        rotation_between_vecs_to_quat(qrot, v1, v2);
        
-       VECCOPY(bone->head, vec0);
-       VECCOPY(bone->tail, vec1);
+       copy_v3_v3(bone->head, vec0);
+       copy_v3_v3(bone->tail, vec1);
        
        if (!is_zero_v3(up_axis))
        {
@@ -1884,7 +1887,7 @@ static RetargetMode detectArcRetargetMode(RigArc *iarc)
        RigEdge *edge;
        int large_angle = 0;
        float avg_angle = 0;
-       float avg_length = 0;
+       /* float avg_length = 0; */ /* UNUSED */
        int nb_edges = 0;
        
        
@@ -1896,7 +1899,7 @@ static RetargetMode detectArcRetargetMode(RigArc *iarc)
        
        avg_angle /= nb_edges - 1; /* -1 because last edge doesn't have an angle */
 
-       avg_length = iarc->length / nb_edges;
+       /* avg_length = iarc->length / nb_edges; */  /* UNUSED */
        
        
        if (nb_edges > 2)
@@ -2010,11 +2013,11 @@ static float costAngle(float original_angle, float vec_first[3], float vec_secon
                {
                        current_angle = saacos(dot_v3v3(vec_first, vec_second));
 
-                       return angle_weight * fabs(current_angle - original_angle);
+                       return angle_weight * fabsf(current_angle - original_angle);
                }
                else
                {
-                       return angle_weight * M_PI;
+                       return angle_weight * (float)M_PI;
                }
        }
        else
@@ -2197,7 +2200,9 @@ static void retargetArctoArcAggresive(bContext *C, RigGraph *rigg, RigArc *iarc,
        float angle_weight = 1.0; // GET FROM CONTEXT
        float length_weight = 1.0;
        float distance_weight = 1.0;
+#ifndef USE_THREADS
        float min_cost = FLT_MAX;
+#endif
        float *vec0, *vec1;
        int *best_positions;
        int nb_edges = BLI_countlist(&iarc->edges);
@@ -2240,7 +2245,9 @@ static void retargetArctoArcAggresive(bContext *C, RigGraph *rigg, RigArc *iarc,
                int nb_positions = earc->bcount;
                int nb_memo_nodes = nb_positions * nb_positions * (nb_joints + 1);
                MemoNode *table = MEM_callocN(nb_memo_nodes * sizeof(MemoNode), "memoization table");
+#ifndef USE_THREADS
                MemoNode *result;
+#endif
                float **positions_cache = MEM_callocN(sizeof(float*) * (nb_positions + 2), "positions cache");
                int i;
                
@@ -2255,11 +2262,15 @@ static void retargetArctoArcAggresive(bContext *C, RigGraph *rigg, RigArc *iarc,
                        positions_cache[i] = bucket->p;
                }
 
+#ifndef USE_THREADS
                result = solveJoints(table, iter, positions_cache, nb_joints, earc->bcount, 0, 0, iarc->edges.first, nb_joints, angle_weight, length_weight, distance_weight);
-               
                min_cost = result->weight;
+#else
+               solveJoints(table, iter, positions_cache, nb_joints, earc->bcount, 0, 0, iarc->edges.first, nb_joints, angle_weight, length_weight, distance_weight);
+#endif
+
                copyMemoPositions(best_positions, table, earc->bcount, nb_joints);
-               
+
                MEM_freeN(table);
                MEM_freeN(positions_cache);
        }
@@ -2276,8 +2287,8 @@ static void retargetArctoArcAggresive(bContext *C, RigGraph *rigg, RigArc *iarc,
 
        /* set joints to best position */
        for (edge = iarc->edges.first, i = 0;
-                edge;
-                edge = edge->next, i++)
+            edge;
+            edge = edge->next, i++)
        {
                float *no = NULL;
                if (i < nb_joints)
@@ -2732,8 +2743,6 @@ static void retargetGraphs(bContext *C, RigGraph *rigg)
        BIF_flagMultiArcs(reebg, ARC_FREE);
        
        /* return to first level */
-       reebg = rigg->link_mesh;
-       
        inode = rigg->head;
        
        matchMultiResolutionStartingNode(rigg, reebg, inode);
@@ -2749,7 +2758,7 @@ static void retargetGraphs(bContext *C, RigGraph *rigg)
        ED_armature_from_edit(rigg->ob);
 }
 
-char *RIG_nameBone(RigGraph *rg, int arc_index, int bone_index)
+const char *RIG_nameBone(RigGraph *rg, int arc_index, int bone_index)
 {
        RigArc *arc = BLI_findlink(&rg->arcs, arc_index);
        RigEdge *iedge;
@@ -2794,7 +2803,7 @@ int RIG_nbJoints(RigGraph *rg)
        return total;
 }
 
-void BIF_freeRetarget()
+static void BIF_freeRetarget(void)
 {
        if (GLOBAL_RIGG)
        {
@@ -2826,7 +2835,7 @@ void BIF_retargetArmature(bContext *C)
                {
                        RigGraph *rigg;
                        bArmature *arm;
-                       
+
                        arm = ob->data;
                
                        /* Put the armature into editmode */
@@ -2889,7 +2898,6 @@ void BIF_retargetArc(bContext *C, ReebArc *earc, RigGraph *template_rigg)
        Object *ob;
        RigGraph *rigg;
        RigArc *iarc;
-       bArmature *arm;
        char *side_string = scene->toolsettings->skgen_side_string;
        char *num_string = scene->toolsettings->skgen_num_string;
        int free_template = 0;
@@ -2897,14 +2905,12 @@ void BIF_retargetArc(bContext *C, ReebArc *earc, RigGraph *template_rigg)
        if (template_rigg)
        {
                ob = template_rigg->ob;         
-               arm = ob->data;
        }
        else
        {
                free_template = 1;
-               ob = obedit;    
-               arm = ob->data;
-               template_rigg = armatureSelectedToGraph(C, ob, arm);
+               ob = obedit;
+               template_rigg = armatureSelectedToGraph(C, ob, ob->data);
        }
        
        if (template_rigg->arcs.first == NULL)