[Bf-blender-cvs] [f560116552d] temp-angavrilov: Cloth: share self and object collision BVH when possible.

Alexander Gavrilov noreply at git.blender.org
Thu Dec 29 16:09:29 CET 2022


Commit: f560116552d0737f046d65710e19f840ca584640
Author: Alexander Gavrilov
Date:   Wed Dec 28 23:39:36 2022 +0200
Branches: temp-angavrilov
https://developer.blender.org/rBf560116552d0737f046d65710e19f840ca584640

Cloth: share self and object collision BVH when possible.

The only difference between the two BVH structures is the
epsilon value, so if the actual values happen to be the same,
they can be shared.

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

M	source/blender/blenkernel/BKE_cloth.h
M	source/blender/blenkernel/intern/cloth.cc
M	source/blender/blenkernel/intern/collision.c

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

diff --git a/source/blender/blenkernel/BKE_cloth.h b/source/blender/blenkernel/BKE_cloth.h
index 8185e1883a9..d26a96bea21 100644
--- a/source/blender/blenkernel/BKE_cloth.h
+++ b/source/blender/blenkernel/BKE_cloth.h
@@ -72,7 +72,7 @@ typedef struct Cloth {
   unsigned char pad2;
   short pad3;
   struct BVHTree *bvhtree;     /* collision tree for this cloth object */
-  struct BVHTree *bvhselftree; /* collision tree for this cloth object */
+  struct BVHTree *bvhselftree; /* collision tree for this cloth object (may be same as bvhtree) */
   struct MVertTri *tri;
   struct Implicit_Data *implicit; /* our implicit solver connects to this pointer */
   struct EdgeSet *edgeset;        /* used for selfcollisions */
diff --git a/source/blender/blenkernel/intern/cloth.cc b/source/blender/blenkernel/intern/cloth.cc
index 23bbaaf58b2..905492a6e12 100644
--- a/source/blender/blenkernel/intern/cloth.cc
+++ b/source/blender/blenkernel/intern/cloth.cc
@@ -461,7 +461,7 @@ void cloth_free_modifier(ClothModifierData *clmd)
       BLI_bvhtree_free(cloth->bvhtree);
     }
 
-    if (cloth->bvhselftree) {
+    if (cloth->bvhselftree && cloth->bvhselftree != cloth->bvhtree) {
       BLI_bvhtree_free(cloth->bvhselftree);
     }
 
@@ -538,7 +538,7 @@ void cloth_free_modifier_extern(ClothModifierData *clmd)
       BLI_bvhtree_free(cloth->bvhtree);
     }
 
-    if (cloth->bvhselftree) {
+    if (cloth->bvhselftree && cloth->bvhselftree != cloth->bvhtree) {
       BLI_bvhtree_free(cloth->bvhselftree);
     }
 
@@ -820,7 +820,14 @@ static bool cloth_from_object(
   }
 
   clmd->clothObject->bvhtree = bvhtree_build_from_cloth(clmd, clmd->coll_parms->epsilon);
-  clmd->clothObject->bvhselftree = bvhtree_build_from_cloth(clmd, clmd->coll_parms->selfepsilon);
+
+  if (compare_ff(clmd->coll_parms->selfepsilon, clmd->coll_parms->epsilon, 1e-6f)) {
+    /* Share the BVH tree if the epsilon is the same. */
+    clmd->clothObject->bvhselftree = clmd->clothObject->bvhtree;
+  }
+  else {
+    clmd->clothObject->bvhselftree = bvhtree_build_from_cloth(clmd, clmd->coll_parms->selfepsilon);
+  }
 
   return true;
 }
diff --git a/source/blender/blenkernel/intern/collision.c b/source/blender/blenkernel/intern/collision.c
index cd34ae29efb..194979ac6de 100644
--- a/source/blender/blenkernel/intern/collision.c
+++ b/source/blender/blenkernel/intern/collision.c
@@ -1552,6 +1552,7 @@ int cloth_bvh_collision(
   BVHTreeOverlap **overlap_obj = NULL;
   uint coll_count_self = 0;
   BVHTreeOverlap *overlap_self = NULL;
+  bool bvh_updated = false;
 
   if ((clmd->sim_parms->flags & CLOTH_SIMSETTINGS_FLAG_COLLOBJ) || cloth_bvh == NULL) {
     return 0;
@@ -1562,6 +1563,7 @@ int cloth_bvh_collision(
 
   if (clmd->coll_parms->flags & CLOTH_COLLSETTINGS_FLAG_ENABLED) {
     bvhtree_update_from_cloth(clmd, false, false);
+    bvh_updated = true;
 
     /* Enable self collision if this is a hair sim */
     const bool is_hair = (clmd->hairdata != NULL);
@@ -1598,7 +1600,9 @@ int cloth_bvh_collision(
   }
 
   if (clmd->coll_parms->flags & CLOTH_COLLSETTINGS_FLAG_SELF) {
-    bvhtree_update_from_cloth(clmd, false, true);
+    if (cloth->bvhselftree != cloth->bvhtree || !bvh_updated) {
+      bvhtree_update_from_cloth(clmd, false, true);
+    }
 
     overlap_self = BLI_bvhtree_overlap_self(
         cloth->bvhselftree, &coll_count_self, cloth_bvh_self_overlap_cb, clmd);



More information about the Bf-blender-cvs mailing list