use BLI_strncpy and BLI_snprintf when the size of the string is known.
[blender.git] / source / blender / editors / armature / editarmature_retarget.c
index 16e78f7..9ec27b6 100644 (file)
@@ -1,6 +1,4 @@
-/**
- * $Id:
- *
+/*
  * ***** BEGIN GPL LICENSE BLOCK *****
  *
  * This program is free software; you can redistribute it and/or
@@ -15,7 +13,7 @@
  *
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software Foundation,
- * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  *
  * Contributor(s): Martin Poirier
  *
  * 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>
 #include <math.h> 
 #include <float.h>
 
-#ifdef HAVE_CONFIG_H
-#include <config.h>
-#endif
-
 #include "MEM_guardedalloc.h"
 
 #include "PIL_time.h"
 
-#include "DNA_ID.h"
-#include "DNA_action_types.h"
 #include "DNA_armature_types.h"
 #include "DNA_constraint_types.h"
-#include "DNA_mesh_types.h"
-#include "DNA_meshdata_types.h"
-#include "DNA_object_types.h"
 #include "DNA_scene_types.h"
-#include "DNA_view3d_types.h"
+#include "DNA_object_types.h"
 
 #include "BLI_blenlib.h"
-#include "BLI_arithb.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"
@@ -57,8 +52,6 @@
 
 //#include "BDR_editobject.h"
 
-#include "BKE_global.h"
-#include "BKE_utildefines.h"
 #include "BKE_constraint.h"
 #include "BKE_armature.h"
 #include "BKE_context.h"
@@ -68,7 +61,6 @@
 
 #include "BIF_retarget.h"
 
-#include "PIL_time.h"
 
 //#include "mydevice.h"
 #include "reeb.h" // FIX ME
@@ -109,7 +101,7 @@ typedef enum
        ARC_USED = 2
 } ArcUsageFlags;
 
-RigGraph *GLOBAL_RIGG = NULL;
+static RigGraph *GLOBAL_RIGG = NULL;
 
 /*******************************************************************************************************/
 
@@ -123,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;
@@ -139,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;
        
@@ -158,55 +150,55 @@ 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];
 
-       VecSubf(nor, bone->tail, bone->head);
+       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);
-       QuatMulVecf(qrot, new_up_axis);
+       copy_v3_v3(new_up_axis, old_up_axis);
+       mul_qt_v3(qrot, new_up_axis);
        
-       VecSubf(nor, bone->tail, bone->head);
+       sub_v3_v3v3(nor, bone->tail, bone->head);
        
-       Crossf(x_axis, nor, aligned_axis);
-       Crossf(z_axis, x_axis, nor);
+       cross_v3_v3v3(x_axis, nor, aligned_axis);
+       cross_v3_v3v3(z_axis, x_axis, nor);
        
-       Normalize(new_up_axis);
-       Normalize(x_axis);
-       Normalize(z_axis);
+       normalize_v3(new_up_axis);
+       normalize_v3(x_axis);
+       normalize_v3(z_axis);
        
-       if (Inpf(new_up_axis, x_axis) < 0)
+       if (dot_v3v3(new_up_axis, x_axis) < 0)
        {
-               VecMulf(x_axis, -1);
+               negate_v3(x_axis);
        }
        
-       if (Inpf(new_up_axis, z_axis) < 0)
+       if (dot_v3v3(new_up_axis, z_axis) < 0)
        {
-               VecMulf(z_axis, -1);
+               negate_v3(z_axis);
        }
        
