[Bf-blender-cvs] [b832be6206e] functions: improved particle to tetrahedon conversion

Jacques Lucke noreply at git.blender.org
Sat Jul 6 17:30:28 CEST 2019


Commit: b832be6206ebd9247257aa36e6d808e1e2bdceaf
Author: Jacques Lucke
Date:   Sat Jul 6 16:38:26 2019 +0200
Branches: functions
https://developer.blender.org/rBb832be6206ebd9247257aa36e6d808e1e2bdceaf

improved particle to tetrahedon conversion

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

M	source/blender/modifiers/intern/MOD_nodeparticles.c
M	source/blender/simulations/bparticles/c_wrapper.cpp

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

diff --git a/source/blender/modifiers/intern/MOD_nodeparticles.c b/source/blender/modifiers/intern/MOD_nodeparticles.c
index 428933c73f5..dc0410b4a75 100644
--- a/source/blender/modifiers/intern/MOD_nodeparticles.c
+++ b/source/blender/modifiers/intern/MOD_nodeparticles.c
@@ -96,61 +96,6 @@ static Mesh *point_mesh_from_particle_state(BParticlesState state)
   return mesh;
 }
 
-static Mesh *tetrahedon_mesh_from_particle_state(BParticlesState state, float scale)
-{
-  uint point_amount = BParticles_state_particle_count(state);
-  Mesh *mesh = BKE_mesh_new_nomain(point_amount * 4, 0, 0, point_amount * 12, point_amount * 4);
-
-  float(*positions)[3] = MEM_malloc_arrayN(point_amount, sizeof(float[3]), __func__);
-  BParticles_state_get_positions(state, positions);
-
-  MLoopCol *loop_colors = CustomData_add_layer_named(
-      &mesh->ldata, CD_MLOOPCOL, CD_DEFAULT, NULL, mesh->totloop, "test");
-
-  for (uint i = 0; i < mesh->totloop; i++) {
-    MLoopCol color = {255, 255, 0, 255};
-    memcpy(loop_colors + i, &color, sizeof(MLoopCol));
-  }
-
-  for (uint i = 0; i < point_amount; i++) {
-    float offset0[3] = {1, -1, -1};
-    float offset1[3] = {1, 1, 1};
-    float offset2[3] = {-1, -1, 1};
-    float offset3[3] = {-1, 1, -1};
-
-    madd_v3_v3v3fl(mesh->mvert[i * 4 + 0].co, positions[i], offset0, scale);
-    madd_v3_v3v3fl(mesh->mvert[i * 4 + 1].co, positions[i], offset1, scale);
-    madd_v3_v3v3fl(mesh->mvert[i * 4 + 2].co, positions[i], offset2, scale);
-    madd_v3_v3v3fl(mesh->mvert[i * 4 + 3].co, positions[i], offset3, scale);
-
-    for (uint j = 0; j < 4; j++) {
-      mesh->mpoly[i * 4 + j].loopstart = (i * 4 + j) * 3;
-      mesh->mpoly[i * 4 + j].totloop = 3;
-    }
-
-    mesh->mloop[i * 12 + 0].v = i * 4 + 0;
-    mesh->mloop[i * 12 + 1].v = i * 4 + 1;
-    mesh->mloop[i * 12 + 2].v = i * 4 + 2;
-
-    mesh->mloop[i * 12 + 3].v = i * 4 + 0;
-    mesh->mloop[i * 12 + 4].v = i * 4 + 3;
-    mesh->mloop[i * 12 + 5].v = i * 4 + 1;
-
-    mesh->mloop[i * 12 + 6].v = i * 4 + 0;
-    mesh->mloop[i * 12 + 7].v = i * 4 + 2;
-    mesh->mloop[i * 12 + 8].v = i * 4 + 3;
-
-    mesh->mloop[i * 12 + 9].v = i * 4 + 1;
-    mesh->mloop[i * 12 + 10].v = i * 4 + 2;
-    mesh->mloop[i * 12 + 11].v = i * 4 + 3;
-  }
-  BKE_mesh_calc_edges(mesh, false, false);
-
-  MEM_freeN(positions);
-
-  return mesh;
-}
-
 static Mesh *applyModifier(ModifierData *md,
                            const struct ModifierEvalContext *ctx,
                            Mesh *UNUSED(mesh))
