Merging r42621 through r42647 from trunk into soc-2011-tomato
authorSergey Sharybin <sergey.vfx@gmail.com>
Thu, 15 Dec 2011 16:04:59 +0000 (16:04 +0000)
committerSergey Sharybin <sergey.vfx@gmail.com>
Thu, 15 Dec 2011 16:04:59 +0000 (16:04 +0000)
1  2 
source/blender/blenkernel/intern/constraint.c
source/blender/blenloader/intern/readfile.c

index 9d8aae3bb52b54f1aa955421338ca7c71a554766,87a50b895477f9d70f6b99df22b9a02efe8661d3..ba1bfc8f3030387830796ac1ff8dda07a5986a60
@@@ -436,16 -436,15 +436,15 @@@ static void contarget_get_mesh_mat (Obj
        float vec[3] = {0.0f, 0.0f, 0.0f};
        float normal[3] = {0.0f, 0.0f, 0.0f}, plane[3];
        float imat[3][3], tmat[3][3];
-       int dgroup;
+       const int defgroup= defgroup_name_index(ob, substring);
        short freeDM = 0;
        
        /* initialize target matrix using target matrix */
        copy_m4_m4(mat, ob->obmat);
        
        /* get index of vertex group */
-       dgroup = defgroup_name_index(ob, substring);
-       if (dgroup < 0) return;
-       
+       if (defgroup == -1) return;
        /* get DerivedMesh */
        if (em) {
                /* target is in editmode, so get a special derived mesh */
        if (dm) {
                MDeformVert *dvert = dm->getVertDataArray(dm, CD_MDEFORMVERT);
                int numVerts = dm->getNumVerts(dm);
-               int i, j, count = 0;
+               int i, count = 0;
                float co[3], nor[3];
                
                /* check that dvert is a valid pointers (just in case) */
                if (dvert) {
+                       MDeformVert *dv= dvert;
                        /* get the average of all verts with that are in the vertex-group */
-                       for (i = 0; i < numVerts; i++) {        
-                               for (j = 0; j < dvert[i].totweight; j++) {
-                                       /* does this vertex belong to nominated vertex group? */
-                                       if (dvert[i].dw[j].def_nr == dgroup) {
-                                               dm->getVertCo(dm, i, co);
-                                               dm->getVertNo(dm, i, nor);
-                                               add_v3_v3(vec, co);
-                                               add_v3_v3(normal, nor);
-                                               count++;
-                                               break;
-                                       }
+                       for (i = 0; i < numVerts; i++, dv++) {
+                               MDeformWeight *dw= defvert_find_index(dv, defgroup);
+                               if (dw && dw->weight != 0.0f) {
+                                       dm->getVertCo(dm, i, co);
+                                       dm->getVertNo(dm, i, nor);
+                                       add_v3_v3(vec, co);
+                                       add_v3_v3(normal, nor);
+                                       count++;
                                        
                                }
                        }
-                       
-                       
                        /* calculate averages of normal and coordinates */
                        if (count > 0) {
                                mul_v3_fl(vec, 1.0f / count);
@@@ -535,43 -531,38 +531,38 @@@ static void contarget_get_lattice_mat (
        float *co = dl?dl->verts:NULL;
        BPoint *bp = lt->def;
        
-       MDeformVert *dvert = lt->dvert;
+       MDeformVert *dv = lt->dvert;
        int tot_verts= lt->pntsu*lt->pntsv*lt->pntsw;
        float vec[3]= {0.0f, 0.0f, 0.0f}, tvec[3];
-       int dgroup=0, grouped=0;
+       int grouped=0;
        int i, n;
+       const int defgroup= defgroup_name_index(ob, substring);
        
        /* initialize target matrix using target matrix */
        copy_m4_m4(mat, ob->obmat);
-       
        /* get index of vertex group */
-       dgroup = defgroup_name_index(ob, substring);
-       if (dgroup < 0) return;
-       if (dvert == NULL) return;
+       if (defgroup == -1) return;
+       if (dv == NULL) return;
        
        /* 1. Loop through control-points checking if in nominated vertex-group.
         * 2. If it is, add it to vec to find the average point.
         */
-       for (i=0; i < tot_verts; i++, dvert++) {
-               for (n= 0; n < dvert->totweight; n++) {
-                       /* found match - vert is in vgroup */
-                       if (dvert->dw[n].def_nr == dgroup) {
+       for (i=0; i < tot_verts; i++, dv++) {
+               for (n= 0; n < dv->totweight; n++) {
+                       MDeformWeight *dw= defvert_find_index(dv, defgroup);
+                       if (dw && dw->weight > 0.0f) {
                                /* copy coordinates of point to temporary vector, then add to find average */
-                               if (co)
-                                       memcpy(tvec, co, 3*sizeof(float));
-                               else
-                                       memcpy(tvec, bp->vec, 3*sizeof(float));
-                                       
+                               memcpy(tvec, co ? co : bp->vec, 3 * sizeof(float));
                                add_v3_v3(vec, tvec);
                                grouped++;
-                               
-                               break;
                        }
                }
                
                /* advance pointer to coordinate data */
-               if (co) co+= 3;
-               else bp++;
+               if (co) co += 3;
+               else    bp++;
        }
        
        /* find average location, then multiply by ob->obmat to find world-space location */
@@@ -1106,10 -1097,10 +1097,10 @@@ static void kinematic_new_data (void *c
  {
        bKinematicConstraint *data= (bKinematicConstraint *)cdata;
        
-       data->weight= (float)1.0;
-       data->orientweight= (float)1.0;
+       data->weight= 1.0f;
+       data->orientweight= 1.0f;
        data->iterations = 500;
-       data->dist= (float)1.0;
+       data->dist= 1.0f;
        data->flag= CONSTRAINT_IK_TIP|CONSTRAINT_IK_STRETCH|CONSTRAINT_IK_POS;
  }
  
@@@ -3953,95 -3944,67 +3944,95 @@@ static void followtrack_evaluate (bCons
        Scene *scene= cob->scene;
        bFollowTrackConstraint *data= con->data;
        MovieClip *clip= data->clip;
 +      MovieTracking *tracking;
        MovieTrackingTrack *track;
 -      
 +      MovieTrackingObject *tracking_object;
 +
        if (data->flag & FOLLOWTRACK_ACTIVECLIP)
                clip= scene->clip;
 -      
 +
        if (!clip || !data->track[0])
                return;
 -      
 -      track= BKE_tracking_named_track(&clip->tracking, data->track);
 -      
 +
 +      tracking= &clip->tracking;
 +
 +      if(data->object[0])
 +              tracking_object= BKE_tracking_named_object(tracking, data->object);
 +      else
 +              tracking_object= BKE_tracking_get_camera_object(tracking);
 +
 +      if(!tracking_object)
 +              return;
 +
 +      track= BKE_tracking_named_track(tracking, tracking_object, data->track);
 +
        if (!track)
                return;
 -      
 +
        if (data->flag & FOLLOWTRACK_USE_3D_POSITION) {
                if (track->flag & TRACK_HAS_BUNDLE) {
 -                      float pos[3], mat[4][4], obmat[4][4];
 -                      
 -                      copy_m4_m4(obmat, cob->matrix);
 -                      
 -                      BKE_get_tracking_mat(cob->scene, NULL, mat);
 +                      MovieTracking *tracking= &clip->tracking;
 +                      float pos[3], mat[4][4];
 +
 +                      if((tracking_object->flag&TRACKING_OBJECT_CAMERA)==0) {
 +                              Object *camob= scene->camera;
 +                              float obmat[4][4], imat[4][4];
 +
 +                              if(!camob)
 +                                      return;
 +
 +                              copy_m4_m4(mat, camob->obmat);
 +
 +                              BKE_tracking_get_interpolated_camera(tracking, tracking_object, scene->r.cfra, obmat);
 +
 +                              invert_m4_m4(imat, obmat);
 +                              mul_m4_m4m4(mat, imat, mat);
 +                      }
 +                      else {
 +                              BKE_get_tracking_mat(cob->scene, NULL, mat);
 +                      }
 +
                        mul_v3_m4v3(pos, mat, track->bundle_pos);
 -                      
 -                      cob->matrix[3][0] += pos[0];
 -                      cob->matrix[3][1] += pos[1];
 -                      cob->matrix[3][2] += pos[2];
 +
 +                      cob->matrix[3][0] = pos[0];
 +                      cob->matrix[3][1] = pos[1];
 +                      cob->matrix[3][2] = pos[2];
                }
        } 
        else {
 -              Object *camob= cob->scene->camera;
 -              
 +              Object *camob= scene->camera;
 +
                if (camob) {
                        MovieClipUser user;
                        MovieTrackingMarker *marker;
                        float vec[3], disp[3], axis[3], mat[4][4];
                        float aspect= (scene->r.xsch*scene->r.xasp) / (scene->r.ysch*scene->r.yasp);
                        float len, d;
 -                      
 +
                        where_is_object_mat(scene, camob, mat);
 -                      
 +
                        /* camera axis */
                        vec[0]= 0.0f;
                        vec[1]= 0.0f;
                        vec[2]= 1.0f;
                        mul_v3_m4v3(axis, mat, vec);
 -                      
 +
                        /* distance to projection plane */
                        copy_v3_v3(vec, cob->matrix[3]);
                        sub_v3_v3(vec, mat[3]);
                        project_v3_v3v3(disp, vec, axis);
 -                      
 +
                        len= len_v3(disp);
 -                      
 +
                        if (len > FLT_EPSILON) {
                                CameraParams params;
                                float pos[2], rmat[4][4];
 -                              
 +
                                user.framenr= scene->r.cfra;
                                marker= BKE_tracking_get_marker(track, user.framenr);
 -                              
 +
                                add_v2_v2v2(pos, marker->pos, track->offset);
 -                              
 +
                                camera_params_init(&params);
                                camera_params_from_object(&params, camob);
  
                                        vec[0]= params.ortho_scale * (pos[0]-0.5f+params.shiftx);
                                        vec[1]= params.ortho_scale * (pos[1]-0.5f+params.shifty);
                                        vec[2]= -len;
 -                                      
 +
                                        if (aspect > 1.0f) vec[1] /= aspect;
                                        else vec[0] *= aspect;
 -                                      
 +
                                        mul_v3_m4v3(disp, camob->obmat, vec);
 -                                      
 +
                                        copy_m4_m4(rmat, camob->obmat);
                                        zero_v3(rmat[3]);
                                        mul_m4_m4m4(cob->matrix, rmat, cob->matrix);
 -                                      
 +
                                        copy_v3_v3(cob->matrix[3], disp);
                                }
                                else {
                                        d= (len*params.sensor_x) / (2.0f*params.lens);
 -                                      
 +
                                        vec[0]= d*(2.0f*(pos[0]+params.shiftx)-1.0f);
                                        vec[1]= d*(2.0f*(pos[1]+params.shifty)-1.0f);
                                        vec[2]= -len;
 -                                      
 +
                                        if (aspect > 1.0f) vec[1] /= aspect;
                                        else vec[0] *= aspect;
 -                                      
 +
                                        mul_v3_m4v3(disp, camob->obmat, vec);
 -                                      
 +
                                        /* apply camera rotation so Z-axis would be co-linear */
                                        copy_m4_m4(rmat, camob->obmat);
                                        zero_v3(rmat[3]);
                                        mul_m4_m4m4(cob->matrix, rmat, cob->matrix);
 -                                      
 +
                                        copy_v3_v3(cob->matrix[3], disp);
                                }
                        }
@@@ -4123,19 -4086,16 +4114,19 @@@ static void camerasolver_evaluate (bCon
        Scene *scene= cob->scene;
        bCameraSolverConstraint *data= con->data;
        MovieClip *clip= data->clip;
 -      
 +
        if (data->flag & CAMERASOLVER_ACTIVECLIP)
                clip= scene->clip;
 -      
 +
        if (clip) {
                float mat[4][4], obmat[4][4];
 -              
 -              BKE_tracking_get_interpolated_camera(&clip->tracking, scene->r.cfra, mat);
 -              
 +              MovieTracking *tracking= &clip->tracking;
 +              MovieTrackingObject *object= BKE_tracking_get_camera_object(tracking);
 +
 +              BKE_tracking_get_interpolated_camera(tracking, object, scene->r.cfra, mat);
 +
                copy_m4_m4(obmat, cob->matrix);
 +
                mul_m4_m4m4(cob->matrix, mat, obmat);
        }
  }
@@@ -4156,76 -4116,6 +4147,76 @@@ static bConstraintTypeInfo CTI_CAMERASO
        camerasolver_evaluate /* evaluate */
  };
  
 +/* ----------- Object Solver ------------- */
 +
 +static void objectsolver_new_data (void *cdata)
 +{
 +      bObjectSolverConstraint *data= (bObjectSolverConstraint *)cdata;
 +
 +      data->clip = NULL;
 +      data->flag |= OBJECTSOLVER_ACTIVECLIP;
 +}
 +
 +static void objectsolver_id_looper (bConstraint *con, ConstraintIDFunc func, void *userdata)
 +{
 +      bObjectSolverConstraint *data= con->data;
 +
 +      func(con, (ID**)&data->clip, userdata);
 +}
 +
 +static void objectsolver_evaluate (bConstraint *con, bConstraintOb *cob, ListBase *UNUSED(targets))
 +{
 +      Scene *scene= cob->scene;
 +      bObjectSolverConstraint *data= con->data;
 +      MovieClip *clip= data->clip;
 +
 +      if (data->flag & OBJECTSOLVER_ACTIVECLIP)
 +              clip= scene->clip;
 +
 +      if(!scene->camera)
 +              return;
 +
 +      if (clip) {
 +              MovieTracking *tracking= &clip->tracking;
 +              MovieTrackingObject *object;
 +
 +              object= BKE_tracking_named_object(tracking, data->object);
 +
 +              if(object) {
 +                      float mat[4][4], obmat[4][4], imat[4][4], cammat[4][4], camimat[4][4];
 +
 +                      where_is_object_mat(scene, scene->camera, cammat);
 +
 +                      BKE_tracking_get_interpolated_camera(tracking, object, scene->r.cfra, mat);
 +
 +                      invert_m4_m4(camimat, cammat);
 +
 +                      copy_m4_m4(cammat, scene->camera->obmat);
 +                      copy_m4_m4(obmat, cob->matrix);
 +
 +                      invert_m4_m4(imat, mat);
 +
 +                      mul_serie_m4(cob->matrix, cammat, imat, camimat, obmat, NULL, NULL, NULL, NULL);
 +              }
 +      }
 +}
 +
 +static bConstraintTypeInfo CTI_OBJECTSOLVER = {
 +      CONSTRAINT_TYPE_OBJECTSOLVER, /* type */
 +      sizeof(bObjectSolverConstraint), /* size */
 +      "Object Solver", /* name */
 +      "bObjectSolverConstraint", /* struct name */
 +      NULL, /* free data */
 +      NULL, /* relink data */
 +      objectsolver_id_looper, /* id looper */
 +      NULL, /* copy data */
 +      objectsolver_new_data, /* new data */
 +      NULL, /* get constraint targets */
 +      NULL, /* flush constraint targets */
 +      NULL, /* get target matrix */
 +      objectsolver_evaluate /* evaluate */
 +};
 +
  /* ************************* Constraints Type-Info *************************** */
  /* All of the constraints api functions use bConstraintTypeInfo structs to carry out
   * and operations that involve constraint specific code.
@@@ -4266,7 -4156,6 +4257,7 @@@ static void constraints_init_typeinfo (
        constraintsTypeInfo[25]= &CTI_PIVOT;                    /* Pivot Constraint */
        constraintsTypeInfo[26]= &CTI_FOLLOWTRACK;              /* Follow Track Constraint */
        constraintsTypeInfo[27]= &CTI_CAMERASOLVER;             /* Camera Solver Constraint */
 +      constraintsTypeInfo[28]= &CTI_OBJECTSOLVER;             /* Object Solver Constraint */
  }
  
  /* This function should be used for getting the appropriate type-info when only
index 340582ba6276e6423e2537fcd76d9af960f5760f,36df6f76601a93c6be23d66857f66563198884bb..c8130ba6f244f4e3301a6ccb4402584995ed5e0f
@@@ -5934,29 -5934,10 +5934,29 @@@ static void lib_link_group(FileData *fd
  
  /* ***************** READ MOVIECLIP *************** */
  
 +static void direct_link_movieReconstruction(FileData *fd, MovieTrackingReconstruction *reconstruction)
 +{
 +      reconstruction->cameras= newdataadr(fd, reconstruction->cameras);
 +}
 +
 +static void direct_link_movieTracks(FileData *fd, ListBase *tracksbase)
 +{
 +      MovieTrackingTrack *track;
 +
 +      link_list(fd, tracksbase);
 +
 +      track= tracksbase->first;
 +      while(track) {
 +              track->markers= newdataadr(fd, track->markers);
 +
 +              track= track->next;
 +      }
 +}
 +
  static void direct_link_movieclip(FileData *fd, MovieClip *clip)
  {
        MovieTracking *tracking= &clip->tracking;
 -      MovieTrackingTrack *track;
 +      MovieTrackingObject *object;
  
        if(fd->movieclipmap) clip->cache= newmclipadr(fd, clip->cache);
        else clip->cache= NULL;
        if(fd->movieclipmap) clip->tracking.camera.intrinsics= newmclipadr(fd, clip->tracking.camera.intrinsics);
        else clip->tracking.camera.intrinsics= NULL;
  
 -      tracking->reconstruction.cameras= newdataadr(fd, tracking->reconstruction.cameras);
 -
 -      link_list(fd, &tracking->tracks);
 -
 -      track= tracking->tracks.first;
 -      while(track) {
 -              track->markers= newdataadr(fd, track->markers);
 -
 -              track= track->next;
 -      }
 +      direct_link_movieTracks(fd, &tracking->tracks);
 +      direct_link_movieReconstruction(fd, &tracking->reconstruction);
  
        clip->tracking.act_track= newdataadr(fd, clip->tracking.act_track);
  
        clip->tracking.stabilization.ok= 0;
        clip->tracking.stabilization.scaleibuf= NULL;
        clip->tracking.stabilization.rot_track= newdataadr(fd, clip->tracking.stabilization.rot_track);
 +
 +      link_list(fd, &tracking->objects);
 +
 +      object= tracking->objects.first;
 +      while(object) {
 +              direct_link_movieTracks(fd, &object->tracks);
 +              direct_link_movieReconstruction(fd, &object->reconstruction);
 +
 +              object= object->next;
 +      }
  }
  
  static void lib_link_movieclip(FileData *fd, Main *main)
@@@ -8746,9 -8725,9 +8746,9 @@@ static void do_versions(FileData *fd, L
                        }
                        if(ob->soft && ob->soft->vertgroup==0) {
                                bDeformGroup *locGroup = defgroup_find_name(ob, "SOFTGOAL");
-                               if(locGroup){
+                               if (locGroup) {
                                        /* retrieve index for that group */
-                                       ob->soft->vertgroup =  1 + defgroup_find_index(ob, locGroup); 
+                                       ob->soft->vertgroup =  1 + BLI_findindex(&ob->defbase, locGroup);
                                }
                        }
                }
  
        /* put compatibility code here until next subversion bump */
        {
 -              /* nothing! */
 +              MovieClip *clip;
 +
 +              for (clip= main->movieclip.first; clip; clip= clip->id.next) {
 +                      MovieTracking *tracking= &clip->tracking;
 +
 +                      if(tracking->objects.first == NULL)
 +                              BKE_tracking_new_object(tracking, "Camera");
 +              }
        }
  
        /* WATCH IT!!!: pointers from libdata have not been converted yet here! */