diff options
author | Joshua Leung <aligorith@gmail.com> | 2007-07-15 07:35:37 +0400 |
---|---|---|
committer | Joshua Leung <aligorith@gmail.com> | 2007-07-15 07:35:37 +0400 |
commit | af55d208f48017872e30faa76a66d777c983106d (patch) | |
tree | 818c07d693d001230e7730efd10575813f98f178 /source/blender/blenkernel | |
parent | 0f692d999a3529b73d6b153f6beaf9b570396d36 (diff) |
== Constraints System ==
After just over a week of coding, I've finished doing a major refactor/cleanup of the constraints code. In the process, quite a few old kludges and ugly hacks have been removed. Also, some new features which will greatly benefit riggers have been implemented.
=== What's New ===
* The long-awaited ``ChildOf Constraint'':
This allows you to animate parent influences, and choose which transformation channels the parent affects the child on (i.e. no translation/rotation/scaling). It should be noted that disabling some combinations may not totally work as expected. Also, the 'Set Inverse' and 'Clear Inverse' buttons at the bottom of this constraint's panel set/clear the inverse correction for the parent's effects. Use these to make the owner not stick/be glued to the parent.
* Constraint/Target Evaluation Spaces:
In some constraints, there are now 1-2 combo boxes at the bottom of their panel, which allows you to pick which `co-ordinate space' they are evaluated in. This is much more flexible than the old 'local' options for bones only were.
* Action Constraint - Loc/Rot/Size Inputs
The Action Constraint can finally use the target's location/rotation/scaling transforms as input, to control the owner of the constraint. This should work much more reliably than it used to. The target evaluation should now also be more accurate due to the new space conversion stuff.
* Transform - No longer in Crazy Space (TM)
Transforming objects/bones with constraints applied should no longer occur in Crazy Space. They are now correctly inverse-corrected. This also applies to old-style object tracking.
=== General Code Changes ===
* solve_constraints is now in constraints.c. I've removed the old `blend consecutive constraints of same type' junk, which made the code more complex than it needed to be.
* evaluate_constraint is now only passed the constraint, and two matrices. A few unused variables have been removed from here.
* A tempolary struct, bConstraintOb, is now passed to solve_constraints instead of relying on an ugly, static workobject in some cases. This works much better.
* Made the formatting of constraint code consistent
* There's a version patch for older files so that constraint settings are correctly converted to the new system. This is currently done for MajorVersion <= 244, and SubVersion < 3. I've bumped up the subversion to 3 for this purpose. However, with the imminent 2.45 release, this may need to be adjusted accordingly.
* LocEulSizeToMat4 and LocQuatSizeToMat4 now work in the order Size, Rot, Location. I've also added a few other math functions.
* Mat4BlendMat4 is now in arithb. I've modified it's method slightly, to use other arithb functions, instead of its crazy blending scheme.
* Moved some of the RigidBodyJoint constraint's code out of blenkernel, and into src. It shouldn't be setting its target in its data initialisation function based + accessing scene stuff where it was doing so.
=== Future Work ===
* Geometry to act as targets for constraints. A space has been reserved for this already.
* Tidy up UI buttons of constraints
Diffstat (limited to 'source/blender/blenkernel')
-rw-r--r-- | source/blender/blenkernel/BKE_blender.h | 2 | ||||
-rw-r--r-- | source/blender/blenkernel/BKE_constraint.h | 53 | ||||
-rw-r--r-- | source/blender/blenkernel/BKE_object.h | 1 | ||||
-rw-r--r-- | source/blender/blenkernel/bad_level_call_stubs/stubs.c | 2 | ||||
-rw-r--r-- | source/blender/blenkernel/intern/action.c | 8 | ||||
-rw-r--r-- | source/blender/blenkernel/intern/armature.c | 272 | ||||
-rw-r--r-- | source/blender/blenkernel/intern/constraint.c | 1436 | ||||
-rw-r--r-- | source/blender/blenkernel/intern/object.c | 138 |
8 files changed, 955 insertions, 957 deletions
diff --git a/source/blender/blenkernel/BKE_blender.h b/source/blender/blenkernel/BKE_blender.h index 4b9e10651cf..e175725a6ae 100644 --- a/source/blender/blenkernel/BKE_blender.h +++ b/source/blender/blenkernel/BKE_blender.h @@ -44,7 +44,7 @@ struct ListBase; struct MemFile; #define BLENDER_VERSION 244 -#define BLENDER_SUBVERSION 2 +#define BLENDER_SUBVERSION 3 #define BLENDER_MINVERSION 240 #define BLENDER_MINSUBVERSION 0 diff --git a/source/blender/blenkernel/BKE_constraint.h b/source/blender/blenkernel/BKE_constraint.h index 2bec643d5b0..6a11159645c 100644 --- a/source/blender/blenkernel/BKE_constraint.h +++ b/source/blender/blenkernel/BKE_constraint.h @@ -33,43 +33,66 @@ #ifndef BKE_CONSTRAINT_H #define BKE_CONSTRAINT_H + struct bConstraint; -struct Object; struct ListBase; +struct Object; struct bConstraintChannel; +struct bPoseChannel; struct bAction; struct bArmature; -/* Function prototypes */ +/* ---------------------------------------------------------------------------- */ + +/* Constraint target/owner types */ +#define TARGET_OBJECT 1 /* string is "" */ +#define TARGET_BONE 2 /* string is bone-name */ +#define TARGET_VERT 3 /* string is vertex-group name */ +#define TARGET_CV 4 /* string is vertex-group name - is not available until curves get vgroups */ + +/* ---------------------------------------------------------------------------- */ + +/* special struct for use in constraint evaluation */ +typedef struct bConstraintOb { + struct Object *ob; /* if pchan, then armature that it comes from, otherwise constraint owner */ + struct bPoseChannel *pchan; /* pose channel that owns the constraints being evaluated */ + + float matrix[4][4]; /* matrix where constraints are accumulated + solved */ + float startmat[4][4]; /* original matrix (before constraint solving) */ + + short type; /* type of owner */ +} bConstraintOb; + +/* ---------------------------------------------------------------------------- */ + +/* Constraint function prototypes */ void unique_constraint_name (struct bConstraint *con, struct ListBase *list); void *new_constraint_data (short type); -void evaluate_constraint (struct bConstraint *constraint, struct Object *ob, short ownertype, void *ownerdata, float targetmat[][4]); void free_constraints (struct ListBase *conlist); void copy_constraints (struct ListBase *dst, struct ListBase *src); -void copy_constraint_channels (ListBase *dst, ListBase *src); -void clone_constraint_channels (struct ListBase *dst, struct ListBase *src); void relink_constraints (struct ListBase *list); void free_constraint_data (struct bConstraint *con); -/* channels */ +/* Constraint Channel function prototypes */ struct bConstraintChannel *get_constraint_channel (ListBase *list, const char *name); struct bConstraintChannel *verify_constraint_channel (ListBase *list, const char *name); +void do_constraint_channels (struct ListBase *conbase, struct ListBase *chanbase, float ctime); +void copy_constraint_channels (ListBase *dst, ListBase *src); +void clone_constraint_channels (struct ListBase *dst, struct ListBase *src); void free_constraint_channels (ListBase *chanbase); -/* Gemeric functions */ -void do_constraint_channels (struct ListBase *conbase, struct ListBase *chanbase, float ctime); -short get_constraint_target_matrix (struct bConstraint *con, short ownertype, void *ownerdata, float mat[][4], float size[3], float time); +/* Target function prototypes */ char constraint_has_target (struct bConstraint *con); struct Object *get_constraint_target(struct bConstraint *con, char **subtarget); void set_constraint_target(struct bConstraint *con, struct Object *ob, char *subtarget); +/* Constraint Evaluation function prototypes */ +struct bConstraintOb *constraints_make_evalob (struct Object *ob, void *subdata, short datatype); +void constraints_clear_evalob(struct bConstraintOb *cob); + +short get_constraint_target_matrix (struct bConstraint *con, short ownertype, void *ownerdata, float mat[][4], float time); +void solve_constraints (struct ListBase *conlist, struct bConstraintOb *cob, float ctime); -/* Constraint target/owner types */ -#define TARGET_OBJECT 1 // string is "" -#define TARGET_BONE 2 // string is bone-name -#define TARGET_VERT 3 // string is "VE:#" -#define TARGET_FACE 4 // string is "FA:#" -#define TARGET_CV 5 // string is "CV:#" #endif diff --git a/source/blender/blenkernel/BKE_object.h b/source/blender/blenkernel/BKE_object.h index 52d529ea32d..e38446e0d46 100644 --- a/source/blender/blenkernel/BKE_object.h +++ b/source/blender/blenkernel/BKE_object.h @@ -104,7 +104,6 @@ void object_boundbox_flag(struct Object *ob, int flag, int set); void minmax_object(struct Object *ob, float *min, float *max); void minmax_object_duplis(struct Object *ob, float *min, float *max); void solve_tracking (struct Object *ob, float targetmat[][4]); -void solve_constraints (struct Object *ob, short obtype, void *obdata, float ctime); void object_handle_update(struct Object *ob); diff --git a/source/blender/blenkernel/bad_level_call_stubs/stubs.c b/source/blender/blenkernel/bad_level_call_stubs/stubs.c index e76c53fc72c..340ac1ed693 100644 --- a/source/blender/blenkernel/bad_level_call_stubs/stubs.c +++ b/source/blender/blenkernel/bad_level_call_stubs/stubs.c @@ -126,7 +126,7 @@ int BPY_button_eval(char *expr, double *value) } /* constraint.c */ -void BPY_pyconstraint_eval(struct bPythonConstraint *con, float obmat[][4], short ownertype, void *ownerdata, float targetmat[][4]) +void BPY_pyconstraint_eval(struct bPythonConstraint *con, float ownermat[][4], float targetmat[][4]) { } int BPY_pyconstraint_targets(struct bPythonConstraint *con, float targetmat[][4]) diff --git a/source/blender/blenkernel/intern/action.c b/source/blender/blenkernel/intern/action.c index aa9d3d30f98..bb0953cc1cc 100644 --- a/source/blender/blenkernel/intern/action.c +++ b/source/blender/blenkernel/intern/action.c @@ -225,12 +225,12 @@ bPoseChannel *verify_pose_channel(bPose* pose, const char* name) { bPoseChannel *chan; - if (!pose){ + if (!pose) { return NULL; } /* See if this channel exists */ - for (chan=pose->chanbase.first; chan; chan=chan->next){ + for (chan=pose->chanbase.first; chan; chan=chan->next) { if (!strcmp (name, chan->name)) return chan; } @@ -239,7 +239,7 @@ bPoseChannel *verify_pose_channel(bPose* pose, const char* name) chan = MEM_callocN(sizeof(bPoseChannel), "verifyPoseChannel"); strncpy (chan->name, name, 31); - /* init vars to prevent mat errors */ + /* init vars to prevent math errors */ chan->quat[0] = 1.0F; chan->size[0] = chan->size[1] = chan->size[2] = 1.0F; @@ -247,6 +247,8 @@ bPoseChannel *verify_pose_channel(bPose* pose, const char* name) chan->limitmax[0]= chan->limitmax[1]= chan->limitmax[2]= 180.0f; chan->stiffness[0]= chan->stiffness[1]= chan->stiffness[2]= 0.0f; + Mat4One(chan->constinv); + BLI_addtail (&pose->chanbase, chan); return chan; diff --git a/source/blender/blenkernel/intern/armature.c b/source/blender/blenkernel/intern/armature.c index 669724bd250..b320a7e0106 100644 --- a/source/blender/blenkernel/intern/armature.c +++ b/source/blender/blenkernel/intern/armature.c @@ -1321,7 +1321,7 @@ static void execute_posetree(Object *ob, PoseTree *tree) float goalrot[3][3], goalpos[3]; float rootmat[4][4], imat[4][4]; float goal[4][4], goalinv[4][4]; - float size[3], irest_basis[3][3], full_basis[3][3]; + float irest_basis[3][3], full_basis[3][3]; float end_pose[4][4], world_pose[4][4]; float length, basis[3][3], rest_basis[3][3], start[3], *ikstretch=NULL; int a, flag, hasstretch=0; @@ -1444,7 +1444,7 @@ static void execute_posetree(Object *ob, PoseTree *tree) data= (bKinematicConstraint*)target->con->data; /* 1.0=ctime, we pass on object for auto-ik */ - get_constraint_target_matrix(target->con, TARGET_BONE, ob, rootmat, size, 1.0); + get_constraint_target_matrix(target->con, TARGET_BONE, ob, rootmat, 1.0); /* and set and transform goal */ Mat4MulMat4(goal, rootmat, goalinv); @@ -1603,211 +1603,7 @@ static void where_is_ik_bone(bPoseChannel *pchan, float ik_mat[][3]) // nr = t pchan->flag |= POSE_DONE; } -static void do_local_constraint(bPoseChannel *pchan, bConstraint *con) -{ - switch(con->type) { - case CONSTRAINT_TYPE_LOCLIKE: - { - bLocateLikeConstraint *data= con->data; - float fac= con->enforce; - - if(data->tar && data->subtarget[0]) { - bPoseChannel *pchant= get_pose_channel(data->tar->pose, data->subtarget); - if(pchant) { - float loc[3]; - - /* copy location of tip of bone? */ - if (data->flag & LOCLIKE_TIP) { - float mat[4][4], tmat[4][4]; - - Mat4One(tmat); - VECCOPY(tmat[3], pchant->pose_tail); - - armature_mat_pose_to_delta(mat, tmat, pchant->bone->arm_mat); - VECCOPY(loc, mat[3]); - } - else - VECCOPY(loc, pchant->loc); - - /* do offsets? */ - if (data->flag & LOCLIKE_OFFSET) - VecAddf(loc, loc, pchan->loc); - - if (data->flag & LOCLIKE_X) - pchan->loc[0]= FloatLerpf(loc[0], pchan->loc[0], fac); - if (data->flag & LOCLIKE_Y) - pchan->loc[1]= FloatLerpf(loc[1], pchan->loc[1], fac); - if (data->flag & LOCLIKE_Z) - pchan->loc[2]= FloatLerpf(loc[2], pchan->loc[2], fac); - if (data->flag & LOCLIKE_X_INVERT) - pchan->loc[0]= FloatLerpf(pchant->loc[0], pchan->loc[0], -fac); - if (data->flag & LOCLIKE_Y_INVERT) - pchan->loc[1]= FloatLerpf(pchant->loc[1], pchan->loc[1], -fac); - if (data->flag & LOCLIKE_Z_INVERT) - pchan->loc[2]= FloatLerpf(pchant->loc[2], pchan->loc[2], -fac); - } - } - } - break; - case CONSTRAINT_TYPE_ROTLIKE: - { - bRotateLikeConstraint *data= con->data; - if(data->tar && data->subtarget[0]) { - bPoseChannel *pchant= get_pose_channel(data->tar->pose, data->subtarget); - if(pchant) { - if(data->flag != (ROTLIKE_X|ROTLIKE_Y|ROTLIKE_Z)) { - float eul[3], eult[3], euln[3]; - float fac= con->enforce; - - QuatToEul(pchan->quat, eul); - QuatToEul(pchant->quat, eult); - VECCOPY(euln, eul); - - if(data->flag & ROTLIKE_X) euln[0]= FloatLerpf(eult[0], eul[0], fac); - if(data->flag & ROTLIKE_Y) euln[1]= FloatLerpf(eult[1], eul[1], fac); - if(data->flag & ROTLIKE_Z) euln[2]= FloatLerpf(eult[2], eul[2], fac); - if(data->flag & ROTLIKE_X_INVERT) euln[0]= FloatLerpf(eult[0], eul[0], -fac); - if(data->flag & ROTLIKE_Y_INVERT) euln[1]= FloatLerpf(eult[1], eul[1], -fac); - if(data->flag & ROTLIKE_Z_INVERT) euln[2]= FloatLerpf(eult[2], eul[2], -fac); - - compatible_eul(eul, euln); - EulToQuat(euln, pchan->quat); - } - else { - QuatInterpol(pchan->quat, pchan->quat, pchant->quat, con->enforce); - } - } - } - } - break; - case CONSTRAINT_TYPE_SIZELIKE: - { - bSizeLikeConstraint *data= con->data; - float fac= con->enforce; - - if(data->tar && data->subtarget[0]) { - bPoseChannel *pchant= get_pose_channel(data->tar->pose, data->subtarget); - if(pchant) { - if (data->flag & SIZELIKE_X) - pchan->size[0]= FloatLerpf(pchant->size[0], pchan->size[0], fac); - if (data->flag & SIZELIKE_Y) - pchan->size[1]= FloatLerpf(pchant->size[1], pchan->size[1], fac); - if (data->flag & SIZELIKE_Z) - pchan->size[2]= FloatLerpf(pchant->size[2], pchan->size[2], fac); - } - } - } - break; - case CONSTRAINT_TYPE_LOCLIMIT: - { - bLocLimitConstraint *data= con->data; - float fac= con->enforce; - - if (data->flag & LIMIT_XMIN) { - if(pchan->loc[0] < data->xmin) - pchan->loc[0] = FloatLerpf(data->xmin, pchan->loc[0], fac); - } - if (data->flag & LIMIT_XMAX) { - if (pchan->loc[0] > data->xmax) - pchan->loc[0] = FloatLerpf(data->xmax, pchan->loc[0], fac); - } - if (data->flag & LIMIT_YMIN) { - if(pchan->loc[1] < data->ymin) - pchan->loc[1] = FloatLerpf(data->ymin, pchan->loc[1], fac); - } - if (data->flag & LIMIT_YMAX) { - if (pchan->loc[1] > data->ymax) - pchan->loc[1] = FloatLerpf(data->ymax, pchan->loc[1], fac); - } - if (data->flag & LIMIT_ZMIN) { - if(pchan->loc[2] < data->zmin) - pchan->loc[2] = FloatLerpf(data->zmin, pchan->loc[2], fac); - } - if (data->flag & LIMIT_ZMAX) { - if (pchan->loc[2] > data->zmax) - pchan->loc[2] = FloatLerpf(data->zmax, pchan->loc[2], fac); - } - } - break; - case CONSTRAINT_TYPE_ROTLIMIT: - { - bRotLimitConstraint *data = con->data; - float eul[3]; - float fac= con->enforce; - - QuatToEul(pchan->quat, eul); - - /* eulers: radians to degrees! */ - eul[0] = (eul[0] / (2*M_PI) * 360); - eul[1] = (eul[1] / (2*M_PI) * 360); - eul[2] = (eul[2] / (2*M_PI) * 360); - - /* limiting of euler values... */ - if (data->flag & LIMIT_XROT) { - if (eul[0] < data->xmin) - eul[0] = FloatLerpf(data->xmin, eul[0], fac); - - if (eul[0] > data->xmax) - eul[0] = FloatLerpf(data->xmax, eul[0], fac); - } - if (data->flag & LIMIT_YROT) { - if (eul[1] < data->ymin) - eul[1] = FloatLerpf(data->ymin, eul[1], fac); - - if (eul[1] > data->ymax) - eul[1] = FloatLerpf(data->ymax, eul[1], fac); - } - if (data->flag & LIMIT_ZROT) { - if (eul[2] < data->zmin) - eul[2] = FloatLerpf(data->zmin, eul[2], fac); - - if (eul[2] > data->zmax) - eul[2] = FloatLerpf(data->zmax, eul[2], fac); - } - - /* eulers: degrees to radians ! */ - eul[0] = (eul[0] / 360 * (2*M_PI)); - eul[1] = (eul[1] / 360 * (2*M_PI)); - eul[2] = (eul[2] / 360 * (2*M_PI)); - - /* convert back */ - EulToQuat(eul, pchan->quat); - } - break; - case CONSTRAINT_TYPE_SIZELIMIT: - { - bSizeLimitConstraint *data= con->data; - float fac= con->enforce; - - if (data->flag & LIMIT_XMIN) { - if(pchan->size[0] < data->xmin) - pchan->size[0] = FloatLerpf(data->xmin, pchan->size[0], fac); - } - if (data->flag & LIMIT_XMAX) { - if (pchan->size[0] > data->xmax) - pchan->size[0] = FloatLerpf(data->xmax, pchan->size[0], fac); - } - if (data->flag & LIMIT_YMIN) { - if(pchan->size[1] < data->ymin) - pchan->size[1] = FloatLerpf(data->ymin, pchan->size[1], fac); - } - if (data->flag & LIMIT_YMAX) { - if (pchan->size[1] > data->ymax) - pchan->size[1] = FloatLerpf(data->ymax, pchan->size[1], fac); - } - if (data->flag & LIMIT_ZMIN) { - if(pchan->size[2] < data->zmin) - pchan->size[2] = FloatLerpf(data->zmin, pchan->size[2], fac); - } - if (data->flag & LIMIT_ZMAX) { - if (pchan->size[2] > data->zmax) - pchan->size[2] = FloatLerpf(data->zmax, pchan->size[2], fac); - } - } - break; - } -} - +/* NLA strip modifiers */ static void do_strip_modifiers(Object *armob, Bone *bone, bPoseChannel *pchan) { bActionModifier *amod; @@ -1906,32 +1702,16 @@ static void where_is_pose_bone(Object *ob, bPoseChannel *pchan, float ctime) { Bone *bone, *parbone; bPoseChannel *parchan; - float vec[3], quat[4]; - int did_local= 0; /* copying quaternion should be limited, chan_calc_mat() normalizes quat */ + float vec[3]; /* set up variables for quicker access below */ bone= pchan->bone; parbone= bone->parent; parchan= pchan->parent; - - /* Do local constraints, these only work on the channel data (loc rot size) */ - QUATCOPY(quat, pchan->quat); - if(pchan->constraints.first) { - bConstraint *con; - for(con=pchan->constraints.first; con; con= con->next) { - if(con->flag & CONSTRAINT_LOCAL) { - do_local_constraint(pchan, con); - did_local= 1; - } - } - } /* this gives a chan_mat with actions (ipos) results */ chan_calc_mat(pchan); - if(did_local) - QUATCOPY(pchan->quat, quat); /* local constraint hack. bad! */ - /* construct the posemat based on PoseChannels, that we do before applying constraints */ /* pose_mat(b)= pose_mat(b-1) * yoffs(b-1) * d_root(b) * bone_mat(b) * chan_mat(b) */ @@ -1970,44 +1750,33 @@ static void where_is_pose_bone(Object *ob, bPoseChannel *pchan, float ctime) VecAddf(pchan->pose_mat[3], pchan->pose_mat[3], ob->pose->cyclic_offset); } + /* do NLA strip modifiers - i.e. curve follow */ do_strip_modifiers(ob, bone, pchan); /* Do constraints */ - if(pchan->constraints.first) { - static Object conOb; - static int initialized= 0; + if (pchan->constraints.first) { + bConstraintOb *cob; + /* make a copy of location of PoseChannel for later */ VECCOPY(vec, pchan->pose_mat[3]); - /* Build a workob to pass the bone to the constraint solver */ - if(initialized==0) { - memset(&conOb, 0, sizeof(Object)); - initialized= 1; - } - conOb.size[0]= conOb.size[1]= conOb.size[2]= 1.0; - conOb.data = ob->data; - conOb.type = ob->type; - conOb.parent = ob; // ik solver retrieves the armature that way !?!?!?! - conOb.pose= ob->pose; // needed for retrieving pchan - conOb.trackflag = ob->trackflag; - conOb.upflag = ob->upflag; - - /* Collect the constraints from the pose (listbase copy) */ - conOb.constraints = pchan->constraints; + /* prepare PoseChannel for Constraint solving + * - makes a copy of matrix, and creates temporary struct to use + */ + cob= constraints_make_evalob(ob, pchan, TARGET_BONE); - /* conOb.obmat takes bone to worldspace */ - Mat4MulMat4 (conOb.obmat, pchan->pose_mat, ob->obmat); + /* Solve PoseChannel's Constraints */ + solve_constraints(&pchan->constraints, cob, ctime); // ctime doesnt alter objects - /* Solve */ - solve_constraints (&conOb, TARGET_BONE, (void*)pchan, ctime); // ctime doesnt alter objects - - /* Take out of worldspace */ - Mat4MulMat4 (pchan->pose_mat, conOb.obmat, ob->imat); + /* cleanup after Constraint Solving + * - applies matrix back to pchan, and frees temporary struct used + */ + constraints_clear_evalob(cob); /* prevent constraints breaking a chain */ - if(pchan->bone->flag & BONE_CONNECTED) + if(pchan->bone->flag & BONE_CONNECTED) { VECCOPY(pchan->pose_mat[3], vec); - + } } /* calculate head */ @@ -2016,7 +1785,6 @@ static void where_is_pose_bone(Object *ob, bPoseChannel *pchan, float ctime) VECCOPY(vec, pchan->pose_mat[1]); VecMulf(vec, bone->length); VecAddf(pchan->pose_tail, pchan->pose_head, vec); - } /* This only reads anim data from channels, and writes to channels */ diff --git a/source/blender/blenkernel/intern/constraint.c b/source/blender/blenkernel/intern/constraint.c index 25b8c9ac171..5613ff80c9f 100644 --- a/source/blender/blenkernel/intern/constraint.c +++ b/source/blender/blenkernel/intern/constraint.c @@ -25,7 +25,7 @@ * * The Original Code is: all of this file. * - * Contributor(s): none yet. + * Contributor(s): 2007, Joshua Leung, major recode * * ***** END GPL/BL DUAL LICENSE BLOCK ***** */ @@ -72,7 +72,6 @@ #define M_PI 3.14159265358979323846 #endif -/* Local function prototypes */ /* ********************* Data level ****************** */ @@ -165,22 +164,21 @@ void copy_constraints (ListBase *dst, ListBase *src) bConstraint *con, *srccon; dst->first= dst->last= NULL; - duplicatelist (dst, src); for (con = dst->first, srccon=src->first; con; srccon=srccon->next, con=con->next) { con->data = MEM_dupallocN (con->data); + + /* only do specific constraints if required */ if (con->type == CONSTRAINT_TYPE_PYTHON) { - bPythonConstraint *pycon = (bPythonConstraint*) con->data; - bPythonConstraint *opycon = (bPythonConstraint*) srccon->data; - + bPythonConstraint *pycon = (bPythonConstraint *)con->data; + bPythonConstraint *opycon = (bPythonConstraint *)srccon->data; + pycon->prop = IDP_CopyProperty(opycon->prop); } - /* removed a whole lot of useless code here (ton) */ } } - /* **************** Editor Functions **************** */ char constraint_has_target (bConstraint *con) @@ -189,103 +187,97 @@ char constraint_has_target (bConstraint *con) case CONSTRAINT_TYPE_PYTHON: { bPythonConstraint *data = con->data; - if (data->tar) - return 1; + if (data->tar) return 1; } break; case CONSTRAINT_TYPE_TRACKTO: { bTrackToConstraint *data = con->data; - if (data->tar) - return 1; + if (data->tar) return 1; } break; case CONSTRAINT_TYPE_KINEMATIC: { bKinematicConstraint *data = con->data; - if (data->tar) - return 1; + if (data->tar) return 1; } break; case CONSTRAINT_TYPE_FOLLOWPATH: { bFollowPathConstraint *data = con->data; - if (data->tar) - return 1; + if (data->tar) return 1; } break; case CONSTRAINT_TYPE_ROTLIKE: { bRotateLikeConstraint *data = con->data; - if (data->tar) - return 1; + if (data->tar) return 1; } break; case CONSTRAINT_TYPE_LOCLIKE: { bLocateLikeConstraint *data = con->data; - if (data->tar) - return 1; + if (data->tar) return 1; } break; case CONSTRAINT_TYPE_SIZELIKE: { bSizeLikeConstraint *data = con->data; - if (data->tar) - return 1; + if (data->tar) return 1; } break; case CONSTRAINT_TYPE_MINMAX: { bMinMaxConstraint *data = con->data; - if (data->tar) - return 1; + if (data->tar) return 1; } break; case CONSTRAINT_TYPE_ACTION: { bActionConstraint *data = con->data; - if (data->tar) - return 1; + if (data->tar) return 1; } break; case CONSTRAINT_TYPE_LOCKTRACK: { bLockTrackConstraint *data = con->data; - if (data->tar) - return 1; + if (data->tar) return 1; } case CONSTRAINT_TYPE_STRETCHTO: { bStretchToConstraint *data = con->data; - if (data->tar) - return 1; + if (data->tar) return 1; } break; case CONSTRAINT_TYPE_RIGIDBODYJOINT: { bRigidBodyJointConstraint *data = con->data; - if (data->tar) - return 1; + if (data->tar) return 1; } break; case CONSTRAINT_TYPE_CLAMPTO: { bClampToConstraint *data = con->data; - if (data->tar) - return 1; + if (data->tar) return 1; } break; + case CONSTRAINT_TYPE_CHILDOF: + { + bChildOfConstraint *data = con->data; + if (data->tar) return 1; + } + break; } - // Unknown types or CONSTRAINT_TYPE_NULL or no target + + /* Unknown types or CONSTRAINT_TYPE_NULL or no target */ return 0; } Object *get_constraint_target(bConstraint *con, char **subtarget) { /* If the target for this constraint is target, return a pointer - * to the name for this constraints subtarget ... NULL otherwise - */ + * to the name for this constraints subtarget ... NULL otherwise + */ switch (con->type) { case CONSTRAINT_TYPE_PYTHON: { @@ -361,7 +353,7 @@ Object *get_constraint_target(bConstraint *con, char **subtarget) { bStretchToConstraint *data = con->data; *subtarget= data->subtarget; - return (data->tar); + return data->tar; } break; case CONSTRAINT_TYPE_RIGIDBODYJOINT: @@ -378,6 +370,13 @@ Object *get_constraint_target(bConstraint *con, char **subtarget) return data->tar; } break; + case CONSTRAINT_TYPE_CHILDOF: + { + bChildOfConstraint *data = con->data; + *subtarget= data->subtarget; + return data->tar; + } + break; default: *subtarget= NULL; break; @@ -388,63 +387,62 @@ Object *get_constraint_target(bConstraint *con, char **subtarget) void set_constraint_target(bConstraint *con, Object *ob, char *subtarget) { - /* Set the target for this constraint */ + /* Set the target for this constraint */ switch (con->type) { - case CONSTRAINT_TYPE_PYTHON: { bPythonConstraint *data = con->data; data->tar= ob; - if(subtarget) BLI_strncpy(data->subtarget, subtarget, 32); + if (subtarget) BLI_strncpy(data->subtarget, subtarget, 32); } break; case CONSTRAINT_TYPE_ACTION: { bActionConstraint *data = con->data; data->tar= ob; - if(subtarget) BLI_strncpy(data->subtarget, subtarget, 32); + if (subtarget) BLI_strncpy(data->subtarget, subtarget, 32); } break; case CONSTRAINT_TYPE_LOCLIKE: { bLocateLikeConstraint *data = con->data; data->tar= ob; - if(subtarget) BLI_strncpy(data->subtarget, subtarget, 32); + if (subtarget) BLI_strncpy(data->subtarget, subtarget, 32); } break; case CONSTRAINT_TYPE_ROTLIKE: { bRotateLikeConstraint *data = con->data; data->tar= ob; - if(subtarget) BLI_strncpy(data->subtarget, subtarget, 32); + if (subtarget) BLI_strncpy(data->subtarget, subtarget, 32); } break; case CONSTRAINT_TYPE_SIZELIKE: { bSizeLikeConstraint *data = con->data; data->tar= ob; - if(subtarget) BLI_strncpy(data->subtarget, subtarget, 32); + if (subtarget) BLI_strncpy(data->subtarget, subtarget, 32); } break; case CONSTRAINT_TYPE_KINEMATIC: { bKinematicConstraint *data = con->data; data->tar= ob; - if(subtarget) BLI_strncpy(data->subtarget, subtarget, 32); + if (subtarget) BLI_strncpy(data->subtarget, subtarget, 32); } break; case CONSTRAINT_TYPE_TRACKTO: { bTrackToConstraint *data = con->data; data->tar= ob; - if(subtarget) BLI_strncpy(data->subtarget, subtarget, 32); + if (subtarget) BLI_strncpy(data->subtarget, subtarget, 32); } break; case CONSTRAINT_TYPE_LOCKTRACK: { bLockTrackConstraint *data = con->data; data->tar= ob; - if(subtarget) BLI_strncpy(data->subtarget, subtarget, 32); + if (subtarget) BLI_strncpy(data->subtarget, subtarget, 32); } break; case CONSTRAINT_TYPE_FOLLOWPATH: @@ -457,7 +455,7 @@ void set_constraint_target(bConstraint *con, Object *ob, char *subtarget) { bStretchToConstraint *data = con->data; data->tar= ob; - if(subtarget) BLI_strncpy(data->subtarget, subtarget, 32); + if (subtarget) BLI_strncpy(data->subtarget, subtarget, 32); } break; case CONSTRAINT_TYPE_RIGIDBODYJOINT: @@ -470,7 +468,7 @@ void set_constraint_target(bConstraint *con, Object *ob, char *subtarget) { bMinMaxConstraint *data = (bMinMaxConstraint*)con->data; data->tar= ob; - if(subtarget) BLI_strncpy(data->subtarget, subtarget, 32); + if (subtarget) BLI_strncpy(data->subtarget, subtarget, 32); } break; case CONSTRAINT_TYPE_CLAMPTO: @@ -479,26 +477,32 @@ void set_constraint_target(bConstraint *con, Object *ob, char *subtarget) data->tar= ob; } break; + case CONSTRAINT_TYPE_CHILDOF: + { + bChildOfConstraint *data = con->data; + data->tar= ob; + if (subtarget) BLI_strncpy(data->subtarget, subtarget, 32); + } + break; } } void unique_constraint_name (bConstraint *con, ListBase *list) { - char tempname[64]; - int number; - char *dot; - int exists = 0; bConstraint *curcon; + char tempname[64]; + int number = 1, exists = 0; + char *dot; /* See if we are given an empty string */ if (con->name[0] == '\0') { /* give it default name first */ - strcpy (con->name, "Const"); + strcpy(con->name, "Const"); } /* See if we even need to do this */ for (curcon = list->first; curcon; curcon=curcon->next) { - if (curcon!=con){ + if (curcon != con) { if (!strcmp(curcon->name, con->name)) { exists = 1; break; @@ -514,20 +518,20 @@ void unique_constraint_name (bConstraint *con, ListBase *list) if (dot) *dot=0; - for (number = 1; number <=999; number++) { - sprintf (tempname, "%s.%03d", con->name, number); + for (number = 1; number <= 999; number++) { + sprintf(tempname, "%s.%03d", con->name, number); exists = 0; for (curcon=list->first; curcon; curcon=curcon->next) { if (con!=curcon) { - if (!strcmp (curcon->name, tempname)) { + if (!strcmp(curcon->name, tempname)) { exists = 1; break; } } } if (!exists) { - strcpy (con->name, tempname); + strcpy(con->name, tempname); return; } } @@ -590,7 +594,7 @@ void *new_constraint_data (short type) case CONSTRAINT_TYPE_LOCLIKE: { bLocateLikeConstraint *data; - data = MEM_callocN(sizeof(bLocateLikeConstraint), "loclikeConstraint"); + data = MEM_callocN(sizeof(bLocateLikeConstraint), "LocLikeConstraint"); data->flag = LOCLIKE_X|LOCLIKE_Y|LOCLIKE_Z; result = data; } @@ -598,7 +602,7 @@ void *new_constraint_data (short type) case CONSTRAINT_TYPE_ROTLIKE: { bRotateLikeConstraint *data; - data = MEM_callocN(sizeof(bRotateLikeConstraint), "rotlikeConstraint"); + data = MEM_callocN(sizeof(bRotateLikeConstraint), "RotLikeConstraint"); data->flag = ROTLIKE_X|ROTLIKE_Y|ROTLIKE_Z; result = data; } @@ -606,7 +610,7 @@ void *new_constraint_data (short type) case CONSTRAINT_TYPE_SIZELIKE: { bSizeLikeConstraint *data; - data = MEM_callocN(sizeof(bLocateLikeConstraint), "sizelikeConstraint"); + data = MEM_callocN(sizeof(bLocateLikeConstraint), "SizeLikeConstraint"); data->flag = SIZELIKE_X|SIZELIKE_Y|SIZELIKE_Z; result = data; } @@ -614,8 +618,10 @@ void *new_constraint_data (short type) case CONSTRAINT_TYPE_ACTION: { bActionConstraint *data; - data = MEM_callocN(sizeof(bActionConstraint), "actionConstraint"); - data->local= 1; + data = MEM_callocN(sizeof(bActionConstraint), "ActionConstraint"); + + /* set type to 20 (Loc X), as 0 is Rot X for backwards compatability */ + data->type = 20; result = data; } @@ -624,10 +630,10 @@ void *new_constraint_data (short type) { bLockTrackConstraint *data; data = MEM_callocN(sizeof(bLockTrackConstraint), "locktrackConstraint"); - + data->trackflag = TRACK_Y; data->lockflag = LOCK_Z; - + result = data; } break; @@ -680,32 +686,11 @@ void *new_constraint_data (short type) case CONSTRAINT_TYPE_RIGIDBODYJOINT: { bRigidBodyJointConstraint *data; - int i; - Base *base_iter; - data = MEM_callocN(sizeof(bRigidBodyJointConstraint), "RigidBodyToConstraint"); - base_iter = G.scene->base.first; - while( base_iter && !data->tar ) { - if( ( ( base_iter->flag & SELECT ) && -// ( base_iter->lay & G.vd->lay ) ) && - ( base_iter != G.scene->basact ) ) - ) - data->tar=base_iter->object; - base_iter = base_iter->next; - } + + // removed code which set target of this constraint data->type=1; - data->pivX=0.0; - data->pivY=0.0; - data->pivZ=0.0; - data->axX=0.0; - data->axY=0.0; - data->axZ=0.0; - for (i=0;i<6;i++) - { - data->minLimit[i]=0.0; - data->maxLimit[i]=0.0; - } - data->extraFz=0.0; + result = data; } break; @@ -716,6 +701,19 @@ void *new_constraint_data (short type) result = data; } break; + case CONSTRAINT_TYPE_CHILDOF: + { + bChildOfConstraint *data; + data = MEM_callocN(sizeof(bChildOfConstraint), "ChildOfConstraint"); + + data->flag = (CHILDOF_LOCX | CHILDOF_LOCY | CHILDOF_LOCZ | + CHILDOF_ROTX |CHILDOF_ROTY | CHILDOF_ROTZ | + CHILDOF_SIZEX | CHILDOF_SIZEY | CHILDOF_SIZEZ); + Mat4One(data->invmat); + + result = data; + } + break; default: result = NULL; @@ -755,55 +753,334 @@ bConstraintChannel *verify_constraint_channel (ListBase *list, const char *name) /* ***************** Evaluating ********************* */ -/* does ipos only */ +/* package an object/bone for use in constraint evaluation */ +/* This function MEM_calloc's a bConstraintOb struct, that will need to be freed after evaluation */ +bConstraintOb *constraints_make_evalob (Object *ob, void *subdata, short datatype) +{ + bConstraintOb *cob; + + /* create regardless of whether we have any data! */ + cob= MEM_callocN(sizeof(bConstraintOb), "bConstraintOb"); + + /* based on type of available data */ + switch (datatype) { + case TARGET_OBJECT: + { + /* disregard subdata... calloc should set other values right */ + if (ob) { + cob->ob = ob; + cob->type = datatype; + Mat4CpyMat4(cob->matrix, ob->obmat); + Mat4CpyMat4(cob->startmat, ob->obmat); + } + else + Mat4One(cob->matrix); + } + break; + case TARGET_BONE: + { + /* only set if we have valid bone, otherwise default */ + if (ob && subdata) { + cob->ob = ob; + cob->pchan = (bPoseChannel *)subdata; + cob->type = datatype; + + /* matrix in world-space */ + Mat4MulMat4 (cob->matrix, cob->pchan->pose_mat, ob->obmat); + Mat4CpyMat4(cob->startmat, cob->matrix); + } + else + Mat4One(cob->matrix); + Mat4CpyMat4(cob->startmat, cob->matrix); + } + break; + + default: // other types not yet handled + Mat4One(cob->matrix); + break; + } + + return cob; +} + +/* cleanup after constraint evaluation */ +void constraints_clear_evalob (bConstraintOb *cob) +{ + float delta[4][4], imat[4][4]; + + /* prevent crashes */ + if (cob == NULL) + return; + + /* calculate delta of constraints evaluation */ + Mat4Invert(imat, cob->startmat); + Mat4MulMat4(delta, cob->matrix, imat); + + /* copy matrices back to source */ + switch (cob->type) { + case TARGET_OBJECT: + { + /* copy new ob-matrix back to owner */ + Mat4CpyMat4(cob->ob->obmat, cob->matrix); + + /* copy inverse of delta back to owner */ + Mat4Invert(cob->ob->constinv, delta); + } + break; + case TARGET_BONE: + { + /* copy new pose-matrix back to owner */ + Mat4MulMat4(cob->pchan->pose_mat, cob->matrix, cob->ob->imat); + + /* copy inverse of delta back to owner */ + Mat4Invert(cob->pchan->constinv, delta); + } + break; + } + + /* free tempolary struct */ + MEM_freeN(cob); +} + +/* -------------------------------- Constraint Channels ---------------------------- */ + +/* does IPO's of constraint channels only */ void do_constraint_channels (ListBase *conbase, ListBase *chanbase, float ctime) { bConstraint *con; bConstraintChannel *chan; - IpoCurve *icu=NULL; + IpoCurve *icu= NULL; + /* for each Constraint, calculate its Influence from the corresponding ConstraintChannel */ for (con=conbase->first; con; con=con->next) { chan = get_constraint_channel(chanbase, con->name); - if (chan && chan->ipo){ + if (chan && chan->ipo) { calc_ipo(chan->ipo, ctime); - for (icu=chan->ipo->curve.first; icu; icu=icu->next){ - switch (icu->adrcode){ - case CO_ENFORCE: - con->enforce = icu->curval; - if (con->enforce<0.0f) con->enforce= 0.0f; - else if (con->enforce>1.0f) con->enforce= 1.0f; - break; + for (icu=chan->ipo->curve.first; icu; icu=icu->next) { + switch (icu->adrcode) { + case CO_ENFORCE: + { + con->enforce = icu->curval; + if (con->enforce<0.0f) con->enforce= 0.0f; + else if (con->enforce>1.0f) con->enforce= 1.0f; + } + break; } } } } } -static void constraint_target_to_mat4 (Object *ob, const char *substring, float mat[][4], float size[3]) +/* ------------------------------- Space-Conversion API ---------------------------- */ + +/* This function is responsible for the correct transformations/conversions + * of a matrix from one space to another for constraint evaluation. + * For now, this is only implemented for Objects and PoseChannels. + */ +static void constraint_mat_convertspace (Object *ob, bPoseChannel *pchan, float mat[][4], short from, short to) { + float tempmat[4][4]; + float diff_mat[4][4]; + float imat[4][4]; + + /* prevent crashes in these unlikely events */ + if (ob==NULL || mat==NULL) return; + /* optimise trick - check if need to do anything */ + if (from == to) return; + + /* are we dealing with pose-channels or objects */ + if (pchan) { + /* pose channels */ + switch (from) { + case CONSTRAINT_SPACE_WORLD: /* ---------- FROM WORLDSPACE ---------- */ + { + /* world to pose */ + if (to==CONSTRAINT_SPACE_POSE || to==CONSTRAINT_SPACE_LOCAL || to==CONSTRAINT_SPACE_PARLOCAL) { + Mat4Invert(imat, ob->obmat); + Mat4CpyMat4(tempmat, mat); + Mat4MulMat4(mat, tempmat, imat); + } + /* pose to local */ + if (to == CONSTRAINT_SPACE_LOCAL) { + /* call self with slightly different values */ + constraint_mat_convertspace(ob, pchan, mat, CONSTRAINT_SPACE_POSE, to); + } + /* pose to local + parent */ + else if (to == CONSTRAINT_SPACE_PARLOCAL) { + /* call self with slightly different values */ + constraint_mat_convertspace(ob, pchan, mat, CONSTRAINT_SPACE_POSE, to); + } + } + break; + case CONSTRAINT_SPACE_POSE: /* ---------- FROM POSESPACE ---------- */ + { + /* pose to world */ + if (to == CONSTRAINT_SPACE_WORLD) { + Mat4CpyMat4(tempmat, mat); + Mat4MulMat4(mat, tempmat, ob->obmat); + } + /* pose to local */ + else if (to == CONSTRAINT_SPACE_LOCAL) { + if (pchan->bone) { + if (pchan->parent && pchan->parent->bone) { + float offs_bone[4][4]; + + /* construct offs_bone the same way it is done in armature.c */ + Mat4CpyMat3(offs_bone, pchan->bone->bone_mat); + VECCOPY(offs_bone[3], pchan->bone->head); + offs_bone[3][1]+= pchan->parent->bone->length; + + if (pchan->bone->flag & BONE_HINGE) { + /* pose_mat = par_pose-space_location * chan_mat */ + float tmat[4][4]; + + /* the rotation of the parent restposition */ + Mat4CpyMat4(tmat, pchan->parent->bone->arm_mat); + + /* the location of actual parent transform */ + VECCOPY(tmat[3], offs_bone[3]); + offs_bone[3][0]= offs_bone[3][1]= offs_bone[3][2]= 0.0f; + Mat4MulVecfl(pchan->parent->pose_mat, tmat[3]); + + Mat4MulMat4(diff_mat, offs_bone, tmat); + Mat4Invert(imat, diff_mat); + } + else { + /* pose_mat = par_pose_mat * bone_mat * chan_mat */ + Mat4MulMat4(diff_mat, pchan->parent->pose_mat, offs_bone); + Mat4Invert(imat, diff_mat); + } + } + else { + /* pose_mat = chan_mat * arm_mat */ + Mat4Invert(imat, pchan->bone->arm_mat); + } + + Mat4CpyMat4(tempmat, mat); + Mat4MulMat4(mat, tempmat, imat); + } + } + /* pose to local with parent */ + else if (to == CONSTRAINT_SPACE_PARLOCAL) { + if (pchan->bone) { + Mat4Invert(imat, pchan->bone->arm_mat); + Mat4CpyMat4(tempmat, mat); + Mat4MulMat4(mat, tempmat, imat); + } + } + } + break; + case CONSTRAINT_SPACE_LOCAL: /* ------------ FROM LOCALSPACE --------- */ + { + /* local to pose */ + if (to==CONSTRAINT_SPACE_POSE || to==CONSTRAINT_SPACE_WORLD) { + /* do inverse procedure that was done for pose to local */ + if (pchan->bone) { + /* we need the posespace_matrix = local_matrix + (parent_posespace_matrix + restpos) */ + if (pchan->parent) { + float offs_bone[4][4]; + + /* construct offs_bone the same way it is done in armature.c */ + Mat4CpyMat3(offs_bone, pchan->bone->bone_mat); + VECCOPY(offs_bone[3], pchan->bone->head); + offs_bone[3][1]+= pchan->parent->bone->length; + + Mat4MulMat4(diff_mat, offs_bone, pchan->parent->pose_mat); + Mat4CpyMat4(tempmat, mat); + Mat4MulMat4(mat, tempmat, diff_mat); + } + else { + Mat4CpyMat4(diff_mat, pchan->bone->arm_mat); + + Mat4CpyMat4(tempmat, mat); + Mat4MulMat4(mat, tempmat, diff_mat); + } + } + } + /* local to world */ + if (to == CONSTRAINT_SPACE_WORLD) { + /* call self with slightly different values */ + constraint_mat_convertspace(ob, pchan, mat, CONSTRAINT_SPACE_POSE, to); + } + } + break; + case CONSTRAINT_SPACE_PARLOCAL: /* -------------- FROM LOCAL WITH PARENT ---------- */ + { + /* local to pose */ + if (to==CONSTRAINT_SPACE_POSE || to==CONSTRAINT_SPACE_WORLD) { + if (pchan->bone) { + Mat4CpyMat4(diff_mat, pchan->bone->arm_mat); + Mat4CpyMat4(tempmat, mat); + Mat4MulMat4(mat, diff_mat, tempmat); + } + } + /* local to world */ + if (to == CONSTRAINT_SPACE_WORLD) { + /* call self with slightly different values */ + constraint_mat_convertspace(ob, pchan, mat, CONSTRAINT_SPACE_POSE, to); + } + } + break; + } + } + else { + /* objects */ + if (from==CONSTRAINT_SPACE_WORLD && to==CONSTRAINT_SPACE_LOCAL) { + /* check if object has a parent - otherwise this won't work */ + if (ob->parent) { + /* 'subtract' parent's effects from owner */ + Mat4MulMat4(diff_mat, ob->parentinv, ob->parent->obmat); + Mat4Invert(imat, diff_mat); + Mat4CpyMat4(tempmat, mat); + Mat4MulMat4(mat, tempmat, imat); + } + } + else if (from==CONSTRAINT_SPACE_LOCAL && to==CONSTRAINT_SPACE_WORLD) { + /* check that object has a parent - otherwise this won't work */ + if (ob->parent) { + /* 'add' parent's effect back to owner */ + Mat4CpyMat4(tempmat, mat); + Mat4MulMat4(diff_mat, ob->parentinv, ob->parent->obmat); + Mat4MulMat4(mat, tempmat, diff_mat); + } + } + } +} + +/* ------------------------------- Target ---------------------------- */ +/* generic function to get the appropriate matrix for most target cases */ +/* The cases where the target can be object data have not been implemented */ +// TODO: add 'space' handling stuff here! +static void constraint_target_to_mat4 (Object *ob, const char *substring, float mat[][4], short from, short to) +{ /* Case OBJECT */ if (!strlen(substring)) { - Mat4CpyMat4 (mat, ob->obmat); - VECCOPY (size, ob->size); // whats this for, hack! (ton) + Mat4CpyMat4(mat, ob->obmat); + constraint_mat_convertspace(ob, NULL, mat, from, to); + } + /* Case VERTEXGROUP */ + else if (ELEM(ob->type, OB_MESH, OB_LATTICE)) { + /* devise a matrix from the data in the vertexgroup */ + /* TODO: will be handled in other files */ } /* Case BONE */ else { bPoseChannel *pchan; - float bsize[3]={1, 1, 1}; - + pchan = get_pose_channel(ob->pose, substring); - if (pchan){ - /** - * Multiply the objectspace bonematrix by the skeletons's global - * transform to obtain the worldspace transformation of the target + if (pchan) { + /* Multiply the PoseSpace accumulation/final matrix for this + * PoseChannel by the Armature Object's Matrix to get a worldspace + * matrix. */ - Mat4MulMat4 (mat, pchan->pose_mat, ob->obmat); + Mat4MulMat4(mat, pchan->pose_mat, ob->obmat); } else - Mat4CpyMat4 (mat, ob->obmat); - - VECCOPY(size, bsize); // whats this for, hack! (ton) + Mat4CpyMat4(mat, ob->obmat); + + /* convert matrix space as required */ + constraint_mat_convertspace(ob, pchan, mat, from, to); } } @@ -826,7 +1103,7 @@ static void vectomat(float *vec, float *target_up, short axis, short upflag, sho float right[3]; float neg = -1; int right_index; - + VecCopyf(n, vec); if(Normalize(n) == 0.0) { n[0] = 0.0; @@ -883,7 +1160,7 @@ static void vectomat(float *vec, float *target_up, short axis, short upflag, sho m[axis][1] = n[1]; m[axis][2] = n[2]; } - + /* identity matrix - don't do anything if the two axes are the same */ else { m[0][0]= m[1][1]= m[2][2]= 1.0; m[0][1]= m[0][2]= m[0][3]= 0.0; @@ -895,9 +1172,9 @@ static void vectomat(float *vec, float *target_up, short axis, short upflag, sho /* called during solve_constraints */ /* also for make_parent, to find correct inverse of "follow path" */ -/* warning, ownerdata is void... is not Bone anymore, but PoseChannel or Object */ +/* warning: ownerdata is PoseChannel or Object */ /* ctime is global time, uncorrected for local bsystem_time */ -short get_constraint_target_matrix (bConstraint *con, short ownertype, void* ownerdata, float mat[][4], float size[3], float ctime) +short get_constraint_target_matrix (bConstraint *con, short ownertype, void *ownerdata, float mat[][4], float ctime) { short valid=0; @@ -914,87 +1191,64 @@ short get_constraint_target_matrix (bConstraint *con, short ownertype, void* own bActionConstraint *data = (bActionConstraint*)con->data; bPose *pose; bPoseChannel *pchan, *tchan; - float tempmat3[3][3]; - float eul[3]; - float s,t; + float tempmat[4][4], vec[3]; + float s, t; + short axis; - Mat4One(mat); // return mat + /* initialise return matrix */ + Mat4One(mat); + /* only continue if there is a target */ if (data->tar==NULL) return 0; - /* need proper check for bone... */ - if (data->subtarget[0]) { - pchan = get_pose_channel(data->tar->pose, data->subtarget); - if (pchan) { - float arm_mat[3][3], pose_mat[3][3]; /* arm mat should be bone mat! bug... */ - - Mat3CpyMat4(arm_mat, pchan->bone->arm_mat); - Mat3CpyMat4(pose_mat, pchan->pose_mat); - - /* new; true local rotation constraint */ - if(data->local) { - float diff_mat[3][3], par_mat[3][3], ipar_mat[3][3]; - /* we need the local rotation = current rotation - (parent rotation + restpos) */ - - if (pchan->parent) { - Mat3CpyMat4(par_mat, pchan->parent->pose_mat); - Mat3MulMat3(diff_mat, par_mat, arm_mat); - - Mat3Inv(ipar_mat, diff_mat); - } - else { - Mat3Inv(ipar_mat, arm_mat); - } - - Mat3MulMat3(tempmat3, ipar_mat, pose_mat); - } - else { /* we use the deform mat, for backwards compatibility */ - float imat[3][3]; - - Mat3Inv(imat, arm_mat); - Mat3MulMat3(tempmat3, pose_mat, imat); - } - } - else Mat3One(tempmat3); + /* get the transform matrix of the target */ + constraint_target_to_mat4(data->tar, data->subtarget, tempmat, CONSTRAINT_SPACE_WORLD, con->tarspace); // FIXME: change these spaces + + /* determine where in transform range target is */ + /* data->type is mapped as follows for backwards compatability: + * 00,01,02 - rotation (it used to be like this) + * 10,11,12 - scaling + * 20,21,22 - location + */ + if (data->type < 10) { + /* extract rotation (is in whatever space target should be in) */ + Mat4ToEul(tempmat, vec); + vec[0] *= (float)(180.0/M_PI); + vec[1] *= (float)(180.0/M_PI); + vec[2] *= (float)(180.0/M_PI); + axis= data->type; + } + else if (data->type < 20) { + /* extract scaling (is in whatever space target should be in) */ + Mat4ToSize(tempmat, vec); + axis= data->type - 10; } else { - float ans[4][4]; - - constraint_target_to_mat4(data->tar, data->subtarget, ans, size); - /* extract rotation, is in global world coordinates */ - Mat3CpyMat4(tempmat3, ans); + /* extract location */ + VECCOPY(vec, tempmat[3]); + axis= data->type - 20; } - Mat3ToEul(tempmat3, eul); - eul[0] *= (float)(180.0/M_PI); - eul[1] *= (float)(180.0/M_PI); - eul[2] *= (float)(180.0/M_PI); - /* Target defines the animation */ - s = (eul[data->type]-data->min)/(data->max-data->min); - if (s<0) - s=0; - if (s>1) - s=1; - + s = (vec[axis]-data->min) / (data->max-data->min); + CLAMP(s, 0, 1); t = ( s * (data->end-data->start)) + data->start; - + /* Get the appropriate information from the action, we make temp pose */ pose = MEM_callocN(sizeof(bPose), "pose"); pchan = ownerdata; tchan= verify_pose_channel(pose, pchan->name); - extract_pose_from_action (pose, data->act, t); + extract_pose_from_action(pose, data->act, t); chan_calc_mat(tchan); Mat4CpyMat4(mat, tchan->chan_mat); - + /* Clean up */ free_pose_channels(pose); MEM_freeN(pose); } - } break; case CONSTRAINT_TYPE_LOCLIKE: @@ -1006,77 +1260,80 @@ short get_constraint_target_matrix (bConstraint *con, short ownertype, void* own if (strlen(data->subtarget)) { bPoseChannel *pchan; float tmat[4][4]; - float bsize[3]={1, 1, 1}; pchan = get_pose_channel(ob->pose, data->subtarget); if (pchan) { Mat4CpyMat4(tmat, pchan->pose_mat); - if (data->flag & LOCLIKE_TIP) + if (data->flag & LOCLIKE_TIP) { VECCOPY(tmat[3], pchan->pose_tail); + } - Mat4MulMat4 (mat, tmat, ob->obmat); + Mat4MulMat4(mat, tmat, ob->obmat); } else - Mat4CpyMat4 (mat, ob->obmat); - - VECCOPY(size, bsize); // what's this hack for? + Mat4CpyMat4(mat, ob->obmat); + + /* convert matrix space as required */ + constraint_mat_convertspace(ob, pchan, mat, CONSTRAINT_SPACE_WORLD, con->tarspace); } else { - Mat4CpyMat4 (mat, ob->obmat); - VECCOPY(size, data->tar->size); // what's this hack for? + Mat4CpyMat4(mat, ob->obmat); + + /* convert matrix space as required */ + constraint_mat_convertspace(ob, NULL, mat, CONSTRAINT_SPACE_WORLD, con->tarspace); } valid=1; } else - Mat4One (mat); + Mat4One(mat); } break; - case CONSTRAINT_TYPE_MINMAX: + case CONSTRAINT_TYPE_ROTLIKE: { - bMinMaxConstraint *data = (bMinMaxConstraint*)con->data; - + bRotateLikeConstraint *data; + data = (bRotateLikeConstraint*)con->data; + if (data->tar) { - constraint_target_to_mat4(data->tar, data->subtarget, mat, size); + constraint_target_to_mat4(data->tar, data->subtarget, mat, CONSTRAINT_SPACE_WORLD, con->tarspace); valid=1; } else - Mat4One (mat); + Mat4One(mat); } break; - case CONSTRAINT_TYPE_ROTLIKE: + case CONSTRAINT_TYPE_SIZELIKE: { - bRotateLikeConstraint *data; - data = (bRotateLikeConstraint*)con->data; - + bSizeLikeConstraint *data; + data = (bSizeLikeConstraint*)con->data; + if (data->tar) { - constraint_target_to_mat4(data->tar, data->subtarget, mat, size); + constraint_target_to_mat4(data->tar, data->subtarget, mat, CONSTRAINT_SPACE_WORLD, con->tarspace); valid=1; } else - Mat4One (mat); + Mat4One(mat); } break; - case CONSTRAINT_TYPE_SIZELIKE: + case CONSTRAINT_TYPE_MINMAX: { - bSizeLikeConstraint *data; - data = (bSizeLikeConstraint*)con->data; - + bMinMaxConstraint *data = (bMinMaxConstraint*)con->data; + if (data->tar) { - constraint_target_to_mat4(data->tar, data->subtarget, mat, size); + constraint_target_to_mat4(data->tar, data->subtarget, mat, CONSTRAINT_SPACE_WORLD, con->tarspace); valid=1; } else - Mat4One (mat); + Mat4One(mat); } break; case CONSTRAINT_TYPE_TRACKTO: { bTrackToConstraint *data; data = (bTrackToConstraint*)con->data; - + if (data->tar) { - constraint_target_to_mat4(data->tar, data->subtarget, mat, size); + constraint_target_to_mat4(data->tar, data->subtarget, mat, CONSTRAINT_SPACE_WORLD, con->tarspace); valid=1; } else @@ -1087,13 +1344,13 @@ short get_constraint_target_matrix (bConstraint *con, short ownertype, void* own { bKinematicConstraint *data; data = (bKinematicConstraint*)con->data; - + if (data->tar) { - constraint_target_to_mat4(data->tar, data->subtarget, mat, size); + constraint_target_to_mat4(data->tar, data->subtarget, mat, CONSTRAINT_SPACE_WORLD, con->tarspace); valid=1; } else if (data->flag & CONSTRAINT_IK_AUTO) { - Object *ob= ownerdata; + Object *ob= (Object *)ownerdata; if (ob==NULL) Mat4One(mat); @@ -1107,46 +1364,46 @@ short get_constraint_target_matrix (bConstraint *con, short ownertype, void* own } } else - Mat4One (mat); + Mat4One(mat); } break; case CONSTRAINT_TYPE_LOCKTRACK: { bLockTrackConstraint *data; data = (bLockTrackConstraint*)con->data; - + if (data->tar) { - constraint_target_to_mat4(data->tar, data->subtarget, mat, size); + constraint_target_to_mat4(data->tar, data->subtarget, mat, CONSTRAINT_SPACE_WORLD, con->tarspace); valid=1; } else - Mat4One (mat); + Mat4One(mat); } break; case CONSTRAINT_TYPE_FOLLOWPATH: { bFollowPathConstraint *data; data = (bFollowPathConstraint*)con->data; - + if (data->tar) { Curve *cu; - float q[4], vec[4], dir[3], *quat, x1, totmat[4][4]; + float q[4], vec[4], dir[3], *quat, x1; + float totmat[4][4]; float curvetime; - - Mat4One (totmat); - Mat4One (mat); - + + Mat4One(totmat); + Mat4One(mat); + cu= data->tar->data; - - /* note; when creating constraints that follow path, the curve gets the CU_PATH set now, + + /* note: when creating constraints that follow path, the curve gets the CU_PATH set now, currently for paths to work it needs to go through the bevlist/displist system (ton) */ if (cu->path==NULL || cu->path->data==NULL) /* only happens on reload file, but violates depsgraph still... fix! */ makeDispListCurveTypes(data->tar, 0); if (cu->path && cu->path->data) { - curvetime= bsystem_time(data->tar, data->tar->parent, (float)ctime, 0.0) - data->offset; - + if (calc_ipo_spec(cu->ipo, CU_SPEED, &curvetime)==0) { curvetime /= cu->pathlen; CLAMP(curvetime, 0.0, 1.0); @@ -1167,27 +1424,27 @@ short get_constraint_target_matrix (bConstraint *con, short ownertype, void* own QuatToMat4(quat, totmat); } VECCOPY(totmat[3], vec); - + Mat4MulSerie(mat, data->tar->obmat, totmat, NULL, NULL, NULL, NULL, NULL, NULL); } } valid=1; } else - Mat4One (mat); + Mat4One(mat); } break; case CONSTRAINT_TYPE_STRETCHTO: { bStretchToConstraint *data; data = (bStretchToConstraint*)con->data; - + if (data->tar) { - constraint_target_to_mat4(data->tar, data->subtarget, mat, size); + constraint_target_to_mat4(data->tar, data->subtarget, mat, CONSTRAINT_SPACE_WORLD, con->tarspace); valid = 1; } else - Mat4One (mat); + Mat4One(mat); } break; case CONSTRAINT_TYPE_PYTHON: @@ -1207,11 +1464,11 @@ short get_constraint_target_matrix (bConstraint *con, short ownertype, void* own /* if the script doesn't set the target matrix for any reason, fall back to standard methods */ if (BPY_pyconstraint_targets(data, mat) < 1) { if (data->tar) { - constraint_target_to_mat4(data->tar, data->subtarget, mat, size); + constraint_target_to_mat4(data->tar, data->subtarget, mat, CONSTRAINT_SPACE_WORLD, con->tarspace); valid = 1; } else - Mat4One (mat); + Mat4One(mat); } } break; @@ -1219,18 +1476,33 @@ short get_constraint_target_matrix (bConstraint *con, short ownertype, void* own { bClampToConstraint *data; data = (bClampToConstraint*)con->data; - + if (data->tar) { Curve *cu= data->tar->data; /* note; when creating constraints that follow path, the curve gets the CU_PATH set now, currently for paths to work it needs to go through the bevlist/displist system (ton) */ - if(cu->path==NULL || cu->path->data==NULL) /* only happens on reload file, but violates depsgraph still... fix! */ + if (cu->path==NULL || cu->path->data==NULL) /* only happens on reload file, but violates depsgraph still... fix! */ makeDispListCurveTypes(data->tar, 0); + + valid = 1; } - Mat4One (mat); + Mat4One(mat); + } + break; + case CONSTRAINT_TYPE_CHILDOF: + { + bChildOfConstraint *data; + data= (bChildOfConstraint *)con->data; + + if (data->tar) { + constraint_target_to_mat4(data->tar, data->subtarget, mat, CONSTRAINT_SPACE_WORLD, con->tarspace); + valid = 1; + } + else + Mat4One(mat); } break; @@ -1242,19 +1514,16 @@ short get_constraint_target_matrix (bConstraint *con, short ownertype, void* own return valid; } -/* only called during solve_constraints */ -/* bone constraints create a fake object to work on, then ob is a workob */ -/* if ownerdata is set, it's the posechannel */ -void evaluate_constraint (bConstraint *constraint, Object *ob, short ownertype, void *ownerdata, float targetmat[][4]) +/* ---------------------------------------------- Constraint Evaluation ------------------------------------------------- */ + +/* This is only called during solve_constraints to solve a particular constraint. + * It works on ownermat, and uses targetmat to help accomplish its tasks. + */ +static void evaluate_constraint (bConstraint *constraint, float ownermat[][4], float targetmat[][4]) { - float M_oldmat[4][4]; - float M_identity[4][4]; - - if (!constraint || !ob) + if (constraint == NULL || constraint->data == NULL) return; - - Mat4One (M_identity); - + switch (constraint->type) { case CONSTRAINT_TYPE_NULL: case CONSTRAINT_TYPE_KINEMATIC: /* removed */ @@ -1263,8 +1532,8 @@ void evaluate_constraint (bConstraint *constraint, Object *ob, short ownertype, { bPythonConstraint *data; - data= constraint->data; - BPY_pyconstraint_eval(data, ob->obmat, ownertype, ownerdata, targetmat); + data = constraint->data; + BPY_pyconstraint_eval(data, ownermat, targetmat); } break; case CONSTRAINT_TYPE_ACTION: @@ -1273,9 +1542,9 @@ void evaluate_constraint (bConstraint *constraint, Object *ob, short ownertype, float temp[4][4]; data = constraint->data; - Mat4CpyMat4 (temp, ob->obmat); + Mat4CpyMat4(temp, ownermat); - Mat4MulMat4(ob->obmat, targetmat, temp); + Mat4MulMat4(ownermat, targetmat, temp); } break; case CONSTRAINT_TYPE_LOCLIKE: @@ -1286,25 +1555,25 @@ void evaluate_constraint (bConstraint *constraint, Object *ob, short ownertype, data = constraint->data; if (data->flag & LOCLIKE_OFFSET) - VECCOPY(offset, ob->obmat[3]); + VECCOPY(offset, ownermat[3]); if (data->flag & LOCLIKE_X) { - ob->obmat[3][0] = targetmat[3][0]; + ownermat[3][0] = targetmat[3][0]; - if(data->flag & LOCLIKE_X_INVERT) ob->obmat[3][0] *= -1; - ob->obmat[3][0] += offset[0]; + if(data->flag & LOCLIKE_X_INVERT) ownermat[3][0] *= -1; + ownermat[3][0] += offset[0]; } if (data->flag & LOCLIKE_Y) { - ob->obmat[3][1] = targetmat[3][1]; + ownermat[3][1] = targetmat[3][1]; - if(data->flag & LOCLIKE_Y_INVERT) ob->obmat[3][1] *= -1; - ob->obmat[3][1] += offset[1]; + if(data->flag & LOCLIKE_Y_INVERT) ownermat[3][1] *= -1; + ownermat[3][1] += offset[1]; } if (data->flag & LOCLIKE_Z) { - ob->obmat[3][2] = targetmat[3][2]; + ownermat[3][2] = targetmat[3][2]; - if(data->flag & LOCLIKE_Z_INVERT) ob->obmat[3][2] *= -1; - ob->obmat[3][2] += offset[2]; + if(data->flag & LOCLIKE_Z_INVERT) ownermat[3][2] *= -1; + ownermat[3][2] += offset[2]; } } break; @@ -1314,36 +1583,52 @@ void evaluate_constraint (bConstraint *constraint, Object *ob, short ownertype, float loc[3]; float eul[3], obeul[3]; float size[3]; + short changed= 0; data = constraint->data; - VECCOPY(loc, ob->obmat[3]); - Mat4ToSize(ob->obmat, size); + VECCOPY(loc, ownermat[3]); + Mat4ToSize(ownermat, size); Mat4ToEul(targetmat, eul); - Mat4ToEul(ob->obmat, obeul); - - if(data->flag != (ROTLIKE_X|ROTLIKE_Y|ROTLIKE_Z)) { - if(!(data->flag & ROTLIKE_X)) { - eul[0]= obeul[0]; - } - if(!(data->flag & ROTLIKE_Y)) { - eul[1]= obeul[1]; - } - if(!(data->flag & ROTLIKE_Z)) { - eul[2]= obeul[2]; - } - compatible_eul(eul, obeul); + Mat4ToEul(ownermat, obeul); + + if ((data->flag & ROTLIKE_X)==0) { + eul[0] = obeul[0]; + changed = 1; + } + else if (data->flag & ROTLIKE_X_INVERT) { + eul[0] *= -1; + changed = 1; + } + if ((data->flag & ROTLIKE_Y)==0) { + eul[1] = obeul[1]; + changed = 1; + } + else if (data->flag & ROTLIKE_Y_INVERT) { + eul[1] *= -1; + changed = 1; + } + if ((data->flag & ROTLIKE_Z)==0) { + eul[2] = obeul[2]; + changed = 1; + } + else if (data->flag & ROTLIKE_Z_INVERT) { + eul[2] *= -1; + changed = 1; } - if((data->flag & ROTLIKE_X) && (data->flag & ROTLIKE_X_INVERT)) - eul[0]*= -1; - if((data->flag & ROTLIKE_Y) && (data->flag & ROTLIKE_Y_INVERT)) - eul[1]*= -1; - if((data->flag & ROTLIKE_Z) && (data->flag & ROTLIKE_Z_INVERT)) - eul[2]*= -1; - LocEulSizeToMat4(ob->obmat, loc, eul, size); + if (changed) { + compatible_eul(eul, obeul); + LocEulSizeToMat4(ownermat, loc, eul, size); + } + else { + float quat[4]; + + Mat4ToQuat(targetmat, quat); + LocQuatSizeToMat4(ownermat, loc, quat, size); + } } break; case CONSTRAINT_TYPE_SIZELIKE: @@ -1354,36 +1639,36 @@ void evaluate_constraint (bConstraint *constraint, Object *ob, short ownertype, data = constraint->data; Mat4ToSize(targetmat, size); - Mat4ToSize(ob->obmat, obsize); + Mat4ToSize(ownermat, obsize); - if (data->flag & SIZELIKE_X && obsize[0] != 0) - VecMulf(ob->obmat[0], size[0] / obsize[0]); - if (data->flag & SIZELIKE_Y && obsize[1] != 0) - VecMulf(ob->obmat[1], size[1] / obsize[1]); - if (data->flag & SIZELIKE_Z && obsize[2] != 0) - VecMulf(ob->obmat[2], size[2] / obsize[2]); + if ((data->flag & SIZELIKE_X) && obsize[0] != 0) + VecMulf(ownermat[0], size[0] / obsize[0]); + if ((data->flag & SIZELIKE_Y) && obsize[1] != 0) + VecMulf(ownermat[1], size[1] / obsize[1]); + if ((data->flag & SIZELIKE_Z) && obsize[2] != 0) + VecMulf(ownermat[2], size[2] / obsize[2]); } break; case CONSTRAINT_TYPE_MINMAX: { bMinMaxConstraint *data; + float obmat[4][4], imat[4][4], tarmat[4][4], tmat[4][4]; float val1, val2; int index; - float obmat[4][4],imat[4][4],tarmat[4][4],tmat[4][4]; - + data = constraint->data; - Mat4CpyMat4(obmat,ob->obmat); - Mat4CpyMat4(tarmat,targetmat); + Mat4CpyMat4(obmat, ownermat); + Mat4CpyMat4(tarmat, targetmat); if (data->flag & MINMAX_USEROT) { /* take rotation of target into account by doing the transaction in target's localspace */ - Mat4Invert(imat,tarmat); - Mat4MulMat4(tmat,obmat,imat); - Mat4CpyMat4(obmat,tmat); + Mat4Invert(imat, tarmat); + Mat4MulMat4(tmat, obmat, imat); + Mat4CpyMat4(obmat, tmat); Mat4One(tarmat); } - + switch (data->minmaxflag) { case TRACK_Z: val1 = tarmat[3][2]; @@ -1432,11 +1717,11 @@ void evaluate_constraint (bConstraint *constraint, Object *ob, short ownertype, } if (data->flag & MINMAX_USEROT) { /* get out of localspace */ - Mat4MulMat4(tmat,obmat,targetmat); - Mat4CpyMat4(ob->obmat,tmat); + Mat4MulMat4(tmat, obmat, targetmat); + Mat4CpyMat4(ownermat, tmat); } else { - VECCOPY(ob->obmat[3],obmat[3]); + VECCOPY(ownermat[3], obmat[3]); } } else { @@ -1447,8 +1732,7 @@ void evaluate_constraint (bConstraint *constraint, Object *ob, short ownertype, case CONSTRAINT_TYPE_TRACKTO: { bTrackToConstraint *data; - float size[3]; - float vec[3]; + float size[3], vec[3]; float totmat[3][3]; float tmat[4][4]; @@ -1456,29 +1740,26 @@ void evaluate_constraint (bConstraint *constraint, Object *ob, short ownertype, if (data->tar) { /* Get size property, since ob->size is only the object's own relative size, not its global one */ - Mat4ToSize (ob->obmat, size); - - Mat4CpyMat4 (M_oldmat, ob->obmat); + Mat4ToSize (ownermat, size); /* Clear the object's rotation */ - ob->obmat[0][0]=size[0]; - ob->obmat[0][1]=0; - ob->obmat[0][2]=0; - ob->obmat[1][0]=0; - ob->obmat[1][1]=size[1]; - ob->obmat[1][2]=0; - ob->obmat[2][0]=0; - ob->obmat[2][1]=0; - ob->obmat[2][2]=size[2]; + ownermat[0][0]=size[0]; + ownermat[0][1]=0; + ownermat[0][2]=0; + ownermat[1][0]=0; + ownermat[1][1]=size[1]; + ownermat[1][2]=0; + ownermat[2][0]=0; + ownermat[2][1]=0; + ownermat[2][2]=size[2]; - VecSubf(vec, ob->obmat[3], targetmat[3]); - vectomat(vec, targetmat[2], + VecSubf(vec, ownermat[3], targetmat[3]); + vectomat(vec, ownermat[2], (short)data->reserved1, (short)data->reserved2, data->flags, totmat); - Mat4CpyMat4(tmat, ob->obmat); - - Mat4MulMat34(ob->obmat, totmat, tmat); + Mat4CpyMat4(tmat, ownermat); + Mat4MulMat34(ownermat, totmat, tmat); } } break; @@ -1495,10 +1776,8 @@ void evaluate_constraint (bConstraint *constraint, Object *ob, short ownertype, data = constraint->data; if (data->tar) { - Mat4CpyMat4 (M_oldmat, ob->obmat); - /* Vector object -> target */ - VecSubf(vec, targetmat[3], ob->obmat[3]); + VecSubf(vec, targetmat[3], ownermat[3]); switch (data->lockflag){ case LOCK_X: /* LOCK X */ { @@ -1506,74 +1785,70 @@ void evaluate_constraint (bConstraint *constraint, Object *ob, short ownertype, case TRACK_Y: /* LOCK X TRACK Y */ { /* Projection of Vector on the plane */ - Projf(vec2, vec, ob->obmat[0]); + Projf(vec2, vec, ownermat[0]); VecSubf(totmat[1], vec, vec2); Normalize(totmat[1]); - /* the x axis is fixed*/ - totmat[0][0] = ob->obmat[0][0]; - totmat[0][1] = ob->obmat[0][1]; - totmat[0][2] = ob->obmat[0][2]; + /* the x axis is fixed */ + totmat[0][0] = ownermat[0][0]; + totmat[0][1] = ownermat[0][1]; + totmat[0][2] = ownermat[0][2]; Normalize(totmat[0]); - /* the z axis gets mapped onto - a third orthogonal vector */ + /* the z axis gets mapped onto a third orthogonal vector */ Crossf(totmat[2], totmat[0], totmat[1]); } break; case TRACK_Z: /* LOCK X TRACK Z */ { /* Projection of Vector on the plane */ - Projf(vec2, vec, ob->obmat[0]); + Projf(vec2, vec, ownermat[0]); VecSubf(totmat[2], vec, vec2); Normalize(totmat[2]); - /* the x axis is fixed*/ - totmat[0][0] = ob->obmat[0][0]; - totmat[0][1] = ob->obmat[0][1]; - totmat[0][2] = ob->obmat[0][2]; + /* the x axis is fixed */ + totmat[0][0] = ownermat[0][0]; + totmat[0][1] = ownermat[0][1]; + totmat[0][2] = ownermat[0][2]; Normalize(totmat[0]); - /* the z axis gets mapped onto - a third orthogonal vector */ + /* the z axis gets mapped onto a third orthogonal vector */ Crossf(totmat[1], totmat[2], totmat[0]); } break; case TRACK_nY: /* LOCK X TRACK -Y */ { /* Projection of Vector on the plane */ - Projf(vec2, vec, ob->obmat[0]); + Projf(vec2, vec, ownermat[0]); VecSubf(totmat[1], vec, vec2); Normalize(totmat[1]); VecMulf(totmat[1],-1); - /* the x axis is fixed*/ - totmat[0][0] = ob->obmat[0][0]; - totmat[0][1] = ob->obmat[0][1]; - totmat[0][2] = ob->obmat[0][2]; + /* the x axis is fixed */ + totmat[0][0] = ownermat[0][0]; + totmat[0][1] = ownermat[0][1]; + totmat[0][2] = ownermat[0][2]; Normalize(totmat[0]); - /* the z axis gets mapped onto - a third orthogonal vector */ + /* the z axis gets mapped onto a third orthogonal vector */ Crossf(totmat[2], totmat[0], totmat[1]); } break; case TRACK_nZ: /* LOCK X TRACK -Z */ { /* Projection of Vector on the plane */ - Projf(vec2, vec, ob->obmat[0]); + Projf(vec2, vec, ownermat[0]); VecSubf(totmat[2], vec, vec2); Normalize(totmat[2]); VecMulf(totmat[2],-1); - /* the x axis is fixed*/ - totmat[0][0] = ob->obmat[0][0]; - totmat[0][1] = ob->obmat[0][1]; - totmat[0][2] = ob->obmat[0][2]; + /* the x axis is fixed */ + totmat[0][0] = ownermat[0][0]; + totmat[0][1] = ownermat[0][1]; + totmat[0][2] = ownermat[0][2]; Normalize(totmat[0]); - /* the z axis gets mapped onto - a third orthogonal vector */ + /* the z axis gets mapped onto a third orthogonal vector */ Crossf(totmat[1], totmat[2], totmat[0]); } break; @@ -1593,74 +1868,70 @@ void evaluate_constraint (bConstraint *constraint, Object *ob, short ownertype, case TRACK_X: /* LOCK Y TRACK X */ { /* Projection of Vector on the plane */ - Projf(vec2, vec, ob->obmat[1]); + Projf(vec2, vec, ownermat[1]); VecSubf(totmat[0], vec, vec2); Normalize(totmat[0]); - /* the y axis is fixed*/ - totmat[1][0] = ob->obmat[1][0]; - totmat[1][1] = ob->obmat[1][1]; - totmat[1][2] = ob->obmat[1][2]; + /* the y axis is fixed */ + totmat[1][0] = ownermat[1][0]; + totmat[1][1] = ownermat[1][1]; + totmat[1][2] = ownermat[1][2]; Normalize(totmat[1]); - /* the z axis gets mapped onto - a third orthogonal vector */ + /* the z axis gets mapped onto a third orthogonal vector */ Crossf(totmat[2], totmat[0], totmat[1]); } break; case TRACK_Z: /* LOCK Y TRACK Z */ { /* Projection of Vector on the plane */ - Projf(vec2, vec, ob->obmat[1]); + Projf(vec2, vec, ownermat[1]); VecSubf(totmat[2], vec, vec2); Normalize(totmat[2]); - /* the y axis is fixed*/ - totmat[1][0] = ob->obmat[1][0]; - totmat[1][1] = ob->obmat[1][1]; - totmat[1][2] = ob->obmat[1][2]; + /* the y axis is fixed */ + totmat[1][0] = ownermat[1][0]; + totmat[1][1] = ownermat[1][1]; + totmat[1][2] = ownermat[1][2]; Normalize(totmat[1]); - /* the z axis gets mapped onto - a third orthogonal vector */ + /* the z axis gets mapped onto a third orthogonal vector */ Crossf(totmat[0], totmat[1], totmat[2]); } break; case TRACK_nX: /* LOCK Y TRACK -X */ { /* Projection of Vector on the plane */ - Projf(vec2, vec, ob->obmat[1]); + Projf(vec2, vec, ownermat[1]); VecSubf(totmat[0], vec, vec2); Normalize(totmat[0]); VecMulf(totmat[0],-1); - /* the y axis is fixed*/ - totmat[1][0] = ob->obmat[1][0]; - totmat[1][1] = ob->obmat[1][1]; - totmat[1][2] = ob->obmat[1][2]; + /* the y axis is fixed */ + totmat[1][0] = ownermat[1][0]; + totmat[1][1] = ownermat[1][1]; + totmat[1][2] = ownermat[1][2]; Normalize(totmat[1]); - /* the z axis gets mapped onto - a third orthogonal vector */ + /* the z axis gets mapped onto a third orthogonal vector */ Crossf(totmat[2], totmat[0], totmat[1]); } break; case TRACK_nZ: /* LOCK Y TRACK -Z */ { /* Projection of Vector on the plane */ - Projf(vec2, vec, ob->obmat[1]); + Projf(vec2, vec, ownermat[1]); VecSubf(totmat[2], vec, vec2); Normalize(totmat[2]); VecMulf(totmat[2],-1); - /* the y axis is fixed*/ - totmat[1][0] = ob->obmat[1][0]; - totmat[1][1] = ob->obmat[1][1]; - totmat[1][2] = ob->obmat[1][2]; + /* the y axis is fixed */ + totmat[1][0] = ownermat[1][0]; + totmat[1][1] = ownermat[1][1]; + totmat[1][2] = ownermat[1][2]; Normalize(totmat[1]); - /* the z axis gets mapped onto - a third orthogonal vector */ + /* the z axis gets mapped onto a third orthogonal vector */ Crossf(totmat[0], totmat[1], totmat[2]); } break; @@ -1680,74 +1951,70 @@ void evaluate_constraint (bConstraint *constraint, Object *ob, short ownertype, case TRACK_X: /* LOCK Z TRACK X */ { /* Projection of Vector on the plane */ - Projf(vec2, vec, ob->obmat[2]); + Projf(vec2, vec, ownermat[2]); VecSubf(totmat[0], vec, vec2); Normalize(totmat[0]); - /* the z axis is fixed*/ - totmat[2][0] = ob->obmat[2][0]; - totmat[2][1] = ob->obmat[2][1]; - totmat[2][2] = ob->obmat[2][2]; + /* the z axis is fixed */ + totmat[2][0] = ownermat[2][0]; + totmat[2][1] = ownermat[2][1]; + totmat[2][2] = ownermat[2][2]; Normalize(totmat[2]); - /* the x axis gets mapped onto - a third orthogonal vector */ + /* the x axis gets mapped onto a third orthogonal vector */ Crossf(totmat[1], totmat[2], totmat[0]); } break; case TRACK_Y: /* LOCK Z TRACK Y */ { /* Projection of Vector on the plane */ - Projf(vec2, vec, ob->obmat[2]); + Projf(vec2, vec, ownermat[2]); VecSubf(totmat[1], vec, vec2); Normalize(totmat[1]); - /* the z axis is fixed*/ - totmat[2][0] = ob->obmat[2][0]; - totmat[2][1] = ob->obmat[2][1]; - totmat[2][2] = ob->obmat[2][2]; + /* the z axis is fixed */ + totmat[2][0] = ownermat[2][0]; + totmat[2][1] = ownermat[2][1]; + totmat[2][2] = ownermat[2][2]; Normalize(totmat[2]); - /* the x axis gets mapped onto - a third orthogonal vector */ + /* the x axis gets mapped onto a third orthogonal vector */ Crossf(totmat[0], totmat[1], totmat[2]); } break; case TRACK_nX: /* LOCK Z TRACK -X */ { /* Projection of Vector on the plane */ - Projf(vec2, vec, ob->obmat[2]); + Projf(vec2, vec, ownermat[2]); VecSubf(totmat[0], vec, vec2); Normalize(totmat[0]); VecMulf(totmat[0],-1); - /* the z axis is fixed*/ - totmat[2][0] = ob->obmat[2][0]; - totmat[2][1] = ob->obmat[2][1]; - totmat[2][2] = ob->obmat[2][2]; + /* the z axis is fixed */ + totmat[2][0] = ownermat[2][0]; + totmat[2][1] = ownermat[2][1]; + totmat[2][2] = ownermat[2][2]; Normalize(totmat[2]); - /* the x axis gets mapped onto - a third orthogonal vector */ + /* the x axis gets mapped onto a third orthogonal vector */ Crossf(totmat[1], totmat[2], totmat[0]); } break; case TRACK_nY: /* LOCK Z TRACK -Y */ { /* Projection of Vector on the plane */ - Projf(vec2, vec, ob->obmat[2]); + Projf(vec2, vec, ownermat[2]); VecSubf(totmat[1], vec, vec2); Normalize(totmat[1]); VecMulf(totmat[1],-1); - /* the z axis is fixed*/ - totmat[2][0] = ob->obmat[2][0]; - totmat[2][1] = ob->obmat[2][1]; - totmat[2][2] = ob->obmat[2][2]; + /* the z axis is fixed */ + totmat[2][0] = ownermat[2][0]; + totmat[2][1] = ownermat[2][1]; + totmat[2][2] = ownermat[2][2]; Normalize(totmat[2]); - /* the x axis gets mapped onto - a third orthogonal vector */ + /* the x axis gets mapped onto a third orthogonal vector */ Crossf(totmat[0], totmat[1], totmat[2]); } break; @@ -1770,19 +2037,19 @@ void evaluate_constraint (bConstraint *constraint, Object *ob, short ownertype, break; } /* Block to keep matrix heading */ - tmpmat[0][0] = ob->obmat[0][0];tmpmat[0][1] = ob->obmat[0][1];tmpmat[0][2] = ob->obmat[0][2]; - tmpmat[1][0] = ob->obmat[1][0];tmpmat[1][1] = ob->obmat[1][1];tmpmat[1][2] = ob->obmat[1][2]; - tmpmat[2][0] = ob->obmat[2][0];tmpmat[2][1] = ob->obmat[2][1];tmpmat[2][2] = ob->obmat[2][2]; + tmpmat[0][0] = ownermat[0][0];tmpmat[0][1] = ownermat[0][1];tmpmat[0][2] = ownermat[0][2]; + tmpmat[1][0] = ownermat[1][0];tmpmat[1][1] = ownermat[1][1];tmpmat[1][2] = ownermat[1][2]; + tmpmat[2][0] = ownermat[2][0];tmpmat[2][1] = ownermat[2][1];tmpmat[2][2] = ownermat[2][2]; Normalize(tmpmat[0]); Normalize(tmpmat[1]); Normalize(tmpmat[2]); Mat3Inv(invmat,tmpmat); - Mat3MulMat3(tmpmat,totmat,invmat); + Mat3MulMat3(tmpmat, totmat, invmat); totmat[0][0] = tmpmat[0][0];totmat[0][1] = tmpmat[0][1];totmat[0][2] = tmpmat[0][2]; totmat[1][0] = tmpmat[1][0];totmat[1][1] = tmpmat[1][1];totmat[1][2] = tmpmat[1][2]; totmat[2][0] = tmpmat[2][0];totmat[2][1] = tmpmat[2][1];totmat[2][2] = tmpmat[2][2]; - Mat4CpyMat4(tmat, ob->obmat); + Mat4CpyMat4(tmat, ownermat); mdet = Det3x3( totmat[0][0],totmat[0][1],totmat[0][2], totmat[1][0],totmat[1][1],totmat[1][2], @@ -1795,7 +2062,7 @@ void evaluate_constraint (bConstraint *constraint, Object *ob, short ownertype, } /* apply out transformaton to the object */ - Mat4MulMat34(ob->obmat, totmat, tmat); + Mat4MulMat34(ownermat, totmat, tmat); } } break; @@ -1804,27 +2071,28 @@ void evaluate_constraint (bConstraint *constraint, Object *ob, short ownertype, bFollowPathConstraint *data; float obmat[4][4]; float size[3], obsize[3]; - + data = constraint->data; - + if (data->tar) { /* get Object local transform (loc/rot/size) to determine transformation from path */ - object_to_mat4(ob, obmat); + //object_to_mat4(ob, obmat); + Mat4CpyMat4(obmat, ownermat); // FIXME!!! /* get scaling of object before applying constraint */ - Mat4ToSize(ob->obmat, size); - + Mat4ToSize(ownermat, size); + /* apply targetmat - containing location on path, and rotation */ - Mat4MulSerie(ob->obmat, targetmat, obmat, NULL, NULL, NULL, NULL, NULL, NULL); + Mat4MulSerie(ownermat, targetmat, obmat, NULL, NULL, NULL, NULL, NULL, NULL); /* un-apply scaling caused by path */ - Mat4ToSize(ob->obmat, obsize); + Mat4ToSize(ownermat, obsize); if (obsize[0] != 0) - VecMulf(ob->obmat[0], size[0] / obsize[0]); + VecMulf(ownermat[0], size[0] / obsize[0]); if (obsize[1] != 0) - VecMulf(ob->obmat[1], size[1] / obsize[1]); + VecMulf(ownermat[1], size[1] / obsize[1]); if (obsize[2] != 0) - VecMulf(ob->obmat[2], size[2] / obsize[2]); + VecMulf(ownermat[2], size[2] / obsize[2]); } } break; @@ -1837,22 +2105,22 @@ void evaluate_constraint (bConstraint *constraint, Object *ob, short ownertype, float dist; data = constraint->data; - Mat4ToSize (ob->obmat, size); + Mat4ToSize (ownermat, size); if (data->tar) { /* store X orientation before destroying obmat */ - xx[0] = ob->obmat[0][0]; - xx[1] = ob->obmat[0][1]; - xx[2] = ob->obmat[0][2]; + xx[0] = ownermat[0][0]; + xx[1] = ownermat[0][1]; + xx[2] = ownermat[0][2]; Normalize(xx); /* store Z orientation before destroying obmat */ - zz[0] = ob->obmat[2][0]; - zz[1] = ob->obmat[2][1]; - zz[2] = ob->obmat[2][2]; + zz[0] = ownermat[2][0]; + zz[1] = ownermat[2][1]; + zz[2] = ownermat[2][2]; Normalize(zz); - VecSubf(vec, ob->obmat[3], targetmat[3]); + VecSubf(vec, ownermat[3], targetmat[3]); vec[0] /= size[0]; vec[1] /= size[1]; vec[2] /= size[2]; @@ -1888,18 +2156,19 @@ void evaluate_constraint (bConstraint *constraint, Object *ob, short ownertype, } /* switch (data->volmode) */ /* Clear the object's rotation and scale */ - ob->obmat[0][0]=size[0]*scale[0]; - ob->obmat[0][1]=0; - ob->obmat[0][2]=0; - ob->obmat[1][0]=0; - ob->obmat[1][1]=size[1]*scale[1]; - ob->obmat[1][2]=0; - ob->obmat[2][0]=0; - ob->obmat[2][1]=0; - ob->obmat[2][2]=size[2]*scale[2]; + ownermat[0][0]=size[0]*scale[0]; + ownermat[0][1]=0; + ownermat[0][2]=0; + ownermat[1][0]=0; + ownermat[1][1]=size[1]*scale[1]; + ownermat[1][2]=0; + ownermat[2][0]=0; + ownermat[2][1]=0; + ownermat[2][2]=size[2]*scale[2]; - VecSubf(vec, ob->obmat[3], targetmat[3]); - Normalize(vec); + VecSubf(vec, ownermat[3], targetmat[3]); + Normalize(vec); + /* new Y aligns object target connection*/ totmat[1][0] = -vec[0]; totmat[1][1] = -vec[1]; @@ -1917,7 +2186,7 @@ void evaluate_constraint (bConstraint *constraint, Object *ob, short ownertype, totmat[2][2] = orth[2]; /* we decided to keep X plane*/ - Crossf(xx,orth, vec); + Crossf(xx, orth, vec); Normalize(xx); totmat[0][0] = xx[0]; totmat[0][1] = xx[1]; @@ -1935,7 +2204,7 @@ void evaluate_constraint (bConstraint *constraint, Object *ob, short ownertype, totmat[0][2] = -orth[2]; /* we decided to keep Z */ - Crossf(zz,orth, vec); + Crossf(zz, orth, vec); Normalize(zz); totmat[2][0] = zz[0]; totmat[2][1] = zz[1]; @@ -1943,9 +2212,9 @@ void evaluate_constraint (bConstraint *constraint, Object *ob, short ownertype, break; } /* switch (data->plane) */ - Mat4CpyMat4(tmat, ob->obmat); - - Mat4MulMat34(ob->obmat, totmat, tmat); + Mat4CpyMat4(tmat, ownermat); + + Mat4MulMat34(ownermat, totmat, tmat); } } break; @@ -1955,79 +2224,29 @@ void evaluate_constraint (bConstraint *constraint, Object *ob, short ownertype, data = constraint->data; - /* limit location relative to origin or parent */ - if ((data->flag2 & LIMIT_NOPARENT) && ob->parent) { - /* limiting relative to parent */ - float parmat[4][4]; /* matrix of parent */ - float objLoc[3], parLoc[3]; /* location of object, and location of parent */ - float relLoc[3]; /* objLoc - parLoc*/ - - /* get matrix of parent */ - Mat4CpyMat4(parmat, ob->parent->obmat); - - /* get locations as vectors */ - objLoc[0] = ob->obmat[3][0]; - objLoc[1] = ob->obmat[3][1]; - objLoc[2] = ob->obmat[3][2]; - - parLoc[0] = parmat[3][0]; - parLoc[1] = parmat[3][1]; - parLoc[2] = parmat[3][2]; - - /* get relative location of obj from parent */ - VecSubf(relLoc, objLoc, parLoc); - - /* limiting location */ - if (data->flag & LIMIT_XMIN) { - if(relLoc[0] < data->xmin) - ob->obmat[3][0] = (parLoc[0]+data->xmin); - } - if (data->flag & LIMIT_XMAX) { - if (relLoc[0] > data->xmax) - ob->obmat[3][0] = (parLoc[0]+data->xmax); - } - if (data->flag & LIMIT_YMIN) { - if(relLoc[1] < data->ymin) - ob->obmat[3][1] = (parLoc[1]+data->ymin); - } - if (data->flag & LIMIT_YMAX) { - if (relLoc[1] > data->ymax) - ob->obmat[3][1] = (parLoc[1]+data->ymax); - } - if (data->flag & LIMIT_ZMIN) { - if(relLoc[2] < data->zmin) - ob->obmat[3][2] = (parLoc[2]+data->zmin); - } - if (data->flag & LIMIT_ZMAX) { - if (relLoc[2] > data->zmax) - ob->obmat[3][2] = (parLoc[2]+data->zmax); - } - } else { - /* limiting relative to origin */ - if (data->flag & LIMIT_XMIN) { - if(ob->obmat[3][0] < data->xmin) - ob->obmat[3][0] = data->xmin; - } - if (data->flag & LIMIT_XMAX) { - if (ob->obmat[3][0] > data->xmax) - ob->obmat[3][0] = data->xmax; - } - if (data->flag & LIMIT_YMIN) { - if(ob->obmat[3][1] < data->ymin) - ob->obmat[3][1] = data->ymin; - } - if (data->flag & LIMIT_YMAX) { - if (ob->obmat[3][1] > data->ymax) - ob->obmat[3][1] = data->ymax; - } - if (data->flag & LIMIT_ZMIN) { - if(ob->obmat[3][2] < data->zmin) - ob->obmat[3][2] = data->zmin; - } - if (data->flag & LIMIT_ZMAX) { - if (ob->obmat[3][2] > data->zmax) - ob->obmat[3][2] = data->zmax; - } + if (data->flag & LIMIT_XMIN) { + if(ownermat[3][0] < data->xmin) + ownermat[3][0] = data->xmin; + } + if (data->flag & LIMIT_XMAX) { + if (ownermat[3][0] > data->xmax) + ownermat[3][0] = data->xmax; + } + if (data->flag & LIMIT_YMIN) { + if(ownermat[3][1] < data->ymin) + ownermat[3][1] = data->ymin; + } + if (data->flag & LIMIT_YMAX) { + if (ownermat[3][1] > data->ymax) + ownermat[3][1] = data->ymax; + } + if (data->flag & LIMIT_ZMIN) { + if(ownermat[3][2] < data->zmin) + ownermat[3][2] = data->zmin; + } + if (data->flag & LIMIT_ZMAX) { + if (ownermat[3][2] > data->zmax) + ownermat[3][2] = data->zmax; } } break; @@ -2040,10 +2259,10 @@ void evaluate_constraint (bConstraint *constraint, Object *ob, short ownertype, data = constraint->data; - VECCOPY(loc, ob->obmat[3]); - Mat4ToSize(ob->obmat, size); + VECCOPY(loc, ownermat[3]); + Mat4ToSize(ownermat, size); - Mat4ToEul(ob->obmat, eul); + Mat4ToEul(ownermat, eul); /* eulers: radians to degrees! */ eul[0] = (eul[0] / M_PI * 180); @@ -2078,85 +2297,52 @@ void evaluate_constraint (bConstraint *constraint, Object *ob, short ownertype, eul[1] = (eul[1] / 180 * M_PI); eul[2] = (eul[2] / 180 * M_PI); - LocEulSizeToMat4(ob->obmat, loc, eul, size); + LocEulSizeToMat4(ownermat, loc, eul, size); } break; case CONSTRAINT_TYPE_SIZELIMIT: { bSizeLimitConstraint *data; float obsize[3], size[3]; - int clearNegScale=0; data = constraint->data; - Mat4ToSize(ob->obmat, size); - Mat4ToSize(ob->obmat, obsize); + Mat4ToSize(ownermat, size); + Mat4ToSize(ownermat, obsize); if (data->flag & LIMIT_XMIN) { - if (ob->transflag & OB_NEG_SCALE) { - size[0] *= -1; - - if (size[0] < data->xmin) { - size[0] = data->xmin; - clearNegScale += 1; - } - } else { - if (size[0] < data->xmin) - size[0] = data->xmin; - } + if (size[0] < data->xmin) + size[0] = data->xmin; } if (data->flag & LIMIT_XMAX) { if (size[0] > data->xmax) size[0] = data->xmax; } if (data->flag & LIMIT_YMIN) { - if (ob->transflag & OB_NEG_SCALE) { - size[1] *= -1; - - if (size[1] < data->ymin) { - size[1] = data->ymin; - clearNegScale += 1; - } - } else { - if (size[1] < data->ymin) - size[1] = data->ymin; - } + if (size[1] < data->ymin) + size[1] = data->ymin; } if (data->flag & LIMIT_YMAX) { if (size[1] > data->ymax) size[1] = data->ymax; } if (data->flag & LIMIT_ZMIN) { - if (ob->transflag & OB_NEG_SCALE) { - size[2] *= -1; - - if (size[2] < data->zmin) { - size[2] = data->zmin; - clearNegScale += 1; - } - } else { - if (size[2] < data->zmin) - size[2] = data->zmin; - } + if (size[2] < data->zmin) + size[2] = data->zmin; } if (data->flag & LIMIT_ZMAX) { if (size[2] > data->zmax) size[2] = data->zmax; } - if (clearNegScale != 0) { - ob->transflag &= ~OB_NEG_SCALE; /* is this how we remove that flag? */ - } - - VecMulf(ob->obmat[0], size[0]/obsize[0]); - VecMulf(ob->obmat[1], size[1]/obsize[1]); - VecMulf(ob->obmat[2], size[2]/obsize[2]); + VecMulf(ownermat[0], size[0]/obsize[0]); + VecMulf(ownermat[1], size[1]/obsize[1]); + VecMulf(ownermat[2], size[2]/obsize[2]); } break; case CONSTRAINT_TYPE_RIGIDBODYJOINT: { - - + /* Do nothing. The GameEngine will take care of this.*/ } break; case CONSTRAINT_TYPE_CLAMPTO: @@ -2174,7 +2360,7 @@ void evaluate_constraint (bConstraint *constraint, Object *ob, short ownertype, else cu= data->tar->data; - Mat4CpyMat4(obmat, ob->obmat); + Mat4CpyMat4(obmat, ownermat); Mat4One(targetMatrix); VECCOPY(ownLoc, obmat[3]); @@ -2182,7 +2368,7 @@ void evaluate_constraint (bConstraint *constraint, Object *ob, short ownertype, minmax_object(data->tar, curveMin, curveMax); /* get targetmatrix */ - if(cu->path && cu->path->data) { + if (cu->path && cu->path->data) { float vec[4], dir[3], totmat[4][4]; float curvetime; short clamp_axis; @@ -2214,7 +2400,7 @@ void evaluate_constraint (bConstraint *constraint, Object *ob, short ownertype, else if (ownLoc[clamp_axis] >= curveMax[clamp_axis]) curvetime = 1.0; else - curvetime = (ownLoc[clamp_axis] - curveMin[clamp_axis]) / (curveMax[clamp_axis] - curveMin[clamp_axis]); // umm + curvetime = (ownLoc[clamp_axis] - curveMin[clamp_axis]) / (curveMax[clamp_axis] - curveMin[clamp_axis]); /* 3. position on curve */ if(where_on_path(data->tar, curvetime, vec, dir) ) { @@ -2226,7 +2412,59 @@ void evaluate_constraint (bConstraint *constraint, Object *ob, short ownertype, } /* obtain final object position */ - VECCOPY(ob->obmat[3], targetMatrix[3]); + VECCOPY(ownermat[3], targetMatrix[3]); + } + break; + case CONSTRAINT_TYPE_CHILDOF: + { + bChildOfConstraint *data; + + data = constraint->data; + + /* only evaluate if there is a target */ + if (data->tar) { + float parmat[4][4], invmat[4][4], tempmat[4][4]; + float loc[3], eul[3], size[3]; + float loco[3], eulo[3], sizo[3]; + + /* get offset (parent-inverse) matrix */ + Mat4CpyMat4(invmat, data->invmat); + + /* extract components of both matrices */ + VECCOPY(loc, targetmat[3]); + Mat4ToEul(targetmat, eul); + Mat4ToSize(targetmat, size); + + VECCOPY(loco, invmat[3]); + Mat4ToEul(invmat, eulo); + Mat4ToSize(invmat, sizo); + + /* disable channels not enabled */ + if (!(data->flag & CHILDOF_LOCX)) loc[0]= loco[0]= 0.0f; + if (!(data->flag & CHILDOF_LOCY)) loc[1]= loco[1]= 0.0f; + if (!(data->flag & CHILDOF_LOCZ)) loc[2]= loco[2]= 0.0f; + if (!(data->flag & CHILDOF_ROTX)) eul[0]= eulo[0]= 0.0f; + if (!(data->flag & CHILDOF_ROTY)) eul[1]= eulo[1]= 0.0f; + if (!(data->flag & CHILDOF_ROTZ)) eul[2]= eulo[2]= 0.0f; + if (!(data->flag & CHILDOF_SIZEX)) size[0]= sizo[0]= 1.0f; + if (!(data->flag & CHILDOF_SIZEY)) size[1]= sizo[1]= 1.0f; + if (!(data->flag & CHILDOF_SIZEZ)) size[2]= sizo[2]= 1.0f; + + /* make new target mat and offset mat */ + LocEulSizeToMat4(targetmat, loc, eul, size); + LocEulSizeToMat4(invmat, loco, eulo, sizo); + + /* multiply target (parent matrix) by offset (parent inverse) to get + * the effect of the parent that will be exherted on the owner + */ + Mat4MulMat4(parmat, invmat, targetmat); + + /* now multiply the parent matrix by the owner matrix to get the + * the effect of this constraint (i.e. owner is 'parented' to parent) + */ + Mat4CpyMat4(tempmat, ownermat); + Mat4MulMat4(ownermat, tempmat, parmat); + } } break; default: @@ -2234,3 +2472,65 @@ void evaluate_constraint (bConstraint *constraint, Object *ob, short ownertype, break; } } + +/* this function is called whenever constraints need to be evaluated */ +void solve_constraints (ListBase *conlist, bConstraintOb *cob, float ctime) +{ + bConstraint *con; + void *ownerdata; + float tarmat[4][4], oldmat[4][4]; + float solution[4][4], delta[4][4], imat[4][4]; + float enf; + + /* check that there is a valid constraint object to evaluate */ + if (cob == NULL) + return; + + /* loop over available constraints, solving and blending them */ + for (con= conlist->first; con; con= con->next) { + /* this we can skip completely */ + if (con->flag & CONSTRAINT_DISABLE) continue; + /* and inverse kinematics is solved seperate */ + if (con->type==CONSTRAINT_TYPE_KINEMATIC) continue; + /* rigidbody is really a game-engine thing - and is not solved here */ + if (con->type==CONSTRAINT_TYPE_RIGIDBODYJOINT) continue; + + /* influence of constraint */ + /* value should have been set from IPO's/Constraint Channels already */ + enf = con->enforce; + + /* move target/owner into right spaces */ + constraint_mat_convertspace(cob->ob, cob->pchan, cob->matrix, CONSTRAINT_SPACE_WORLD, con->ownspace); + + /* Get the target matrix */ + ownerdata= ((cob->pchan)? (void *)cob->pchan : (void *)cob->ob); + get_constraint_target_matrix(con, cob->type, ownerdata, tarmat, ctime); + + Mat4CpyMat4(oldmat, cob->matrix); + + /* solve the constraint */ + evaluate_constraint(con, cob->matrix, tarmat); + + /* Interpolate the enforcement, to blend result of constraint into final owner transform */ + /* 1. Remove effects of original matrix from constraint solution ==> delta */ + Mat4Invert(imat, oldmat); + Mat4CpyMat4(solution, cob->matrix); + Mat4MulMat4(delta, solution, imat); + + /* 2. If constraint influence is not full strength, then interpolate + * identity_matrix --> delta_matrix to get the effect the constraint actually exerts + */ + if (enf < 1.0) { + float identity[4][4]; + Mat4One(identity); + Mat4BlendMat4(delta, identity, delta, enf); + } + + /* 3. Now multiply the delta by the matrix in use before the evaluation */ + Mat4MulMat4(cob->matrix, delta, oldmat); + + /* move target/owner back into worldspace for next constraint/other business */ + if ((con->flag & CONSTRAINT_SPACEONCE) == 0) + constraint_mat_convertspace(cob->ob, cob->pchan, cob->matrix, con->ownspace, CONSTRAINT_SPACE_WORLD); + } +} diff --git a/source/blender/blenkernel/intern/object.c b/source/blender/blenkernel/intern/object.c index 713ee3b2f5d..922931ca0b8 100644 --- a/source/blender/blenkernel/intern/object.c +++ b/source/blender/blenkernel/intern/object.c @@ -772,6 +772,7 @@ Object *add_only_object(int type, char *name) ob->rot[0]= ob->rot[1]= ob->rot[2]= 0.0; ob->size[0]= ob->size[1]= ob->size[2]= 1.0; + Mat4One(ob->constinv); Mat4One(ob->parentinv); Mat4One(ob->obmat); ob->dt= OB_SHADED; @@ -1560,9 +1561,18 @@ void where_is_object_time(Object *ob, float ctime) } - /* constraints need ctime, not stime. it calls where_is_object_time and bsystem_time */ - solve_constraints (ob, TARGET_OBJECT, NULL, ctime); - + /* solve constraints */ + if (ob->constraints.first) { + bConstraintOb *cob; + + cob= constraints_make_evalob(ob, NULL, TARGET_OBJECT); + + /* constraints need ctime, not stime. Some call where_is_object_time and bsystem_time */ + solve_constraints (&ob->constraints, cob, ctime); + + constraints_clear_evalob(cob); + } + if(ob->scriptlink.totscript && !during_script()) { if (G.f & G_DOSCRIPTLINKS) BPY_do_pyscript((ID *)ob, SCRIPT_REDRAW); } @@ -1728,122 +1738,17 @@ for a lamp that is the child of another object */ if(ob->track) solve_tracking(ob, ob->track->obmat); - solve_constraints(ob, TARGET_OBJECT, NULL, G.scene->r.cfra); + /* solve constraints */ + if (ob->constraints.first) { + bConstraintOb *cob; + + cob= constraints_make_evalob(ob, NULL, TARGET_OBJECT); + solve_constraints (&ob->constraints, cob, G.scene->r.cfra); + constraints_clear_evalob(cob); + } /* WATCH IT!!! */ ob->ipo= ipo; - -} - -void solve_constraints (Object *ob, short obtype, void *obdata, float ctime) -{ - bConstraint *con; - float tmat[4][4], focusmat[4][4], lastmat[4][4]; - int i, clear=1, tot=0; - float a=0; - float aquat[4], quat[4]; - float aloc[3], loc[3]; - float asize[3], size[3]; - float oldmat[4][4]; - float smat[3][3], rmat[3][3], mat[3][3]; - float enf; - - for (con = ob->constraints.first; con; con=con->next) { - // inverse kinematics is solved seperate - if (con->type==CONSTRAINT_TYPE_KINEMATIC) continue; - // and this we can skip completely - if (con->flag & CONSTRAINT_DISABLE) continue; - // local constraints are handled in armature.c only - if (con->flag & CONSTRAINT_LOCAL) continue; - - /* Clear accumulators if necessary*/ - if (clear) { - clear= 0; - a= 0; - tot= 0; - memset(aquat, 0, sizeof(float)*4); - memset(aloc, 0, sizeof(float)*3); - memset(asize, 0, sizeof(float)*3); - } - - enf = con->enforce; // value from ipos (from action channels) - - /* Get the targetmat */ - get_constraint_target_matrix(con, obtype, obdata, tmat, size, ctime); - - Mat4CpyMat4(focusmat, tmat); - - /* Extract the components & accumulate */ - Mat4ToQuat(focusmat, quat); - VECCOPY(loc, focusmat[3]); - Mat3CpyMat4(mat, focusmat); - Mat3ToSize(mat, size); - - a+= enf; - tot++; - - for(i=0; i<3; i++) { - aquat[i+1]+=(quat[i+1]) * enf; - aloc[i]+=(loc[i]) * enf; - asize[i]+=(size[i]-1.0f) * enf; - } - aquat[0]+=(quat[0])*enf; - Mat4CpyMat4(lastmat, focusmat); - - /* removed for now, probably becomes option? (ton) */ - - /* If the next constraint is not the same type (or there isn't one), - * then evaluate the accumulator & request a clear */ - if (TRUE) { //(!con->next)||(con->next && con->next->type!=con->type)) { - clear= 1; - Mat4CpyMat4(oldmat, ob->obmat); - - /* If we have several inputs, do a blend of them */ - if (tot) { - if (tot>1) { - if (a) { - for (i=0; i<3; i++) { - asize[i]=1.0f + (asize[i]/(a)); - aloc[i]=(aloc[i]/a); - } - - NormalQuat(aquat); - - QuatToMat3(aquat, rmat); - SizeToMat3(asize, smat); - Mat3MulMat3(mat, rmat, smat); - Mat4CpyMat3(focusmat, mat); - VECCOPY(focusmat[3], aloc); - - evaluate_constraint(con, ob, obtype, obdata, focusmat); - } - - } - /* If we only have one, blend with the current obmat */ - else { - float solution[4][4]; - float delta[4][4]; - float imat[4][4]; - float identity[4][4]; - - /* solve the constraint then blend it to the previous one */ - evaluate_constraint(con, ob, obtype, obdata, lastmat); - - Mat4CpyMat4 (solution, ob->obmat); - - /* Interpolate the enforcement */ - Mat4Invert (imat, oldmat); - Mat4MulMat4 (delta, solution, imat); - - if (a<1.0) { - Mat4One(identity); - Mat4BlendMat4(delta, identity, delta, a); - } - Mat4MulMat4 (ob->obmat, delta, oldmat); - } - } - } - } } /* for calculation of the inverse parent transform, only used for editor */ @@ -1853,6 +1758,7 @@ void what_does_parent(Object *ob) clear_workob(); Mat4One(workob.obmat); Mat4One(workob.parentinv); + Mat4One(workob.constinv); workob.parent= ob->parent; workob.track= ob->track; |