diff options
author | Campbell Barton <ideasman42@gmail.com> | 2008-08-12 16:57:18 +0400 |
---|---|---|
committer | Campbell Barton <ideasman42@gmail.com> | 2008-08-12 16:57:18 +0400 |
commit | 17c81efcedbdd6b657acd54b076dec21ec2cdc64 (patch) | |
tree | 4e04861989b9c91e0ae5b0f627420f83e2f0a2cb /source | |
parent | 4043226064f856dc50976d59383b91c20e5c5790 (diff) |
svn merge -r16009:HEAD https://svn.blender.org/svnroot/bf-blender/trunk/blender
Diffstat (limited to 'source')
-rw-r--r-- | source/blender/blenkernel/intern/anim.c | 12 | ||||
-rw-r--r-- | source/blender/blenkernel/intern/bvhutils.c | 29 | ||||
-rw-r--r-- | source/blender/blenkernel/intern/particle_system.c | 18 | ||||
-rw-r--r-- | source/blender/blenkernel/intern/softbody.c | 837 | ||||
-rw-r--r-- | source/blender/blenlib/intern/BLI_kdopbvh.c | 933 | ||||
-rw-r--r-- | source/blender/include/BIF_editarmature.h | 6 | ||||
-rw-r--r-- | source/blender/python/api2_2x/Texture.c | 40 | ||||
-rw-r--r-- | source/blender/python/api2_2x/doc/Render.py | 11 | ||||
-rw-r--r-- | source/blender/python/api2_2x/doc/Texture.py | 6 | ||||
-rw-r--r-- | source/blender/render/intern/source/rayshade.c | 7 | ||||
-rw-r--r-- | source/blender/src/editarmature.c | 151 | ||||
-rw-r--r-- | source/blender/src/editobject.c | 8 | ||||
-rw-r--r-- | source/blender/src/poselib.c | 7 | ||||
-rw-r--r-- | source/blender/src/transform_conversions.c | 4 | ||||
-rw-r--r-- | source/gameengine/Converter/BL_BlenderDataConversion.cpp | 12 | ||||
-rw-r--r-- | source/gameengine/Expressions/Value.cpp | 28 |
16 files changed, 1284 insertions, 825 deletions
diff --git a/source/blender/blenkernel/intern/anim.c b/source/blender/blenkernel/intern/anim.c index 4b8728257b2..e2ce4b9f6f3 100644 --- a/source/blender/blenkernel/intern/anim.c +++ b/source/blender/blenkernel/intern/anim.c @@ -742,9 +742,8 @@ static void new_particle_duplilist(ListBase *lb, ID *id, Object *par, float par_ ParticleCacheKey *cache; ParticleSystemModifierData *psmd; float ctime, pa_time, scale = 1.0f; - float tmat[4][4], mat[4][4], obrotmat[4][4], pamat[4][4], size=0.0; + float tmat[4][4], mat[4][4], pamat[4][4], size=0.0; float (*obmat)[4], (*oldobmat)[4]; - float xvec[3] = {-1.0, 0.0, 0.0}, q[4]; int lay, a, b, k, step_nbr = 0, counter, hair = 0; int totpart, totchild, totgroup=0, pa_num; @@ -906,14 +905,7 @@ static void new_particle_duplilist(ListBase *lb, ID *id, Object *par, float par_ /* to give ipos in object correct offset */ where_is_object_time(ob, ctime-pa_time); - if(!hair) { - vectoquat(xvec, ob->trackflag, ob->upflag, q); - QuatToMat4(q, obrotmat); - obrotmat[3][3]= 1.0f; - Mat4MulMat4(mat, obrotmat, pamat); - } - else - Mat4CpyMat4(mat, pamat); + Mat4CpyMat4(mat, pamat); Mat4MulMat4(tmat, obmat, mat); Mat4MulFloat3((float *)tmat, size*scale); diff --git a/source/blender/blenkernel/intern/bvhutils.c b/source/blender/blenkernel/intern/bvhutils.c index 5b68a637ea2..10e92b8b705 100644 --- a/source/blender/blenkernel/intern/bvhutils.c +++ b/source/blender/blenkernel/intern/bvhutils.c @@ -55,7 +55,7 @@ static float ray_tri_intersection(const BVHTreeRay *ray, const float m_dist, con { float dist; - if(RayIntersectsTriangle(ray->origin, ray->direction, v0, v1, v2, &dist, NULL)) + if(RayIntersectsTriangle((float*)ray->origin, (float*)ray->direction, (float*)v0, (float*)v1, (float*)v2, &dist, NULL)) return dist; return FLT_MAX; @@ -71,7 +71,7 @@ static float sphereray_tri_intersection(const BVHTreeRay *ray, float radius, con CalcNormFloat((float*)v0, (float*)v1, (float*)v2, plane_normal); VECADDFAC( p1, ray->origin, ray->direction, m_dist); - if(SweepingSphereIntersectsTriangleUV(ray->origin, p1, radius, v0, v1, v2, &idist, &hit_point)) + if(SweepingSphereIntersectsTriangleUV((float*)ray->origin, p1, radius, (float*)v0, (float*)v1, (float*)v2, &idist, hit_point)) { return idist * m_dist; } @@ -268,17 +268,24 @@ static void mesh_faces_nearest_point(void *userdata, int index, const float *co, do { float nearest_tmp[3], dist; - - dist = nearest_point_in_tri_surface(co,t0, t1, t2, nearest_tmp); - if(dist < nearest->dist) + float vec[3][3]; + + // only insert valid triangles / quads with area > 0 + VECSUB(vec[0], t2, t1); + VECSUB(vec[1], t0, t1); + Crossf(vec[2], vec[0], vec[1]); + if(INPR(vec[2], vec[2]) >= FLT_EPSILON) { - nearest->index = index; - nearest->dist = dist; - VECCOPY(nearest->co, nearest_tmp); - CalcNormFloat((float*)t0, (float*)t1, (float*)t2, nearest->no); //TODO.. (interpolate normals from the vertexs coordinates? + dist = nearest_point_in_tri_surface(co,t0, t1, t2, nearest_tmp); + if(dist < nearest->dist) + { + nearest->index = index; + nearest->dist = dist; + VECCOPY(nearest->co, nearest_tmp); + CalcNormFloat((float*)t0, (float*)t1, (float*)t2, nearest->no); //TODO.. (interpolate normals from the vertexs coordinates? + } } - t1 = t2; t2 = t3; t3 = NULL; @@ -396,7 +403,7 @@ void bvhtree_from_mesh_faces(BVHTreeFromMesh *data, DerivedMesh *mesh, float eps VECCOPY(co[2], vert[ face[i].v3 ].co); if(face[i].v4) VECCOPY(co[3], vert[ face[i].v4 ].co); - + BLI_bvhtree_insert(tree, i, co[0], face[i].v4 ? 4 : 3); } BLI_bvhtree_balance(tree); diff --git a/source/blender/blenkernel/intern/particle_system.c b/source/blender/blenkernel/intern/particle_system.c index 7dca87d5c13..d1c0cdec71d 100644 --- a/source/blender/blenkernel/intern/particle_system.c +++ b/source/blender/blenkernel/intern/particle_system.c @@ -4653,7 +4653,7 @@ static void system_step(Object *ob, ParticleSystem *psys, ParticleSystemModifier PTCacheID pid; int totpart, oldtotpart, totchild, oldtotchild, p; float disp, *vg_vel= 0, *vg_tan= 0, *vg_rot= 0, *vg_size= 0; - int init= 0, distr= 0, alloc= 0, usecache= 0; + int init= 0, distr= 0, alloc= 0, usecache= 0, only_children_changed= 0; int framenr, framedelta, startframe, endframe; part= psys->part; @@ -4720,6 +4720,7 @@ static void system_step(Object *ob, ParticleSystem *psys, ParticleSystemModifier totchild = get_psys_tot_child(psys); if(oldtotpart != totpart || (psys->part->childtype && oldtotchild != totchild)) { + only_children_changed = (oldtotpart == totpart); realloc_particles(ob, psys, totpart); alloc = 1; distr= 1; @@ -4740,14 +4741,17 @@ static void system_step(Object *ob, ParticleSystem *psys, ParticleSystemModifier if((psys->part->type == PART_HAIR) && !(psys->flag & PSYS_HAIR_DONE)) /* don't generate children while growing hair - waste of time */ - psys_free_children(psys); - else if(get_psys_tot_child(psys)) - distribute_particles(ob, psys, PART_FROM_CHILD); + psys_free_children(psys); + else if(get_psys_tot_child(psys)) + distribute_particles(ob, psys, PART_FROM_CHILD); } - initialize_all_particles(ob, psys, psmd); - if(alloc) - reset_all_particles(ob, psys, psmd, 0.0, cfra, oldtotpart); + if(only_children_changed==0) { + initialize_all_particles(ob, psys, psmd); + + if(alloc) + reset_all_particles(ob, psys, psmd, 0.0, cfra, oldtotpart); + } /* flag for possible explode modifiers after this system */ psmd->flag |= eParticleSystemFlag_Pars; diff --git a/source/blender/blenkernel/intern/softbody.c b/source/blender/blenkernel/intern/softbody.c index d5b5ab6d63e..d465c058d30 100644 --- a/source/blender/blenkernel/intern/softbody.c +++ b/source/blender/blenkernel/intern/softbody.c @@ -69,6 +69,8 @@ variables on the UI for now #include "BLI_blenlib.h" #include "BLI_arithb.h" #include "BLI_ghash.h" +#include "BLI_threads.h" + #include "BKE_curve.h" #include "BKE_effect.h" #include "BKE_global.h" @@ -118,6 +120,20 @@ typedef struct SBScratch { float aabbmin[3],aabbmax[3]; }SBScratch; +typedef struct SB_thread_context{ + Object *ob; + float forcetime; + float timenow; + int ifirst; + int ilast; + ListBase *do_effector; + int do_deflector; + float fieldfactor; + float windfactor; + int nr; + int tot; +}SB_thread_context; + #define NLF_BUILD 1 #define NLF_SOLVE 2 @@ -1514,17 +1530,15 @@ int sb_detect_edge_collisionCached(float edge_v1[3],float edge_v2[3],float *damp -void scan_for_ext_spring_forces(Object *ob,float timenow) +void _scan_for_ext_spring_forces(Object *ob,float timenow,int ifirst,int ilast, struct ListBase *do_effector) { SoftBody *sb = ob->soft; - ListBase *do_effector; int a; float damp; float feedback[3]; - do_effector= pdInitEffectors(ob,NULL); if (sb && sb->totspring){ - for(a=0; a<sb->totspring; a++) { + for(a=ifirst; a<ilast; a++) { BodySpring *bs = &sb->bspring[a]; bs->ext_force[0]=bs->ext_force[1]=bs->ext_force[2]=0.0f; feedback[0]=feedback[1]=feedback[2]=0.0f; @@ -1584,9 +1598,88 @@ void scan_for_ext_spring_forces(Object *ob,float timenow) } } } - if(do_effector) - pdEndEffectors(do_effector); } + + +void scan_for_ext_spring_forces(Object *ob,float timenow) +{ + SoftBody *sb = ob->soft; + ListBase *do_effector= NULL; + do_effector= pdInitEffectors(ob,NULL); + if (sb){ + _scan_for_ext_spring_forces(ob,timenow,0,sb->totspring,do_effector); + } + if(do_effector) + pdEndEffectors(do_effector); +} + +void *exec_scan_for_ext_spring_forces(void *data) +{ + SB_thread_context *pctx = (SB_thread_context*)data; + _scan_for_ext_spring_forces(pctx->ob,pctx->timenow,pctx->ifirst,pctx->ilast,pctx->do_effector); + return 0; +} + +void sb_sfesf_threads_run(struct Object *ob, float timenow,int totsprings,int *ptr_to_break_func()) +{ + ListBase *do_effector = NULL; + ListBase threads; + SB_thread_context *sb_threads; + int i, totthread,left,dec; + int lowsprings =10; /* wild guess .. may increase with better thread management 'above' or even be UI option sb->spawn_cf_threads_nopts */ + + do_effector= pdInitEffectors(ob,NULL); + + /* figure the number of threads while preventing pretty pointless threading overhead */ + if(totsprings < lowsprings) {totthread=1;} + else{ + if(G.scene->r.mode & R_FIXED_THREADS) + totthread= G.scene->r.threads; + else + totthread= BLI_system_thread_count(); + } + /*left to do--> what if we got zillions of CPUs running but 'totsprings' tasks to spread*/ + + sb_threads= MEM_callocN(sizeof(SB_thread_context)*totthread, "SBSpringsThread"); + memset(sb_threads, 0, sizeof(SB_thread_context)*totthread); + left = totsprings; + dec = totsprings/totthread +1; + for(i=0; i<totthread; i++) { + sb_threads[i].ob = ob; + sb_threads[i].forcetime = 0.0; // not used here + sb_threads[i].timenow = timenow; + sb_threads[i].ilast = left; + left = left - dec; + if (left >0){ + sb_threads[i].ifirst = left; + } + else + sb_threads[i].ifirst = 0; + sb_threads[i].do_effector = do_effector; + sb_threads[i].do_deflector = 0;// not used here + sb_threads[i].fieldfactor = 0.0f;// not used here + sb_threads[i].windfactor = 0.0f;// not used here + sb_threads[i].nr= i; + sb_threads[i].tot= totthread; + } + if(totthread > 1) { + BLI_init_threads(&threads, exec_scan_for_ext_spring_forces, totthread); + + for(i=0; i<totthread; i++) + BLI_insert_thread(&threads, &sb_threads[i]); + + BLI_end_threads(&threads); + } + else + exec_scan_for_ext_spring_forces(&sb_threads[0]); + /* clean up */ + MEM_freeN(sb_threads); + + if(do_effector) + pdEndEffectors(do_effector); +} + + /* --- the spring external section*/ int choose_winner(float*w, float* pos,float*a,float*b,float*c,float*ca,float*cb,float*cc) @@ -2023,109 +2116,72 @@ static void sb_spring_force(Object *ob,int bpi,BodySpring *bs,float iks,float fo } -static void softbody_calc_forces(Object *ob, float forcetime, float timenow, int nl_flags) +/* since this is definitely the most CPU consuming task here .. try to spread it */ +/* core function _softbody_calc_forces_slice_in_a_thread */ +/* result is int to be able to flag user break */ +int _softbody_calc_forces_slice_in_a_thread(Object *ob, float forcetime, float timenow,int ifirst,int ilast,int *ptr_to_break_func(),ListBase *do_effector,int do_deflector,float fieldfactor, float windfactor) { -/* rule we never alter free variables :bp->vec bp->pos in here ! - * this will ruin adaptive stepsize AKA heun! (BM) - */ + float iks; + int bb,do_selfcollision,do_springcollision,do_aero; + int number_of_points_here = ilast - ifirst; SoftBody *sb= ob->soft; /* is supposed to be there */ BodyPoint *bp; - BodyPoint *bproot; - BodySpring *bs; - ListBase *do_effector; - float iks, ks, kd, gravity; - float fieldfactor = 1000.0f, windfactor = 250.0f; - float tune = sb->ballstiff; - int a, b, do_deflector,do_selfcollision,do_springcollision,do_aero; - - -/* jacobian - NLboolean success; - - if(nl_flags){ - nlBegin(NL_SYSTEM); - nlBegin(NL_MATRIX); - } -*/ - - - gravity = sb->grav * sb_grav_force_scale(ob); - + /* intitialize */ + if (sb) { /* check conditions for various options */ - do_deflector= query_external_colliders(ob); + /* +++ could be done on object level to squeeze out the last bits of it */ do_selfcollision=((ob->softflag & OB_SB_EDGES) && (sb->bspring)&& (ob->softflag & OB_SB_SELF)); do_springcollision=do_deflector && (ob->softflag & OB_SB_EDGES) &&(ob->softflag & OB_SB_EDGECOLL); do_aero=((sb->aeroedge)&& (ob->softflag & OB_SB_EDGES)); - - iks = 1.0f/(1.0f-sb->inspring)-1.0f ;/* inner spring constants function */ - bproot= sb->bpoint; /* need this for proper spring addressing */ - - if (do_springcollision || do_aero) scan_for_ext_spring_forces(ob,timenow); - /* after spring scan because it uses Effoctors too */ - do_effector= pdInitEffectors(ob,NULL); + /* --- could be done on object level to squeeze out the last bits of it */ + } + else { + printf("Error expected a SB here \n"); + return (999); + } - if (do_deflector) { - float defforce[3]; - do_deflector = sb_detect_aabb_collisionCached(defforce,ob->lay,ob,timenow); +/* debugerin */ + if (sb->totpoint < ifirst) { + printf("Aye 998"); + return (998); } +/* debugerin */ - for(a=sb->totpoint, bp= sb->bpoint; a>0; a--, bp++) { + + bp = &sb->bpoint[ifirst]; + for(bb=number_of_points_here; bb>0; bb--, bp++) { /* clear forces accumulator */ bp->force[0]= bp->force[1]= bp->force[2]= 0.0; - if(nl_flags & NLF_BUILD){ - //int ia =3*(sb->totpoint-a); - //int op =3*sb->totpoint; - /* dF/dV = v */ - /* jacobioan - nlMatrixAdd(op+ia,ia,-forcetime); - nlMatrixAdd(op+ia+1,ia+1,-forcetime); - nlMatrixAdd(op+ia+2,ia+2,-forcetime); - - nlMatrixAdd(ia,ia,1); - nlMatrixAdd(ia+1,ia+1,1); - nlMatrixAdd(ia+2,ia+2,1); - - nlMatrixAdd(op+ia,op+ia,1); - nlMatrixAdd(op+ia+1,op+ia+1,1); - nlMatrixAdd(op+ia+2,op+ia+2,1); - */ - - - } - /* naive ball self collision */ /* needs to be done if goal snaps or not */ if(do_selfcollision){ int attached; BodyPoint *obp; + BodySpring *bs; int c,b; float velcenter[3],dvel[3],def[3]; float distance; float compare; + float bstune = sb->ballstiff; - for(c=sb->totpoint, obp= sb->bpoint; c>=a; c--, obp++) { - - //if ((bp->octantflag & obp->octantflag) == 0) continue; - + for(c=sb->totpoint, obp= sb->bpoint; c>=ifirst+bb; c--, obp++) { compare = (obp->colball + bp->colball); VecSubf(def, bp->pos, obp->pos); - /* rather check the AABBoxes before ever calulating the real distance */ /* mathematically it is completly nuts, but performace is pretty much (3) times faster */ if ((ABS(def[0]) > compare) || (ABS(def[1]) > compare) || (ABS(def[2]) > compare)) continue; - distance = Normalize(def); if (distance < compare ){ /* exclude body points attached with a spring */ attached = 0; for(b=obp->nofsprings;b>0;b--){ bs = sb->bspring + obp->springs[b-1]; - if (( sb->totpoint-a == bs->v2) || ( sb->totpoint-a == bs->v1)){ + if (( ilast-bb == bs->v2) || ( ilast-bb == bs->v1)){ attached=1; continue;} } if (!attached){ - float f = tune/(distance) + tune/(compare*compare)*distance - 2.0f*tune/compare ; + float f = bstune/(distance) + bstune/(compare*compare)*distance - 2.0f*bstune/compare ; VecMidf(velcenter, bp->vec, obp->vec); VecSubf(dvel,velcenter,bp->vec); @@ -2134,38 +2190,12 @@ static void softbody_calc_forces(Object *ob, float forcetime, float timenow, int Vec3PlusStVec(bp->force,f*(1.0f-sb->balldamp),def); Vec3PlusStVec(bp->force,sb->balldamp,dvel); - if(nl_flags & NLF_BUILD){ - //int ia =3*(sb->totpoint-a); - //int ic =3*(sb->totpoint-c); - //int op =3*sb->totpoint; - //float mvel = forcetime*sb->nodemass*sb->balldamp; - //float mpos = forcetime*tune*(1.0f-sb->balldamp); - /*some quick and dirty entries to the jacobian*/ - //dfdx_goal(ia,ia,op,mpos); - //dfdv_goal(ia,ia,mvel); - /* exploit force(a,b) == -force(b,a) part1/2 */ - //dfdx_goal(ic,ic,op,mpos); - //dfdv_goal(ic,ic,mvel); - - - /*TODO sit down an X-out the true jacobian entries*/ - /*well does not make to much sense because the eigenvalues - of the jacobian go negative; and negative eigenvalues - on a complex iterative system z(n+1)=A * z(n) - give imaginary roots in the charcateristic polynom - --> solutions that to z(t)=u(t)* exp ( i omega t) --> oscilations we don't want here - where u(t) is a unknown amplitude function (worst case rising fast) - */ - } - /* exploit force(a,b) == -force(b,a) part2/2 */ VecSubf(dvel,velcenter,obp->vec); VecMulf(dvel,sb->nodemass); Vec3PlusStVec(obp->force,sb->balldamp,dvel); Vec3PlusStVec(obp->force,-f*(1.0f-sb->balldamp),def); - - } } } @@ -2179,20 +2209,13 @@ static void softbody_calc_forces(Object *ob, float forcetime, float timenow, int /* do goal stuff */ if(ob->softflag & OB_SB_GOAL) { /* true elastic goal */ + float ks,kd; VecSubf(auxvect,bp->pos,bp->origT); ks = 1.0f/(1.0f- bp->goal*sb->goalspring)-1.0f ; bp->force[0]+= -ks*(auxvect[0]); bp->force[1]+= -ks*(auxvect[1]); bp->force[2]+= -ks*(auxvect[2]); - if(nl_flags & NLF_BUILD){ - //int ia =3*(sb->totpoint-a); - //int op =3*(sb->totpoint); - /* depending on my pos */ - //dfdx_goal(ia,ia,op,ks*forcetime); - } - - /* calulate damping forces generated by goals*/ VecSubf(velgoal,bp->origS, bp->origE); kd = sb->goalfrict * sb_fric_force_scale(ob) ; @@ -2202,13 +2225,6 @@ static void softbody_calc_forces(Object *ob, float forcetime, float timenow, int bp->force[0]-= kd * (auxvect[0]); bp->force[1]-= kd * (auxvect[1]); bp->force[2]-= kd * (auxvect[2]); - if(nl_flags & NLF_BUILD){ - //int ia =3*(sb->totpoint-a); - Normalize(auxvect); - /* depending on my vel */ - //dfdv_goal(ia,ia,kd*forcetime); - } - } else { bp->force[0]-= kd * (velgoal[0] - bp->vec[0]); @@ -2218,14 +2234,15 @@ static void softbody_calc_forces(Object *ob, float forcetime, float timenow, int } /* done goal stuff */ - /* gravitation */ + if (sb){ + float gravity = sb->grav * sb_grav_force_scale(ob); bp->force[2]-= gravity*sb->nodemass; /* individual mass of node here */ - //bp->force[1]-= gravity*sb->nodemass; /* individual mass of node here */ - + } /* particle field & vortex */ if(do_effector) { + float kd; float force[3]= {0.0f, 0.0f, 0.0f}; float speed[3]= {0.0f, 0.0f, 0.0f}; float eval_sb_fric_force_scale = sb_fric_force_scale(ob); /* just for calling function once */ @@ -2246,21 +2263,12 @@ static void softbody_calc_forces(Object *ob, float forcetime, float timenow, int } else { /* BP friction in media (not) moving*/ - kd= sb->mediafrict* sb_fric_force_scale(ob); + float kd = sb->mediafrict* sb_fric_force_scale(ob); /* assume it to be proportional to actual velocity */ bp->force[0]-= bp->vec[0]*kd; bp->force[1]-= bp->vec[1]*kd; bp->force[2]-= bp->vec[2]*kd; /* friction in media done */ - if(nl_flags & NLF_BUILD){ - //int ia =3*(sb->totpoint-a); - /* da/dv = */ - -// nlMatrixAdd(ia,ia,forcetime*kd); -// nlMatrixAdd(ia+1,ia+1,forcetime*kd); -// nlMatrixAdd(ia+2,ia+2,forcetime*kd); - } - } /* +++cached collision targets */ bp->choke = 0.0f; @@ -2268,44 +2276,25 @@ static void softbody_calc_forces(Object *ob, float forcetime, float timenow, int bp->flag &= ~SBF_DOFUZZY; if(do_deflector) { float cfforce[3],defforce[3] ={0.0f,0.0f,0.0f}, vel[3] = {0.0f,0.0f,0.0f}, facenormal[3], cf = 1.0f,intrusion; - kd = 1.0f; + float kd = 1.0f; if (sb_deflect_face(ob,bp->pos,facenormal,defforce,&cf,timenow,vel,&intrusion)){ - if ((!nl_flags)&&(intrusion < 0.0f)){ - /*bjornmose: uugh.. what an evil hack - violation of the 'don't touch bp->pos in here' rule - but works nice, like this--> - we predict the solution beeing out of the collider - in heun step No1 and leave the heun step No2 adapt to it - so we kind of introduced a implicit solver for this case - */ - Vec3PlusStVec(bp->pos,-intrusion,facenormal); - - sb->scratch->flag |= SBF_DOFUZZY; - bp->flag |= SBF_DOFUZZY; - bp->choke = sb->choke*0.01f; - } - else{ + VECSUB(cfforce,bp->vec,vel); Vec3PlusStVec(bp->force,-cf*50.0f,cfforce); - } - Vec3PlusStVec(bp->force,kd,defforce); - if (nl_flags & NLF_BUILD){ - // int ia =3*(sb->totpoint-a); - // int op =3*sb->totpoint; - //dfdx_goal(ia,ia,op,mpos); // don't do unless you know - //dfdv_goal(ia,ia,-cf); - - } - + + Vec3PlusStVec(bp->force,kd,defforce); } } /* ---cached collision targets */ /* +++springs */ + iks = 1.0f/(1.0f-sb->inspring)-1.0f ;/* inner spring constants function */ if(ob->softflag & OB_SB_EDGES) { if (sb->bspring){ /* spring list exists at all ? */ + int b; + BodySpring *bs; for(b=bp->nofsprings;b>0;b--){ bs = sb->bspring + bp->springs[b-1]; if (do_springcollision || do_aero){ @@ -2315,90 +2304,513 @@ static void softbody_calc_forces(Object *ob, float forcetime, float timenow, int } // sb_spring_force(Object *ob,int bpi,BodySpring *bs,float iks,float forcetime,int nl_flags) - sb_spring_force(ob,sb->totpoint-a,bs,iks,forcetime,nl_flags); + sb_spring_force(ob,ilast-bb,bs,iks,forcetime,0); }/* loop springs */ }/* existing spring list */ }/*any edges*/ /* ---springs */ }/*omit on snap */ }/*loop all bp's*/ +return 0; /*done fine*/ +} + +void *exec_softbody_calc_forces(void *data) +{ + SB_thread_context *pctx = (SB_thread_context*)data; + _softbody_calc_forces_slice_in_a_thread(pctx->ob,pctx->forcetime,pctx->timenow,pctx->ifirst,pctx->ilast,NULL,pctx->do_effector,pctx->do_deflector,pctx->fieldfactor,pctx->windfactor); + return 0; +} + +void sb_cf_threads_run(struct Object *ob, float forcetime, float timenow,int totpoint,int *ptr_to_break_func(),struct ListBase *do_effector,int do_deflector,float fieldfactor, float windfactor) +{ + ListBase threads; + SB_thread_context *sb_threads; + int i, totthread,left,dec; + int lowpoints =10; /* wild guess .. may increase with better thread management 'above' or even be UI option sb->spawn_cf_threads_nopts */ + + /* figure the number of threads while preventing pretty pointless threading overhead */ + if(totpoint < lowpoints) {totthread=1;} + else{ + if(G.scene->r.mode & R_FIXED_THREADS) + totthread= G.scene->r.threads; + else + totthread= BLI_system_thread_count(); + } + /*left to do--> what if we got zillions of CPUs running but 'totpoint' tasks to spread*/ + + sb_threads= MEM_callocN(sizeof(SB_thread_context)*totthread, "SBThread"); + memset(sb_threads, 0, sizeof(SB_thread_context)*totthread); + left = totpoint; + dec = totpoint/totthread +1; + for(i=0; i<totthread; i++) { + sb_threads[i].ob = ob; + sb_threads[i].forcetime = forcetime; + sb_threads[i].timenow = timenow; + sb_threads[i].ilast = left; + left = left - dec; + if (left >0){ + sb_threads[i].ifirst = left; + } + else + sb_threads[i].ifirst = 0; + sb_threads[i].do_effector = do_effector; + sb_threads[i].do_deflector = do_deflector; + sb_threads[i].fieldfactor = fieldfactor; + sb_threads[i].windfactor = windfactor; + sb_threads[i].nr= i; + sb_threads[i].tot= totthread; + } + + + if(totthread > 1) { + BLI_init_threads(&threads, exec_softbody_calc_forces, totthread); + + for(i=0; i<totthread; i++) + BLI_insert_thread(&threads, &sb_threads[i]); + + BLI_end_threads(&threads); + } + else + exec_softbody_calc_forces(&sb_threads[0]); + /* clean up */ + MEM_freeN(sb_threads); +} + +static void softbody_calc_forcesEx(Object *ob, float forcetime, float timenow, int nl_flags) +{ +/* rule we never alter free variables :bp->vec bp->pos in here ! + * this will ruin adaptive stepsize AKA heun! (BM) + */ + SoftBody *sb= ob->soft; /* is supposed to be there */ + BodyPoint *bproot; + ListBase *do_effector; + float iks, gravity; + float fieldfactor = 1000.0f, windfactor = 250.0f; + int do_deflector,do_selfcollision,do_springcollision,do_aero; + + gravity = sb->grav * sb_grav_force_scale(ob); + + /* check conditions for various options */ + do_deflector= query_external_colliders(ob); + do_selfcollision=((ob->softflag & OB_SB_EDGES) && (sb->bspring)&& (ob->softflag & OB_SB_SELF)); + do_springcollision=do_deflector && (ob->softflag & OB_SB_EDGES) &&(ob->softflag & OB_SB_EDGECOLL); + do_aero=((sb->aeroedge)&& (ob->softflag & OB_SB_EDGES)); + + iks = 1.0f/(1.0f-sb->inspring)-1.0f ;/* inner spring constants function */ + bproot= sb->bpoint; /* need this for proper spring addressing */ + + if (do_springcollision || do_aero) + sb_sfesf_threads_run(ob,timenow,sb->totspring,NULL); + + /* after spring scan because it uses Effoctors too */ + do_effector= pdInitEffectors(ob,NULL); + if (do_deflector) { + float defforce[3]; + do_deflector = sb_detect_aabb_collisionCached(defforce,ob->lay,ob,timenow); + } + + sb_cf_threads_run(ob,forcetime,timenow,sb->totpoint,NULL,do_effector,do_deflector,fieldfactor,windfactor); /* finally add forces caused by face collision */ if (ob->softflag & OB_SB_FACECOLL) scan_for_ext_face_forces(ob,timenow); /* finish matrix and solve */ -#if (0) // remove onl linking for now .. still i am not sure .. the jacobian can be usefull .. so keep that BM - if(nl_flags & NLF_SOLVE){ - //double sct,sst=PIL_check_seconds_timer(); - for(a=sb->totpoint, bp= sb->bpoint; a>0; a--, bp++) { - int iv =3*(sb->totpoint-a); - int ip =3*(2*sb->totpoint-a); - int n; - for (n=0;n<3;n++) {nlRightHandSideSet(0, iv+n, bp->force[0+n]);} - for (n=0;n<3;n++) {nlRightHandSideSet(0, ip+n, bp->vec[0+n]);} + if(do_effector) pdEndEffectors(do_effector); +} + + + + +static void softbody_calc_forces(Object *ob, float forcetime, float timenow, int nl_flags) +{ + /* redirection to the new threaded Version */ + if (G.rt !=16){ + softbody_calc_forcesEx(ob, forcetime, timenow, nl_flags); + return; + } + else{ + /* so the following will die */ + /* |||||||||||||||||||||||||| */ + /* VVVVVVVVVVVVVVVVVVVVVVVVVV */ + + /* rule we never alter free variables :bp->vec bp->pos in here ! + * this will ruin adaptive stepsize AKA heun! (BM) + */ + SoftBody *sb= ob->soft; /* is supposed to be there */ + BodyPoint *bp; + BodyPoint *bproot; + BodySpring *bs; + ListBase *do_effector; + float iks, ks, kd, gravity; + float fieldfactor = 1000.0f, windfactor = 250.0f; + float tune = sb->ballstiff; + int a, b, do_deflector,do_selfcollision,do_springcollision,do_aero; + + + /* jacobian + NLboolean success; + + if(nl_flags){ + nlBegin(NL_SYSTEM); + nlBegin(NL_MATRIX); } - nlEnd(NL_MATRIX); - nlEnd(NL_SYSTEM); + */ + + + gravity = sb->grav * sb_grav_force_scale(ob); + + /* check conditions for various options */ + do_deflector= query_external_colliders(ob); + do_selfcollision=((ob->softflag & OB_SB_EDGES) && (sb->bspring)&& (ob->softflag & OB_SB_SELF)); + do_springcollision=do_deflector && (ob->softflag & OB_SB_EDGES) &&(ob->softflag & OB_SB_EDGECOLL); + do_aero=((sb->aeroedge)&& (ob->softflag & OB_SB_EDGES)); + + iks = 1.0f/(1.0f-sb->inspring)-1.0f ;/* inner spring constants function */ + bproot= sb->bpoint; /* need this for proper spring addressing */ - if ((G.rt >0) && (nl_flags & NLF_BUILD)) - { - printf("####MEE#####\n"); - nlPrintMatrix(); + if (do_springcollision || do_aero) scan_for_ext_spring_forces(ob,timenow); + /* after spring scan because it uses Effoctors too */ + do_effector= pdInitEffectors(ob,NULL); + + if (do_deflector) { + float defforce[3]; + do_deflector = sb_detect_aabb_collisionCached(defforce,ob->lay,ob,timenow); } - success= nlSolveAdvanced(NULL, 1); + for(a=sb->totpoint, bp= sb->bpoint; a>0; a--, bp++) { + /* clear forces accumulator */ + bp->force[0]= bp->force[1]= bp->force[2]= 0.0; + if(nl_flags & NLF_BUILD){ + //int ia =3*(sb->totpoint-a); + //int op =3*sb->totpoint; + /* dF/dV = v */ + /* jacobioan + nlMatrixAdd(op+ia,ia,-forcetime); + nlMatrixAdd(op+ia+1,ia+1,-forcetime); + nlMatrixAdd(op+ia+2,ia+2,-forcetime); + + nlMatrixAdd(ia,ia,1); + nlMatrixAdd(ia+1,ia+1,1); + nlMatrixAdd(ia+2,ia+2,1); + + nlMatrixAdd(op+ia,op+ia,1); + nlMatrixAdd(op+ia+1,op+ia+1,1); + nlMatrixAdd(op+ia+2,op+ia+2,1); + */ - // nlPrintMatrix(); /* for debug purpose .. anyhow cropping B vector looks like working */ - if(success){ - float f; - int index =0; - /* for debug purpose .. anyhow cropping B vector looks like working */ - if (G.rt >0) - for(a=2*sb->totpoint, bp= sb->bpoint; a>0; a--, bp++) { - f=nlGetVariable(0,index); - printf("(%f ",f);index++; - f=nlGetVariable(0,index); - printf("%f ",f);index++; - f=nlGetVariable(0,index); - printf("%f)",f);index++; + + } + + /* naive ball self collision */ + /* needs to be done if goal snaps or not */ + if(do_selfcollision){ + int attached; + BodyPoint *obp; + int c,b; + float velcenter[3],dvel[3],def[3]; + float distance; + float compare; + + for(c=sb->totpoint, obp= sb->bpoint; c>=a; c--, obp++) { + + //if ((bp->octantflag & obp->octantflag) == 0) continue; + + compare = (obp->colball + bp->colball); + VecSubf(def, bp->pos, obp->pos); + + /* rather check the AABBoxes before ever calulating the real distance */ + /* mathematically it is completly nuts, but performace is pretty much (3) times faster */ + if ((ABS(def[0]) > compare) || (ABS(def[1]) > compare) || (ABS(def[2]) > compare)) continue; + + distance = Normalize(def); + if (distance < compare ){ + /* exclude body points attached with a spring */ + attached = 0; + for(b=obp->nofsprings;b>0;b--){ + bs = sb->bspring + obp->springs[b-1]; + if (( sb->totpoint-a == bs->v2) || ( sb->totpoint-a == bs->v1)){ + attached=1; + continue;} + } + if (!attached){ + float f = tune/(distance) + tune/(compare*compare)*distance - 2.0f*tune/compare ; + + VecMidf(velcenter, bp->vec, obp->vec); + VecSubf(dvel,velcenter,bp->vec); + VecMulf(dvel,sb->nodemass); + + Vec3PlusStVec(bp->force,f*(1.0f-sb->balldamp),def); + Vec3PlusStVec(bp->force,sb->balldamp,dvel); + + if(nl_flags & NLF_BUILD){ + //int ia =3*(sb->totpoint-a); + //int ic =3*(sb->totpoint-c); + //int op =3*sb->totpoint; + //float mvel = forcetime*sb->nodemass*sb->balldamp; + //float mpos = forcetime*tune*(1.0f-sb->balldamp); + /*some quick and dirty entries to the jacobian*/ + //dfdx_goal(ia,ia,op,mpos); + //dfdv_goal(ia,ia,mvel); + /* exploit force(a,b) == -force(b,a) part1/2 */ + //dfdx_goal(ic,ic,op,mpos); + //dfdv_goal(ic,ic,mvel); + + + /*TODO sit down an X-out the true jacobian entries*/ + /*well does not make to much sense because the eigenvalues + of the jacobian go negative; and negative eigenvalues + on a complex iterative system z(n+1)=A * z(n) + give imaginary roots in the charcateristic polynom + --> solutions that to z(t)=u(t)* exp ( i omega t) --> oscilations we don't want here + where u(t) is a unknown amplitude function (worst case rising fast) + */ + } + + /* exploit force(a,b) == -force(b,a) part2/2 */ + VecSubf(dvel,velcenter,obp->vec); + VecMulf(dvel,sb->nodemass); + + Vec3PlusStVec(obp->force,sb->balldamp,dvel); + Vec3PlusStVec(obp->force,-f*(1.0f-sb->balldamp),def); + + + } + } } + } + /* naive ball self collision done */ - index =0; - for(a=sb->totpoint, bp= sb->bpoint; a>0; a--, bp++) { + if(bp->goal < SOFTGOALSNAP){ /* ommit this bp when it snaps */ + float auxvect[3]; + float velgoal[3]; + + /* do goal stuff */ + if(ob->softflag & OB_SB_GOAL) { + /* true elastic goal */ + VecSubf(auxvect,bp->pos,bp->origT); + ks = 1.0f/(1.0f- bp->goal*sb->goalspring)-1.0f ; + bp->force[0]+= -ks*(auxvect[0]); + bp->force[1]+= -ks*(auxvect[1]); + bp->force[2]+= -ks*(auxvect[2]); + + if(nl_flags & NLF_BUILD){ + //int ia =3*(sb->totpoint-a); + //int op =3*(sb->totpoint); + /* depending on my pos */ + //dfdx_goal(ia,ia,op,ks*forcetime); + } + + + /* calulate damping forces generated by goals*/ + VecSubf(velgoal,bp->origS, bp->origE); + kd = sb->goalfrict * sb_fric_force_scale(ob) ; + VecAddf(auxvect,velgoal,bp->vec); + + if (forcetime > 0.0 ) { /* make sure friction does not become rocket motor on time reversal */ + bp->force[0]-= kd * (auxvect[0]); + bp->force[1]-= kd * (auxvect[1]); + bp->force[2]-= kd * (auxvect[2]); + if(nl_flags & NLF_BUILD){ + //int ia =3*(sb->totpoint-a); + Normalize(auxvect); + /* depending on my vel */ + //dfdv_goal(ia,ia,kd*forcetime); + } + + } + else { + bp->force[0]-= kd * (velgoal[0] - bp->vec[0]); + bp->force[1]-= kd * (velgoal[1] - bp->vec[1]); + bp->force[2]-= kd * (velgoal[2] - bp->vec[2]); + } + } + /* done goal stuff */ + + + /* gravitation */ + bp->force[2]-= gravity*sb->nodemass; /* individual mass of node here */ + //bp->force[1]-= gravity*sb->nodemass; /* individual mass of node here */ + + + /* particle field & vortex */ + if(do_effector) { + float force[3]= {0.0f, 0.0f, 0.0f}; + float speed[3]= {0.0f, 0.0f, 0.0f}; + float eval_sb_fric_force_scale = sb_fric_force_scale(ob); /* just for calling function once */ + + pdDoEffectors(do_effector, bp->pos, force, speed, (float)G.scene->r.cfra, 0.0f, PE_WIND_AS_SPEED); + + /* apply forcefield*/ + VecMulf(force,fieldfactor* eval_sb_fric_force_scale); + VECADD(bp->force, bp->force, force); + + /* BP friction in moving media */ + kd= sb->mediafrict* eval_sb_fric_force_scale; + bp->force[0] -= kd * (bp->vec[0] + windfactor*speed[0]/eval_sb_fric_force_scale); + bp->force[1] -= kd * (bp->vec[1] + windfactor*speed[1]/eval_sb_fric_force_scale); + bp->force[2] -= kd * (bp->vec[2] + windfactor*speed[2]/eval_sb_fric_force_scale); + /* now we'll have nice centrifugal effect for vortex */ + + } + else { + /* BP friction in media (not) moving*/ + kd= sb->mediafrict* sb_fric_force_scale(ob); + /* assume it to be proportional to actual velocity */ + bp->force[0]-= bp->vec[0]*kd; + bp->force[1]-= bp->vec[1]*kd; + bp->force[2]-= bp->vec[2]*kd; + /* friction in media done */ + if(nl_flags & NLF_BUILD){ + //int ia =3*(sb->totpoint-a); + /* da/dv = */ + + // nlMatrixAdd(ia,ia,forcetime*kd); + // nlMatrixAdd(ia+1,ia+1,forcetime*kd); + // nlMatrixAdd(ia+2,ia+2,forcetime*kd); + } + + } + /* +++cached collision targets */ + bp->choke = 0.0f; + bp->choke2 = 0.0f; + bp->flag &= ~SBF_DOFUZZY; + if(do_deflector) { + float cfforce[3],defforce[3] ={0.0f,0.0f,0.0f}, vel[3] = {0.0f,0.0f,0.0f}, facenormal[3], cf = 1.0f,intrusion; + kd = 1.0f; + + if (sb_deflect_face(ob,bp->pos,facenormal,defforce,&cf,timenow,vel,&intrusion)){ + if ((!nl_flags)&&(intrusion < 0.0f)){ + /*bjornmose: uugh.. what an evil hack + violation of the 'don't touch bp->pos in here' rule + but works nice, like this--> + we predict the solution beeing out of the collider + in heun step No1 and leave the heun step No2 adapt to it + so we kind of introduced a implicit solver for this case + */ + Vec3PlusStVec(bp->pos,-intrusion,facenormal); + + sb->scratch->flag |= SBF_DOFUZZY; + bp->flag |= SBF_DOFUZZY; + bp->choke = sb->choke*0.01f; + } + else{ + VECSUB(cfforce,bp->vec,vel); + Vec3PlusStVec(bp->force,-cf*50.0f,cfforce); + } + Vec3PlusStVec(bp->force,kd,defforce); + if (nl_flags & NLF_BUILD){ + // int ia =3*(sb->totpoint-a); + // int op =3*sb->totpoint; + //dfdx_goal(ia,ia,op,mpos); // don't do unless you know + //dfdv_goal(ia,ia,-cf); + + } + + } + + } + /* ---cached collision targets */ + + /* +++springs */ + if(ob->softflag & OB_SB_EDGES) { + if (sb->bspring){ /* spring list exists at all ? */ + for(b=bp->nofsprings;b>0;b--){ + bs = sb->bspring + bp->springs[b-1]; + if (do_springcollision || do_aero){ + VecAddf(bp->force,bp->force,bs->ext_force); + if (bs->flag & BSF_INTERSECT) + bp->choke = bs->cf; + + } + // sb_spring_force(Object *ob,int bpi,BodySpring *bs,float iks,float forcetime,int nl_flags) + // rather remove nl_falgs from code .. will make things a lot cleaner + sb_spring_force(ob,sb->totpoint-a,bs,iks,forcetime,0); + }/* loop springs */ + }/* existing spring list */ + }/*any edges*/ + /* ---springs */ + }/*omit on snap */ + }/*loop all bp's*/ + + + /* finally add forces caused by face collision */ + if (ob->softflag & OB_SB_FACECOLL) scan_for_ext_face_forces(ob,timenow); + + /* finish matrix and solve */ +#if (0) // remove onl linking for now .. still i am not sure .. the jacobian can be usefull .. so keep that BM + if(nl_flags & NLF_SOLVE){ + //double sct,sst=PIL_check_seconds_timer(); + for(a=sb->totpoint, bp= sb->bpoint; a>0; a--, bp++) { + int iv =3*(sb->totpoint-a); + int ip =3*(2*sb->totpoint-a); + int n; + for (n=0;n<3;n++) {nlRightHandSideSet(0, iv+n, bp->force[0+n]);} + for (n=0;n<3;n++) {nlRightHandSideSet(0, ip+n, bp->vec[0+n]);} + } + nlEnd(NL_MATRIX); + nlEnd(NL_SYSTEM); + + if ((G.rt == 32) && (nl_flags & NLF_BUILD)) + { + printf("####MEE#####\n"); + nlPrintMatrix(); + } + + success= nlSolveAdvanced(NULL, 1); + + // nlPrintMatrix(); /* for debug purpose .. anyhow cropping B vector looks like working */ + if(success){ + float f; + int index =0; + /* for debug purpose .. anyhow cropping B vector looks like working */ + if (G.rt ==32) + for(a=2*sb->totpoint, bp= sb->bpoint; a>0; a--, bp++) { + f=nlGetVariable(0,index); + printf("(%f ",f);index++; + f=nlGetVariable(0,index); + printf("%f ",f);index++; + f=nlGetVariable(0,index); + printf("%f)",f);index++; + } + + index =0; + for(a=sb->totpoint, bp= sb->bpoint; a>0; a--, bp++) { + f=nlGetVariable(0,index); + bp->impdv[0] = f; index++; + f=nlGetVariable(0,index); + bp->impdv[1] = f; index++; + f=nlGetVariable(0,index); + bp->impdv[2] = f; index++; + } + /* + for(a=sb->totpoint, bp= sb->bpoint; a>0; a--, bp++) { f=nlGetVariable(0,index); - bp->impdv[0] = f; index++; + bp->impdx[0] = f; index++; f=nlGetVariable(0,index); - bp->impdv[1] = f; index++; + bp->impdx[1] = f; index++; f=nlGetVariable(0,index); - bp->impdv[2] = f; index++; - } - /* + bp->impdx[2] = f; index++; + } + */ + } + else{ + printf("Matrix inversion failed \n"); for(a=sb->totpoint, bp= sb->bpoint; a>0; a--, bp++) { - f=nlGetVariable(0,index); - bp->impdx[0] = f; index++; - f=nlGetVariable(0,index); - bp->impdx[1] = f; index++; - f=nlGetVariable(0,index); - bp->impdx[2] = f; index++; + VECCOPY(bp->impdv,bp->force); } - */ - } - else{ - printf("Matrix inversion failed \n"); - for(a=sb->totpoint, bp= sb->bpoint; a>0; a--, bp++) { - VECCOPY(bp->impdv,bp->force); + } + //sct=PIL_check_seconds_timer(); + //if (sct-sst > 0.01f) printf(" implicit solver time %f %s \r",sct-sst,ob->id.name); } - - //sct=PIL_check_seconds_timer(); - //if (sct-sst > 0.01f) printf(" implicit solver time %f %s \r",sct-sst,ob->id.name); - } - /* cleanup */ + /* cleanup */ #endif - if(do_effector) pdEndEffectors(do_effector); + if(do_effector) pdEndEffectors(do_effector); + } } + static void softbody_apply_forces(Object *ob, float forcetime, int mode, float *err, int mid_flags) { /* time evolution */ @@ -2458,7 +2870,7 @@ static void softbody_apply_forces(Object *ob, float forcetime, int mode, float * /* x(t + dt) = x(t) + v(t~) * dt */ VecMulf(dx,forcetime); - /* the freezer */ + /* the freezer coming sooner or later */ /* if ((Inpf(dx,dx)<freezeloc )&&(Inpf(bp->force,bp->force)<freezeforce )){ bp->frozen /=2; @@ -3529,6 +3941,7 @@ static void softbody_step(Object *ob, SoftBody *sb, float dtime) * we don't want to lock up the system if physics fail */ int loops =0 ; + SoftHeunTol = sb->rklimit; /* humm .. this should be calculated from sb parameters and sizes */ if (sb->minloops > 0) forcetimemax = 1.0f / sb->minloops; @@ -3546,13 +3959,13 @@ static void softbody_step(Object *ob, SoftBody *sb, float dtime) sb->scratch->flag &= ~SBF_DOFUZZY; /* do predictive euler step */ softbody_calc_forces(ob, forcetime,timedone/dtime,0); - softbody_apply_forces(ob, forcetime, 1, NULL,mid_flags); + softbody_apply_forces(ob, forcetime, 1, NULL,mid_flags); /* crop new slope values to do averaged slope step */ softbody_calc_forces(ob, forcetime,timedone/dtime,0); - softbody_apply_forces(ob, forcetime, 2, &err,mid_flags); + softbody_apply_forces(ob, forcetime, 2, &err,mid_flags); softbody_apply_goalsnap(ob); if (err > SoftHeunTol) { /* error needs to be scaled to some quantity */ @@ -3603,7 +4016,7 @@ static void softbody_step(Object *ob, SoftBody *sb, float dtime) // if(G.f & G_DEBUG){ if(sb->solverflags & SBSO_MONITOR ){ if (loops > HEUNWARNLIMIT) /* monitor high loop counts */ - printf("\r needed %d steps/frame ",loops); + printf("\r needed %d steps/frame",loops); } } @@ -3627,7 +4040,7 @@ static void softbody_step(Object *ob, SoftBody *sb, float dtime) if(sb->solverflags & SBSO_MONITOR ){ sct=PIL_check_seconds_timer(); - if (sct-sst > 0.5f) printf(" solver time %f %s \r",sct-sst,ob->id.name); + if (sct-sst > 0.5f) printf(" solver time %f sec %s \n",sct-sst,ob->id.name); } } diff --git a/source/blender/blenlib/intern/BLI_kdopbvh.c b/source/blender/blenlib/intern/BLI_kdopbvh.c index ddea701dac5..9671551a7f1 100644 --- a/source/blender/blenlib/intern/BLI_kdopbvh.c +++ b/source/blender/blenlib/intern/BLI_kdopbvh.c @@ -1,7 +1,5 @@ /** * - * $Id$ - * * ***** BEGIN GPL LICENSE BLOCK ***** * * This program is free software; you can redistribute it and/or @@ -45,15 +43,17 @@ #include <omp.h> #endif + + +#define MAX_TREETYPE 32 + typedef struct BVHNode { - struct BVHNode **children; // max 8 children - struct BVHNode *parent; // needed for bottom - top update + struct BVHNode **children; float *bv; // Bounding volume of all nodes, max 13 axis int index; // face, edge, vertex index char totnode; // how many nodes are used, used for speedup - char traversed; // how many nodes already traversed until this level? - char main_axis; + char main_axis; // Axis used to split this node } BVHNode; struct BVHTree @@ -75,6 +75,7 @@ typedef struct BVHOverlapData BVHTree *tree1, *tree2; BVHTreeOverlap *overlap; int i, max_overlap; /* i is number of overlaps */ + int start_axis, stop_axis; } BVHOverlapData; typedef struct BVHNearestData @@ -285,139 +286,9 @@ int partition_nth_element(BVHNode **a, int _begin, int _end, int n, int axis){ ////////////////////////////////////////////////////////////////////////////////////////////////////// -void BLI_bvhtree_free(BVHTree *tree) -{ - if(tree) - { - MEM_freeN(tree->nodes); - MEM_freeN(tree->nodearray); - MEM_freeN(tree->nodebv); - MEM_freeN(tree->nodechild); - MEM_freeN(tree); - } -} - -// calculate max number of branches -int needed_branches(int tree_type, int leafs) -{ -#if 1 - //Worst case scenary ( return max(0, leafs-tree_type)+1 ) - if(leafs <= tree_type) - return 1; - else - return leafs-tree_type+1; - -#else - //If our bvh kdop is "almost perfect" - //TODO i dont trust the float arithmetic in here (and I am not sure this formula is according to our splitting method) - int i, numbranches = 0; - for(i = 1; i <= (int)ceil((float)((float)log(leafs)/(float)log(tree_type))); i++) - numbranches += (pow(tree_type, i) / tree_type); - - return numbranches; -#endif -} - - -BVHTree *BLI_bvhtree_new(int maxsize, float epsilon, char tree_type, char axis) -{ - BVHTree *tree; - int numnodes, i; - - // theres not support for trees below binary-trees :P - if(tree_type < 2) - return NULL; - - tree = (BVHTree *)MEM_callocN(sizeof(BVHTree), "BVHTree"); - - if(tree) - { - tree->epsilon = epsilon; - tree->tree_type = tree_type; - tree->axis = axis; - - if(axis == 26) - { - tree->start_axis = 0; - tree->stop_axis = 13; - } - else if(axis == 18) - { - tree->start_axis = 7; - tree->stop_axis = 13; - } - else if(axis == 14) - { - tree->start_axis = 0; - tree->stop_axis = 7; - } - else if(axis == 8) // AABB - { - tree->start_axis = 0; - tree->stop_axis = 4; - } - else if(axis == 6) // OBB - { - tree->start_axis = 0; - tree->stop_axis = 3; - } - else - { - MEM_freeN(tree); - return NULL; - } - - - //Allocate arrays - numnodes = maxsize + needed_branches(tree_type, maxsize) + tree_type; - - tree->nodes = (BVHNode **)MEM_callocN(sizeof(BVHNode *)*numnodes, "BVHNodes"); - - if(!tree->nodes) - { - MEM_freeN(tree); - return NULL; - } - - tree->nodebv = (float*)MEM_callocN(sizeof(float)* axis * numnodes, "BVHNodeBV"); - if(!tree->nodebv) - { - MEM_freeN(tree->nodes); - MEM_freeN(tree); - } - - tree->nodechild = (BVHNode**)MEM_callocN(sizeof(BVHNode*) * tree_type * numnodes, "BVHNodeBV"); - if(!tree->nodechild) - { - MEM_freeN(tree->nodebv); - MEM_freeN(tree->nodes); - MEM_freeN(tree); - } - - tree->nodearray = (BVHNode *)MEM_callocN(sizeof(BVHNode)* numnodes, "BVHNodeArray"); - - if(!tree->nodearray) - { - MEM_freeN(tree->nodechild); - MEM_freeN(tree->nodebv); - MEM_freeN(tree->nodes); - MEM_freeN(tree); - return NULL; - } - - //link the dynamic bv and child links - for(i=0; i< numnodes; i++) - { - tree->nodearray[i].bv = tree->nodebv + i * axis; - tree->nodearray[i].children = tree->nodechild + i * tree_type; - } - - } - - return tree; -} - - +/* + * BVHTree bounding volumes functions + */ static void create_kdop_hull(BVHTree *tree, BVHNode *node, float *co, int numpoints, int moving) { float newminmax; @@ -479,36 +350,6 @@ static void refit_kdop_hull(BVHTree *tree, BVHNode *node, int start, int end) } -int BLI_bvhtree_insert(BVHTree *tree, int index, float *co, int numpoints) -{ - int i; - BVHNode *node = NULL; - - // insert should only possible as long as tree->totbranch is 0 - if(tree->totbranch > 0) - return 0; - - if(tree->totleaf+1 >= MEM_allocN_len(tree->nodes)/sizeof(*(tree->nodes))) - return 0; - - // TODO check if have enough nodes in array - - node = tree->nodes[tree->totleaf] = &(tree->nodearray[tree->totleaf]); - tree->totleaf++; - - create_kdop_hull(tree, node, co, numpoints, 0); - node->index= index; - - // inflate the bv with some epsilon - for (i = tree->start_axis; i < tree->stop_axis; i++) - { - node->bv[(2 * i)] -= tree->epsilon; // minimum - node->bv[(2 * i) + 1] += tree->epsilon; // maximum - } - - return 1; -} - // only supports x,y,z axis in the moment // but we should use a plain and simple function here for speed sake static char get_largest_axis(float *bv) @@ -534,113 +375,42 @@ static char get_largest_axis(float *bv) } } -static void bvh_div_nodes(BVHTree *tree, BVHNode *node, int start, int end, int free_node_index) +// bottom-up update of bvh node BV +// join the children on the parent BV +static void node_join(BVHTree *tree, BVHNode *node) { - int i; - - const char laxis = get_largest_axis(node->bv); //determine longest axis to split along - const int slice = (end-start)/tree->tree_type; //division rounded down - const int rest = (end-start)%tree->tree_type; //remainder of division - - assert( node->totnode == 0 ); - - node->main_axis = laxis/2; + int i, j; - // split nodes along longest axis - for (i=0; start < end; node->totnode = ++i) //i counts the current child - { - int tend = start + slice + (i < rest ? 1 : 0); - - assert( tend <= end); - - if(tend-start == 1) // ok, we have 1 left for this node - { - node->children[i] = tree->nodes[start]; - node->children[i]->parent = node; - } - else - { - BVHNode *tnode = node->children[i] = tree->nodes[free_node_index] = &(tree->nodearray[free_node_index]); - tnode->parent = node; - - if(tend != end) - partition_nth_element(tree->nodes, start, end, tend, laxis); - - refit_kdop_hull(tree, tnode, start, tend); - - bvh_div_nodes(tree, tnode, start, tend, free_node_index+1); - free_node_index += needed_branches(tree->tree_type, tend-start); - } - start = tend; + for (i = tree->start_axis; i < tree->stop_axis; i++) + { + node->bv[2*i] = FLT_MAX; + node->bv[2*i + 1] = -FLT_MAX; } - return; -} - -static void omp_bvh_div_nodes(BVHTree *tree, BVHNode *node, int start, int end, int free_node_index) -{ - int i; - - const char laxis = get_largest_axis(node->bv); //determine longest axis to split along - const int slice = (end-start)/tree->tree_type; //division rounded down - const int rest = (end-start)%tree->tree_type; //remainder of division - - int omp_data_start[tree->tree_type]; - int omp_data_end [tree->tree_type]; - int omp_data_index[tree->tree_type]; - - assert( node->totnode == 0 ); - - node->main_axis = laxis/2; - - // split nodes along longest axis - for (i=0; start < end; node->totnode = ++i) //i counts the current child - { - //Split the rest from left to right (TODO: this doenst makes an optimal tree) - int tend = start + slice + (i < rest ? 1 : 0); - - assert( tend <= end); - - //save data for later OMP - omp_data_start[i] = start; - omp_data_end [i] = tend; - omp_data_index[i] = free_node_index; - - if(tend-start == 1) - { - node->children[i] = tree->nodes[start]; - node->children[i]->parent = node; - } - else - { - node->children[i] = tree->nodes[free_node_index] = &(tree->nodearray[free_node_index]); - node->children[i]->parent = node; - - if(tend != end) - partition_nth_element(tree->nodes, start, end, tend, laxis); - - free_node_index += needed_branches(tree->tree_type, tend-start); - } - - start = tend; - } - -#pragma omp parallel for private(i) schedule(static) - for( i = 0; i < node->totnode; i++) + for (i = 0; i < tree->tree_type; i++) { - if(omp_data_end[i]-omp_data_start[i] > 1) + if (node->children[i]) { - BVHNode *tnode = node->children[i]; - refit_kdop_hull(tree, tnode, omp_data_start[i], omp_data_end[i]); - bvh_div_nodes (tree, tnode, omp_data_start[i], omp_data_end[i], omp_data_index[i]+1); + for (j = tree->start_axis; j < tree->stop_axis; j++) + { + // update minimum + if (node->children[i]->bv[(2 * j)] < node->bv[(2 * j)]) + node->bv[(2 * j)] = node->children[i]->bv[(2 * j)]; + + // update maximum + if (node->children[i]->bv[(2 * j) + 1] > node->bv[(2 * j) + 1]) + node->bv[(2 * j) + 1] = node->children[i]->bv[(2 * j) + 1]; + } } + else + break; } - - return; } - -static void print_tree(BVHTree *tree, BVHNode *node, int depth) +/* + * Debug and information functions + */ +static void bvhtree_print_tree(BVHTree *tree, BVHNode *node, int depth) { int i; for(i=0; i<depth; i++) printf(" "); @@ -651,70 +421,89 @@ static void print_tree(BVHTree *tree, BVHNode *node, int depth) for(i=0; i<tree->tree_type; i++) if(node->children[i]) - print_tree(tree, node->children[i], depth+1); + bvhtree_print_tree(tree, node->children[i], depth+1); +} + +static void bvhtree_info(BVHTree *tree) +{ + printf("BVHTree info\n"); + printf("tree_type = %d, axis = %d, epsilon = %f\n", tree->tree_type, tree->axis, tree->epsilon); + printf("nodes = %d, branches = %d, leafs = %d\n", tree->totbranch + tree->totleaf, tree->totbranch, tree->totleaf); + printf("Memory per node = %dbytes\n", sizeof(BVHNode) + sizeof(BVHNode*)*tree->tree_type + sizeof(float)*tree->axis); + printf("BV memory = %dbytes\n", MEM_allocN_len(tree->nodebv)); + + printf("Total memory = %dbytes\n", sizeof(BVHTree) + + MEM_allocN_len(tree->nodes) + + MEM_allocN_len(tree->nodearray) + + MEM_allocN_len(tree->nodechild) + + MEM_allocN_len(tree->nodebv) + ); + +// bvhtree_print_tree(tree, tree->nodes[tree->totleaf], 0); } #if 0 + static void verify_tree(BVHTree *tree) { int i, j, check = 0; // check the pointer list for(i = 0; i < tree->totleaf; i++) -{ + { if(tree->nodes[i]->parent == NULL) printf("Leaf has no parent: %d\n", i); - else -{ - for(j = 0; j < tree->tree_type; j++) -{ - if(tree->nodes[i]->parent->children[j] == tree->nodes[i]) - check = 1; -} - if(!check) -{ - printf("Parent child relationship doesn't match: %d\n", i); -} - check = 0; -} -} + else + { + for(j = 0; j < tree->tree_type; j++) + { + if(tree->nodes[i]->parent->children[j] == tree->nodes[i]) + check = 1; + } + if(!check) + { + printf("Parent child relationship doesn't match: %d\n", i); + } + check = 0; + } + } // check the leaf list - for(i = 0; i < tree->totleaf; i++) -{ - if(tree->nodearray[i].parent == NULL) - printf("Leaf has no parent: %d\n", i); - else -{ - for(j = 0; j < tree->tree_type; j++) -{ - if(tree->nodearray[i].parent->children[j] == &tree->nodearray[i]) - check = 1; -} - if(!check) -{ - printf("Parent child relationship doesn't match: %d\n", i); -} - check = 0; -} -} + for(i = 0; i < tree->totleaf; i++) + { + if(tree->nodearray[i].parent == NULL) + printf("Leaf has no parent: %d\n", i); + else + { + for(j = 0; j < tree->tree_type; j++) + { + if(tree->nodearray[i].parent->children[j] == &tree->nodearray[i]) + check = 1; + } + if(!check) + { + printf("Parent child relationship doesn't match: %d\n", i); + } + check = 0; + } + } - printf("branches: %d, leafs: %d, total: %d\n", tree->totbranch, tree->totleaf, tree->totbranch + tree->totleaf); + printf("branches: %d, leafs: %d, total: %d\n", tree->totbranch, tree->totleaf, tree->totbranch + tree->totleaf); } #endif -//Helper data and structures to build generalized implicit trees -//This code can be easily reduced +//Helper data and structures to build a min-leaf generalized implicit tree +//This code can be easily reduced (basicly this is only method to calculate pow(k, n) in O(1).. and stuff like that) typedef struct BVHBuildHelper { - int tree_type; // - int totleafs; // + int tree_type; // + int totleafs; // - int leafs_per_child [32]; //Min number of leafs that are archievable from a node at depth N - int branches_on_level[32]; //Number of nodes at depth N (tree_type^N) + int leafs_per_child [32]; //Min number of leafs that are archievable from a node at depth N + int branches_on_level[32]; //Number of nodes at depth N (tree_type^N) - int remain_leafs; //Number of leafs that are placed on the level that is not 100% filled + int remain_leafs; //Number of leafs that are placed on the level that is not 100% filled } BVHBuildHelper; @@ -729,10 +518,10 @@ static void build_implicit_tree_helper(BVHTree *tree, BVHBuildHelper *data) //Calculate the smallest tree_type^n such that tree_type^n >= num_leafs for( - data->leafs_per_child[0] = 1; - data->leafs_per_child[0] < data->totleafs; - data->leafs_per_child[0] *= data->tree_type - ); + data->leafs_per_child[0] = 1; + data->leafs_per_child[0] < data->totleafs; + data->leafs_per_child[0] *= data->tree_type + ); data->branches_on_level[0] = 1; @@ -760,52 +549,141 @@ static int implicit_leafs_index(BVHBuildHelper *data, int depth, int child_index return data->remain_leafs; } -//WARNING: Beautiful/tricky code starts here :P -//Generalized implicit trees -static void non_recursive_bvh_div_nodes(BVHTree *tree) +/** + * Generalized implicit tree build + * + * An implicit tree is a tree where its structure is implied, thus there is no need to store child pointers or indexs. + * Its possible to find the position of the child or the parent with simple maths (multiplication and adittion). This type + * of tree is for example used on heaps.. where node N has its childs at indexs N*2 and N*2+1. + * + * Altought in this case the tree type is general.. and not know until runtime. + * tree_type stands for the maximum number of childs that a tree node can have. + * All tree types >= 2 are supported. + * + * Advantages of the used trees include: + * - No need to store child/parent relations (they are implicit); + * - Any node child always has an index greater than the parent; + * - Brother nodes are sequencial in memory; + * + * + * Some math relations derived for general implicit trees: + * + * K = tree_type, ( 2 <= K ) + * ROOT = 1 + * N child of node A = A * K + (2 - K) + N, (0 <= N < K) + * + * Util methods: + * TODO... + * (looping elements, knowing if its a leaf or not.. etc...) + */ + +// This functions returns the number of branches needed to have the requested number of leafs. +static int implicit_needed_branches(int tree_type, int leafs) +{ + return MAX2(1, (leafs + tree_type - 3) / (tree_type-1) ); +} + +/* + * This function handles the problem of "sorting" the leafs (along the split_axis). + * + * It arranges the elements in the given partitions such that: + * - any element in partition N is less or equal to any element in partition N+1. + * - if all elements are diferent all partition will get the same subset of elements + * as if the array was sorted. + * + * partition P is described as the elements in the range ( nth[P] , nth[P+1] ] + * + * TODO: This can be optimized a bit by doing a specialized nth_element instead of K nth_elements + */ +static void split_leafs(BVHNode **leafs_array, int *nth, int partitions, int split_axis) +{ + int i; + for(i=0; i < partitions-1; i++) + { + if(nth[i] >= nth[partitions]) + break; + + partition_nth_element(leafs_array, nth[i], nth[partitions], nth[i+1], split_axis); + } +} + +/* + * This functions builds an optimal implicit tree from the given leafs. + * Where optimal stands for: + * - The resulting tree will have the smallest number of branches; + * - At most only one branch will have NULL childs; + * - All leafs will be stored at level N or N+1. + * + * This function creates an implicit tree on branches_array, the leafs are given on the leafs_array. + * + * The tree is built per depth levels. First branchs at depth 1.. then branches at depth 2.. etc.. + * The reason is that we can build level N+1 from level N witouth any data dependencies.. thus it allows + * to use multithread building. + * + * To archieve this is necessary to find how much leafs are accessible from a certain branch, BVHBuildHelper + * implicit_needed_branches and implicit_leafs_index are auxiliar functions to solve that "optimal-split". + */ +static void non_recursive_bvh_div_nodes(BVHTree *tree, BVHNode *branches_array, BVHNode **leafs_array, int num_leafs) { int i; const int tree_type = tree->tree_type; const int tree_offset = 2 - tree->tree_type; //this value is 0 (on binary trees) and negative on the others - const int num_leafs = tree->totleaf; - const int num_branches= MAX2(1, (num_leafs + tree_type - 3) / (tree_type-1) ); - - BVHNode* branches_array = tree->nodearray + tree->totleaf - 1; // This code uses 1 index arrays - BVHNode** leafs_array = tree->nodes; + const int num_branches= implicit_needed_branches(tree_type, num_leafs); BVHBuildHelper data; - int depth = 0; + int depth; + branches_array--; //Implicit trees use 1-based indexs + build_implicit_tree_helper(tree, &data); - //YAY this could be 1 loop.. but had to split in 2 to remove OMP dependencies - for(i=1; i <= num_branches; i = i*tree_type + tree_offset) + //Loop tree levels (log N) loops + for(i=1, depth = 1; i <= num_branches; i = i*tree_type + tree_offset, depth++) { const int first_of_next_level = i*tree_type + tree_offset; const int end_j = MIN2(first_of_next_level, num_branches + 1); //index of last branch on this level int j; - depth++; - + //Loop all branches on this level #pragma omp parallel for private(j) schedule(static) for(j = i; j < end_j; j++) { int k; const int parent_level_index= j-i; BVHNode* parent = branches_array + j; + int nth_positions[ MAX_TREETYPE + 1]; char split_axis; int parent_leafs_begin = implicit_leafs_index(&data, depth, parent_level_index); int parent_leafs_end = implicit_leafs_index(&data, depth, parent_level_index+1); - //split_axis = (depth*2 % 6); //use this instead of the 2 following lines for XYZ splitting - + //This calculates the bounding box of this branch + //and chooses the largest axis as the axis to divide leafs refit_kdop_hull(tree, parent, parent_leafs_begin, parent_leafs_end); split_axis = get_largest_axis(parent->bv); + //Save split axis (this can be used on raytracing to speedup the query time) parent->main_axis = split_axis / 2; + //Split the childs along the split_axis, note: its not needed to sort the whole leafs array + //Only to assure that the elements are partioned on a way that each child takes the elements + //it would take in case the whole array was sorted. + //Split_leafs takes care of that "sort" problem. + nth_positions[ 0] = parent_leafs_begin; + nth_positions[tree_type] = parent_leafs_end; + for(k = 1; k < tree_type; k++) + { + int child_index = j * tree_type + tree_offset + k; + int child_level_index = child_index - first_of_next_level; //child level index + nth_positions[k] = implicit_leafs_index(&data, depth+1, child_level_index); + } + + split_leafs(leafs_array, nth_positions, tree_type, split_axis); + + + //Setup children and totnode counters + //Not really needed but currently most of BVH code relies on having an explicit children structure for(k = 0; k < tree_type; k++) { int child_index = j * tree_type + tree_offset + k; @@ -814,83 +692,245 @@ static void non_recursive_bvh_div_nodes(BVHTree *tree) int child_leafs_begin = implicit_leafs_index(&data, depth+1, child_level_index); int child_leafs_end = implicit_leafs_index(&data, depth+1, child_level_index+1); - assert( k != 0 || child_leafs_begin == parent_leafs_begin); - if(child_leafs_end - child_leafs_begin > 1) - { parent->children[k] = branches_array + child_index; - parent->children[k]->parent = parent; - -/* - printf("Add child %d (%d) to branch %d\n", - branches_array + child_index - tree->nodearray, - branches_array[ child_index ].index, - parent - tree->nodearray - ); -*/ - - partition_nth_element(leafs_array, child_leafs_begin, parent_leafs_end, child_leafs_end, split_axis); - } else if(child_leafs_end - child_leafs_begin == 1) - { -/* - printf("Add child %d (%d) to branch %d\n", - leafs_array[ child_leafs_begin ] - tree->nodearray, - leafs_array[ child_leafs_begin ]->index, - parent - tree->nodearray - ); -*/ parent->children[k] = leafs_array[ child_leafs_begin ]; - parent->children[k]->parent = parent; - } else - { - parent->children[k] = NULL; break; - } + parent->totnode = k+1; } } } +} + + +/* + * BLI_bvhtree api + */ +BVHTree *BLI_bvhtree_new(int maxsize, float epsilon, char tree_type, char axis) +{ + BVHTree *tree; + int numnodes, i; + + // theres not support for trees below binary-trees :P + if(tree_type < 2) + return NULL; + + if(tree_type > MAX_TREETYPE) + return NULL; + + tree = (BVHTree *)MEM_callocN(sizeof(BVHTree), "BVHTree"); + + if(tree) + { + tree->epsilon = epsilon; + tree->tree_type = tree_type; + tree->axis = axis; + + if(axis == 26) + { + tree->start_axis = 0; + tree->stop_axis = 13; + } + else if(axis == 18) + { + tree->start_axis = 7; + tree->stop_axis = 13; + } + else if(axis == 14) + { + tree->start_axis = 0; + tree->stop_axis = 7; + } + else if(axis == 8) // AABB + { + tree->start_axis = 0; + tree->stop_axis = 4; + } + else if(axis == 6) // OBB + { + tree->start_axis = 0; + tree->stop_axis = 3; + } + else + { + MEM_freeN(tree); + return NULL; + } + + + //Allocate arrays + numnodes = maxsize + implicit_needed_branches(tree_type, maxsize) + tree_type; + tree->nodes = (BVHNode **)MEM_callocN(sizeof(BVHNode *)*numnodes, "BVHNodes"); + + if(!tree->nodes) + { + MEM_freeN(tree); + return NULL; + } + + tree->nodebv = (float*)MEM_callocN(sizeof(float)* axis * numnodes, "BVHNodeBV"); + if(!tree->nodebv) + { + MEM_freeN(tree->nodes); + MEM_freeN(tree); + } - for(i = 0; i<num_branches; i++) - tree->nodes[tree->totleaf + i] = branches_array + 1 + i; + tree->nodechild = (BVHNode**)MEM_callocN(sizeof(BVHNode*) * tree_type * numnodes, "BVHNodeBV"); + if(!tree->nodechild) + { + MEM_freeN(tree->nodebv); + MEM_freeN(tree->nodes); + MEM_freeN(tree); + } + + tree->nodearray = (BVHNode *)MEM_callocN(sizeof(BVHNode)* numnodes, "BVHNodeArray"); + + if(!tree->nodearray) + { + MEM_freeN(tree->nodechild); + MEM_freeN(tree->nodebv); + MEM_freeN(tree->nodes); + MEM_freeN(tree); + return NULL; + } + + //link the dynamic bv and child links + for(i=0; i< numnodes; i++) + { + tree->nodearray[i].bv = tree->nodebv + i * axis; + tree->nodearray[i].children = tree->nodechild + i * tree_type; + } + + } - tree->totbranch = num_branches; + return tree; +} -// BLI_bvhtree_update_tree(tree); //Uncoment this for XYZ splitting +void BLI_bvhtree_free(BVHTree *tree) +{ + if(tree) + { + MEM_freeN(tree->nodes); + MEM_freeN(tree->nodearray); + MEM_freeN(tree->nodebv); + MEM_freeN(tree->nodechild); + MEM_freeN(tree); + } } void BLI_bvhtree_balance(BVHTree *tree) { - if(tree->totleaf == 0) return; + int i; + + BVHNode* branches_array = tree->nodearray + tree->totleaf; + BVHNode** leafs_array = tree->nodes; + //This function should only be called once (some big bug goes here if its being called more than once per tree) assert(tree->totbranch == 0); - non_recursive_bvh_div_nodes(tree); -/* - if(tree->totleaf != 0) + //Build the implicit tree + non_recursive_bvh_div_nodes(tree, branches_array, leafs_array, tree->totleaf); + + //current code expects the branches to be linked to the nodes array + //we perform that linkage here + tree->totbranch = implicit_needed_branches(tree->tree_type, tree->totleaf); + for(i = 0; i < tree->totbranch; i++) + tree->nodes[tree->totleaf + i] = branches_array + i; + + //bvhtree_info(tree); +} + +int BLI_bvhtree_insert(BVHTree *tree, int index, float *co, int numpoints) +{ + int i; + BVHNode *node = NULL; + + // insert should only possible as long as tree->totbranch is 0 + if(tree->totbranch > 0) + return 0; + + if(tree->totleaf+1 >= MEM_allocN_len(tree->nodes)/sizeof(*(tree->nodes))) + return 0; + + // TODO check if have enough nodes in array + + node = tree->nodes[tree->totleaf] = &(tree->nodearray[tree->totleaf]); + tree->totleaf++; + + create_kdop_hull(tree, node, co, numpoints, 0); + node->index= index; + + // inflate the bv with some epsilon + for (i = tree->start_axis; i < tree->stop_axis; i++) { - // create root node - BVHNode *node = tree->nodes[tree->totleaf] = &(tree->nodearray[tree->totleaf]); - tree->totbranch++; - < - // refit root bvh node - refit_kdop_hull(tree, node, 0, tree->totleaf); - - // create + balance tree - omp_bvh_div_nodes(tree, node, 0, tree->totleaf, tree->totleaf+1); - tree->totbranch = needed_branches( tree->tree_type, tree->totleaf ); - // verify_tree(tree); + node->bv[(2 * i)] -= tree->epsilon; // minimum + node->bv[(2 * i) + 1] += tree->epsilon; // maximum + } + + return 1; } -*/ + +// call before BLI_bvhtree_update_tree() +int BLI_bvhtree_update_node(BVHTree *tree, int index, float *co, float *co_moving, int numpoints) +{ + int i; + BVHNode *node= NULL; + + // check if index exists + if(index > tree->totleaf) + return 0; + + node = tree->nodearray + index; + + create_kdop_hull(tree, node, co, numpoints, 0); + + if(co_moving) + create_kdop_hull(tree, node, co_moving, numpoints, 1); + + // inflate the bv with some epsilon + for (i = tree->start_axis; i < tree->stop_axis; i++) + { + node->bv[(2 * i)] -= tree->epsilon; // minimum + node->bv[(2 * i) + 1] += tree->epsilon; // maximum + } + + return 1; } +// call BLI_bvhtree_update_node() first for every node/point/triangle +void BLI_bvhtree_update_tree(BVHTree *tree) +{ + //Update bottom=>top + //TRICKY: the way we build the tree all the childs have an index greater than the parent + //This allows us todo a bottom up update by starting on the biger numbered branch + + BVHNode** root = tree->nodes + tree->totleaf; + BVHNode** index = tree->nodes + tree->totleaf + tree->totbranch-1; + + for (; index >= root; index--) + node_join(tree, *index); +} + +float BLI_bvhtree_getepsilon(BVHTree *tree) +{ + return tree->epsilon; +} + + +/* + * BLI_bvhtree_overlap + */ // overlap - is it possbile for 2 bv's to collide ? -static int tree_overlap(float *bv1, float *bv2, int start_axis, int stop_axis) +static int tree_overlap(BVHNode *node1, BVHNode *node2, int start_axis, int stop_axis) { + float *bv1 = node1->bv; + float *bv2 = node2->bv; + float *bv1_end = bv1 + (stop_axis<<1); bv1 += start_axis<<1; @@ -910,7 +950,7 @@ static void traverse(BVHOverlapData *data, BVHNode *node1, BVHNode *node2) { int j; - if(tree_overlap(node1->bv, node2->bv, MIN2(data->tree1->start_axis, data->tree2->start_axis), MIN2(data->tree1->stop_axis, data->tree2->stop_axis))) + if(tree_overlap(node1, node2, data->start_axis, data->stop_axis)) { // check if node1 is a leaf if(!node1->totnode) @@ -976,7 +1016,7 @@ BVHTreeOverlap *BLI_bvhtree_overlap(BVHTree *tree1, BVHTree *tree2, int *result) return 0; // fast check root nodes for collision before doing big splitting + traversal - if(!tree_overlap(tree1->nodes[tree1->totleaf]->bv, tree2->nodes[tree2->totleaf]->bv, MIN2(tree1->start_axis, tree2->start_axis), MIN2(tree1->stop_axis, tree2->stop_axis))) + if(!tree_overlap(tree1->nodes[tree1->totleaf], tree2->nodes[tree2->totleaf], MIN2(tree1->start_axis, tree2->start_axis), MIN2(tree1->stop_axis, tree2->stop_axis))) return 0; data = MEM_callocN(sizeof(BVHOverlapData *)* tree1->tree_type, "BVHOverlapData_star"); @@ -991,6 +1031,8 @@ BVHTreeOverlap *BLI_bvhtree_overlap(BVHTree *tree1, BVHTree *tree2, int *result) data[j]->tree2 = tree2; data[j]->max_overlap = MAX2(tree1->totleaf, tree2->totleaf); data[j]->i = 0; + data[j]->start_axis = MIN2(tree1->start_axis, tree2->start_axis); + data[j]->stop_axis = MIN2(tree1->stop_axis, tree2->stop_axis ); } #pragma omp parallel for private(j) schedule(static) @@ -1022,98 +1064,8 @@ BVHTreeOverlap *BLI_bvhtree_overlap(BVHTree *tree1, BVHTree *tree2, int *result) } - -// bottom up update of bvh tree: -// join the 4 children here -static void node_join(BVHTree *tree, BVHNode *node) -{ - int i, j; - - for (i = tree->start_axis; i < tree->stop_axis; i++) - { - node->bv[2*i] = FLT_MAX; - node->bv[2*i + 1] = -FLT_MAX; - } - - for (i = 0; i < tree->tree_type; i++) - { - if (node->children[i]) - { - for (j = tree->start_axis; j < tree->stop_axis; j++) - { - // update minimum - if (node->children[i]->bv[(2 * j)] < node->bv[(2 * j)]) - node->bv[(2 * j)] = node->children[i]->bv[(2 * j)]; - - // update maximum - if (node->children[i]->bv[(2 * j) + 1] > node->bv[(2 * j) + 1]) - node->bv[(2 * j) + 1] = node->children[i]->bv[(2 * j) + 1]; - } - } - else - break; - } -} - -// call before BLI_bvhtree_update_tree() -int BLI_bvhtree_update_node(BVHTree *tree, int index, float *co, float *co_moving, int numpoints) -{ - BVHNode *node= NULL; - int i = 0; - - // check if index exists - if(index > tree->totleaf) - return 0; - - node = tree->nodearray + index; - - create_kdop_hull(tree, node, co, numpoints, 0); - - if(co_moving) - create_kdop_hull(tree, node, co_moving, numpoints, 1); - - // inflate the bv with some epsilon - for (i = tree->start_axis; i < tree->stop_axis; i++) - { - node->bv[(2 * i)] -= tree->epsilon; // minimum - node->bv[(2 * i) + 1] += tree->epsilon; // maximum - } - - return 1; -} - -// call BLI_bvhtree_update_node() first for every node/point/triangle -void BLI_bvhtree_update_tree(BVHTree *tree) -{ - BVHNode *leaf, *parent; - - // reset tree traversing flag - for (leaf = tree->nodearray + tree->totleaf; leaf != tree->nodearray + tree->totleaf + tree->totbranch; leaf++) - leaf->traversed = 0; - - for (leaf = tree->nodearray; leaf != tree->nodearray + tree->totleaf; leaf++) - { - for (parent = leaf->parent; parent; parent = parent->parent) - { - parent->traversed++; // we tried to go up in hierarchy - if (parent->traversed < parent->totnode) - break; // we do not need to check further - else - node_join(tree, parent); - } - } -} - -float BLI_bvhtree_getepsilon(BVHTree *tree) -{ - return tree->epsilon; -} - - - - /* - * Nearest neighbour + * Nearest neighbour - BLI_bvhtree_find_nearest */ static float squared_dist(const float *a, const float *b) { @@ -1122,6 +1074,7 @@ static float squared_dist(const float *a, const float *b) return INPR(tmp, tmp); } +//Determines the nearest point of the given node BV. Returns the squared distance to that point. static float calc_nearest_point(BVHNearestData *data, BVHNode *node, float *nearest) { int i; @@ -1143,19 +1096,19 @@ static float calc_nearest_point(BVHNearestData *data, BVHNode *node, float *near VECCOPY(nearest, data->co); for(i = data->tree->start_axis; i != data->tree->stop_axis; i++, bv+=2) { - float proj = INPR( nearest, KDOP_AXES[i]); - float dl = bv[0] - proj; - float du = bv[1] - proj; + float proj = INPR( nearest, KDOP_AXES[i]); + float dl = bv[0] - proj; + float du = bv[1] - proj; - if(dl > 0) - { - VECADDFAC(nearest, nearest, KDOP_AXES[i], dl); -} - else if(du < 0) - { - VECADDFAC(nearest, nearest, KDOP_AXES[i], du); -} -} + if(dl > 0) + { + VECADDFAC(nearest, nearest, KDOP_AXES[i], dl); + } + else if(du < 0) + { + VECADDFAC(nearest, nearest, KDOP_AXES[i], du); + } + } */ return squared_dist(data->co, nearest); } @@ -1193,6 +1146,7 @@ int BLI_bvhtree_find_nearest(BVHTree *tree, const float *co, BVHTreeNearest *nea int i; BVHNearestData data; + BVHNode* root = tree->nodes[tree->totleaf]; //init data to search data.tree = tree; @@ -1217,7 +1171,8 @@ int BLI_bvhtree_find_nearest(BVHTree *tree, const float *co, BVHTreeNearest *nea } //dfs search - dfs_find_nearest(&data, tree->nodes[tree->totleaf] ); + if(root) + dfs_find_nearest(&data, root); //copy back results if(nearest) @@ -1229,11 +1184,13 @@ int BLI_bvhtree_find_nearest(BVHTree *tree, const float *co, BVHTreeNearest *nea } - /* - * Ray cast + * Raycast - BLI_bvhtree_ray_cast + * + * raycast is done by performing a DFS on the BVHTree and saving the closest hit */ +//Determines the distance that the ray must travel to hit the bounding volume of the given node static float ray_nearest_hit(BVHRayCastData *data, BVHNode *node) { int i; @@ -1247,7 +1204,7 @@ static float ray_nearest_hit(BVHRayCastData *data, BVHNode *node) { //axis aligned ray if(data->ray.origin[i] < bv[0] - || data->ray.origin[i] > bv[1]) + || data->ray.origin[i] > bv[1]) return FLT_MAX; } else @@ -1312,12 +1269,11 @@ static void dfs_raycast(BVHRayCastData *data, BVHNode *node) } } - - int BLI_bvhtree_ray_cast(BVHTree *tree, const float *co, const float *dir, BVHTreeRayHit *hit, BVHTree_RayCastCallback callback, void *userdata) { int i; BVHRayCastData data; + BVHNode * root = tree->nodes[tree->totleaf]; data.tree = tree; @@ -1346,7 +1302,8 @@ int BLI_bvhtree_ray_cast(BVHTree *tree, const float *co, const float *dir, BVHTr data.hit.dist = FLT_MAX; } - dfs_raycast(&data, tree->nodes[tree->totleaf]); + if(root) + dfs_raycast(&data, root); if(hit) diff --git a/source/blender/include/BIF_editarmature.h b/source/blender/include/BIF_editarmature.h index 13c16749612..07fc8f08b4a 100644 --- a/source/blender/include/BIF_editarmature.h +++ b/source/blender/include/BIF_editarmature.h @@ -114,6 +114,7 @@ void setflag_armature(short mode); void unique_editbone_name (struct ListBase *ebones, char *name); void auto_align_armature(short mode); +void switch_direction_armature(void); void create_vgroups_from_armature(struct Object *ob, struct Object *par); void add_verts_to_dgroups(struct Object *ob, struct Object *par, int heat, int mirror); @@ -135,7 +136,6 @@ void transform_armature_mirror_update(void); void hide_selected_armature_bones(void); void hide_unselected_armature_bones(void); void show_all_armature_bones(void); -void set_locks_armature_bones(short lock); #define BONESEL_ROOT 0x10000000 #define BONESEL_TIP 0x20000000 @@ -144,6 +144,10 @@ void set_locks_armature_bones(short lock); #define BONESEL_NOSEL 0x80000000 /* Indicates a negative number */ +/* useful macros */ +#define EBONE_VISIBLE(arm, ebone) ((arm->layer & ebone->layer) && !(ebone->flag & BONE_HIDDEN_A)) +#define EBONE_EDITABLE(ebone) ((ebone->flag & BONE_SELECTED) && !(ebone->flag & BONE_EDITMODE_LOCKED)) + /* used in bone_select_hierachy() */ #define BONE_SELECT_PARENT 0 #define BONE_SELECT_CHILD 1 diff --git a/source/blender/python/api2_2x/Texture.c b/source/blender/python/api2_2x/Texture.c index 696c78f2bac..7ba8ad88ea6 100644 --- a/source/blender/python/api2_2x/Texture.c +++ b/source/blender/python/api2_2x/Texture.c @@ -105,6 +105,10 @@ #define EXPP_TEX_LACUNARITY_MAX 6.0f #define EXPP_TEX_OCTS_MIN 0.0f #define EXPP_TEX_OCTS_MAX 8.0f +#define EXPP_TEX_OFST_MIN 0.0f +#define EXPP_TEX_OFST_MAX 6.0f +#define EXPP_TEX_GAIN_MIN 0.0f +#define EXPP_TEX_GAIN_MAX 6.0f #define EXPP_TEX_ISCALE_MIN 0.0f #define EXPP_TEX_ISCALE_MAX 10.0f #define EXPP_TEX_EXP_MIN 0.010f @@ -430,6 +434,8 @@ GETFUNC( getNoiseDepth ); GETFUNC( getNoiseSize ); GETFUNC( getNoiseType ); GETFUNC( getOcts ); +GETFUNC( getOffset ); +GETFUNC( getGain ); GETFUNC( getRepeat ); GETFUNC( getRGBCol ); GETFUNC( getSType ); @@ -478,6 +484,8 @@ SETFUNC( setNoiseDepth ); SETFUNC( setNoiseSize ); SETFUNC( setNoiseType ); SETFUNC( setOcts ); +SETFUNC( setOffset ); +SETFUNC( setGain ); SETFUNC( setRepeat ); SETFUNC( setRGBCol ); SETFUNC( setSType ); @@ -646,6 +654,14 @@ static PyGetSetDef BPy_Texture_getseters[] = { (getter)Texture_getLacunarity, (setter)Texture_setLacunarity, "Gap between succesive frequencies (for Musgrave textures)", NULL}, + {"offset", + (getter)Texture_getOffset, (setter)Texture_setOffset, + "Fractal offset (for Musgrave textures)", + NULL}, + {"gain", + (getter)Texture_getGain, (setter)Texture_setGain, + "Gain multiplier (for Musgrave textures)", + NULL}, {"noiseBasis", (getter)Texture_getNoiseBasis, (setter)Texture_setNoiseBasis, "Noise basis type (wood, stucci, marble, clouds, Musgrave, distorted noise)", @@ -1837,6 +1853,20 @@ static int Texture_setLacunarity( BPy_Texture * self, PyObject * value ) EXPP_TEX_LACUNARITY_MAX ); } +static int Texture_setOffset( BPy_Texture * self, PyObject * value ) +{ + return EXPP_setFloatClamped ( value, &self->texture->mg_offset, + EXPP_TEX_OFST_MIN, + EXPP_TEX_OFST_MAX ); +} + +static int Texture_setGain( BPy_Texture * self, PyObject * value ) +{ + return EXPP_setFloatClamped ( value, &self->texture->mg_gain, + EXPP_TEX_GAIN_MIN, + EXPP_TEX_GAIN_MAX ); +} + static int Texture_setOcts( BPy_Texture * self, PyObject * value ) { return EXPP_setFloatClamped ( value, &self->texture->mg_octaves, @@ -2168,6 +2198,16 @@ static PyObject *Texture_getOcts( BPy_Texture *self ) return PyFloat_FromDouble( self->texture->mg_octaves ); } +static PyObject *Texture_getOffset( BPy_Texture *self ) +{ + return PyFloat_FromDouble( self->texture->mg_offset ); +} + +static PyObject *Texture_getGain( BPy_Texture *self ) +{ + return PyFloat_FromDouble( self->texture->mg_gain ); +} + static PyObject *Texture_getRepeat( BPy_Texture *self ) { return Py_BuildValue( "(i,i)", self->texture->xrepeat, diff --git a/source/blender/python/api2_2x/doc/Render.py b/source/blender/python/api2_2x/doc/Render.py index df688893aa3..5300fdab808 100644 --- a/source/blender/python/api2_2x/doc/Render.py +++ b/source/blender/python/api2_2x/doc/Render.py @@ -170,17 +170,6 @@ def SetRenderWinPos(locationList): the location of the Render window on the screen. """ -def EnableEdgeShift(): - """ - Globally with the unified renderer enabled the outlines of the render - are shifted a bit. - """ - -def EnableEdgeAll(): - """ - Globally consider transparent faces for edge-rendering with the unified renderer. - """ - class RenderData: """ The RenderData object diff --git a/source/blender/python/api2_2x/doc/Texture.py b/source/blender/python/api2_2x/doc/Texture.py index 7f42d06240b..cebb7de7011 100644 --- a/source/blender/python/api2_2x/doc/Texture.py +++ b/source/blender/python/api2_2x/doc/Texture.py @@ -344,6 +344,12 @@ class Texture: @ivar octs: Number of frequencies (for Musgrave textures). Value is clamped to the range [0.0,8.0]. @type octs: float + @ivar offset: Fractal offset (for hetero terrain and multifractal Musgrave textures). + Value is clamped to the range [0.0,6.0]. + @type offset: float + @ivar gain: Gain multiplier (for multifractal Musgrave textures). + Value is clamped to the range [0.0,6.0]. + @type gain: float @ivar repeat: Repetition multiplier (for image textures). @type repeat: tuple of 2 ints @ivar rgbCol: RGB color tuple. diff --git a/source/blender/render/intern/source/rayshade.c b/source/blender/render/intern/source/rayshade.c index e108f2cdca0..0fd9365477c 100644 --- a/source/blender/render/intern/source/rayshade.c +++ b/source/blender/render/intern/source/rayshade.c @@ -263,7 +263,12 @@ static void shade_ray(Isect *is, ShadeInput *shi, ShadeResult *shr) shade_input_set_shade_texco(shi); if(is->mode==RE_RAY_SHADOW_TRA) - shade_color(shi, shr); + if(shi->mat->nodetree && shi->mat->use_nodes) { + ntreeShaderExecTree(shi->mat->nodetree, shi, shr); + shi->mat= vlr->mat; /* shi->mat is being set in nodetree */ + } + else + shade_color(shi, shr); else { if(shi->mat->nodetree && shi->mat->use_nodes) { ntreeShaderExecTree(shi->mat->nodetree, shi, shr); diff --git a/source/blender/src/editarmature.c b/source/blender/src/editarmature.c index 80c24f3a989..35986fcff4a 100644 --- a/source/blender/src/editarmature.c +++ b/source/blender/src/editarmature.c @@ -854,6 +854,7 @@ static void separate_armature_bones (Object *ob, short sel) BLI_freelistN(&edbo); } +/* separate selected bones into their armature */ void separate_armature (void) { Object *oldob, *newob; @@ -1094,13 +1095,13 @@ void armature_select_hierarchy(short direction, short add_to_sel) arm= (bArmature *)ob->data; for (curbone= G.edbo.first; curbone; curbone= curbone->next) { - if (arm->layer & curbone->layer) { + if (EBONE_VISIBLE(arm, curbone)) { if (curbone->flag & (BONE_ACTIVE)) { if (direction == BONE_SELECT_PARENT) { if (curbone->parent == NULL) continue; else pabone = curbone->parent; - if ((arm->layer & pabone->layer) && !(pabone->flag & BONE_HIDDEN_A)) { + if (EBONE_VISIBLE(arm, pabone)) { pabone->flag |= (BONE_ACTIVE|BONE_SELECTED|BONE_TIPSEL|BONE_ROOTSEL); if (pabone->parent) pabone->parent->flag |= BONE_TIPSEL; @@ -1109,11 +1110,12 @@ void armature_select_hierarchy(short direction, short add_to_sel) break; } - } else { // BONE_SELECT_CHILD + } + else { // BONE_SELECT_CHILD chbone = editbone_get_child(curbone, 1); if (chbone == NULL) continue; - if ((arm->layer & chbone->layer) && !(chbone->flag & BONE_HIDDEN_A)) { + if (EBONE_VISIBLE(arm, chbone)) { chbone->flag |= (BONE_ACTIVE|BONE_SELECTED|BONE_TIPSEL|BONE_ROOTSEL); if (!add_to_sel) { @@ -1159,17 +1161,18 @@ void setflag_armature (short mode) /* get flag to set (sync these with the ones used in eBone_Flag */ if (mode == 2) - flag= pupmenu("Disable Setting%t|Draw Wire%x1|Deform%x2|Mult VG%x3|Hinge%x4|No Scale%x5"); + flag= pupmenu("Disable Setting%t|Draw Wire%x1|Deform%x2|Mult VG%x3|Hinge%x4|No Scale%x5|Locked%x6"); else if (mode == 1) - flag= pupmenu("Enable Setting%t|Draw Wire%x1|Deform%x2|Mult VG%x3|Hinge%x4|No Scale%x5"); + flag= pupmenu("Enable Setting%t|Draw Wire%x1|Deform%x2|Mult VG%x3|Hinge%x4|No Scale%x5|Locked%x6"); else - flag= pupmenu("Toggle Setting%t|Draw Wire%x1|Deform%x2|Mult VG%x3|Hinge%x4|No Scale%x5"); + flag= pupmenu("Toggle Setting%t|Draw Wire%x1|Deform%x2|Mult VG%x3|Hinge%x4|No Scale%x5|Locked%x6"); switch (flag) { case 1: flag = BONE_DRAWWIRE; break; case 2: flag = BONE_NO_DEFORM; break; case 3: flag = BONE_MULT_VG_ENV; break; case 4: flag = BONE_HINGE; break; case 5: flag = BONE_NO_SCALE; break; + case 6: flag = BONE_EDITMODE_LOCKED; break; default: return; } @@ -1725,12 +1728,12 @@ void auto_align_armature(short mode) float *cursor= give_cursor(); for (ebone = G.edbo.first; ebone; ebone=ebone->next) { - if (arm->layer & ebone->layer) { + if (EBONE_VISIBLE(arm, ebone)) { if (arm->flag & ARM_MIRROR_EDIT) flipbone = armature_bone_get_mirrored(ebone); if ((ebone->flag & BONE_SELECTED) || - (flipbone && flipbone->flag & BONE_SELECTED)) + (flipbone && (flipbone->flag & BONE_SELECTED))) { /* specific method used to calculate roll depends on mode */ if (mode == 1) { @@ -1975,7 +1978,7 @@ void addvert_armature(void) /* find the active or selected bone */ for (ebone = G.edbo.first; ebone; ebone=ebone->next) { - if (arm->layer & ebone->layer) { + if (EBONE_VISIBLE(arm, ebone)) { if (ebone->flag & (BONE_ACTIVE|BONE_TIPSEL)) break; } @@ -1983,7 +1986,7 @@ void addvert_armature(void) if (ebone==NULL) { for (ebone = G.edbo.first; ebone; ebone=ebone->next) { - if (arm->layer & ebone->layer) { + if (EBONE_VISIBLE(arm, ebone)) { if (ebone->flag & (BONE_ACTIVE|BONE_ROOTSEL)) break; } @@ -2066,11 +2069,12 @@ static EditBone *get_named_editbone(char *name) { EditBone *eBone; - if (name) + if (name) { for (eBone=G.edbo.first; eBone; eBone=eBone->next) { if (!strcmp(name, eBone->name)) return eBone; } + } return NULL; } @@ -2136,7 +2140,7 @@ void adduplicate_armature(void) /* Select mirrored bones */ if (arm->flag & ARM_MIRROR_EDIT) { for (curBone=G.edbo.first; curBone; curBone=curBone->next) { - if (arm->layer & curBone->layer) { + if (EBONE_VISIBLE(arm, curBone)) { if (curBone->flag & BONE_SELECTED) { eBone = armature_bone_get_mirrored(curBone); if (eBone) @@ -2148,13 +2152,13 @@ void adduplicate_armature(void) /* Find the selected bones and duplicate them as needed */ for (curBone=G.edbo.first; curBone && curBone!=firstDup; curBone=curBone->next) { - if (arm->layer & curBone->layer) { + if (EBONE_VISIBLE(arm, curBone)) { if (curBone->flag & BONE_SELECTED) { eBone=MEM_callocN(sizeof(EditBone), "addup_editbone"); eBone->flag |= BONE_SELECTED; /* Copy data from old bone to new bone */ - memcpy (eBone, curBone, sizeof(EditBone)); + memcpy(eBone, curBone, sizeof(EditBone)); curBone->temp = eBone; eBone->temp = curBone; @@ -2204,7 +2208,7 @@ void adduplicate_armature(void) /* Run though the list and fix the pointers */ for (curBone=G.edbo.first; curBone && curBone!=firstDup; curBone=curBone->next) { - if (arm->layer & curBone->layer) { + if (EBONE_VISIBLE(arm, curBone)) { if (curBone->flag & BONE_SELECTED) { eBone=(EditBone*) curBone->temp; @@ -2236,7 +2240,7 @@ void adduplicate_armature(void) /* Deselect the old bones and select the new ones */ for (curBone=G.edbo.first; curBone && curBone!=firstDup; curBone=curBone->next) { - if (arm->layer & curBone->layer) + if (EBONE_VISIBLE(arm, curBone)) curBone->flag &= ~(BONE_SELECTED | BONE_TIPSEL | BONE_ROOTSEL | BONE_ACTIVE); } @@ -2373,7 +2377,7 @@ void fill_bones_armature(void) /* loop over all bones, and only consider if visible */ for (ebo= G.edbo.first; ebo; ebo= ebo->next) { - if ((arm->layer & ebo->layer) && !(ebo->flag & BONE_HIDDEN_A)) { + if (EBONE_VISIBLE(arm, ebo)) { if (!(ebo->flag & BONE_CONNECTED) && (ebo->flag & BONE_ROOTSEL)) fill_add_joint(ebo, 0, &points); if (ebo->flag & BONE_TIPSEL) @@ -2608,7 +2612,7 @@ void merge_armature(void) /* only consider bones that are visible and selected */ for (ebo=chain->data; ebo; child=ebo, ebo=ebo->parent) { /* check if visible + selected */ - if ( (arm->layer & ebo->layer) && !(ebo->flag & BONE_HIDDEN_A) && + if ( EBONE_VISIBLE(arm, ebo) && ((ebo->flag & BONE_CONNECTED) || (ebo->parent==NULL)) && (ebo->flag & (BONE_SELECTED|BONE_ACTIVE)) ) { @@ -2659,7 +2663,7 @@ void hide_selected_armature_bones(void) EditBone *ebone; for (ebone = G.edbo.first; ebone; ebone=ebone->next) { - if (arm->layer & ebone->layer) { + if (EBONE_VISIBLE(arm, ebone)) { if (ebone->flag & (BONE_SELECTED)) { ebone->flag &= ~(BONE_TIPSEL|BONE_SELECTED|BONE_ROOTSEL|BONE_ACTIVE); ebone->flag |= BONE_HIDDEN_A; @@ -2678,7 +2682,7 @@ void hide_unselected_armature_bones(void) for (ebone = G.edbo.first; ebone; ebone=ebone->next) { bArmature *arm= G.obedit->data; - if (arm->layer & ebone->layer) { + if (EBONE_VISIBLE(arm, ebone)) { if (ebone->flag & (BONE_TIPSEL|BONE_SELECTED|BONE_ROOTSEL)); else { ebone->flag &= ~BONE_ACTIVE; @@ -2711,32 +2715,6 @@ void show_all_armature_bones(void) BIF_undo_push("Reveal Bones"); } -/* Sets editmode transform locks for bones (adds if lock==1, clears otherwise) */ -void set_locks_armature_bones(short lock) -{ - bArmature *arm= G.obedit->data; - EditBone *ebone; - - for (ebone = G.edbo.first; ebone; ebone=ebone->next) { - if (arm->layer & ebone->layer) { - if (ebone->flag & BONE_SELECTED) { - if (lock) - ebone->flag |= BONE_EDITMODE_LOCKED; - else - ebone->flag &= ~BONE_EDITMODE_LOCKED; - } - } - } - countall(); - allqueue(REDRAWVIEW3D, 0); - allqueue(REDRAWBUTSEDIT, 0); - - if (lock) - BIF_undo_push("Lock Bones"); - else - BIF_undo_push("Unlock Bones"); -} - /* check for null, before calling! */ static void bone_connect_to_existing_parent(EditBone *bone) { @@ -2803,7 +2781,7 @@ void make_bone_parent(void) /* find active bone to parent to */ for (actbone = G.edbo.first; actbone; actbone=actbone->next) { - if (arm->layer & actbone->layer) { + if (EBONE_VISIBLE(arm, actbone)) { if (actbone->flag & BONE_ACTIVE) break; } @@ -2815,7 +2793,7 @@ void make_bone_parent(void) /* find selected bones */ for (ebone = G.edbo.first; ebone; ebone=ebone->next) { - if (arm->layer & ebone->layer) { + if (EBONE_VISIBLE(arm, ebone)) { if ((ebone->flag & BONE_SELECTED) && (ebone != actbone)) { foundselbone++; if (ebone->parent != actbone) allchildbones= 1; @@ -2851,7 +2829,7 @@ void make_bone_parent(void) else { /* loop through all editbones, parenting all selected bones to the active bone */ for (selbone = G.edbo.first; selbone; selbone=selbone->next) { - if (arm->layer & selbone->layer) { + if (EBONE_VISIBLE(arm, selbone)) { if ((selbone->flag & BONE_SELECTED) && (selbone!=actbone)) { /* parent selbone to actbone */ bone_connect_to_new_parent(selbone, actbone, val); @@ -2909,7 +2887,7 @@ void clear_bone_parent(void) if (val<1) return; for (ebone = G.edbo.first; ebone; ebone=ebone->next) { - if (arm->layer & ebone->layer) { + if (EBONE_VISIBLE(arm, ebone)) { if (ebone->flag & BONE_SELECTED) { if (arm->flag & ARM_MIRROR_EDIT) flipbone = armature_bone_get_mirrored(ebone); @@ -2959,7 +2937,7 @@ void unique_editbone_name (ListBase *ebones, char *name) } for (number = 1; number <=999; number++) { - sprintf (tempname, "%s.%03d", name, number); + sprintf(tempname, "%s.%03d", name, number); if (!editbone_name_exists(ebones, tempname)) { BLI_strncpy(name, tempname, 32); return; @@ -2980,7 +2958,7 @@ void extrude_armature(int forked) /* since we allow root extrude too, we have to make sure selection is OK */ for (ebone = G.edbo.first; ebone; ebone=ebone->next) { - if (arm->layer & ebone->layer) { + if (EBONE_VISIBLE(arm, ebone)) { if (ebone->flag & BONE_ROOTSEL) { if (ebone->parent && (ebone->flag & BONE_CONNECTED)) { if (ebone->parent->flag & BONE_TIPSEL) @@ -2992,7 +2970,7 @@ void extrude_armature(int forked) /* Duplicate the necessary bones */ for (ebone = G.edbo.first; ((ebone) && (ebone!=first)); ebone=ebone->next) { - if (arm->layer & ebone->layer) { + if (EBONE_VISIBLE(arm, ebone)) { /* we extrude per definition the tip */ do_extrude= 0; if (ebone->flag & (BONE_TIPSEL|BONE_SELECTED)) @@ -3006,7 +2984,7 @@ void extrude_armature(int forked) if (do_extrude) { /* we re-use code for mirror editing... */ flipbone= NULL; - if(arm->flag & ARM_MIRROR_EDIT) { + if (arm->flag & ARM_MIRROR_EDIT) { flipbone= armature_bone_get_mirrored(ebone); if (flipbone) { forked= 0; // we extrude 2 different bones @@ -3036,7 +3014,7 @@ void extrude_armature(int forked) newbone->parent = ebone; newbone->flag = ebone->flag & BONE_TIPSEL; // copies it, in case mirrored bone - + if (newbone->parent) newbone->flag |= BONE_CONNECTED; } else { @@ -3046,7 +3024,7 @@ void extrude_armature(int forked) newbone->flag= BONE_TIPSEL; - if (newbone->parent && ebone->flag & BONE_CONNECTED) { + if (newbone->parent && (ebone->flag & BONE_CONNECTED)) { newbone->flag |= BONE_CONNECTED; } } @@ -3065,8 +3043,8 @@ void extrude_armature(int forked) BLI_strncpy (newbone->name, ebone->name, 32); if (flipbone && forked) { // only set if mirror edit - if(strlen(newbone->name)<30) { - if(a==0) strcat(newbone->name, "_L"); + if (strlen(newbone->name)<30) { + if (a==0) strcat(newbone->name, "_L"); else strcat(newbone->name, "_R"); } } @@ -3111,7 +3089,7 @@ void subdivide_armature(int numcuts) if (numcuts < 1) return; for (mbone = G.edbo.last; mbone; mbone= mbone->prev) { - if (arm->layer & mbone->layer) { + if (EBONE_VISIBLE(arm, mbone)) { if (mbone->flag & BONE_SELECTED) { for (i=numcuts+1; i>1; i--) { /* compute cut ratio first */ @@ -3176,6 +3154,59 @@ void subdivide_armature(int numcuts) else BIF_undo_push("Subdivide multi"); } +/* switch direction of bone chains */ +void switch_direction_armature (void) +{ + bArmature *arm= (G.obedit) ? G.obedit->data : NULL; + ListBase chains = {NULL, NULL}; + LinkData *chain; + + /* error checking paranoia */ + if (arm == NULL) + return; + + /* get chains of bones (ends on chains) */ + chains_find_tips(&chains); + if (chains.first == NULL) return; + + /* loop over chains, only considering selected and visible bones */ + for (chain= chains.first; chain; chain= chain->next) { + EditBone *ebo, *child=NULL, *parent=NULL; + + /* loop over bones in chain */ + for (ebo= chain->data; ebo; child= ebo, ebo=parent) { + parent= ebo->parent; + + /* only if selected and editable */ + if (EBONE_VISIBLE(arm, ebo) && EBONE_EDITABLE(ebo)) { + /* swap head and tail coordinates */ + SWAP(float, ebo->head[0], ebo->tail[0]); + SWAP(float, ebo->head[1], ebo->tail[1]); + SWAP(float, ebo->head[2], ebo->tail[2]); + + /* do parent swapping: + * - use 'child' as new parent + * - connected flag is only set if points are coincidental + */ + ebo->parent= child; + if ((child) && VecEqual(ebo->head, child->tail)) + ebo->flag |= BONE_CONNECTED; + else + ebo->flag &= ~BONE_CONNECTED; + + /* FIXME: other things that need fixing? + * i.e. roll? + */ + } + } + } + + /* free chains */ + BLI_freelistN(&chains); + + BIF_undo_push("Switch Direction"); +} + /* ***************** Pose tools ********************* */ void clear_armature(Object *ob, char mode) diff --git a/source/blender/src/editobject.c b/source/blender/src/editobject.c index cbf7691754d..fee967bcd9a 100644 --- a/source/blender/src/editobject.c +++ b/source/blender/src/editobject.c @@ -2760,7 +2760,7 @@ void special_editmenu(void) DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA); } else if(G.obedit->type==OB_ARMATURE) { - nr= pupmenu("Specials%t|Subdivide %x1|Subdivide Multi%x2|Flip Left-Right Names%x3|%l|AutoName Left-Right%x4|AutoName Front-Back%x5|AutoName Top-Bottom%x6|%l|Lock%x7|Unlock%x8"); + nr= pupmenu("Specials%t|Subdivide %x1|Subdivide Multi%x2|Switch Direction%x7|Flip Left-Right Names%x3|%l|AutoName Left-Right%x4|AutoName Front-Back%x5|AutoName Top-Bottom%x6"); if(nr==1) subdivide_armature(1); if(nr==2) { @@ -2773,10 +2773,8 @@ void special_editmenu(void) else if(ELEM3(nr, 4, 5, 6)) { armature_autoside_names(nr-4); } - else if(nr==7) - set_locks_armature_bones(1); - else if(nr==8) - set_locks_armature_bones(0); + else if(nr == 7) + switch_direction_armature(); } else if(G.obedit->type==OB_LATTICE) { static float weight= 1.0f; diff --git a/source/blender/src/poselib.c b/source/blender/src/poselib.c index fb2bfe5b605..6aeef7c75c2 100644 --- a/source/blender/src/poselib.c +++ b/source/blender/src/poselib.c @@ -312,7 +312,7 @@ void poselib_add_current_pose (Object *ob, int val) /* mode - add new or replace existing */ if (val == 0) { if ((ob->poselib) && (ob->poselib->markers.first)) { - val= pupmenu("PoseLib Add Current Pose%t|Add New%x1|Replace Existing%x2"); + val= pupmenu("PoseLib Add Current Pose%t|Add New%x1|Add New (Current Frame)%x3|Replace Existing%x2"); if (val <= 0) return; } else @@ -347,7 +347,10 @@ void poselib_add_current_pose (Object *ob, int val) act= poselib_validate(ob); /* get frame */ - frame= poselib_get_free_index(act); + if (val == 3) + frame= CFRA; + else /* if (val == 1) */ + frame= poselib_get_free_index(act); /* add pose to poselib - replaces any existing pose there */ for (marker= act->markers.first; marker; marker= marker->next) { diff --git a/source/blender/src/transform_conversions.c b/source/blender/src/transform_conversions.c index 83ce7ef991a..459048df1d0 100644 --- a/source/blender/src/transform_conversions.c +++ b/source/blender/src/transform_conversions.c @@ -913,8 +913,8 @@ static void createTransPose(TransInfo *t, Object *ob) if (arm==NULL || ob->pose==NULL) return; if (arm->flag & ARM_RESTPOS) { - if(t->mode!=TFM_BONESIZE) { - notice ("Pose edit not possible while Rest Position is enabled"); + if(ELEM(t->mode, TFM_DUMMY, TFM_BONESIZE)==0) { + notice("Pose edit not possible while Rest Position is enabled"); return; } } diff --git a/source/gameengine/Converter/BL_BlenderDataConversion.cpp b/source/gameengine/Converter/BL_BlenderDataConversion.cpp index b99ba0c3b89..cbf3b595dfd 100644 --- a/source/gameengine/Converter/BL_BlenderDataConversion.cpp +++ b/source/gameengine/Converter/BL_BlenderDataConversion.cpp @@ -1937,7 +1937,17 @@ void BL_ConvertBlenderObjects(struct Main* maggie, float* fl = (float*) blenderobject->parentinv; MT_Transform parinvtrans(fl); parentinversenode->SetLocalPosition(parinvtrans.getOrigin()); - parentinversenode->SetLocalOrientation(parinvtrans.getBasis()); + // problem here: the parent inverse transform combines scaling and rotation + // in the basis but the scenegraph needs separate rotation and scaling. + // This is not important for OpenGL (it uses 4x4 matrix) but it is important + // for the physic engine that needs a separate scaling + //parentinversenode->SetLocalOrientation(parinvtrans.getBasis()); + + // Extract the rotation and the scaling from the basis + MT_Matrix3x3 inverseOrientation(parinvtrans.getRotation()); + parentinversenode->SetLocalOrientation(inverseOrientation); + MT_Matrix3x3 scale(inverseOrientation.transposed()*parinvtrans.getBasis()); + parentinversenode->SetLocalScale(MT_Vector3(scale[0][0], scale[1][1], scale[2][2])); parentinversenode->AddChild(gameobj->GetSGNode()); } diff --git a/source/gameengine/Expressions/Value.cpp b/source/gameengine/Expressions/Value.cpp index 48136eb9dc3..b4694740679 100644 --- a/source/gameengine/Expressions/Value.cpp +++ b/source/gameengine/Expressions/Value.cpp @@ -700,9 +700,7 @@ CValue* CValue::ConvertPythonToValue(PyObject* pyobj) CValue* vallie = NULL; - PyTypeObject* type = pyobj->ob_type; - - if (type == &PyList_Type) + if (PyList_Check(pyobj)) { CListValue* listval = new CListValue(); bool error = false; @@ -732,26 +730,25 @@ CValue* CValue::ConvertPythonToValue(PyObject* pyobj) } } else - if (type == &PyFloat_Type) + if (PyFloat_Check(pyobj)) { - float fl; - PyArg_Parse(pyobj,"f",&fl); - vallie = new CFloatValue(fl); + vallie = new CFloatValue( (float)PyFloat_AsDouble(pyobj) ); } else - if (type==&PyInt_Type) + if (PyInt_Check(pyobj)) { - int innie; - PyArg_Parse(pyobj,"i",&innie); - vallie = new CIntValue(innie); + vallie = new CIntValue( (int)PyInt_AS_LONG(pyobj) ); } else - - if (type==&PyString_Type) + if (PyString_Check(pyobj)) { vallie = new CStringValue(PyString_AsString(pyobj),""); } else - if (type==&CValue::Type || type==&CListValue::Type) + if (pyobj->ob_type==&CValue::Type || pyobj->ob_type==&CListValue::Type) { vallie = ((CValue*) pyobj)->AddRef(); + } else + { + /* return an error value from the caller */ + PyErr_SetString(PyExc_TypeError, "This python value could not be assigned to a game engine property"); } return vallie; @@ -778,6 +775,9 @@ int CValue::_setattr(const STR_String& attr,PyObject* pyobj) SetProperty(attr,vallie); } vallie->Release(); + } else + { + return 1; /* ConvertPythonToValue sets the error message */ } //PyObjectPlus::_setattr(attr,value); |