mmcfilters
Public API documentation
Loading...
Searching...
No Matches
AreaComputer.hpp
1#pragma once
2
3#include "AttributeComputerDomain.hpp"
4#include "AttributeComputerFamily.hpp"
5#include "../detail/AttributeKernelSupport.hpp"
6#include "../../trees/detail/TreeKernelAccess.hpp"
7#include "../../utils/Contract.hpp"
8#include "../../trees/MorphologicalTree.hpp"
9
10#include <array>
11#include <concepts>
12#include <stdexcept>
13#include <string_view>
14
15namespace mmcfilters::attributes::computers {
16
17namespace detail {
18
19template <std::floating_point Real> inline void validateAreaContext(const AttributeComputeContext<Real>& context) {
20 requireAttributeBufferShape(context.tree, context.buffer, context.attrNames);
21 if (!context.attrNames.contains(Area)) {
22 throw std::invalid_argument("AREA computation requires an AREA column in the output layout.");
23 }
24}
25
26namespace kernel {
27
29template <std::floating_point Real> inline void computeArea(const AttributeComputeContext<Real>& context) {
30 const int stride = context.attrNames.NUM_ATTRIBUTES;
31 const int offset = context.attrNames.indexMap.find(Area)->second;
32 auto indexOfArea = [&](NodeId node) { return static_cast<std::size_t>(node * stride + offset); };
33 ::mmcfilters::detail::TreeKernelAccess::traversePostOrder(
34 context.tree, context.tree.root(),
35 [&](NodeId node) {
36 context.buffer[indexOfArea(node)] = static_cast<Real>(::mmcfilters::detail::TreeKernelAccess::properPartCardinality(context.tree, node));
37 },
38 [&](NodeId parent, NodeId child) { context.buffer[indexOfArea(parent)] += context.buffer[indexOfArea(child)]; }, [](NodeId) {});
39}
40
41} // namespace kernel
42} // namespace detail
43
64 public:
66 static constexpr std::string_view familyName = "area";
67
69 static constexpr AttributeComputerFamily family = AttributeComputerFamily::Area;
70
72 static constexpr AttributeComputerDomain domain = AttributeComputerDomain::Topology;
73
77 inline static constexpr std::array<Attribute, 1> producedAttributes{Area};
78
90 template <std::floating_point Real> static void compute(const AttributeComputeContext<Real>& context) {
91 MMCFILTERS_CONTRACT_CHECKED_ONLY(detail::validateAreaContext(context));
92 detail::kernel::computeArea(context);
93 }
94
95 public:
103 template <std::floating_point Real> static void computeUnitRows(const UnitAttributeComputeContext<Real>& context) {
104 requireUnitAttributeBufferShape(context.tree, context.unitPixels, context.buffer, context.attrNames);
105 computeUnitRowsEstablished(context);
106 }
107
108 private:
115 template <class Computer, class Context> friend void mmcfilters::detail::kernel::computeUnitRows(const Context& context);
116
118 template <std::floating_point Real> static void computeUnitRowsEstablished(const UnitAttributeComputeContext<Real>& context) {
119 if (!requestsAttribute(context.requestedAttributes, Area)) {
120 return;
121 }
122 for (NodeId leafIndex = 0; leafIndex < static_cast<NodeId>(context.unitPixels.size()); ++leafIndex) {
123 context.buffer[context.attrNames.linearIndex(leafIndex, Area)] = Real{1};
124 }
125 }
126};
127
128} // namespace mmcfilters::attributes::computers
int NodeId
Node identifier type used throughout the project.
Definition Common.hpp:17
#define MMCFILTERS_CONTRACT_CHECKED_ONLY(...)
Executes validation statements only when defensive checks are enabled.
Definition Contract.hpp:67
Computes the canonical subtree area attribute.
static constexpr std::array< Attribute, 1 > producedAttributes
Canonical list of attributes produced by this computer.
static void computeUnitRows(const UnitAttributeComputeContext< Real > &context)
Materializes AREA for one-pixel unit supports.
static constexpr AttributeComputerDomain domain
Execution domain required by the computer.
static constexpr std::string_view familyName
Family name used in dependency-plan diagnostics.
static constexpr AttributeComputerFamily family
Stable family id used by the scheduler.
static void compute(const AttributeComputeContext< Real > &context)
Computes area by summing direct proper-part counts bottom-up.
Owning result for one computed scalar attribute layout and buffer.
std::vector< Real > second
Flat per-node attribute buffer indexed through first.