MayaFlux 1.0.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
Perception.hpp
Go to the documentation of this file.
1#pragma once
2
4
5#include <glm/gtc/quaternion.hpp>
6
7namespace MayaFlux::Nexus {
8
9/**
10 * @struct PerceptionContext
11 * @brief Data passed to a Sensor or Agent perception function on each commit.
12 *
13 * @c spatial_results contains the entities within @c radius of @c position
14 * at the moment of the last published snapshot. Results hold entity ids and
15 * squared distances as returned by @c SpatialIndex::within_radius.
16 *
17 * Fields marked @note future are reserved for later domain expansions.
18 */
20 glm::vec3 position {};
21 float radius { 1.0F }; ///< Query radius the spatial results were gathered with.
22 std::optional<glm::quat> orientation; ///< Optional orientation of the perceiver. Local +Z is its forward direction.
23 std::span<const Kinesis::QueryResult> spatial_results;
24
25 // @note future: EnergyAnalysis audio_energy {};
26 // @note future: std::span<const double> audio_snapshot {};
27};
28
29/**
30 * @class Perception
31 * @brief A value of any type, read from the world through the perception context and handed to its targets.
32 *
33 * T is whatever is sensed: a scalar from a node or buffer, a vector, a struct
34 * of readings, a Tendency, a DataVariant. The source reads it, using the
35 * context when it needs position or neighbours. A target is any callable taking
36 * const T& and decides where it lands, such as a cell the perception function
37 * reads. On each invoke the source runs once and every target gets the value,
38 * in the order they were added.
39 *
40 * How many targets a Perception may have is decided by its owner: a Sensor
41 * keeps one, an Agent keeps as many as are added.
42 */
43template <typename T>
45public:
46 using Source = std::function<T(const PerceptionContext&)>;
47 using Target = std::function<void(const T&)>;
48
49 explicit Perception(Source source)
50 : m_source(std::move(source))
51 {
52 }
53
54 /** @brief Add a target. Returns this perception for chaining. */
56 {
57 m_targets.push_back(std::move(target));
58 return *this;
59 }
60
61 /** @brief Remove every target. */
62 void clear_targets() { m_targets.clear(); }
63
64 /** @brief Number of targets currently added. */
65 [[nodiscard]] size_t target_count() const { return m_targets.size(); }
66
67 /** @brief Read the value using @p ctx and hand it to every target. */
68 void invoke(const PerceptionContext& ctx) const
69 {
70 if (!m_source) {
71 return;
72 }
73 const T value = m_source(ctx);
74 for (const auto& target : m_targets) {
75 target(value);
76 }
77 }
78
79private:
81 std::vector<Target> m_targets;
82};
83
84} // namespace MayaFlux::Nexus
glm::vec3 value
std::function< void(const T &)> Target
void invoke(const PerceptionContext &ctx) const
Read the value using ctx and hand it to every target.
std::vector< Target > m_targets
size_t target_count() const
Number of targets currently added.
std::function< T(const PerceptionContext &)> Source
void clear_targets()
Remove every target.
Perception & add_target(Target target)
Add a target.
A value of any type, read from the world through the perception context and handed to its targets.
std::optional< glm::quat > orientation
Optional orientation of the perceiver. Local +Z is its forward direction.
float radius
Query radius the spatial results were gathered with.
std::span< const Kinesis::QueryResult > spatial_results
Data passed to a Sensor or Agent perception function on each commit.