Skip to content

Commit

Permalink
perf: reduce memory usage for 26-connected
Browse files Browse the repository at this point in the history
  • Loading branch information
william-silversmith committed May 21, 2024
1 parent f241887 commit daa2d24
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions cc3d_graphs.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,22 @@ OUT* color_connectivity_graph_26(
throw new std::runtime_error("26-connectivity requires a 32-bit voxel graph for color_connectivity_graph_26.");
}

template <typename T>
uint64_t estimate_vcg_provisional_label_count(
T* vcg, const int64_t sx, const int64_t voxels
) {
uint64_t count = 0; // number of transitions between labels

for (int64_t row = 0, loc = 0; loc < voxels; loc += sx, row++) {
count++;
for (int64_t x = 1; x < sx; x++) {
count += static_cast<uint64_t>((vcg[loc + x] & 0b10) == 0);
}
}

return count;
}

template <typename OUT>
OUT* color_connectivity_graph_26(
const uint32_t* vcg, // voxel connectivity graph
Expand All @@ -463,6 +479,7 @@ OUT* color_connectivity_graph_26(

uint64_t max_labels = static_cast<uint64_t>(voxels) + 1; // + 1L for an array with no zeros
max_labels = std::min(max_labels, static_cast<uint64_t>(std::numeric_limits<OUT>::max()));
max_labels = std::min(max_labels, estimate_vcg_provisional_label_count(vcg, sx, voxels) + 1);

if (out_labels == NULL) {
out_labels = new OUT[voxels]();
Expand Down

0 comments on commit daa2d24

Please sign in to comment.