MayaFlux 0.5.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches

◆ decode()

bool MayaFlux::Nexus::StateDecoder::decode ( Fabric fabric,
const std::string &  base_path 
)

Decode and apply to fabric.

Parameters
fabricTarget fabric. Must already contain entities with ids matching the schema.
base_pathPath stem without extension, same value passed to StateEncoder::encode.
Returns
True on success. Partial patches (some ids missing) still return true; failures are logged.

Definition at line 134 of file Decoder.cpp.

135{
136 m_last_error.clear();
137 m_patched_count = 0;
138 m_missing_count = 0;
139
140 const std::string json_path = base_path + ".json";
141 const std::string exr_path = base_path + ".exr";
142
143 IO::JSONSerializer ser;
144 auto schema_opt = ser.read<State::FabricSchema>(json_path);
145 if (!schema_opt) {
146 m_last_error = "Failed to load schema: " + ser.last_error();
148 return false;
149 }
150 const auto& schema = *schema_opt;
151
152 if (schema.version != State::k_schema_version) {
153 m_last_error = "Unsupported schema version: " + std::to_string(schema.version)
154 + " (expected " + std::to_string(State::k_schema_version) + ")";
156 return false;
157 }
158
159 if (schema.entities.empty()) {
160 m_last_error = "Schema contains no entities: " + json_path;
162 return false;
163 }
164
165 const uint32_t expected_rows = State::k_exr_rows;
166 auto pv_opt = load_exr(exr_path, static_cast<uint32_t>(schema.entities.size()), expected_rows, m_last_error);
167 if (!pv_opt) {
169 return false;
170 }
171 auto& pv = *pv_opt;
172 const auto* pixels = pv.image.as_float();
173 const uint32_t width = pv.width;
174 const auto& r = schema.ranges;
175
176 for (size_t i = 0; i < schema.entities.size(); ++i) {
177 const auto& entry = schema.entities[i];
178
179 if (!State::kind_known(entry.kind)) {
181 "StateDecoder: unknown kind '{}' for id {}, skipping", entry.kind, entry.id);
183 continue;
184 }
185
186 const size_t row0 = (static_cast<size_t>(0) * width + i) * State::k_channels;
187 const size_t row1 = (static_cast<size_t>(1) * width + i) * State::k_channels;
188 const size_t row2 = (static_cast<size_t>(2) * width + i) * State::k_channels;
189
190 const glm::vec3 position {
191 denormalize((*pixels)[row0 + 0], r.pos_x),
192 denormalize((*pixels)[row0 + 1], r.pos_y),
193 denormalize((*pixels)[row0 + 2], r.pos_z),
194 };
195
196 switch (State::parse_kind(entry.kind)) {
198 auto e = fabric.get_emitter(entry.id);
199 if (!e) {
201 "StateDecoder: id {} not found as Emitter, skipping", entry.id);
203 continue;
204 }
205 if (!entry.influence_fn_name.empty() && e->fn_name() != entry.influence_fn_name) {
207 "StateDecoder: Emitter {} fn_name mismatch: schema='{}' live='{}'",
208 entry.id, entry.influence_fn_name, e->fn_name());
209 }
210 e->set_position(position);
211 e->set_intensity(denormalize((*pixels)[row0 + 3], r.intensity));
212 if (entry.color) {
213 e->set_color(glm::vec3 {
214 denormalize((*pixels)[row1 + 0], r.color_r),
215 denormalize((*pixels)[row1 + 1], r.color_g),
216 denormalize((*pixels)[row1 + 2], r.color_b),
217 });
218 }
219 if (entry.size) {
220 e->set_size(denormalize((*pixels)[row1 + 3], r.size));
221 }
222 e->set_radius(denormalize((*pixels)[row2 + 0], r.radius));
223 break;
224 }
226 auto s = fabric.get_sensor(entry.id);
227 if (!s) {
229 "StateDecoder: id {} not found as Sensor, skipping", entry.id);
231 continue;
232 }
233 if (!entry.perception_fn_name.empty() && s->fn_name() != entry.perception_fn_name) {
235 "StateDecoder: Sensor {} fn_name mismatch: schema='{}' live='{}'",
236 entry.id, entry.perception_fn_name, s->fn_name());
237 }
238 s->set_position(position);
239 s->set_query_radius(denormalize((*pixels)[row2 + 1], r.query_radius));
240 break;
241 }
242 case Fabric::Kind::Agent: {
243 auto a = fabric.get_agent(entry.id);
244 if (!a) {
246 "StateDecoder: id {} not found as Agent, skipping", entry.id);
248 continue;
249 }
250 if (!entry.perception_fn_name.empty() && a->perception_fn_name() != entry.perception_fn_name) {
252 "StateDecoder: Agent {} perception_fn_name mismatch: schema='{}' live='{}'",
253 entry.id, entry.perception_fn_name, a->perception_fn_name());
254 }
255 if (!entry.influence_fn_name.empty() && a->influence_fn_name() != entry.influence_fn_name) {
257 "StateDecoder: Agent {} influence_fn_name mismatch: schema='{}' live='{}'",
258 entry.id, entry.influence_fn_name, a->influence_fn_name());
259 }
260 a->set_position(position);
261 a->set_intensity(denormalize((*pixels)[row0 + 3], r.intensity));
262 if (entry.color) {
263 a->set_color(glm::vec3 {
264 denormalize((*pixels)[row1 + 0], r.color_r),
265 denormalize((*pixels)[row1 + 1], r.color_g),
266 denormalize((*pixels)[row1 + 2], r.color_b),
267 });
268 }
269 if (entry.size) {
270 a->set_size(denormalize((*pixels)[row1 + 3], r.size));
271 }
272 a->set_radius(denormalize((*pixels)[row2 + 0], r.radius));
273 a->set_query_radius(denormalize((*pixels)[row2 + 1], r.query_radius));
274
275 if (entry.locus_nav) {
276 if (!apply_locus_nav(a, *entry.locus_nav)) {
278 "StateDecoder: Agent {} has locus_nav in schema but is not a Locus at runtime",
279 entry.id);
280 }
281 }
282
283 if (auto presence = std::dynamic_pointer_cast<Presence>(a)) {
284 if (!entry.falloff_curve_name.empty()) {
285 if (auto fc = Reflect::string_to_enum_case_insensitive<Presence::FalloffCurve>(entry.falloff_curve_name))
286 presence->set_falloff_curve(*fc);
287 }
288 if (entry.falloff_radius)
289 presence->set_falloff_radius(*entry.falloff_radius);
290 }
291 break;
292 }
293 }
294
296 }
297
298 for (const auto& xrec : schema.expanses) {
299 if (xrec.fn_name.empty()) {
301 "StateDecoder: Expanse {} has no fn_name, skipping", xrec.id);
302 continue;
303 }
304 auto contains_fn = fabric.resolve_expanse_fn(xrec.fn_name);
305 if (!contains_fn || !*contains_fn) {
307 "StateDecoder: Expanse {} fn '{}' not in registry, skipping",
308 xrec.id, xrec.fn_name);
309 continue;
310 }
311 auto on_enter_fn = xrec.on_enter_fn_name.empty()
313 : [ptr = fabric.resolve_crossing_fn(xrec.on_enter_fn_name)](uint32_t id) {
314 if (ptr && *ptr)
315 (*ptr)(id);
316 };
317 auto on_exit_fn = xrec.on_exit_fn_name.empty()
319 : [ptr = fabric.resolve_crossing_fn(xrec.on_exit_fn_name)](uint32_t id) {
320 if (ptr && *ptr)
321 (*ptr)(id);
322 };
323 auto expanse = std::make_shared<Expanse>(
324 xrec.fn_name,
325 xrec.on_enter_fn_name,
326 xrec.on_exit_fn_name,
327 *contains_fn,
328 std::move(on_enter_fn),
329 std::move(on_exit_fn));
330 fabric.add_expanse(std::move(expanse));
331 }
332
334 "StateDecoder: patched {} entities ({} missing) from {} + {}",
335 m_patched_count, m_missing_count, exr_path, json_path);
336
337 return true;
338}
#define MF_INFO(comp, ctx,...)
#define MF_ERROR(comp, ctx,...)
#define MF_WARN(comp, ctx,...)
uint32_t width
Definition Decoder.cpp:66
const std::vector< float > * pixels
Definition Decoder.cpp:65
size_t a
const uint8_t * ptr
std::function< void(uint32_t)> CrossingFn
Definition Expanse.hpp:34
@ FileIO
Filesystem I/O operations.
@ Runtime
General runtime operations (default fallback)
@ Nexus
Spatial indexing and scheduling for user-defined behaviour.
constexpr T denormalize(T t, T lo, T hi) noexcept
Denormalize t from [0, 1] to [lo, hi].
Definition Scalar.hpp:61
constexpr uint32_t k_schema_version
Current schema version written by StateEncoder and accepted by StateDecoder.
Definition Schema.hpp:20
bool kind_known(std::string_view s)
Return true if s maps to a known Fabric::Kind token.
Definition Schema.hpp:368
Fabric::Kind parse_kind(std::string_view s)
Parse a JSON kind token to Fabric::Kind (case-insensitive).
Definition Schema.hpp:379
constexpr uint32_t k_exr_rows
RGBA32F EXR layout constants shared between encoder and decoder.
Definition Schema.hpp:31
constexpr uint32_t k_channels
Definition Schema.hpp:32

References a, MayaFlux::Nexus::Fabric::add_expanse(), MayaFlux::Nexus::Fabric::Agent, MayaFlux::Nexus::Fabric::Emitter, MayaFlux::Journal::FileIO, MayaFlux::Nexus::Fabric::get_agent(), MayaFlux::Nexus::Fabric::get_emitter(), MayaFlux::Nexus::Fabric::get_sensor(), MayaFlux::Nexus::State::k_channels, MayaFlux::Nexus::State::k_exr_rows, MayaFlux::Nexus::State::k_schema_version, MayaFlux::Nexus::State::kind_known(), MayaFlux::IO::JSONSerializer::last_error(), m_last_error, m_missing_count, m_patched_count, MF_ERROR, MF_INFO, MF_WARN, MayaFlux::Journal::Nexus, MayaFlux::Nexus::State::parse_kind(), pixels, ptr, MayaFlux::IO::JSONSerializer::read(), MayaFlux::Nexus::Fabric::resolve_crossing_fn(), MayaFlux::Nexus::Fabric::resolve_expanse_fn(), MayaFlux::Journal::Runtime, MayaFlux::Nexus::Fabric::Sensor, and width.

+ Here is the call graph for this function: