[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [59719] trunk/blender/source/blender: use strict flags for kdtree, and replace ints with unsigned ints where possible.

Campbell Barton ideasman42 at gmail.com
Sun Sep 1 22:17:57 CEST 2013


Revision: 59719
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=59719
Author:   campbellbarton
Date:     2013-09-01 20:17:56 +0000 (Sun, 01 Sep 2013)
Log Message:
-----------
use strict flags for kdtree, and replace ints with unsigned ints where possible.
also replace callocs with mallocs since zeroing memory can be avoided.

Modified Paths:
--------------
    trunk/blender/source/blender/blenkernel/intern/boids.c
    trunk/blender/source/blender/blenkernel/intern/dynamicpaint.c
    trunk/blender/source/blender/blenkernel/intern/particle_system.c
    trunk/blender/source/blender/blenlib/BLI_kdtree.h
    trunk/blender/source/blender/blenlib/intern/BLI_kdtree.c
    trunk/blender/source/blender/editors/physics/particle_edit.c

Modified: trunk/blender/source/blender/blenkernel/intern/boids.c
===================================================================
--- trunk/blender/source/blender/blenkernel/intern/boids.c	2013-09-01 18:19:59 UTC (rev 59718)
+++ trunk/blender/source/blender/blenkernel/intern/boids.c	2013-09-01 20:17:56 UTC (rev 59719)
@@ -253,7 +253,8 @@
 
 	//check boids in own system
 	if (acbr->options & BRULE_ACOLL_WITH_BOIDS) {
-		neighbors = BLI_kdtree_range_search(bbd->sim->psys->tree, acbr->look_ahead * len_v3(pa->prev_state.vel), pa->prev_state.co, pa->prev_state.ave, &ptn);
+		neighbors = BLI_kdtree_range_search(bbd->sim->psys->tree, pa->prev_state.co, pa->prev_state.ave,
+		                                    &ptn, acbr->look_ahead * len_v3(pa->prev_state.vel));
 		if (neighbors > 1) for (n=1; n<neighbors; n++) {
 			copy_v3_v3(co1, pa->prev_state.co);
 			copy_v3_v3(vel1, pa->prev_state.vel);
@@ -299,7 +300,8 @@
 		ParticleSystem *epsys = psys_get_target_system(bbd->sim->ob, pt);
 
 		if (epsys) {
-			neighbors = BLI_kdtree_range_search(epsys->tree, acbr->look_ahead * len_v3(pa->prev_state.vel), pa->prev_state.co, pa->prev_state.ave, &ptn);
+			neighbors = BLI_kdtree_range_search(epsys->tree, pa->prev_state.co, pa->prev_state.ave,
+			                                    &ptn, acbr->look_ahead * len_v3(pa->prev_state.vel));
 			if (neighbors > 0) for (n=0; n<neighbors; n++) {
 				copy_v3_v3(co1, pa->prev_state.co);
 				copy_v3_v3(vel1, pa->prev_state.vel);
@@ -354,7 +356,8 @@
 	ParticleTarget *pt;
 	float len = 2.0f * val->personal_space * pa->size + 1.0f;
 	float vec[3] = {0.0f, 0.0f, 0.0f};
-	int neighbors = BLI_kdtree_range_search(bbd->sim->psys->tree, 2.0f * val->personal_space * pa->size, pa->prev_state.co, NULL, &ptn);
+	int neighbors = BLI_kdtree_range_search(bbd->sim->psys->tree, pa->prev_state.co, NULL,
+	                                        &ptn, 2.0f * val->personal_space * pa->size);
 	int ret = 0;
 
 	if (neighbors > 1 && ptn[1].dist!=0.0f) {
@@ -372,7 +375,8 @@
 		ParticleSystem *epsys = psys_get_target_system(bbd->sim->ob, pt);
 
 		if (epsys) {
-			neighbors = BLI_kdtree_range_search(epsys->tree, 2.0f * val->personal_space * pa->size, pa->prev_state.co, NULL, &ptn);
+			neighbors = BLI_kdtree_range_search(epsys->tree, pa->prev_state.co, NULL,
+			                                    &ptn, 2.0f * val->personal_space * pa->size);
 			
 			if (neighbors > 0 && ptn[0].dist < len) {
 				sub_v3_v3v3(vec, pa->prev_state.co, ptn[0].co);
@@ -392,7 +396,7 @@
 {
 	KDTreeNearest ptn[11];
 	float vec[3] = {0.0f, 0.0f, 0.0f}, loc[3] = {0.0f, 0.0f, 0.0f};
-	int neighbors = BLI_kdtree_find_n_nearest(bbd->sim->psys->tree, 11, pa->state.co, pa->prev_state.ave, ptn);
+	int neighbors = BLI_kdtree_find_nearest_n(bbd->sim->psys->tree, pa->state.co, pa->prev_state.ave, ptn, 11);
 	int n;
 	int ret = 0;
 
@@ -619,7 +623,8 @@
 	int n, ret = 0;
 
 	/* calculate own group strength */
-	int neighbors = BLI_kdtree_range_search(bbd->sim->psys->tree, fbr->distance, pa->prev_state.co, NULL, &ptn);
+	int neighbors = BLI_kdtree_range_search(bbd->sim->psys->tree, pa->prev_state.co, NULL,
+	                                        &ptn, fbr->distance);
 	for (n=0; n<neighbors; n++) {
 		bpa = bbd->sim->psys->particles[ptn[n].index].boid;
 		health += bpa->data.health;
@@ -635,7 +640,8 @@
 		if (epsys) {
 			epars = epsys->particles;
 
-			neighbors = BLI_kdtree_range_search(epsys->tree, fbr->distance, pa->prev_state.co, NULL, &ptn);
+			neighbors = BLI_kdtree_range_search(epsys->tree, pa->prev_state.co, NULL,
+			                                    &ptn, fbr->distance);
 			
 			health = 0.0f;
 

Modified: trunk/blender/source/blender/blenkernel/intern/dynamicpaint.c
===================================================================
--- trunk/blender/source/blender/blenkernel/intern/dynamicpaint.c	2013-09-01 18:19:59 UTC (rev 59718)
+++ trunk/blender/source/blender/blenkernel/intern/dynamicpaint.c	2013-09-01 20:17:56 UTC (rev 59719)
@@ -3802,14 +3802,15 @@
 					 */
 					KDTreeNearest *nearest;
 
-					int n, particles = 0;
+					int n, particles;
 					float smooth_range = smooth * (1.0f - strength), dist;
 					/* calculate max range that can have particles with higher influence than the nearest one */
 					float max_range = smooth - strength * smooth + solidradius;
 					/* Make gcc happy! */
 					dist = max_range;
 
-					particles = BLI_kdtree_range_search(tree, max_range, bData->realCoord[bData->s_pos[index]].v, NULL, &nearest);
+					particles = BLI_kdtree_range_search(tree, bData->realCoord[bData->s_pos[index]].v, NULL,
+					                                    &nearest, max_range);
 
 					/* Find particle that produces highest influence */
 					for (n = 0; n < particles; n++) {

Modified: trunk/blender/source/blender/blenkernel/intern/particle_system.c
===================================================================
--- trunk/blender/source/blender/blenkernel/intern/particle_system.c	2013-09-01 18:19:59 UTC (rev 59718)
+++ trunk/blender/source/blender/blenkernel/intern/particle_system.c	2013-09-01 20:17:56 UTC (rev 59719)
@@ -815,7 +815,7 @@
 
 			psys_particle_on_dm(ctx->dm,from,pa->num,pa->num_dmcache,pa->fuv,pa->foffset,co1,0,0,0,orco1,0);
 			BKE_mesh_orco_verts_transform((Mesh*)ob->data, &orco1, 1, 1);
-			maxw = BLI_kdtree_find_n_nearest(ctx->tree,3,orco1,NULL,ptn);
+			maxw = BLI_kdtree_find_nearest_n(ctx->tree,orco1,NULL,ptn,3);
 
 			for (w=0; w<maxw; w++) {
 				pa->verts[w]=ptn->num;
@@ -940,7 +940,7 @@
 
 			psys_particle_on_dm(dm,cfrom,cpa->num,DMCACHE_ISCHILD,cpa->fuv,cpa->foffset,co1,nor1,NULL,NULL,orco1,NULL);
 			BKE_mesh_orco_verts_transform((Mesh*)ob->data, &orco1, 1, 1);
-			maxw = BLI_kdtree_find_n_nearest(ctx->tree,4,orco1,NULL,ptn);
+			maxw = BLI_kdtree_find_nearest_n(ctx->tree,orco1,NULL,ptn,3);
 
 			maxd=ptn[maxw-1].dist;
 			/* mind=ptn[0].dist; */ /* UNUSED */

Modified: trunk/blender/source/blender/blenlib/BLI_kdtree.h
===================================================================
--- trunk/blender/source/blender/blenlib/BLI_kdtree.h	2013-09-01 18:19:59 UTC (rev 59718)
+++ trunk/blender/source/blender/blenlib/BLI_kdtree.h	2013-09-01 20:17:56 UTC (rev 59719)
@@ -31,6 +31,8 @@
  *  \author Brecht van Lommel
  */
 
+#include "BLI_compiler_attrs.h"
+
 struct KDTree;
 typedef struct KDTree KDTree;
 
@@ -40,22 +42,19 @@
 	float co[3];
 } KDTreeNearest;
 
-/* Creates or free a kdtree */
-KDTree *BLI_kdtree_new(int maxsize);
+KDTree *BLI_kdtree_new(unsigned int maxsize);
 void BLI_kdtree_free(KDTree *tree);
 
-/* Construction: first insert points, then call balance. Normal is optional. */
-void BLI_kdtree_insert(KDTree *tree, int index, const float co[3], const float nor[3]);
-void BLI_kdtree_balance(KDTree *tree);
+void BLI_kdtree_insert(KDTree *tree, int index, const float co[3], const float nor[3]) ATTR_NONNULL(1, 3);
+void BLI_kdtree_balance(KDTree *tree) ATTR_NONNULL(1);
 
-/* Find nearest returns index, and -1 if no node is found.
- * Find n nearest returns number of points found, with results in nearest.
- * Normal is optional, but if given will limit results to points in normal direction from co. */
-int BLI_kdtree_find_nearest(KDTree *tree, const float co[3], const float nor[3], KDTreeNearest *nearest);
-int BLI_kdtree_find_n_nearest(KDTree *tree, int n, const float co[3], const float nor[3], KDTreeNearest *nearest);
+int BLI_kdtree_find_nearest(KDTree *tree, const float co[3], const float nor[3],
+                            KDTreeNearest *r_nearest) ATTR_NONNULL(1, 2);
+int BLI_kdtree_find_nearest_n(KDTree *tree, const float co[3], const float nor[3],
+                              KDTreeNearest *r_nearest,
+                              unsigned int n) ATTR_NONNULL(1, 2, 4);
+int BLI_kdtree_range_search(KDTree *tree, const float co[3], const float nor[3],
+                            KDTreeNearest **r_nearest,
+                            float range) ATTR_NONNULL(1, 2, 4) ATTR_WARN_UNUSED_RESULT;
 
-/* Range search returns number of points found, with results in nearest */
-/* Normal is optional, but if given will limit results to points in normal direction from co. */
-/* Remember to free nearest after use! */
-int BLI_kdtree_range_search(KDTree *tree, float range, const float co[3], const float nor[3], KDTreeNearest **nearest);
-#endif
+#endif  /* __BLI_KDTREE_H__ */

Modified: trunk/blender/source/blender/blenlib/intern/BLI_kdtree.c
===================================================================
--- trunk/blender/source/blender/blenlib/intern/BLI_kdtree.c	2013-09-01 18:19:59 UTC (rev 59718)
+++ trunk/blender/source/blender/blenlib/intern/BLI_kdtree.c	2013-09-01 20:17:56 UTC (rev 59719)
@@ -30,18 +30,19 @@
 #include "BLI_math.h"
 #include "BLI_kdtree.h"
 #include "BLI_utildefines.h"
+#include "BLI_strict_flags.h"
 
 
 typedef struct KDTreeNode {
 	struct KDTreeNode *left, *right;
 	float co[3], nor[3];
 	int index;
-	short d;
+	unsigned int d;  /* range is only (0-2) */
 } KDTreeNode;
 
 struct KDTree {
 	KDTreeNode *nodes;
-	int totnode;
+	unsigned int totnode;
 	KDTreeNode *root;
 };
 
@@ -49,13 +50,17 @@
 #define KD_NEAR_ALLOC_INC 100  /* alloc increment for collecting nearest */
 #define KD_FOUND_ALLOC_INC 50  /* alloc increment for collecting nearest */
 
-KDTree *BLI_kdtree_new(int maxsize)
+/**
+ * Creates or free a kdtree
+ */
+KDTree *BLI_kdtree_new(unsigned int maxsize)
 {
 	KDTree *tree;
 
-	tree = MEM_callocN(sizeof(KDTree), "KDTree");
-	tree->nodes = MEM_callocN(sizeof(KDTreeNode) * maxsize, "KDTreeNode");
+	tree = MEM_mallocN(sizeof(KDTree), "KDTree");
+	tree->nodes = MEM_mallocN(sizeof(KDTreeNode) * maxsize, "KDTreeNode");
 	tree->totnode = 0;
+	tree->root = NULL;
 
 	return tree;
 }
@@ -68,20 +73,32 @@
 	}
 }
 
+/**
+ * Construction: first insert points, then call balance. Normal is optional.
+ */
 void BLI_kdtree_insert(KDTree *tree, int index, const float co[3], const float nor[3])
 {
 	KDTreeNode *node = &tree->nodes[tree->totnode++];
 
+	/* note, array isn't calloc'd,
+	 * need to initialize all struct members */
+
+	node->left = node->right = NULL;
+	copy_v3_v3(node->co, co);
+	if (nor)
+		copy_v3_v3(node->nor, nor);
+	else
+		zero_v3(node->nor);
+
 	node->index = index;
-	copy_v3_v3(node->co, co);
-	if (nor) copy_v3_v3(node->nor, nor);
+	node->d = 0;
 }
 
-static KDTreeNode *kdtree_balance(KDTreeNode *nodes, int totnode, int axis)

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list