[Bf-blender-cvs] [f112ca46a98] soc-2019-adaptive-cloth: Cloth: ported remeshing code to cpp

ishbosamiya noreply at git.blender.org
Fri Jun 28 19:52:40 CEST 2019


Commit: f112ca46a984fb66003c4ac2e079e13820001fc1
Author: ishbosamiya
Date:   Thu Jun 27 16:43:58 2019 +0530
Branches: soc-2019-adaptive-cloth
https://developer.blender.org/rBf112ca46a984fb66003c4ac2e079e13820001fc1

Cloth: ported remeshing code to cpp

This has been done to simplify the code (especially with the use of vector<> over BLI_array and BLI_linklist). This will allow for faster iteration of the code as well.

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

M	source/blender/blenkernel/BKE_cloth.h
A	source/blender/blenkernel/BKE_cloth_remeshing.h
M	source/blender/blenkernel/CMakeLists.txt
M	source/blender/blenkernel/intern/cloth.c
A	source/blender/blenkernel/intern/cloth_remeshing.cpp

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

diff --git a/source/blender/blenkernel/BKE_cloth.h b/source/blender/blenkernel/BKE_cloth.h
index 58c984bcc56..4d5df6591e5 100644
--- a/source/blender/blenkernel/BKE_cloth.h
+++ b/source/blender/blenkernel/BKE_cloth.h
@@ -33,6 +33,7 @@ struct MFace;
 struct Mesh;
 struct Object;
 struct Scene;
+struct BVHTree;
 
 #define DO_INLINE MALWAYS_INLINE
 
