From 53ab5f85ebdf8e7717a3460d14a3a780b4fadfa2 Mon Sep 17 00:00:00 2001 From: William Silversmith Date: Tue, 2 Apr 2024 21:19:05 -0400 Subject: [PATCH] fix: error in surface area calculation --- automated_test.py | 8 +++++++- cc3d_graphs.hpp | 19 ++++++------------- 2 files changed, 13 insertions(+), 14 deletions(-) diff --git a/automated_test.py b/automated_test.py index 59a23eb..642ead5 100644 --- a/automated_test.py +++ b/automated_test.py @@ -723,7 +723,13 @@ def test_contacts_surface_area(): assert res[(1,3)] == 1 assert res[(1,4)] == 1 assert res[(1,5)] == 1 - + + data = np.ones((3,3,3)) + data[1,1,1] = 2 + labels_out = cc3d.connected_components(data, connectivity=6) + res = cc3d.contacts(labels_out, connectivity=6, surface_area=True) + assert res[(1,2)] == 6 + def test_contacts_26(): labels = np.zeros( (10, 10, 10), dtype=np.uint32 ) diff --git a/cc3d_graphs.hpp b/cc3d_graphs.hpp index 1bf3b7a..591a551 100644 --- a/cc3d_graphs.hpp +++ b/cc3d_graphs.hpp @@ -317,7 +317,6 @@ extract_region_graph( T cur = 0; T label = 0; - T last_label = 0; std::unordered_map, float, pair_hash> edges; @@ -332,25 +331,19 @@ extract_region_graph( } compute_neighborhood(neighborhood, x, y, z, sx, sy, sz, connectivity); - - last_label = cur; for (int i = 0; i < connectivity / 2; i++) { int64_t neighboridx = loc + neighborhood[i]; label = labels[neighboridx]; - if (label == 0 || label == last_label) { + if (label == 0) { continue; } - else if (label != cur) { - if (cur > label) { - edges[std::pair(label, cur)] += areas[i]; - } - else { - edges[std::pair(cur, label)] += areas[i]; - } - - last_label = label; + else if (cur > label) { + edges[std::pair(label, cur)] += areas[i]; + } + else if (cur < label) { + edges[std::pair(cur, label)] += areas[i]; } } }