[Bf-blender-cvs] [ec58cf21fa5] temp-sybren-meshdeform: WIP mesh deform
Sybren A. Stüvel
noreply at git.blender.org
Tue May 8 09:59:32 CEST 2018
Commit: ec58cf21fa511792fb9aed635cb902363ac7dbc6
Author: Sybren A. Stüvel
Date: Fri May 4 11:42:55 2018 +0200
Branches: temp-sybren-meshdeform
https://developer.blender.org/rBec58cf21fa511792fb9aed635cb902363ac7dbc6
WIP mesh deform
===================================================================
M source/blender/blenkernel/BKE_bvhutils.h
M source/blender/blenkernel/BKE_mesh.h
M source/blender/blenkernel/CMakeLists.txt
M source/blender/blenkernel/intern/bvhutils.c
M source/blender/blenkernel/intern/mesh.c
M source/blender/blenkernel/intern/mesh_evaluate.c
A source/blender/blenkernel/intern/mesh_runtime.c
M source/blender/editors/armature/meshlaplacian.c
M source/blender/editors/include/ED_armature.h
M source/blender/makesdna/DNA_mesh_types.h
M source/blender/makesdna/DNA_modifier_types.h
M source/blender/modifiers/intern/MOD_meshdeform.c
M source/blender/modifiers/intern/MOD_util.c
M source/blender/modifiers/intern/MOD_util.h
===================================================================
diff --git a/source/blender/blenkernel/BKE_bvhutils.h b/source/blender/blenkernel/BKE_bvhutils.h
index 1b4bb08756b..1157e20d6ac 100644
--- a/source/blender/blenkernel/BKE_bvhutils.h
+++ b/source/blender/blenkernel/BKE_bvhutils.h
@@ -40,6 +40,7 @@
struct DerivedMesh;
struct BMEditMesh;
+struct Mesh;
struct MVert;
struct MFace;
@@ -142,6 +143,8 @@ BVHTree *bvhtree_from_editmesh_looptri_ex(
const BLI_bitmap *mask, int looptri_num_active,
float epsilon, int tree_type, int axis, BVHCache **bvhCache);
+BVHTree *BKE_bvhtree_from_mesh_looptri(
+ struct BVHTreeFromMesh *data, struct Mesh *mesh, float epsilon, int tree_type, int axis);
BVHTree *bvhtree_from_mesh_looptri_ex(
struct BVHTreeFromMesh *data,
const struct MVert *vert, const bool vert_allocated,
diff --git a/source/blender/blenkernel/BKE_mesh.h b/source/blender/blenkernel/BKE_mesh.h
index afb087ed822..ea8f1f9e8b3 100644
--- a/source/blender/blenkernel/BKE_mesh.h
+++ b/source/blender/blenkernel/BKE_mesh.h
@@ -101,11 +101,16 @@ struct Mesh *BKE_mesh_copy(struct Main *bmain, const struct Mesh *me);
void BKE_mesh_update_customdata_pointers(struct Mesh *me, const bool do_ensure_tess_cd);
void BKE_mesh_ensure_skin_customdata(struct Mesh *me);
-struct Mesh * BKE_mesh_from_template(
+struct Mesh *BKE_mesh_new(int numVerts, int numEdges, int numTessFaces,int numLoops, int numPolys);
+struct Mesh *BKE_mesh_from_template(
const struct Mesh *me_src,
int numVerts, int numEdges, int numTessFaces,
int numLoops, int numPolys);
+/* These functions construct a new Mesh, contrary to BKE_mesh_from_nurbs which modifies ob itself. */
+struct Mesh *BKE_new_mesh_from_curve(struct Object *ob);
+struct Mesh *BKE_new_mesh_from_curve_displist(struct Object *ob, struct ListBase *dispbase);
+
bool BKE_mesh_ensure_edit_data(struct Mesh *me);
bool BKE_mesh_clear_edit_data(struct Mesh *me);
@@ -181,7 +186,13 @@ int BKE_mesh_mselect_find(struct Mesh *me, int index, int type);
int BKE_mesh_mselect_active_get(struct Mesh *me, int type);
void BKE_mesh_mselect_active_set(struct Mesh *me, int index, int type);
+void BKE_mesh_apply_vert_coords(struct Mesh *mesh, float (*vertCoords)[3]);
+
+/* *** mesh_runtime.c *** */
+void BKE_mesh_runtime_recalc_looptri(struct Mesh *mesh);
+int BKE_mesh_get_looptri_num(struct Mesh *mesh);
+const struct MLoopTri *BKE_mesh_get_looptri_array(struct Mesh *mesh);
/* *** mesh_evaluate.c *** */
@@ -201,6 +212,7 @@ void BKE_mesh_calc_normals_poly(
int numLoops, int numPolys, float (*r_polyNors)[3],
const bool only_face_normals);
void BKE_mesh_calc_normals(struct Mesh *me);
+void BKE_mesh_ensure_normals(struct Mesh *me);
void BKE_mesh_calc_normals_tessface(
struct MVert *mverts, int numVerts,
const struct MFace *mfaces, int numFaces,
diff --git a/source/blender/blenkernel/CMakeLists.txt b/source/blender/blenkernel/CMakeLists.txt
index 9e241b7fa81..83241d3c4c7 100644
--- a/source/blender/blenkernel/CMakeLists.txt
+++ b/source/blender/blenkernel/CMakeLists.txt
@@ -143,6 +143,7 @@ set(SRC
intern/mesh_mapping.c
intern/mesh_merge.c
intern/mesh_remap.c
+ intern/mesh_runtime.c
intern/mesh_tangent.c
intern/mesh_validate.c
intern/modifier.c
diff --git a/source/blender/blenkernel/intern/bvhutils.c b/source/blender/blenkernel/intern/bvhutils.c
index 0240bb4b624..8c390fb8fd3 100644
--- a/source/blender/blenkernel/intern/bvhutils.c
+++ b/source/blender/blenkernel/intern/bvhutils.c
@@ -34,6 +34,7 @@
#include <math.h>
#include <assert.h>
+#include "DNA_mesh_types.h"
#include "DNA_meshdata_types.h"
#include "BLI_utildefines.h"
@@ -43,6 +44,7 @@
#include "BKE_DerivedMesh.h"
#include "BKE_editmesh.h"
+#include "BKE_mesh.h"
#include "MEM_guardedalloc.h"
@@ -1161,6 +1163,70 @@ BVHTree *bvhtree_from_mesh_get(
return tree;
}
+/* This is a Mesh-specific copy of bvhtree_from_mesh_looptri() */
+/**
+ * Builds a bvh tree where nodes are the looptri faces of the given dm
+ *
+ * \note for editmesh this is currently a duplicate of bvhtree_from_mesh_faces
+ */
+BVHTree *BKE_bvhtree_from_mesh_looptri(
+ BVHTreeFromMesh *data, Mesh *mesh,
+ float epsilon, int tree_type, int axis)
+{
+ BVHTree *tree;
+ MVert *mvert = NULL;
+ MLoop *mloop = NULL;
+ const MLoopTri *looptri = NULL;
+
+ BLI_rw_mutex_lock(&cache_rwlock, THREAD_LOCK_READ);
+ tree = bvhcache_find(mesh->runtime.bvh_cache, BVHTREE_FROM_LOOPTRI);
+ BLI_rw_mutex_unlock(&cache_rwlock);
+
+ mvert = mesh->mvert;
+ mloop = mesh->mloop;
+ looptri = BKE_mesh_get_looptri_array(mesh);
+
+ /* Not in cache */
+ if (tree == NULL) {
+ BLI_rw_mutex_lock(&cache_rwlock, THREAD_LOCK_WRITE);
+ tree = bvhcache_find(mesh->runtime.bvh_cache, BVHTREE_FROM_LOOPTRI);
+ if (tree == NULL) {
+ int looptri_num = BKE_mesh_get_looptri_num(mesh);
+
+ /* this assert checks we have looptris,
+ * if not caller should use DM_ensure_looptri() */
+ BLI_assert(!(looptri_num == 0 && mesh->totpoly != 0));
+
+ tree = bvhtree_from_mesh_looptri_create_tree(
+ epsilon, tree_type, axis,
+ mvert, mloop, looptri, looptri_num, NULL, -1);
+ if (tree) {
+ /* Save on cache for later use */
+ /* printf("BVHTree built and saved on cache\n"); */
+ bvhcache_insert(&mesh->runtime.bvh_cache, tree, BVHTREE_FROM_LOOPTRI);
+ }
+ }
+ BLI_rw_mutex_unlock(&cache_rwlock);
+ }
+ else {
+ /* printf("BVHTree is already build, using cached tree\n"); */
+ }
+
+ if (tree) {
+ /* Setup BVHTreeFromMesh */
+ bvhtree_from_mesh_looptri_setup_data(
+ data, tree, true, epsilon,
+ mvert, false,
+ mloop, false,
+ looptri, false);
+ }
+ else {
+ memset(data, 0, sizeof(*data));
+ }
+
+ return tree;
+}
+
/** \} */
diff --git a/source/blender/blenkernel/intern/mesh.c b/source/blender/blenkernel/intern/mesh.c
index 3163ea6551e..690005f1457 100644
--- a/source/blender/blenkernel/intern/mesh.c
+++ b/source/blender/blenkernel/intern/mesh.c
@@ -51,6 +51,7 @@
#include "BKE_main.h"
#include "BKE_DerivedMesh.h"
#include "BKE_global.h"
+#include "BKE_idcode.h"
#include "BKE_mesh.h"
#include "BKE_mesh_mapping.h"
#include "BKE_displist.h"
@@ -614,6 +615,42 @@ void BKE_mesh_copy_data(Main *bmain, Mesh *me_dst, const Mesh *me_src, const int
}
}
+Mesh *BKE_mesh_new(int numVerts, int numEdges, int numTessFaces,int numLoops, int numPolys)
+{
+ Mesh *mesh = BKE_libblock_alloc(NULL, ID_ME,
+ BKE_idcode_to_name(ID_ME),
+ LIB_ID_CREATE_NO_MAIN |
+ LIB_ID_CREATE_NO_USER_REFCOUNT |
+ LIB_ID_CREATE_NO_DEG_TAG);
+ BKE_libblock_init_empty(&mesh->id);
+
+ /* don't use CustomData_reset(...); because we dont want to touch customdata */
+ copy_vn_i(mesh->vdata.typemap, CD_NUMTYPES, -1);
+ copy_vn_i(mesh->edata.typemap, CD_NUMTYPES, -1);
+ copy_vn_i(mesh->fdata.typemap, CD_NUMTYPES, -1);
+ copy_vn_i(mesh->ldata.typemap, CD_NUMTYPES, -1);
+ copy_vn_i(mesh->pdata.typemap, CD_NUMTYPES, -1);
+
+ CustomData_add_layer(&mesh->vdata, CD_ORIGINDEX, CD_CALLOC, NULL, numVerts);
+ CustomData_add_layer(&mesh->edata, CD_ORIGINDEX, CD_CALLOC, NULL, numEdges);
+ CustomData_add_layer(&mesh->fdata, CD_ORIGINDEX, CD_CALLOC, NULL, numTessFaces);
+ CustomData_add_layer(&mesh->pdata, CD_ORIGINDEX, CD_CALLOC, NULL, numPolys);
+
+ CustomData_add_layer(&mesh->vdata, CD_MVERT, CD_CALLOC, NULL, numVerts);
+ CustomData_add_layer(&mesh->edata, CD_MEDGE, CD_CALLOC, NULL, numEdges);
+ CustomData_add_layer(&mesh->fdata, CD_MFACE, CD_CALLOC, NULL, numTessFaces);
+ CustomData_add_layer(&mesh->ldata, CD_MLOOP, CD_CALLOC, NULL, numLoops);
+ CustomData_add_layer(&mesh->pdata, CD_MPOLY, CD_CALLOC, NULL, numPolys);
+
+ mesh->mvert = CustomData_get_layer(&mesh->vdata, CD_MVERT);
+ mesh->medge = CustomData_get_layer(&mesh->edata, CD_MEDGE);
+ mesh->mface = CustomData_get_layer(&mesh->fdata, CD_MFACE);
+ mesh->mloop = CustomData_get_layer(&mesh->ldata, CD_MLOOP);
+ mesh->mpoly = CustomData_get_layer(&mesh->pdata, CD_MPOLY);
+
+ return mesh;
+}
+
static Mesh *mesh_from_template_ex(
const Mesh *me_src,
int numVerts, int numEdges, int numTessFaces,
@@ -1460,6 +1497,59 @@ int BKE_mesh_nurbs_displist_to_mdata(
return 0;
}
+Mesh *BKE_new_mesh_from_curve_displist(Object *ob, ListBase *dispbase)
+{
+ Curve *cu = ob->data;
+ Mesh *mesh;
+ MVert *allvert;
+ MEdge *alledge;
+ MLoop *allloop;
+ MPoly *allpoly;
+ MLoopUV *alluv = NULL;
+ int totvert, totedge, totloop, totpoly;
+ bool use_orco_uv = (cu->flag & CU_UV_ORCO) != 0;
+
+ if (BKE_mesh_nurbs_displist_to_mdata(
+ ob, dispbase, &allvert, &totvert, &alledge,
+ &totedge, &allloop, &allpoly, (use_orco_uv) ? &alluv : NULL,
+ &totloop, &totpoly) != 0)
+ {
+ /* Error initializing mdata. This often happens when curve is empty */
+ return BKE_mesh_new(0, 0, 0, 0, 0);
+ }
+
+ mesh = BKE_mesh_new(totvert, totedge, 0, totloop, totpoly);
+// mesh->deformedOnly = 1;
+ mesh->runtime.cd_dirty_vert |= CD_MASK_NORMAL;
+
+ memcpy(mesh->mvert, allvert, totvert * sizeof(MVert));
+ memcpy(mesh->medge, alledge, totedge * sizeof(MEdge));
+ memcpy(mesh->mloop, allloop, totloop * sizeof(MLoop));
+ memcpy(mesh->mpoly, allpoly, totpoly * sizeof(MPoly));
+
+ if (alluv) {
+ const char *uvname = "Orco";
+ CustomData_add_layer_named(&mesh->ldata, CD_MLOOPUV, CD_ASSIGN, alluv, totloop, uvname);
+ }
+
+ MEM_freeN(allvert);
+ MEM_freeN(alledge);
+ MEM_freeN(allloop);
+ MEM_freeN(allpoly);
+
+ return mesh;
+}
+
+Mesh *BKE_new_mesh_from_curve(Object *ob)
+{
+ ListBase disp = {NULL, NULL};
+
+ if (ob->curve_cache) {
+ disp = ob->curve_cache->disp;
+ }
+
+ return BKE_new_mesh_from_curve_displist(ob, &disp);
+}
/* this may fail replacing ob->data, be sure to check ob->type */
void BKE_mesh_from_nurbs_displist(Object *ob, ListBase *dispbase, const bool use_orco_uv, const char *obdata_name)
@@ -2189,6 +2279,22 @@ void BKE_mesh_mselect_active_set(Mesh *me, int index, int type)
(me->mselect[me->totselect - 1].type == type));
}
+
+void BKE_mesh_apply_vert_coords(Mesh *mesh, float (*vertCoords)[3])
+{
+ MVert *vert;
+ int i;
+
+ /* this will just return the pointer if
@@ Diff output truncated at 10240 characters. @@
More information about the Bf-blender-cvs
mailing list