[Bf-blender-cvs] [9f69efbd81] snap_system_clip_planes: Innitial commit: Snap system - Adds Clip Plan support and simulates occlusion

Germano Cavalcante noreply at git.blender.org
Fri Feb 24 16:40:57 CET 2017


Commit: 9f69efbd819fc9c914f66fae0368e30ea955182a
Author: Germano Cavalcante
Date:   Fri Feb 24 12:40:39 2017 -0300
Branches: snap_system_clip_planes
https://developer.blender.org/rB9f69efbd819fc9c914f66fae0368e30ea955182a

Innitial commit: Snap system - Adds Clip Plan support and simulates occlusion

The main alterations here were made in the transform_snap_object.c file.
The main change add the "clip plans" of the `rv3d->clip` to bypass the snap to unseen elements. This solution continues to take advantage of the BVH.

Since we can use clip planes, we can now simulate occlusion (ie ignore what is behind a face), first a raycast is made to get the normal and coordinates of the polygon of the hit. Then a plane is created just behind the polygon, thus ignoring everything that is behind it when a snap is made for vertex or edge.

This is not the ideal occlusion solution, but it is already a good step to get the perfect occlusion test.

In addition to this change other changes were made:

Now snap_to is a flag that can indicate whether the snap is made for one, two or all elements at the same time (this will not change much for the user, the only operator using mixed snap is the ruler) ;
if the object is in wireframe mode or Bounding Box, they are ignored in snap to face (good idea?);
use the BVHTree of looptris to snap vertices and edges (saves memory);

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

M	source/blender/blenkernel/BKE_bvhutils.h
M	source/blender/blenkernel/intern/bvhutils.c
M	source/blender/blenlib/BLI_kdopbvh.h
M	source/blender/blenlib/BLI_math_geom.h
M	source/blender/blenlib/intern/BLI_kdopbvh.c
M	source/blender/blenlib/intern/math_geom.c
M	source/blender/editors/include/ED_transform_snap_object_context.h
M	source/blender/editors/transform/transform_snap_object.c
M	source/blender/makesrna/intern/rna_scene_api.c

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

diff --git a/source/blender/blenkernel/BKE_bvhutils.h b/source/blender/blenkernel/BKE_bvhutils.h
index cb72f0859d..351dd652ac 100644
--- a/source/blender/blenkernel/BKE_bvhutils.h
+++ b/source/blender/blenkernel/BKE_bvhutils.h
@@ -160,8 +160,8 @@ BVHTree *bvhtree_from_mesh_looptri(
         struct BVHTreeFromMesh *data, struct DerivedMesh *mesh, float epsilon, int tree_type, int axis);
 BVHTree *bvhtree_from_mesh_looptri_ex(
         struct BVHTreeFromMesh *data,
-        const struct MVert *vert, const bool vert_allocated,
-        const struct MLoop *mloop, const bool loop_allocated,
+        struct MVert *vert, const bool vert_allocated,
+        struct MLoop *mloop, const bool loop_allocated,
         const struct MLoopTri *looptri, const int looptri_num, const bool looptri_allocated,
         const BLI_bitmap *mask, int looptri_num_active,
         float epsilon, int tree_type, int axis);
@@ -195,12 +195,20 @@ enum {
 	BVHTREE_FROM_LOOPTRI         = 3,
 
 	BVHTREE_FROM_EM_LOOPTRI      = 4,
+
+	BVHTREE_FROM_LOOSE_VERTS     = 5,
+	BVHTREE_FROM_LOOSE_EDGES     = 6,
 };
 
 
 BVHTree *bvhcache_find(BVHCache *cache, int type);
+BVHTree *bvhcache_thread_safe_find(BVHCache *cache, int type);
+
 bool     bvhcache_has_tree(const BVHCache *cache, const BVHTree *tree);
+
 void     bvhcache_insert(BVHCache **cache_p, BVHTree *tree, int type);
+void     bvhcache_thread_safe_insert(BVHCache **cache_p, BVHTree *tree, int type);
+
 void     bvhcache_init(BVHCache **cache_p);
 void     bvhcache_free(BVHCache **cache_p);
 
