[Bf-blender-cvs] [8f014aa] mathutils_bvhtree: Minor edits to custom bvh

Campbell Barton noreply at git.blender.org
Wed Jul 15 13:34:40 CEST 2015


Commit: 8f014aa7f58489f06a6e71db4140b688ecd4c6ea
Author: Campbell Barton
Date:   Wed Jul 15 21:28:44 2015 +1000
Branches: mathutils_bvhtree
https://developer.blender.org/rB8f014aa7f58489f06a6e71db4140b688ecd4c6ea

Minor edits to custom bvh

- use array triples & UNPACK macro
- replace (int)PyLong_AsLong -> _PyLong_AsInt

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

M	source/blender/python/mathutils/mathutils_bvhtree.c

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

diff --git a/source/blender/python/mathutils/mathutils_bvhtree.c b/source/blender/python/mathutils/mathutils_bvhtree.c
index 310ec41..4a19e0a 100644
--- a/source/blender/python/mathutils/mathutils_bvhtree.c
+++ b/source/blender/python/mathutils/mathutils_bvhtree.c
@@ -78,7 +78,7 @@ typedef struct BVHVertex {
 } BVHVertex;
 
 typedef struct BVHTriangle {
-	int v1, v2, v3;
+	int tri[3];
 } BVHTriangle;
 
 typedef struct PyBVHTree_Custom {
@@ -86,8 +86,8 @@ typedef struct PyBVHTree_Custom {
 	/* geometry data */
 	struct BVHTree *tree;
 	
-	struct BVHVertex *vert;
-	struct BVHTriangle *tri;
+	struct BVHVertex *vert_array;
+	struct BVHTriangle *tri_array;
 	int totvert, tottri;
 	
 	float epsilon;
@@ -728,9 +728,9 @@ static BVHTree *bvhtree_from_triangles_create_tree(float epsilon, int tree_type,
 		for (i = 0; i < numtris; i++) {
 			float co[3][3];
 			
-			copy_v3_v3(co[0], vert[tri[i].v1].co);
-			copy_v3_v3(co[1], vert[tri[i].v2].co);
-			copy_v3_v3(co[2], vert[tri[i].v3].co);
+			copy_v3_v3(co[0], vert[tri[i].tri[0]].co);
+			copy_v3_v3(co[1], vert[tri[i].tri[1]].co);
+			copy_v3_v3(co[2], vert[tri[i].tri[2]].co);
 			
 			BLI_bvhtree_insert(tree, i, co[0], 3);
 		}
@@ -802,22 +802,23 @@ static int PyBVHTreeCustom__tp_init(PyBVHTree_Custom *self, PyObject *args, PyOb
 				break;
 			}
 			
-			tp->v1 = (int)PyLong_AsLong(PySequence_GetItem(py_triverts, 0));
-			tp->v2 = (int)PyLong_AsLong(PySequence_GetItem(py_triverts, 1));
-			tp->v3 = (int)PyLong_AsLong(PySequence_GetItem(py_triverts, 2));
+			tp->tri[0] = _PyLong_AsInt(PySequence_GetItem(py_triverts, 0));
+			tp->tri[1] = _PyLong_AsInt(PySequence_GetItem(py_triverts, 1));
+			tp->tri[2] = _PyLong_AsInt(PySequence_GetItem(py_triverts, 2));
 		}
 	}
 	
 	if (valid) {
-		self->vert = verts;
+		self->vert_array = verts;
 		self->totvert = numverts;
-		self->tri = tris;
+		self->tri_array = tris;
 		self->tottri = numtris;
 		
 		/* XXX make configurable? */
 		self->epsilon = 0.0f;
 		
-		self->tree = bvhtree_from_triangles_create_tree(self->epsilon, 4, 6, self->vert, self->tri, self->tottri);
+		self->tree = bvhtree_from_triangles_create_tree(
+		        self->epsilon, 4, 6, self->vert_array, self->tri_array, self->tottri);
 		
 		return 0;
 	}
@@ -836,10 +837,10 @@ static void PyBVHTreeCustom__tp_dealloc(PyBVHTree_Custom *self)
 	if (self->tree)
 		BLI_bvhtree_free(self->tree);
 	
-	if (self->vert)
-		MEM_freeN(self->vert);
-	if (self->tri)
-		MEM_freeN(self->tri);
+	if (self->vert_array)
+		MEM_freeN(self->vert_array);
+	if (self->tri_array)
+		MEM_freeN(self->tri_array);
 	
 	Py_TYPE(self)->tp_free((PyObject *)self);
 }
