83 std::size_t treeMutationVersion_ = 0;
90 AltitudeView view()
const noexcept {
return view_; }
105 template <std::
floating_po
int Real>
static void requireAttributePointer(
const Real* attribute,
const char* context) {
107 throw std::invalid_argument(std::string(context) +
" requires a non-null attribute buffer."));
117 template <detail::NodePreservationDecisions Mask>
118 static void requireNodePreservationMaskSize(
const MorphologicalTree& tree,
const Mask& nodePreservationMask,
119 const char* context) {
121 throw std::invalid_argument(std::string(context) +
122 " node-preservation-mask size must match the internal node slot count."));
132 template <
typename TImagePtr>
static void requireOutputImage(
const MorphologicalTree& tree,
const TImagePtr& image,
const char* context) {
133 MMCFILTERS_CONTRACT_REQUIRE(image !=
nullptr,
throw std::invalid_argument(std::string(context) +
" requires a non-null output image."));
135 throw std::invalid_argument(std::string(context) +
" output image shape must match the tree image domain."));
145 static T altitudeOf(
const AltitudeView& view,
NodeId nodeId)
noexcept {
return view.nodeAltitudes()[
static_cast<std::size_t
>(nodeId)]; }
158 template <
typename TValue>
static void writeProperParts(
const MorphologicalTree& tree,
NodeId nodeId, TValue* output, TValue value) {
159 for (
PixelId pixel : detail::TreeKernelAccess::properParts(tree, nodeId)) {
160 output[pixel] = value;
176 template <
typename TValue>
static void writeSubtreeProperParts(
const MorphologicalTree& tree,
NodeId nodeId, TValue* output, TValue value) {
177 for (
NodeId subtreeNodeId : detail::TreeKernelAccess::subtree(tree, nodeId)) {
178 writeProperParts(tree, subtreeNodeId, output, value);
195 template <detail::NodePreservationDecisions Mask>
196 static void filteringByPruningMinMaskImpl(AltitudeView view,
const Mask& nodePreservationMask,
ImagePtr<T> imgOutputPtr) {
197 const char* context =
"AttributeFilters::filteringByPruningMin";
198 view.requireTopologyUnchanged(context);
200 requireNodePreservationMaskSize(tree, nodePreservationMask, context);
201 requireOutputImage(tree, imgOutputPtr, context);
202 std::stack<NodeId> stack;
203 stack.push(tree.
root());
204 auto imgOutput = imgOutputPtr->rawData();
206 while (!stack.empty()) {
207 const NodeId nodeId = stack.top();
209 writeProperParts(tree, nodeId, imgOutput, altitudeOf(view, nodeId));
210 for (
NodeId childNodeId : detail::TreeKernelAccess::children(tree, nodeId)) {
211 if (nodePreservationMask[
static_cast<std::size_t
>(childNodeId)]) {
212 stack.push(childNodeId);
214 writeSubtreeProperParts(tree, childNodeId, imgOutput, altitudeOf(view, nodeId));
233 template <detail::NodePreservationDecisions Mask>
234 static void filteringByPruningMaxMaskImpl(AltitudeView view,
const Mask& nodePreservationMask,
ImagePtr<T> imgOutputPtr) {
235 const char* context =
"AttributeFilters::filteringByPruningMax";
236 view.requireTopologyUnchanged(context);
238 requireNodePreservationMaskSize(tree, nodePreservationMask, context);
239 requireOutputImage(tree, imgOutputPtr, context);
244 detail::TreeKernelAccess::traversePostOrder(
246 [&collapsibleRejectedSubtree, &nodePreservationMask](
NodeId nodeId) ->
void {
247 collapsibleRejectedSubtree[nodeId] = !nodePreservationMask[static_cast<std::size_t>(nodeId)];
249 [&collapsibleRejectedSubtree](
NodeId parentNodeId,
NodeId childNodeId) ->
void {
250 collapsibleRejectedSubtree[parentNodeId] =
251 (collapsibleRejectedSubtree[parentNodeId] & collapsibleRejectedSubtree[childNodeId]);
255 auto imgOutput = imgOutputPtr->rawData();
256 std::stack<NodeId> stack;
257 stack.push(tree.
root());
258 while (!stack.empty()) {
259 const NodeId nodeId = stack.top();
261 writeProperParts(tree, nodeId, imgOutput, altitudeOf(view, nodeId));
262 for (
NodeId childNodeId : detail::TreeKernelAccess::children(tree, nodeId)) {
263 if (!collapsibleRejectedSubtree[childNodeId]) {
264 stack.push(childNodeId);
266 writeSubtreeProperParts(tree, childNodeId, imgOutput, altitudeOf(view, childNodeId));
286 template <std::
floating_po
int Real>
287 static void filteringByPruningMinAttributeImpl(AltitudeView view,
const Real* attribute, Real threshold,
ImagePtr<T> imgOutputPtr) {
288 const char* context =
"AttributeFilters::filteringByPruningMin";
289 view.requireTopologyUnchanged(context);
291 requireAttributePointer(attribute, context);
292 requireOutputImage(tree, imgOutputPtr, context);
293 auto imgOutput = imgOutputPtr->rawData();
294 std::stack<NodeId> stack;
295 stack.push(tree.
root());
296 while (!stack.empty()) {
297 const NodeId nodeId = stack.top();
299 writeProperParts(tree, nodeId, imgOutput, altitudeOf(view, nodeId));
300 for (
NodeId childNodeId : detail::TreeKernelAccess::children(tree, nodeId)) {
301 if (attribute[childNodeId] >= threshold) {
302 stack.push(childNodeId);
304 writeSubtreeProperParts(tree, childNodeId, imgOutput, altitudeOf(view, nodeId));
323 template <std::
floating_po
int Real>
324 static void filteringByPruningMaxAttributeImpl(AltitudeView view,
const Real* attribute, Real threshold,
ImagePtr<T> imgOutputPtr) {
325 const char* context =
"AttributeFilters::filteringByPruningMax";
326 view.requireTopologyUnchanged(context);
328 requireAttributePointer(attribute, context);
329 requireOutputImage(tree, imgOutputPtr, context);
334 detail::TreeKernelAccess::traversePostOrder(
336 [&collapsibleRejectedSubtree, attribute, threshold](
NodeId nodeId) ->
void {
337 if (attribute[nodeId] < threshold) {
338 collapsibleRejectedSubtree[nodeId] = true;
341 [&collapsibleRejectedSubtree](
NodeId parentNodeId,
NodeId childNodeId) ->
void {
342 collapsibleRejectedSubtree[parentNodeId] =
343 (collapsibleRejectedSubtree[parentNodeId] & collapsibleRejectedSubtree[childNodeId]);
347 auto imgOutput = imgOutputPtr->rawData();
348 std::stack<NodeId> stack;
349 stack.push(tree.
root());
350 while (!stack.empty()) {
351 const NodeId nodeId = stack.top();
353 writeProperParts(tree, nodeId, imgOutput, altitudeOf(view, nodeId));
354 for (
NodeId childNodeId : detail::TreeKernelAccess::children(tree, nodeId)) {
355 if (!collapsibleRejectedSubtree[childNodeId]) {
356 stack.push(childNodeId);
358 writeSubtreeProperParts(tree, childNodeId, imgOutput, altitudeOf(view, childNodeId));
375 explicit AttributeFilters(AltitudeView view) : view_{view}, tree{view_.topology()}, treeMutationVersion_{tree.getMutationVersion()} {
421 filteringByPruningMinAttributeImpl(view(), attr, threshold, imgOutput);
450 filteringByPruningMaxAttributeImpl(view(), attr, threshold, imgOutput);
469 requireStableTree(
"AttributeFilters::filteringByViterbiRule");
470 auto costs = detail::makeThresholdViterbiCosts(tree, attr, threshold);
471 NodePreservationMask nodePreservationMask(detail::computeViterbiPreservationDecisions(tree, costs));
472 return applyDirectAttributeFilter(view(), nodePreservationMask);
482 template <detail::NodePreservationDecisions Mask>
485 filteringByPruningMinMaskImpl(view(), nodePreservationMask, imgOutput);
496 template <detail::NodePreservationDecisions Mask>
499 filteringByPruningMaxMaskImpl(view(), nodePreservationMask, imgOutput);
511 template <detail::NodePreservationDecisions Mask>
514 filteringByPruningMinMaskImpl(valuedTree, nodePreservationMask, imgOutputPtr);
525 template <detail::NodePreservationDecisions Mask>
539 template <detail::NodePreservationDecisions Mask>
542 filteringByPruningMaxMaskImpl(valuedTree, nodePreservationMask, imgOutputPtr);
553 template <detail::NodePreservationDecisions Mask>
568 template <std::
floating_po
int Real>
582 template <std::
floating_po
int Real>
597 template <std::
floating_po
int Real>
599 filteringByPruningMinAttributeImpl(valuedTree, attribute, threshold, imgOutputPtr);
611 template <std::
floating_po
int Real>
625 template <std::
floating_po
int Real>
639 template <std::
floating_po
int Real>
654 template <std::
floating_po
int Real>
656 filteringByPruningMaxAttributeImpl(valuedTree, attribute, threshold, imgOutputPtr);
668 template <std::
floating_po
int Real>