-       if (NormalizedVecAngle2(x_axis, new_up_axis) < NormalizedVecAngle2(z_axis, new_up_axis))
+       if (angle_normalized_v3v3(x_axis, new_up_axis) < angle_normalized_v3v3(z_axis, new_up_axis))
        {
-               RotationBetweenVectorsToQuat(qroll, new_up_axis, x_axis); /* set roll rotation quat */
-               return ED_rollBoneToVector(bone, x_axis);
+               rotation_between_vecs_to_quat(qroll, new_up_axis, x_axis); /* set roll rotation quat */
+               return ED_rollBoneToVector(bone, x_axis, FALSE);
        }
        else
        {
-               RotationBetweenVectorsToQuat(qroll, new_up_axis, z_axis); /* set roll rotation quat */
-               return ED_rollBoneToVector(bone, z_axis);
+               rotation_between_vecs_to_quat(qroll, new_up_axis, z_axis); /* set roll rotation quat */
+               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)
        {
@@ -220,11 +212,11 @@ float rollBoneByQuatJoint(RigEdge *edge, RigEdge *previous, float qrot[4], float
                
                if (previous->bone)
                {
-                       VecSubf(vec_first, previous->bone->tail, previous->bone->head);
+                       sub_v3_v3v3(vec_first, previous->bone->tail, previous->bone->head);
                } 
                else if (previous->prev->bone)
                {
-                       VecSubf(vec_first, edge->bone->head, previous->prev->bone->tail);
+                       sub_v3_v3v3(vec_first, edge->bone->head, previous->prev->bone->tail);
                }
                else
                {
@@ -232,27 +224,27 @@ float rollBoneByQuatJoint(RigEdge *edge, RigEdge *previous, float qrot[4], float
                        return rollBoneByQuatAligned(edge->bone, edge->up_axis, qrot, qroll, up_axis);
                }
                
-               VecSubf(vec_second, edge->bone->tail, edge->bone->head);
+               sub_v3_v3v3(vec_second, edge->bone->tail, edge->bone->head);
        
-               Normalize(vec_first);
-               Normalize(vec_second);
+               normalize_v3(vec_first);
+               normalize_v3(vec_second);
                
-               Crossf(normal, vec_first, vec_second);
-               Normalize(normal);
+               cross_v3_v3v3(normal, vec_first, vec_second);
+               normalize_v3(normal);
                
-               AxisAngleToQuat(qroll, vec_second, edge->up_angle);
+               axis_angle_to_quat(qroll, vec_second, edge->up_angle);
                
-               QuatMulVecf(qroll, normal);
+               mul_qt_v3(qroll, normal);
                        
-               VECCOPY(new_up_axis, edge->up_axis);
-               QuatMulVecf(qrot, new_up_axis);
+               copy_v3_v3(new_up_axis, edge->up_axis);
+               mul_qt_v3(qrot, new_up_axis);
                
-               Normalize(new_up_axis);
+               normalize_v3(new_up_axis);
                
                /* real qroll between normal and up_axis */
-               RotationBetweenVectorsToQuat(qroll, new_up_axis, normal);
+               rotation_between_vecs_to_quat(qroll, new_up_axis, normal);
 
-               return ED_rollBoneToVector(edge->bone, normal);
+               return ED_rollBoneToVector(edge->bone, normal, FALSE);
        }
 }
 
@@ -260,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);
-       QuatMulVecf(qrot, new_up_axis);
+       copy_v3_v3(new_up_axis, old_up_axis);
+       mul_qt_v3(qrot, new_up_axis);
        
-       Normalize(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);
 }
@@ -318,7 +308,7 @@ void RIG_freeRigGraph(BGraph *rg)
 
 /************************************* ALLOCATORS ******************************************************/
 
-static RigGraph *newRigGraph()
+static RigGraph *newRigGraph(void)
 {
        RigGraph *rg;
        int totthread;
@@ -327,8 +317,8 @@ static RigGraph *newRigGraph()
        
        rg->head = NULL;
        
-       rg->bones_map = BLI_ghash_new(BLI_ghashutil_strhash, BLI_ghashutil_strcmp);
-       rg->controls_map = BLI_ghash_new(BLI_ghashutil_strhash, BLI_ghashutil_strcmp);
+       rg->bones_map = BLI_ghash_new(BLI_ghashutil_strhash, BLI_ghashutil_strcmp, "newRigGraph bones gh");
+       rg->controls_map = BLI_ghash_new(BLI_ghashutil_strhash, BLI_ghashutil_strcmp, "newRigGraph cont gh");
        
        rg->free_arc = RIG_freeRigArc;
        rg->free_node = NULL;
@@ -377,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;
        
@@ -386,7 +376,7 @@ static RigNode *newRigNodeHead(RigGraph *rg, RigArc *arc, float p[3])
        return node;
 }
 
-static void addRigNodeHead(RigGraph *rg, RigArc *arc, RigNode *node)
+static void addRigNodeHead(RigGraph *UNUSED(rg), RigArc *arc, RigNode *node)
 {
        node->degree++;
 
@@ -399,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;
        
@@ -422,16 +412,16 @@ 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);
        }
        
-       edge->length = VecLenf(edge->head, edge->tail);
+       edge->length = len_v3v3(edge->head, edge->tail);
        
        arc->length += edge->length;
        