@@ -179,7 +124,7 @@ static Mesh *applyModifier(ModifierData *md,
     runtime->last_simulated_frame = current_frame;
   }
 
-  return point_mesh_from_particle_state(runtime->state);
+  return BParticles_test_mesh_from_state(runtime->state);
 }
 
 static void initData(ModifierData *UNUSED(md))
diff --git a/source/blender/simulations/bparticles/c_wrapper.cpp b/source/blender/simulations/bparticles/c_wrapper.cpp
index 9dcbdb26f38..6e669fe8abe 100644
--- a/source/blender/simulations/bparticles/c_wrapper.cpp
+++ b/source/blender/simulations/bparticles/c_wrapper.cpp
@@ -638,92 +638,63 @@ void BParticles_state_get_positions(BParticlesState state_c, float (*dst_c)[3])
   }
 }
 
-static inline void append_tetrahedon_mesh_data(float3 position,
-                                               float scale,
-                                               MLoopCol color,
-                                               SmallVector<float3> &vertex_positions,
-                                               SmallVector<uint> &poly_starts,
-                                               SmallVector<uint> &poly_lengths,
-                                               SmallVector<uint> &loops,
-                                               SmallVector<MLoopCol> &loop_colors)
-{
-  uint vertex_offset = vertex_positions.size();
-
-  vertex_positions.append(position + scale * float3(1, -1, -1));
-  vertex_positions.append(position + scale * float3(1, 1, 1));
-  vertex_positions.append(position + scale * float3(-1, -1, 1));
-  vertex_positions.append(position + scale * float3(-1, 1, -1));
-
-  poly_lengths.append_n_times(3, 4);
-
-  poly_starts.append(loops.size());
-  loops.extend({vertex_offset + 0, vertex_offset + 1, vertex_offset + 2});
-  poly_starts.append(loops.size());
-  loops.extend({vertex_offset + 0, vertex_offset + 3, vertex_offset + 1});
-  poly_starts.append(loops.size());
-  loops.extend({vertex_offset + 0, vertex_offset + 2, vertex_offset + 3});
-  poly_starts.append(loops.size());
-  loops.extend({vertex_offset + 1, vertex_offset + 2, vertex_offset + 3});
+static float3 tetrahedon_vertices[4] = {
+    {1, -1, -1},
+    {1, 1, 1},
+    {-1, -1, 1},
+    {-1, 1, -1},
+};
 
-  loop_colors.append_n_times(color, 12);
-}
+static uint tetrahedon_loop_starts[4] = {0, 3, 6, 9};
+static uint tetrahedon_loop_lengths[4] = {3, 3, 3, 3};
+static uint tetrahedon_loop_vertices[12] = {0, 1, 2, 0, 3, 1, 0, 2, 3, 1, 2, 3};
+static uint tetrahedon_edges[6][2] = {{0, 1}, {0, 2}, {0, 3}, {1, 2}, {1, 3}, {2, 3}};
 
