MayaFlux 1.0.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
Sensor.hpp
Go to the documentation of this file.
1#pragma once
2
4
5namespace MayaFlux::Nexus {
6
7/**
8 * @class Sensor
9 * @brief Object that reacts to nearby entities when committed.
10 *
11 * Constructed with a query radius and, optionally, a perception function. Position
12 * is optional: call @c set_position before registering with @c Fabric if
13 * spatial queries are required. A Sensor without a position receives an
14 * empty @c spatial_results span on each commit.
15 *
16 * The id is assigned by @c Fabric::wire and is stable for the object's
17 * lifetime.
18 */
19class MAYAFLUX_API Sensor {
20public:
21 using PerceptionFn = std::function<void(const PerceptionContext&)>;
22
23 /**
24 * @brief Construct with only a query radius, for a Sensor whose behaviour comes
25 * from a typed perception.
26 * @param query_radius Radius passed to the spatial index on each commit.
27 * Ignored if no position has been set.
28 */
29 explicit Sensor(float query_radius = 1.0F)
30 : m_query_radius(query_radius)
31 {
32 }
33
34 /**
35 * @brief Construct with a query radius and a perception function.
36 * @param query_radius Radius passed to the spatial index on each commit.
37 * Ignored if no position has been set.
38 * @param fn Called on every commit with the current context.
39 */
40 explicit Sensor(float query_radius, PerceptionFn fn)
41 : m_query_radius(query_radius)
42 , m_fn(std::move(fn))
43 {
44 }
45
46 /**
47 * @brief Construct with a query radius and a named perception function.
48 * @param query_radius Radius passed to the spatial index on each commit.
49 * @param fn_name Identifier used for state encoding.
50 * @param fn Called on every commit with the current context.
51 */
52 Sensor(float query_radius, std::string fn_name, PerceptionFn fn)
53 : m_query_radius(query_radius)
54 , m_fn_name(std::move(fn_name))
55 , m_fn(std::move(fn))
56 {
57 }
58
59 /** @brief Identifier assigned to the perception function, empty if anonymous. */
60 [[nodiscard]] const std::string& fn_name() const { return m_fn_name; }
61
62 /** @brief Set or replace the perception function's identifier. */
63 void set_fn_name(std::string name) { m_fn_name = std::move(name); }
64
65 /** @brief The perception function itself. */
66 [[nodiscard]] const PerceptionFn& fn() const { return m_fn; }
67
68 /**
69 * @brief Return the current position, if set.
70 */
71 [[nodiscard]] const std::optional<glm::vec3>& position() const { return m_position; }
72
73 /**
74 * @brief Set the position, enabling spatial queries for this object.
75 * @param p World-space coordinates.
76 */
77 void set_position(const glm::vec3& p) { m_position = p; }
78
79 /**
80 * @brief Clear the position, disabling spatial queries for this object.
81 */
82 void clear_position() { m_position.reset(); }
83
84 /**
85 * @brief Return the query radius.
86 */
87 [[nodiscard]] float query_radius() const { return m_query_radius; }
88
89 /**
90 * @brief Set the query radius.
91 * @param r New radius in world-space units.
92 */
93 void set_query_radius(float r) { m_query_radius = r; }
94
95 /**
96 * @brief Return the stable object id assigned by Fabric.
97 */
98 [[nodiscard]] uint32_t id() const { return m_id; }
99
100 /**
101 * @brief Set the one source of a perception of any type.
102 * @tparam T Type the source returns; whatever the target takes.
103 * @param source Reads the value, using the perception context.
104 * @param target Takes the value and decides where it lands.
105 *
106 * A Sensor has a single source: setting another replaces it. Runs on every
107 * invoke before the perception function. Not part of encoded state.
108 */
109 template <typename T>
110 void set_perception(typename Perception<T>::Source source, typename Perception<T>::Target target)
111 {
112 auto perception = std::make_shared<Perception<T>>(std::move(source));
113 perception->add_target(std::move(target));
114 m_perception = [perception](const PerceptionContext& ctx) { perception->invoke(ctx); };
115 }
116
117 /** @brief Remove the source set with set_perception(). */
118 void clear_perception() { m_perception = nullptr; }
119
120 /**
121 * @brief Invoke the perception function with the supplied context.
122 * @param ctx Populated context for this commit.
123 */
124 void invoke(const PerceptionContext& ctx) const
125 {
126 if (m_perception) {
127 m_perception(ctx);
128 }
129 if (m_fn) {
130 m_fn(ctx);
131 }
132 }
133
134private:
135 std::optional<glm::vec3> m_position;
137 std::string m_fn_name;
140 uint32_t m_id { 0 };
141
142 friend class Fabric;
143};
144
145} // namespace MayaFlux::Nexus
float query_radius
Definition Decoder.cpp:96
std::string name
Definition VKDevice.cpp:123
Orchestrates spatial indexing and scheduling for Nexus objects.
Definition Fabric.hpp:38
std::function< void(const T &)> Target
std::function< T(const PerceptionContext &)> Source
void invoke(const PerceptionContext &ctx) const
Invoke the perception function with the supplied context.
Definition Sensor.hpp:124
float query_radius() const
Return the query radius.
Definition Sensor.hpp:87
uint32_t id() const
Return the stable object id assigned by Fabric.
Definition Sensor.hpp:98
const PerceptionFn & fn() const
The perception function itself.
Definition Sensor.hpp:66
std::optional< glm::vec3 > m_position
Definition Sensor.hpp:135
void set_query_radius(float r)
Set the query radius.
Definition Sensor.hpp:93
void set_position(const glm::vec3 &p)
Set the position, enabling spatial queries for this object.
Definition Sensor.hpp:77
const std::optional< glm::vec3 > & position() const
Return the current position, if set.
Definition Sensor.hpp:71
Sensor(float query_radius, std::string fn_name, PerceptionFn fn)
Construct with a query radius and a named perception function.
Definition Sensor.hpp:52
std::function< void(const PerceptionContext &)> PerceptionFn
Definition Sensor.hpp:21
const std::string & fn_name() const
Identifier assigned to the perception function, empty if anonymous.
Definition Sensor.hpp:60
void clear_position()
Clear the position, disabling spatial queries for this object.
Definition Sensor.hpp:82
std::string m_fn_name
Definition Sensor.hpp:137
void clear_perception()
Remove the source set with set_perception().
Definition Sensor.hpp:118
void set_perception(typename Perception< T >::Source source, typename Perception< T >::Target target)
Set the one source of a perception of any type.
Definition Sensor.hpp:110
void set_fn_name(std::string name)
Set or replace the perception function's identifier.
Definition Sensor.hpp:63
Sensor(float query_radius=1.0F)
Construct with only a query radius, for a Sensor whose behaviour comes from a typed perception.
Definition Sensor.hpp:29
Sensor(float query_radius, PerceptionFn fn)
Construct with a query radius and a perception function.
Definition Sensor.hpp:40
PerceptionFn m_perception
Definition Sensor.hpp:139
Object that reacts to nearby entities when committed.
Definition Sensor.hpp:19
Data passed to a Sensor or Agent perception function on each commit.