[Bf-blender-cvs] [9efc8d1d9fb] functions: combine particle and world state in c api

Jacques Lucke noreply at git.blender.org
Mon Aug 26 15:37:21 CEST 2019


Commit: 9efc8d1d9fbb488af70a37681f71695227af8041
Author: Jacques Lucke
Date:   Mon Aug 26 09:57:40 2019 +0200
Branches: functions
https://developer.blender.org/rB9efc8d1d9fbb488af70a37681f71695227af8041

combine particle and world state in c api

===================================================================

M	source/blender/modifiers/intern/MOD_bparticles.c
M	source/blender/simulations/BParticles.h
M	source/blender/simulations/bparticles/c_wrapper.cpp

===================================================================

diff --git a/source/blender/modifiers/intern/MOD_bparticles.c b/source/blender/modifiers/intern/MOD_bparticles.c
index 739558a6b2f..a360f3e59b8 100644
--- a/source/blender/modifiers/intern/MOD_bparticles.c
+++ b/source/blender/modifiers/intern/MOD_bparticles.c
@@ -49,8 +49,7 @@
 #include "BParticles.h"
 
 typedef struct RuntimeData {
-  BParticlesState particles_state;
-  BParticlesWorldState world_state;
+  BParticlesSimulationState simulation_state;
   float last_simulated_frame;
 } RuntimeData;
 
