[Bf-blender-cvs] [5afe71daf54] soc-2021-adaptive-cloth: adaptive_mesh: serialize and dump the cloth mesh every frame

ishbosamiya noreply at git.blender.org
Sun Aug 22 17:23:40 CEST 2021


Commit: 5afe71daf54212c3488306c986fb73504183bfb6
Author: ishbosamiya
Date:   Thu Aug 19 16:24:56 2021 +0530
Branches: soc-2021-adaptive-cloth
https://developer.blender.org/rB5afe71daf54212c3488306c986fb73504183bfb6

adaptive_mesh: serialize and dump the cloth mesh every frame

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

M	source/blender/blenkernel/BKE_cloth_remesh.hh
M	source/blender/blenkernel/intern/cloth.c
M	source/blender/blenkernel/intern/cloth_remesh.cc

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

diff --git a/source/blender/blenkernel/BKE_cloth_remesh.hh b/source/blender/blenkernel/BKE_cloth_remesh.hh
index cba6bd108a4..f9d977660db 100644
--- a/source/blender/blenkernel/BKE_cloth_remesh.hh
+++ b/source/blender/blenkernel/BKE_cloth_remesh.hh
@@ -63,6 +63,11 @@ extern "C" {
 
 Mesh *BKE_cloth_remesh(struct Object *ob, struct ClothModifierData *clmd, struct Mesh *mesh);
 
+void BKE_cloth_serialize_adaptive_mesh(struct Object *ob,
+                                       struct ClothModifierData *clmd,
+                                       struct Mesh *mesh,
+                                       const char *location);
+
 #ifdef __cplusplus
 }
 } /* namespace blender::bke */
diff --git a/source/blender/blenkernel/intern/cloth.c b/source/blender/blenkernel/intern/cloth.c
index d8dfc88aece..1e99921b911 100644
--- a/source/blender/blenkernel/intern/cloth.c
+++ b/source/blender/blenkernel/intern/cloth.c
@@ -33,6 +33,7 @@
 #include "BLI_linklist.h"
 #include "BLI_math.h"
 #include "BLI_rand.h"
+#include "BLI_string.h"
 #include "BLI_utildefines.h"
 
 #include "DEG_depsgraph.h"
@@ -328,6 +329,12 @@ static Mesh *do_step_cloth(
 
   // printf ( "%f\n", ( float ) tval() );
 
+  {
+    char location[48];
+    BLI_snprintf(location, 48, "/tmp/cloth_sim/framenr_%05d.mesh", framenr);
+    BKE_cloth_serialize_adaptive_mesh(ob, clmd, mesh, location);
+  }
+
   if (remesh) {
     /* In case remeshing is enabled, the remeshing function will
      * return the final mesh needed */
diff --git a/source/blender/blenkernel/intern/cloth_remesh.cc b/source/blender/blenkernel/intern/cloth_remesh.cc
index 4b64ad4fc0e..e76b51629e3 100644
--- a/source/blender/blenkernel/intern/cloth_remesh.cc
+++ b/source/blender/blenkernel/intern/cloth_remesh.cc
@@ -1374,6 +1374,66 @@ static void set_cloth_information_when_new_mesh(Object *ob, ClothModifierData *c
   clmd->clothObject->bvhselftree = bvhtree_build_from_cloth(clmd, clmd->coll_parms->selfepsilon);
 }
 
+void BKE_cloth_serialize_adaptive_mesh(Object *ob,
+                                       ClothModifierData *clmd,
+                                       Mesh *mesh,
+                                       const char *location)
+{
+  AdaptiveRemeshParams<internal::ClothNodeData, Cloth> params;
+  params.size_min = clmd->sim_parms->remeshing_size_min;
+  params.extra_data_to_end = [](const Cloth &cloth, size_t index) {
+    BLI_assert(index < cloth.mvert_num);
+    BLI_assert(cloth.verts);
+    return internal::ClothNodeData(cloth.verts[index]);
+  };
+  params.post_extra_data_to_end = [](Cloth & /*unused*/) {
+    /* Do nothing */
+  };
+
+  params.end_to_extra_data =
+      [](Cloth & /*unused*/, internal::ClothNodeData /*unused*/, size_t /*unused*/) {
+        /* Do nothing */
+      };
+  params.pre_end_to_extra_data = [](Cloth &cloth, size_t num_nodes) {
+    /* Do not allocate cloth.verts, it shouldn't have been modified */
+    BLI_assert(cloth.verts != nullptr);
+    BLI_assert(cloth.mvert_num == num_nodes);
+  };
+
+  const auto remeshing = clmd->sim_parms->flags & CLOTH_SIMSETTINGS_FLAG_REMESH;
+  Mesh *cloth_to_object_res = nullptr;
+  if (remeshing && clmd->prev_frame_mesh) {
+    cloth_to_object_res = cloth_to_object(ob, clmd, clmd->prev_frame_mesh, true);
+  }
+  else {
+    cloth_to_object_res = cloth_to_object(ob, clmd, mesh, true);
+  }
+
+  internal::MeshIO meshio_input;
+  meshio_input.read(cloth_to_object_res);
+
+  internal::AdaptiveMesh<internal::ClothNodeData> adaptive_mesh;
+  adaptive_mesh.read(meshio_input);
+
+  Cloth &extra_data = *clmd->clothObject;
+
+  /* Load up the `NodeData`'s extra_data */
+  {
+    auto i = 0;
+    for (auto &node : adaptive_mesh.get_nodes_mut()) {
+      node.set_extra_data(internal::NodeData(params.extra_data_to_end(extra_data, i)));
+      i++;
+    }
+
+    params.post_extra_data_to_end(extra_data);
+  }
+
+  const auto serialized = adaptive_mesh.serialize();
+  internal::dump_file(location, serialized);
+
+  BKE_mesh_eval_delete(cloth_to_object_res);
+}
+
 Mesh *BKE_cloth_remesh(Object *ob, ClothModifierData *clmd, Mesh *mesh)
 {
   if (clmd->prev_frame_mesh) {



More information about the Bf-blender-cvs mailing list