MayaFlux 0.1.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
Region.hpp
Go to the documentation of this file.
1#pragma once
2
4
5namespace MayaFlux::Kakshya {
6
7/**
8 * @enum RegionSelectionPattern
9 * @brief Describes how regions are selected for processing or playback.
10 */
11enum class RegionSelectionPattern : uint8_t {
12 ALL, ///< Process all regions
13 SEQUENTIAL, ///< Process regions in order
14 RANDOM, ///< Random selection
15 ROUND_ROBIN, ///< Cycle through regions
16 WEIGHTED, ///< Weighted random selection
17 OVERLAP, ///< Overlapping selection
18 EXCLUSIVE, ///< Mutually exclusive selection
19 CUSTOM ///< User-defined selection logic
20};
21
22/**
23 * @enum RegionTransition
24 * @brief Describes how transitions between regions are handled.
25 */
26enum class RegionTransition : uint8_t {
27 IMMEDIATE, ///< No transition, jump directly
28 CROSSFADE, ///< Crossfade between regions
29 OVERLAP, ///< Overlap regions during transition
30 GATED, ///< Hard gate between regions
31 CALLBACK ///< Use callback for custom transition
32};
33
34/**
35 * @enum RegionState
36 * @brief Processing state for regions.
37 */
38enum class RegionState : uint8_t {
39 IDLE, ///< Not being processed
40 LOADING, ///< Data being loaded
41 READY, ///< Ready for processing
42 ACTIVE, ///< Currently being processed
43 TRANSITIONING, ///< In transition to another region
44 UNLOADING ///< Being removed from memory
45};
46
47/**
48 * @struct Region
49 * @brief Represents a point or span in N-dimensional space.
50 *
51 * Regions represent precise locations or segments within signal data,
52 * defined by start and end frame positions. Each region can have additional
53 * attributes stored in a flexible key-value map, allowing for rich metadata
54 * association with each point.
55 *
56 * Common DSP-specific uses include:
57 * - Marking transients and onset detection points
58 * - Identifying spectral features or frequency domain events
59 * - Defining zero-crossing boundaries for phase analysis
60 * - Marking signal transformation points (e.g., filter application boundaries)
61 * - Storing analysis results like RMS peaks, harmonic content points, or noise floors
62 *
63 * The flexible attribute system allows for storing any computed values or metadata
64 * associated with specific signal locations, enabling advanced signal processing
65 * workflows and algorithmic decision-making.
66 */
67struct MAYAFLUX_API Region {
68 /** @brief Starting frame index (inclusive) */
69 std::vector<uint64_t> start_coordinates;
70
71 /** @brief Ending frame index (inclusive) */
72 std::vector<uint64_t> end_coordinates;
73
74 /** @brief Flexible key-value store for region-specific attributes */
75 std::unordered_map<std::string, std::any> attributes;
76
77 Region() = default;
78
79 /**
80 * @brief Construct a point-like region (single coordinate).
81 * @param coordinates The N-dimensional coordinates.
82 * @param attributes Optional metadata.
83 */
84 Region(const std::vector<uint64_t>& coordinates,
85 std::unordered_map<std::string, std::any> attributes = {})
86 : start_coordinates(coordinates)
87 , end_coordinates(coordinates)
88 , attributes(std::move(attributes))
89 {
90 }
91
92 /**
93 * @brief Construct a span-like region (start and end coordinates).
94 * @param start Start coordinates (inclusive).
95 * @param end End coordinates (inclusive).
96 * @param attributes Optional metadata.
97 */
98 Region(std::vector<uint64_t> start,
99 std::vector<uint64_t> end,
100 std::unordered_map<std::string, std::any> attributes = {})
101 : start_coordinates(std::move(start))
102 , end_coordinates(std::move(end))
103 , attributes(std::move(attributes))
104 {
105 }
106
107 /**
108 * @brief Create a Region representing a single time point (e.g., a frame or sample).
109 * @param frame The frame index (time coordinate).
110 * @param label Optional label for this point.
111 * @param extra_data Optional extra metadata (e.g., analysis result).
112 * @return Region at the specified frame.
113 */
114 static Region time_point(uint64_t frame,
115 const std::string& label = "",
116 const std::any& extra_data = {})
117 {
118 std::unordered_map<std::string, std::any> attrs;
119 if (!label.empty())
120 attrs["label"] = label;
121 if (extra_data.has_value())
122 attrs["data"] = extra_data;
123 attrs["type"] = "time_point";
124 return Region({ frame }, attrs);
125 }
126
127 /**
128 * @brief Create a Region representing a time span (e.g., a segment of frames).
129 * @param start_frame Start frame index (inclusive).
130 * @param end_frame End frame index (inclusive).
131 * @param label Optional label for this span.
132 * @param extra_data Optional extra metadata.
133 * @return Region covering the specified time span.
134 */
135 static Region time_span(uint64_t start_frame,
136 uint64_t end_frame,
137 const std::string& label = "",
138 const std::any& extra_data = {})
139 {
140 std::unordered_map<std::string, std::any> attrs;
141 if (!label.empty())
142 attrs["label"] = label;
143 if (extra_data.has_value())
144 attrs["data"] = extra_data;
145 attrs["type"] = "time_span";
146 return Region({ start_frame }, { end_frame }, attrs);
147 }
148
149 /**
150 * @brief Create a Region for a single audio sample/channel location.
151 * @param frame The frame index (time).
152 * @param channel The channel index.
153 * @param label Optional label for this region.
154 * @return Region at the specified frame/channel.
155 */
156 static Region audio_point(uint64_t frame,
157 uint32_t channel,
158 const std::string& label = "")
159 {
160 std::unordered_map<std::string, std::any> attrs;
161 if (!label.empty())
162 attrs["label"] = label;
163 attrs["type"] = "audio_point";
164 return Region({ frame, channel }, attrs);
165 }
166
167 /**
168 * @brief Create a Region representing a span in audio (frames and channels).
169 * @param start_frame Start frame index (inclusive).
170 * @param end_frame End frame index (inclusive).
171 * @param start_channel Start channel index (inclusive).
172 * @param end_channel End channel index (inclusive).
173 * @param label Optional label for this region.
174 * @return Region covering the specified audio region.
175 */
176 static Region audio_span(uint64_t start_frame,
177 uint64_t end_frame,
178 uint32_t start_channel,
179 uint32_t end_channel,
180 const std::string& label = "")
181 {
182 std::unordered_map<std::string, std::any> attrs;
183 if (!label.empty())
184 attrs["label"] = label;
185 attrs["type"] = "audio_region";
186 return Region({ start_frame, start_channel },
187 { end_frame, end_channel }, attrs);
188 }
189
190 /**
191 * @brief Create a Region representing a rectangular region in an image.
192 * @param x1 Top-left X coordinate.
193 * @param y1 Top-left Y coordinate.
194 * @param x2 Bottom-right X coordinate.
195 * @param y2 Bottom-right Y coordinate.
196 * @param label Optional label for this rectangle.
197 * @return Region covering the specified image rectangle.
198 */
199 static Region image_rect(uint64_t x1, uint64_t y1,
200 uint64_t x2, uint64_t y2,
201 const std::string& label = "")
202 {
203 std::unordered_map<std::string, std::any> attrs;
204 if (!label.empty())
205 attrs["label"] = label;
206 attrs["type"] = "image_rect";
207 return Region({ x1, y1 }, { x2, y2 }, attrs);
208 }
209
210 /**
211 * @brief Create a Region representing a region in a video (frames and spatial rectangle).
212 * @param start_frame Start frame index (inclusive).
213 * @param end_frame End frame index (inclusive).
214 * @param x1 Top-left X coordinate.
215 * @param y1 Top-left Y coordinate.
216 * @param x2 Bottom-right X coordinate.
217 * @param y2 Bottom-right Y coordinate.
218 * @param label Optional label for this region.
219 * @return Region covering the specified video region.
220 */
221 static Region video_region(uint64_t start_frame,
222 uint64_t end_frame,
223 uint64_t x1, uint64_t y1,
224 uint64_t x2, uint64_t y2,
225 const std::string& label = "")
226 {
227 std::unordered_map<std::string, std::any> attrs;
228 if (!label.empty())
229 attrs["label"] = label;
230 attrs["type"] = "video_region";
231 return Region({ start_frame, x1, y1 },
232 { end_frame, x2, y2 }, attrs);
233 }
234
235 /**
236 * @brief Check if this region is a single point (start == end).
237 */
238 bool is_point() const
239 {
240 return start_coordinates == end_coordinates;
241 }
242
243 /**
244 * @brief Check if the given coordinates are contained within this region.
245 * @param coordinates N-dimensional coordinates to check.
246 * @return True if contained, false otherwise.
247 */
248 bool contains(const std::vector<uint64_t>& coordinates) const
249 {
250 if (coordinates.size() != start_coordinates.size())
251 return false;
252
253 for (size_t i = 0; i < start_coordinates.size(); i++) {
254 if (coordinates[i] < start_coordinates[i] || coordinates[i] > end_coordinates[i]) {
255 return false;
256 }
257 }
258 return true;
259 }
260
261 /**
262 * @brief Check if this region overlaps with another region.
263 * @param other The other region.
264 * @return True if overlapping, false otherwise.
265 */
266 bool overlaps(const Region& other) const
267 {
268 if (start_coordinates.size() != other.start_coordinates.size())
269 return false;
270
271 for (size_t i = 0; i < start_coordinates.size(); i++) {
272 if (end_coordinates[i] < other.start_coordinates[i] || start_coordinates[i] > other.end_coordinates[i]) {
273 return false;
274 }
275 }
276 return true;
277 }
278
279 /**
280 * @brief Get the span (length) of the region along a dimension.
281 * @param dimension_index The dimension to query.
282 * @return The span (number of elements) along the dimension.
283 */
284 uint64_t get_span(uint32_t dimension_index = 0) const
285 {
286 if (dimension_index >= start_coordinates.size())
287 return 0;
288 return end_coordinates[dimension_index] - start_coordinates[dimension_index] + 1;
289 }
290
291 /**
292 * @brief Get the total volume (number of elements) in the region.
293 * @return The product of spans across all dimensions.
294 */
295 uint64_t get_volume() const
296 {
297 uint64_t volume = 1;
298 for (size_t i = 0; i < start_coordinates.size(); i++) {
299 volume *= get_span(i);
300 }
301 return volume;
302 }
303
304 /**
305 * @brief Get the duration (span) along a specific dimension.
306 * @param dimension_index The dimension to query.
307 * @return The duration (number of elements) along the dimension.
308 */
309 uint64_t get_duration(uint32_t dimension_index = 0) const
310 {
311 if (dimension_index >= start_coordinates.size()) {
312 return 0;
313 }
314 return end_coordinates[dimension_index] - start_coordinates[dimension_index] + 1;
315 }
316
317 /**
318 * @brief Get an attribute value by key, with type conversion support.
319 * @tparam T The expected type.
320 * @param key The attribute key.
321 * @return Optional value if present and convertible.
322 */
323 template <typename T>
324 std::optional<T> get_attribute(const std::string& key) const
325 {
326 auto it = attributes.find(key);
327 if (it == attributes.end()) {
328 return std::nullopt;
329 }
330
331 return safe_any_cast<T>(it->second);
332 }
333
334 /**
335 * @brief Set an attribute value by key.
336 * @param key The attribute key.
337 * @param value The value to set.
338 */
339 void set_attribute(const std::string& key, std::any value)
340 {
341 attributes[key] = std::move(value);
342 }
343
344 /**
345 * @brief Get the label attribute (if present).
346 * @return The label string, or empty if not set.
347 */
348 std::string get_label() const
349 {
350 auto label = get_attribute<std::string>("label");
351 return label.value_or("");
352 }
353
354 /**
355 * @brief Set the label attribute.
356 * @param label The label string.
357 */
358 void set_label(const std::string& label)
359 {
360 set_attribute("label", label);
361 }
362
363 /**
364 * @brief Translate the region by an offset vector.
365 * @param offset The offset for each dimension (can be negative).
366 * @return A new translated Region.
367 */
368 Region translate(const std::vector<int64_t>& offset) const
369 {
370 Region result = *this;
371 for (size_t i = 0; i < std::min(offset.size(), start_coordinates.size()); i++) {
372 if (offset[i] < 0 && std::abs(offset[i]) > static_cast<int64_t>(result.start_coordinates[i])) {
373 result.start_coordinates[i] = 0;
374 } else {
375 result.start_coordinates[i] += offset[i];
376 }
377
378 if (offset[i] < 0 && std::abs(offset[i]) > static_cast<int64_t>(result.end_coordinates[i])) {
379 result.end_coordinates[i] = 0;
380 } else {
381 result.end_coordinates[i] += offset[i];
382 }
383 }
384 return result;
385 }
386
387 /**
388 * @brief Scale the region about its center by the given factors.
389 * @param factors Scaling factors for each dimension.
390 * @return A new scaled Region.
391 */
392 Region scale(const std::vector<double>& factors) const
393 {
394 Region result = *this;
395 for (size_t i = 0; i < std::min<size_t>(factors.size(), start_coordinates.size()); i++) {
396 uint64_t center = (start_coordinates[i] + end_coordinates[i]) / 2;
397 uint64_t half_span = get_span(i) / 2;
398 auto new_half_span = static_cast<uint64_t>((double)half_span * factors[i]);
399
400 result.start_coordinates[i] = center - new_half_span;
401 result.end_coordinates[i] = center + new_half_span;
402 }
403 return result;
404 }
405
406 /**
407 * @brief Equality comparison for Regions.
408 */
409 bool operator==(const Region& other) const
410 {
411 if (start_coordinates.size() != other.start_coordinates.size() || end_coordinates.size() != other.end_coordinates.size()) {
412 return false;
413 }
414
415 for (size_t i = 0; i < start_coordinates.size(); ++i) {
416 if (start_coordinates[i] != other.start_coordinates[i]) {
417 return false;
418 }
419 }
420
421 for (size_t i = 0; i < end_coordinates.size(); ++i) {
422 if (end_coordinates[i] != other.end_coordinates[i]) {
423 return false;
424 }
425 }
426
427 return true;
428 }
429
430 /**
431 * @brief Inequality comparison for Regions.
432 */
433 bool operator!=(const Region& other) const
434 {
435 return !(*this == other);
436 }
437};
438
439} // namespace MayaFlux::Kakshya
RegionSelectionPattern
Describes how regions are selected for processing or playback.
Definition Region.hpp:11
@ SEQUENTIAL
Process regions in order.
@ CUSTOM
User-defined selection logic.
@ WEIGHTED
Weighted random selection.
@ EXCLUSIVE
Mutually exclusive selection.
RegionTransition
Describes how transitions between regions are handled.
Definition Region.hpp:26
@ GATED
Hard gate between regions.
@ CROSSFADE
Crossfade between regions.
@ IMMEDIATE
No transition, jump directly.
@ CALLBACK
Use callback for custom transition.
RegionState
Processing state for regions.
Definition Region.hpp:38
@ ACTIVE
Currently being processed.
@ READY
Ready for processing.
@ UNLOADING
Being removed from memory.
@ TRANSITIONING
In transition to another region.
@ IDLE
Not being processed.
@ LOADING
Data being loaded.
void set_label(const std::string &label)
Set the label attribute.
Definition Region.hpp:358
uint64_t get_duration(uint32_t dimension_index=0) const
Get the duration (span) along a specific dimension.
Definition Region.hpp:309
bool is_point() const
Check if this region is a single point (start == end).
Definition Region.hpp:238
static Region audio_point(uint64_t frame, uint32_t channel, const std::string &label="")
Create a Region for a single audio sample/channel location.
Definition Region.hpp:156
static Region audio_span(uint64_t start_frame, uint64_t end_frame, uint32_t start_channel, uint32_t end_channel, const std::string &label="")
Create a Region representing a span in audio (frames and channels).
Definition Region.hpp:176
static Region time_span(uint64_t start_frame, uint64_t end_frame, const std::string &label="", const std::any &extra_data={})
Create a Region representing a time span (e.g., a segment of frames).
Definition Region.hpp:135
Region(const std::vector< uint64_t > &coordinates, std::unordered_map< std::string, std::any > attributes={})
Construct a point-like region (single coordinate).
Definition Region.hpp:84
void set_attribute(const std::string &key, std::any value)
Set an attribute value by key.
Definition Region.hpp:339
Region translate(const std::vector< int64_t > &offset) const
Translate the region by an offset vector.
Definition Region.hpp:368
bool operator!=(const Region &other) const
Inequality comparison for Regions.
Definition Region.hpp:433
std::unordered_map< std::string, std::any > attributes
Flexible key-value store for region-specific attributes.
Definition Region.hpp:75
uint64_t get_span(uint32_t dimension_index=0) const
Get the span (length) of the region along a dimension.
Definition Region.hpp:284
Region scale(const std::vector< double > &factors) const
Scale the region about its center by the given factors.
Definition Region.hpp:392
std::optional< T > get_attribute(const std::string &key) const
Get an attribute value by key, with type conversion support.
Definition Region.hpp:324
uint64_t get_volume() const
Get the total volume (number of elements) in the region.
Definition Region.hpp:295
std::vector< uint64_t > end_coordinates
Ending frame index (inclusive)
Definition Region.hpp:72
static Region video_region(uint64_t start_frame, uint64_t end_frame, uint64_t x1, uint64_t y1, uint64_t x2, uint64_t y2, const std::string &label="")
Create a Region representing a region in a video (frames and spatial rectangle).
Definition Region.hpp:221
bool overlaps(const Region &other) const
Check if this region overlaps with another region.
Definition Region.hpp:266
std::string get_label() const
Get the label attribute (if present).
Definition Region.hpp:348
std::vector< uint64_t > start_coordinates
Starting frame index (inclusive)
Definition Region.hpp:69
bool contains(const std::vector< uint64_t > &coordinates) const
Check if the given coordinates are contained within this region.
Definition Region.hpp:248
static Region image_rect(uint64_t x1, uint64_t y1, uint64_t x2, uint64_t y2, const std::string &label="")
Create a Region representing a rectangular region in an image.
Definition Region.hpp:199
bool operator==(const Region &other) const
Equality comparison for Regions.
Definition Region.hpp:409
Region(std::vector< uint64_t > start, std::vector< uint64_t > end, std::unordered_map< std::string, std::any > attributes={})
Construct a span-like region (start and end coordinates).
Definition Region.hpp:98
static Region time_point(uint64_t frame, const std::string &label="", const std::any &extra_data={})
Create a Region representing a single time point (e.g., a frame or sample).
Definition Region.hpp:114
Represents a point or span in N-dimensional space.
Definition Region.hpp:67