@@ -444,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)
@@ -460,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] == '&')
                {
@@ -495,21 +485,21 @@ 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;
 
        renameTemplateBone(name, src_ctrl->bone->name, rg->editbones, side_string, num_string);
        ctrl->bone = duplicateEditBoneObjects(src_ctrl->bone, name, rg->editbones, src_rg->ob, rg->ob);
-       ctrl->bone->flag &= ~(BONE_TIPSEL|BONE_SELECTED|BONE_ROOTSEL|BONE_ACTIVE);
+       ctrl->bone->flag &= ~(BONE_TIPSEL|BONE_SELECTED|BONE_ROOTSEL);
        BLI_ghash_insert(ptr_hash, src_ctrl->bone, ctrl->bone);
        
        ctrl->link = src_ctrl->link;
@@ -541,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;
@@ -551,10 +541,10 @@ 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|BONE_ACTIVE);
+                       edge->bone->flag &= ~(BONE_TIPSEL|BONE_SELECTED|BONE_ROOTSEL);
                        BLI_ghash_insert(ptr_hash, src_edge->bone, edge->bone);
                }
 
@@ -572,7 +562,7 @@ static RigGraph *cloneRigGraph(RigGraph *src, ListBase *editbones, Object *ob, c
        RigControl *ctrl;
        RigGraph *rg;
        
-       ptr_hash = BLI_ghash_new(BLI_ghashutil_ptrhash, BLI_ghashutil_ptrcmp);
+       ptr_hash = BLI_ghash_new(BLI_ghashutil_ptrhash, BLI_ghashutil_ptrcmp, "cloneRigGraph gh");
 
        rg = newRigGraph();
        
@@ -675,22 +665,22 @@ static void RIG_calculateEdgeAngles(RigEdge *edge_first, RigEdge *edge_second)
 {
        float vec_first[3], vec_second[3];
        
-       VecSubf(vec_first, edge_first->tail, edge_first->head); 
-       VecSubf(vec_second, edge_second->tail, edge_second->head);
+       sub_v3_v3v3(vec_first, edge_first->tail, edge_first->head); 
+       sub_v3_v3v3(vec_second, edge_second->tail, edge_second->head);
 
-       Normalize(vec_first);
-       Normalize(vec_second);
+       normalize_v3(vec_first);
+       normalize_v3(vec_second);
        
-       edge_first->angle = NormalizedVecAngle2(vec_first, vec_second);
+       edge_first->angle = angle_normalized_v3v3(vec_first, vec_second);
        
        if (edge_second->bone != NULL)
        {
                float normal[3];
 
-               Crossf(normal, vec_first, vec_second);
-               Normalize(normal);
+               cross_v3_v3v3(normal, vec_first, vec_second);
+               normalize_v3(normal);
 
-               edge_second->up_angle = NormalizedVecAngle2(normal, edge_second->up_axis);
+               edge_second->up_angle = angle_normalized_v3v3(normal, edge_second->up_axis);
        }
 }
 
@@ -700,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;
        
@@ -715,28 +705,28 @@ static int RIG_parentControl(RigControl *ctrl, EditBone *link)
                float offset[3];
                int flag = 0;
                
-               VecSubf(offset, ctrl->bone->head, link->head);
+               sub_v3_v3v3(offset, ctrl->bone->head, link->head);
 
                /* if root matches, check for direction too */          
-               if (Inpf(offset, offset) < 0.0001)
+               if (dot_v3v3(offset, offset) < 0.0001f)
                {
                        float vbone[3], vparent[3];
                        
                        flag |= RIG_CTRL_FIT_ROOT;
                        
-                       VecSubf(vbone, ctrl->bone->tail, ctrl->bone->head);
-                       VecSubf(vparent, link->tail, link->head);
+                       sub_v3_v3v3(vbone, ctrl->bone->tail, ctrl->bone->head);
+                       sub_v3_v3v3(vparent, link->tail, link->head);
                        
                        /* test for opposite direction */
-                       if (Inpf(vbone, vparent) > 0)
+                       if (dot_v3v3(vbone, vparent) > 0)
                        {
                                float nor[3];
                                float len;
                                
-                               Crossf(nor, vbone, vparent);
+                               cross_v3_v3v3(nor, vbone, vparent);
                                
-                               len = Inpf(nor, nor);
-                               if (len < 0.0001)
+                               len = dot_v3v3(nor, nor);
+                               if (len < 0.0001f)
                                {
                                        flag |= RIG_CTRL_FIT_BONE;
                                }
@@ -775,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;
        }
@@ -869,8 +859,8 @@ static void RIG_reconnectControlBones(RigGraph *rg)
                                                {
                                                        int fit = 0;
                                                        
-                                                       fit = VecLenf(ctrl->bone->head, edge->bone->head) < 0.0001;
-                                                       fit = fit || VecLenf(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)
                                                        {
@@ -1026,13 +1016,13 @@ static void RIG_reconnectControlBones(RigGraph *rg)
                                /* don't link with parent */
                                if (bone->parent != ctrl->bone)
                                {
-                                       if (VecLenf(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 (VecLenf(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;
@@ -1132,14 +1122,14 @@ static void RIG_removeUneededOffsets(RigGraph *rg)
                
                if (first_edge->bone == NULL)
                {
-                       if (first_edge->bone == NULL && VecLenf(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)
                                {
@@ -1156,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);
                                        }
                                }
                        }
@@ -1232,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)
@@ -1262,14 +1252,14 @@ static void RIG_removeUneededOffsets(RigGraph *rg)
                
                if (last_edge->bone == NULL)
                {
-                       if (VecLenf(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)
                                {
@@ -1294,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;
                                        }
                                }
@@ -1427,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)
        {
@@ -1438,7 +1428,7 @@ void RIG_printNode(RigNode *node, char name[])
                else if (node->symmetry_flag & SYM_RADIAL)
                        printf("Symmetry RADIAL\n");
                        
-               printvecf("symmetry axis", node->symmetry_axis);
+               print_v3("symmetry axis", node->symmetry_axis);
        }
 }
 
@@ -1456,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);
-       printvecf(text, ctrl->offset);
+       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];
@@ -1544,18 +1534,17 @@ 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
        {
                rg->editbones = MEM_callocN(sizeof(ListBase), "EditBones");
-               make_boneList(rg->editbones, &arm->bonebase, NULL);
+               make_boneList(rg->editbones, &arm->bonebase, NULL, NULL);
                rg->flag |= RIG_FREE_BONELIST;
        }
        
@@ -1591,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)
@@ -1607,7 +1596,7 @@ RigGraph *armatureSelectedToGraph(bContext *C, Object *ob, bArmature *arm)
        else
        {
                rg->editbones = MEM_callocN(sizeof(ListBase), "EditBones");
-               make_boneList(rg->editbones, &arm->bonebase, NULL);
+               make_boneList(rg->editbones, &arm->bonebase, NULL, NULL);
                rg->flag |= RIG_FREE_BONELIST;
        }
 
@@ -1649,7 +1638,7 @@ static EditBone *add_editbonetolist(char *name, ListBase *list)
 {
        EditBone *bone= MEM_callocN(sizeof(EditBone), "eBone");
        
-       BLI_strncpy(bone->name, name, 32);
+       BLI_strncpy(bone->name, name, sizeof(bone->name));
        unique_editbone_name(list, bone->name, NULL);
        
        BLI_addtail(list, bone);
@@ -1670,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)
        {
@@ -1703,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)
@@ -1722,6 +1712,7 @@ void generateMissingArcs(RigGraph *rigg)
                }
        }
 }
