2011-02-23 10:52:22 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								/*
							 | 
						
					
						
							
								
									
										
										
										
											2009-09-24 21:22:24 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								 * ***** BEGIN GPL LICENSE BLOCK *****
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								 *
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								 * This program is free software; you can redistribute it and/or
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								 * modify it under the terms of the GNU General Public License
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								 * as published by the Free Software Foundation; either version 2
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								 * of the License, or (at your option) any later version.
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								 *
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								 * This program is distributed in the hope that it will be useful,
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								 * but WITHOUT ANY WARRANTY; without even the implied warranty of
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								 * GNU General Public License for more details.
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								 *
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								 * You should have received a copy of the GNU General Public License
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								 * along with this program; if not, write to the Free Software Foundation,
							 | 
						
					
						
							
								
									
										
										
										
											2010-02-12 13:34:04 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
							 | 
						
					
						
							
								
									
										
										
										
											2009-09-24 21:22:24 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								 *
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								 * All rights reserved.
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								 *
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								 * The Original Code is: all of this file.
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								 *
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								 * Original author: Benoit Bolsee
							 | 
						
					
						
							
								
									
										
										
										
											2012-07-01 09:54:44 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								 * Contributor(s):
							 | 
						
					
						
							
								
									
										
										
										
											2009-09-24 21:22:24 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								 *
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								 * ***** END GPL LICENSE BLOCK *****
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								 */
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2011-02-27 20:24:49 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								/** \file blender/ikplugin/intern/iksolver_plugin.c
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								 *  \ingroup ikplugin
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								 */
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2009-09-24 21:22:24 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								#include "MEM_guardedalloc.h"
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								#include "BIK_api.h"
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								#include "BLI_blenlib.h"
							 | 
						
					
						
							
								
									
										
										
										
											2009-11-10 20:43:45 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								#include "BLI_math.h"
							 | 
						
					
						
							
								
									
										
										
										
											2011-01-07 18:36:47 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								#include "BLI_utildefines.h"
							 | 
						
					
						
							
								
									
										
										
										
											2009-09-24 21:22:24 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								#include "BKE_armature.h"
							 | 
						
					
						
							
								
									
										
										
										
											2009-09-25 01:29:30 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								#include "BKE_constraint.h"
							 | 
						
					
						
							
								
									
										
										
										
											2011-01-07 19:18:31 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2009-09-24 21:22:24 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								#include "DNA_object_types.h"
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								#include "DNA_action_types.h"
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								#include "DNA_constraint_types.h"
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								#include "DNA_armature_types.h"
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								#include "IK_solver.h"
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								#include "iksolver_plugin.h"
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2009-09-25 01:29:30 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								#include <string.h> /* memcpy */
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2009-09-24 21:22:24 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								/* ********************** THE IK SOLVER ******************* */
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								/* allocates PoseTree, and links that to root bone/channel */
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								/* Note: detecting the IK chain is duplicate code... in drawarmature.c and in transform_conversions.c */
							 | 
						
					
						
							
								
									
										
										
										
											2010-10-16 02:40:31 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								static void initialize_posetree(struct Object *UNUSED(ob), bPoseChannel *pchan_tip)
							 | 
						
					
						
							
								
									
										
										
										
											2009-09-24 21:22:24 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								{
							 | 
						
					
						
							
								
									
										
										
										
											2012-05-25 09:51:53 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
									bPoseChannel *curchan, *pchan_root = NULL, *chanlist[256], **oldchan;
							 | 
						
					
						
							
								
									
										
										
										
											2009-09-24 21:22:24 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
									PoseTree *tree;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
									PoseTarget *target;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
									bConstraint *con;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
									bKinematicConstraint *data;
							 | 
						
					
						
							
								
									
										
										
										
											2012-05-25 09:51:53 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
									int a, t, segcount = 0, size, newsize, *oldparent, parent;
							 | 
						
					
						
							
								
									
										
										
										
											2011-11-13 13:08:15 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2009-09-24 21:22:24 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
									/* find IK constraint, and validate it */
							 | 
						
					
						
							
								
									
										
										
										
											2012-05-25 09:51:53 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
									for (con = pchan_tip->constraints.first; con; con = con->next) {
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
										if (con->type == CONSTRAINT_TYPE_KINEMATIC) {
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
											data = (bKinematicConstraint *)con->data;
							 | 
						
					
						
							
								
									
										
										
										
											2009-09-24 21:22:24 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
											if (data->flag & CONSTRAINT_IK_AUTO) break;
							 | 
						
					
						
							
								
									
										
										
										
											2012-05-25 09:51:53 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
											if (data->tar == NULL) continue;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
											if (data->tar->type == OB_ARMATURE && data->subtarget[0] == 0) continue;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
											if ((con->flag & (CONSTRAINT_DISABLE | CONSTRAINT_OFF)) == 0 && (con->enforce != 0.0f)) break;
							 | 
						
					
						
							
								
									
										
										
										
											2009-09-24 21:22:24 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
										}
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
									}
							 | 
						
					
						
							
								
									
										
										
										
											2012-05-25 09:51:53 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
									if (con == NULL) return;
							 | 
						
					
						
							
								
									
										
										
										
											2012-07-01 09:54:44 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2009-09-24 21:22:24 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
									/* exclude tip from chain? */
							 | 
						
					
						
							
								
									
										
										
										
											2012-03-24 06:24:53 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
									if (!(data->flag & CONSTRAINT_IK_TIP))
							 | 
						
					
						
							
								
									
										
										
										
											2012-05-25 09:51:53 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
										pchan_tip = pchan_tip->parent;
							 | 
						
					
						
							
								
									
										
										
										
											2012-07-01 09:54:44 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2009-09-24 21:22:24 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
									/* Find the chain's root & count the segments needed */
							 | 
						
					
						
							
								
									
										
										
										
											2012-05-25 09:51:53 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
									for (curchan = pchan_tip; curchan; curchan = curchan->parent) {
							 | 
						
					
						
							
								
									
										
										
										
											2009-09-24 21:22:24 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
										pchan_root = curchan;
							 | 
						
					
						
							
								
									
										
										
										
											2012-07-01 09:54:44 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2012-05-25 09:51:53 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
										curchan->flag |= POSE_CHAIN;    // don't forget to clear this
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
										chanlist[segcount] = curchan;
							 | 
						
					
						
							
								
									
										
										
										
											2009-09-24 21:22:24 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
										segcount++;
							 | 
						
					
						
							
								
									
										
										
										
											2012-07-01 09:54:44 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2012-05-25 09:51:53 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
										if (segcount == data->rootbone || segcount > 255) break;  // 255 is weak
							 | 
						
					
						
							
								
									
										
										
										
											2009-09-24 21:22:24 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
									}
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
									if (!segcount) return;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
									/* setup the chain data */
							 | 
						
					
						
							
								
									
										
										
										
											2012-07-01 09:54:44 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2009-09-24 21:22:24 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
									/* we make tree-IK, unless all existing targets are in this chain */
							 | 
						
					
						
							
								
									
										
										
										
											2012-05-25 09:51:53 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
									for (tree = pchan_root->iktree.first; tree; tree = tree->next) {
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
										for (target = tree->targets.first; target; target = target->next) {
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
											curchan = tree->pchan[target->tip];
							 | 
						
					
						
							
								
									
										
										
										
											2012-03-24 06:24:53 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
											if (curchan->flag & POSE_CHAIN)
							 | 
						
					
						
							
								
									
										
										
										
											2009-09-24 21:22:24 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
												curchan->flag &= ~POSE_CHAIN;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
											else
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
												break;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
										}
							 | 
						
					
						
							
								
									
										
										
										
											2012-03-24 06:24:53 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
										if (target) break;
							 | 
						
					
						
							
								
									
										
										
										
											2009-09-24 21:22:24 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
									}
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
									/* create a target */
							 | 
						
					
						
							
								
									
										
										
										
											2012-05-25 09:51:53 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
									target = MEM_callocN(sizeof(PoseTarget), "posetarget");
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
									target->con = con;
							 | 
						
					
						
							
								
									
										
										
										
											2009-09-24 21:22:24 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
									pchan_tip->flag &= ~POSE_CHAIN;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2012-05-25 09:51:53 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
									if (tree == NULL) {
							 | 
						
					
						
							
								
									
										
										
										
											2009-09-24 21:22:24 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
										/* make new tree */
							 | 
						
					
						
							
								
									
										
										
										
											2012-05-25 09:51:53 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
										tree = MEM_callocN(sizeof(PoseTree), "posetree");
							 | 
						
					
						
							
								
									
										
										
										
											2011-11-13 13:08:15 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2012-05-25 09:51:53 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
										tree->type = CONSTRAINT_TYPE_KINEMATIC;
							 | 
						
					
						
							
								
									
										
										
										
											2012-07-01 09:54:44 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2012-05-25 09:51:53 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
										tree->iterations = data->iterations;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
										tree->totchannel = segcount;
							 | 
						
					
						
							
								
									
										
										
										
											2009-09-24 21:22:24 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
										tree->stretch = (data->flag & CONSTRAINT_IK_STRETCH);
							 | 
						
					
						
							
								
									
										
										
										
											2012-07-01 09:54:44 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2012-05-25 09:51:53 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
										tree->pchan = MEM_callocN(segcount * sizeof(void *), "ik tree pchan");
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
										tree->parent = MEM_callocN(segcount * sizeof(int), "ik tree parent");
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
										for (a = 0; a < segcount; a++) {
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
											tree->pchan[a] = chanlist[segcount - a - 1];
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
											tree->parent[a] = a - 1;
							 | 
						
					
						
							
								
									
										
										
										
											2009-09-24 21:22:24 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
										}
							 | 
						
					
						
							
								
									
										
										
										
											2012-05-25 09:51:53 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
										target->tip = segcount - 1;
							 | 
						
					
						
							
								
									
										
										
										
											2012-07-01 09:54:44 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2009-09-24 21:22:24 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
										/* AND! link the tree to the root */
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
										BLI_addtail(&pchan_root->iktree, tree);
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
									}
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
									else {
							 | 
						
					
						
							
								
									
										
										
										
											2012-05-25 09:51:53 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
										tree->iterations = MAX2(data->iterations, tree->iterations);
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
										tree->stretch = tree->stretch && !(data->flag & CONSTRAINT_IK_STRETCH);
							 | 
						
					
						
							
								
									
										
										
										
											2009-09-24 21:22:24 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
										/* skip common pose channels and add remaining*/
							 | 
						
					
						
							
								
									
										
										
										
											2012-05-25 09:51:53 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
										size = MIN2(segcount, tree->totchannel);
							 | 
						
					
						
							
								
									
										
										
										
											2011-11-13 13:08:15 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
										a = t = 0;
							 | 
						
					
						
							
								
									
										
										
										
											2012-05-25 09:51:53 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
										while (a < size && t < tree->totchannel) {
							 | 
						
					
						
							
								
									
										
										
										
											2012-07-07 22:51:57 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
											/* locate first matching channel */
							 | 
						
					
						
							
								
									
										
										
										
											2012-05-25 09:51:53 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
											for (; t < tree->totchannel && tree->pchan[t] != chanlist[segcount - a - 1]; t++) ;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
											if (t >= tree->totchannel)
							 | 
						
					
						
							
								
									
										
										
										
											2011-11-13 13:08:15 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
												break;
							 | 
						
					
						
							
								
									
										
										
										
											2012-05-25 09:51:53 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
											for (; a < size && t < tree->totchannel && tree->pchan[t] == chanlist[segcount - a - 1]; a++, t++) ;
							 | 
						
					
						
							
								
									
										
										
										
											2011-11-13 13:08:15 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
										}
							 | 
						
					
						
							
								
									
										
										
										
											2009-09-24 21:22:24 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2012-05-25 09:51:53 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
										segcount = segcount - a;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
										target->tip = tree->totchannel + segcount - 1;
							 | 
						
					
						
							
								
									
										
										
										
											2009-09-24 21:22:24 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
										if (segcount > 0) {
							 | 
						
					
						
							
								
									
										
										
										
											2012-03-24 06:24:53 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
											for (parent = a - 1; parent < tree->totchannel; parent++)
							 | 
						
					
						
							
								
									
										
										
										
											2012-05-25 09:51:53 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
												if (tree->pchan[parent] == chanlist[segcount - 1]->parent)
							 | 
						
					
						
							
								
									
										
										
										
											2011-11-13 13:08:15 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
													break;
							 | 
						
					
						
							
								
									
										
										
										
											2012-07-01 09:54:44 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2011-11-13 13:08:15 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
											/* shouldn't happen, but could with dependency cycles */
							 | 
						
					
						
							
								
									
										
										
										
											2012-03-24 06:24:53 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
											if (parent == tree->totchannel)
							 | 
						
					
						
							
								
									
										
										
										
											2011-11-13 13:08:15 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
												parent = a - 1;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2009-09-24 21:22:24 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
											/* resize array */
							 | 
						
					
						
							
								
									
										
										
										
											2012-05-25 09:51:53 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
											newsize = tree->totchannel + segcount;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
											oldchan = tree->pchan;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
											oldparent = tree->parent;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
											tree->pchan = MEM_callocN(newsize * sizeof(void *), "ik tree pchan");
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
											tree->parent = MEM_callocN(newsize * sizeof(int), "ik tree parent");
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
											memcpy(tree->pchan, oldchan, sizeof(void *) * tree->totchannel);
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
											memcpy(tree->parent, oldparent, sizeof(int) * tree->totchannel);
							 | 
						
					
						
							
								
									
										
										
										
											2009-09-24 21:22:24 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
											MEM_freeN(oldchan);
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
											MEM_freeN(oldparent);
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
											/* add new pose channels at the end, in reverse order */
							 | 
						
					
						
							
								
									
										
										
										
											2012-05-25 09:51:53 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
											for (a = 0; a < segcount; a++) {
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
												tree->pchan[tree->totchannel + a] = chanlist[segcount - a - 1];
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
												tree->parent[tree->totchannel + a] = tree->totchannel + a - 1;
							 | 
						
					
						
							
								
									
										
										
										
											2009-09-24 21:22:24 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
											}
							 | 
						
					
						
							
								
									
										
										
										
											2012-05-25 09:51:53 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
											tree->parent[tree->totchannel] = parent;
							 | 
						
					
						
							
								
									
										
										
										
											2012-07-01 09:54:44 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2012-05-25 09:51:53 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
											tree->totchannel = newsize;
							 | 
						
					
						
							
								
									
										
										
										
											2009-09-24 21:22:24 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
										}
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
										/* move tree to end of list, for correct evaluation order */
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
										BLI_remlink(&pchan_root->iktree, tree);
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
										BLI_addtail(&pchan_root->iktree, tree);
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
									}
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
									/* add target to the tree */
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
									BLI_addtail(&tree->targets, target);
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
									/* mark root channel having an IK tree */
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
									pchan_root->flag |= POSE_IKTREE;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								}
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								/* transform from bone(b) to bone(b+1), store in chan_mat */
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								static void make_dmats(bPoseChannel *pchan)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								{
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
									if (pchan->parent) {
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
										float iR_parmat[4][4];
							 | 
						
					
						
							
								
									
										
										
										
											2009-11-10 20:43:45 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
										invert_m4_m4(iR_parmat, pchan->parent->pose_mat);
							 | 
						
					
						
							
								
									
										
										
										
											2012-05-25 09:51:53 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
										mult_m4_m4m4(pchan->chan_mat, iR_parmat,  pchan->pose_mat); // delta mat
							 | 
						
					
						
							
								
									
										
										
										
											2009-09-24 21:22:24 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
									}
							 | 
						
					
						
							
								
									
										
										
										
											2009-11-10 20:43:45 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
									else copy_m4_m4(pchan->chan_mat, pchan->pose_mat);
							 | 
						
					
						
							
								
									
										
										
										
											2009-09-24 21:22:24 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								}
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								/* applies IK matrix to pchan, IK is done separated */
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								/* formula: pose_mat(b) = pose_mat(b-1) * diffmat(b-1, b) * ik_mat(b) */
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								/* to make this work, the diffmats have to be precalculated! Stored in chan_mat */
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								static void where_is_ik_bone(bPoseChannel *pchan, float ik_mat[][3])   // nr = to detect if this is first bone
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								{
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
									float vec[3], ikmat[4][4];
							 | 
						
					
						
							
								
									
										
										
										
											2012-07-01 09:54:44 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2009-11-10 20:43:45 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
									copy_m4_m3(ikmat, ik_mat);
							 | 
						
					
						
							
								
									
										
										
										
											2012-07-01 09:54:44 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2009-09-24 21:22:24 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
									if (pchan->parent)
							 | 
						
					
						
							
								
									
										
										
										
											2009-11-10 20:43:45 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
										mul_serie_m4(pchan->pose_mat, pchan->parent->pose_mat, pchan->chan_mat, ikmat, NULL, NULL, NULL, NULL, NULL);
							 | 
						
					
						
							
								
									
										
										
										
											2012-07-01 09:54:44 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
									else
							 | 
						
					
						
							
								
									
										
										
											
												Math lib: matrix multiplication order fix for two functions that were
inconsistent with similar functions & math notation:
mul_m4_m4m4(R, B, A) => mult_m4_m4m4(R, A, B)
mul_m3_m3m4(R, B, A) => mult_m3_m3m4(R, A, B)
For branch maintainers, it should be relatively simple to fix things manually,
it's also possible run this script after merging to do automatic replacement:
http://www.pasteall.org/27459/python
											
										 
										
											2011-12-16 19:53:12 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
										mult_m4_m4m4(pchan->pose_mat, pchan->chan_mat, ikmat);
							 | 
						
					
						
							
								
									
										
										
										
											2009-09-24 21:22:24 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
									/* calculate head */
							 | 
						
					
						
							
								
									
										
										
										
											2011-11-07 01:38:32 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
									copy_v3_v3(pchan->pose_head, pchan->pose_mat[3]);
							 | 
						
					
						
							
								
									
										
										
										
											2009-09-24 21:22:24 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
									/* calculate tail */
							 | 
						
					
						
							
								
									
										
										
										
											2011-11-07 01:38:32 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
									copy_v3_v3(vec, pchan->pose_mat[1]);
							 | 
						
					
						
							
								
									
										
										
										
											2009-11-10 20:43:45 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
									mul_v3_fl(vec, pchan->bone->length);
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
									add_v3_v3v3(pchan->pose_tail, pchan->pose_head, vec);
							 | 
						
					
						
							
								
									
										
										
										
											2009-09-24 21:22:24 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
									pchan->flag |= POSE_DONE;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								}
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2012-05-05 16:03:57 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								/* called from within the core BKE_pose_where_is loop, all animsystems and constraints
							 | 
						
					
						
							
								
									
										
										
										
											2012-03-09 18:28:30 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								 * were executed & assigned. Now as last we do an IK pass */
							 | 
						
					
						
							
								
									
										
										
										
											2009-10-12 09:39:57 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								static void execute_posetree(struct Scene *scene, Object *ob, PoseTree *tree)
							 | 
						
					
						
							
								
									
										
										
										
											2009-09-24 21:22:24 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								{
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
									float R_parmat[3][3], identity[3][3];
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
									float iR_parmat[3][3];
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
									float R_bonemat[3][3];
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
									float goalrot[3][3], goalpos[3];
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
									float rootmat[4][4], imat[4][4];
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
									float goal[4][4], goalinv[4][4];
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
									float irest_basis[3][3], full_basis[3][3];
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
									float end_pose[4][4], world_pose[4][4];
							 | 
						
					
						
							
								
									
										
										
										
											2012-05-25 09:51:53 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
									float length, basis[3][3], rest_basis[3][3], start[3], *ikstretch = NULL;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
									float resultinf = 0.0f;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
									int a, flag, hasstretch = 0, resultblend = 0;
							 | 
						
					
						
							
								
									
										
										
										
											2009-09-24 21:22:24 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
									bPoseChannel *pchan;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
									IK_Segment *seg, *parent, **iktree, *iktarget;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
									IK_Solver *solver;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
									PoseTarget *target;
							 | 
						
					
						
							
								
									
										
										
										
											2012-05-25 09:51:53 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
									bKinematicConstraint *data, *poleangledata = NULL;
							 | 
						
					
						
							
								
									
										
										
										
											2009-09-24 21:22:24 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
									Bone *bone;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
									if (tree->totchannel == 0)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
										return;
							 | 
						
					
						
							
								
									
										
										
										
											2012-07-01 09:54:44 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2012-05-25 09:51:53 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
									iktree = MEM_mallocN(sizeof(void *) * tree->totchannel, "ik tree");
							 | 
						
					
						
							
								
									
										
										
										
											2009-09-24 21:22:24 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2012-05-25 09:51:53 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
									for (a = 0; a < tree->totchannel; a++) {
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
										pchan = tree->pchan[a];
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
										bone = pchan->bone;
							 | 
						
					
						
							
								
									
										
										
										
											2012-07-01 09:54:44 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2009-09-24 21:22:24 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
										/* set DoF flag */
							 | 
						
					
						
							
								
									
										
										
										
											2012-05-25 09:51:53 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
										flag = 0;
							 | 
						
					
						
							
								
									
										
										
										
											2012-03-24 06:24:53 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
										if (!(pchan->ikflag & BONE_IK_NO_XDOF) && !(pchan->ikflag & BONE_IK_NO_XDOF_TEMP))
							 | 
						
					
						
							
								
									
										
										
										
											2009-09-24 21:22:24 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
											flag |= IK_XDOF;
							 | 
						
					
						
							
								
									
										
										
										
											2012-03-24 06:24:53 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
										if (!(pchan->ikflag & BONE_IK_NO_YDOF) && !(pchan->ikflag & BONE_IK_NO_YDOF_TEMP))
							 | 
						
					
						
							
								
									
										
										
										
											2009-09-24 21:22:24 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
											flag |= IK_YDOF;
							 | 
						
					
						
							
								
									
										
										
										
											2012-03-24 06:24:53 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
										if (!(pchan->ikflag & BONE_IK_NO_ZDOF) && !(pchan->ikflag & BONE_IK_NO_ZDOF_TEMP))
							 | 
						
					
						
							
								
									
										
										
										
											2009-09-24 21:22:24 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
											flag |= IK_ZDOF;
							 | 
						
					
						
							
								
									
										
										
										
											2012-07-01 09:54:44 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2012-03-24 06:24:53 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
										if (tree->stretch && (pchan->ikstretch > 0.0f)) {
							 | 
						
					
						
							
								
									
										
										
										
											2009-09-24 21:22:24 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
											flag |= IK_TRANS_YDOF;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
											hasstretch = 1;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
										}
							 | 
						
					
						
							
								
									
										
										
										
											2012-07-01 09:54:44 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2012-05-25 09:51:53 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
										seg = iktree[a] = IK_CreateSegment(flag);
							 | 
						
					
						
							
								
									
										
										
										
											2012-07-01 09:54:44 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2009-09-24 21:22:24 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
										/* find parent */
							 | 
						
					
						
							
								
									
										
										
										
											2012-03-24 06:24:53 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
										if (a == 0)
							 | 
						
					
						
							
								
									
										
										
										
											2012-05-25 09:51:53 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
											parent = NULL;
							 | 
						
					
						
							
								
									
										
										
										
											2009-09-24 21:22:24 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
										else
							 | 
						
					
						
							
								
									
										
										
										
											2012-05-25 09:51:53 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
											parent = iktree[tree->parent[a]];
							 | 
						
					
						
							
								
									
										
										
										
											2012-07-01 09:54:44 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2009-09-24 21:22:24 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
										IK_SetParent(seg, parent);
							 | 
						
					
						
							
								
									
										
										
										
											2012-07-01 09:54:44 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2009-09-24 21:22:24 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
										/* get the matrix that transforms from prevbone into this bone */
							 | 
						
					
						
							
								
									
										
										
										
											2009-11-10 20:43:45 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
										copy_m3_m4(R_bonemat, pchan->pose_mat);
							 | 
						
					
						
							
								
									
										
										
										
											2012-07-01 09:54:44 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2009-09-24 21:22:24 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
										/* gather transformations for this IK segment */
							 | 
						
					
						
							
								
									
										
										
										
											2012-07-01 09:54:44 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2009-09-24 21:22:24 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
										if (pchan->parent)
							 | 
						
					
						
							
								
									
										
										
										
											2009-11-10 20:43:45 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
											copy_m3_m4(R_parmat, pchan->parent->pose_mat);
							 | 
						
					
						
							
								
									
										
										
										
											2009-09-24 21:22:24 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
										else
							 | 
						
					
						
							
								
									
										
										
										
											2009-11-10 20:43:45 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
											unit_m3(R_parmat);
							 | 
						
					
						
							
								
									
										
										
										
											2012-07-01 09:54:44 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2009-09-24 21:22:24 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
										/* bone offset */
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
										if (pchan->parent && (a > 0))
							 | 
						
					
						
							
								
									
										
										
										
											2009-11-10 20:43:45 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
											sub_v3_v3v3(start, pchan->pose_head, pchan->parent->pose_tail);
							 | 
						
					
						
							
								
									
										
										
										
											2009-09-24 21:22:24 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
										else
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
											/* only root bone (a = 0) has no parent */
							 | 
						
					
						
							
								
									
										
										
										
											2012-05-25 09:51:53 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
											start[0] = start[1] = start[2] = 0.0f;
							 | 
						
					
						
							
								
									
										
										
										
											2012-07-01 09:54:44 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2009-09-24 21:22:24 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
										/* change length based on bone size */
							 | 
						
					
						
							
								
									
										
										
										
											2012-05-25 09:51:53 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
										length = bone->length * len_v3(R_bonemat[1]);
							 | 
						
					
						
							
								
									
										
										
										
											2012-07-01 09:54:44 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2009-09-24 21:22:24 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
										/* compute rest basis and its inverse */
							 | 
						
					
						
							
								
									
										
										
										
											2009-11-10 20:43:45 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
										copy_m3_m3(rest_basis, bone->bone_mat);
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
										copy_m3_m3(irest_basis, bone->bone_mat);
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
										transpose_m3(irest_basis);
							 | 
						
					
						
							
								
									
										
										
										
											2012-07-01 09:54:44 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2009-09-24 21:22:24 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
										/* compute basis with rest_basis removed */
							 | 
						
					
						
							
								
									
										
										
										
											2009-11-10 20:43:45 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
										invert_m3_m3(iR_parmat, R_parmat);
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
										mul_m3_m3m3(full_basis, iR_parmat, R_bonemat);
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
										mul_m3_m3m3(basis, irest_basis, full_basis);
							 | 
						
					
						
							
								
									
										
										
										
											2012-07-01 09:54:44 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2009-09-24 21:22:24 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
										/* basis must be pure rotation */
							 | 
						
					
						
							
								
									
										
										
										
											2009-11-10 20:43:45 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
										normalize_m3(basis);
							 | 
						
					
						
							
								
									
										
										
										
											2012-07-01 09:54:44 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2009-09-24 21:22:24 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
										/* transform offset into local bone space */
							 | 
						
					
						
							
								
									
										
										
										
											2009-11-10 20:43:45 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
										normalize_m3(iR_parmat);
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
										mul_m3_v3(iR_parmat, start);
							 | 
						
					
						
							
								
									
										
										
										
											2012-07-01 09:54:44 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2009-09-24 21:22:24 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
										IK_SetTransform(seg, start, rest_basis, basis, length);
							 | 
						
					
						
							
								
									
										
										
										
											2012-07-01 09:54:44 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2009-09-24 21:22:24 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
										if (pchan->ikflag & BONE_IK_XLIMIT)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
											IK_SetLimit(seg, IK_X, pchan->limitmin[0], pchan->limitmax[0]);
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
										if (pchan->ikflag & BONE_IK_YLIMIT)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
											IK_SetLimit(seg, IK_Y, pchan->limitmin[1], pchan->limitmax[1]);
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
										if (pchan->ikflag & BONE_IK_ZLIMIT)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
											IK_SetLimit(seg, IK_Z, pchan->limitmin[2], pchan->limitmax[2]);
							 | 
						
					
						
							
								
									
										
										
										
											2012-07-01 09:54:44 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2009-09-24 21:22:24 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
										IK_SetStiffness(seg, IK_X, pchan->stiffness[0]);
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
										IK_SetStiffness(seg, IK_Y, pchan->stiffness[1]);
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
										IK_SetStiffness(seg, IK_Z, pchan->stiffness[2]);
							 | 
						
					
						
							
								
									
										
										
										
											2012-07-01 09:54:44 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2012-03-24 06:24:53 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
										if (tree->stretch && (pchan->ikstretch > 0.0f)) {
							 | 
						
					
						
							
								
									
										
										
										
											2012-07-29 17:49:14 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
											const float ikstretch = pchan->ikstretch * pchan->ikstretch;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
											/* this function does its own clamping */
							 | 
						
					
						
							
								
									
										
										
										
											2012-07-27 23:16:33 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
											IK_SetStiffness(seg, IK_TRANS_Y, 1.0f - ikstretch);
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
											IK_SetLimit(seg, IK_TRANS_Y, IK_STRETCH_STIFF_MIN, IK_STRETCH_STIFF_MAX);
							 | 
						
					
						
							
								
									
										
										
										
											2009-09-24 21:22:24 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
										}
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
									}
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2012-05-25 09:51:53 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
									solver = IK_CreateSolver(iktree[0]);
							 | 
						
					
						
							
								
									
										
										
										
											2009-09-24 21:22:24 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
									/* set solver goals */
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
									/* first set the goal inverse transform, assuming the root of tree was done ok! */
							 | 
						
					
						
							
								
									
										
										
										
											2012-05-25 09:51:53 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
									pchan = tree->pchan[0];
							 | 
						
					
						
							
								
									
										
										
										
											2012-04-20 17:26:40 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
									if (pchan->parent) {
							 | 
						
					
						
							
								
									
										
										
										
											2009-09-24 21:22:24 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
										/* transform goal by parent mat, so this rotation is not part of the
							 | 
						
					
						
							
								
									
										
										
										
											2012-03-09 18:28:30 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
										 * segment's basis. otherwise rotation limits do not work on the
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
										 * local transform of the segment itself. */
							 | 
						
					
						
							
								
									
										
										
										
											2009-11-10 20:43:45 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
										copy_m4_m4(rootmat, pchan->parent->pose_mat);
							 | 
						
					
						
							
								
									
										
										
										
											2012-04-20 17:26:40 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
										/* However, we do not want to get (i.e. reverse) parent's scale, as it generates [#31008]
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
										 * kind of nasty bugs... */
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
										normalize_m4(rootmat);
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
									}
							 | 
						
					
						
							
								
									
										
										
										
											2009-09-24 21:22:24 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
									else
							 | 
						
					
						
							
								
									
										
										
										
											2009-11-10 20:43:45 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
										unit_m4(rootmat);
							 | 
						
					
						
							
								
									
										
										
										
											2011-11-07 01:38:32 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
									copy_v3_v3(rootmat[3], pchan->pose_head);
							 | 
						
					
						
							
								
									
										
										
										
											2012-07-01 09:54:44 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
											
												Math lib: matrix multiplication order fix for two functions that were
inconsistent with similar functions & math notation:
mul_m4_m4m4(R, B, A) => mult_m4_m4m4(R, A, B)
mul_m3_m3m4(R, B, A) => mult_m3_m3m4(R, A, B)
For branch maintainers, it should be relatively simple to fix things manually,
it's also possible run this script after merging to do automatic replacement:
http://www.pasteall.org/27459/python
											
										 
										
											2011-12-16 19:53:12 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
									mult_m4_m4m4(imat, ob->obmat, rootmat);
							 | 
						
					
						
							
								
									
										
										
										
											2009-11-10 20:43:45 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
									invert_m4_m4(goalinv, imat);
							 | 
						
					
						
							
								
									
										
										
										
											2012-07-01 09:54:44 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2012-05-25 09:51:53 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
									for (target = tree->targets.first; target; target = target->next) {
							 | 
						
					
						
							
								
									
										
										
										
											2009-09-24 21:22:24 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
										float polepos[3];
							 | 
						
					
						
							
								
									
										
										
										
											2012-05-25 09:51:53 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
										int poleconstrain = 0;
							 | 
						
					
						
							
								
									
										
										
										
											2012-07-01 09:54:44 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2012-05-25 09:51:53 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
										data = (bKinematicConstraint *)target->con->data;
							 | 
						
					
						
							
								
									
										
										
										
											2012-07-01 09:54:44 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2009-09-24 21:22:24 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
										/* 1.0=ctime, we pass on object for auto-ik (owner-type here is object, even though
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
										 * strictly speaking, it is a posechannel)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
										 */
							 | 
						
					
						
							
								
									
										
										
										
											2009-10-12 09:39:57 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
										get_constraint_target_matrix(scene, target->con, 0, CONSTRAINT_OBTYPE_OBJECT, ob, rootmat, 1.0);
							 | 
						
					
						
							
								
									
										
										
										
											2012-07-01 09:54:44 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2009-09-24 21:22:24 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
										/* and set and transform goal */
							 | 
						
					
						
							
								
									
										
										
											
												Math lib: matrix multiplication order fix for two functions that were
inconsistent with similar functions & math notation:
mul_m4_m4m4(R, B, A) => mult_m4_m4m4(R, A, B)
mul_m3_m3m4(R, B, A) => mult_m3_m3m4(R, A, B)
For branch maintainers, it should be relatively simple to fix things manually,
it's also possible run this script after merging to do automatic replacement:
http://www.pasteall.org/27459/python
											
										 
										
											2011-12-16 19:53:12 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
										mult_m4_m4m4(goal, goalinv, rootmat);
							 | 
						
					
						
							
								
									
										
										
										
											2012-07-01 09:54:44 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2011-11-07 01:38:32 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
										copy_v3_v3(goalpos, goal[3]);
							 | 
						
					
						
							
								
									
										
										
										
											2009-11-10 20:43:45 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
										copy_m3_m4(goalrot, goal);
							 | 
						
					
						
							
								
									
										
										
										
											2012-11-24 00:18:34 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
										normalize_m3(goalrot);
							 | 
						
					
						
							
								
									
										
										
										
											2012-07-01 09:54:44 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2009-09-24 21:22:24 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
										/* same for pole vector target */
							 | 
						
					
						
							
								
									
										
										
										
											2012-03-24 06:24:53 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
										if (data->poletar) {
							 | 
						
					
						
							
								
									
										
										
										
											2009-10-12 09:39:57 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
											get_constraint_target_matrix(scene, target->con, 1, CONSTRAINT_OBTYPE_OBJECT, ob, rootmat, 1.0);
							 | 
						
					
						
							
								
									
										
										
										
											2012-07-01 09:54:44 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2012-03-24 06:24:53 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
											if (data->flag & CONSTRAINT_IK_SETANGLE) {
							 | 
						
					
						
							
								
									
										
										
										
											2009-09-24 21:22:24 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
												/* don't solve IK when we are setting the pole angle */
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
												break;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
											}
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
											else {
							 | 
						
					
						
							
								
									
										
										
											
												Math lib: matrix multiplication order fix for two functions that were
inconsistent with similar functions & math notation:
mul_m4_m4m4(R, B, A) => mult_m4_m4m4(R, A, B)
mul_m3_m3m4(R, B, A) => mult_m3_m3m4(R, A, B)
For branch maintainers, it should be relatively simple to fix things manually,
it's also possible run this script after merging to do automatic replacement:
http://www.pasteall.org/27459/python
											
										 
										
											2011-12-16 19:53:12 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
												mult_m4_m4m4(goal, goalinv, rootmat);
							 | 
						
					
						
							
								
									
										
										
										
											2011-11-07 01:38:32 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
												copy_v3_v3(polepos, goal[3]);
							 | 
						
					
						
							
								
									
										
										
										
											2012-05-25 09:51:53 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
												poleconstrain = 1;
							 | 
						
					
						
							
								
									
										
										
										
											2009-09-24 21:22:24 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
												/* for pole targets, we blend the result of the ik solver
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
												 * instead of the target position, otherwise we can't get
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
												 * a smooth transition */
							 | 
						
					
						
							
								
									
										
										
										
											2012-05-25 09:51:53 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
												resultblend = 1;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
												resultinf = target->con->enforce;
							 | 
						
					
						
							
								
									
										
										
										
											2012-07-01 09:54:44 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2012-03-24 06:24:53 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
												if (data->flag & CONSTRAINT_IK_GETANGLE) {
							 | 
						
					
						
							
								
									
										
										
										
											2012-05-25 09:51:53 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
													poleangledata = data;
							 | 
						
					
						
							
								
									
										
										
										
											2009-09-24 21:22:24 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
													data->flag &= ~CONSTRAINT_IK_GETANGLE;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
												}
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
											}
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
										}
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
										/* do we need blending? */
							 | 
						
					
						
							
								
									
										
										
										
											2011-11-11 12:00:08 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
										if (!resultblend && target->con->enforce != 1.0f) {
							 | 
						
					
						
							
								
									
										
										
										
											2009-09-24 21:22:24 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
											float q1[4], q2[4], q[4];
							 | 
						
					
						
							
								
									
										
										
										
											2012-05-25 09:51:53 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
											float fac = target->con->enforce;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
											float mfac = 1.0f - fac;
							 | 
						
					
						
							
								
									
										
										
										
											2012-07-01 09:54:44 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2012-05-25 09:51:53 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
											pchan = tree->pchan[target->tip];
							 | 
						
					
						
							
								
									
										
										
										
											2012-07-01 09:54:44 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2009-09-24 21:22:24 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
											/* end effector in world space */
							 | 
						
					
						
							
								
									
										
										
										
											2009-11-10 20:43:45 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
											copy_m4_m4(end_pose, pchan->pose_mat);
							 | 
						
					
						
							
								
									
										
										
										
											2011-11-07 01:38:32 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
											copy_v3_v3(end_pose[3], pchan->pose_tail);
							 | 
						
					
						
							
								
									
										
										
										
											2011-03-03 17:58:06 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
											mul_serie_m4(world_pose, goalinv, ob->obmat, end_pose, NULL, NULL, NULL, NULL, NULL);
							 | 
						
					
						
							
								
									
										
										
										
											2012-07-01 09:54:44 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2009-09-24 21:22:24 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
											/* blend position */
							 | 
						
					
						
							
								
									
										
										
										
											2012-05-25 09:51:53 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
											goalpos[0] = fac * goalpos[0] + mfac * world_pose[3][0];
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
											goalpos[1] = fac * goalpos[1] + mfac * world_pose[3][1];
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
											goalpos[2] = fac * goalpos[2] + mfac * world_pose[3][2];
							 | 
						
					
						
							
								
									
										
										
										
											2012-07-01 09:54:44 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2009-09-24 21:22:24 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
											/* blend rotation */
							 | 
						
					
						
							
								
									
										
										
										
											2012-04-29 15:47:02 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
											mat3_to_quat(q1, goalrot);
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
											mat4_to_quat(q2, world_pose);
							 | 
						
					
						
							
								
									
										
										
										
											2009-11-10 20:43:45 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
											interp_qt_qtqt(q, q1, q2, mfac);
							 | 
						
					
						
							
								
									
										
										
										
											2012-04-29 15:47:02 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
											quat_to_mat3(goalrot, q);
							 | 
						
					
						
							
								
									
										
										
										
											2009-09-24 21:22:24 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
										}
							 | 
						
					
						
							
								
									
										
										
										
											2012-07-01 09:54:44 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2012-05-25 09:51:53 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
										iktarget = iktree[target->tip];
							 | 
						
					
						
							
								
									
										
										
										
											2012-07-01 09:54:44 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2012-11-24 00:18:34 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
										if ((data->flag & CONSTRAINT_IK_POS) && data->weight != 0.0f) {
							 | 
						
					
						
							
								
									
										
										
										
											2012-03-24 06:24:53 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
											if (poleconstrain)
							 | 
						
					
						
							
								
									
										
										
										
											2009-09-24 21:22:24 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
												IK_SolverSetPoleVectorConstraint(solver, iktarget, goalpos,
							 | 
						
					
						
							
								
									
										
										
										
											2012-05-25 09:51:53 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
												                                 polepos, data->poleangle, (poleangledata == data));
							 | 
						
					
						
							
								
									
										
										
										
											2009-09-24 21:22:24 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
											IK_SolverAddGoal(solver, iktarget, goalpos, data->weight);
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
										}
							 | 
						
					
						
							
								
									
										
										
										
											2012-03-24 06:24:53 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
										if ((data->flag & CONSTRAINT_IK_ROT) && (data->orientweight != 0.0f))
							 | 
						
					
						
							
								
									
										
										
										
											2012-05-25 09:51:53 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
											if ((data->flag & CONSTRAINT_IK_AUTO) == 0)
							 | 
						
					
						
							
								
									
										
										
										
											2009-09-24 21:22:24 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
												IK_SolverAddGoalOrientation(solver, iktarget, goalrot,
							 | 
						
					
						
							
								
									
										
										
										
											2012-05-25 09:51:53 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
												                            data->orientweight);
							 | 
						
					
						
							
								
									
										
										
										
											2009-09-24 21:22:24 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
									}
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
									/* solve */
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
									IK_Solve(solver, 0.0f, tree->iterations);
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2012-03-24 06:24:53 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
									if (poleangledata)
							 | 
						
					
						
							
								
									
										
										
										
											2012-05-25 09:51:53 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
										poleangledata->poleangle = IK_SolverGetPoleAngle(solver);
							 | 
						
					
						
							
								
									
										
										
										
											2009-09-24 21:22:24 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
									IK_FreeSolver(solver);
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
									/* gather basis changes */
							 | 
						
					
						
							
								
									
										
										
										
											2012-05-25 09:51:53 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
									tree->basis_change = MEM_mallocN(sizeof(float[3][3]) * tree->totchannel, "ik basis change");
							 | 
						
					
						
							
								
									
										
										
										
											2012-03-24 06:24:53 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
									if (hasstretch)
							 | 
						
					
						
							
								
									
										
										
										
											2012-05-25 09:51:53 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
										ikstretch = MEM_mallocN(sizeof(float) * tree->totchannel, "ik stretch");
							 | 
						
					
						
							
								
									
										
										
										
											2012-07-01 09:54:44 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2012-05-25 09:51:53 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
									for (a = 0; a < tree->totchannel; a++) {
							 | 
						
					
						
							
								
									
										
										
										
											2009-09-24 21:22:24 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
										IK_GetBasisChange(iktree[a], tree->basis_change[a]);
							 | 
						
					
						
							
								
									
										
										
										
											2012-07-01 09:54:44 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2012-03-24 06:24:53 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
										if (hasstretch) {
							 | 
						
					
						
							
								
									
										
										
										
											2009-09-24 21:22:24 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
											/* have to compensate for scaling received from parent */
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
											float parentstretch, stretch;
							 | 
						
					
						
							
								
									
										
										
										
											2012-07-01 09:54:44 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2012-05-25 09:51:53 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
											pchan = tree->pchan[a];
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
											parentstretch = (tree->parent[a] >= 0) ? ikstretch[tree->parent[a]] : 1.0f;
							 | 
						
					
						
							
								
									
										
										
										
											2012-07-01 09:54:44 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2012-03-24 06:24:53 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
											if (tree->stretch && (pchan->ikstretch > 0.0f)) {
							 | 
						
					
						
							
								
									
										
										
										
											2009-09-24 21:22:24 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
												float trans[3], length;
							 | 
						
					
						
							
								
									
										
										
										
											2012-07-01 09:54:44 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2009-09-24 21:22:24 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
												IK_GetTranslationChange(iktree[a], trans);
							 | 
						
					
						
							
								
									
										
										
										
											2012-05-25 09:51:53 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
												length = pchan->bone->length * len_v3(pchan->pose_mat[1]);
							 | 
						
					
						
							
								
									
										
										
										
											2012-07-01 09:54:44 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2012-05-25 09:51:53 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
												ikstretch[a] = (length == 0.0f) ? 1.0f : (trans[1] + length) / length;
							 | 
						
					
						
							
								
									
										
										
										
											2009-09-24 21:22:24 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
											}
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
											else
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
												ikstretch[a] = 1.0;
							 | 
						
					
						
							
								
									
										
										
										
											2012-07-01 09:54:44 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2012-05-25 09:51:53 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
											stretch = (parentstretch == 0.0f) ? 1.0f : ikstretch[a] / parentstretch;
							 | 
						
					
						
							
								
									
										
										
										
											2012-07-01 09:54:44 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2009-11-10 20:43:45 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
											mul_v3_fl(tree->basis_change[a][0], stretch);
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
											mul_v3_fl(tree->basis_change[a][1], stretch);
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
											mul_v3_fl(tree->basis_change[a][2], stretch);
							 | 
						
					
						
							
								
									
										
										
										
											2009-09-24 21:22:24 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
										}
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2012-05-25 09:51:53 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
										if (resultblend && resultinf != 1.0f) {
							 | 
						
					
						
							
								
									
										
										
										
											2009-11-10 20:43:45 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
											unit_m3(identity);
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
											blend_m3_m3m3(tree->basis_change[a], identity,
							 | 
						
					
						
							
								
									
										
										
										
											2012-05-25 09:51:53 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
											              tree->basis_change[a], resultinf);
							 | 
						
					
						
							
								
									
										
										
										
											2009-09-24 21:22:24 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
										}
							 | 
						
					
						
							
								
									
										
										
										
											2012-07-01 09:54:44 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2009-09-24 21:22:24 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
										IK_FreeSegment(iktree[a]);
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
									}
							 | 
						
					
						
							
								
									
										
										
										
											2012-07-01 09:54:44 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2009-09-24 21:22:24 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
									MEM_freeN(iktree);
							 | 
						
					
						
							
								
									
										
										
										
											2012-03-24 06:24:53 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
									if (ikstretch) MEM_freeN(ikstretch);
							 | 
						
					
						
							
								
									
										
										
										
											2009-09-24 21:22:24 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								}
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								static void free_posetree(PoseTree *tree)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								{
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
									BLI_freelistN(&tree->targets);
							 | 
						
					
						
							
								
									
										
										
										
											2012-03-24 06:24:53 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
									if (tree->pchan) MEM_freeN(tree->pchan);
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
									if (tree->parent) MEM_freeN(tree->parent);
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
									if (tree->basis_change) MEM_freeN(tree->basis_change);
							 | 
						
					
						
							
								
									
										
										
										
											2009-09-24 21:22:24 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
									MEM_freeN(tree);
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								}
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								///----------------------------------------
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								/// Plugin API for legacy iksolver
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2010-10-15 05:18:45 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								void iksolver_initialize_tree(struct Scene *UNUSED(scene), struct Object *ob, float UNUSED(ctime))
							 | 
						
					
						
							
								
									
										
										
										
											2009-09-24 21:22:24 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								{
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
									bPoseChannel *pchan;
							 | 
						
					
						
							
								
									
										
										
										
											2012-07-01 09:54:44 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2012-05-25 09:51:53 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
									for (pchan = ob->pose->chanbase.first; pchan; pchan = pchan->next) {
							 | 
						
					
						
							
								
									
										
										
										
											2012-03-24 06:24:53 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
										if (pchan->constflag & PCHAN_HAS_IK) // flag is set on editing constraints
							 | 
						
					
						
							
								
									
										
										
										
											2012-05-25 09:51:53 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
											initialize_posetree(ob, pchan);  // will attach it to root!
							 | 
						
					
						
							
								
									
										
										
										
											2009-09-24 21:22:24 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
									}
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
									ob->pose->flag &= ~POSE_WAS_REBUILT;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								}
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								void iksolver_execute_tree(struct Scene *scene, struct Object *ob,  struct bPoseChannel *pchan, float ctime)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								{
							 | 
						
					
						
							
								
									
										
										
										
											2012-03-24 06:24:53 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
									while (pchan->iktree.first) {
							 | 
						
					
						
							
								
									
										
										
										
											2012-05-25 09:51:53 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
										PoseTree *tree = pchan->iktree.first;
							 | 
						
					
						
							
								
									
										
										
										
											2009-09-24 21:22:24 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
										int a;
							 | 
						
					
						
							
								
									
										
										
										
											2012-07-01 09:54:44 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2009-11-01 11:29:40 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
										/* stop on the first tree that isn't a standard IK chain */
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
										if (tree->type != CONSTRAINT_TYPE_KINEMATIC)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
											return;
							 | 
						
					
						
							
								
									
										
										
										
											2012-07-01 09:54:44 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2009-09-24 21:22:24 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
										/* 4. walk over the tree for regular solving */
							 | 
						
					
						
							
								
									
										
										
										
											2012-05-25 09:51:53 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
										for (a = 0; a < tree->totchannel; a++) {
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
											if (!(tree->pchan[a]->flag & POSE_DONE))    // successive trees can set the flag
							 | 
						
					
						
							
								
									
										
										
										
											2012-05-05 16:03:57 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
												BKE_pose_where_is_bone(scene, ob, tree->pchan[a], ctime, 1);
							 | 
						
					
						
							
								
									
										
										
										
											2012-07-07 22:51:57 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
											/* tell blender that this channel was controlled by IK, it's cleared on each BKE_pose_where_is() */
							 | 
						
					
						
							
								
									
										
										
										
											2009-09-24 21:22:24 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
											tree->pchan[a]->flag |= POSE_CHAIN;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
										}
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
										/* 5. execute the IK solver */
							 | 
						
					
						
							
								
									
										
										
										
											2009-10-12 09:39:57 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
										execute_posetree(scene, ob, tree);
							 | 
						
					
						
							
								
									
										
										
										
											2012-07-01 09:54:44 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
										/* 6. apply the differences to the channels,
							 | 
						
					
						
							
								
									
										
										
										
											2012-03-09 18:28:30 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
										 *    we need to calculate the original differences first */
							 | 
						
					
						
							
								
									
										
										
										
											2012-05-25 09:51:53 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
										for (a = 0; a < tree->totchannel; a++) {
							 | 
						
					
						
							
								
									
										
										
										
											2009-09-24 21:22:24 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
											make_dmats(tree->pchan[a]);
							 | 
						
					
						
							
								
									
										
										
										
											2011-11-07 13:25:17 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
										}
							 | 
						
					
						
							
								
									
										
										
										
											2012-07-01 09:54:44 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2012-05-25 09:51:53 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
										for (a = 0; a < tree->totchannel; a++) {
							 | 
						
					
						
							
								
									
										
										
										
											2009-09-24 21:22:24 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
											/* sets POSE_DONE */
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
											where_is_ik_bone(tree->pchan[a], tree->basis_change[a]);
							 | 
						
					
						
							
								
									
										
										
										
											2011-11-07 13:25:17 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
										}
							 | 
						
					
						
							
								
									
										
										
										
											2012-07-01 09:54:44 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2009-09-24 21:22:24 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
										/* 7. and free */
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
										BLI_remlink(&pchan->iktree, tree);
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
										free_posetree(tree);
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
									}
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								}
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 |