MayaFlux 0.4.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
Fabric.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "Agent.hpp"
4#include "Emitter.hpp"
5#include "Sensor.hpp"
6#include "Wiring.hpp"
7
9
10namespace MayaFlux::Vruta {
11class TaskScheduler;
12class EventManager;
13}
14
15namespace MayaFlux::Nexus {
16
17/**
18 * @class Fabric
19 * @brief Orchestrates spatial indexing and scheduling for Nexus objects.
20 *
21 * Plain object, not a subsystem. Constructed with references to the engine's
22 * scheduler and event manager. Owns a @c Kinesis::SpatialIndex3D populated
23 * only for objects that have a position set.
24 *
25 * Objects are registered via @c wire(), which returns a @c Wiring builder.
26 * The builder describes when and how the object's function is invoked.
27 * All coroutines and events are owned by the scheduler and event manager
28 * respectively. Fabric holds only the names needed to cancel them.
29 *
30 * @c commit() updates all positions in the index, publishes the snapshot,
31 * and fires all registered objects. @c fire(id) fires a single object
32 * against the current snapshot without republishing.
33 *
34 * Spatial queries read the last published snapshot and are safe from any
35 * thread.
36 */
37class MAYAFLUX_API Fabric {
38public:
39 enum class Kind : uint8_t {
40 Emitter,
41 Sensor,
42 Agent,
43 };
44
45 /**
46 * @brief Construct with scheduler and event manager from the engine.
47 * @param scheduler Used to register coroutines created by Wiring.
48 * @param event_manager Used to register event-driven coroutines.
49 * @param cell_size Grid cell edge length for the spatial index.
50 */
51 explicit Fabric(
52 Vruta::TaskScheduler& scheduler,
53 Vruta::EventManager& event_manager,
54 float cell_size = 1.0F);
55
56 ~Fabric();
57
58 Fabric(const Fabric&) = delete;
59 Fabric& operator=(const Fabric&) = delete;
60 Fabric(Fabric&&) = delete;
61 Fabric& operator=(Fabric&&) = delete;
62
63 /**
64 * @brief Assigned name, empty if the Fabric was constructed outside a Tapestry.
65 */
66 [[nodiscard]] const std::string& name() const { return m_name; }
67
68 /**
69 * @brief Set or replace the Fabric's name. Typically called by Tapestry.
70 */
71 void set_name(std::string name) { m_name = std::move(name); }
72
73 // =========================================================================
74 // Registration
75 // =========================================================================
76
77 /**
78 * @brief Begin wiring an Emitter into the Fabric.
79 * @param emitter Object to register.
80 * @return Wiring builder. Call @c Wiring::finalise() to apply.
81 */
82 [[nodiscard]] Wiring wire(std::shared_ptr<Emitter> emitter);
83
84 /**
85 * @brief Begin wiring a Sensor into the Fabric.
86 * @param sensor Object to register.
87 * @return Wiring builder. Call @c Wiring::finalise() to apply.
88 */
89 [[nodiscard]] Wiring wire(std::shared_ptr<Sensor> sensor);
90
91 /**
92 * @brief Begin wiring an Agent into the Fabric.
93 * @param agent Object to register.
94 * @return Wiring builder. Call @c Wiring::finalise() to apply.
95 */
96 [[nodiscard]] Wiring wire(std::shared_ptr<Agent> agent);
97
98 /**
99 * @brief Remove a registered object by id, cancelling any associated tasks.
100 * @param id Stable id assigned at registration.
101 */
102 void remove(uint32_t id);
103
104 // =========================================================================
105 // Commit
106 // =========================================================================
107
108 /**
109 * @brief Update all positions, publish the snapshot, fire all registered objects.
110 *
111 * For each object with a position, calls @c m_index->update() then
112 * @c m_index->publish(). Then fires each object's function with a
113 * context built from the new snapshot.
114 */
115 void commit();
116
117 /**
118 * @brief Fire a single object against the current snapshot without republishing.
119 * @param id Id assigned at registration.
120 */
121 void fire(uint32_t id);
122
123 // =========================================================================
124 // Spatial queries
125 // =========================================================================
126
127 /**
128 * @brief Find all objects within a radius of a point.
129 * @param center Query origin.
130 * @param radius Search radius in world-space units.
131 * @return Unsorted results with squared distances.
132 */
133 [[nodiscard]] std::vector<Kinesis::QueryResult> within_radius(
134 const glm::vec3& center, float radius) const;
135
136 /**
137 * @brief Find the k nearest objects to a point.
138 * @param center Query origin.
139 * @param k Maximum number of results.
140 * @return Results sorted by ascending squared distance.
141 */
142 [[nodiscard]] std::vector<Kinesis::QueryResult> k_nearest(
143 const glm::vec3& center, uint32_t k) const;
144
145 /**
146 * @brief Access the finalised wiring for an entity for introspection.
147 * @param id Entity id assigned at registration.
148 * @return Pointer to the wiring, or nullptr if id is not registered
149 * or @c finalise() has not yet been called.
150 */
151 [[nodiscard]] const Wiring* wiring_for(uint32_t id) const;
152
153 /**
154 * @brief List all registered entity ids in insertion order.
155 */
156 [[nodiscard]] std::vector<uint32_t> all_ids() const;
157
158 /**
159 * @brief Return the kind of entity registered under @p id.
160 * @throws std::out_of_range if id is not registered.
161 */
162 [[nodiscard]] Kind kind(uint32_t id) const;
163
164 /**
165 * @brief Get the Emitter registered under @p id.
166 * @return The Emitter, or nullptr if id is not an Emitter or not registered.
167 */
168 [[nodiscard]] std::shared_ptr<Emitter> get_emitter(uint32_t id) const;
169
170 /**
171 * @brief Get the Sensor registered under @p id.
172 * @return The Sensor, or nullptr if id is not a Sensor or not registered.
173 */
174 [[nodiscard]] std::shared_ptr<Sensor> get_sensor(uint32_t id) const;
175
176 /**
177 * @brief Get the Agent registered under @p id.
178 * @return The Agent, or nullptr if id is not an Agent or not registered.
179 */
180 [[nodiscard]] std::shared_ptr<Agent> get_agent(uint32_t id) const;
181
182 // =========================================================================
183 // Function registry
184 // =========================================================================
185
186 /**
187 * @brief Look up a registered influence function by name.
188 * @return Shared pointer to the stored function, or nullptr if not found.
189 */
190 [[nodiscard]] std::shared_ptr<Emitter::InfluenceFn> resolve_influence_fn(std::string_view name) const;
191
192 /**
193 * @brief Look up a registered perception function by name.
194 * @return Shared pointer to the stored function, or nullptr if not found.
195 */
196 [[nodiscard]] std::shared_ptr<Sensor::PerceptionFn> resolve_perception_fn(std::string_view name) const;
197
198private:
199 friend class Wiring;
200
201 std::string m_name;
202
203 using Member = std::variant<
204 std::shared_ptr<Emitter>,
205 std::shared_ptr<Sensor>,
206 std::shared_ptr<Agent>>;
207
210 std::string task_name;
211 std::string event_name;
212 std::string chain_name;
213 bool commit_driven { false };
214 std::optional<uint32_t> spatial_id;
215 std::optional<Wiring> wiring;
216 };
217
218 uint32_t assign_id(Member& m);
219 void fire(const Registration& reg) const;
220
223
224 std::unique_ptr<Kinesis::SpatialIndex3D> m_index;
225 std::unordered_map<uint32_t, Registration> m_registrations;
226 uint32_t m_next_id { 1 };
227
228 std::unordered_map<std::string, std::shared_ptr<Emitter::InfluenceFn>> m_influence_fns;
229 std::unordered_map<std::string, std::shared_ptr<Sensor::PerceptionFn>> m_perception_fns;
230};
231
232} // namespace MayaFlux::Nexus
Range radius
std::string kind
Object that both perceives nearby entities and acts on MayaFlux objects.
Definition Agent.hpp:30
Object that acts on existing MayaFlux objects when committed.
Definition Emitter.hpp:26
std::unique_ptr< Kinesis::SpatialIndex3D > m_index
Definition Fabric.hpp:224
std::variant< std::shared_ptr< Emitter >, std::shared_ptr< Sensor >, std::shared_ptr< Agent > > Member
Definition Fabric.hpp:206
Fabric & operator=(const Fabric &)=delete
Fabric(Fabric &&)=delete
std::unordered_map< std::string, std::shared_ptr< Sensor::PerceptionFn > > m_perception_fns
Definition Fabric.hpp:229
Fabric & operator=(Fabric &&)=delete
const std::string & name() const
Assigned name, empty if the Fabric was constructed outside a Tapestry.
Definition Fabric.hpp:66
Fabric(const Fabric &)=delete
void set_name(std::string name)
Set or replace the Fabric's name.
Definition Fabric.hpp:71
Vruta::TaskScheduler & m_scheduler
Definition Fabric.hpp:221
std::unordered_map< uint32_t, Registration > m_registrations
Definition Fabric.hpp:225
std::unordered_map< std::string, std::shared_ptr< Emitter::InfluenceFn > > m_influence_fns
Definition Fabric.hpp:228
Vruta::EventManager & m_event_manager
Definition Fabric.hpp:222
Orchestrates spatial indexing and scheduling for Nexus objects.
Definition Fabric.hpp:37
Object that reacts to nearby entities when committed.
Definition Sensor.hpp:19
Fluent builder that wires an entity into Fabric's scheduling infrastructure.
Definition Wiring.hpp:39
Token-based multimodal task scheduling system for unified coroutine processing.
Definition Scheduler.hpp:51
std::optional< Wiring > wiring
Definition Fabric.hpp:215
std::optional< uint32_t > spatial_id
Definition Fabric.hpp:214