[Bf-blender-cvs] [0b5a0d8] master: Transform/Snap: EditMesh/BKE_bvhutils API improvements

Germano Cavalcante noreply at git.blender.org
Thu May 5 21:00:43 CEST 2016


Commit: 0b5a0d84126e629394b198508789378eb5663561
Author: Germano Cavalcante
Date:   Fri May 6 04:49:21 2016 +1000
Branches: master
https://developer.blender.org/rB0b5a0d84126e629394b198508789378eb5663561

Transform/Snap: EditMesh/BKE_bvhutils API improvements

Separate the creation of trees from EditMesh from the creation of trees from DerivedMesh.
This was meant to simplify the API, but didn't work out so well.

`bvhtree_from_mesh_*` actually is working as `bvhtree_from_derivedmesh_*`.
This is inconsistent with the trees created from EditMesh. Since for create them does not use the DerivedMesh.

In such cases the dm is being used only to cache the tree in the struct DerivedMesh. What is immediately released once
bvhtree is being used in functions that change(tag) the DM cleaning the cache.

- Use a filter function so users of SnapObjectContext can define how edit-mesh elements are handled.
- Remove em_evil.
- bvhtree of EditMesh is now really cached in the snap functions.
- Code becomes organized and easier to maintain.

This is an important patch for future improvements in snapping functions.

===================================================================

M	source/blender/blenkernel/BKE_bvhutils.h
M	source/blender/blenkernel/intern/bvhutils.c
M	source/blender/blenkernel/intern/shrinkwrap.c
M	source/blender/blenlib/intern/math_base_inline.c
M	source/blender/bmesh/intern/bmesh_iterators.c
M	source/blender/bmesh/intern/bmesh_iterators.h
M	source/blender/editors/armature/editarmature_sketch.c
M	source/blender/editors/curve/editcurve.c
M	source/blender/editors/include/ED_transform.h
A	source/blender/editors/include/ED_transform_snap_object_context.h
M	source/blender/editors/mesh/editmesh_tools.c
M	source/blender/editors/space_view3d/view3d_ruler.c
M	source/blender/editors/space_view3d/view3d_walk.c
M	source/blender/editors/transform/transform.h
M	source/blender/editors/transform/transform_snap.c
M	source/blender/editors/transform/transform_snap_object.c
M	source/blender/editors/util/CMakeLists.txt
M	source/blender/makesrna/intern/rna_scene_api.c
M	source/blenderplayer/bad_level_call_stubs/stubs.c

===================================================================

diff --git a/source/blender/blenkernel/BKE_bvhutils.h b/source/blender/blenkernel/BKE_bvhutils.h
index 08c0fcc..856923b 100644
--- a/source/blender/blenkernel/BKE_bvhutils.h
+++ b/source/blender/blenkernel/BKE_bvhutils.h
@@ -39,10 +39,30 @@
  */
 
 struct DerivedMesh;
