Add 'level' field to struct MDisps, companion to 'totdisp'.
authorNicholas Bishop <nicholasbishop@gmail.com>
Wed, 14 Mar 2012 03:10:18 +0000 (03:10 +0000)
committerNicholas Bishop <nicholasbishop@gmail.com>
Wed, 14 Mar 2012 03:10:18 +0000 (03:10 +0000)
Gets initialized when loading old files and updated at the same places
totdisp is updated. Saves having to do log+sqrt to extract level from
totdisp.

source/blender/blenkernel/intern/customdata.c
source/blender/blenkernel/intern/mesh.c
source/blender/blenkernel/intern/multires.c
source/blender/blenloader/intern/readfile.c
source/blender/bmesh/intern/bmesh_interp.c
source/blender/bmesh/intern/bmesh_mesh.c
source/blender/makesdna/DNA_meshdata_types.h

index 233d020..2520c9b 100644 (file)
@@ -485,10 +485,12 @@ static void layerCopy_mdisps(const void *source, void *dest, int count)
                if(s[i].disps) {
                        d[i].disps = MEM_dupallocN(s[i].disps);
                        d[i].totdisp = s[i].totdisp;
+                       d[i].level = s[i].level;
                }
                else {
                        d[i].disps = NULL;
                        d[i].totdisp = 0;
+                       d[i].level = 0;
                }
                
        }
@@ -504,6 +506,7 @@ static void layerFree_mdisps(void *data, int count, int UNUSED(size))
                        MEM_freeN(d[i].disps);
                d[i].disps = NULL;
                d[i].totdisp = 0;
+               d[i].level = 0;
        }
 }
 
index 054cbe0..6600099 100644 (file)
@@ -1877,6 +1877,7 @@ static void bm_corners_to_loops(Mesh *me, int findex, int loopstart, int numTex,
                
                        for (i=0; i<tot; i++, disps += side*side, ld++) {
                                ld->totdisp = side*side;
+                               ld->level = (int)(logf(side - 1.0f) / M_LN2) + 1;
                        
                                if (ld->disps)
                                        MEM_freeN(ld->disps);
index 7d50ec6..b708f33 100644 (file)
@@ -320,6 +320,7 @@ static void multires_set_tot_mdisps(Mesh *me, int lvl)
        if (mdisps) {
                for (i = 0; i < me->totloop; i++, mdisps++) {
                        mdisps->totdisp = multires_grid_tot[lvl];
+                       mdisps->level = lvl;
                }
        }
 }
@@ -338,6 +339,7 @@ static void multires_reallocate_mdisps(int totloop, MDisps *mdisps, int lvl)
 
                mdisps[i].disps = disps;
                mdisps[i].totdisp = totdisp;
+               mdisps[i].level = lvl;
        }
 }
 
@@ -426,6 +428,7 @@ static void multires_del_higher(MultiresModifierData *mmd, Object *ob, int lvl)
                                        MEM_freeN(mdisp->disps);
                                        mdisp->disps = disps;
                                        mdisp->totdisp = totdisp;
+                                       mdisp->level = lvl;
                                }
                        }
                }
@@ -999,6 +1002,7 @@ void multires_set_space(DerivedMesh *dm, Object *ob, int from, int to)
                        /* when adding new faces in edit mode, need to allocate disps */
                        if (!mdisp->disps) {
                                mdisp->totdisp = gridSize*gridSize;
+                               mdisp->level = totlvl;
                                mdisp->disps = MEM_callocN(sizeof(float)*3*mdisp->totdisp, "disp in multires_set_space");
                        }
 
@@ -1224,6 +1228,7 @@ static void old_mdisps_convert(MFace *mface, MDisps *mdisp)
        MEM_freeN(mdisp->disps);
 
        mdisp->totdisp= newtotdisp;
+       mdisp->level= newlvl;
        mdisp->disps= disps;
 }
 
@@ -1252,6 +1257,7 @@ void multires_load_old_250(Mesh *me)
                        for (j=0; j < mf->v4 ? 4 : 3; j++, k++) {
                                mdisps2[k].disps = MEM_callocN(sizeof(float)*3*totdisp, "multires disp in conversion");                 
                                mdisps2[k].totdisp = totdisp;
+                               mdisps2[k].level = mdisps[i].level;
                                memcpy(mdisps2[k].disps, mdisps[i].disps + totdisp*j, totdisp);
                        }
 
index 50b8e09..2cbb8a3 100644 (file)
@@ -3751,6 +3751,15 @@ static void direct_link_mdisps(FileData *fd, int count, MDisps *mdisps, int exte
                for(i = 0; i < count; ++i) {
                        mdisps[i].disps = newdataadr(fd, mdisps[i].disps);
 
+                       if (mdisps[i].totdisp && !mdisps[i].level) {
+                               /* this calculation is only correct for loop mdisps;
+                                  if loading pre-BMesh face mdisps this will be
+                                  overwritten with the correct value in
+                                  bm_corners_to_loops() */
+                               float gridsize = sqrtf(mdisps[i].totdisp);
+                               mdisps[i].level = (int)(logf(gridsize - 1.0f) / M_LN2) + 1;
+                       }
+
                        if( (fd->flags & FD_FLAGS_SWITCH_ENDIAN) && (mdisps[i].disps) ) {
                                /* DNA_struct_switch_endian doesn't do endian swap for (*disps)[] */
                                /* this does swap for data written at write_mdisps() - readfile.c */
index 7074adc..5b8b537 100644 (file)
@@ -426,6 +426,7 @@ static void bm_loop_interp_mdisps(BMesh *bm, BMLoop *target, BMFace *source)
                MDisps *md2 = CustomData_bmesh_get(&bm->ldata, BM_FACE_FIRST_LOOP(source)->head.data, CD_MDISPS);
                
                mdisps->totdisp = md2->totdisp;
+               mdisps->level = md2->level;
                if (mdisps->totdisp) {
                        mdisps->disps = MEM_callocN(sizeof(float) * 3 * mdisps->totdisp,
                                                    "mdisp->disps in bmesh_loop_intern_mdisps");
index 04279d3..096c1fa 100644 (file)
@@ -371,6 +371,7 @@ static void UNUSED_FUNCTION(bm_mdisps_space_set)(Object *ob, BMesh *bm, int from
                                        
                                        lmd->disps = MEM_dupallocN(mdisps->disps);
                                        lmd->totdisp = mdisps->totdisp;
+                                       lmd->level = mdisps->level;
                                }
                                
                                mdisps++;
index 4367d25..f5a8a18 100644 (file)
@@ -164,7 +164,7 @@ typedef struct OrigSpaceLoop {
 typedef struct MDisps {
        /* Strange bug in SDNA: if disps pointer comes first, it fails to see totdisp */
        int totdisp;
-       char pad[4];
+       int level;
        float (*disps)[3];
 } MDisps;