15 "PhysicsOperator created");
24 if (vertices.empty()) {
26 "Cannot initialize PhysicsOperator with zero vertices");
34 "PhysicsOperator initialized with {} points", vertices.size());
42 const std::vector<std::vector<PointVertex>>& collections)
44 for (
const auto& collection : collections) {
49 "PhysicsOperator initialized with {} collections",
54 const std::vector<PointVertex>& vertices,
55 float mass_multiplier)
57 if (vertices.empty()) {
59 "Cannot add collection with zero vertices");
64 group.
collection = std::make_shared<GpuSync::PointCollectionNode>();
68 for (
size_t i = 0; i < vertices.size(); ++i) {
71 .force = glm::vec3(0.0F),
72 .mass = mass_multiplier
79 uint32_t expected = 0;
81 std::memory_order_acquire, std::memory_order_relaxed)) {
82 if (
m_shutdown.load(std::memory_order_relaxed))
92 "Added collection #{} with {} points (mass_mult={:.2f})",
109 std::vector<PointVertex> vertices(
count);
110 std::memcpy(vertices.data(), data.data(), data.size());
114 "PhysicsOperator seeded {} vertices from upstream '{}'",
134 uint32_t expected = 0;
136 std::memory_order_acquire, std::memory_order_relaxed)) {
137 if (
m_shutdown.load(std::memory_order_relaxed))
143 group.collection->compute_frame();
155 return Reflect::string_to_enum_case_insensitive<PhysicsParameter>(param);
164 Reflect::string_to_enum_or_throw_case_insensitive<PhysicsParameter>(
165 param,
"PhysicsOperator parameter");
166 }
catch (
const std::invalid_argument& e) {
171 "Unknown physics parameter: '{}'", param);
175 switch (*param_enum) {
186 m_drag = glm::clamp(
static_cast<float>(value), 0.0F, 1.0F);
203 auto&
points = group.collection->get_points();
220 if (query ==
"point_count") {
223 if (query ==
"collection_count") {
226 if (query ==
"avg_velocity") {
228 size_t total_points = 0;
231 for (
const auto& state : group.physics_state) {
232 avg += state.velocity;
237 if (total_points > 0) {
238 avg /=
static_cast<float>(total_points);
240 return static_cast<double>(glm::length(avg));
252 std::vector<PointVertex> positions;
255 const auto&
points = group.collection->get_points();
256 for (
const auto& pt :
points) {
257 positions.push_back(pt);
276 auto span = group.collection->get_vertex_data();
279 span.begin(), span.end());
286 size_t current_offset = 0;
289 size_t group_size = group.physics_state.size();
291 if (global_index < current_offset + group_size) {
292 size_t local_index = global_index - current_offset;
293 return static_cast<double>(glm::length(group.physics_state[local_index].velocity));
296 current_offset += group_size;
308 auto layout_opt =
m_collections[0].collection->get_vertex_layout();
309 if (!layout_opt.has_value()) {
313 auto layout = *layout_opt;
322 total += group.collection->get_vertex_count();
329 return std::ranges::any_of(
331 [](
const auto& group) {
return group.collection->needs_gpu_update(); });
337 group.collection->mark_vertex_data_dirty(
false);
345 total += group.collection->get_point_count();
367 std::string_view param,
368 const std::shared_ptr<NodeNetwork>& source)
372 if (source->get_node_count() != point_count) {
374 "ONE_TO_ONE size mismatch: {} particles vs {} source nodes",
375 point_count, source->get_node_count());
379 if (param ==
"force_x" || param ==
"force_y" || param ==
"force_z") {
381 }
else if (param ==
"mass") {
389 std::string_view param,
390 const std::shared_ptr<NodeNetwork>& source)
392 size_t global_index = 0;
395 for (
auto& i : group.physics_state) {
396 auto val = source->get_node_output(global_index++);
400 auto force =
static_cast<float>(*val);
402 if (param ==
"force_x") {
404 }
else if (param ==
"force_y") {
406 }
else if (param ==
"force_z") {
414 const std::shared_ptr<NodeNetwork>& source)
416 size_t global_index = 0;
419 for (
auto& i : group.physics_state) {
420 auto val = source->get_node_output(global_index++);
424 i.mass = std::max(0.1F,
static_cast<float>(*val));
436 for (
auto& state : group.physics_state) {
455 auto&
points = group.collection->get_points();
457 for (
size_t i = 0; i <
points.size(); ++i) {
459 group.physics_state[i].force += field(
points[i].position);
471 for (
auto& state : group.physics_state) {
472 state.force += field(glm::vec3(0.0F));
481 auto& points1 = group1.collection->get_points();
483 for (
size_t i = 0; i < points1.size(); ++i) {
484 const auto& pos_i = points1[i].position;
485 auto& state_i = group1.physics_state[i];
489 auto& points2 = group2.collection->get_points();
491 size_t start_j = (g1 == g2) ? i + 1 : 0;
493 for (
size_t j = start_j; j < points2.size(); ++j) {
494 const auto& pos_j = points2[j].position;
495 auto& state_j = group2.physics_state[j];
497 glm::vec3 delta = pos_j - pos_i;
498 float distance = glm::length(delta);
500 if (distance < m_interaction_radius && distance > 0.001F) {
501 glm::vec3 direction = delta / distance;
505 float repulsion_force = 0.0F;
510 glm::vec3 force = direction * (spring_force - repulsion_force);
512 state_i.force += force;
513 state_j.force -= force;
526 auto&
points = group.collection->get_points();
528 for (
size_t i = 0; i <
points.size(); ++i) {
529 group.physics_state[i].force += field(
points[i].position) * group.physics_state[i].mass;
537 auto&
points = group.collection->get_points();
539 for (
size_t i = 0; i <
points.size(); ++i) {
540 auto& state = group.physics_state[i];
543 glm::vec3 acceleration = state.force / state.mass;
544 state.velocity += acceleration * dt;
545 state.velocity *= (1.0F -
m_drag);
546 vertex.position += state.velocity * dt;
548 state.force = glm::vec3(0.0F);
559 constexpr float damping = 0.8F;
562 auto&
points = group.collection->get_points();
564 for (
size_t i = 0; i <
points.size(); ++i) {
566 auto& state = group.physics_state[i];
568 for (
int axis = 0; axis < 3; ++axis) {
573 state.velocity[axis] *= -damping;
580 state.velocity[axis] = 0.0F;
585 }
else if (vertex.position[axis] >
m_bounds.
max[axis]) {
589 state.velocity[axis] *= -damping;
596 state.velocity[axis] = 0.0F;
610 group.collection->mark_vertex_data_dirty(
true);
619 size_t local_index = global_index - offset;
620 return &group.collection->get_points()[local_index];
622 offset += group.collection->get_point_count();
630 for (
auto& state : group.physics_state) {
631 state.velocity += impulse / state.mass;
641 size_t local_index = index - offset;
642 group.physics_state[local_index].velocity += impulse / group.physics_state[local_index].mass;
645 offset += group.collection->get_point_count();
662 "Cleared all force fields");
#define MF_ERROR(comp, ctx,...)
#define MF_WARN(comp, ctx,...)
#define MF_DEBUG(comp, ctx,...)
std::vector< glm::vec2 > * points
void apply_one_to_one(std::string_view param, const std::shared_ptr< NodeNetwork > &source) override
Apply ONE_TO_ONE parameter mapping.
virtual std::span< const uint8_t > get_vertex_data() const =0
Get vertex data for GPU upload.
Operator that produces GPU-renderable geometry.
virtual std::string_view get_type_name() const =0
Type name for introspection.
Kinesis::SamplerBounds m_bounds
std::vector< PointVertex > extract_vertices() const
Extract current vertex data as PointVertex array.
size_t get_vertex_count() const override
Get number of vertices (may differ from point count for topology/path)
void apply_impulse(size_t index, const glm::vec3 &impulse)
Apply impulse to specific particle.
size_t get_point_count() const override
Get source point count (before topology expansion)
std::vector< CollectionGroup > m_collections
std::atomic< bool > m_shutdown
Kinesis::Stochastic::Stochastic m_random_generator
void mark_vertex_data_clean() override
Clear dirty flag after GPU upload.
void apply_per_particle_force(std::string_view param, const std::shared_ptr< NodeNetwork > &source)
std::optional< double > get_particle_velocity(size_t global_index) const
Get velocity magnitude for specific particle.
Kakshya::VertexLayout get_vertex_layout() const override
Get vertex layout describing vertex structure.
void set_attraction_point(const glm::vec3 &point)
std::vector< uint8_t > m_vertex_data_aggregate
std::span< const uint8_t > get_vertex_data() const override
Get vertex data for GPU upload.
void initialize_collections(const std::vector< std::vector< PointVertex > > &collections)
Initialize multiple physics collections.
bool is_vertex_data_dirty() const override
Check if geometry changed this frame.
@ BOUNCE
Reflect off boundaries with damping.
@ NONE
No bounds checking.
@ WRAP
Teleport to opposite side.
bool m_has_attraction_point
void apply_per_particle_mass(const std::shared_ptr< NodeNetwork > &source)
void add_collection(const std::vector< PointVertex > &vertices, float mass_multiplier=1.0F)
Add a single physics collection.
void set_bounds(const glm::vec3 &min, const glm::vec3 &max)
Set the simulation bounds.
float m_attraction_strength
float m_turbulence_strength
std::vector< Kinesis::VectorField > m_force_fields
bool m_spatial_interactions_enabled
void * get_data_at(size_t global_index) override
Get mutable access to point at global index.
void sync_to_point_collection()
void handle_boundary_conditions()
std::optional< double > query_state(std::string_view query) const override
Query operator internal state.
void process(float dt) override
Process for one batch cycle.
void seed_from_upstream(const GraphicsOperator *upstream) override
Seed physics state from upstream operator's vertex data.
std::span< const uint8_t > get_vertex_data_for_collection(uint32_t idx) const override
Get vertex data for specific collection (if multiple)
void clear_force_fields()
Remove all external force fields.
float m_repulsion_strength
float m_interaction_radius
void add_force_field(Kinesis::VectorField field)
Add an external force field evaluated per-particle per-frame.
void apply_attraction_forces()
void apply_spatial_interactions()
glm::vec3 m_attraction_point
void set_parameter(std::string_view param, double value) override
Set operator parameter.
void apply_one_to_one(std::string_view param, const std::shared_ptr< NodeNetwork > &source) override
Apply ONE_TO_ONE parameter for physics-specific properties.
void initialize(const std::vector< PointVertex > &vertices)
Initialize with a single physics collection.
void apply_global_impulse(const glm::vec3 &impulse)
Apply impulse to all particles.
static std::optional< PhysicsParameter > string_to_parameter(std::string_view param)
std::atomic< uint32_t > m_access_token
@ NodeProcessing
Node graph processing (Nodes::NodeGraphManager)
@ Nodes
DSP Generator and Filter Nodes, graph pipeline, node management.
VectorField turbulence(float strength, Stochastic::Stochastic rng=Stochastic::Stochastic())
Uniform random force field using Stochastic infrastructure.
VectorField point_attractor(const glm::vec3 &anchor, float strength)
Radial attraction/repulsion toward an anchor point.
Kakshya::PointVertex PointVertex
uint32_t vertex_count
Total number of vertices in this buffer.
Complete description of vertex data layout in a buffer.
Typed, composable, stateless callable from domain D to range R.
std::shared_ptr< GpuSync::PointCollectionNode > collection
std::vector< PhysicsState > physics_state
Physics-specific data parallel to PointVertex array.