code cleanup: make bmesh operator names more consistant since python has access to...
[blender.git] / source / blender / bmesh / operators / bmo_removedoubles.c
index 2eb1cf7db3e7ce8821a6f2389977d3c9db3ba7d6..e866abcb6dc2e8327a4dfde0754911ad58d4983b 100644 (file)
  * ***** END GPL LICENSE BLOCK *****
  */
 
+/** \file blender/bmesh/operators/bmo_removedoubles.c
+ *  \ingroup bmesh
+ */
+
 #include "MEM_guardedalloc.h"
 
 #include "BLI_math.h"
@@ -28,9 +32,9 @@
 #include "BKE_customdata.h"
 
 #include "bmesh.h"
-#include "bmesh_private.h"
+#include "intern/bmesh_private.h"
 
-#include "bmesh_operators_private.h" /* own include */
+#include "intern/bmesh_operators_private.h" /* own include */
 
 static void remdoubles_splitface(BMFace *f, BMesh *bm, BMOperator *op)
 {
@@ -39,10 +43,10 @@ static void remdoubles_splitface(BMFace *f, BMesh *bm, BMOperator *op)
        BMVert *v2, *doub;
        int split = FALSE;
 
-       BM_ITER(l, &liter, bm, BM_LOOPS_OF_FACE, f) {
-               v2 = BMO_slot_map_ptr_get(bm, op, "targetmap", l->v);
+       BM_ITER_ELEM (l, &liter, f, BM_LOOPS_OF_FACE) {
+               v2 = BMO_slot_map_ptr_get(op->slots_in, "targetmap", l->v);
                /* ok: if v2 is NULL (e.g. not in the map) then it's
-                *     a target vert, otherwise it's a doubl */
+                *     a target vert, otherwise it's a double */
                if ((v2 && BM_vert_in_face(f, v2)) &&
                    (v2 != l->prev->v) &&
                    (v2 != l->next->v))
@@ -55,7 +59,7 @@ static void remdoubles_splitface(BMFace *f, BMesh *bm, BMOperator *op)
 
        if (split && doub != v2) {
                BMLoop *nl;
-               BMFace *f2 = BM_face_split(bm, f, doub, v2, &nl, NULL);
+               BMFace *f2 = BM_face_split(bm, f, doub, v2, &nl, NULL, FALSE);
 
                remdoubles_splitface(f, bm, op);
                remdoubles_splitface(f2, bm, op);
@@ -92,7 +96,7 @@ int remdoubles_face_overlaps(BMesh *bm, BMVert **varr,
 }
 #endif
 
-void bmesh_weldverts_exec(BMesh *bm, BMOperator *op)
+void bmo_weld_verts_exec(BMesh *bm, BMOperator *op)
 {
        BMIter iter, liter;
        BMVert *v, *v2;
@@ -103,8 +107,9 @@ void bmesh_weldverts_exec(BMesh *bm, BMOperator *op)
        BMFace *f, *f2;
        int a, b;
 
-       BM_ITER(v, &iter, bm, BM_VERTS_OF_MESH, NULL) {
-               if ((v2 = BMO_slot_map_ptr_get(bm, op, "targetmap", v))) {
+       /* mark merge verts for deletion */
+       BM_ITER_MESH (v, &iter, bm, BM_VERTS_OF_MESH) {
+               if ((v2 = BMO_slot_map_ptr_get(op->slots_in, "targetmap", v))) {
                        BMO_elem_flag_enable(bm, v, ELE_DEL);
 
                        /* merge the vertex flags, else we get randomly selected/unselected verts */
@@ -112,33 +117,37 @@ void bmesh_weldverts_exec(BMesh *bm, BMOperator *op)
                }
        }
 
-       BM_ITER(f, &iter, bm, BM_FACES_OF_MESH, NULL) {
+       /* check if any faces are getting their own corners merged
+        * together, split face if so */
+       BM_ITER_MESH (f, &iter, bm, BM_FACES_OF_MESH) {
                remdoubles_splitface(f, bm, op);
        }
-       
-       BM_ITER(e, &iter, bm, BM_EDGES_OF_MESH, NULL) {
+
+       BM_ITER_MESH (e, &iter, bm, BM_EDGES_OF_MESH) {
                if (BMO_elem_flag_test(bm, e->v1, ELE_DEL) || BMO_elem_flag_test(bm, e->v2, ELE_DEL)) {
-                       v = BMO_slot_map_ptr_get(bm, op, "targetmap", e->v1);
-                       v2 = BMO_slot_map_ptr_get(bm, op, "targetmap", e->v2);
+                       v  = BMO_slot_map_ptr_get(op->slots_in, "targetmap", e->v1);
+                       v2 = BMO_slot_map_ptr_get(op->slots_in, "targetmap", e->v2);
                        
                        if (!v) v = e->v1;
                        if (!v2) v2 = e->v2;
 
-                       if (v == v2)
+                       if (v == v2) {
                                BMO_elem_flag_enable(bm, e, EDGE_COL);
-                       else if (!BM_edge_exists(v, v2))
+                       }
+                       else if (!BM_edge_exists(v, v2)) {
                                BM_edge_create(bm, v, v2, e, TRUE);
+                       }
 
                        BMO_elem_flag_enable(bm, e, ELE_DEL);
                }
        }
 
        /* BMESH_TODO, stop abusing face index here */
-       BM_ITER(f, &iter, bm, BM_FACES_OF_MESH, NULL) {
+       BM_ITER_MESH (f, &iter, bm, BM_FACES_OF_MESH) {
                BM_elem_index_set(f, 0); /* set_dirty! */
-               BM_ITER(l, &liter, bm, BM_LOOPS_OF_FACE, f) {
+               BM_ITER_ELEM (l, &liter, f, BM_LOOPS_OF_FACE) {
                        if (BMO_elem_flag_test(bm, l->v, ELE_DEL)) {
-                               BMO_elem_flag_enable(bm, f, FACE_MARK|ELE_DEL);
+                               BMO_elem_flag_enable(bm, f, FACE_MARK | ELE_DEL);
                        }
                        if (BMO_elem_flag_test(bm, l->e, EDGE_COL)) {
                                BM_elem_index_set(f, BM_elem_index_get(f) + 1); /* set_dirty! */
@@ -147,7 +156,9 @@ void bmesh_weldverts_exec(BMesh *bm, BMOperator *op)
        }
        bm->elem_index_dirty |= BM_FACE;
 
-       BM_ITER(f, &iter, bm, BM_FACES_OF_MESH, NULL) {
+       /* faces get "modified" by creating new faces here, then at the
+        * end the old faces are deleted */
+       BM_ITER_MESH (f, &iter, bm, BM_FACES_OF_MESH) {
                if (!BMO_elem_flag_test(bm, f, FACE_MARK))
                        continue;
 
@@ -159,14 +170,14 @@ void bmesh_weldverts_exec(BMesh *bm, BMOperator *op)
                BLI_array_empty(edges);
                BLI_array_empty(loops);
                a = 0;
-               BM_ITER(l, &liter, bm, BM_LOOPS_OF_FACE, f) {
+               BM_ITER_ELEM (l, &liter, f, BM_LOOPS_OF_FACE) {
                        v = l->v;
                        v2 = l->next->v;
                        if (BMO_elem_flag_test(bm, v, ELE_DEL)) {
-                               v = BMO_slot_map_ptr_get(bm, op, "targetmap", v);
+                               v = BMO_slot_map_ptr_get(op->slots_in, "targetmap", v);
                        }
                        if (BMO_elem_flag_test(bm, v2, ELE_DEL)) {
-                               v2 = BMO_slot_map_ptr_get(bm, op, "targetmap", v2);
+                               v2 = BMO_slot_map_ptr_get(op->slots_in, "targetmap", v2);
                        }
                        
                        e2 = v != v2 ? BM_edge_exists(v, v2) : NULL;
@@ -180,8 +191,8 @@ void bmesh_weldverts_exec(BMesh *bm, BMOperator *op)
                                        continue;
                                }
 
-                               BLI_array_growone(edges);
-                               BLI_array_growone(loops);
+                               BLI_array_grow_one(edges);
+                               BLI_array_grow_one(loops);
 
                                edges[a] = e2;
                                loops[a] = l;
@@ -196,10 +207,10 @@ void bmesh_weldverts_exec(BMesh *bm, BMOperator *op)
                v2 = loops[1]->v;
 
                if (BMO_elem_flag_test(bm, v, ELE_DEL)) {
-                       v = BMO_slot_map_ptr_get(bm, op, "targetmap", v);
+                       v = BMO_slot_map_ptr_get(op->slots_in, "targetmap", v);
                }
                if (BMO_elem_flag_test(bm, v2, ELE_DEL)) {
-                       v2 = BMO_slot_map_ptr_get(bm, op, "targetmap", v2);
+                       v2 = BMO_slot_map_ptr_get(op->slots_in, "targetmap", v2);
                }
                
                f2 = BM_face_create_ngon(bm, v, v2, edges, a, TRUE);
@@ -207,7 +218,7 @@ void bmesh_weldverts_exec(BMesh *bm, BMOperator *op)
                        BM_elem_attrs_copy(bm, bm, f, f2);
 
                        a = 0;
-                       BM_ITER(l, &liter, bm, BM_LOOPS_OF_FACE, f2) {
+                       BM_ITER_ELEM (l, &liter, f2, BM_LOOPS_OF_FACE) {
                                l2 = loops[a];
                                BM_elem_attrs_copy(bm, bm, l2, l);
 
@@ -216,7 +227,7 @@ void bmesh_weldverts_exec(BMesh *bm, BMOperator *op)
                }
        }
 
-       BMO_op_callf(bm, "del geom=%fvef context=%i", ELE_DEL, DEL_ONLYTAGGED);
+       BMO_op_callf(bm, op->flag, "delete geom=%fvef context=%i", ELE_DEL, DEL_ONLYTAGGED);
 
        BLI_array_free(edges);
        BLI_array_free(loops);
@@ -233,16 +244,16 @@ static int vergaverco(const void *e1, const void *e2)
        else return 0;
 }
 
-#define VERT_TESTED    1
+// #define VERT_TESTED 1 // UNUSED
 #define VERT_DOUBLE    2
 #define VERT_TARGET    4
 #define VERT_KEEP      8
-#define VERT_MARK      16
+// #define VERT_MARK   16 // UNUSED
 #define VERT_IN                32
 
 #define EDGE_MARK      1
 
-void bmesh_pointmerge_facedata_exec(BMesh *bm, BMOperator *op)
+void bmo_pointmerge_facedata_exec(BMesh *bm, BMOperator *op)
 {
        BMOIter siter;
        BMIter iter;
@@ -251,14 +262,14 @@ void bmesh_pointmerge_facedata_exec(BMesh *bm, BMOperator *op)
        float fac;
        int i, tot;
 
-       snapv = BMO_iter_new(&siter, bm, op, "snapv", BM_VERT);
+       snapv = BMO_iter_new(&siter, op->slots_in, "snapv", BM_VERT);
        tot = BM_vert_face_count(snapv);
 
        if (!tot)
                return;
 
        fac = 1.0f / tot;
-       BM_ITER(l, &iter, bm, BM_LOOPS_OF_VERT, snapv) {
+       BM_ITER_ELEM (l, &iter, snapv, BM_LOOPS_OF_VERT) {
                if (!firstl) {
                        firstl = l;
                }
@@ -279,8 +290,8 @@ void bmesh_pointmerge_facedata_exec(BMesh *bm, BMOperator *op)
                }
        }
 
-       BMO_ITER(v, &siter, bm, op, "verts", BM_VERT) {
-               BM_ITER(l, &iter, bm, BM_LOOPS_OF_VERT, v) {
+       BMO_ITER (v, &siter, op->slots_in, "verts", BM_VERT) {
+               BM_ITER_ELEM (l, &iter, v, BM_LOOPS_OF_VERT) {
                        if (l == firstl) {
                                continue;
                        }
@@ -290,7 +301,7 @@ void bmesh_pointmerge_facedata_exec(BMesh *bm, BMOperator *op)
        }
 }
 
-void bmesh_vert_average_facedata_exec(BMesh *bm, BMOperator *op)
+void bmo_average_vert_facedata_exec(BMesh *bm, BMOperator *op)
 {
        BMOIter siter;
        BMIter iter;
@@ -307,8 +318,8 @@ void bmesh_vert_average_facedata_exec(BMesh *bm, BMOperator *op)
                type = bm->ldata.layers[i].type;
                CustomData_data_initminmax(type, &min, &max);
 
-               BMO_ITER(v, &siter, bm, op, "verts", BM_VERT) {
-                       BM_ITER(l, &iter, bm, BM_LOOPS_OF_VERT, v) {
+               BMO_ITER (v, &siter, op->slots_in, "verts", BM_VERT) {
+                       BM_ITER_ELEM (l, &iter, v, BM_LOOPS_OF_VERT) {
                                block = CustomData_bmesh_get_layer_n(&bm->ldata, l->head.data, i);
                                CustomData_data_dominmax(type, block, &min, &max);
                        }
@@ -318,8 +329,8 @@ void bmesh_vert_average_facedata_exec(BMesh *bm, BMOperator *op)
                CustomData_data_multiply(type, &max, 0.5f);
                CustomData_data_add(type, &min, &max);
 
-               BMO_ITER(v, &siter, bm, op, "verts", BM_VERT) {
-                       BM_ITER(l, &iter, bm, BM_LOOPS_OF_VERT, v) {
+               BMO_ITER (v, &siter, op->slots_in, "verts", BM_VERT) {
+                       BM_ITER_ELEM (l, &iter, v, BM_LOOPS_OF_VERT) {
                                block = CustomData_bmesh_get_layer_n(&bm->ldata, l->head.data, i);
                                CustomData_data_copy_value(type, &min, block);
                        }
@@ -327,25 +338,25 @@ void bmesh_vert_average_facedata_exec(BMesh *bm, BMOperator *op)
        }
 }
 
-void bmesh_pointmerge_exec(BMesh *bm, BMOperator *op)
+void bmo_pointmerge_exec(BMesh *bm, BMOperator *op)
 {
        BMOperator weldop;
        BMOIter siter;
        BMVert *v, *snapv = NULL;
        float vec[3];
        
-       BMO_slot_vec_get(op, "mergeco", vec);
+       BMO_slot_vec_get(op->slots_in, "merge_co", vec);
 
-       //BMO_op_callf(bm, "collapse_uvs edges=%s", op, "edges");
-       BMO_op_init(bm, &weldop, "weldverts");
+       //BMO_op_callf(bm, op->flag, "collapse_uvs edges=%s", op, "edges");
+       BMO_op_init(bm, &weldop, op->flag, "weld_verts");
        
-       BMO_ITER(v, &siter, bm, op, "verts", BM_VERT) {
+       BMO_ITER (v, &siter, op->slots_in, "verts", BM_VERT) {
                if (!snapv) {
                        snapv = v;
                        copy_v3_v3(snapv->co, vec);
                }
                else {
-                       BMO_slot_map_ptr_insert(bm, &weldop, "targetmap", v, snapv);
+                       BMO_slot_map_ptr_insert(&weldop, weldop.slots_in, "targetmap", v, snapv);
                }
        }
 
@@ -353,26 +364,27 @@ void bmesh_pointmerge_exec(BMesh *bm, BMOperator *op)
        BMO_op_finish(bm, &weldop);
 }
 
-void bmesh_collapse_exec(BMesh *bm, BMOperator *op)
+void bmo_collapse_exec(BMesh *bm, BMOperator *op)
 {
        BMOperator weldop;
        BMWalker walker;
        BMIter iter;
        BMEdge *e, **edges = NULL;
        BLI_array_declare(edges);
-       float min[3], max[3];
+       float min[3], max[3], center[3];
        int i, tot;
        
-       BMO_op_callf(bm, "collapse_uvs edges=%s", op, "edges");
-       BMO_op_init(bm, &weldop, "weldverts");
+       BMO_op_callf(bm, op->flag, "collapse_uvs edges=%s", op, "edges");
+       BMO_op_init(bm, &weldop, op->flag, "weld_verts");
 
-       BMO_slot_buffer_flag_enable(bm, op, "edges", EDGE_MARK, BM_EDGE);
+       BMO_slot_buffer_flag_enable(bm, op->slots_in, "edges", BM_EDGE, EDGE_MARK);
 
        BMW_init(&walker, bm, BMW_SHELL,
-                BMW_MASK_NOP, EDGE_MARK, BMW_MASK_NOP, BMW_MASK_NOP,
+                BMW_MASK_NOP, EDGE_MARK, BMW_MASK_NOP,
+                BMW_FLAG_NOP, /* no need to use BMW_FLAG_TEST_HIDDEN, already marked data */
                 BMW_NIL_LAY);
 
-       BM_ITER(e, &iter, bm, BM_EDGES_OF_MESH, NULL) {
+       BM_ITER_MESH (e, &iter, bm, BM_EDGES_OF_MESH) {
                if (!BMO_elem_flag_test(bm, e, EDGE_MARK))
                        continue;
 
@@ -381,25 +393,24 @@ void bmesh_collapse_exec(BMesh *bm, BMOperator *op)
 
                INIT_MINMAX(min, max);
                for (tot = 0; e; tot++, e = BMW_step(&walker)) {
-                       BLI_array_growone(edges);
+                       BLI_array_grow_one(edges);
                        edges[tot] = e;
 
-                       DO_MINMAX(e->v1->co, min, max);
-                       DO_MINMAX(e->v2->co, min, max);
+                       minmax_v3v3_v3(min, max, e->v1->co);
+                       minmax_v3v3_v3(min, max, e->v2->co);
                }
 
-               add_v3_v3v3(min, min, max);
-               mul_v3_fl(min, 0.5f);
+               mid_v3_v3v3(center, min, max);
 
                /* snap edges to a point.  for initial testing purposes anyway */
                for (i = 0; i < tot; i++) {
-                       copy_v3_v3(edges[i]->v1->co, min);
-                       copy_v3_v3(edges[i]->v2->co, min);
+                       copy_v3_v3(edges[i]->v1->co, center);
+                       copy_v3_v3(edges[i]->v2->co, center);
                        
                        if (edges[i]->v1 != edges[0]->v1)
-                               BMO_slot_map_ptr_insert(bm, &weldop, "targetmap", edges[i]->v1, edges[0]->v1);
+                               BMO_slot_map_ptr_insert(&weldop, weldop.slots_in, "targetmap", edges[i]->v1, edges[0]->v1);
                        if (edges[i]->v2 != edges[0]->v1)
-                               BMO_slot_map_ptr_insert(bm, &weldop, "targetmap", edges[i]->v2, edges[0]->v1);
+                               BMO_slot_map_ptr_insert(&weldop, weldop.slots_in, "targetmap", edges[i]->v2, edges[0]->v1);
                }
        }
        
@@ -410,8 +421,8 @@ void bmesh_collapse_exec(BMesh *bm, BMOperator *op)
        BLI_array_free(edges);
 }
 
-/* uv collapse functio */
-static void bmesh_collapsecon_do_layer(BMesh *bm, BMOperator *op, int layer)
+/* uv collapse function */
+static void bmo_collapsecon_do_layer(BMesh *bm, BMOperator *op, int layer)
 {
        BMIter iter, liter;
        BMFace *f;
@@ -425,23 +436,24 @@ static void bmesh_collapsecon_do_layer(BMesh *bm, BMOperator *op, int layer)
        /* clear all short flags */
        BMO_mesh_flag_disable_all(bm, op, BM_ALL, (1 << 16) - 1);
 
-       BMO_slot_buffer_flag_enable(bm, op, "edges", EDGE_MARK, BM_EDGE);
+       BMO_slot_buffer_flag_enable(bm, op->slots_in, "edges", BM_EDGE, EDGE_MARK);
 
        BMW_init(&walker, bm, BMW_LOOPDATA_ISLAND,
-                BMW_MASK_NOP, EDGE_MARK, BMW_MASK_NOP, BMW_MASK_NOP,
+                BMW_MASK_NOP, EDGE_MARK, BMW_MASK_NOP,
+                BMW_FLAG_NOP, /* no need to use BMW_FLAG_TEST_HIDDEN, already marked data */
                 layer);
 
-       BM_ITER(f, &iter, bm, BM_FACES_OF_MESH, NULL) {
-               BM_ITER(l, &liter, bm, BM_LOOPS_OF_FACE, f) {
+       BM_ITER_MESH (f, &iter, bm, BM_FACES_OF_MESH) {
+               BM_ITER_ELEM (l, &liter, f, BM_LOOPS_OF_FACE) {
                        if (BMO_elem_flag_test(bm, l->e, EDGE_MARK)) {
-                               /* wal */
+                               /* walk */
                                BLI_array_empty(blocks);
                                tot = 0;
                                l2 = BMW_begin(&walker, l);
 
                                CustomData_data_initminmax(type, &min, &max);
                                for (tot = 0; l2; tot++, l2 = BMW_step(&walker)) {
-                                       BLI_array_growone(blocks);
+                                       BLI_array_grow_one(blocks);
                                        blocks[tot] = CustomData_bmesh_get_layer_n(&bm->ldata, l2->head.data, layer);
                                        CustomData_data_dominmax(type, blocks[tot], &min, &max);
                                }
@@ -451,7 +463,7 @@ static void bmesh_collapsecon_do_layer(BMesh *bm, BMOperator *op, int layer)
                                        CustomData_data_multiply(type, &max, 0.5f);
                                        CustomData_data_add(type, &min, &max);
 
-                                       /* snap CD (uv, vcol) points to their centroi */
+                                       /* snap CD (uv, vcol) points to their centroid */
                                        for (i = 0; i < tot; i++) {
                                                CustomData_data_copy_value(type, &min, blocks[i]);
                                        }
@@ -464,101 +476,107 @@ static void bmesh_collapsecon_do_layer(BMesh *bm, BMOperator *op, int layer)
        BLI_array_free(blocks);
 }
 
-void bmesh_collapsecon_exec(BMesh *bm, BMOperator *op)
+void bmo_collapse_uvs_exec(BMesh *bm, BMOperator *op)
 {
        int i;
 
        for (i = 0; i < bm->ldata.totlayer; i++) {
                if (CustomData_layer_has_math(&bm->ldata, i))
-                       bmesh_collapsecon_do_layer(bm, op, i);
+                       bmo_collapsecon_do_layer(bm, op, i);
        }
 }
 
-void bmesh_finddoubles_common(BMesh *bm, BMOperator *op, BMOperator *optarget, const char *targetmapname)
+static void bmesh_find_doubles_common(BMesh *bm, BMOperator *op,
+                                      BMOperator *optarget,
+                                      BMOpSlot optarget_slot_args[BMO_OP_MAX_SLOTS],
+                                      const char *targetmapname)
 {
-       BMOIter oiter;
-       BMVert *v, *v2;
-       BMVert **verts = NULL;
-       BLI_array_declare(verts);
-       float dist, dist3;
-       int i, j, len, keepvert = 0;
-
-       dist = BMO_slot_float_get(op, "dist");
-       dist3 = dist * 3.0f;
-
-       i = 0;
-       BMO_ITER(v, &oiter, bm, op, "verts", BM_VERT) {
-               BLI_array_growone(verts);
-               verts[i++] = v;
-       }
+       BMVert  **verts;
+       int       verts_len;
+
+       int i, j, keepvert = 0;
 
-       /* Test whether keepverts arg exists and is non-empty */
-       if (BMO_slot_exists(op, "keepverts")) {
-               keepvert = BMO_iter_new(&oiter, bm, op, "keepverts", BM_VERT) != NULL;
+       const float dist  = BMO_slot_float_get(op->slots_in, "dist");
+       const float dist3 = dist * 3.0f;
+
+       /* Test whether keep_verts arg exists and is non-empty */
+       if (BMO_slot_exists(op->slots_in, "keep_verts")) {
+               BMOIter oiter;
+               keepvert = BMO_iter_new(&oiter, op->slots_in, "keep_verts", BM_VERT) != NULL;
        }
 
-       /* sort by vertex coordinates added togethe */
-       qsort(verts, BLI_array_count(verts), sizeof(void *), vergaverco);
+       /* get the verts as an array we can sort */
+       verts = BMO_slot_as_arrayN(op->slots_in, "verts", &verts_len);
+
+       /* sort by vertex coordinates added together */
+       qsort(verts, verts_len, sizeof(BMVert *), vergaverco);
 
-       /* Flag keepverts */
+       /* Flag keep_verts */
        if (keepvert) {
-               BMO_slot_buffer_flag_enable(bm, op, "keepverts", VERT_KEEP, BM_VERT);
+               BMO_slot_buffer_flag_enable(bm, op->slots_in, "keep_verts", BM_VERT, VERT_KEEP);
        }
 
-       len = BLI_array_count(verts);
-       for (i = 0; i < len; i++) {
-               v = verts[i];
-               if (BMO_elem_flag_test(bm, v, VERT_DOUBLE)) continue;
-               
-               for (j = i + 1; j < len; j++) {
-                       v2 = verts[j];
+       for (i = 0; i < verts_len; i++) {
+               BMVert *v_check = verts[i];
+
+               if (BMO_elem_flag_test(bm, v_check, VERT_DOUBLE)) {
+                       continue;
+               }
+
+               for (j = i + 1; j < verts_len; j++) {
+                       BMVert *v_other = verts[j];
 
                        /* Compare sort values of the verts using 3x tolerance (allowing for the tolerance
                         * on each of the three axes). This avoids the more expensive length comparison
                         * for most vertex pairs. */
-                       if ((v2->co[0]+v2->co[1]+v2->co[2])-(v->co[0]+v->co[1]+v->co[2]) > dist3)
+                       if ((v_other->co[0] + v_other->co[1] + v_other->co[2]) -
+                           (v_check->co[0] + v_check->co[1] + v_check->co[2]) > dist3)
+                       {
                                break;
+                       }
 
                        if (keepvert) {
-                               if (BMO_elem_flag_test(bm, v2, VERT_KEEP) == BMO_elem_flag_test(bm, v, VERT_KEEP))
+                               if (BMO_elem_flag_test(bm, v_other, VERT_KEEP) == BMO_elem_flag_test(bm, v_check, VERT_KEEP))
                                        continue;
                        }
 
-                       if (compare_len_v3v3(v->co, v2->co, dist)) {
+                       if (compare_len_v3v3(v_check->co, v_other->co, dist)) {
 
                                /* If one vert is marked as keep, make sure it will be the target */
-                               if (BMO_elem_flag_test(bm, v2, VERT_KEEP)) {
-                                       SWAP(BMVert *, v, v2);
+                               if (BMO_elem_flag_test(bm, v_other, VERT_KEEP)) {
+                                       SWAP(BMVert *, v_check, v_other);
                                }
 
-                               BMO_elem_flag_enable(bm, v2, VERT_DOUBLE);
-                               BMO_elem_flag_enable(bm, v, VERT_TARGET);
+                               BMO_elem_flag_enable(bm, v_other, VERT_DOUBLE);
+                               BMO_elem_flag_enable(bm, v_check, VERT_TARGET);
 
-                               BMO_slot_map_ptr_insert(bm, optarget, targetmapname, v2, v);
+                               BMO_slot_map_ptr_insert(optarget, optarget_slot_args, targetmapname, v_other, v_check);
                        }
                }
        }
 
-       BLI_array_free(verts);
+       MEM_freeN(verts);
 }
 
-void bmesh_removedoubles_exec(BMesh *bm, BMOperator *op)
+void bmo_remove_doubles_exec(BMesh *bm, BMOperator *op)
 {
        BMOperator weldop;
 
-       BMO_op_init(bm, &weldop, "weldverts");
-       bmesh_finddoubles_common(bm, op, &weldop, "targetmap");
+       BMO_op_init(bm, &weldop, op->flag, "weld_verts");
+       bmesh_find_doubles_common(bm, op,
+                                 &weldop, weldop.slots_in, "targetmap");
        BMO_op_exec(bm, &weldop);
        BMO_op_finish(bm, &weldop);
 }
 
 
-void bmesh_finddoubles_exec(BMesh *bm, BMOperator *op)
+void bmo_find_doubles_exec(BMesh *bm, BMOperator *op)
 {
-       bmesh_finddoubles_common(bm, op, op, "targetmapout");
+       bmesh_find_doubles_common(bm, op,
+                                 op, op->slots_out, "targetmap.out");
 }
 
-void bmesh_automerge_exec(BMesh *bm, BMOperator *op)
+void bmo_automerge_exec(BMesh *bm, BMOperator *op)
 {
        BMOperator findop, weldop;
        BMIter viter;
@@ -567,8 +585,8 @@ void bmesh_automerge_exec(BMesh *bm, BMOperator *op)
        /* The "verts" input sent to this op is the set of verts that
         * can be merged away into any other verts. Mark all other verts
         * as VERT_KEEP. */
-       BMO_slot_buffer_flag_enable(bm, op, "verts", VERT_IN, BM_VERT);
-       BM_ITER(v, &viter, bm, BM_VERTS_OF_MESH, NULL) {
+       BMO_slot_buffer_flag_enable(bm, op->slots_in, "verts", BM_VERT, VERT_IN);
+       BM_ITER_MESH (v, &viter, bm, BM_VERTS_OF_MESH) {
                if (!BMO_elem_flag_test(bm, v, VERT_IN)) {
                        BMO_elem_flag_enable(bm, v, VERT_KEEP);
                }
@@ -576,13 +594,15 @@ void bmesh_automerge_exec(BMesh *bm, BMOperator *op)
 
        /* Search for doubles among all vertices, but only merge non-VERT_KEEP
         * vertices into VERT_KEEP vertices. */
-       BMO_op_initf(bm, &findop, "finddoubles verts=%av keepverts=%fv", VERT_KEEP);
-       BMO_slot_copy(op, &findop, "dist", "dist");
+       BMO_op_initf(bm, &findop, op->flag, "find_doubles verts=%av keep_verts=%fv", VERT_KEEP);
+       BMO_slot_copy(op,      slots_in, "dist",
+                     &findop, slots_in, "dist");
        BMO_op_exec(bm, &findop);
 
        /* weld the vertices */
-       BMO_op_init(bm, &weldop, "weldverts");
-       BMO_slot_copy(&findop, &weldop, "targetmapout", "targetmap");
+       BMO_op_init(bm, &weldop, op->flag, "weld_verts");
+       BMO_slot_copy(&findop, slots_out, "targetmap.out",
+                     &weldop, slots_in,  "targetmap");
        BMO_op_exec(bm, &weldop);
 
        BMO_op_finish(bm, &findop);