[Bf-blender-cvs] [adccee62d30] soc-2019-adaptive-cloth: Cloth: Store previous mesh for remeshing step

ishbosamiya noreply at git.blender.org
Tue Jun 4 12:41:39 CEST 2019


Commit: adccee62d30b8faf9e6d0ce2375a55e0e1df2ed1
Author: ishbosamiya
Date:   Tue Jun 4 16:07:41 2019 +0530
Branches: soc-2019-adaptive-cloth
https://developer.blender.org/rBadccee62d30b8faf9e6d0ce2375a55e0e1df2ed1

Cloth: Store previous mesh for remeshing step

Final puzzle to connect the remeshing step with the modifier
Currently a hack, but seems like the best possible method for now, will need to interfere with code that calls the modifier stack otherwise. This method uses more memory.
Now can finally proceed with the actual implementation of the paper

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

M	source/blender/blenkernel/BKE_cloth.h
M	source/blender/blenkernel/intern/cloth.c
M	source/blender/makesdna/DNA_cloth_types.h

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

diff --git a/source/blender/blenkernel/BKE_cloth.h b/source/blender/blenkernel/BKE_cloth.h
index ed6aca25571..8280563cea5 100644
--- a/source/blender/blenkernel/BKE_cloth.h
+++ b/source/blender/blenkernel/BKE_cloth.h
@@ -84,7 +84,8 @@ typedef struct Cloth {
   unsigned int mvert_num;    /* The number of verts == m * n. */
   unsigned int tri_num;
   unsigned char old_solver_type; /* unused, only 1 solver here */
-  struct BMesh *bm; /* Used for remeshing step, initialized and deleted then and there */
+  struct BMesh *bm;      /* Used for remeshing step, initialized and deleted then and there */
+  struct BMesh *bm_prev; /* Used for remeshing step, needed previous frame mesh data */
   unsigned char pad2;
   short pad3;
   struct BVHTree *bvhtree;     /* collision tree for this cloth object */
diff --git a/source/blender/blenkernel/intern/cloth.c b/source/blender/blenkernel/intern/cloth.c
index e954047514b..96a2010d6b8 100644
--- a/source/blender/blenkernel/intern/cloth.c
+++ b/source/blender/blenkernel/intern/cloth.c
@@ -122,7 +122,8 @@ void cloth_init(ClothModifierData *clmd)
   clmd->sim_parms->presets = 2;       /* cotton as start setting */
   clmd->sim_parms->timescale = 1.0f;  /* speed factor, describes how fast cloth moves */
   clmd->sim_parms->time_scale = 1.0f; /* multiplies cloth speed */
-  clmd->sim_parms->reset = 0;
+  clmd->sim_parms->reset = 1;
+  clmd->sim_parms->remeshing_reset = 1;
 
   clmd->coll_parms->self_friction = 5.0;
   clmd->coll_parms->friction = 5.0;
@@ -392,24 +393,38 @@ static CustomData_MeshMasks cloth_remeshing_get_cd_mesh_masks(void)
 
 static void cloth_remeshing_init_bmesh(Object *ob, ClothModifierData *clmd, Mesh *mesh)
 {
-  cloth_to_mesh(ob, clmd, mesh);
+  if (clmd->sim_parms->remeshing_reset || !clmd->clothObject->bm_prev) {
+    cloth_to_mesh(ob, clmd, mesh);
 
-  CustomData_MeshMasks cddata_masks = cloth_remeshing_get_cd_mesh_masks();
-  clmd->clothObject->bm = BKE_mesh_to_bmesh_ex(mesh,
-                                               &((struct BMeshCreateParams){0}),
-                                               &((struct BMeshFromMeshParams){
-                                                   .calc_face_normal = true,
-                                                   .cd_mask_extra = cddata_masks,
-                                               }));
-
-  BM_mesh_triangulate(clmd->clothObject->bm,
-                      MOD_TRIANGULATE_QUAD_SHORTEDGE,
-                      MOD_TRIANGULATE_NGON_BEAUTY,
-                      4,
-                      false,
-                      NULL,
-                      NULL,
-                      NULL);
+    CustomData_MeshMasks cddata_masks = cloth_remeshing_get_cd_mesh_masks();
+    if (clmd->clothObject->bm_prev) {
+      BM_mesh_free(clmd->clothObject->bm_prev);
+      clmd->clothObject->bm_prev = NULL;
+    }
+    clmd->clothObject->bm_prev = BKE_mesh_to_bmesh_ex(mesh,
+                                                      &((struct BMeshCreateParams){0}),
+                                                      &((struct BMeshFromMeshParams){
+                                                          .calc_face_normal = true,
+                                                          .cd_mask_extra = cddata_masks,
+                                                      }));
+
+    BM_mesh_triangulate(clmd->clothObject->bm_prev,
+                        MOD_TRIANGULATE_QUAD_SHORTEDGE,
+                        MOD_TRIANGULATE_NGON_BEAUTY,
+                        4,
+                        false,
+                        NULL,
+                        NULL,
+                        NULL);
+  }
+  clmd->clothObject->bm = clmd->clothObject->bm_prev;
+
+  /* BMesh *bm = clmd->clothObject->bm; */
+  /* BM_mesh_elem_table_init(bm, BM_FACE); */
+  /* if (bm->totface > 1) { */
+  /*   BMFace *face = BM_face_at_index(bm, 0); */
+  /*   BM_face_kill_loose(bm, face); */
+  /* } */
 }
 
 static Mesh *cloth_remeshing_update_cloth_object(Object *ob, ClothModifierData *clmd)
@@ -417,7 +432,9 @@ static Mesh *cloth_remeshing_update_cloth_object(Object *ob, ClothModifierData *
   Mesh *mesh_result = NULL;
   CustomData_MeshMasks cddata_masks = cloth_remeshing_get_cd_mesh_masks();
   mesh_result = BKE_mesh_from_bmesh_for_eval_nomain(clmd->clothObject->bm, &cddata_masks);
+  clmd->clothObject->bm_prev = BM_mesh_copy(clmd->clothObject->bm);
   BM_mesh_free(clmd->clothObject->bm);
+  clmd->clothObject->bm = NULL;
 
   do_init_cloth(ob, clmd, mesh_result, 0);
   /* cloth_from_object(ob, clmd, mesh_result, 0, 0); */
@@ -428,7 +445,6 @@ Mesh *cloth_remeshing_step(Object *ob, ClothModifierData *clmd, Mesh *mesh)
 {
   cloth_remeshing_init_bmesh(ob, clmd, mesh);
 
-  BKE_mesh_free(mesh);
   return cloth_remeshing_update_cloth_object(ob, clmd);
 }
 
@@ -463,6 +479,7 @@ Mesh *clothModifier_do(
   if (clmd->sim_parms->reset ||
       (clmd->clothObject && mesh_result->totvert != clmd->clothObject->mvert_num)) {
     clmd->sim_parms->reset = 0;
+    clmd->sim_parms->remeshing_reset = 1;
 #if USE_CLOTH_CACHE
     cache->flag |= PTCACHE_OUTDATED;
     BKE_ptcache_id_reset(scene, &pid, PTCACHE_RESET_OUTDATED);
@@ -471,6 +488,9 @@ Mesh *clothModifier_do(
     cache->flag &= ~PTCACHE_REDO_NEEDED;
 #endif
   }
+  else {
+    clmd->sim_parms->remeshing_reset = 0;
+  }
 
   /* simulation is only active during a specific period */
   if (framenr < startframe) {
@@ -595,6 +615,15 @@ void cloth_free_modifier(ClothModifierData *clmd)
   if (cloth) {
     BPH_cloth_solver_free(clmd);
 
+    if (cloth->bm) {
+      BM_mesh_free(cloth->bm);
+      cloth->bm = NULL;
+    }
+    if (cloth->bm_prev) {
+      BM_mesh_free(cloth->bm_prev);
+      cloth->bm_prev = NULL;
+    }
+
     // Free the verts.
     if (cloth->verts != NULL) {
       MEM_freeN(cloth->verts);
@@ -670,6 +699,15 @@ void cloth_free_modifier_extern(ClothModifierData *clmd)
       printf("cloth_free_modifier_extern in\n");
     }
 
+    if (cloth->bm) {
+      BM_mesh_free(cloth->bm);
+      cloth->bm = NULL;
+    }
+    if (cloth->bm_prev) {
+      BM_mesh_free(cloth->bm_prev);
+      cloth->bm_prev = NULL;
+    }
+
     BPH_cloth_solver_free(clmd);
 
     // Free the verts.
diff --git a/source/blender/makesdna/DNA_cloth_types.h b/source/blender/makesdna/DNA_cloth_types.h
index 9cedd8f0ebf..f9158409cfa 100644
--- a/source/blender/makesdna/DNA_cloth_types.h
+++ b/source/blender/makesdna/DNA_cloth_types.h
@@ -130,8 +130,9 @@ typedef struct ClothSimSettings {
   /** Used for presets on GUI. */
   short presets;
   short reset;
+  short remeshing_reset;
 
-  char _pad0[4];
+  char _pad0[2];
   struct EffectorWeights *effector_weights;
 
   short bending_model;



More information about the Bf-blender-cvs mailing list