98{
100
101
102
103
104 struct InternalRecord {
105 uint32_t id;
106 Fabric::Kind kind;
107 glm::vec3 position {};
108 float intensity { 0.0F };
110 float query_radius { 0.0F };
111 std::optional<glm::vec3> color;
112 std::optional<float> size;
113 std::string influence_fn_name;
114 std::string perception_fn_name;
115
116 float entity_type_norm { 0.0F };
117 float trigger_kind { 0.0F };
118 float time_kind { 0.0F };
119
120 uint32_t sink_type { 0 };
121 uint32_t first_audio_channel { 0 };
122 };
123
124 std::vector<InternalRecord> records;
125
126 for (uint32_t id : fabric.all_ids()) {
127 const auto k = fabric.kind(
id);
130 auto e = fabric.get_emitter(id);
131 if (!e || !e->position()) {
132 continue;
133 }
134 if (e->fn_name().empty()) {
136 "StateEncoder: Emitter {} has no fn_name", id);
137 }
138 auto& rec = records.emplace_back(InternalRecord {
139 .id = id,
141 .position = *e->position(),
142 .intensity = e->intensity(),
143 .radius = e->radius(),
144 .color = e->color(),
145 .size = e->size(),
146 .influence_fn_name = e->fn_name(),
147 .entity_type_norm = 0.0F,
148 });
149 fill_wiring_pixels(fabric, id, rec.trigger_kind, rec.time_kind);
150 rec.sink_type = (e->audio_sinks().empty() ? 0
U : 1U)
151 | (e->render_sinks().empty() ? 0
U : 2U);
152 if (!e->audio_sinks().empty())
153 rec.first_audio_channel = e->audio_sinks().front().channel;
154 break;
155 }
157 auto s = fabric.get_sensor(id);
158 if (!s || !s->position()) {
159 continue;
160 }
161 if (s->fn_name().empty()) {
163 "StateEncoder: Sensor {} has no fn_name", id);
164 }
165 auto& rec = records.emplace_back(InternalRecord {
166 .id = id,
168 .position = *s->position(),
169 .query_radius = s->query_radius(),
170 .perception_fn_name = s->fn_name(),
171 .entity_type_norm = 0.333F,
172 });
173 fill_wiring_pixels(fabric, id, rec.trigger_kind, rec.time_kind);
174 break;
175 }
177 auto a = fabric.get_agent(
id);
178 if (!
a || !
a->position()) {
179 continue;
180 }
181 if (
a->perception_fn_name().empty()) {
183 "StateEncoder: Agent {} has no perception_fn_name", id);
184 }
185 if (
a->influence_fn_name().empty()) {
187 "StateEncoder: Agent {} has no influence_fn_name", id);
188 }
189 auto& rec = records.emplace_back(InternalRecord {
190 .id = id,
192 .position = *
a->position(),
193 .intensity =
a->intensity(),
194 .radius =
a->radius(),
195 .query_radius =
a->query_radius(),
198 .influence_fn_name =
a->influence_fn_name(),
199 .perception_fn_name =
a->perception_fn_name(),
200 .entity_type_norm = 0.667F,
201 });
202 fill_wiring_pixels(fabric, id, rec.trigger_kind, rec.time_kind);
203 rec.sink_type = (
a->audio_sinks().empty() ? 0
U : 1U)
204 | (
a->render_sinks().empty() ? 0
U : 2U);
205 if (!
a->audio_sinks().empty())
206 rec.first_audio_channel =
a->audio_sinks().front().channel;
207 break;
208 }
209 }
210 }
211
212 if (records.empty()) {
215 return false;
216 }
217
218
219
220
221 State::RangeSet rs;
222 bool init_pos_x = false, init_pos_y = false, init_pos_z = false;
223 bool init_intensity = false, init_radius = false, init_query_radius = false;
224 bool init_color_r = false, init_color_g = false, init_color_b = false;
225 bool init_size = false;
226
227 for (const auto& rec : records) {
228 expand_range(rs.pos_x, rec.position.x, init_pos_x);
229 expand_range(rs.pos_y, rec.position.y, init_pos_y);
230 expand_range(rs.pos_z, rec.position.z, init_pos_z);
231
233 expand_range(rs.intensity, rec.intensity, init_intensity);
234 expand_range(rs.radius, rec.radius, init_radius);
235 if (rec.color) {
236 expand_range(rs.color_r, rec.color->r, init_color_r);
237 expand_range(rs.color_g, rec.color->g, init_color_g);
238 expand_range(rs.color_b, rec.color->b, init_color_b);
239 }
240 if (rec.size) {
241 expand_range(rs.size, *rec.size, init_size);
242 }
243 }
245 expand_range(rs.query_radius, rec.query_radius, init_query_radius);
246 }
247 }
248
249
250
251
252 const auto width =
static_cast<uint32_t
>(records.size());
253
259
261
262 for (size_t i = 0; i < records.size(); ++i) {
263 const auto& rec = records[i];
264
270
272 if (rec.color) {
276 }
277 if (rec.size) {
279 }
280
284
286 pixels[row3 + 0] =
static_cast<float>(rec.id);
287 pixels[row3 + 1] = rec.entity_type_norm;
288 pixels[row3 + 2] = rec.trigger_kind;
289 pixels[row3 + 3] = rec.time_kind;
290
292 pixels[row4 + 0] =
static_cast<float>(rec.sink_type);
293 pixels[row4 + 1] =
static_cast<float>(rec.first_audio_channel);
294 }
295
297
298
299
300
301 const std::string exr_path = base_path + ".exr";
303 if (!writer) {
306 return false;
307 }
308
309 IO::ImageWriteOptions options;
310 options.channel_names = { "R", "G", "B", "A" };
311
312 if (!writer->write(exr_path,
image, options)) {
313 m_last_error =
"EXR write failed: " + writer->get_last_error();
315 return false;
316 }
317
318
319
320
321 State::FabricSchema schema;
323 schema.fabric_name = fabric.name();
324 schema.ranges = rs;
325 schema.entities.reserve(records.size());
326
327 for (const auto& rec : records) {
328 State::EntityRecord ent;
329 ent.id = rec.id;
331 ent.position = rec.position;
332 ent.intensity = rec.intensity;
333 ent.radius = rec.radius;
334 ent.query_radius = rec.query_radius;
335 ent.color = rec.color;
336 ent.size = rec.size;
337 ent.influence_fn_name = rec.influence_fn_name;
338 ent.perception_fn_name = rec.perception_fn_name;
339 ent.wiring = build_wiring(fabric, rec.id);
340
342 auto e = fabric.get_emitter(rec.id);
343 for (const auto& s : e->audio_sinks())
344 ent.audio_sinks.push_back({ .channel = s.channel, .fn_name = s.fn_name });
345 for (const auto& s : e->render_sinks())
346 ent.render_sinks.push_back({ .fn_name = s.fn_name });
348 auto a = fabric.get_agent(rec.id);
349 for (
const auto& s :
a->audio_sinks())
350 ent.audio_sinks.push_back({ .channel = s.channel, .fn_name = s.fn_name });
351
352 for (
const auto& s :
a->render_sinks())
353 ent.render_sinks.push_back({ .fn_name = s.fn_name });
354
355 if (
auto locus = std::dynamic_pointer_cast<Locus>(
a)) {
356 ent.subkind = "locus";
357 const auto& nav = locus->nav();
358 ent.locus_nav = State::LocusNavRecord {
359 .eye = nav.eye,
360 .target = nav.eye + glm::vec3 { std::cos(nav.pitch) * std::sin(nav.yaw), std::sin(nav.pitch), std::cos(nav.pitch) * std::cos(nav.yaw) },
361 .up = { 0.0F, 1.0F, 0.0F },
362 .fov = nav.fov_radians,
363 .near_plane = nav.near_plane,
364 .far_plane = nav.far_plane,
365 .speed = nav.move_speed,
366 };
367 }
else if (
auto presence = std::dynamic_pointer_cast<Presence>(
a)) {
368 ent.subkind = "presence";
369 ent.radiate_fn_name = presence->radiate_fn_name();
370 ent.falloff_radius = presence->falloff_radius() != presence->query_radius()
371 ? std::optional<float>(presence->falloff_radius())
373 if (auto fc = presence->falloff_curve())
375 }
376 }
377
378 schema.entities.push_back(std::move(ent));
379 }
380
381 for (uint32_t xid : fabric.all_expanse_ids()) {
382 const auto x = fabric.get_expanse(xid);
383 if (!x)
384 continue;
385 if (x->fn_name().empty()) {
387 "StateEncoder: Expanse {} has no fn_name, skipping", xid);
388 continue;
389 }
390 schema.expanses.push_back(State::ExpanseRecord {
391 .id = xid,
392 .fn_name = x->fn_name(),
393 .on_enter_fn_name = x->on_enter_fn_name(),
394 .on_exit_fn_name = x->on_exit_fn_name(),
395 });
396 }
397
398 IO::JSONSerializer ser;
399 const std::string json_path = base_path + ".json";
400 if (!ser.write(json_path, schema)) {
401 m_last_error =
"Failed to write schema: " + ser.last_error();
403 return false;
404 }
405
407 "StateEncoder: wrote {} entities to {} + {}",
408 records.size(), exr_path, json_path);
409
410 return true;
411}
#define MF_INFO(comp, ctx,...)
#define MF_ERROR(comp, ctx,...)
#define MF_WARN(comp, ctx,...)
const std::vector< float > * pixels
std::unique_ptr< ImageWriter > create_writer(const std::string &filepath) const
static ImageWriterRegistry & instance()
@ FileIO
Filesystem I/O operations.
@ Nexus
Spatial indexing and scheduling for user-defined behaviour.
constexpr uint32_t k_schema_version
Current schema version written by StateEncoder and accepted by StateDecoder.
constexpr uint32_t k_exr_rows
RGBA32F EXR layout constants shared between encoder and decoder.
constexpr uint32_t k_channels
std::string kind_to_string(Fabric::Kind k)
Map Fabric::Kind to its lowercase JSON string token via magic_enum.
@ RGBA32F
Four channel 32-bit float.
std::string enum_to_lowercase_string(EnumType value) noexcept
Universal enum to lowercase string converter using magic_enum.
void normalize(std::vector< double > &data, double target_peak)
Normalize single-channel data to specified peak level (in-place)