MayaFlux 0.4.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 only a perception function and a query radius. 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 Sensor {
20public:
21 using PerceptionFn = std::function<void(const PerceptionContext&)>;
22
23 /**
24 * @brief Construct with a query radius and a perception function.
25 * @param query_radius Radius passed to the spatial index on each commit.
26 * Ignored if no position has been set.
27 * @param fn Called on every commit with the current context.
28 */
31 , m_fn(std::move(fn))
32 {
33 }
34
35 /**
36 * @brief Construct with a query radius and a named perception function.
37 * @param query_radius Radius passed to the spatial index on each commit.
38 * @param fn_name Identifier used for state encoding.
39 * @param fn Called on every commit with the current context.
40 */
43 , m_fn_name(std::move(fn_name))
44 , m_fn(std::move(fn))
45 {
46 }
47
48 /** @brief Identifier assigned to the perception function, empty if anonymous. */
49 [[nodiscard]] const std::string& fn_name() const { return m_fn_name; }
50
51 /** @brief Set or replace the perception function's identifier. */
52 void set_fn_name(std::string name) { m_fn_name = std::move(name); }
53
54 /** @brief The perception function itself. */
55 [[nodiscard]] const PerceptionFn& fn() const { return m_fn; }
56
57 /**
58 * @brief Return the current position, if set.
59 */
60 [[nodiscard]] const std::optional<glm::vec3>& position() const { return m_position; }
61
62 /**
63 * @brief Set the position, enabling spatial queries for this object.
64 * @param p World-space coordinates.
65 */
66 void set_position(const glm::vec3& p) { m_position = p; }
67
68 /**
69 * @brief Clear the position, disabling spatial queries for this object.
70 */
71 void clear_position() { m_position.reset(); }
72
73 /**
74 * @brief Return the query radius.
75 */
76 [[nodiscard]] float query_radius() const { return m_query_radius; }
77
78 /**
79 * @brief Set the query radius.
80 * @param r New radius in world-space units.
81 */
82 void set_query_radius(float r) { m_query_radius = r; }
83
84 /**
85 * @brief Return the stable object id assigned by Fabric.
86 */
87 [[nodiscard]] uint32_t id() const { return m_id; }
88
89 /**
90 * @brief Invoke the perception function with the supplied context.
91 * @param ctx Populated context for this commit.
92 */
93 void invoke(const PerceptionContext& ctx) const
94 {
95 if (m_fn) {
96 m_fn(ctx);
97 }
98 }
99
100private:
101 std::optional<glm::vec3> m_position;
103 std::string m_fn_name;
105 uint32_t m_id { 0 };
106
107 friend class Fabric;
108};
109
110} // namespace MayaFlux::Nexus
Orchestrates spatial indexing and scheduling for Nexus objects.
Definition Fabric.hpp:37
void invoke(const PerceptionContext &ctx) const
Invoke the perception function with the supplied context.
Definition Sensor.hpp:93
float query_radius() const
Return the query radius.
Definition Sensor.hpp:76
uint32_t id() const
Return the stable object id assigned by Fabric.
Definition Sensor.hpp:87
const PerceptionFn & fn() const
The perception function itself.
Definition Sensor.hpp:55
std::optional< glm::vec3 > m_position
Definition Sensor.hpp:101
void set_query_radius(float r)
Set the query radius.
Definition Sensor.hpp:82
void set_position(const glm::vec3 &p)
Set the position, enabling spatial queries for this object.
Definition Sensor.hpp:66
const std::optional< glm::vec3 > & position() const
Return the current position, if set.
Definition Sensor.hpp:60
Sensor(float query_radius, std::string fn_name, PerceptionFn fn)
Construct with a query radius and a named perception function.
Definition Sensor.hpp:41
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:49
void clear_position()
Clear the position, disabling spatial queries for this object.
Definition Sensor.hpp:71
std::string m_fn_name
Definition Sensor.hpp:103
void set_fn_name(std::string name)
Set or replace the perception function's identifier.
Definition Sensor.hpp:52
Sensor(float query_radius, PerceptionFn fn)
Construct with a query radius and a perception function.
Definition Sensor.hpp:29
Object that reacts to nearby entities when committed.
Definition Sensor.hpp:19
Data passed to a Sensor or Agent perception function on each commit.