MayaFlux 0.5.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
ParticleNetwork.cpp
Go to the documentation of this file.
1#include "ParticleNetwork.hpp"
2
4
6
8
9//-----------------------------------------------------------------------------
10// Construction
11//-----------------------------------------------------------------------------
12
14 size_t num_particles,
15 const glm::vec3& bounds_min,
16 const glm::vec3& bounds_max,
18 : m_num_points(num_particles)
19 , m_bounds(bounds_min, bounds_max)
20 , m_init_mode(init_mode)
21{
24 m_operator_chain = std::make_shared<OperatorChain>();
25
27 "Created ParticleNetwork with {} points, bounds [{:.2f}, {:.2f}, {:.2f}] to [{:.2f}, {:.2f}, {:.2f}]",
28 num_particles,
29 bounds_min.x, bounds_min.y, bounds_min.z,
30 bounds_max.x, bounds_max.y, bounds_max.z);
31}
32
33//-----------------------------------------------------------------------------
34// NodeNetwork Interface
35//-----------------------------------------------------------------------------
36
38{
39 if (m_initialized) {
40 return;
41 }
42
43 auto positions = generate_initial_vertices();
44
46 auto physics = std::make_unique<PhysicsOperator>();
47 physics->set_bounds(m_bounds.min, m_bounds.max);
48 physics->initialize(positions);
49 m_operator = std::move(physics);
50 }
51
52 m_initialized = true;
53
55 "Initialized ParticleNetwork: {} points, operator={}",
57 m_operator ? m_operator->get_type_name() : "none");
58}
59
61 size_t num_particles,
62 const glm::vec3& bounds_min,
63 const glm::vec3& bounds_max,
65{
66 m_num_points = num_particles;
67 m_bounds = { .min = bounds_min, .max = bounds_max };
68 m_init_mode = init_mode;
69
70 reset();
71}
72
74{
75 auto vertices = generate_initial_vertices();
76
77 if (m_operator) {
78 if (auto* physics = dynamic_cast<PhysicsOperator*>(m_operator.get())) {
79 physics->initialize(vertices);
80 } else if (auto* field = dynamic_cast<FieldOperator*>(m_operator.get())) {
81 field->initialize(vertices);
82 }
83 } else {
84 auto physics = std::make_unique<PhysicsOperator>();
85 physics->set_bounds(m_bounds.min, m_bounds.max);
86 physics->initialize(vertices);
87 m_operator = std::move(physics);
88 }
89
91 "Reset ParticleNetwork: {} points reinitialized", m_num_points);
92}
93
94void ParticleNetwork::process_batch(unsigned int num_samples)
95{
97
98 if (!is_enabled()) {
99 return;
100 }
101
103
104 if (m_operator) {
105 for (unsigned int frame = 0; frame < num_samples; ++frame) {
106 m_operator->process(m_timestep);
107 }
108 }
109
110 if (m_operator_chain && !m_operator_chain->empty()) {
111 for (unsigned int frame = 0; frame < num_samples; ++frame) {
112 m_operator_chain->process(m_timestep, m_operator.get());
113 }
114 }
115
117 "ParticleNetwork processed {} frames with {} operator",
118 num_samples, m_operator->get_type_name());
119}
120
122{
124
125 if (auto* physics = dynamic_cast<PhysicsOperator*>(m_operator.get())) {
126 bool should_interact = (topology == Topology::SPATIAL || topology == Topology::GRID_2D || topology == Topology::GRID_3D);
127 physics->enable_spatial_interactions(should_interact);
128 }
129}
130
132{
133 if (!m_operator) {
134 return m_num_points;
135 }
136
137 if (auto* graphics_op = dynamic_cast<const GraphicsOperator*>(m_operator.get())) {
138 return graphics_op->get_point_count();
139 }
140
141 return m_num_points;
142}
143
144std::optional<double> ParticleNetwork::get_node_output(size_t index) const
145{
146 if (!m_operator) {
147 return std::nullopt;
148 }
149
150 if (auto* physics = dynamic_cast<const PhysicsOperator*>(m_operator.get())) {
151 return physics->get_particle_velocity(index);
152 }
153
154 return std::nullopt;
155}
156
157std::unordered_map<std::string, std::string> ParticleNetwork::get_metadata() const
158{
159 auto metadata = NodeNetwork::get_metadata();
160
161 metadata["point_count"] = std::to_string(get_node_count());
162 metadata["operator"] = std::string(m_operator ? m_operator->get_type_name() : "none");
163 metadata["timestep"] = std::to_string(m_timestep);
164 metadata["bounds_min"] = std::format("({:.2f}, {:.2f}, {:.2f})",
166 metadata["bounds_max"] = std::format("({:.2f}, {:.2f}, {:.2f})",
168
169 if (m_operator) {
170 if (auto* physics = dynamic_cast<PhysicsOperator*>(m_operator.get())) {
171 metadata["gravity"] = std::format("({:.2f}, {:.2f}, {:.2f})",
172 physics->get_gravity().x,
173 physics->get_gravity().y,
174 physics->get_gravity().z);
175 metadata["drag"] = std::to_string(physics->get_drag());
176
177 auto avg_vel = physics->query_state("avg_velocity");
178 if (avg_vel) {
179 metadata["avg_velocity"] = std::to_string(*avg_vel);
180 }
181 }
182 }
183
184 return metadata;
185}
186
187//-----------------------------------------------------------------------------
188// Operator Management
189//-----------------------------------------------------------------------------
190
191void ParticleNetwork::set_operator(std::unique_ptr<NetworkOperator> op)
192{
193 if (!op) {
195 "Cannot set null operator");
196 return;
197 }
198
199 if (!is_compatible(*op)) {
201 "ParticleNetwork: unsupported operator type '{}' rejected as primary. "
202 "The primary must own PointVertex storage; a chain operator need not.",
203 op->get_type_name());
204 return;
205 }
206
207 const char* old_name = m_operator ? m_operator->get_type_name().data() : "none";
208 const char* new_name = op->get_type_name().data();
209
211 "Switching operator: '{}' → '{}'",
212 old_name, new_name);
213
214 std::vector<PointVertex> vertices;
215
216 if (auto* old_graphics = dynamic_cast<PhysicsOperator*>(m_operator.get())) {
217 vertices = old_graphics->extract_vertices();
218
220 "Extracted {} vertices from old operator",
221 vertices.size());
222 } else if (auto* old_field = dynamic_cast<FieldOperator*>(m_operator.get())) {
223 vertices = old_field->extract_point_vertices();
224
226 "Extracted {} vertices from old FieldOperator",
227 vertices.size());
228 } else if (!m_operator) {
229 vertices = generate_initial_vertices();
230 }
231
232 if (auto* new_graphics = dynamic_cast<PhysicsOperator*>(op.get())) {
233 new_graphics->initialize(vertices);
234
235 if (auto* physics = dynamic_cast<PhysicsOperator*>(op.get())) {
236 physics->set_bounds(m_bounds.min, m_bounds.max);
237 }
238
240 "Initialized new graphics operator with {} points",
241 vertices.size());
242 }
243
244 if (auto* new_field = dynamic_cast<FieldOperator*>(op.get())) {
245 new_field->initialize(vertices);
246
248 "Initialized new FieldOperator with {} points",
249 vertices.size());
250 }
251
252 m_operator = std::move(op);
253
254 if (auto* physics = dynamic_cast<PhysicsOperator*>(m_operator.get())) {
255 bool should_interact = (get_topology() == Topology::SPATIAL);
256 physics->enable_spatial_interactions(should_interact);
257 }
258
260 "Operator switched successfully to '{}'", new_name);
261}
262
263//-----------------------------------------------------------------------------
264// Parameter Mapping (Delegates to Operator)
265//-----------------------------------------------------------------------------
266
268 const std::string& param_name,
269 const std::shared_ptr<Node>& source,
270 MappingMode mode)
271{
272 NodeNetwork::map_parameter(param_name, source, mode);
273}
274
275void ParticleNetwork::unmap_parameter(const std::string& param_name)
276{
278}
279
281{
282 if (!m_operator) {
283 return;
284 }
285
286 for (const auto& mapping : m_parameter_mappings) {
287 if (mapping.mode == MappingMode::BROADCAST && mapping.broadcast_source) {
288 double value = mapping.broadcast_source->get_last_output();
289 m_operator->set_parameter(mapping.param_name, value);
290
291 } else if (mapping.mode == MappingMode::ONE_TO_ONE && mapping.network_source) {
292 m_operator->apply_one_to_one(mapping.param_name, mapping.network_source);
293 }
294 }
295}
296
297//-----------------------------------------------------------------------------
298// Internal Helpers
299//-----------------------------------------------------------------------------
300
302{
303 if (!m_initialized) {
304 initialize();
305 }
306}
307
309{
311 return Kakshya::to_point_vertices(samples, { 8.0F, 12.0F });
312}
313
320
322{
323 return dynamic_cast<const PhysicsOperator*>(&op) != nullptr
324 || dynamic_cast<const FieldOperator*>(&op) != nullptr;
325}
326
327} // namespace MayaFlux::Nodes::Network
#define MF_INFO(comp, ctx,...)
#define MF_ERROR(comp, ctx,...)
#define MF_RT_TRACE(comp, ctx,...)
#define MF_DEBUG(comp, ctx,...)
uint32_t index
Definition VKDevice.cpp:142
float value
Pure field-driven vertex manipulation via Tendency evaluation.
Operator that produces GPU-renderable geometry.
Domain-agnostic interpretive lens for network processing.
std::vector< ParameterMapping > m_parameter_mappings
virtual void unmap_parameter(const std::string &param_name)
Remove parameter mapping.
Topology get_topology() const
Get the current topology.
virtual void map_parameter(const std::string &param_name, const std::shared_ptr< Node > &source, MappingMode mode=MappingMode::BROADCAST)
Map external node output to network parameter.
virtual void set_topology(Topology topology)
Set the network's topology.
bool is_enabled() const
Check if network is enabled.
std::shared_ptr< OperatorChain > m_operator_chain
virtual std::unordered_map< std::string, std::string > get_metadata() const
Get network metadata for debugging/visualization.
void set_output_mode(OutputMode mode)
Set the network's output routing mode.
std::optional< double > get_node_output(size_t index) const override
Get output value for specific particle.
Kinesis::Stochastic::Stochastic m_random_gen
void set_operator(std::unique_ptr< NetworkOperator > op)
Set active operator (runtime switching)
void process_batch(unsigned int num_samples) override
Process the network for the given number of samples.
void reinitialize(size_t num_particles, const glm::vec3 &bounds_min, const glm::vec3 &bounds_max, Kinesis::SpatialDistribution init_mode)
Reinitialize particle network with new parameters.
void unmap_parameter(const std::string &param_name) override
Remove parameter mapping.
void set_topology(Topology topology) override
Set the network's topology.
std::vector< PointVertex > generate_initial_vertices()
void initialize() override
Called once before first process_batch()
static bool is_compatible(const NetworkOperator &op) noexcept
Whether an operator can serve as this network's primary.
PointVertex generate_single_vertex(Kinesis::SpatialDistribution mode, size_t index, size_t total)
void update_mapped_parameters()
Update mapped parameters before physics step.
void reset() override
Reset network to initial state.
std::unique_ptr< NetworkOperator > m_operator
void map_parameter(const std::string &param_name, const std::shared_ptr< Node > &source, MappingMode mode=MappingMode::BROADCAST) override
Map external node output to network parameter.
std::unordered_map< std::string, std::string > get_metadata() const override
Get network metadata for debugging/visualization.
ParticleNetwork(size_t num_particles, const glm::vec3 &bounds_min=glm::vec3(-10.0F), const glm::vec3 &bounds_max=glm::vec3(10.0F), Kinesis::SpatialDistribution init_mode=Kinesis::SpatialDistribution::RANDOM_VOLUME)
Create particle network with spatial bounds.
size_t get_node_count() const override
Get number of particles in network.
N-body physics simulation with point rendering.
@ NodeProcessing
Node graph processing (Nodes::NodeGraphManager)
@ Nodes
DSP Generator and Filter Nodes, graph pipeline, node management.
PointVertex to_point_vertex(const Vertex &s, glm::vec2 size_range={ 8.0F, 12.0F }) noexcept
Project raw Vertex to PointVertex.
std::vector< PointVertex > to_point_vertices(std::span< const Vertex > vertices, glm::vec2 size_range)
Batch-project raw Vertex vector to PointVertex.
Vertex generate_sample_at(SpatialDistribution dist, size_t index, size_t total, const SamplerBounds &bounds, Stochastic::Stochastic &rng)
Generate a single sample at a specific index (for indexed/sequential modes).
std::vector< Vertex > generate_samples(SpatialDistribution dist, size_t count, const SamplerBounds &bounds, Stochastic::Stochastic &rng)
Generate a batch of spatially distributed samples.
SpatialDistribution
Spatial distribution mode for point cloud and particle generation.
Topology
Defines the structural relationships between nodes in the network.
@ GRID_2D
2D lattice with 4-connectivity
@ INDEPENDENT
No connections, nodes process independently.
@ GRID_3D
3D lattice with 6-connectivity
@ SPATIAL
Dynamic proximity-based (nodes within radius interact)
MappingMode
Defines how nodes map to external entities (e.g., audio channels, graphics objects)
@ ONE_TO_ONE
Node array/network → network nodes (must match count)
@ BROADCAST
One node → all network nodes.
@ GRAPHICS_BIND
State available for visualization (read-only)
Vertex type for point primitives (POINT_LIST topology)