[Bf-blender-cvs] [091b2430bd7] temp-modifier-rm-cddm: Moved vertex merging to mesh_merge.c
Sybren A. Stüvel
noreply at git.blender.org
Thu Apr 26 17:22:22 CEST 2018
Commit: 091b2430bd79ec01c04f541659a47c2141f82748
Author: Sybren A. Stüvel
Date: Thu Apr 26 17:19:13 2018 +0200
Branches: temp-modifier-rm-cddm
https://developer.blender.org/rB091b2430bd79ec01c04f541659a47c2141f82748
Moved vertex merging to mesh_merge.c
===================================================================
M source/blender/blenkernel/CMakeLists.txt
M source/blender/blenkernel/intern/mesh.c
A source/blender/blenkernel/intern/mesh_merge.c
===================================================================
diff --git a/source/blender/blenkernel/CMakeLists.txt b/source/blender/blenkernel/CMakeLists.txt
index 91915bf679a..fe79f74ef27 100644
--- a/source/blender/blenkernel/CMakeLists.txt
+++ b/source/blender/blenkernel/CMakeLists.txt
@@ -140,6 +140,7 @@ set(SRC
intern/mesh.c
intern/mesh_evaluate.c
intern/mesh_mapping.c
+ intern/mesh_merge.c
intern/mesh_remap.c
intern/mesh_tangent.c
intern/mesh_validate.c
diff --git a/source/blender/blenkernel/intern/mesh.c b/source/blender/blenkernel/intern/mesh.c
index f966d383026..bc5800dd1fe 100644
--- a/source/blender/blenkernel/intern/mesh.c
+++ b/source/blender/blenkernel/intern/mesh.c
@@ -2748,648 +2748,6 @@ Mesh *BKE_mesh_new_from_object(
}
-
-/**
- * Poly compare with vtargetmap
- * Function used by #BKE_mesh_merge_verts.
- * The function compares poly_source after applying vtargetmap, with poly_target.
- * The two polys are identical if they share the same vertices in the same order, or in reverse order,
- * but starting position loopstart may be different.
- * The function is called with direct_reverse=1 for same order (i.e. same normal),
- * and may be called again with direct_reverse=-1 for reverse order.
- * \return 1 if polys are identical, 0 if polys are different.
- */
-static int cddm_poly_compare(
- MLoop *mloop_array,
- MPoly *mpoly_source, MPoly *mpoly_target,
- const int *vtargetmap, const int direct_reverse)
-{
- int vert_source, first_vert_source, vert_target;
- int i_loop_source;
- int i_loop_target, i_loop_target_start, i_loop_target_offset, i_loop_target_adjusted;
- bool compare_completed = false;
- bool same_loops = false;
-
- MLoop *mloop_source, *mloop_target;
-
- BLI_assert(direct_reverse == 1 || direct_reverse == -1);
-
- i_loop_source = 0;
- mloop_source = mloop_array + mpoly_source->loopstart;
- vert_source = mloop_source->v;
-
- if (vtargetmap[vert_source] != -1) {
- vert_source = vtargetmap[vert_source];
- }
- else {
- /* All source loop vertices should be mapped */
- BLI_assert(false);
- }
-
- /* Find same vertex within mpoly_target's loops */
- mloop_target = mloop_array + mpoly_target->loopstart;
- for (i_loop_target = 0; i_loop_target < mpoly_target->totloop; i_loop_target++, mloop_target++) {
- if (mloop_target->v == vert_source) {
- break;
- }
- }
-
- /* If same vertex not found, then polys cannot be equal */
- if (i_loop_target >= mpoly_target->totloop) {
- return false;
- }
-
- /* Now mloop_source and m_loop_target have one identical vertex */
- /* mloop_source is at position 0, while m_loop_target has advanced to find identical vertex */
- /* Go around the loop and check that all vertices match in same order */
- /* Skipping source loops when consecutive source vertices are mapped to same target vertex */
-
- i_loop_target_start = i_loop_target;
- i_loop_target_offset = 0;
- first_vert_source = vert_source;
-
- compare_completed = false;
- same_loops = false;
-
- while (!compare_completed) {
-
- vert_target = mloop_target->v;
-
- /* First advance i_loop_source, until it points to different vertex, after mapping applied */
- do {
- i_loop_source++;
-
- if (i_loop_source == mpoly_source->totloop) {
- /* End of loops for source, must match end of loop for target. */
- if (i_loop_target_offset == mpoly_target->totloop - 1) {
- compare_completed = true;
- same_loops = true;
- break; /* Polys are identical */
- }
- else {
- compare_completed = true;
- same_loops = false;
- break; /* Polys are different */
- }
- }
-
- mloop_source++;
- vert_source = mloop_source->v;
-
- if (vtargetmap[vert_source] != -1) {
- vert_source = vtargetmap[vert_source];
- }
- else {
- /* All source loop vertices should be mapped */
- BLI_assert(false);
- }
-
- } while (vert_source == vert_target);
-
- if (compare_completed) {
- break;
- }
-
- /* Now advance i_loop_target as well */
- i_loop_target_offset++;
-
- if (i_loop_target_offset == mpoly_target->totloop) {
- /* End of loops for target only, that means no match */
- /* except if all remaining source vertices are mapped to first target */
- for (; i_loop_source < mpoly_source->totloop; i_loop_source++, mloop_source++) {
- vert_source = vtargetmap[mloop_source->v];
- if (vert_source != first_vert_source) {
- compare_completed = true;
- same_loops = false;
- break;
- }
- }
- if (!compare_completed) {
- same_loops = true;
- }
- break;
- }
-
- /* Adjust i_loop_target for cycling around and for direct/reverse order defined by delta = +1 or -1 */
- i_loop_target_adjusted = (i_loop_target_start + direct_reverse * i_loop_target_offset) % mpoly_target->totloop;
- if (i_loop_target_adjusted < 0) {
- i_loop_target_adjusted += mpoly_target->totloop;
- }
- mloop_target = mloop_array + mpoly_target->loopstart + i_loop_target_adjusted;
- vert_target = mloop_target->v;
-
- if (vert_target != vert_source) {
- same_loops = false; /* Polys are different */
- break;
- }
- }
- return same_loops;
-}
-
-
-/* Utility stuff for using GHash with polys, used by vertex merging. */
-
-typedef struct PolyKey {
- int poly_index; /* index of the MPoly within the derived mesh */
- int totloops; /* number of loops in the poly */
- unsigned int hash_sum; /* Sum of all vertices indices */
- unsigned int hash_xor; /* Xor of all vertices indices */
-} PolyKey;
-
-
-static unsigned int poly_gset_hash_fn(const void *key)
-{
- const PolyKey *pk = key;
- return pk->hash_sum;
-}
-
-static bool poly_gset_compare_fn(const void *k1, const void *k2)
-{
- const PolyKey *pk1 = k1;
- const PolyKey *pk2 = k2;
- if ((pk1->hash_sum == pk2->hash_sum) &&
- (pk1->hash_xor == pk2->hash_xor) &&
- (pk1->totloops == pk2->totloops))
- {
- /* Equality - note that this does not mean equality of polys */
- return false;
- }
- else {
- return true;
- }
-}
-
-/**
- * Merge Verts
- *
- * This frees the given mesh and returns a new mesh.
- *
- * \param vtargetmap The table that maps vertices to target vertices. a value of -1
- * indicates a vertex is a target, and is to be kept.
- * This array is aligned with 'mesh->totvert'
- * \warning \a vtargetmap must **not** contain any chained mapping (v1 -> v2 -> v3 etc.), this is not supported
- * and will likely generate corrupted geometry.
- *
- * \param tot_vtargetmap The number of non '-1' values in vtargetmap. (not the size)
- *
- * \param merge_mode enum with two modes.
- * - #MESH_MERGE_VERTS_DUMP_IF_MAPPED
- * When called by the Mirror Modifier,
- * In this mode it skips any faces that have all vertices merged (to avoid creating pairs
- * of faces sharing the same set of vertices)
- * - #MESH_MERGE_VERTS_DUMP_IF_EQUAL
- * When called by the Array Modifier,
- * In this mode, faces where all vertices are merged are double-checked,
- * to see whether all target vertices actually make up a poly already.
- * Indeed it could be that all of a poly's vertices are merged,
- * but merged to vertices that do not make up a single poly,
- * in which case the original poly should not be dumped.
- * Actually this later behavior could apply to the Mirror Modifier as well, but the additional checks are
- * costly and not necessary in the case of mirror, because each vertex is only merged to its own mirror.
- *
- * \note #BKE_mesh_recalc_tessellation has to run on the returned DM if you want to access tessfaces.
- */
-Mesh *BKE_mesh_merge_verts(Mesh *mesh, const int *vtargetmap, const int tot_vtargetmap, const int merge_mode)
-{
- /* This was commented out back in 2013, see commit f45d8827bafe6b9eaf9de42f4054e9d84a21955d. */
-// #define USE_LOOPS
-
- Mesh *result = NULL;
-
- const int totvert = mesh->totvert;
- const int totedge = mesh->totedge;
- const int totloop = mesh->totloop;
- const int totpoly = mesh->totpoly;
-
- const int totvert_final = totvert - tot_vtargetmap;
-
- MVert *mv, *mvert = MEM_malloc_arrayN(totvert_final, sizeof(*mvert), __func__);
- int *oldv = MEM_malloc_arrayN(totvert_final, sizeof(*oldv), __func__);
- int *newv = MEM_malloc_arrayN(totvert, sizeof(*newv), __func__);
- STACK_DECLARE(mvert);
- STACK_DECLARE(oldv);
-
- /* Note: create (totedge + totloop) elements because partially invalid polys due to merge may require
- * generating new edges, and while in 99% cases we'll still end with less final edges than totedge,
- * cases can be forged that would end requiring more... */
- MEdge *med, *medge = MEM_malloc_arrayN((totedge + totloop), sizeof(*medge), __func__);
- int *olde = MEM_malloc_arrayN((totedge + totloop), sizeof(*olde), __func__);
- int *newe = MEM_malloc_arrayN((totedge + totloop), sizeof(*newe), __func__);
- STACK_DECLARE(medge);
- STACK_DECLARE(olde);
-
- MLoop *ml, *mloop = MEM_malloc_arrayN(totloop, sizeof(*mloop), __func__);
- int *oldl = MEM_malloc_arrayN(totloop, sizeof(*oldl), __func__);
-#ifdef USE_LOOPS
- int *newl = MEM_malloc_arrayN(totloop, sizeof(*newl), __func__);
-#endif
- STACK_DECLARE(mloop);
- STACK_DECLARE(oldl);
-
- MPoly *mp, *mpoly = MEM_malloc_arrayN(totpoly, sizeof(*medge), __func__);
- int *oldp = MEM_malloc_arrayN(totpoly, sizeof(*oldp), __func__);
- STACK_DECLARE(mpoly);
- STACK_DECLARE(oldp);
-
- EdgeHash *ehash = BLI_edgehash_new_ex(__func__, totedge);
-
- int i, j, c;
-
- PolyKey *poly_keys;
- GSet *poly_gset = NULL;
- MeshElemMap *poly_map = NULL;
- int *poly_map_mem = NULL;
-
- STACK_INIT(oldv, totvert_final);
- STACK_INIT(olde, totedge);
- STACK_INIT(oldl, totloop);
- STACK_INIT(oldp, totpoly);
-
- STACK_INIT(mvert, totvert_final);
- STACK_INIT(medge, totedge);
- STACK_INIT(mloop, totloop);
- STACK_INIT(mpoly, totpoly);
-
- /* fill newv with destination vertex indices */
- mv = mesh->mvert;
- c = 0;
- for (i = 0; i < totvert; i++, mv++) {
- if (vtargetmap[i] == -1) {
- STACK_PUSH(oldv, i);
- STACK_PUSH(mvert, *mv);
- newv[i] = c++;
- }
- else {
- /* dummy value */
- newv[i] = 0;
- }
- }
-
- /* now link target vertices to destination indices */
- for (i = 0; i < totvert; i++) {
- if (vtargetmap[i] != -1) {
- newv[i] = newv[vtargetmap[i]];
- }
- }
-
- /* Don't remap vertices in cddm->mloop, because we need to know the original
- * indice
@@ Diff output truncated at 10240 characters. @@
More information about the Bf-blender-cvs
mailing list