|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include "bioimage_cpp/detail/mutex_storage.hxx" |
| 4 | +#include "bioimage_cpp/detail/relabel.hxx" |
| 5 | +#include "bioimage_cpp/detail/union_find.hxx" |
| 6 | +#include "bioimage_cpp/graph/undirected_graph.hxx" |
| 7 | + |
| 8 | +#include <algorithm> |
| 9 | +#include <array> |
| 10 | +#include <cstddef> |
| 11 | +#include <cstdint> |
| 12 | +#include <stdexcept> |
| 13 | +#include <string> |
| 14 | +#include <vector> |
| 15 | + |
| 16 | +namespace bioimage_cpp::graph { |
| 17 | + |
| 18 | +// Mutex watershed clustering on an arbitrary undirected graph. |
| 19 | +// |
| 20 | +// `graph` defines the attractive edges with one cost per edge in `edge_costs`. |
| 21 | +// `mutex_uvs` defines the long-range repulsive (mutex) edges as (u, v) pairs |
| 22 | +// with one cost per edge in `mutex_costs`. Higher costs win — they are |
| 23 | +// processed first when sorting jointly by descending weight. |
| 24 | +// |
| 25 | +// Returns dense node labels in [0, k) following first-occurrence order. |
| 26 | +// |
| 27 | +// This is a port of `compute_mws_clustering` from the affogato library, |
| 28 | +// adapted to bioimage-cpp's UndirectedGraph and detail/ primitives. |
| 29 | +inline std::vector<std::uint64_t> mutex_watershed_clustering( |
| 30 | + const UndirectedGraph &graph, |
| 31 | + const std::vector<double> &edge_costs, |
| 32 | + const std::vector<std::array<std::uint64_t, 2>> &mutex_uvs, |
| 33 | + const std::vector<double> &mutex_costs |
| 34 | +) { |
| 35 | + const auto number_of_edges = static_cast<std::size_t>(graph.number_of_edges()); |
| 36 | + if (edge_costs.size() != number_of_edges) { |
| 37 | + throw std::invalid_argument( |
| 38 | + "edge_costs size must match graph.number_of_edges(), got edge_costs size=" + |
| 39 | + std::to_string(edge_costs.size()) + |
| 40 | + ", number_of_edges=" + std::to_string(number_of_edges) |
| 41 | + ); |
| 42 | + } |
| 43 | + if (mutex_costs.size() != mutex_uvs.size()) { |
| 44 | + throw std::invalid_argument( |
| 45 | + "mutex_costs size must match mutex_uvs size, got mutex_costs size=" + |
| 46 | + std::to_string(mutex_costs.size()) + |
| 47 | + ", mutex_uvs size=" + std::to_string(mutex_uvs.size()) |
| 48 | + ); |
| 49 | + } |
| 50 | + |
| 51 | + const auto number_of_nodes = static_cast<std::size_t>(graph.number_of_nodes()); |
| 52 | + const auto number_of_mutex = mutex_uvs.size(); |
| 53 | + |
| 54 | + for (std::size_t index = 0; index < number_of_mutex; ++index) { |
| 55 | + const auto u = mutex_uvs[index][0]; |
| 56 | + const auto v = mutex_uvs[index][1]; |
| 57 | + if (u >= number_of_nodes || v >= number_of_nodes) { |
| 58 | + throw std::invalid_argument( |
| 59 | + "mutex_uvs endpoints must be < number_of_nodes, got u=" + |
| 60 | + std::to_string(u) + ", v=" + std::to_string(v) + |
| 61 | + ", number_of_nodes=" + std::to_string(number_of_nodes) |
| 62 | + ); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + struct WeightedEdge { |
| 67 | + double weight; |
| 68 | + std::uint64_t index; |
| 69 | + bool is_mutex; |
| 70 | + }; |
| 71 | + |
| 72 | + std::vector<WeightedEdge> edge_order; |
| 73 | + edge_order.reserve(number_of_edges + number_of_mutex); |
| 74 | + for (std::size_t index = 0; index < number_of_edges; ++index) { |
| 75 | + edge_order.push_back( |
| 76 | + WeightedEdge{edge_costs[index], static_cast<std::uint64_t>(index), false} |
| 77 | + ); |
| 78 | + } |
| 79 | + for (std::size_t index = 0; index < number_of_mutex; ++index) { |
| 80 | + edge_order.push_back( |
| 81 | + WeightedEdge{mutex_costs[index], static_cast<std::uint64_t>(index), true} |
| 82 | + ); |
| 83 | + } |
| 84 | + |
| 85 | + std::sort(edge_order.begin(), edge_order.end(), [](const auto &first, const auto &second) { |
| 86 | + if (first.weight != second.weight) { |
| 87 | + return first.weight > second.weight; |
| 88 | + } |
| 89 | + if (first.is_mutex != second.is_mutex) { |
| 90 | + return !first.is_mutex; |
| 91 | + } |
| 92 | + return first.index < second.index; |
| 93 | + }); |
| 94 | + |
| 95 | + bioimage_cpp::detail::UnionFind sets(number_of_nodes); |
| 96 | + MutexStorage mutexes(number_of_nodes); |
| 97 | + |
| 98 | + for (const auto &edge : edge_order) { |
| 99 | + std::uint64_t u; |
| 100 | + std::uint64_t v; |
| 101 | + if (edge.is_mutex) { |
| 102 | + const auto &pair = mutex_uvs[static_cast<std::size_t>(edge.index)]; |
| 103 | + u = pair[0]; |
| 104 | + v = pair[1]; |
| 105 | + } else { |
| 106 | + const auto uv = graph.uv(edge.index); |
| 107 | + u = uv.first; |
| 108 | + v = uv.second; |
| 109 | + } |
| 110 | + if (u == v) { |
| 111 | + continue; |
| 112 | + } |
| 113 | + |
| 114 | + const auto root_u = sets.find(u); |
| 115 | + const auto root_v = sets.find(v); |
| 116 | + if (root_u == root_v) { |
| 117 | + continue; |
| 118 | + } |
| 119 | + |
| 120 | + if (edge.is_mutex) { |
| 121 | + insert_mutex(root_u, root_v, mutexes); |
| 122 | + } else { |
| 123 | + if (check_mutex(root_u, root_v, mutexes)) { |
| 124 | + continue; |
| 125 | + } |
| 126 | + const auto new_root = sets.unite_roots(root_u, root_v); |
| 127 | + const auto old_root = (new_root == root_u) ? root_v : root_u; |
| 128 | + merge_mutexes(old_root, new_root, mutexes); |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + std::vector<std::uint64_t> roots(number_of_nodes); |
| 133 | + for (std::size_t node = 0; node < number_of_nodes; ++node) { |
| 134 | + roots[node] = sets.find(static_cast<std::uint64_t>(node)); |
| 135 | + } |
| 136 | + return bioimage_cpp::detail::dense_relabel(roots); |
| 137 | +} |
| 138 | + |
| 139 | +} // namespace bioimage_cpp::graph |
0 commit comments