speedup for vertex parent lookups, were looping over array elements when it wasn...
authorCampbell Barton <ideasman42@gmail.com>
Sun, 16 Sep 2012 08:25:31 +0000 (08:25 +0000)
committerCampbell Barton <ideasman42@gmail.com>
Sun, 16 Sep 2012 08:25:31 +0000 (08:25 +0000)
do dirrect array lookups instead where possible.

source/blender/blenkernel/BKE_curve.h
source/blender/blenkernel/intern/curve.c
source/blender/blenkernel/intern/object.c

index a845ac10c9d1f5dd9416812928480991a91aede7..e6161cebf54d63fa6b94ed652583d78db5f80a60 100644 (file)
@@ -93,6 +93,8 @@ void BKE_curve_forward_diff_bezier(float q0, float q1, float q2, float q3, float
 
 /* ** Nurbs ** */
 
+int BKE_nurbList_index_get_co(struct ListBase *editnurb, const int index, float r_co[3]);
+
 int BKE_nurbList_verts_count(struct ListBase *nurb);
 int BKE_nurbList_verts_count_without_handles(struct ListBase *nurb);
 
index a2f88781cbb7760545bb0e0ac1b58c4f781d45b0..09ca0df06805230fd93989a47c32f5e3cd44075d 100644 (file)
@@ -429,6 +429,33 @@ void BKE_curve_texspace_calc(Curve *cu)
        }
 }
 
+int BKE_nurbList_index_get_co(ListBase *nurb, const int index, float r_co[3])
+{
+       Nurb *nu;
+       int tot = 0;
+
+       for (nu = nurb->first; nu; nu = nu->next) {
+               int tot_nu;
+               if (nu->type == CU_BEZIER) {
+                       tot_nu = nu->pntsu;
+                       if (index - tot < tot_nu) {
+                               copy_v3_v3(r_co, nu->bezt[index - tot].vec[1]);
+                               return TRUE;
+                       }
+               }
+               else {
+                       tot_nu = nu->pntsu * nu->pntsv;
+                       if (index - tot < tot_nu) {
+                               copy_v3_v3(r_co, nu->bp[index - tot].vec);
+                               return TRUE;
+                       }
+               }
+               tot += tot_nu;
+       }
+
+       return FALSE;
+}
+
 int BKE_nurbList_verts_count(ListBase *nurb)
 {
        Nurb *nu;
index 5ba56a85c724468987449e6b9f5a0fecf7e6658f..4e3096d03254b3e607b773646a8a99fc4db36fd8 100644 (file)
@@ -1766,13 +1766,13 @@ static void ob_parbone(Object *ob, Object *par, float mat[][4])
 static void give_parvert(Object *par, int nr, float vec[3])
 {
        BMEditMesh *em;
-       int a, count;
 
        zero_v3(vec);
        
        if (par->type == OB_MESH) {
                Mesh *me = par->data;
                DerivedMesh *dm;
+               int count;
 
                em = me->edit_btmesh;
 
@@ -1795,18 +1795,28 @@ static void give_parvert(Object *par, int nr, float vec[3])
                dm = (em) ? em->derivedFinal : par->derivedFinal;
                        
                if (dm) {
-                       MVert *mvert = dm->getVertArray(dm);
-                       int *index = (int *)dm->getVertDataArray(dm, CD_ORIGINDEX);
-                       int i, vindex, numVerts = dm->getNumVerts(dm);
-
-                       /* get the average of all verts with (original index == nr) */
-                       count = 0;
-                       for (i = 0; i < numVerts; i++) {
-                               vindex = (index) ? index[i] : i;
-
-                               if (vindex == nr) {
-                                       add_v3_v3(vec, mvert[i].co);
-                                       count++;
+                       int numVerts = dm->getNumVerts(dm);
+
+                       if (nr < numVerts) {
+                               MVert *mvert = dm->getVertArray(dm);
+                               int   *index = (int *)dm->getVertDataArray(dm, CD_ORIGINDEX);
+                               int i;
+
+                               /* get the average of all verts with (original index == nr) */
+                               count = 0;
+                               if (index) {
+                                       for (i = 0; i < numVerts; i++) {
+                                               if (index[i] == nr) {
+                                                       add_v3_v3(vec, mvert[i].co);
+                                                       count++;
+                                               }
+                                       }
+                               }
+                               else {
+                                       if (nr < numVerts) {
+                                               add_v3_v3(vec, mvert[nr].co);
+                                               count++;
+                                       }
                                }
                        }
 
@@ -1828,71 +1838,31 @@ static void give_parvert(Object *par, int nr, float vec[3])
                }
        }
        else if (ELEM(par->type, OB_CURVE, OB_SURF)) {
-               Nurb *nu;
-               Curve *cu;
-               BPoint *bp;
-               BezTriple *bezt;
-               int found = 0;
-               ListBase *nurbs;
-
-               cu = par->data;
-               nurbs = BKE_curve_nurbs_get(cu);
-               nu = nurbs->first;
-
-               count = 0;
-               while (nu && !found) {
-                       if (nu->type == CU_BEZIER) {
-                               bezt = nu->bezt;
-                               a = nu->pntsu;
-                               while (a--) {
-                                       if (count == nr) {
-                                               found = 1;
-                                               copy_v3_v3(vec, bezt->vec[1]);
-                                               break;
-                                       }
-                                       count++;
-                                       bezt++;
-                               }
-                       }
-                       else {
-                               bp = nu->bp;
-                               a = nu->pntsu * nu->pntsv;
-                               while (a--) {
-                                       if (count == nr) {
-                                               found = 1;
-                                               copy_v3_v3(vec, bp->vec);
-                                               break;
-                                       }
-                                       count++;
-                                       bp++;
-                               }
-                       }
-                       nu = nu->next;
-               }
+               Curve *cu       = par->data;
+               ListBase *nurb  = BKE_curve_nurbs_get(cu);;
 
+               BKE_nurbList_index_get_co(nurb, nr, vec);
        }
        else if (par->type == OB_LATTICE) {
-               Lattice *latt = par->data;
-               BPoint *bp;
-               DispList *dl = BKE_displist_find(&par->disp, DL_VERTS);
-               float *co = dl ? dl->verts : NULL;
-               
+               Lattice *latt  = par->data;
+               DispList *dl   = BKE_displist_find(&par->disp, DL_VERTS);
+               float (*co)[3] = dl ? (float (*)[3])dl->verts : NULL;
+               int tot;
+
                if (latt->editlatt) latt = latt->editlatt->latt;
-               
-               a = latt->pntsu * latt->pntsv * latt->pntsw;
-               count = 0;
-               bp = latt->def;
-               while (a--) {
-                       if (count == nr) {
-                               if (co)
-                                       copy_v3_v3(vec, co);
-                               else
-                                       copy_v3_v3(vec, bp->vec);
-                               break;
+
+               tot = latt->pntsu * latt->pntsv * latt->pntsw;
+
+               /* ensure dl is correct size */
+               BLI_assert(dl == NULL || dl->nr == tot);
+
+               if (nr < tot) {
+                       if (co) {
+                               copy_v3_v3(vec, co[nr]);
+                       }
+                       else {
+                               copy_v3_v3(vec, latt->def[nr].vec);
                        }
-                       count++;
-                       if (co) co += 3;
-                       else bp++;
                }
        }
 }