@@ -308,4 +309,9 @@ void cloth_parallel_transport_hair_frame(float mat[3][3],
 
 ////////////////////////////////////////////////
 
+/* for cloth_remeshing.cpp */
+void cloth_to_mesh(struct Object *ob, struct ClothModifierData *clmd, struct Mesh *r_mesh);
+int cloth_build_springs(struct ClothModifierData *clmd, struct Mesh *mesh);
+struct BVHTree *bvhtree_build_from_cloth(struct ClothModifierData *clmd, float epsilon);
+
 #endif
diff --git a/source/blender/blenkernel/BKE_cloth_remeshing.h b/source/blender/blenkernel/BKE_cloth_remeshing.h
new file mode 100644
index 00000000000..2300e520cc4
--- /dev/null
+++ b/source/blender/blenkernel/BKE_cloth_remeshing.h
@@ -0,0 +1,45 @@
+/*
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ * The Original Code is Copyright (C) Blender Foundation.
+ * All rights reserved.
+ */
+#ifndef __BKE_CLOTH_REMESHING_H__
+#define __BKE_CLOTH_REMESHING_H__
+
+/** \file
+ * \ingroup bke
+ */
+
+#include <float.h>
+#include "BLI_math_inline.h"
+
+#define DO_INLINE MALWAYS_INLINE
+
+struct ClothModifierData;
+struct Mesh;
+struct Object;
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+Mesh *cloth_remeshing_step(Object *ob, ClothModifierData *clmd, Mesh *mesh);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
diff --git a/source/blender/blenkernel/CMakeLists.txt b/source/blender/blenkernel/CMakeLists.txt
index e564e91749c..296086cd175 100644
--- a/source/blender/blenkernel/CMakeLists.txt
+++ b/source/blender/blenkernel/CMakeLists.txt
@@ -85,6 +85,7 @@ set(SRC
   intern/camera.c
   intern/cdderivedmesh.c
   intern/cloth.c
+  intern/cloth_remeshing.cpp
   intern/collection.c
   intern/collision.c
   intern/colorband.c
@@ -245,6 +246,7 @@ set(SRC
   BKE_ccg.h
   BKE_cdderivedmesh.h
   BKE_cloth.h
+  BKE_cloth_remeshing.h
   BKE_collection.h
   BKE_collision.h
   BKE_colorband.h
diff --git a/source/blender/blenkernel/intern/cloth.c b/source/blender/blenkernel/intern/cloth.c
index eb77fd71734..38060fbaa9f 100644
--- a/source/blender/blenkernel/intern/cloth.c
+++ b/source/blender/blenkernel/intern/cloth.c
@@ -42,6 +42,7 @@
 
 #include "BKE_bvhutils.h"
 #include "BKE_cloth.h"
+#include "BKE_cloth_remeshing.h"
 #include "BKE_effect.h"
 #include "BKE_global.h"
 #include "BKE_mesh.h"
@@ -63,16 +64,13 @@
 /* Prototypes for internal functions.
  */
 static void cloth_to_object(Object *ob, ClothModifierData *clmd, float (*vertexCos)[3]);
-static void cloth_to_mesh(Object *ob, ClothModifierData *clmd, Mesh *r_mesh);
 static void cloth_from_mesh(ClothModifierData *clmd, Mesh *mesh);
 static int cloth_from_object(
     Object *ob, ClothModifierData *clmd, Mesh *mesh, float framenr, int first);
 static void cloth_update_springs(ClothModifierData *clmd);
 static void cloth_update_verts(Object *ob, ClothModifierData *clmd, Mesh *mesh);
 static void cloth_update_spring_lengths(ClothModifierData *clmd, Mesh *mesh);
-static int cloth_build_springs(ClothModifierData *clmd, Mesh *mesh);
 static void cloth_apply_vgroup(ClothModifierData *clmd, Mesh *mesh);
-static Mesh *cloth_remeshing_step(Object *ob, ClothModifierData *clmd, Mesh *mesh);
 
 typedef struct BendSpringRef {
   int index;
@@ -175,7 +173,7 @@ void cloth_init(ClothModifierData *clmd)
 #endif
 }
 
-static BVHTree *bvhtree_build_from_cloth(ClothModifierData *clmd, float epsilon)
+BVHTree *bvhtree_build_from_cloth(ClothModifierData *clmd, float epsilon)
 {
   unsigned int i;
   BVHTree *bvhtree;
@@ -391,774 +389,6 @@ static Mesh *do_step_cloth(
   return mesh_result;
 }
 
-/******************************************************************************
- * cloth_remeshing_step - remeshing function for adaptive cloth modifier
- * reference http://graphics.berkeley.edu/papers/Narain-AAR-2012-11/index.html
- ******************************************************************************/
-
-static CustomData_MeshMasks cloth_remeshing_get_cd_mesh_masks(void)
-{
-  CustomData_MeshMasks cddata_masks = {
-      .vmask = CD_MASK_ORIGINDEX, .emask = CD_MASK_ORIGINDEX, .pmask = CD_MASK_ORIGINDEX};
-  return cddata_masks;
-}
-
-static void cloth_remeshing_init_bmesh(Object *ob, ClothModifierData *clmd, Mesh *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();
-    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);
-    BMVert *v;
-    BMIter viter;
-    BM_ITER_MESH (v, &viter, clmd->clothObject->bm_prev, BM_VERTS_OF_MESH) {
-      mul_m4_v3(ob->obmat, v->co);
-    }
-    printf("remeshing_reset has been set to true or bm_prev does not exist\n");
-  }
-  clmd->clothObject->mvert_num_prev = clmd->clothObject->mvert_num;
-  clmd->clothObject->bm = clmd->clothObject->bm_prev;
-}
-
-static ClothVertex cloth_remeshing_mean_cloth_vert(ClothVertex *v1, ClothVertex *v2)
-{
-  ClothVertex new_vert;
-  /* TODO(Ish): flags */
-  add_v3_v3v3(new_vert.v, v1->v, v2->v);
-  mul_v3_fl(new_vert.v, 0.5f);
-
-  add_v3_v3v3(new_vert.xconst, v1->xconst, v2->xconst);
-  mul_v3_fl(new_vert.xconst, 0.5f);
-
-  add_v3_v3v3(new_vert.x, v1->x, v2->x);
-  mul_v3_fl(new_vert.x, 0.5f);
-
-  add_v3_v3v3(new_vert.xold, v1->xold, v2->xold);
-  mul_v3_fl(new_vert.xold, 0.5f);
-
-  add_v3_v3v3(new_vert.tx, v1->tx, v2->tx);
-  mul_v3_fl(new_vert.tx, 0.5f);
-
-  add_v3_v3v3(new_vert.txold, v1->txold, v2->txold);
-  mul_v3_fl(new_vert.txold, 0.5f);
-
-  add_v3_v3v3(new_vert.tv, v1->tv, v2->tv);
-  mul_v3_fl(new_vert.tv, 0.5f);
-
-  add_v3_v3v3(new_vert.impulse, v1->impulse, v2->impulse);
-  mul_v3_fl(new_vert.impulse, 0.5f);
-
-  add_v3_v3v3(new_vert.xrest, v1->xrest, v2->xrest);
-  mul_v3_fl(new_vert.xrest, 0.5f);
-
-  add_v3_v3v3(new_vert.dcvel, v1->dcvel, v2->dcvel);
-  mul_v3_fl(new_vert.dcvel, 0.5f);
-
-  new_vert.mass = (v1->mass + v2->mass) * 0.5;
-  new_vert.goal = (v1->goal + v2->goal) * 0.5;
-  new_vert.impulse_count = (v1->impulse_count + v2->impulse_count) * 0.5;
-  /* new_vert.avg_spring_len = (v1->avg_spring_len + v2->avg_spring_len) * 0.5; */
-  new_vert.struct_stiff = (v1->struct_stiff + v2->struct_stiff) * 0.5;
-  new_vert.bend_stiff = (v1->bend_stiff + v2->bend_stiff) * 0.5;
-  new_vert.shear_stiff = (v1->shear_stiff + v2->shear_stiff) * 0.5;
-  /* new_vert.spring_count = (v1->spring_count + v2->spring_count) * 0.5; */
-  new_vert.shrink_factor = (v1->shrink_factor + v2->shrink_factor) * 0.5;
-
-  return new_vert;
-}
-
-static Mesh *cloth_remeshing_update_cloth_object_bmesh(Object *ob, ClothModifierData *clmd)
-{
-  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);
-  if (clmd->clothObject->mvert_num_prev == clmd->clothObject->mvert_num) {
-    clmd->clothObject->bm_prev = BM_mesh_copy(clmd->clothObject->bm);
-    BM_mesh_free(clmd->clothObject->bm);
-    clmd->clothObject->bm = NULL;
-    return mesh_result;
-  }
-  /* cloth_remeshing_update_cloth_object_mesh(clmd, mesh_result); */
-  /**/
-
-  Cloth *cloth = clmd->clothObject;
-  // Free the springs.
-  if (cloth->springs != NULL) {
-    LinkNode *search = cloth->springs;
-    while (search) {
-      ClothSpring *spring = search->link;
-
-      MEM_SAFE_FREE(spring->pa);
-      MEM_SAFE_FREE(spring->pb);
-
-      MEM_freeN(spring);
-      search = search->next;
-    }
-    BLI_linklist_free(cloth->springs, NULL);
-
-    cloth->springs = NULL;
-  }
-
-  cloth->springs = NULL;
-  cloth->numsprings = 0;
-
-  // free BVH collision tree
-  if (cloth->bvhtree) {
-    BLI_bvhtree_free(cloth->bvhtree);
-    cloth->bvhtree = NULL;
-  }
-
-  if (cloth->bvhselftree) {
-    BLI_bvhtree_free(cloth->bvhselftree);
-    cloth->bvhselftree = NULL;
-  }
-
-  if (cloth->implicit) {
-    BPH_mass_spring_solver_free(cloth->implicit);
-    cloth->implicit = NULL;
-  }
-
-  // we save our faces for collision objects
-  if (cloth->tri) {
-    MEM_freeN(cloth->tri);
-    cloth->tri = NULL;
-  }
-
-  if (cloth->edgeset) {
-    BLI_edgeset_free(cloth->edgeset);
-    cloth->edgeset = NULL;
-  }
-
-  /* Now build those things */
-  const MLoop *mloop = mesh_result->mloop;
-  const MLoopTri *looptri = BKE_mesh_runtime_looptri_ensure(mesh_result);
-  const unsigned int looptri_num = mesh_result->runtime.looptris.len;
-
-  /* save face information */
-

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list