[Bf-blender-cvs] [d3c815bd083] blender2.8: BLI_heap: add an API function to directly read the top node value.

Alexander Gavrilov noreply at git.blender.org
Sun Nov 4 11:40:36 CET 2018


Commit: d3c815bd0831f46c18bb4bddc9f290b5f47e2c45
Author: Alexander Gavrilov
Date:   Sun Nov 4 13:27:10 2018 +0300
Branches: blender2.8
https://developer.blender.org/rBd3c815bd0831f46c18bb4bddc9f290b5f47e2c45

BLI_heap: add an API function to directly read the top node value.

It is very commonly needed in loop conditions to check if
the items in the heap are good enough to continue.

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

M	source/blender/blenkernel/intern/colorband.c
M	source/blender/blenlib/BLI_heap.h
M	source/blender/blenlib/intern/BLI_heap.c
M	source/blender/bmesh/tools/bmesh_decimate_collapse.c
M	source/blender/editors/mesh/editmesh_tools.c
M	tests/gtests/blenlib/BLI_heap_test.cc

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

diff --git a/source/blender/blenkernel/intern/colorband.c b/source/blender/blenkernel/intern/colorband.c
index f72d4df725a..38631c76009 100644
--- a/source/blender/blenkernel/intern/colorband.c
+++ b/source/blender/blenkernel/intern/colorband.c
@@ -208,7 +208,7 @@ static void colorband_init_from_table_rgba_resample(
 	}
 
 	while ((carr_len > 1 && !BLI_heap_is_empty(heap)) &&
-	       ((carr_len >= MAXCOLORBAND) || (BLI_heap_node_value(BLI_heap_top(heap)) <= eps_2x)))
+	       ((carr_len >= MAXCOLORBAND) || (BLI_heap_top_value(heap) <= eps_2x)))
 	{
 		c = BLI_heap_pop_min(heap);
 		struct ColorResampleElem *c_next = c->next, *c_prev = c->prev;
diff --git a/source/blender/blenlib/BLI_heap.h b/source/blender/blenlib/BLI_heap.h
index 771b9dabe4d..35c8df3075c 100644
--- a/source/blender/blenlib/BLI_heap.h
+++ b/source/blender/blenlib/BLI_heap.h
@@ -43,6 +43,7 @@ void            BLI_heap_remove(Heap *heap, HeapNode *node) ATTR_NONNULL(1, 2);
 bool            BLI_heap_is_empty(const Heap *heap) ATTR_NONNULL(1);
 unsigned int    BLI_heap_len(const Heap *heap) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1);
 HeapNode       *BLI_heap_top(const Heap *heap) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1);
+float           BLI_heap_top_value(const Heap *heap) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1);
 void           *BLI_heap_pop_min(Heap *heap) ATTR_NONNULL(1);
 void            BLI_heap_node_value_update(Heap *heap, HeapNode *node, float value) ATTR_NONNULL(1, 2);
 void            BLI_heap_node_value_update_ptr(Heap *heap, HeapNode *node, float value, void *ptr) ATTR_NONNULL(1, 2);
diff --git a/source/blender/blenlib/intern/BLI_heap.c b/source/blender/blenlib/intern/BLI_heap.c
index 5658c1fd103..17a15f93266 100644
--- a/source/blender/blenlib/intern/BLI_heap.c
+++ b/source/blender/blenlib/intern/BLI_heap.c
@@ -317,6 +317,17 @@ HeapNode *BLI_heap_top(const Heap *heap)
 	return heap->tree[0];
 }
 
+/**
+ * Return the value of top node of the heap.
+ * This is the node with the lowest value.
+ */
+float BLI_heap_top_value(const Heap *heap)
+{
+	BLI_assert(heap->size != 0);
+
+	return heap->tree[0]->value;
+}
+
 /**
  * Pop the top node off the heap and return it's pointer.
  */
diff --git a/source/blender/bmesh/tools/bmesh_decimate_collapse.c b/source/blender/bmesh/tools/bmesh_decimate_collapse.c
index c1b2bc2625b..fa427b3b9eb 100644
--- a/source/blender/bmesh/tools/bmesh_decimate_collapse.c
+++ b/source/blender/bmesh/tools/bmesh_decimate_collapse.c
@@ -1354,7 +1354,7 @@ void BM_mesh_decimate_collapse(
 		/* simple non-mirror case */
 		while ((bm->totface > face_tot_target) &&
 		       (BLI_heap_is_empty(eheap) == false) &&
-		       (BLI_heap_node_value(BLI_heap_top(eheap)) != COST_INVALID))
+		       (BLI_heap_top_value(eheap) != COST_INVALID))
 		{
 			// const float value = BLI_heap_node_value(BLI_heap_top(eheap));
 			BMEdge *e = BLI_heap_pop_min(eheap);
@@ -1379,7 +1379,7 @@ void BM_mesh_decimate_collapse(
 	else {
 		while ((bm->totface > face_tot_target) &&
 		       (BLI_heap_is_empty(eheap) == false) &&
-		       (BLI_heap_node_value(BLI_heap_top(eheap)) != COST_INVALID))
+		       (BLI_heap_top_value(eheap) != COST_INVALID))
 		{
 			/**
 			 * \note
diff --git a/source/blender/editors/mesh/editmesh_tools.c b/source/blender/editors/mesh/editmesh_tools.c
index c11aefb7e14..b16195023c9 100644
--- a/source/blender/editors/mesh/editmesh_tools.c
+++ b/source/blender/editors/mesh/editmesh_tools.c
@@ -7868,10 +7868,10 @@ static int edbm_average_normals_exec(bContext *C, wmOperator *op)
 
 					BLI_SMALLSTACK_DECLARE(loops, BMLoop *);
 					float wnor[3], avg_normal[3] = { 0.0f }, count = 0;
-					float val = BLI_heap_node_value(BLI_heap_top(loop_weight));
+					float val = BLI_heap_top_value(loop_weight);
 
 					while (!BLI_heap_is_empty(loop_weight)) {
-						const float cur_val = BLI_heap_node_value(BLI_heap_top(loop_weight));
+						const float cur_val = BLI_heap_top_value(loop_weight);
 						if (!compare_ff(val, cur_val, threshold)) {
 							count++;
 							val = cur_val;
diff --git a/tests/gtests/blenlib/BLI_heap_test.cc b/tests/gtests/blenlib/BLI_heap_test.cc
index 69566d8dca6..26f3aa19b9f 100644
--- a/tests/gtests/blenlib/BLI_heap_test.cc
+++ b/tests/gtests/blenlib/BLI_heap_test.cc
@@ -176,6 +176,7 @@ static void random_heap_reinsert_helper(
 	for (int out_test = 0; out_test < items_total; out_test++) {
 		HeapNode *node_top = BLI_heap_top(heap);
 		float out = BLI_heap_node_value(node_top);
+		EXPECT_EQ(out, BLI_heap_top_value(heap));
 		EXPECT_EQ((float)out_test, out);
 		BLI_heap_pop_min(heap);
 	}



More information about the Bf-blender-cvs mailing list