+#endif
 
 /************************************ RETARGETTING *****************************************************/
 
@@ -1761,15 +1752,15 @@ static void finalizeControl(RigGraph *rigg, RigControl *ctrl, float resize)
                                tail_vec = ctrl->link_tail->head;
                        }
                        
-                       VecSubf(v1, ctrl->bone->tail, ctrl->bone->head);
-                       VecSubf(v2, tail_vec, ctrl->bone->head);
+                       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);
                        
-                       RotationBetweenVectorsToQuat(qtail, v1, v2);
-                       QuatMul(ctrl->qrot, qtail, ctrl->qrot);
+                       rotation_between_vecs_to_quat(qtail, v1, v2);
+                       mul_qt_qtqt(ctrl->qrot, qtail, ctrl->qrot);
                        
-                       resize = VecLength(v2) / VecLenf(ctrl->head, ctrl->tail);
+                       resize = len_v3(v2) / len_v3v3(ctrl->head, ctrl->tail);
                }
                
                ctrl->bone->roll = rollBoneByQuat(ctrl->bone, ctrl->up_axis, ctrl->qrot);
@@ -1796,27 +1787,27 @@ static void repositionTailControl(RigGraph *rigg, RigControl *ctrl)
        finalizeControl(rigg, ctrl, 1); /* resize will be recalculated anyway so we don't need it */
 }
 
