[Bf-blender-cvs] [115cf5ef988] master: Cleanup: Move cloth.c to C++

Hans Goudey noreply at git.blender.org
Sat Nov 12 19:14:18 CET 2022


Commit: 115cf5ef9885fb1c83d6a90203b9daef228f24e6
Author: Hans Goudey
Date:   Sat Nov 12 11:48:09 2022 -0600
Branches: master
https://developer.blender.org/rB115cf5ef9885fb1c83d6a90203b9daef228f24e6

Cleanup: Move cloth.c to C++

To support further mesh data structure refactoring.

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

M	source/blender/blenkernel/BKE_cloth.h
M	source/blender/blenkernel/CMakeLists.txt
R092	source/blender/blenkernel/intern/cloth.c	source/blender/blenkernel/intern/cloth.cc

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

diff --git a/source/blender/blenkernel/BKE_cloth.h b/source/blender/blenkernel/BKE_cloth.h
index 2ef81b754cc..8185e1883a9 100644
--- a/source/blender/blenkernel/BKE_cloth.h
+++ b/source/blender/blenkernel/BKE_cloth.h
@@ -216,7 +216,7 @@ int cloth_bvh_collision(struct Depsgraph *depsgraph,
                         float dt);
 
 /* -------------------------------------------------------------------- */
-/* cloth.c */
+/* cloth.cc */
 
 /* Needed for modifier.cc */
 /** Frees all. */
