Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Look at scipy mergetabel #92

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions include/GooseEYE/GooseEYE.h
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,7 @@ class ClusterLabeller {
*/
std::vector<ptrdiff_t> m_next;
std::vector<ptrdiff_t> m_connected; ///< List of labels connected to the current block.
std::vector<ptrdiff_t> m_mergetable; ///< ??

public:
/**
Expand Down Expand Up @@ -493,6 +494,7 @@ class ClusterLabeller {
{
static_assert(Dim == 1 || Dim == 2, "WIP: 1d and 2d supported.");
m_label = xt::empty<ptrdiff_t>(shape);
m_mergetable.resize(m_label.size() + 1);
m_renum.resize(m_label.size() + 1);
m_next.resize(m_label.size() + 1);
for (size_t i = 0; i < Dim; ++i) {
Expand Down Expand Up @@ -613,6 +615,46 @@ class ClusterLabeller {
m_nmerge = 0;
}

/**
* https://github.com/scipy/scipy/blob/v1.11.4/scipy/ndimage/src/_ni_label.pyx
*/
ptrdiff_t mark_for_merge(ptrdiff_t a, ptrdiff_t b)
{
ptrdiff_t orig_a = a;
ptrdiff_t orig_b = b;
ptrdiff_t minlabel;

// find smallest root for each of a and b
while (a != m_mergetable[a]) {
a = m_mergetable[a];
}
while (b != m_mergetable[b]) {
b = m_mergetable[b];
}

if (a < b) {
minlabel = a;
}
else {
minlabel = b;
}

// merge roots
m_mergetable[a] = m_mergetable[b] = minlabel;

// merge every step to minlabel
a = orig_a;
b = orig_b;
while (a != minlabel) {
a, m_mergetable[a] = m_mergetable[a], minlabel;
}
while (b != minlabel) {
b, m_mergetable[b] = m_mergetable[b], minlabel;
}

return minlabel;
}

void label_impl(size_t idx)
{
static_assert(Dim == 1 || Dim == 2, "WIP: 1d and 2d supported.");
Expand Down Expand Up @@ -652,6 +694,7 @@ class ClusterLabeller {

if (nconnected == 0) {
m_label.flat(idx) = m_new_label;
m_mergetable[m_new_label] = m_new_label;
m_new_label += 1;
return;
}
Expand Down
Loading