MorphologicalAttributeFilters
Public API documentation
Loading...
Searching...
No Matches
AttributeNames.hpp
1#pragma once
2
3#include "AttributeRegistry.hpp"
4#include "AttributeTypes.hpp"
5
6#include <optional>
7#include <stdexcept>
8#include <string>
9#include <unordered_map>
10#include <vector>
11
12namespace mmcfilters {
21inline const std::unordered_map<AttributeGroup, std::vector<Attribute>>& ATTRIBUTE_GROUPS =
22 attributes::registry::attributeGroups();
23
24
40public:
42 std::unordered_map<AttributeKey, int> indexMap;
43
45 const int NUM_ATTRIBUTES;
46
53 AttributeNamesWithDelta(std::unordered_map<AttributeKey, int>&& map)
55
59 [[nodiscard]] static AttributeNamesWithDelta create(int delta, const std::vector<Attribute>& attributes) {
60 std::unordered_map<AttributeKey, int> map;
61 int offset = 0;
62 for (int d = -delta; d <= delta; ++d) {
63 for (std::size_t i = 0; i < attributes.size(); ++i) {
64 const AttributeKey key{attributes[i], d};
65 const auto [_, inserted] = map.emplace(key, offset++);
66 if (!inserted) {
67 throw std::invalid_argument(
68 "AttributeNamesWithDelta::create received duplicate attribute " +
69 toString(key));
70 }
71 }
72 }
73 return AttributeNamesWithDelta(std::move(map));
74 }
75
79 [[nodiscard]] int getIndex(Attribute attr, int delta) const {
80 return getIndex(AttributeKey{attr, delta});
81 }
82
87 return indexMap.at(attrKey);
88 }
89
94 [[nodiscard]] int linearIndex(int nodeIndex, Attribute attr, int delta) const {
95 return nodeIndex * NUM_ATTRIBUTES + getIndex(attr, delta);
96 }
97
102 return linearIndex(nodeIndex, attrKey.attr, attrKey.delta);
103 }
104
108 [[nodiscard]] static std::string toString(AttributeKey attrKey) {
109 return toString(attrKey.attr, attrKey.delta);
110 }
111
115 [[nodiscard]] static std::string toString(Attribute attr, int delta) {
116 std::string name(attributes::registry::name(attr));
117
118 if (delta < 0) {
119 name += "_ASC_" + std::to_string(-delta);
120 } else if (delta > 0) {
121 name += "_DESC_" + std::to_string(delta);
122 }
123
124 return name;
125 }
126};
127
128
147public:
149 std::unordered_map<Attribute, int> indexMap;
150
152 const int NUM_ATTRIBUTES;
153
160 AttributeNames(std::unordered_map<Attribute, int>&& map)
162
169 [[nodiscard]] static AttributeNames fromList(const std::vector<Attribute>& attributes) {
170 std::unordered_map<Attribute, int> map;
171 int i = 0;
172 for (auto attr : attributes) {
173 const auto [_, inserted] = map.emplace(attr, i++);
174 if (!inserted) {
175 throw std::invalid_argument(
176 "AttributeNames::fromList received duplicate attribute " +
177 toString(attr));
178 }
179 }
180 return AttributeNames(std::move(map));
181 }
182
186 [[nodiscard]] static AttributeNames fromGroup(AttributeGroup group) {
187 auto it = ATTRIBUTE_GROUPS.find(group);
188 if (it == ATTRIBUTE_GROUPS.end()) {
189 throw std::invalid_argument("Unknown attribute group.");
190 }
191 return fromList(it->second);
192 }
193
197 [[nodiscard]] int getIndex(Attribute attr) const {
198 return indexMap.at(attr);
199 }
200
204 [[nodiscard]] bool contains(Attribute attr) const noexcept {
205 return indexMap.contains(attr);
206 }
207
211 [[nodiscard]] int offset(Attribute attr) const {
212 return getIndex(attr);
213 }
214
218 [[nodiscard]] int linearIndex(int nodeIndex, Attribute attr) const {
219 return nodeIndex * NUM_ATTRIBUTES + getIndex(attr);
220 }
221
225 [[nodiscard]] static std::string toString(Attribute attr) {
226 return std::string(attributes::registry::name(attr));
227 }
228
236 static std::string describe(Attribute attr) {
237 return std::string(attributes::registry::description(attr));
238 }
239
246 [[nodiscard]] static std::optional<Attribute> parse(const std::string& str) {
247 return attributes::registry::parse(str);
248 }
249};
250
251} // namespace mmcfilters
Layout object for buffers that store several (attribute, delta) combinations per node.
AttributeNamesWithDelta(std::unordered_map< AttributeKey, int > &&map)
Constructs a layout from an already validated offset map.
int linearIndex(int nodeIndex, Attribute attr, int delta) const
Returns the linear index in the flat buffer for a given node and delta-augmented attribute.
int getIndex(AttributeKey attrKey) const
Returns the per-node offset associated with a composite delta key.
static std::string toString(Attribute attr, int delta)
Returns a human-readable label for an attribute at a given delta.
int linearIndex(int nodeIndex, AttributeKey attrKey) const
Convenience overload taking an AttributeKey.
static AttributeNamesWithDelta create(int delta, const std::vector< Attribute > &attributes)
Builds the canonical dense layout for all deltas in [-delta, delta].
std::unordered_map< AttributeKey, int > indexMap
Maps each (attribute, delta) key to its per-node offset.
int getIndex(Attribute attr, int delta) const
Returns the per-node offset associated with (attr, delta).
const int NUM_ATTRIBUTES
Number of delta-augmented entries stored for each node.
static std::string toString(AttributeKey attrKey)
Returns a human-readable label for a composite key.
Layout object that maps scalar attributes to flat-buffer offsets.
static std::string describe(Attribute attr)
Returns a user-facing description of an attribute.
static std::string toString(Attribute attr)
Returns the stable symbolic name associated with attr.
static AttributeNames fromList(const std::vector< Attribute > &attributes)
Builds a dense layout from an explicit attribute list.
static AttributeNames fromGroup(AttributeGroup group)
Builds a layout from a public attribute group.
int getIndex(Attribute attr) const
Returns the per-node offset associated with attr.
static std::optional< Attribute > parse(const std::string &str)
Parses a stable symbolic attribute name.
int linearIndex(int nodeIndex, Attribute attr) const
Returns the linear index in the flat buffer for (node, attr).
const int NUM_ATTRIBUTES
Number of scalar attributes stored for each node.
int offset(Attribute attr) const
Returns the per-node offset associated with attr.
AttributeNames(std::unordered_map< Attribute, int > &&map)
Constructs a layout from an already validated offset map.
std::unordered_map< Attribute, int > indexMap
Maps each requested attribute to its per-node offset.
bool contains(Attribute attr) const noexcept
Returns whether this layout contains attr.
Composite key used to index delta-augmented attribute layouts.
Owning result for one computed scalar attribute layout and buffer.
std::vector< Real > second
Flat per-node attribute buffer indexed through first.