@@ -58,8 +57,7 @@ static RuntimeData *get_runtime_struct(BParticlesModifierData *bpmd)
 {
   if (bpmd->modifier.runtime == NULL) {
     RuntimeData *runtime = MEM_callocN(sizeof(RuntimeData), __func__);
-    runtime->particles_state = NULL;
-    runtime->world_state = NULL;
+    runtime->simulation_state = NULL;
     runtime->last_simulated_frame = 0.0f;
     bpmd->modifier.runtime = runtime;
   }
@@ -69,8 +67,7 @@ static RuntimeData *get_runtime_struct(BParticlesModifierData *bpmd)
 
 static void free_runtime_data(RuntimeData *runtime)
 {
-  BParticles_state_free(runtime->particles_state);
-  BParticles_world_state_free(runtime->world_state);
+  BParticles_simulation_free(runtime->simulation_state);
   MEM_freeN(runtime);
 }
 
@@ -95,37 +92,33 @@ static Mesh *applyModifier(ModifierData *md,
 
   RuntimeData *runtime = get_runtime_struct(bpmd);
 
-  if (runtime->particles_state == NULL) {
-    runtime->particles_state = BParticles_new_empty_state();
-    runtime->world_state = BParticles_new_world_state();
+  if (runtime->simulation_state == NULL) {
+    runtime->simulation_state = BParticles_new_simulation();
   }
 
   if (current_frame == runtime->last_simulated_frame) {
     /* do nothing */
   }
   else if (current_frame == runtime->last_simulated_frame + 1.0f) {
-    BParticles_simulate_modifier(
-        bpmd, ctx->depsgraph, runtime->particles_state, runtime->world_state, 1.0f / FPS);
+    BParticles_simulate_modifier(bpmd, ctx->depsgraph, runtime->simulation_state, 1.0f / FPS);
     runtime->last_simulated_frame = current_frame;
   }
   else {
     free_modifier_runtime_data(bpmd);
     runtime = get_runtime_struct(bpmd);
-    runtime->particles_state = BParticles_new_empty_state();
-    runtime->world_state = BParticles_new_world_state();
+    runtime->simulation_state = BParticles_new_simulation();
     runtime->last_simulated_frame = current_frame;
     BParticles_modifier_free_cache(bpmd_orig);
 
-    BParticles_simulate_modifier(
-        bpmd, ctx->depsgraph, runtime->particles_state, runtime->world_state, 0.0f);
+    BParticles_simulate_modifier(bpmd, ctx->depsgraph, runtime->simulation_state, 0.0f);
     runtime->last_simulated_frame = current_frame;
   }
 
   if (bpmd->output_type == MOD_BPARTICLES_OUTPUT_POINTS) {
-    return BParticles_modifier_point_mesh_from_state(runtime->particles_state);
+    return BParticles_modifier_point_mesh_from_state(runtime->simulation_state);
   }
   else {
-    return BParticles_modifier_mesh_from_state(runtime->particles_state);
+    return BParticles_modifier_mesh_from_state(runtime->simulation_state);
   }
 }
 
diff --git a/source/blender/simulations/BParticles.h b/source/blender/simulations/BParticles.h
index ef844f73471..159caa410e1 100644
--- a/source/blender/simulations/BParticles.h
+++ b/source/blender/simulations/BParticles.h
@@ -13,30 +13,23 @@ struct Depsgraph;
 struct BParticlesModifierData;
 struct BParticlesFrameCache;
 
-typedef struct OpaqueBParticlesState *BParticlesState;
-typedef struct OpaqueBParticlesWorldState *BParticlesWorldState;
+typedef struct OpaqueBParticlesSimulationState *BParticlesSimulationState;
 
-BParticlesState BParticles_new_empty_state(void);
-void BParticles_state_free(BParticlesState particles_state);
-
-BParticlesWorldState BParticles_new_world_state(void);
-void BParticles_world_state_free(BParticlesWorldState world_state);
+BParticlesSimulationState BParticles_new_simulation(void);
+void BParticles_simulation_free(BParticlesSimulationState simulation_state);
 
 void BParticles_simulate_modifier(struct BParticlesModifierData *bpmd,
                                   Depsgraph *depsgraph,
-                                  BParticlesState particles_state,
-                                  BParticlesWorldState world_state,
+                                  BParticlesSimulationState simulation_state,
                                   float time_step);
 
-uint BParticles_state_particle_count(BParticlesState particles_state);
-void BParticles_state_get_positions(BParticlesState particles_state, float (*dst)[3]);
+Mesh *BParticles_modifier_point_mesh_from_state(BParticlesSimulationState simulation_state);
+Mesh *BParticles_modifier_mesh_from_state(BParticlesSimulationState simulation_state);
 
-Mesh *BParticles_modifier_point_mesh_from_state(BParticlesState state);
-Mesh *BParticles_modifier_mesh_from_state(BParticlesState particles_state);
 void BParticles_modifier_free_cache(struct BParticlesModifierData *bpmd);
 struct Mesh *BParticles_modifier_mesh_from_cache(struct BParticlesFrameCache *cached_frame);
 void BParticles_modifier_cache_state(struct BParticlesModifierData *bpmd,
-                                     BParticlesState particles_state,
+                                     BParticlesSimulationState simulation_state,
                                      float frame);
 
 #ifdef __cplusplus
diff --git a/source/blender/simulations/bparticles/c_wrapper.cpp b/source/blender/simulations/bparticles/c_wrapper.cpp
index c82bb5feb94..be8dff6a2ae 100644
--- a/source/blender/simulations/bparticles/c_wrapper.cpp
+++ b/source/blender/simulations/bparticles/c_wrapper.cpp
@@ -35,36 +35,29 @@ using BLI::float3;
 using BLI::StringRef;
 using BLI::Vector;
 
-WRAPPERS(ParticlesState *, BParticlesState);
+struct SimulationState {
+  std::unique_ptr<ParticlesState> particles_state;
+  std::unique_ptr<WorldState> world_state;
+};
+
+WRAPPERS(SimulationState *, BParticlesSimulationState);
 
-BParticlesState BParticles_new_empty_state()
+BParticlesSimulationState BParticles_new_simulation()
 {
-  ParticlesState *state = new ParticlesState();
+  SimulationState *state = new SimulationState();
+  state->particles_state = std::unique_ptr<ParticlesState>(new ParticlesState());
+  state->world_state = std::unique_ptr<WorldState>(new WorldState());
   return wrap(state);
 }
 
-void BParticles_state_free(BParticlesState state_c)
+void BParticles_simulation_free(BParticlesSimulationState state_c)
 {
   delete unwrap(state_c);
 }
 
-WRAPPERS(WorldState *, BParticlesWorldState);
-
-BParticlesWorldState BParticles_new_world_state()
-{
-  WorldState *world_state = new WorldState();
-  return wrap(world_state);
-}
-
-void BParticles_world_state_free(BParticlesWorldState world_state_c)
-{
-  delete unwrap(world_state_c);
-}
-
 void BParticles_simulate_modifier(BParticlesModifierData *bpmd,
                                   Depsgraph *UNUSED(depsgraph),
-                                  BParticlesState particles_state_c,
-                                  BParticlesWorldState world_state_c,
+                                  BParticlesSimulationState state_c,
                                   float time_step)
 {
   SCOPED_TIMER(__func__);
@@ -73,7 +66,8 @@ void BParticles_simulate_modifier(BParticlesModifierData *bpmd,
     return;
   }
 
-  WorldState &world_state = *unwrap(world_state_c);
+  SimulationState &simulation_state = *unwrap(state_c);
+  WorldState &world_state = *simulation_state.world_state;
 
   bNodeTree *btree = (bNodeTree *)DEG_get_original_id((ID *)bpmd->bparticles_tree);
 
@@ -83,7 +77,7 @@ void BParticles_simulate_modifier(BParticlesModifierData *bpmd,
 
   auto step_description = step_description_from_node_tree(vtree, world_state, time_step);
 
-  ParticlesState &particles_state = *unwrap(particles_state_c);
+  ParticlesState &particles_state = *simulation_state.particles_state;
   simulate_step(particles_state, *step_description);
   world_state.current_step_is_over();
 
@@ -95,28 +89,6 @@ void BParticles_simulate_modifier(BParticlesModifierData *bpmd,
   });
 }
 
-uint BParticles_state_particle_count(BParticlesState state_c)
-{
-  ParticlesState &state = *unwrap(state_c);
-
-  uint count = 0;
-  state.particle_containers().foreach_value(
-      [&count](ParticlesContainer *container) { count += container->count_active(); });
-  return count;
-}
-
-void BParticles_state_get_positions(BParticlesState state_c, float (*dst_c)[3])
-{
-  SCOPED_TIMER(__func__);
-  ParticlesState &state = *unwrap(state_c);
-
-  uint index = 0;
-  state.particle_containers().foreach_value([dst_c, &index](ParticlesContainer *container) {
-    container->flatten_attribute_data("Position", dst_c + index);
-    index += container->count_active();
-  });
-}
-
 static float3 tetrahedon_vertices[4] = {
     {1, -1, -1},
     {1, 1, 1},
@@ -227,16 +199,17 @@ void BParticles_modifier_free_cache(BParticlesModifierData *bpmd)
   bpmd->num_cached_frames = 0;
 }
 
-Mesh *BParticles_modifier_point_mesh_from_state(BParticlesState state_c)
+Mesh *BParticles_modifier_point_mesh_from_state(BParticlesSimulationState state_c)
 {
   SCOPED_TIMER(__func__);
 
-  ParticlesState &state = *unwrap(state_c);
+  SimulationState &state = *unwrap(state_c);
 
   Vector<float3> positions;
-  state.particle_containers().foreach_value([&positions](ParticlesContainer *container) {
-    positions.extend(container->flatten_attribute<float3>("Position"));
-  });
+  state.particles_state->particle_containers().foreach_value(
+      [&positions](ParticlesContainer *container) {
+        positions.extend(container->flatten_attribute<float3>("Position"));
+      });
 
   Mesh *mesh = BKE_mesh_new_nomain(positions.size(), 0, 0, 0, 0);
 
@@ -247,17 +220,17 @@ Mesh *BParticles_modifier_point_mesh_from_state(BParticlesState state_c)
   return mesh;
 }
 
-Mesh *BParticles_modifier_mesh_from_state(BParticlesState state_c)
+Mesh *BParticles_modifier_mesh_from_state(BParticlesSimulationState state_c)
 {
   SCOPED_TIMER(__func__);
 
-  ParticlesState &state = *unwrap(state_c);
+  SimulationState &state = *unwrap(state_c);
 
   Vector<float3> positions;
   Vector<float> sizes;
   Vector<rgba_f> colors;
 
-  state.particle_containers()

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list