-Mesh *BParticles_test_mesh_from_state(BParticlesState state_c)
+static Mesh *distribute_tetrahedons(ArrayRef<float3> centers, float scale)
 {
-  ParticlesState &state = *unwrap(state_c);
-
-  SmallVector<float3> vertex_positions;
-  SmallVector<uint> poly_starts;
-  SmallVector<uint> poly_lengths;
-  SmallVector<uint> loops;
-  SmallVector<MLoopCol> loop_colors;
-
-  SmallVector<MLoopCol> colors_to_use = {
-      {230, 30, 30, 255}, {30, 230, 30, 255}, {30, 30, 230, 255}};
-
-  uint type_index = 0;
-  for (ParticlesContainer *container : state.particle_containers().values()) {
-    for (ParticlesBlock *block : container->active_blocks()) {
-      AttributeArrays attributes = block->attributes();
-      auto positions = attributes.get_float3("Position");
-
-      for (uint pindex = 0; pindex < attributes.size(); pindex++) {
-        append_tetrahedon_mesh_data(positions[pindex],
-                                    0.03f,
-                                    colors_to_use[type_index],
-                                    vertex_positions,
-                                    poly_starts,
-                                    poly_lengths,
-                                    loops,
-                                    loop_colors);
-      }
+  uint amount = centers.size();
+  Mesh *mesh = BKE_mesh_new_nomain(amount * ARRAY_SIZE(tetrahedon_vertices),
+                                   amount * ARRAY_SIZE(tetrahedon_edges),
+                                   0,
+                                   amount * ARRAY_SIZE(tetrahedon_loop_vertices),
+                                   amount * ARRAY_SIZE(tetrahedon_loop_starts));
+
+  for (uint instance = 0; instance < amount; instance++) {
+    uint vertex_offset = instance * ARRAY_SIZE(tetrahedon_vertices);
+    uint face_offset = instance * ARRAY_SIZE(tetrahedon_loop_starts);
+    uint loop_offset = instance * ARRAY_SIZE(tetrahedon_loop_vertices);
+    uint edge_offset = instance * ARRAY_SIZE(tetrahedon_edges);
+
+    float3 center = centers[instance];
+    for (uint i = 0; i < ARRAY_SIZE(tetrahedon_vertices); i++) {
+      copy_v3_v3(mesh->mvert[vertex_offset + i].co, center + tetrahedon_vertices[i] * scale);
     }
-    type_index++;
-  }
 
-  Mesh *mesh = BKE_mesh_new_nomain(
-      vertex_positions.size(), 0, 0, loops.size(), poly_starts.size());
+    for (uint i = 0; i < ARRAY_SIZE(tetrahedon_loop_starts); i++) {
+      mesh->mpoly[face_offset + i].loopstart = loop_offset + tetrahedon_loop_starts[i];
+      mesh->mpoly[face_offset + i].totloop = tetrahedon_loop_lengths[i];
+    }
 
-  for (uint i = 0; i < vertex_positions.size(); i++) {
-    copy_v3_v3(mesh->mvert[i].co, vertex_positions[i]);
-  }
+    for (uint i = 0; i < ARRAY_SIZE(tetrahedon_loop_vertices); i++) {
+      mesh->mloop[loop_offset + i].v = vertex_offset + tetrahedon_loop_vertices[i];
+    }
 
-  for (uint i = 0; i < poly_starts.size(); i++) {
-    mesh->mpoly[i].loopstart = poly_starts[i];
-    mesh->mpoly[i].totloop = poly_lengths[i];
+    for (uint i = 0; i < ARRAY_SIZE(tetrahedon_edges); i++) {
+      mesh->medge[edge_offset + i].v1 = vertex_offset + tetrahedon_edges[i][0];
+      mesh->medge[edge_offset + i].v2 = vertex_offset + tetrahedon_edges[i][1];
+    }
   }
 
-  for (uint i = 0; i < loops.size(); i++) {
-    mesh->mloop[i].v = loops[i];
-  }
+  return mesh;
+}
 
-  MLoopCol *mesh_loop_colors = (MLoopCol *)CustomData_add_layer_named(
-      &mesh->ldata, CD_MLOOPCOL, CD_DEFAULT, nullptr, mesh->totloop, "test");
+Mesh *BParticles_test_mesh_from_state(BParticlesState state_c)
+{
+  SCOPED_TIMER(__func__);
 
-  for (uint i = 0; i < loop_colors.size(); i++) {
-    mesh_loop_colors[i] = loop_colors[i];
-  }
+  uint particle_count = BParticles_state_particle_count(state_c);
+  SmallVector<float3> positions(particle_count);
+  BParticles_state_get_positions(state_c, (float(*)[3])positions.begin());
 
-  BKE_mesh_calc_edges(mesh, false, false);
-  return mesh;
+  return distribute_tetrahedons(positions, 0.025f);
 }



More information about the Bf-blender-cvs mailing list