MayaFlux 0.4.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
Fabric.cpp
Go to the documentation of this file.
1#include "Fabric.hpp"
2
7
8namespace MayaFlux::Nexus {
9
11 Vruta::TaskScheduler& scheduler,
12 Vruta::EventManager& event_manager,
13 float cell_size)
14 : m_scheduler(scheduler)
15 , m_event_manager(event_manager)
16 , m_index(Kinesis::make_spatial_index_3d(cell_size))
17{
18}
19
21{
22 for (auto& [id, reg] : m_registrations) {
23 if (!reg.task_name.empty()) {
24 m_scheduler.cancel_task(reg.task_name);
25 }
26 if (!reg.chain_name.empty()) {
27 m_scheduler.cancel_task(reg.chain_name);
28 }
29 if (!reg.event_name.empty()) {
30 m_event_manager.cancel_event(reg.event_name);
31 }
32 }
33}
34
35// =============================================================================
36// Registration
37// =============================================================================
38
39Wiring Fabric::wire(std::shared_ptr<Emitter> emitter)
40{
41 Member m = emitter;
42 uint32_t id = assign_id(m);
43 Registration reg { .member = std::move(m) };
44 if (emitter->m_position.has_value()) {
45 reg.spatial_id = m_index->insert(*emitter->m_position);
46 }
47 m_registrations.try_emplace(id, std::move(reg));
48 return Wiring { *this, id };
49}
50
51Wiring Fabric::wire(std::shared_ptr<Sensor> sensor)
52{
53 Member m = sensor;
54 uint32_t id = assign_id(m);
55 Registration reg { .member = std::move(m) };
56 if (sensor->m_position.has_value()) {
57 reg.spatial_id = m_index->insert(*sensor->m_position);
58 }
59 m_registrations.try_emplace(id, std::move(reg));
60 return Wiring { *this, id };
61}
62
63Wiring Fabric::wire(std::shared_ptr<Agent> agent)
64{
65 Member m = agent;
66 uint32_t id = assign_id(m);
67 Registration reg { .member = std::move(m) };
68 if (agent->m_position.has_value()) {
69 reg.spatial_id = m_index->insert(*agent->m_position);
70 }
71 m_registrations.try_emplace(id, std::move(reg));
72 return Wiring { *this, id };
73}
74
75void Fabric::remove(uint32_t id)
76{
77 auto it = m_registrations.find(id);
78 if (it == m_registrations.end()) {
79 return;
80 }
81
82 auto& reg = it->second;
83
84 if (!reg.task_name.empty()) {
85 m_scheduler.cancel_task(reg.task_name);
86 }
87 if (!reg.chain_name.empty()) {
88 m_scheduler.cancel_task(reg.chain_name);
89 }
90 if (!reg.event_name.empty()) {
91 m_event_manager.cancel_event(reg.event_name);
92 }
93 if (reg.spatial_id.has_value()) {
94 m_index->remove(*reg.spatial_id);
95 }
96
97 m_registrations.erase(it);
98}
99
100std::vector<uint32_t> Fabric::all_ids() const
101{
102 std::vector<uint32_t> ids;
103 ids.reserve(m_registrations.size());
104 for (const auto& [id, reg] : m_registrations) {
105 ids.push_back(id);
106 }
107 return ids;
108}
109
110Fabric::Kind Fabric::kind(uint32_t id) const
111{
112 const auto& reg = m_registrations.at(id);
113 return std::visit([](const auto& ptr) -> Kind {
114 using T = std::decay_t<decltype(*ptr)>;
115 if constexpr (std::is_same_v<T, Emitter>) {
116 return Kind::Emitter;
117 } else if constexpr (std::is_same_v<T, Sensor>) {
118 return Kind::Sensor;
119 } else {
120 return Kind::Agent;
121 }
122 },
123 reg.member);
124}
125
126std::shared_ptr<Emitter> Fabric::get_emitter(uint32_t id) const
127{
128 auto it = m_registrations.find(id);
129 if (it == m_registrations.end())
130 return nullptr;
131 if (auto* ptr = std::get_if<std::shared_ptr<Emitter>>(&it->second.member)) {
132 return *ptr;
133 }
134 return nullptr;
135}
136
137std::shared_ptr<Sensor> Fabric::get_sensor(uint32_t id) const
138{
139 auto it = m_registrations.find(id);
140 if (it == m_registrations.end())
141 return nullptr;
142 if (auto* ptr = std::get_if<std::shared_ptr<Sensor>>(&it->second.member)) {
143 return *ptr;
144 }
145 return nullptr;
146}
147
148std::shared_ptr<Agent> Fabric::get_agent(uint32_t id) const
149{
150 auto it = m_registrations.find(id);
151 if (it == m_registrations.end())
152 return nullptr;
153 if (auto* ptr = std::get_if<std::shared_ptr<Agent>>(&it->second.member)) {
154 return *ptr;
155 }
156 return nullptr;
157}
158
159// =============================================================================
160// Commit
161// =============================================================================
162
164{
165 for (auto& [id, reg] : m_registrations) {
166 if (reg.spatial_id.has_value()) {
167 std::visit([&](const auto& ptr) {
168 if (ptr->m_position.has_value()) {
169 m_index->update(*reg.spatial_id, *ptr->m_position);
170 }
171 },
172 reg.member);
173 }
174 }
175
176 m_index->publish();
177
178 for (auto& [id, reg] : m_registrations) {
179 if (reg.commit_driven) {
180 fire(reg);
181 }
182 }
183}
184
185void Fabric::fire(uint32_t id)
186{
187 auto it = m_registrations.find(id);
188 if (it == m_registrations.end()) {
190 "Fabric::fire: id {} not registered", id);
191 return;
192 }
193 fire(it->second);
194}
195
196// =============================================================================
197// Spatial queries
198// =============================================================================
199
200std::vector<Kinesis::QueryResult> Fabric::within_radius(
201 const glm::vec3& center, float radius) const
202{
203 return m_index->within_radius(center, radius);
204}
205
206std::vector<Kinesis::QueryResult> Fabric::k_nearest(
207 const glm::vec3& center, uint32_t k) const
208{
209 return m_index->k_nearest(center, k);
210}
211
212const Wiring* Fabric::wiring_for(uint32_t id) const
213{
214 auto it = m_registrations.find(id);
215 if (it == m_registrations.end() || !it->second.wiring.has_value()) {
216 return nullptr;
217 }
218 return &*it->second.wiring;
219}
220
221// =============================================================================
222// Private
223// =============================================================================
224
226{
227 uint32_t id = m_next_id++;
228 std::visit([id](const auto& ptr) {
229 ptr->m_id = id;
230 },
231 m);
232 return id;
233}
234
235void Fabric::fire(const Registration& reg) const
236{
237 std::visit([&](const auto& ptr) {
238 using T = std::decay_t<decltype(*ptr)>;
239
240 if constexpr (std::is_same_v<T, Emitter>) {
242 if (ptr->m_position.has_value()) {
243 ctx.position = *ptr->m_position;
244 }
245 ctx.intensity = ptr->m_intensity;
246 ctx.radius = ptr->m_radius;
247 ctx.color = ptr->m_color;
248 ctx.size = ptr->m_size;
249 ctx.render_proc = ptr->m_influence_target;
250 ptr->invoke(ctx);
251
252 } else if constexpr (std::is_same_v<T, Sensor>) {
254 if (ptr->m_position.has_value()) {
255 ctx.position = *ptr->m_position;
256 auto results = m_index->within_radius(*ptr->m_position, ptr->m_query_radius);
257 ctx.spatial_results = std::span(results);
258 }
259 ptr->invoke(ctx);
260
261 } else if constexpr (std::is_same_v<T, Agent>) {
262 if (ptr->m_position.has_value()) {
263 auto results = m_index->within_radius(*ptr->m_position, ptr->m_query_radius);
265 pctx.position = *ptr->m_position;
266 pctx.spatial_results = std::span(results);
267 ptr->invoke_perception(pctx);
268
269 InfluenceContext ictx;
270 ictx.position = *ptr->m_position;
271 ictx.intensity = ptr->m_intensity;
272 ictx.radius = ptr->m_radius;
273 ictx.color = ptr->m_color;
274 ictx.size = ptr->m_size;
275 ictx.render_proc = ptr->m_influence_target;
276 ptr->invoke_influence(ictx);
277 } else {
278 ptr->invoke_perception(PerceptionContext {});
279 InfluenceContext ictx;
280 ictx.intensity = ptr->m_intensity;
281 ictx.radius = ptr->m_radius;
282 ictx.color = ptr->m_color;
283 ictx.size = ptr->m_size;
284 ictx.render_proc = ptr->m_influence_target;
285 ptr->invoke_influence(ictx);
286 }
287 }
288 },
289 reg.member);
290}
291
292std::shared_ptr<Emitter::InfluenceFn> Fabric::resolve_influence_fn(std::string_view name) const
293{
294 auto it = m_influence_fns.find(std::string(name));
295 return it != m_influence_fns.end() ? it->second : nullptr;
296}
297
298std::shared_ptr<Sensor::PerceptionFn> Fabric::resolve_perception_fn(std::string_view name) const
299{
300 auto it = m_perception_fns.find(std::string(name));
301 return it != m_perception_fns.end() ? it->second : nullptr;
302}
303
304} // namespace MayaFlux::Nexus
#define MF_WARN(comp, ctx,...)
Range radius
uint32_t id
std::vector< Kinesis::QueryResult > k_nearest(const glm::vec3 &center, uint32_t k) const
Find the k nearest objects to a point.
Definition Fabric.cpp:206
std::shared_ptr< Sensor > get_sensor(uint32_t id) const
Get the Sensor registered under id.
Definition Fabric.cpp:137
std::vector< uint32_t > all_ids() const
List all registered entity ids in insertion order.
Definition Fabric.cpp:100
std::shared_ptr< Emitter::InfluenceFn > resolve_influence_fn(std::string_view name) const
Look up a registered influence function by name.
Definition Fabric.cpp:292
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
void fire(uint32_t id)
Fire a single object against the current snapshot without republishing.
Definition Fabric.cpp:185
void remove(uint32_t id)
Remove a registered object by id, cancelling any associated tasks.
Definition Fabric.cpp:75
Fabric(Vruta::TaskScheduler &scheduler, Vruta::EventManager &event_manager, float cell_size=1.0F)
Construct with scheduler and event manager from the engine.
Definition Fabric.cpp:10
std::unordered_map< std::string, std::shared_ptr< Sensor::PerceptionFn > > m_perception_fns
Definition Fabric.hpp:229
std::shared_ptr< Sensor::PerceptionFn > resolve_perception_fn(std::string_view name) const
Look up a registered perception function by name.
Definition Fabric.cpp:298
std::shared_ptr< Agent > get_agent(uint32_t id) const
Get the Agent registered under id.
Definition Fabric.cpp:148
const Wiring * wiring_for(uint32_t id) const
Access the finalised wiring for an entity for introspection.
Definition Fabric.cpp:212
const std::string & name() const
Assigned name, empty if the Fabric was constructed outside a Tapestry.
Definition Fabric.hpp:66
Kind kind(uint32_t id) const
Return the kind of entity registered under id.
Definition Fabric.cpp:110
uint32_t assign_id(Member &m)
Definition Fabric.cpp:225
Vruta::TaskScheduler & m_scheduler
Definition Fabric.hpp:221
std::vector< Kinesis::QueryResult > within_radius(const glm::vec3 &center, float radius) const
Find all objects within a radius of a point.
Definition Fabric.cpp:200
std::shared_ptr< Emitter > get_emitter(uint32_t id) const
Get the Emitter registered under id.
Definition Fabric.cpp:126
Wiring wire(std::shared_ptr< Emitter > emitter)
Begin wiring an Emitter into the Fabric.
Definition Fabric.cpp:39
std::unordered_map< uint32_t, Registration > m_registrations
Definition Fabric.hpp:225
void commit()
Update all positions, publish the snapshot, fire all registered objects.
Definition Fabric.cpp:163
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
Fluent builder that wires an entity into Fabric's scheduling infrastructure.
Definition Wiring.hpp:39
bool cancel_event(const std::shared_ptr< Event > &event)
Cancels and removes a event from the manager.
bool cancel_task(const std::shared_ptr< Routine > &routine)
Cancels and removes a task from the scheduler.
Definition Scheduler.cpp:62
Token-based multimodal task scheduling system for unified coroutine processing.
Definition Scheduler.hpp:51
@ Runtime
General runtime operations (default fallback)
@ Nexus
Spatial indexing and scheduling for user-defined behaviour.
std::weak_ptr< Buffers::RenderProcessor > render_proc
glm::vec3 position
Position of the influence point in world space.
std::optional< glm::vec3 > color
Optional color hint for the influence, may be used for visualisation or shader effects.
std::optional< float > size
Optional size hint for the influence, may be used for visualisation or shader effects.
float intensity
Intensity of the influence, typically in the range [0, 1], but may exceed 1 for strong influences.
float radius
Radius of influence around the position, defining the area of effect for spatially-dependent influenc...
Data passed to an Emitter or Agent influence function on each commit.
std::span< const Kinesis::QueryResult > spatial_results
Data passed to a Sensor or Agent perception function on each commit.