diff --git a/source/blender/blenkernel/CMakeLists.txt b/source/blender/blenkernel/CMakeLists.txt
index bb34ba3f135..d85c7791df6 100644
--- a/source/blender/blenkernel/CMakeLists.txt
+++ b/source/blender/blenkernel/CMakeLists.txt
@@ -94,7 +94,7 @@ set(SRC
   intern/callbacks.c
   intern/camera.c
   intern/cdderivedmesh.c
-  intern/cloth.c
+  intern/cloth.cc
   intern/collection.c
   intern/collision.c
   intern/colorband.c
diff --git a/source/blender/blenkernel/intern/cloth.c b/source/blender/blenkernel/intern/cloth.cc
similarity index 92%
rename from source/blender/blenkernel/intern/cloth.c
rename to source/blender/blenkernel/intern/cloth.cc
index 88ba50fe901..73b3d6fcba6 100644
--- a/source/blender/blenkernel/intern/cloth.c
+++ b/source/blender/blenkernel/intern/cloth.cc
@@ -49,11 +49,11 @@ static void cloth_update_spring_lengths(ClothModifierData *clmd, Mesh *mesh);
 static bool cloth_build_springs(ClothModifierData *clmd, Mesh *mesh);
 static void cloth_apply_vgroup(ClothModifierData *clmd, Mesh *mesh);
 
-typedef struct BendSpringRef {
+struct BendSpringRef {
   int index;
   int polys;
   ClothSpring *spring;
-} BendSpringRef;
+};
 
 /******************************************************************************
  *
@@ -64,13 +64,13 @@ typedef struct BendSpringRef {
 static BVHTree *bvhtree_build_from_cloth(ClothModifierData *clmd, float epsilon)
 {
   if (!clmd) {
-    return NULL;
+    return nullptr;
   }
 
   Cloth *cloth = clmd->clothObject;
 
   if (!cloth) {
-    return NULL;
+    return nullptr;
   }
 
   ClothVertex *verts = cloth->verts;
@@ -78,14 +78,14 @@ static BVHTree *bvhtree_build_from_cloth(ClothModifierData *clmd, float epsilon)
 
   /* in the moment, return zero if no faces there */
   if (!cloth->primitive_num) {
-    return NULL;
+    return nullptr;
   }
 
   /* Create quad-tree with k=26. */
   BVHTree *bvhtree = BLI_bvhtree_new(cloth->primitive_num, epsilon, 4, 26);
 
   /* fill tree */
-  if (clmd->hairdata == NULL) {
+  if (clmd->hairdata == nullptr) {
     for (int i = 0; i < cloth->primitive_num; i++, vt++) {
       float co[3][3];
 
@@ -123,7 +123,7 @@ void bvhtree_update_from_cloth(ClothModifierData *clmd, bool moving, bool self)
   ClothVertex *verts = cloth->verts;
   const MVertTri *vt;
 
-  BLI_assert(!(clmd->hairdata != NULL && self));
+  BLI_assert(!(clmd->hairdata != nullptr && self));
 
   if (self) {
     bvhtree = cloth->bvhselftree;
@@ -139,7 +139,7 @@ void bvhtree_update_from_cloth(ClothModifierData *clmd, bool moving, bool self)
   vt = cloth->tri;
 
   /* update vertex position in bvh tree */
-  if (clmd->hairdata == NULL) {
+  if (clmd->hairdata == nullptr) {
     if (verts && vt) {
       for (i = 0; i < cloth->primitive_num; i++, vt++) {
         float co[3][3], co_moving[3][3];
@@ -163,7 +163,7 @@ void bvhtree_update_from_cloth(ClothModifierData *clmd, bool moving, bool self)
           copy_v3_v3(co[1], verts[vt->tri[1]].tx);
           copy_v3_v3(co[2], verts[vt->tri[2]].tx);
 
-          ret = BLI_bvhtree_update_node(bvhtree, i, co[0], NULL, 3);
+          ret = BLI_bvhtree_update_node(bvhtree, i, co[0], nullptr, 3);
         }
 
         /* check if tree is already full */
@@ -185,7 +185,7 @@ void bvhtree_update_from_cloth(ClothModifierData *clmd, bool moving, bool self)
         copy_v3_v3(co[0], verts[edges[i].v1].tx);
         copy_v3_v3(co[1], verts[edges[i].v2].tx);
 
-        if (!BLI_bvhtree_update_node(bvhtree, i, co[0], NULL, 2)) {
+        if (!BLI_bvhtree_update_node(bvhtree, i, co[0], nullptr, 2)) {
           break;
         }
       }
@@ -216,14 +216,14 @@ static bool do_init_cloth(Object *ob, ClothModifierData *clmd, Mesh *result, int
   cache = clmd->point_cache;
 
   /* initialize simulation data if it didn't exist already */
-  if (clmd->clothObject == NULL) {
+  if (clmd->clothObject == nullptr) {
     if (!cloth_from_object(ob, clmd, result, framenr, 1)) {
       BKE_ptcache_invalidate(cache);
       BKE_modifier_set_error(ob, &(clmd->modifier), "Can't initialize cloth");
       return false;
     }
 
-    if (clmd->clothObject == NULL) {
+    if (clmd->clothObject == nullptr) {
       BKE_ptcache_invalidate(cache);
       BKE_modifier_set_error(ob, &(clmd->modifier), "Null cloth object");
       return false;
@@ -248,9 +248,9 @@ static int do_step_cloth(
     Depsgraph *depsgraph, Object *ob, ClothModifierData *clmd, Mesh *result, int framenr)
 {
   /* simulate 1 frame forward */
-  ClothVertex *verts = NULL;
+  ClothVertex *verts = nullptr;
   Cloth *cloth;
-  ListBase *effectors = NULL;
+  ListBase *effectors = nullptr;
   MVert *mvert;
   uint i = 0;
   int ret = 0;
@@ -277,7 +277,8 @@ static int do_step_cloth(
     }
   }
 
-  effectors = BKE_effectors_create(depsgraph, ob, NULL, clmd->sim_parms->effector_weights, false);
+  effectors = BKE_effectors_create(
+      depsgraph, ob, nullptr, clmd->sim_parms->effector_weights, false);
 
   if (clmd->sim_parms->flags & CLOTH_SIMSETTINGS_FLAG_DYNAMIC_BASEMESH) {
     cloth_update_verts(ob, clmd, result);
@@ -368,7 +369,7 @@ void clothModifier_do(ClothModifierData *clmd,
   bool can_simulate = (framenr == clmd->clothObject->last_frame + 1) &&
                       !(cache->flag & PTCACHE_BAKED);
 
-  cache_result = BKE_ptcache_read(&pid, (float)framenr + scene->r.subframe, can_simulate);
+  cache_result = BKE_ptcache_read(&pid, float(framenr) + scene->r.subframe, can_simulate);
 
   if (cache_result == PTCACHE_READ_EXACT || cache_result == PTCACHE_READ_INTERPOLATED ||
       (!can_simulate && cache_result == PTCACHE_READ_OLD)) {
@@ -420,7 +421,7 @@ void clothModifier_do(ClothModifierData *clmd,
 
 void cloth_free_modifier(ClothModifierData *clmd)
 {
-  Cloth *cloth = NULL;
+  Cloth *cloth = nullptr;
 
   if (!clmd) {
     return;
@@ -436,10 +437,10 @@ void cloth_free_modifier(ClothModifierData *clmd)
     cloth->mvert_num = 0;
 
     /* Free the springs. */
-    if (cloth->springs != NULL) {
+    if (cloth->springs != nullptr) {
       LinkNode *search = cloth->springs;
       while (search) {
-        ClothSpring *spring = search->link;
+        ClothSpring *spring = static_cast<ClothSpring *>(search->link);
 
         MEM_SAFE_FREE(spring->pa);
         MEM_SAFE_FREE(spring->pb);
@@ -447,12 +448,12 @@ void cloth_free_modifier(ClothModifierData *clmd)
         MEM_freeN(spring);
         search = search->next;
       }
-      BLI_linklist_free(cloth->springs, NULL);
+      BLI_linklist_free(cloth->springs, nullptr);
 
-      cloth->springs = NULL;
+      cloth->springs = nullptr;
     }
 
-    cloth->springs = NULL;
+    cloth->springs = nullptr;
     cloth->numsprings = 0;
 
     /* free BVH collision tree */
@@ -475,7 +476,7 @@ void cloth_free_modifier(ClothModifierData *clmd)
 
     if (cloth->sew_edge_graph) {
       BLI_edgeset_free(cloth->sew_edge_graph);
-      cloth->sew_edge_graph = NULL;
+      cloth->sew_edge_graph = nullptr;
     }
 
 #if 0
@@ -484,13 +485,13 @@ void cloth_free_modifier(ClothModifierData *clmd)
     }
 #endif
     MEM_freeN(cloth);
-    clmd->clothObject = NULL;
+    clmd->clothObject = nullptr;
   }
 }
 
 void cloth_free_modifier_extern(ClothModifierData *clmd)
 {
-  Cloth *cloth = NULL;
+  Cloth *cloth = nullptr;
   if (G.debug & G_DEBUG_SIMDATA) {
     printf("cloth_free_modifier_extern\n");
   }
@@ -513,10 +514,10 @@ void cloth_free_modifier_extern(ClothModifierData *clmd)
     cloth->mvert_num = 0;
 
     /* Free the springs. */
-    if (cloth->springs != NULL) {
+    if (cloth->springs != nullptr) {
       LinkNode *search = cloth->springs;
       while (search) {
-        ClothSpring *spring = search->link;
+        ClothSpring *spring = static_cast<ClothSpring *>(search->link);
 
         MEM_SAFE_FREE(spring->pa);
         MEM_SAFE_FREE(spring->pb);
@@ -524,12 +525,12 @@ void cloth_free_modifier_extern(ClothModifierData *clmd)
         MEM_freeN(spring);
         search = search->next;
       }
-      BLI_linklist_free(cloth->springs, NULL);
+      BLI_linklist_free(cloth->springs, nullptr);
 
-      cloth->springs = NULL;
+      cloth->springs = nullptr;
     }
 
-    cloth->springs = NULL;
+    cloth->springs = nullptr;
     cloth->numsprings = 0;
 
     /* free BVH collision tree */
@@ -552,7 +553,7 @@ void cloth_free_modifier_extern(ClothModifierData *clmd)
 
     if (cloth->sew_edge_graph) {
       BLI_edgeset_free(cloth->sew_edge_graph);
-      cloth->sew_edge_graph = NULL;
+      cloth->sew_edge_graph = nullptr;
     }
 
 #if 0
@@ -561,7 +562,7 @@ void cloth_free_modifier_extern(ClothModifierData *clmd)
     }
 #endif
     MEM_freeN(cloth);
-    clmd->clothObject = NULL;
+    clmd->clothObject = nullptr;
   }
 }
 
@@ -614,6 +615,8 @@ static void cloth_apply_vgroup(ClothModifierData *clmd, Mesh *mesh)
 
   ClothVertex *verts = clmd->clothObject->verts;
 
+  const blender::Span<MDeformVert> dverts = mesh->deform_verts();
+
   if (cloth_uses_vgroup(clmd)) {
     for (int i = 0; i < mvert_num; i++, verts++) {
 
@@ -632,8 +635,8 @@ static void cloth_apply_vgroup(ClothModifierData *clmd, Mesh *mesh)
       verts->flags &= ~(CLOTH_VERT_FLAG_PINNED | CLOTH_VERT_FLAG_NOSELFCOLL |
                         CLOTH_VERT_FLAG_NOOBJCOLL);
 
-      const MDeformVert *dvert = CustomData_get(&mesh->vdata, i, CD_MDEFORMVERT);
-      if (dvert) {
+      if (!dverts.is_empty()) {
+        const MDeformVert *dvert = &dverts[i];
         for (int j = 0; j < dvert->totweight; j++) {
           if (dvert->dw[j].def_nr == (clmd->sim_parms->vgroup_mass - 1)) {
             verts->goal = dvert->dw[j].weight;
@@ -713,12 +716,12 @@ static bool cloth_from_object(
     Object *ob, ClothModifierData *clmd, Mesh *mesh, float UNUSED(framenr), int first)
 {
   int i = 0;
-  ClothVertex *verts = NULL;
-  const float(*shapekey_rest)[3] 

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list