MayaFlux 1.0.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
Expanse.cpp
Go to the documentation of this file.
1#include "Expanse.hpp"
2
3#include "glm/vec3.hpp"
4
5namespace MayaFlux::Nexus {
6
8{
9 static std::atomic<uint32_t> next { 1 };
10 return next.fetch_add(1, std::memory_order_relaxed);
11}
12
13void Expanse::evict(uint32_t fabric_id)
14{
15 const auto it = m_occupants_by_fabric.find(fabric_id);
16 if (it == m_occupants_by_fabric.end()) {
17 return;
18 }
19
20 const auto leaving = std::move(it->second);
21 m_occupants_by_fabric.erase(it);
22
23 for (uint32_t eid : leaving) {
24 if (m_on_exit)
25 m_on_exit(eid);
26
27 const CrossingContext ctx { .fabric_id = fabric_id, .entity = eid };
28 for (const auto& [handle, action] : m_exits)
29 action(ctx);
30 }
31}
32
33void Expanse::evaluate(uint32_t fabric_id,
34 std::span<const std::pair<uint32_t, glm::vec3>> snapshot)
35{
37
38 auto& prev = m_occupants_by_fabric[fabric_id];
39
40 std::unordered_set<uint32_t> inside;
41 for (const auto& [eid, pos] : snapshot) {
42 if (contains(pos))
43 inside.insert(eid);
44 }
45
46 if (!m_perceptions.empty()) {
47 const auto [center, radius] = reach();
48
49 std::vector<Kinesis::QueryResult> occupants;
50 occupants.reserve(inside.size());
51 for (const auto& [eid, pos] : snapshot) {
52 if (inside.contains(eid)) {
53 const glm::vec3 offset = pos - center;
54 occupants.push_back({ .id = eid, .distance_sq = glm::dot(offset, offset) });
55 }
56 }
57
58 const PerceptionContext pctx { .position = center, .radius = radius, .spatial_results = occupants };
59 for (const auto& [handle, perception] : m_perceptions)
60 perception(pctx);
61 }
62
63 for (const auto& [eid, pos] : snapshot) {
64 if (!inside.contains(eid))
65 continue;
66
67 const CrossingContext ctx { .fabric_id = fabric_id, .entity = eid, .position = pos };
68
69 if (!prev.contains(eid)) {
70 if (m_on_enter)
71 m_on_enter(eid);
72 for (const auto& [handle, action] : m_entries)
73 action(ctx);
74 }
75
76 for (const auto& [handle, action] : m_insides)
77 action(ctx);
78 }
79
80 std::unordered_map<uint32_t, glm::vec3> where;
81 if (!m_exits.empty() && !prev.empty()) {
82 where.reserve(snapshot.size());
83 for (const auto& [eid, pos] : snapshot)
84 where.emplace(eid, pos);
85 }
86
87 for (uint32_t eid : prev) {
88 if (inside.contains(eid))
89 continue;
90
91 if (m_on_exit)
92 m_on_exit(eid);
93
94 if (m_exits.empty())
95 continue;
96
97 CrossingContext ctx { .fabric_id = fabric_id, .entity = eid };
98 if (const auto it = where.find(eid); it != where.end())
99 ctx.position = it->second;
100 for (const auto& [handle, action] : m_exits)
101 action(ctx);
102 }
103
104 if (inside.empty()) {
105 m_occupants_by_fabric.erase(fabric_id);
106 } else {
107 prev = std::move(inside);
108 }
109}
110
111} // namespace MayaFlux::Nexus
uint64_t offset
Definition Composite.cpp:10
Expanse::ContainsFn contains
Definition Decoder.cpp:517
float radius
const std::unordered_set< uint32_t > * occupants(uint32_t fabric_id) const
Entity ids inside this Expanse for fabric_id, or nullptr if that fabric has no occupants.
Definition Expanse.hpp:177
static uint32_t allocate_id()
Definition Expanse.cpp:7
std::unordered_map< uint32_t, std::unordered_set< uint32_t > > m_occupants_by_fabric
Definition Expanse.hpp:354
std::vector< std::pair< std::shared_ptr< void >, PerceptionFn > > m_perceptions
Definition Expanse.hpp:349
virtual std::pair< glm::vec3, float > reach() const
Where the region is centred and how far it reaches, for perceptions.
Definition Expanse.hpp:314
void evaluate(uint32_t fabric_id, std::span< const std::pair< uint32_t, glm::vec3 > > snapshot)
Evaluate a spatial snapshot from one Fabric against this Expanse.
Definition Expanse.cpp:33
virtual void begin_evaluate()
Called at the start of every evaluate(), before any position is tested.
Definition Expanse.hpp:306
void evict(uint32_t fabric_id)
Fire the exit actions for everything inside for fabric_id and forget it.
Definition Expanse.cpp:13
uint32_t fabric_id
Id of the Fabric that evaluated the Expanse.
Definition Expanse.hpp:17
Data passed to a typed Expanse action for one entity.
Definition Expanse.hpp:16
Data passed to a Sensor or Agent perception function on each commit.