diff --git a/source/blender/blenkernel/intern/bvhutils.c b/source/blender/blenkernel/intern/bvhutils.c
index c0e4ef37a9..4a548b69f9 100644
--- a/source/blender/blenkernel/intern/bvhutils.c
+++ b/source/blender/blenkernel/intern/bvhutils.c
@@ -431,20 +431,24 @@ static BVHTree *bvhtree_from_mesh_verts_create_tree(
 		verts_num_active = verts_num;
 	}
 
-	BVHTree *tree = BLI_bvhtree_new(verts_num_active, epsilon, tree_type, axis);
+	if (verts_num_active) {
+		BVHTree *tree = BLI_bvhtree_new(verts_num_active, epsilon, tree_type, axis);
 
-	if (tree) {
-		for (int i = 0; i < verts_num; i++) {
-			if (verts_mask && !BLI_BITMAP_TEST_BOOL(verts_mask, i)) {
-				continue;
+		if (tree) {
+			for (int i = 0; i < verts_num; i++) {
+				if (verts_mask && !BLI_BITMAP_TEST_BOOL(verts_mask, i)) {
+					continue;
+				}
+				BLI_bvhtree_insert(tree, i, vert[i].co, 1);
 			}
-			BLI_bvhtree_insert(tree, i, vert[i].co, 1);
+			BLI_assert(BLI_bvhtree_get_size(tree) == verts_num_active);
+			BLI_bvhtree_balance(tree);
 		}
-		BLI_assert(BLI_bvhtree_get_size(tree) == verts_num_active);
-		BLI_bvhtree_balance(tree);
+
+		return tree;
 	}
 
-	return tree;
+	return NULL;
 }
 
 static void bvhtree_from_mesh_verts_setup_data(
@@ -596,27 +600,31 @@ static BVHTree *bvhtree_from_editmesh_edges_create_tree(
 		edges_num_active = edges_num;
 	}
 
-	BVHTree *tree = BLI_bvhtree_new(edges_num_active, epsilon, tree_type, axis);
+	if (edges_num_active) {
+		BVHTree *tree = BLI_bvhtree_new(edges_num_active, epsilon, tree_type, axis);
 
-	if (tree) {
-		int i;
-		BMIter iter;
-		BMEdge *eed;
-		BM_ITER_MESH_INDEX (eed, &iter, em->bm, BM_EDGES_OF_MESH, i) {
-			if (edges_mask && !BLI_BITMAP_TEST_BOOL(edges_mask, i)) {
-				continue;
-			}
-			float co[2][3];
-			copy_v3_v3(co[0], eed->v1->co);
-			copy_v3_v3(co[1], eed->v2->co);
+		if (tree) {
+			int i;
+			BMIter iter;
+			BMEdge *eed;
+			BM_ITER_MESH_INDEX (eed, &iter, em->bm, BM_EDGES_OF_MESH, i) {
+				if (edges_mask && !BLI_BITMAP_TEST_BOOL(edges_mask, i)) {
+					continue;
+				}
+				float co[2][3];
+				copy_v3_v3(co[0], eed->v1->co);
+				copy_v3_v3(co[1], eed->v2->co);
 
-			BLI_bvhtree_insert(tree, i, co[0], 2);
+				BLI_bvhtree_insert(tree, i, co[0], 2);
+			}
+			BLI_assert(BLI_bvhtree_get_size(tree) == edges_num_active);
+			BLI_bvhtree_balance(tree);
 		}
-		BLI_assert(BLI_bvhtree_get_size(tree) == edges_num_active);
-		BLI_bvhtree_balance(tree);
+
+		return tree;
 	}
 
-	return tree;
+	return NULL;
 }
 
 static BVHTree *bvhtree_from_mesh_edges_create_tree(
@@ -634,22 +642,26 @@ static BVHTree *bvhtree_from_mesh_edges_create_tree(
 	BLI_assert(edge != NULL);
 
 	/* Create a bvh-tree of the given target */
-	BVHTree *tree = BLI_bvhtree_new(edges_num_active, epsilon, tree_type, axis);
-	if (tree) {
-		for (int i = 0; i < edge_num; i++) {
-			if (edges_mask && !BLI_BITMAP_TEST_BOOL(edges_mask, i)) {
-				continue;
-			}
-			float co[2][3];
-			copy_v3_v3(co[0], vert[edge[i].v1].co);
-			copy_v3_v3(co[1], vert[edge[i].v2].co);
+	if (edges_num_active) {
+		BVHTree *tree = BLI_bvhtree_new(edges_num_active, epsilon, tree_type, axis);
+		if (tree) {
+			for (int i = 0; i < edge_num; i++) {
+				if (edges_mask && !BLI_BITMAP_TEST_BOOL(edges_mask, i)) {
+					continue;
+				}
+				float co[2][3];
+				copy_v3_v3(co[0], vert[edge[i].v1].co);
+				copy_v3_v3(co[1], vert[edge[i].v2].co);
 
-			BLI_bvhtree_insert(tree, i, co[0], 2);
+				BLI_bvhtree_insert(tree, i, co[0], 2);
+			}
+			BLI_bvhtree_balance(tree);
 		}
-		BLI_bvhtree_balance(tree);
+
+		return tree;
 	}
 
-	return tree;
+	return NULL;
 }
 
 static void bvhtree_from_mesh_edges_setup_data(
@@ -954,16 +966,15 @@ static BVHTree *bvhtree_from_editmesh_looptri_create_tree(
         const BLI_bitmap *looptri_mask, int looptri_num_active)
 {
 	BVHTree *tree = NULL;
-	int i;
 
-	if (looptri_num) {
-		if (looptri_mask) {
-			BLI_assert(IN_RANGE_INCL(looptri_num_active, 0, looptri_num));
-		}
-		else {
-			looptri_num_active = looptri_num;
-		}
+	if (looptri_mask) {
+		BLI_assert(IN_RANGE_INCL(looptri_num_active, 0, looptri_num));
+	}
+	else {
+		looptri_num_active = looptri_num;
+	}
 
+	if (looptri_num_active) {
 		/* Create a bvh-tree of the given target */
 		/* printf("%s: building BVH, total=%d\n", __func__, numFaces); */
 		tree = BLI_bvhtree_new(looptri_num_active, epsilon, tree_type, axis);
@@ -975,7 +986,7 @@ static BVHTree *bvhtree_from_editmesh_looptri_create_tree(
 				 * and/or selected. Even if the faces themselves are not selected for the snapped
 				 * transform, having a vertex selected means the face (and thus it's tessellated
 				 * triangles) will be moving and will not be a good snap targets. */
-				for (i = 0; i < looptri_num; i++) {
+				for (int i = 0; i < looptri_num; i++) {
 					const BMLoop **ltri = looptris[i];
 					bool insert = looptri_mask ? BLI_BITMAP_TEST_BOOL(looptri_mask, i) : true;
 
@@ -1004,22 +1015,21 @@ static BVHTree *bvhtree_from_mesh_looptri_create_tree(
         const BLI_bitmap *looptri_mask, int looptri_num_active)
 {
 	BVHTree *tree = NULL;
-	int i;
 
-	if (looptri_num) {
-		if (looptri_mask) {
-			BLI_assert(IN_RANGE_INCL(looptri_num_active, 0, looptri_num));
-		}
-		else {
-			looptri_num_active = looptri_num;
-		}
+	if (looptri_mask) {
+		BLI_assert(IN_RANGE_INCL(looptri_num_active, 0, looptri_num));
+	}
+	else {
+		looptri_num_active = looptri_num;
+	}
 
+	if (looptri_num_active) {
 		/* Create a bvh-tree of the given target */
 		/* printf("%s: building BVH, total=%d\n", __func__, numFaces); */
 		tree = BLI_bvhtree_new(looptri_num_active, epsilon, tree_type, axis);
 		if (tree) {
 			if (vert && looptri) {
-				for (i = 0; i < looptri_num; i++) {
+				for (int i = 0; i < looptri_num; i++) {
 					float co[3][3];
 					if (looptri_mask && !BLI_BITMAP_TEST_BOOL(looptri_mask, i)) {
 						continue;
@@ -1042,8 +1052,8 @@ static BVHTree *bvhtree_from_mesh_looptri_create_tree(
 
 static void bvhtree_from_mesh_looptri_setup_data(
         BVHTreeFromMesh *data, BVHTree *tree, const bool is_cached, float epsilon,
-        const MVert *vert, const bool vert_allocated,
-        const MLoop *mloop, const bool loop_allocated,
+        MVert *vert, const bool vert_allocated,
+        MLoop *mloop, const bool loop_allocated,
         const MLoopTri *looptri, const bool looptri_allocated)
 {
 	memset(data, 0, sizeof(*data));
@@ -1213,8 +1223,8 @@ BVHTree *bvhtree_from_mesh_looptri(
 
 BVHTree *bvhtree_from_mesh_looptri_ex(
         BVHTreeFromMesh *data,
-        const struct MVert *vert, const bool vert_allocated,
-        const struct MLoop *mloop, const bool loop_allocated,
+        struct MVert *vert, const bool vert_allocated,
+        struct MLoop *mloop, const bool loop_allocated,
         const struct MLoopTri *looptri, const int looptri_num, const bool looptri_allocated,
         const BLI_bitmap *looptri_mask, int looptri_num_active,
         float epsilon, int tree_type, int axis)
@@ -1254,7 +1264,6 @@ void free_bvhtree_from_mesh(struct BVHTreeFromMesh *data)
 	if (data->tree && !data->cached) {
 		BLI_bvhtree_free(data->tree);
 	}
-
 	if (data->vert_allocated) {
 		MEM_freeN((void *)data->vert);
 	}
@@ -1301,6 +1310,18 @@ BVHTree *bvhcache_find(BVHCache *cache, int type)
 	return NULL;
 }
 
+/**
+ * Queries a bvhcache for the cache bvhtree of the request type
+ * using locks to protect shared resources from concurrent access
+ */
+BVHTree *bvhcache_thread_safe_find(BVHCache *cache, int type)
+{
+	BLI_rw_mutex_lock(&cache_rwlock, THREAD_LOCK_READ);
+	BVHTree *tree = bvhcache_find(cache, type);
+	BLI_rw_mutex_unlock(&cache_rwlock);
+	return tree;
+}
+
 bool bvhcache_has_tree(const BVHCache *cache, const BVHTree *tree)
 {
 	while (cache) {
@@ -1337,6 +1358,16 @@ void bvhcache_insert(BVHCache **cache_p, BVHTree *tree, int type)
 }
 
 /**
+ * `bvhcache_insert` with locks to protect shared resources from concurrent access
+ */
+void bvhcache_thread_safe_insert(BVHCache **cache_p, BVHTree *tree, int type)
+{
+	BLI_rw_mutex_lock(&cache_rwlock, THREAD_LOCK_WRITE);
+	bvhcache_insert(cache_p, tree, type);
+	BLI_rw_mutex_unlock(&cache_rwlock);
+}
+
+/**
  * inits and frees a bvhcache
  */
 void bvhcache_init(BVHCache **cache_p)
diff --git a/source/blender/blenlib/BLI_kdopbvh.h b/source/blender/blenlib/BLI_kdopbvh.h
index ba565fca52..cfa06bb211 100644
--- a/source/blender/blenlib/BLI_kdopbvh.h
+++ b/source/blender/blenlib/BLI_kdopbvh.h
@@ -104,11 +104,11 @@ typedef void (*BVHTree_RangeQuery)(void *userdata, int index, const float co[3],
 
 /* callbacks to BLI_bvhtree_walk_dfs */
 /* return true to traverse into this nodes children, else skip. */
-typedef bool (*BVHTree_WalkParentCallback)(const BVHTreeAxisRange *bounds, void *user

@@ Diff output truncated at 10240 characters. @@




More information about the Bf-blender-cvs mailing list