@@ -847,28 +848,21 @@ static void PyBVHTreeCustom__tp_dealloc(PyBVHTree_Custom *self)
 static void bvhtree_custom_raycast_cb(void *userdata, int index, const BVHTreeRay *ray, BVHTreeRayHit *hit)
 {
 	PyBVHTree_Custom *self = (PyBVHTree_Custom *)userdata;
-	const BVHVertex *verts = self->vert;
-	const BVHTriangle *tris = self->tri;
+	const BVHVertex *verts = self->vert_array;
+	const BVHTriangle *tris = self->tri_array;
+	const float *tri[3] = {verts[tris->tri[0]].co, verts[tris->tri[1]].co, verts[tris->tri[2]].co};
+	float dist;
 
-	const float *t0, *t1, *t2;
-	t0 = verts[ tris[index].v1 ].co;
-	t1 = verts[ tris[index].v2 ].co;
-	t2 = verts[ tris[index].v3 ].co;
-
-	{
-		float dist;
-		if (self->epsilon == 0.0f)
-			dist = bvhtree_ray_tri_intersection(ray, hit->dist, t0, t1, t2);
-		else
-			dist = bvhtree_sphereray_tri_intersection(ray, self->epsilon, hit->dist, t0, t1, t2);
-
-		if (dist >= 0 && dist < hit->dist) {
-			hit->index = index;
-			hit->dist = dist;
-			madd_v3_v3v3fl(hit->co, ray->origin, ray->direction, dist);
+	if (self->epsilon == 0.0f)
+		dist = bvhtree_ray_tri_intersection(ray, hit->dist, UNPACK3(tri));
+	else
+		dist = bvhtree_sphereray_tri_intersection(ray, self->epsilon, hit->dist, UNPACK3(tri));
 
-			normal_tri_v3(hit->no, t0, t1, t2);
-		}
+	if (dist >= 0 && dist < hit->dist) {
+		hit->index = index;
+		hit->dist = dist;
+		madd_v3_v3v3fl(hit->co, ray->origin, ray->direction, dist);
+		normal_tri_v3(hit->no, UNPACK3(tri));
 	}
 }
 
@@ -930,26 +924,19 @@ static PyObject *py_BVHTreeCustom_ray_cast(PyBVHTree_Custom *self, PyObject *arg
 static void bvhtree_custom_nearest_point_cb(void *userdata, int index, const float co[3], BVHTreeNearest *nearest)
 {
 	PyBVHTree_Custom *self = (PyBVHTree_Custom *)userdata;
-	const BVHVertex *verts = self->vert;
-	const BVHTriangle *tris = self->tri;
-
-	const float *t0, *t1, *t2, *t3;
-	t0 = verts[ tris->v1 ].co;
-	t1 = verts[ tris->v2 ].co;
-	t2 = verts[ tris->v3 ].co;
-
-	{
-		float nearest_tmp[3], dist_sq;
-
-		closest_on_tri_to_point_v3(nearest_tmp, co, t0, t1, t2);
-		dist_sq = len_squared_v3v3(co, nearest_tmp);
-
-		if (dist_sq < nearest->dist_sq) {
-			nearest->index = index;
-			nearest->dist_sq = dist_sq;
-			copy_v3_v3(nearest->co, nearest_tmp);
-			normal_tri_v3(nearest->no, t0, t1, t2);
-		}
+	const BVHVertex *verts = self->vert_array;
+	const BVHTriangle *tris = self->tri_array;
+	const float *tri[3] = {verts[tris->tri[0]].co, verts[tris->tri[1]].co, verts[tris->tri[2]].co};
+	float nearest_tmp[3], dist_sq;
+
+	closest_on_tri_to_point_v3(nearest_tmp, co, UNPACK3(tri));
+	dist_sq = len_squared_v3v3(co, nearest_tmp);
+
+	if (dist_sq < nearest->dist_sq) {
+		nearest->index = index;
+		nearest->dist_sq = dist_sq;
+		copy_v3_v3(nearest->co, nearest_tmp);
+		normal_tri_v3(nearest->no, UNPACK3(tri));
 	}
 }




More information about the Bf-blender-cvs mailing list