[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [14929] branches/apricot/source/blender: object: ray - boundbox intersection test

Martin Poirier theeth at yahoo.com
Thu May 22 17:20:21 CEST 2008


Revision: 14929
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=14929
Author:   theeth
Date:     2008-05-22 17:20:14 +0200 (Thu, 22 May 2008)

Log Message:
-----------
object: ray - boundbox intersection test

snap: use the above instead (giving up padding but getting rid of stupid projection problems)

Modified Paths:
--------------
    branches/apricot/source/blender/blenkernel/BKE_object.h
    branches/apricot/source/blender/blenkernel/intern/object.c
    branches/apricot/source/blender/src/transform_snap.c

Modified: branches/apricot/source/blender/blenkernel/BKE_object.h
===================================================================
--- branches/apricot/source/blender/blenkernel/BKE_object.h	2008-05-22 14:56:32 UTC (rev 14928)
+++ branches/apricot/source/blender/blenkernel/BKE_object.h	2008-05-22 15:20:14 UTC (rev 14929)
@@ -108,6 +108,7 @@
 void minmax_object(struct Object *ob, float *min, float *max);
 void minmax_object_duplis(struct Object *ob, float *min, float *max);
 void solve_tracking (struct Object *ob, float targetmat[][4]);
+int ray_hit_boundbox(struct BoundBox *bb, float ray_start[3], float ray_normal[3]);
 
 void object_handle_update(struct Object *ob);
 

Modified: branches/apricot/source/blender/blenkernel/intern/object.c
===================================================================
--- branches/apricot/source/blender/blenkernel/intern/object.c	2008-05-22 14:56:32 UTC (rev 14928)
+++ branches/apricot/source/blender/blenkernel/intern/object.c	2008-05-22 15:20:14 UTC (rev 14929)
@@ -2380,3 +2380,31 @@
 	}
 	return 1;
 }
+
+/*
+ * Test a bounding box for ray intersection
+ * assumes the ray is already local to the boundbox space
+ */
+int ray_hit_boundbox(struct BoundBox *bb, float ray_start[3], float ray_normal[3])
+{
+	static int triangle_indexes[12][3] = {{0, 1, 2}, {0, 2, 3},
+										  {3, 2, 6}, {3, 6, 7},
+										  {1, 2, 6}, {1, 6, 5}, 
+										  {5, 6, 7}, {4, 5, 7},
+										  {0, 3, 7}, {0, 4, 7},
+										  {0, 1, 5}, {0, 4, 5}};
+	int result = 0;
+	int i;
+	
+	for (i = 0; i < 12 && result == 0; i++)
+	{
+		float lambda;
+		int v1, v2, v3;
+		v1 = triangle_indexes[i][0];
+		v2 = triangle_indexes[i][1];
+		v3 = triangle_indexes[i][2];
+		result = RayIntersectsTriangle(ray_start, ray_normal, bb->vec[v1], bb->vec[v2], bb->vec[v3], &lambda, NULL);
+	}
+	
+	return result;
+}

Modified: branches/apricot/source/blender/src/transform_snap.c
===================================================================
--- branches/apricot/source/blender/src/transform_snap.c	2008-05-22 14:56:32 UTC (rev 14928)
+++ branches/apricot/source/blender/src/transform_snap.c	2008-05-22 15:20:14 UTC (rev 14929)
@@ -888,88 +888,35 @@
 	float object_depth = FLT_MAX;
 	int retval = 0;
 	int totvert = dm->getNumVerts(dm);
+	int totface = dm->getNumFaces(dm);
 	
 	if (totvert > 0) {
 		float imat[4][4];
+		float ray_start_local[3], ray_normal_local[3];
 		int test = 1;
 
 		Mat4Invert(imat, obmat);
+
+		VECCOPY(ray_start_local, ray_start);
+		VECCOPY(ray_normal_local, ray_normal);
 		
+		Mat4MulVecfl(imat, ray_start_local);
+		Mat4Mul3Vecfl(imat, ray_normal_local);
+		
+		
 		/* If number of vert is more than an arbitrary limit,
 		 * test against boundbox first
 		 * */
-		if (totvert > 16) {
+		if (totface > 16) {
 			struct BoundBox *bb = object_get_boundbox(ob);
-			float camera[3];
-			int outside = 1;
-			
-			VECCOPY(camera, G.vd->viewinv[3]);
-			Mat4MulVecfl(imat, camera);
-
-			/* If we're inside the bounding box, projection is screwy, so test for that */			
-			if (bb->vec[0][0] <= camera[0] && camera[0] <= bb->vec[4][0] && 
-				bb->vec[0][1] <= camera[1] && camera[1] <= bb->vec[2][1] &&
-				bb->vec[0][2] <= camera[2] && camera[2] <= bb->vec[1][2])
-			{
-				outside = 0;
-			}
-			
-			/* only test if camera is outside of the bounding box */			
-			if (outside)
-			{
-				float minx = 0, miny = 0, maxx = 0, maxy = 0;
-				int i;
-				
-				for (i = 0; i < 8; i++) {
-					float gloc[3];
-					float sloc[2];
-					
-					VECCOPY(gloc, bb->vec[i]);
-					Mat4MulVecfl(obmat, gloc);
-					
-					project_float(gloc, sloc);
-					
-					if (i == 0) {
-						minx = maxx = sloc[0];
-						miny = maxy = sloc[1];
-					}
-					else {
-						if (minx > sloc[0]) minx = sloc[0];
-						else if (maxx < sloc[0]) maxx = sloc[0];
-						
-						if (miny > sloc[1]) miny = sloc[1];
-						else if (maxy < sloc[1]) maxy = sloc[1];
-					}
-				}
-				
-				/* Pad with distance */
-		
-				minx -= *dist;
-				miny -= *dist;
-				maxx += *dist;
-				maxy += *dist;
-				
-				if (mval[0] > maxx || mval[0] < minx ||
-					mval[1] > maxy || mval[1] < miny) {
-					
-					test = 0;
-				}
-			}
+			test = ray_hit_boundbox(bb, ray_start_local, ray_normal_local);
 		}
 		
 		if (test == 1) {
 			MVert *verts = dm->getVertArray(dm);
 			MFace *faces = dm->getFaceArray(dm);
-			float ray_start_local[3], ray_normal_local[3];
-			int totface = dm->getNumFaces(dm);
 			int i;
 			
-			VECCOPY(ray_start_local, ray_start);
-			VECCOPY(ray_normal_local, ray_normal);
-			
-			Mat4MulVecfl(imat, ray_start_local);
-			Mat4Mul3Vecfl(imat, ray_normal_local);
-			
 			for( i = 0; i < totface; i++) {
 				MFace *f = faces + i;
 				float lambda;





More information about the Bf-blender-cvs mailing list