[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [56090] trunk/blender/source/blender/ blenkernel/intern/editmesh_bvh.c: bmesh speedup for bvh building, replace SmallHash with BLI_bitmap, using a hash doesn' t make much sense since in most cases all vertices are accessed and the hash isn 't guaranteed to be small.

Campbell Barton ideasman42 at gmail.com
Tue Apr 16 16:57:05 CEST 2013


Revision: 56090
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=56090
Author:   campbellbarton
Date:     2013-04-16 14:57:02 +0000 (Tue, 16 Apr 2013)
Log Message:
-----------
bmesh speedup for bvh building, replace SmallHash with BLI_bitmap, using a hash doesn't make much sense since in most cases all vertices are accessed and the hash isn't guaranteed to be small.
gives ~9x speedup to filling 'cagecos' in my own tests on a high poly mesh.

Modified Paths:
--------------
    trunk/blender/source/blender/blenkernel/intern/editmesh_bvh.c

Modified: trunk/blender/source/blender/blenkernel/intern/editmesh_bvh.c
===================================================================
--- trunk/blender/source/blender/blenkernel/intern/editmesh_bvh.c	2013-04-16 14:00:18 UTC (rev 56089)
+++ trunk/blender/source/blender/blenkernel/intern/editmesh_bvh.c	2013-04-16 14:57:02 UTC (rev 56090)
@@ -34,7 +34,7 @@
 #include "DNA_object_types.h"
 
 #include "BLI_math.h"
-#include "BLI_smallhash.h"
+#include "BLI_bitmap.h"
 
 #include "BKE_DerivedMesh.h"
 #include "BKE_editmesh.h"
@@ -56,21 +56,22 @@
 	float curw, curd;
 	float co[3], (*cagecos)[3], (*cos)[3];
 	int curtag, flag;
-	
-	Object *ob;
 };
 
+struct CageUserData {
+	int totvert;
+	float (*cagecos)[3];
+	BLI_bitmap vert_bitmap;
+};
+
 static void cage_mapped_verts_callback(void *userData, int index, const float co[3],
                                        const float UNUSED(no_f[3]), const short UNUSED(no_s[3]))
 {
-	void **data = userData;
-	BMEditMesh *em = data[0];
-	float (*cagecos)[3] = data[1];
-	SmallHash *hash = data[2];
-	
-	if (index >= 0 && index < em->bm->totvert && !BLI_smallhash_haskey(hash, index)) {
-		BLI_smallhash_insert(hash, index, NULL);
-		copy_v3_v3(cagecos[index], co);
+	struct CageUserData *data = userData;
+
+	if ((index >= 0 && index < data->totvert) && (!BLI_BITMAP_GET(data->vert_bitmap, index))) {
+		BLI_BITMAP_SET(data->vert_bitmap, index);
+		copy_v3_v3(data->cagecos[index], co);
 	}
 }
 
@@ -79,7 +80,6 @@
 	struct BMLoop *(*looptris)[3] = em->looptris;
 	BMBVHTree *tree = MEM_callocN(sizeof(*tree), "BMBVHTree");
 	DerivedMesh *cage, *final;
-	SmallHash shash;
 	float cos[3][3], (*cagecos)[3] = NULL;
 	int i;
 	int tottri;
@@ -87,11 +87,9 @@
 	/* BKE_editmesh_tessface_calc() must be called already */
 	BLI_assert(em->tottri != 0 || em->bm->totface == 0);
 
-	/* when initializing cage verts, we only want the first cage coordinate for each vertex,
-	 * so that e.g. mirror or array use original vertex coordinates and not mirrored or duplicate */
-	BLI_smallhash_init(&shash);
+	/* cage-flag needs scene */
+	BLI_assert(scene || !(flag & BMBVH_USE_CAGE));
 
-	tree->ob = em->ob;
 	tree->em = em;
 	tree->bm = em->bm;
 	tree->epsilon = FLT_EPSILON * 2.0f;
@@ -120,11 +118,12 @@
 	tree->tree = BLI_bvhtree_new(tottri, tree->epsilon, 8, 8);
 	
 	if (flag & BMBVH_USE_CAGE) {
+		BLI_bitmap vert_bitmap;
 		BMIter iter;
 		BMVert *v;
-		void *data[3];
-		
-		tree->cos = MEM_callocN(sizeof(float) * 3 * em->bm->totvert, "bmbvh cos");
+		struct CageUserData data;
+
+		tree->cos = MEM_callocN(sizeof(*tree->cos) * em->bm->totvert, "bmbvh cos");
 		BM_ITER_MESH_INDEX (v, &iter, em->bm, BM_VERTS_OF_MESH, i) {
 			BM_elem_index_set(v, i); /* set_inline */
 			copy_v3_v3(tree->cos[i], v->co);
@@ -135,11 +134,17 @@
 		cage = editbmesh_get_derived_cage_and_final(scene, em->ob, em, &final, CD_MASK_DERIVEDMESH);
 		cagecos = MEM_callocN(sizeof(float) * 3 * em->bm->totvert, "bmbvh cagecos");
 		
-		data[0] = em;
-		data[1] = cagecos;
-		data[2] = &shash;
+		/* when initializing cage verts, we only want the first cage coordinate for each vertex,
+		 * so that e.g. mirror or array use original vertex coordinates and not mirrored or duplicate */
+		vert_bitmap = BLI_BITMAP_NEW(em->bm->totvert, __func__);
+
+		data.totvert = em->bm->totvert;
+		data.cagecos = cagecos;
+		data.vert_bitmap = vert_bitmap;
 		
-		cage->foreachMappedVert(cage, cage_mapped_verts_callback, data);
+		cage->foreachMappedVert(cage, cage_mapped_verts_callback, &data);
+
+		MEM_freeN(vert_bitmap);
 	}
 	
 	tree->cagecos = cagecos;
@@ -175,7 +180,6 @@
 	}
 	
 	BLI_bvhtree_balance(tree->tree);
-	BLI_smallhash_release(&shash);
 	
 	return tree;
 }




More information about the Bf-blender-cvs mailing list