-static void repositionControl(RigGraph *rigg, RigControl *ctrl, float head[3], float tail[3], float qrot[4], float resize)
+static void repositionControl(RigGraph *rigg, RigControl *ctrl, float head[3], float UNUSED(tail[3]), float qrot[4], float resize)
 {
        float parent_offset[3], tail_offset[3];
        
-       VECCOPY(parent_offset, ctrl->offset);
-       VecMulf(parent_offset, resize);
-       QuatMulVecf(qrot, parent_offset);
+       copy_v3_v3(parent_offset, ctrl->offset);
+       mul_v3_fl(parent_offset, resize);
+       mul_qt_v3(qrot, parent_offset);
        
-       VecAddf(ctrl->bone->head, head, parent_offset); 
+       add_v3_v3v3(ctrl->bone->head, head, parent_offset); 
 
        ctrl->flag |= RIG_CTRL_HEAD_DONE;
 
-       QUATCOPY(ctrl->qrot, qrot); 
+       copy_qt_qt(ctrl->qrot, qrot);
 
        if (ctrl->tail_mode == TL_NONE)
        {
-               VecSubf(tail_offset, ctrl->tail, ctrl->head);
-               VecMulf(tail_offset, resize);
-               QuatMulVecf(qrot, tail_offset);
+               sub_v3_v3v3(tail_offset, ctrl->tail, ctrl->head);
+               mul_v3_fl(tail_offset, resize);
+               mul_qt_v3(qrot, tail_offset);
 
-               VecAddf(ctrl->bone->tail, ctrl->bone->head, tail_offset);
+               add_v3_v3v3(ctrl->bone->tail, ctrl->bone->head, tail_offset);
                
                ctrl->flag |= RIG_CTRL_TAIL_DONE;
        }
@@ -1835,20 +1826,20 @@ static void repositionBone(bContext *C, RigGraph *rigg, RigEdge *edge, float vec
        
        bone = edge->bone;
        
-       VecSubf(v1, edge->tail, edge->head);
-       VecSubf(v2, vec1, vec0);
+       sub_v3_v3v3(v1, edge->tail, edge->head);
+       sub_v3_v3v3(v2, vec1, vec0);
        
-       l1 = Normalize(v1);
-       l2 = Normalize(v2);
+       l1 = normalize_v3(v1);
+       l2 = normalize_v3(v2);
 
        resize = l2 / l1;
        
-       RotationBetweenVectorsToQuat(qrot, v1, v2);
+       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 (!VecIsNull(up_axis))
+       if (!is_zero_v3(up_axis))
        {
                float qroll[4];
 
@@ -1862,10 +1853,10 @@ static void repositionBone(bContext *C, RigGraph *rigg, RigEdge *edge, float vec
                }
                else
                {
-                       QuatOne(qroll);
+                       unit_qt(qroll);
                }
                
-               QuatMul(qrot, qroll, qrot);
+               mul_qt_qtqt(qrot, qroll, qrot);
        }
        else
        {
@@ -1896,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;
        
        
@@ -1908,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)
@@ -1937,8 +1928,6 @@ static RetargetMode detectArcRetargetMode(RigArc *iarc)
                mode = RETARGET_LENGTH;
        }
        
-       mode = RETARGET_AGGRESSIVE;
-       
        return mode;
 }
 
