skip assigning vars for inline bmesh flag funcs, just cast.
[blender.git] / source / blender / bmesh / operators / connectops.c
1 #include "MEM_guardedalloc.h"
2
3 #include "BKE_utildefines.h"
4
5 #include "bmesh.h"
6 #include "mesh_intern.h"
7 #include "bmesh_private.h"
8 #include "BLI_math.h"
9 #include "BLI_array.h"
10
11 #include <stdio.h>
12 #include <string.h>
13
14 #define VERT_INPUT      1
15 #define EDGE_OUT        1
16 #define FACE_NEW        2
17
18 void connectverts_exec(BMesh *bm, BMOperator *op)
19 {
20         BMIter iter, liter;
21         BMFace *f, *nf;
22         BMLoop **loops = NULL, *lastl = NULL;
23         BLI_array_declare(loops);
24         BMLoop *l, *nl;
25         BMVert *v1, *v2, **verts = NULL;
26         BLI_array_declare(verts);
27         int i;
28         
29         BMO_Flag_Buffer(bm, op, "verts", VERT_INPUT, BM_VERT);
30
31         for (f=BMIter_New(&iter, bm, BM_FACES_OF_MESH, NULL); f; f=BMIter_Step(&iter)){
32                 BLI_array_empty(loops);
33                 BLI_array_empty(verts);
34                 
35                 if (BMO_TestFlag(bm, f, FACE_NEW)) continue;
36
37                 l = BMIter_New(&liter, bm, BM_LOOPS_OF_FACE, f);
38                 v1 = v2 = NULL;
39                 lastl = NULL;
40                 for (; l; l=BMIter_Step(&liter)) {
41                         if (BMO_TestFlag(bm, l->v, VERT_INPUT)) {
42                                 if (!lastl) {
43                                         lastl = l;
44                                         continue;
45                                 }
46
47                                 if (lastl != l->prev && lastl != 
48                                     l->next)
49                                 {
50                                         BLI_array_growone(loops);
51                                         loops[BLI_array_count(loops)-1] = lastl;
52
53                                         BLI_array_growone(loops);
54                                         loops[BLI_array_count(loops)-1] = l;
55
56                                 }
57                                 lastl = l;
58                         }
59                 }
60
61                 if (BLI_array_count(loops) == 0) continue;
62                 
63                 if (BLI_array_count(loops) > 2) {
64                         BLI_array_growone(loops);
65                         loops[BLI_array_count(loops)-1] = loops[BLI_array_count(loops)-2];
66
67                         BLI_array_growone(loops);
68                         loops[BLI_array_count(loops)-1] = loops[0];
69                 }
70
71                 BM_LegalSplits(bm, f, (BMLoop *(*)[2])loops, BLI_array_count(loops)/2);
72                 
73                 for (i=0; i<BLI_array_count(loops)/2; i++) {
74                         if (loops[i*2]==NULL) continue;
75
76                         BLI_array_growone(verts);
77                         verts[BLI_array_count(verts)-1] = loops[i*2]->v;
78                 
79                         BLI_array_growone(verts);
80                         verts[BLI_array_count(verts)-1] = loops[i*2+1]->v;
81                 }
82
83                 for (i=0; i<BLI_array_count(verts)/2; i++) {
84                         nf = BM_Split_Face(bm, f, verts[i*2],
85                                            verts[i*2+1], &nl, NULL);
86                         f = nf;
87                         
88                         if (!nl || !nf) {
89                                 BMO_RaiseError(bm, op,
90                                         BMERR_CONNECTVERT_FAILED, NULL);
91                                 BLI_array_free(loops);
92                                 return;;;
93                         }
94                         BMO_SetFlag(bm, nf, FACE_NEW);
95                         BMO_SetFlag(bm, nl->e, EDGE_OUT);
96                 }
97         }
98
99         BMO_Flag_To_Slot(bm, op, "edgeout", EDGE_OUT, BM_EDGE);
100
101         BLI_array_free(loops);
102         BLI_array_free(verts);
103 }