This repository has been archived on 2023-10-09. You can view files and clone it, but cannot push or open issues or pull requests.
Files
blender-archive/source/blender/blenkernel/intern/deform.c

452 lines
9.9 KiB
C
Raw Normal View History

2002-10-12 11:37:38 +00:00
/* deform.c June 2001
*
* support for deformation groups
*
* Reevan McKay
*
* $Id$
*
* ***** BEGIN GPL LICENSE BLOCK *****
2002-10-12 11:37:38 +00:00
*
* 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.
2002-10-12 11:37:38 +00:00
*
* 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,
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
* All rights reserved.
*
* The Original Code is: all of this file.
*
* Contributor(s): none yet.
*
* ***** END GPL LICENSE BLOCK *****
2002-10-12 11:37:38 +00:00
*/
#include <string.h>
#include <math.h>
#include "ctype.h"
2002-10-12 11:37:38 +00:00
#include "MEM_guardedalloc.h"
#include "DNA_curve_types.h"
#include "DNA_effect_types.h"
#include "DNA_lattice_types.h"
#include "DNA_mesh_types.h"
#include "DNA_meshdata_types.h"
2002-10-12 11:37:38 +00:00
#include "DNA_object_types.h"
#include "DNA_object_force.h"
OK. Here's the long awaited first step (V0.01!) of SoftBody. It is called from within mesh_modifiers (kernel deform.c). It copies vertices to a temporal particle system (struct SoftBody with BodyPoint structs) to do physics tricks with it. For each frame change the delta movements (based on standard ipo anim or even other deforms (later) are applied to the physics system. How to apply and calculate satisfying results is not my thing... so here I'll commu- nicate with others for. Since it's in the modifier stack, the SoftBody code can run entirely on original data (no displists!). Right now I've implemented 2 things; - "Goal" which is a per vertex value for how much the current position should take into account (goal=1 is without physics). This is a powerful method for artists to get control over what moves and not. Right now i read the vertex color for it. - And some spring stuff, which now only works based on force moving it to the originial location. This doesnt work with 'goal'... erhm. - You can re-use physics vars from engine, used right now is (in Object) - damping - springf (spring factor) - softflag (to set types, or activate softbody for it - The SoftBody pointer in struct Object is only runtime, nothing saved in file To prevent all users going to complain it doesn't work, I've hidden the functionality. :) The buttons to set softbody 'on' only show now (psst psst) when the object has name "soft" as first 4 characters. You can find the buttons in the F7 Particle Interaction Panel (which should be renamed 'physics properties' later or so. Demo file: http://www.blender.org/bf/softbody.blend
2004-10-01 14:04:17 +00:00
#include "DNA_scene_types.h"
#include "BKE_curve.h"
2002-10-12 11:37:38 +00:00
#include "BKE_deform.h"
#include "BKE_displist.h"
#include "BKE_effect.h"
OK. Here's the long awaited first step (V0.01!) of SoftBody. It is called from within mesh_modifiers (kernel deform.c). It copies vertices to a temporal particle system (struct SoftBody with BodyPoint structs) to do physics tricks with it. For each frame change the delta movements (based on standard ipo anim or even other deforms (later) are applied to the physics system. How to apply and calculate satisfying results is not my thing... so here I'll commu- nicate with others for. Since it's in the modifier stack, the SoftBody code can run entirely on original data (no displists!). Right now I've implemented 2 things; - "Goal" which is a per vertex value for how much the current position should take into account (goal=1 is without physics). This is a powerful method for artists to get control over what moves and not. Right now i read the vertex color for it. - And some spring stuff, which now only works based on force moving it to the originial location. This doesnt work with 'goal'... erhm. - You can re-use physics vars from engine, used right now is (in Object) - damping - springf (spring factor) - softflag (to set types, or activate softbody for it - The SoftBody pointer in struct Object is only runtime, nothing saved in file To prevent all users going to complain it doesn't work, I've hidden the functionality. :) The buttons to set softbody 'on' only show now (psst psst) when the object has name "soft" as first 4 characters. You can find the buttons in the F7 Particle Interaction Panel (which should be renamed 'physics properties' later or so. Demo file: http://www.blender.org/bf/softbody.blend
2004-10-01 14:04:17 +00:00
#include "BKE_global.h"
Result of 2 weeks of quiet coding work in Greece :) Aim was to get a total refresh of the animation system. This is needed because; - we need to upgrade it with 21st century features - current code is spaghetti/hack combo, and hides good design - it should become lag-free with using dependency graphs A full log, with complete code API/structure/design explanation will follow, that's a load of work... so here below the list with hot changes; - The entire object update system (matrices, geometry) is now centralized. Calls to where_is_object and makeDispList are forbidden, instead we tag objects 'changed' and let the depgraph code sort it out - Removed all old "Ika" code - Depgraph is aware of all relationships, including meta balls, constraints, bevelcurve, and so on. - Made depgraph aware of relation types and layers, to do smart flushing of 'changed' events. Nothing gets calculated too often! - Transform uses depgraph to detect changes - On frame-advance, depgraph flushes animated changes Armatures; Almost all armature related code has been fully built from scratch. It now reveils the original design much better, with a very clean implementation, lag free without even calculating each Bone more than once. Result is quite a speedup yes! Important to note is; 1) Armature is data containing the 'rest position' 2) Pose is the changes of rest position, and always on object level. That way more Objects can use same Pose. Also constraints are in Pose 3) Actions only contain the Ipos to change values in Poses. - Bones draw unrotated now - Drawing bones speedup enormously (10-20 times) - Bone selecting in EditMode, selection state is saved for PoseMode, and vice-versa - Undo in editmode - Bone renaming does vertexgroups, constraints, posechannels, actions, for all users of Armature in entire file - Added Bone renaming in NKey panel - Nkey PoseMode shows eulers now - EditMode and PoseMode now have 'active' bone too (last clicked) - Parenting in EditMode' CTRL+P, ALT+P, with nice options! - Pose is added in Outliner now, with showing that constraints are in the Pose, not Armature - Disconnected IK solving from constraints. It's a separate phase now, on top of the full Pose calculations - Pose itself has a dependency graph too, so evaluation order is lag free. TODO NOW; - Rotating in Posemode has incorrect inverse transform (Martin will fix) - Python Bone/Armature/Pose API disabled... needs full recode too (wait for my doc!) - Game engine will need upgrade too - Depgraph code needs revision, cleanup, can be much faster! (But, compliments for Jean-Luc, it works like a charm!) - IK changed, it now doesnt use previous position to advance to next position anymore. That system looks nice (no flips) but is not well suited for NLA and background render. TODO LATER; We now can do loadsa new nifty features as well; like: - Kill PoseMode (can be option for armatures itself) - Make B-Bones (Bezier, Bspline, like for spines) - Move all silly button level edit to 3d window (like CTRL+I = add IK) - Much better & informative drawing - Fix action/nla editors - Put all ipos in Actions (object, mesh key, lamp color) - Add hooks - Null bones - Much more advanced constraints... Bugfixes; - OGL render (view3d header) had wrong first frame on anim render - Ipo 'recording' mode had wrong playback speed - Vertex-key mode now sticks to show 'active key', until frame change -Ton-
2005-07-03 17:35:38 +00:00
#include "BKE_key.h"
#include "BKE_lattice.h"
OK. Here's the long awaited first step (V0.01!) of SoftBody. It is called from within mesh_modifiers (kernel deform.c). It copies vertices to a temporal particle system (struct SoftBody with BodyPoint structs) to do physics tricks with it. For each frame change the delta movements (based on standard ipo anim or even other deforms (later) are applied to the physics system. How to apply and calculate satisfying results is not my thing... so here I'll commu- nicate with others for. Since it's in the modifier stack, the SoftBody code can run entirely on original data (no displists!). Right now I've implemented 2 things; - "Goal" which is a per vertex value for how much the current position should take into account (goal=1 is without physics). This is a powerful method for artists to get control over what moves and not. Right now i read the vertex color for it. - And some spring stuff, which now only works based on force moving it to the originial location. This doesnt work with 'goal'... erhm. - You can re-use physics vars from engine, used right now is (in Object) - damping - springf (spring factor) - softflag (to set types, or activate softbody for it - The SoftBody pointer in struct Object is only runtime, nothing saved in file To prevent all users going to complain it doesn't work, I've hidden the functionality. :) The buttons to set softbody 'on' only show now (psst psst) when the object has name "soft" as first 4 characters. You can find the buttons in the F7 Particle Interaction Panel (which should be renamed 'physics properties' later or so. Demo file: http://www.blender.org/bf/softbody.blend
2004-10-01 14:04:17 +00:00
#include "BKE_object.h"
#include "BKE_softbody.h"
#include "BKE_utildefines.h"
#include "BKE_mesh.h"
#include "BLI_blenlib.h"
#include "BLI_math.h"
2002-10-12 11:37:38 +00:00
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
2002-10-12 11:37:38 +00:00
void copy_defgroups (ListBase *outbase, ListBase *inbase)
2002-10-12 11:37:38 +00:00
{
bDeformGroup *defgroup, *defgroupn;
outbase->first= outbase->last= 0;
for (defgroup = inbase->first; defgroup; defgroup=defgroup->next){
defgroupn= copy_defgroup(defgroup);
BLI_addtail(outbase, defgroupn);
}
}
bDeformGroup *copy_defgroup (bDeformGroup *ingroup)
2002-10-12 11:37:38 +00:00
{
bDeformGroup *outgroup;
if (!ingroup)
return NULL;
outgroup=MEM_callocN(sizeof(bDeformGroup), "copy deformGroup");
2002-10-12 11:37:38 +00:00
/* For now, just copy everything over. */
memcpy (outgroup, ingroup, sizeof(bDeformGroup));
outgroup->next=outgroup->prev=NULL;
return outgroup;
}
void copy_defvert (MDeformVert *dvert_r, const MDeformVert *dvert)
{
if(dvert_r->totweight == dvert->totweight) {
if(dvert->totweight)
memcpy(dvert_r->dw, dvert->dw, dvert->totweight * sizeof(MDeformWeight));
}
else {
if(dvert_r->dw)
MEM_freeN(dvert_r->dw);
if(dvert->totweight)
dvert_r->dw= MEM_dupallocN(dvert->dw);
else
dvert_r->dw= NULL;
dvert_r->totweight = dvert->totweight;
}
}
void normalize_defvert (MDeformVert *dvert)
{
if(dvert->totweight<=0) {
/* nothing */
}
else if (dvert->totweight==1) {
dvert->dw[0].weight= 1.0f;
}
else {
int i;
float tot= 0.0f;
MDeformWeight *dw;
for(i=0, dw=dvert->dw; i < dvert->totweight; i++, dw++)
tot += dw->weight;
if(tot > 0.0f) {
for(i=0, dw=dvert->dw; i < dvert->totweight; i++, dw++)
dw->weight /= tot;
}
}
}
void flip_defvert (MDeformVert *dvert, int *flip_map)
{
MDeformWeight *dw;
int i;
for(dw= dvert->dw, i=0; i<dvert->totweight; dw++, i++)
if(flip_map[dw->def_nr] >= 0)
dw->def_nr= flip_map[dw->def_nr];
}
bDeformGroup *get_named_vertexgroup (Object *ob, char *name)
{
/* return a pointer to the deform group with this name
* or return NULL otherwise.
*/
bDeformGroup *curdef;
for (curdef = ob->defbase.first; curdef; curdef=curdef->next) {
if (!strcmp(curdef->name, name)) {
return curdef;
}
}
return NULL;
}
int get_named_vertexgroup_num (Object *ob, const char *name)
{
/* Return the location of the named deform group within the list of
* deform groups. This function is a combination of get_defgroup_num and
* get_named_vertexgroup. The other two could be called instead, but that
* require looping over the vertexgroups twice.
*/
bDeformGroup *curdef;
int def_nr;
if(name[0] != '\0') {
for (curdef=ob->defbase.first, def_nr=0; curdef; curdef=curdef->next, def_nr++) {
if (!strcmp(curdef->name, name))
return def_nr;
}
}
return -1;
}
int get_defgroup_num (Object *ob, bDeformGroup *dg)
{
/* Fetch the location of this deform group
* within the linked list of deform groups.
* (this number is stored in the deform
* weights of the deform verts to link them
* to this deform group).
*
* note: this is zero based, ob->actdef starts at 1.
*/
bDeformGroup *eg;
int def_nr;
eg = ob->defbase.first;
def_nr = 0;
/* loop through all deform groups */
while (eg != NULL) {
/* if the current deform group is
* the one we are after, return
* def_nr
*/
if (eg == dg) {
break;
}
++def_nr;
eg = eg->next;
}
/* if there was no deform group found then
* return -1 (should set up a nice symbolic
* constant for this)
*/
if (eg == NULL) return -1;
return def_nr;
}
/* note, must be freed */
int *get_defgroup_flip_map(Object *ob)
{
bDeformGroup *dg;
int totdg= BLI_countlist(&ob->defbase);
if(totdg==0) {
return NULL;
}
else {
char name[sizeof(dg->name)];
int i, flip_num, *map= MEM_mallocN(totdg * sizeof(int), "get_defgroup_flip_map");
memset(map, -1, totdg * sizeof(int));
for (dg=ob->defbase.first, i=0; dg; dg=dg->next, i++) {
if(map[i] == -1) { /* may be calculated previously */
flip_vertexgroup_name(name, dg->name, 0);
if(strcmp(name, dg->name)) {
flip_num= get_named_vertexgroup_num(ob, name);
if(flip_num > -1) {
map[i]= flip_num;
map[flip_num]= i; /* save an extra lookup */
}
}
}
}
return map;
}
}
void unique_vertexgroup_name (bDeformGroup *dg, Object *ob)
{
bDeformGroup *curdef;
int number;
int exists = 0;
char tempname[64];
char *dot;
if (!ob)
return;
/* See if we are given an empty string */
if (dg->name[0] == '\0') {
/* give it default name first */
strcpy (dg->name, "Group");
}
/* See if we even need to do this */
for (curdef = ob->defbase.first; curdef; curdef=curdef->next) {
if (dg!=curdef) {
if (!strcmp(curdef->name, dg->name)) {
exists = 1;
break;
}
}
}
if (!exists)
return;
/* Strip off the suffix */
dot=strchr(dg->name, '.');
if (dot)
*dot=0;
for (number = 1; number <=999; number++) {
sprintf (tempname, "%s.%03d", dg->name, number);
exists = 0;
for (curdef=ob->defbase.first; curdef; curdef=curdef->next) {
if (dg!=curdef) {
if (!strcmp (curdef->name, tempname)) {
exists = 1;
break;
}
}
}
if (!exists) {
BLI_strncpy (dg->name, tempname, 32);
return;
}
}
}
/* finds the best possible flipped name. For renaming; check for unique names afterwards */
/* if strip_number: removes number extensions */
void flip_vertexgroup_name (char *name, const char *from_name, int strip_number)
{
int len;
char prefix[sizeof((bDeformGroup *)NULL)->name]={""}; /* The part before the facing */
char suffix[sizeof((bDeformGroup *)NULL)->name]={""}; /* The part after the facing */
char replace[sizeof((bDeformGroup *)NULL)->name]={""}; /* The replacement string */
char number[sizeof((bDeformGroup *)NULL)->name]={""}; /* The number extension string */
char *index=NULL;
len= strlen(from_name);
if(len<3) return; // we don't do names like .R or .L
strcpy(name, from_name);
/* We first check the case with a .### extension, let's find the last period */
if(isdigit(name[len-1])) {
index= strrchr(name, '.'); // last occurrence
if (index && isdigit(index[1]) ) { // doesnt handle case bone.1abc2 correct..., whatever!
if(strip_number==0)
strcpy(number, index);
*index= 0;
len= strlen(name);
}
}
strcpy (prefix, name);
#define IS_SEPARATOR(a) ((a)=='.' || (a)==' ' || (a)=='-' || (a)=='_')
/* first case; separator . - _ with extensions r R l L */
if( IS_SEPARATOR(name[len-2]) ) {
switch(name[len-1]) {
case 'l':
prefix[len-1]= 0;
strcpy(replace, "r");
break;
case 'r':
prefix[len-1]= 0;
strcpy(replace, "l");
break;
case 'L':
prefix[len-1]= 0;
strcpy(replace, "R");
break;
case 'R':
prefix[len-1]= 0;
strcpy(replace, "L");
break;
}
}
/* case; beginning with r R l L , with separator after it */
else if( IS_SEPARATOR(name[1]) ) {
switch(name[0]) {
case 'l':
strcpy(replace, "r");
strcpy(suffix, name+1);
prefix[0]= 0;
break;
case 'r':
strcpy(replace, "l");
strcpy(suffix, name+1);
prefix[0]= 0;
break;
case 'L':
strcpy(replace, "R");
strcpy(suffix, name+1);
prefix[0]= 0;
break;
case 'R':
strcpy(replace, "L");
strcpy(suffix, name+1);
prefix[0]= 0;
break;
}
}
else if(len > 5) {
/* hrms, why test for a separator? lets do the rule 'ultimate left or right' */
index = BLI_strcasestr(prefix, "right");
if (index==prefix || index==prefix+len-5) {
if(index[0]=='r')
strcpy (replace, "left");
else {
if(index[1]=='I')
strcpy (replace, "LEFT");
else
strcpy (replace, "Left");
}
*index= 0;
strcpy (suffix, index+5);
}
else {
index = BLI_strcasestr(prefix, "left");
if (index==prefix || index==prefix+len-4) {
if(index[0]=='l')
strcpy (replace, "right");
else {
if(index[1]=='E')
strcpy (replace, "RIGHT");
else
strcpy (replace, "Right");
}
*index= 0;
strcpy (suffix, index+4);
}
}
}
#undef IS_SEPARATOR
sprintf (name, "%s%s%s%s", prefix, replace, suffix, number);
}
float deformvert_get_weight(const struct MDeformVert *dvert, int group_num)
{
if(dvert)
{
const MDeformWeight *dw = dvert->dw;
int i;
for(i=dvert->totweight; i>0; i--, dw++)
if(dw->def_nr == group_num)
return dw->weight;
}
/* Not found */
return 0.0;
}
float vertexgroup_get_vertex_weight(const struct MDeformVert *dvert, int index, int group_num)
{
if(group_num == -1 || dvert == NULL)
return 1.0;
return deformvert_get_weight(dvert+index, group_num);
}