@@ -1979,9 +1968,9 @@ static float costDistance(BArcIterator *iter, float *vec0, float *vec1, int i0,
 
        if (distance_weight > 0)
        {
-               VecSubf(v1, vec0, vec1);
+               sub_v3_v3v3(v1, vec0, vec1);
                
-               v1_inpf = Inpf(v1, v1);
+               v1_inpf = dot_v3v3(v1, v1);
                
                if (v1_inpf > 0)
                {
@@ -1992,11 +1981,11 @@ static float costDistance(BArcIterator *iter, float *vec0, float *vec1, int i0,
                                
                                bucket = IT_peek(iter, j);
        
-                               VecSubf(v2, bucket->p, vec1);
+                               sub_v3_v3v3(v2, bucket->p, vec1);
                
-                               Crossf(c, v1, v2);
+                               cross_v3_v3v3(c, v1, v2);
                                
-                               dist = Inpf(c, c) / v1_inpf;
+                               dist = dot_v3v3(c, c) / v1_inpf;
                                
                                max_dist = dist > max_dist ? dist : max_dist;
                        }
@@ -2020,15 +2009,15 @@ static float costAngle(float original_angle, float vec_first[3], float vec_secon
        {
                float current_angle;
                
-               if (!VecIsNull(vec_first) && !VecIsNull(vec_second))
+               if (!is_zero_v3(vec_first) && !is_zero_v3(vec_second))
                {
-                       current_angle = saacos(Inpf(vec_first, vec_second));
+                       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
@@ -2056,28 +2045,28 @@ static float calcCostLengthDistance(BArcIterator *iter, float **vec_cache, RigEd
        float vec[3];
        float length;
 
-       VecSubf(vec, vec2, vec1);
-       length = Normalize(vec);
+       sub_v3_v3v3(vec, vec2, vec1);
+       length = normalize_v3(vec);
 
        return costLength(edge->length, length) + costDistance(iter, vec1, vec2, i1, i2);
 }
 #endif
 
-static float calcCostAngleLengthDistance(BArcIterator *iter, float **vec_cache, RigEdge *edge, float *vec0, float *vec1, float *vec2, int i1, int i2, float angle_weight, float length_weight, float distance_weight)
+static float calcCostAngleLengthDistance(BArcIterator *iter, float **UNUSED(vec_cache), RigEdge *edge, float *vec0, float *vec1, float *vec2, int i1, int i2, float angle_weight, float length_weight, float distance_weight)
 {
        float vec_second[3], vec_first[3];
        float length2;
        float new_cost = 0;
 
-       VecSubf(vec_second, vec2, vec1);
-       length2 = Normalize(vec_second);
+       sub_v3_v3v3(vec_second, vec2, vec1);
+       length2 = normalize_v3(vec_second);
 
 
        /* Angle cost */        
        if (edge->prev)
        {
-               VecSubf(vec_first, vec1, vec0); 
-               Normalize(vec_first);
+               sub_v3_v3v3(vec_first, vec1, vec0); 
+               normalize_v3(vec_first);
                
                new_cost += costAngle(edge->prev->angle, vec_first, vec_second, angle_weight);
        }
@@ -2211,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);
@@ -2254,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;
                
@@ -2269,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);
        }
@@ -2290,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)
@@ -2352,13 +2349,13 @@ static void retargetArctoArcLength(bContext *C, RigGraph *rigg, RigArc *iarc, Ri
        {
                vec1 = bucket->p;
                
-               embedding_length += VecLenf(vec0, vec1);
+               embedding_length += len_v3v3(vec0, vec1);
                
                vec0 = vec1;
                bucket = IT_next(iter);
        }
        
-       embedding_length += VecLenf(node_end->p, vec1);
+       embedding_length += len_v3v3(node_end->p, vec1);
        
        /* fit bones */
        initArcIterator(iter, earc, node_start);
@@ -2377,7 +2374,7 @@ static void retargetArctoArcLength(bContext *C, RigGraph *rigg, RigArc *iarc, Ri
 
                while (bucket && new_bone_length > length)
                {
-                       length += VecLenf(previous_vec, vec1);
+                       length += len_v3v3(previous_vec, vec1);
                        bucket = IT_next(iter);
                        previous_vec = vec1;
                        vec1 = bucket->p;
@@ -2716,7 +2713,6 @@ static void finishRetarget(RigGraph *rigg)
 
 static void adjustGraphs(bContext *C, RigGraph *rigg)
 {
-       Scene *scene = CTX_data_scene(C);
        bArmature *arm= rigg->ob->data;
        RigArc *arc;
        
@@ -2739,7 +2735,6 @@ static void adjustGraphs(bContext *C, RigGraph *rigg)
 
 static void retargetGraphs(bContext *C, RigGraph *rigg)
 {
-       Scene *scene = CTX_data_scene(C);
        bArmature *arm= rigg->ob->data;
        ReebGraph *reebg = rigg->link_mesh;
        RigNode *inode;
@@ -2748,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);
@@ -2765,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;
@@ -2810,7 +2803,7 @@ int RIG_nbJoints(RigGraph *rg)
        return total;
 }
 
-void BIF_freeRetarget()
+static void BIF_freeRetarget(void)
 {
        if (GLOBAL_RIGG)
        {
@@ -2842,7 +2835,7 @@ void BIF_retargetArmature(bContext *C)
                {
                        RigGraph *rigg;
                        bArmature *arm;
-                       
+
                        arm = ob->data;
                
                        /* Put the armature into editmode */
@@ -2905,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;
@@ -2913,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)
@@ -2949,6 +2939,8 @@ void BIF_retargetArc(bContext *C, ReebArc *earc, RigGraph *template_rigg)
        }
        RIG_freeRigGraph((BGraph*)rigg);
        
+       ED_armature_validate_active(armedit);
+
 //     XXX
 //     allqueue(REDRAWVIEW3D, 0);
 }