Commit: d4be1bfb8187b2ca270982f6e59157ac0ff72647
Author: ishbosamiya
Date:   Fri Jun 11 12:19:52 2021 +0530
Branches: soc-2021-adaptive-cloth
https://developer.blender.org/rBd4be1bfb8187b2ca270982f6e59157ac0ff72647

adaptive_cloth: `clothModifier_do()` returns a new `Mesh` if simulation was 
successful

This is an important change that allows for remeshing operations in
the cloth simulator. `clothModifier_do()` returns the resulting mesh
if it was successful otherwise NULL.

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

M       source/blender/blenkernel/BKE_cloth.h
M       source/blender/blenkernel/intern/cloth.c
M       source/blender/blenkernel/intern/particle_system.c
M       source/blender/modifiers/intern/MOD_cloth.c

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

diff --git a/source/blender/blenkernel/BKE_cloth.h 
b/source/blender/blenkernel/BKE_cloth.h
index 04fcdd6ed6f..7bf47e37c69 100644
--- a/source/blender/blenkernel/BKE_cloth.h
+++ b/source/blender/blenkernel/BKE_cloth.h
@@ -241,12 +241,11 @@ int cloth_bvh_collision(struct Depsgraph *depsgraph,
 // needed for modifier.c
 void cloth_free_modifier_extern(struct ClothModifierData *clmd);
 void cloth_free_modifier(struct ClothModifierData *clmd);
-void clothModifier_do(struct ClothModifierData *clmd,
-                      struct Depsgraph *depsgraph,
-                      struct Scene *scene,
-                      struct Object *ob,
-                      struct Mesh *me,
-                      float (*vertexCos)[3]);
+struct Mesh *clothModifier_do(struct ClothModifierData *clmd,
+                              struct Depsgraph *depsgraph,
+                              struct Scene *scene,
+                              struct Object *ob,
+                              struct Mesh *mesh);
 
 int cloth_uses_vgroup(struct ClothModifierData *clmd);
 
diff --git a/source/blender/blenkernel/intern/cloth.c 
b/source/blender/blenkernel/intern/cloth.c
index 09bd397cc78..a63dbf5f467 100644
--- a/source/blender/blenkernel/intern/cloth.c
+++ b/source/blender/blenkernel/intern/cloth.c
@@ -54,7 +54,7 @@
 /* ********** cloth engine ******* */
 /* Prototypes for internal functions.
  */
-static void cloth_to_object(Object *ob, ClothModifierData *clmd, float 
(*vertexCos)[3]);
+static void cloth_to_object(Object *ob, ClothModifierData *clmd, Mesh *r_mesh);
 static void cloth_from_mesh(ClothModifierData *clmd, const Object *ob, Mesh 
*mesh);
 static bool cloth_from_object(
     Object *ob, ClothModifierData *clmd, Mesh *mesh, float framenr, int first);
@@ -318,12 +318,8 @@ static int do_step_cloth(
 /************************************************
  * clothModifier_do - main simulation function
  ************************************************/
-void clothModifier_do(ClothModifierData *clmd,
-                      Depsgraph *depsgraph,
-                      Scene *scene,
-                      Object *ob,
-                      Mesh *mesh,
-                      float (*vertexCos)[3])
+Mesh *clothModifier_do(
+    ClothModifierData *clmd, Depsgraph *depsgraph, Scene *scene, Object *ob, 
Mesh *mesh)
 {
   PointCache *cache;
   PTCacheID pid;
@@ -351,7 +347,7 @@ void clothModifier_do(ClothModifierData *clmd,
   /* simulation is only active during a specific period */
   if (framenr < startframe) {
     BKE_ptcache_invalidate(cache);
-    return;
+    return NULL;
   }
   if (framenr > endframe) {
     framenr = endframe;
@@ -359,7 +355,7 @@ void clothModifier_do(ClothModifierData *clmd,
 
   /* initialize simulation data if it didn't exist already */
   if (!do_init_cloth(ob, clmd, mesh, framenr)) {
-    return;
+    return NULL;
   }
 
   if (framenr == startframe) {
@@ -368,7 +364,7 @@ void clothModifier_do(ClothModifierData *clmd,
     BKE_ptcache_validate(cache, framenr);
     cache->flag &= ~PTCACHE_REDO_NEEDED;
     clmd->clothObject->last_frame = framenr;
-    return;
+    return NULL;
   }
 
   /* try to read from cache */
@@ -380,7 +376,10 @@ void clothModifier_do(ClothModifierData *clmd,
   if (cache_result == PTCACHE_READ_EXACT || cache_result == 
PTCACHE_READ_INTERPOLATED ||
       (!can_simulate && cache_result == PTCACHE_READ_OLD)) {
     SIM_cloth_solver_set_positions(clmd);
-    cloth_to_object(ob, clmd, vertexCos);
+
+    Mesh *mesh_result = BKE_mesh_copy_for_eval(mesh, false);
+
+    cloth_to_object(ob, clmd, mesh_result);
 
     BKE_ptcache_validate(cache, framenr);
 
@@ -390,7 +389,7 @@ void clothModifier_do(ClothModifierData *clmd,
 
     clmd->clothObject->last_frame = framenr;
 
-    return;
+    return mesh_result;
   }
   if (cache_result == PTCACHE_READ_OLD) {
     SIM_cloth_solver_set_positions(clmd);
@@ -400,7 +399,8 @@ void clothModifier_do(ClothModifierData *clmd,
       /*ob->id.lib ||*/ (cache->flag & PTCACHE_BAKED)) {
     /* if baked and nothing in cache, do nothing */
     BKE_ptcache_invalidate(cache);
-    return;
+    return NULL; /* TODO(ish): figure out if this is an early escape because 
of some error or
+                    something else */
   }
 
   /* if on second frame, write cache for first frame */
@@ -421,8 +421,12 @@ void clothModifier_do(ClothModifierData *clmd,
     BKE_ptcache_write(&pid, framenr);
   }
 
-  cloth_to_object(ob, clmd, vertexCos);
+  Mesh *mesh_result = BKE_mesh_copy_for_eval(mesh, false);
+
+  cloth_to_object(ob, clmd, mesh_result);
   clmd->clothObject->last_frame = framenr;
+
+  return mesh_result;
 }
 
 /* frees all */
@@ -589,21 +593,32 @@ void cloth_free_modifier_extern(ClothModifierData *clmd)
  
******************************************************************************/
 
 /**
- * Copies the deformed vertices to the object.
+ * Copies the deformed vertices to `r_mesh` after converting the world
+ * space coordinates stored in `cloth->verts` to local space coords
+ * required by `r_mesh`
  */
-static void cloth_to_object(Object *ob, ClothModifierData *clmd, float 
(*vertexCos)[3])
+static void cloth_to_object(Object *ob, ClothModifierData *clmd, Mesh *r_mesh)
 {
-  unsigned int i = 0;
+  /* TODO(ish): might need a better name for the function now that it
+   * directly applies the vertex * positions to the mesh */
   Cloth *cloth = clmd->clothObject;
 
   if (clmd->clothObject) {
     /* inverse matrix is not uptodate... */
     invert_m4_m4(ob->imat, ob->obmat);
 
-    for (i = 0; i < cloth->mvert_num; i++) {
-      copy_v3_v3(vertexCos[i], cloth->verts[i].x);
-      mul_m4_v3(ob->imat, vertexCos[i]); /* cloth is in global coords */
+    BLI_assert(cloth->mvert_num == r_mesh->totvert);
+
+    float(*vert_coords)[3] = MEM_mallocN(sizeof(float[3]) * cloth->mvert_num, 
__func__);
+
+    for (size_t i = 0; i < cloth->mvert_num; i++) {
+      copy_v3_v3(vert_coords[i], cloth->verts[i].x);
     }
+
+    /* need to convert from world space to local space */
+    BKE_mesh_vert_coords_apply_with_mat4(r_mesh, vert_coords, ob->imat);
+
+    MEM_freeN(vert_coords);
   }
 }
 
diff --git a/source/blender/blenkernel/intern/particle_system.c 
b/source/blender/blenkernel/intern/particle_system.c
index ce4be411c9a..98c9025da17 100644
--- a/source/blender/blenkernel/intern/particle_system.c
+++ b/source/blender/blenkernel/intern/particle_system.c
@@ -3508,8 +3508,9 @@ static void do_hair_dynamics(ParticleSimulationData *sim)
 
   BKE_id_copy_ex(NULL, &psys->hair_in_mesh->id, (ID **)&psys->hair_out_mesh, 
LIB_ID_COPY_LOCALIZE);
   deformedVerts = BKE_mesh_vert_coords_alloc(psys->hair_out_mesh, NULL);
-  clothModifier_do(
-      psys->clmd, sim->depsgraph, sim->scene, sim->ob, psys->hair_in_mesh, 
deformedVerts);
+  /* TODO(ish): need to make hair simulation work with the new cloth changes */
+  /* clothModifier_do( */
+  /*     psys->clmd, sim->depsgraph, sim->scene, sim->ob, psys->hair_in_mesh, 
deformedVerts); */
   BKE_mesh_vert_coords_apply(psys->hair_out_mesh, deformedVerts);
 
   MEM_freeN(deformedVerts);
diff --git a/source/blender/modifiers/intern/MOD_cloth.c 
b/source/blender/modifiers/intern/MOD_cloth.c
index 3ad4bd209c7..2c6a4e85b93 100644
--- a/source/blender/modifiers/intern/MOD_cloth.c
+++ b/source/blender/modifiers/intern/MOD_cloth.c
@@ -128,15 +128,13 @@ static Mesh *modifyMesh(ModifierData *md, const 
ModifierEvalContext *ctx, Mesh *
   /*   } */
   /* } */
 
-  Mesh *mesh_result = BKE_mesh_copy_for_eval(mesh, false);
-
-  float(*vert_coords)[3] = BKE_mesh_vert_coords_alloc(mesh_result, NULL);
-  BKE_mesh_vert_coords_get(mesh_result, vert_coords);
-
-  clothModifier_do(clmd, ctx->depsgraph, scene, ctx->object, mesh_result, 
vert_coords);
-
-  BKE_mesh_vert_coords_apply(mesh_result, vert_coords);
+  Mesh *mesh_result = clothModifier_do(clmd, ctx->depsgraph, scene, 
ctx->object, mesh);
 
+  if (mesh_result == NULL) {
+    /* didn't get a valid mesh so there is a chance the modifier
+     * failed, return the mesh that the modifier got */
+    return mesh;
+  }
   return mesh_result;
 }

_______________________________________________
Bf-blender-cvs mailing list
Bf-blender-cvs@blender.org
https://lists.blender.org/mailman/listinfo/bf-blender-cvs

Reply via email to