[Bf-blender-cvs] [6355e3a] master: As suggested by Campbell, rather use BVHTree to get len_diff in this morning's fix.

Bastien Montagne noreply at git.blender.org
Mon Jul 28 20:35:27 CEST 2014


Commit: 6355e3a45d1dd26b09713ece4c353bc890fd129c
Author: Bastien Montagne
Date:   Mon Jul 28 20:30:47 2014 +0200
Branches: master
https://developer.blender.org/rB6355e3a45d1dd26b09713ece4c353bc890fd129c

As suggested by Campbell, rather use BVHTree to get len_diff in this morning's fix.

Note than it's using nearest faces, since it showed to be much more performant than
nearest vertex (quite odd, it's about 40% slower for the first element, then 50 times quicker
for all others, as if BVH was cached, and building face was slower than verts one,
but then using it, much quicker!).

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

M	source/blender/editors/transform/transform_snap.c

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

diff --git a/source/blender/editors/transform/transform_snap.c b/source/blender/editors/transform/transform_snap.c
index 26eb836..dfb75bb 100644
--- a/source/blender/editors/transform/transform_snap.c
+++ b/source/blender/editors/transform/transform_snap.c
@@ -1523,21 +1523,25 @@ static bool snapDerivedMesh(short snap_mode, ARegion *ar, Object *ob, DerivedMes
 		}
 		else if (do_ray_start_correction) {
 			/* We *need* a reasonably valid len_diff in this case.
-			 * Default to distance from object's center (i.e. point (0,0,0) since we are in local space)
-			 * minus farthest vertex from this center.
+			 * Use BHVTree to find the closest face from ray_start_local.
 			 */
-			int i = totvert;
-			MVert *mv = dm->getVertArray(dm);
-			float max_dist_squared = 0.0f;
-
-			for (; i; i--, mv++) {
-				const float d = len_squared_v3(mv->co);
-				if (d > max_dist_squared) {
-					max_dist_squared = d;
+			BVHTreeFromMesh treeData;
+			BVHTreeNearest nearest;
+			len_diff = 0.0f;  /* In case BVHTree would fail for some reason... */
+
+			treeData.em_evil = em;
+			bvhtree_from_mesh_faces(&treeData, dm, 0.0f, 2, 6);
+			if (treeData.tree != NULL) {
+				nearest.index = -1;
+				nearest.dist_sq = FLT_MAX;
+				/* Compute and store result. */
+				BLI_bvhtree_find_nearest(treeData.tree, ray_start_local, &nearest,
+				                         treeData.nearest_callback, &treeData);
+				if (nearest.index != -1) {
+					len_diff = sqrtf(nearest.dist_sq);
 				}
 			}
-
-			len_diff = len_v3(ray_start_local) - sqrtf(max_dist_squared);
+			free_bvhtree_from_mesh(&treeData);
 		}
 
 		switch (snap_mode) {




More information about the Bf-blender-cvs mailing list