+struct BMEditMesh;
 struct MVert;
 struct MFace;
 
 /**
+* struct that kepts basic information about a BVHTree build from a editmesh
+*/
+typedef struct BVHTreeFromEditMesh {
+	struct BVHTree *tree;
+
+	/* default callbacks to bvh nearest and raycast */
+	BVHTree_NearestPointCallback nearest_callback;
+	BVHTree_RayCastCallback raycast_callback;
+	BVHTree_NearestToRayCallback nearest_to_ray_callback;
+
+	/* radius for raycast */
+	float sphere_radius;
+
+	/* Private data */
+	struct BMEditMesh *em;
+
+} BVHTreeFromEditMesh;
+
+/**
  * struct that kepts basic information about a BVHTree build from a mesh
  */
 typedef struct BVHTreeFromMesh {
@@ -69,8 +89,6 @@ typedef struct BVHTreeFromMesh {
 	float sphere_radius;
 
 	/* Private data */
-	void *em_evil;
-	bool  em_evil_all;  /* ignore selection/hidden state, adding all loops to the tree */
 	bool cached;
 
 } BVHTreeFromMesh;
@@ -85,11 +103,19 @@ typedef struct BVHTreeFromMesh {
  * 
  * free_bvhtree_from_mesh should be called when the tree is no longer needed.
  */
+BVHTree *bvhtree_from_editmesh_verts(
+        BVHTreeFromEditMesh *data, struct BMEditMesh *em,
+        float epsilon, int tree_type, int axis);
+BVHTree *bvhtree_from_editmesh_verts_ex(
+        BVHTreeFromEditMesh *data, struct BMEditMesh *em,
+        const BLI_bitmap *mask, int verts_num_active,
+        float epsilon, int tree_type, int axis);
+
 BVHTree *bvhtree_from_mesh_verts(
         struct BVHTreeFromMesh *data, struct DerivedMesh *mesh, float epsilon, int tree_type, int axis);
 BVHTree *bvhtree_from_mesh_verts_ex(
         struct BVHTreeFromMesh *data, struct MVert *vert, const int numVerts,
-        const bool vert_allocated, BLI_bitmap *mask, int numVerts_active,
+        const bool vert_allocated, const BLI_bitmap *mask, int verts_num_active,
         float epsilon, int tree_type, int axis);
 
 BVHTree *bvhtree_from_mesh_edges(
@@ -103,7 +129,15 @@ BVHTree *bvhtree_from_mesh_faces_ex(
         struct BVHTreeFromMesh *data,
         struct MVert *vert, const bool vert_allocated,
         struct MFace *face, const int numFaces, const bool face_allocated,
-        BLI_bitmap *mask, int numFaces_active,
+        const BLI_bitmap *mask, int numFaces_active,
+        float epsilon, int tree_type, int axis);
+
+BVHTree *bvhtree_from_editmesh_looptri(
+        BVHTreeFromEditMesh *data, struct BMEditMesh *em, float epsilon,
+        int tree_type, int axis);
+BVHTree *bvhtree_from_editmesh_looptri_ex(
+        BVHTreeFromEditMesh *data, struct BMEditMesh *em,
+        const BLI_bitmap *mask, int looptri_num_active,
         float epsilon, int tree_type, int axis);
 
 BVHTree *bvhtree_from_mesh_looptri(
@@ -113,12 +147,13 @@ BVHTree *bvhtree_from_mesh_looptri_ex(
         const struct MVert *vert, const bool vert_allocated,
         const struct MLoop *mloop, const bool loop_allocated,
         const struct MLoopTri *looptri, const int looptri_num, const bool looptri_allocated,
-        BLI_bitmap *mask, int looptri_num_active,
+        const BLI_bitmap *mask, int looptri_num_active,
         float epsilon, int tree_type, int axis);
 
 /**
  * Frees data allocated by a call to bvhtree_from_mesh_*.
  */
+void free_bvhtree_from_editmesh(struct BVHTreeFromEditMesh *data);
 void free_bvhtree_from_mesh(struct BVHTreeFromMesh *data);
 
 /**
@@ -144,12 +179,6 @@ enum {
 	BVHTREE_FROM_EDGES           = 1,
 	BVHTREE_FROM_FACES           = 2,
 	BVHTREE_FROM_LOOPTRI         = 3,
-	/* all faces */
-	BVHTREE_FROM_FACES_EDITMESH_ALL     = 4,
-	/* visible unselected, only used for transform snapping */
-	BVHTREE_FROM_FACES_EDITMESH_SNAP    = 5,
-	// BVHTREE_FROM_EDGES_EDITMESH_SNAP    = 6,
-	BVHTREE_FROM_VERTS_EDITMESH_SNAP    = 7,
 };
 
 typedef struct LinkNode *BVHCache;
diff --git a/source/blender/blenkernel/intern/bvhutils.c b/source/blender/blenkernel/intern/bvhutils.c
index abba613..5e9c185 100644
--- a/source/blender/blenkernel/intern/bvhutils.c
+++ b/source/blender/blenkernel/intern/bvhutils.c
@@ -151,10 +151,10 @@ static void mesh_looptri_nearest_point(void *userdata, int index, const float co
 	}
 }
 /* copy of function above (warning, should de-duplicate with editmesh_bvh.c) */
-static void editmesh_faces_nearest_point(void *userdata, int index, const float co[3], BVHTreeNearest *nearest)
+static void editmesh_looptri_nearest_point(void *userdata, int index, const float co[3], BVHTreeNearest *nearest)
 {
-	const BVHTreeFromMesh *data = (BVHTreeFromMesh *) userdata;
-	BMEditMesh *em = data->em_evil;
+	const BVHTreeFromEditMesh *data = userdata;
+	BMEditMesh *em = data->em;
 	const BMLoop **ltri = (const BMLoop **)em->looptris[index];
 
 	const float *t0, *t1, *t2;
@@ -240,10 +240,10 @@ static void mesh_looptri_spherecast(void *userdata, int index, const BVHTreeRay
 	}
 }
 /* copy of function above (warning, should de-duplicate with editmesh_bvh.c) */
-static void editmesh_faces_spherecast(void *userdata, int index, const BVHTreeRay *ray, BVHTreeRayHit *hit)
+static void editmesh_looptri_spherecast(void *userdata, int index, const BVHTreeRay *ray, BVHTreeRayHit *hit)
 {
-	const BVHTreeFromMesh *data = (BVHTreeFromMesh *) userdata;
-	BMEditMesh *em = data->em_evil;
+	const BVHTreeFromEditMesh *data = (BVHTreeFromEditMesh *)userdata;
+	BMEditMesh *em = data->em;
 	const BMLoop **ltri = (const BMLoop **)em->looptris[index];
 
 	const float *t0, *t1, *t2;
@@ -296,7 +296,7 @@ static void mesh_edges_nearest_point(void *userdata, int index, const float co[3
 
 /* Helper, does all the point-spherecast work actually. */
 static void mesh_verts_spherecast_do(
-	const BVHTreeFromMesh *UNUSED(data), int index, const float v[3], const BVHTreeRay *ray, BVHTreeRayHit *hit)
+        int index, const float v[3], const BVHTreeRay *ray, BVHTreeRayHit *hit)
 {
 	float dist;
 	const float *r1;
@@ -314,6 +314,14 @@ static void mesh_verts_spherecast_do(
 	}
 }
 
+static void editmesh_verts_spherecast(void *userdata, int index, const BVHTreeRay *ray, BVHTreeRayHit *hit)
+{
+	const BVHTreeFromEditMesh *data = userdata;
+	BMVert *eve = BM_vert_at_index(data->em->bm, index);
+
+	mesh_verts_spherecast_do(index, eve->co, ray, hit);
+}
+
 /* Callback to bvh tree raycast. The tree must have been built using bvhtree_from_mesh_verts.
  * userdata must be a BVHMeshCallbackUserdata built from the same mesh as the tree. */
 static void mesh_verts_spherecast(void *userdata, int index, const BVHTreeRay *ray, BVHTreeRayHit *hit)
@@ -321,7 +329,7 @@ static void mesh_verts_spherecast(void *userdata, int index, const BVHTreeRay *r
 	const BVHTreeFromMesh *data = (BVHTreeFromMesh *)userdata;
 	const float *v = data->vert[index].co;
 
-	mesh_verts_spherecast_do(data, index, v, ray, hit);
+	mesh_verts_spherecast_do(index, v, ray, hit);
 }
 
 /* Callback to bvh tree raycast. The tree must have been built using bvhtree_from_mesh_edges.
@@ -341,7 +349,7 @@ static void mesh_edges_spherecast(void *userdata, int index, const BVHTreeRay *r
 
 	/* In case we get a zero-length edge, handle it as a point! */
 	if (equals_v3v3(v1, v2)) {
-		mesh_verts_spherecast_do(data, index, v1, ray, hit);
+		mesh_verts_spherecast_do(index, v1, ray, hit);
 		return;
 	}
 
@@ -380,75 +388,70 @@ static void mesh_edges_spherecast(void *userdata, int index, const BVHTreeRay *r
 /** \name Vertex Builder
  * \{ */
 
-static BVHTree *bvhtree_from_mesh_verts_create_tree(
+static BVHTree *bvhtree_from_editmesh_verts_create_tree(
         float epsilon, int tree_type, int axis,
-        BMEditMesh *em, const int *index_array,
-        MVert *vert, const int numVerts,
-        BLI_bitmap *mask, int numVerts_active)
+        BMEditMesh *em, const int verts_num,
+        const BLI_bitmap *verts_mask, int verts_num_active)
 {
 	BVHTree *tree = NULL;
-	BMVert *eve = NULL;
 	int i;
-	int index = 0;
-	if (em != NULL) {
-		BM_mesh_elem_table_ensure(em->bm, BM_VERT);
+	BM_mesh_elem_table_ensure(em->bm, BM_VERT);
+	if (verts_mask && verts_num_active == -1) {
+		verts_num_active = 0;
+		for (i = 0; i < verts_num; i++) {
+			if (BLI_BITMAP_TEST_BOOL(verts_mask, i)) {
+				verts_num_active++;
+			}
+		}
+	}
+	else if (!verts_mask) {
+		verts_num_active = verts_num;
+	}
+
+	tree = BLI_bvhtree_new(verts_num_active, epsilon, tree_type, axis);
+
+	if (tree) {
+		BMIter iter;
+		BMVert *eve;
+		BM_ITER_MESH_INDEX (eve, &iter, em->bm, BM_VERTS_OF_MESH, i) {
+			if (verts_mask && !BLI_BITMAP_TEST_BOOL(verts_mask, i)) {
+				continue;
+			}
+			BLI_bvhtree_insert(tree, i, eve->co, 1);
+		}
+		BLI_bvhtree_balance(tree);
 	}
+
+	return tree;
+}
+
+static BVHTree *bvhtree_from_mesh_verts_create_tree(
+        float epsilon, int tree_type, int axis,
+        MVert *vert, const int verts_num,
+        const BLI_bitmap *verts_mask, int verts_num_active)
+{
+	BVHTree *tree = NULL;
+	int i;
 	if (vert) {
-		if (mask && numVerts_active < 0) {
-			numVerts_active = 0;
-			for (i = 0; i < numVerts; i++) {
-				if (BLI_BITMAP_TEST_BOOL(mask, i)) {
-					if (em != NULL) {
-						if (index_array) {
-							index = index_array[i];
-							if (index == ORIGINDEX_NONE) {
-								continue;
-							}
-						}
-						else {
-							index = i;
-						}
-
-						eve = BM_vert_at_index(em->bm, index);
-						if (BM_elem_flag_test(eve, BM_ELEM_HIDDEN) ||
-						    BM_elem_flag_test(eve, BM_ELEM_SELECT))
-						{
-							continue;
-						}
-					}
-					numVerts_active++;
+		if (verts_mask && verts_num_active == -1) {
+			verts_num_active = 0;
+			for (i = 0; i < verts_num; i++) {
+				if (BLI_BITMAP_TEST_BOOL(verts_mask, i)) {
+					verts_num_active++;
 				}
 			}
 		}
-		else if (!mask) {
-			numVerts_active = numVerts;
+		else if (!verts_mask) {
+			verts_num_active = verts_num;
 		}
 
-		tree = BLI_bvhtree_new(numVerts_active, epsilon, tree_type, axis);
+		tree = BLI_bvhtree_new(verts_num_active, epsilon, tree_type, axis);
 
 		if (tree) {
-			for (i = 0; i < numVerts; i++) {
-				if (mask && !BLI_BITMAP_TEST_BOOL(mask, i)) {
+			for (i = 0; i < verts_num; i++) {
+				if (verts_mask

@@ Diff output truncated at 10240 characters. @@




More information about the Bf-blender-cvs mailing list