[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [59428] trunk/blender/source/blender: simplify dist_to_plane_v3 and add dist_squared_to_plane_v3

Campbell Barton ideasman42 at gmail.com
Fri Aug 23 17:19:20 CEST 2013


Revision: 59428
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=59428
Author:   campbellbarton
Date:     2013-08-23 15:19:20 +0000 (Fri, 23 Aug 2013)
Log Message:
-----------
simplify dist_to_plane_v3 and add dist_squared_to_plane_v3

Modified Paths:
--------------
    trunk/blender/source/blender/blenkernel/intern/camera.c
    trunk/blender/source/blender/blenlib/BLI_math_geom.h
    trunk/blender/source/blender/blenlib/intern/math_geom.c

Modified: trunk/blender/source/blender/blenkernel/intern/camera.c
===================================================================
--- trunk/blender/source/blender/blenkernel/intern/camera.c	2013-08-23 15:17:54 UTC (rev 59427)
+++ trunk/blender/source/blender/blenkernel/intern/camera.c	2013-08-23 15:19:20 UTC (rev 59428)
@@ -459,7 +459,7 @@
 	float plane_tx[4][4];  /* 4 planes (not 4x4 matrix)*/
 	float frame_tx[4][3];
 	float normal_tx[4][3];
-	float dist_vals[4];
+	float dist_vals_sq[4];  /* distance squared (signed) */
 	unsigned int tot;
 } CameraViewFrameData;
 
@@ -469,9 +469,9 @@
 	unsigned int i;
 
 	for (i = 0; i < 4; i++) {
-		float nd = dist_to_plane_v3(co, data->plane_tx[i]);
-		if (nd < data->dist_vals[i]) {
-			data->dist_vals[i] = nd;
+		float nd = dist_squared_to_plane_v3(co, data->plane_tx[i]);
+		if (nd < data->dist_vals_sq[i]) {
+			data->dist_vals_sq[i] = nd;
 		}
 	}
 
@@ -520,10 +520,7 @@
 	}
 
 	/* initialize callback data */
-	data_cb.dist_vals[0] =
-	data_cb.dist_vals[1] =
-	data_cb.dist_vals[2] =
-	data_cb.dist_vals[3] = FLT_MAX;
+	copy_v4_fl(data_cb.dist_vals_sq, FLT_MAX);
 	data_cb.tot = 0;
 	/* run callback on all visible points */
 	BKE_scene_foreach_display_point(scene, v3d, BA_SELECT,
@@ -538,11 +535,16 @@
 
 		float plane_isect_pt_1[3], plane_isect_pt_2[3];
 
+		/* could make a generic macro */
+#define SQRT_SIGNED(f) copysign(sqrtf(fabsf(f)), f)
+
 		/* apply the dist-from-plane's to the transformed plane points */
 		for (i = 0; i < 4; i++) {
-			mul_v3_v3fl(plane_tx[i], data_cb.normal_tx[i], data_cb.dist_vals[i]);
+			mul_v3_v3fl(plane_tx[i], data_cb.normal_tx[i], SQRT_SIGNED(data_cb.dist_vals_sq[i]));
 		}
 
+#undef SQRT_SIGNED
+
 		isect_plane_plane_v3(plane_isect_1, plane_isect_1_no,
 		                     plane_tx[0], data_cb.normal_tx[0],
 		                     plane_tx[2], data_cb.normal_tx[2]);

Modified: trunk/blender/source/blender/blenlib/BLI_math_geom.h
===================================================================
--- trunk/blender/source/blender/blenlib/BLI_math_geom.h	2013-08-23 15:17:54 UTC (rev 59427)
+++ trunk/blender/source/blender/blenlib/BLI_math_geom.h	2013-08-23 15:19:20 UTC (rev 59428)
@@ -81,6 +81,7 @@
 float         dist_to_line_segment_v2(const float p[2], const float l1[2], const float l2[2]);
 void closest_to_line_segment_v2(float closest[2], const float p[2], const float l1[2], const float l2[2]);
 
+float dist_squared_to_plane_v3(const float p[3], const float plane[4]);
 float dist_to_plane_v3(const float p[3], const float plane[4]);
 float dist_squared_to_line_segment_v3(const float p[3], const float l1[3], const float l2[3]);
 float         dist_to_line_segment_v3(const float p[3], const float l1[3], const float l2[3]);

Modified: trunk/blender/source/blender/blenlib/intern/math_geom.c
===================================================================
--- trunk/blender/source/blender/blenlib/intern/math_geom.c	2013-08-23 15:17:54 UTC (rev 59427)
+++ trunk/blender/source/blender/blenlib/intern/math_geom.c	2013-08-23 15:19:20 UTC (rev 59428)
@@ -321,17 +321,23 @@
 	madd_v3_v3v3fl(close_r, pt, plane, -side / length);
 }
 
+float dist_squared_to_plane_v3(const float pt[3], const float plane[4])
+{
+	const float length = len_squared_v3(plane);
+	const float side = plane_point_side_v3(plane, pt);
+	const float fac = side / length;
+	return copysign(length * (fac * fac), side);
+}
+
+/**
+ * Return the signed distance from the point to the plane.
+ */
 float dist_to_plane_v3(const float pt[3], const float plane[4])
 {
-	float close[3];
-
-	/* same logic as closest_to_plane_v3() */
 	const float length = len_squared_v3(plane);
 	const float side = plane_point_side_v3(plane, pt);
-	madd_v3_v3v3fl(close, pt, plane, -side / length);
-	/* end same logic */
-
-	return copysign(len_v3v3(pt, close), side);
+	const float fac = side / length;
+	return sqrtf(length) * fac;
 }
 
 /* distance v1 to line-piece l1-l2 in 3D */




More information about the Bf-blender-cvs mailing list