d02bb14bebfa0fc202c97d2ca086ee51dbe6c00b
[blender.git] / source / blender / bmesh / operators / bmo_create.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * Contributor(s): Joseph Eagar.
19  *
20  * ***** END GPL LICENSE BLOCK *****
21  */
22
23 /** \file blender/bmesh/operators/bmo_create.c
24  *  \ingroup bmesh
25  */
26
27 #include "MEM_guardedalloc.h"
28
29 #include "BLI_heap.h"
30 #include "BLI_listbase.h"
31 #include "BLI_math.h"
32 #include "BLI_array.h"
33 #include "BLI_smallhash.h"
34 #include "BLI_rand.h"
35
36 #include "bmesh.h"
37
38 #include "intern/bmesh_operators_private.h" /* own include */
39
40 #define EDGE_MARK       1
41 #define EDGE_VIS        2
42
43 #define FACE_NEW        1
44
45 #define ELE_NEW         1
46 #define ELE_OUT         2
47 #define ELE_ORIG        4
48
49 #define FACE_IGNORE     16
50
51 typedef struct EPathNode {
52         struct EPathNode *next, *prev;
53         BMVert *v;
54         BMEdge *e;
55         BMEdge *cure;
56 } EPathNode;
57
58 typedef struct EPath {
59         ListBase nodes;
60         float weight;
61         int group;
62 } EPath;
63
64 typedef struct PathBase {
65         BLI_mempool *nodepool, *pathpool;
66 } PathBase;
67
68 typedef struct EdgeData {
69         int tag;
70         int ftag;
71         BMDiskLink v1_disk_link, v2_disk_link;
72 } EdgeData;
73
74 typedef struct VertData {
75         BMEdge *e;
76         float no[3], offco[3], sco[3]; /* offco is vertex coordinate slightly offset randomly */
77         int tag;
78 } VertData;
79
80 static int count_edge_faces(BMesh *bm, BMEdge *e);
81
82 /****  rotation system code * */
83
84 BLI_INLINE BMDiskLink *rs_edge_link_get(BMEdge *e, BMVert *v, EdgeData *e_data)
85 {
86         return  v == ((BMEdge *)e)->v1 ? &(((EdgeData *)e_data)->v1_disk_link) :
87                                          &(((EdgeData *)e_data)->v2_disk_link) ;
88 }
89
90 static int rotsys_append_edge(BMEdge *e, BMVert *v,
91                               EdgeData *edata, VertData *vdata)
92 {
93         EdgeData *ed = &edata[BM_elem_index_get(e)];
94         VertData *vd = &vdata[BM_elem_index_get(v)];
95         
96         if (!vd->e) {
97                 Link *e1 = (Link *)rs_edge_link_get(e, v, ed);
98
99                 vd->e = e;
100                 e1->next = e1->prev = (Link *)e;
101         }
102         else {
103                 BMDiskLink *dl1, *dl2, *dl3;
104                 EdgeData *ved = &edata[BM_elem_index_get(vd->e)];
105
106                 dl1 = rs_edge_link_get(e, v, ed);
107                 dl2 = rs_edge_link_get(vd->e, v, ved);
108                 dl3 = dl2->prev ? rs_edge_link_get(dl2->prev, v, &edata[BM_elem_index_get(dl2->prev)]) : NULL;
109
110                 dl1->next = vd->e;
111                 dl1->prev = dl2->prev;
112
113                 dl2->prev = e;
114                 if (dl3) {
115                         dl3->next = e;
116                 }
117         }
118
119         return TRUE;
120 }
121
122 static void UNUSED_FUNCTION(rotsys_remove_edge)(BMEdge *e, BMVert *v,
123                                                 EdgeData *edata, VertData *vdata)
124 {
125         EdgeData *ed = edata + BM_elem_index_get(e);
126         VertData *vd = vdata + BM_elem_index_get(v);
127         BMDiskLink *e1, *e2;
128
129         e1 = rs_edge_link_get(e, v, ed);
130         if (e1->prev) {
131                 e2 = rs_edge_link_get(e1->prev, v, ed);
132                 e2->next = e1->next;
133         }
134
135         if (e1->next) {
136                 e2 = rs_edge_link_get(e1->next, v, ed);
137                 e2->prev = e1->prev;
138         }
139
140         if (vd->e == e)
141                 vd->e = (e != e1->next) ? e1->next : NULL;
142
143         e1->next = e1->prev = NULL;
144 }
145
146 static BMEdge *rotsys_nextedge(BMEdge *e, BMVert *v,
147                                EdgeData *edata, VertData *UNUSED(vdata))
148 {
149         if (v == e->v1)
150                 return edata[BM_elem_index_get(e)].v1_disk_link.next;
151         if (v == e->v2)
152                 return edata[BM_elem_index_get(e)].v2_disk_link.next;
153         return NULL;
154 }
155
156 static BMEdge *rotsys_prevedge(BMEdge *e, BMVert *v,
157                                EdgeData *edata, VertData *UNUSED(vdata))
158 {
159         if (v == e->v1)
160                 return edata[BM_elem_index_get(e)].v1_disk_link.prev;
161         if (v == e->v2)
162                 return edata[BM_elem_index_get(e)].v2_disk_link.prev;
163         return NULL;
164 }
165
166 static void rotsys_reverse(BMEdge *UNUSED(e), BMVert *v, EdgeData *edata, VertData *vdata)
167 {
168         BMEdge **edges = NULL;
169         BMEdge *e_first;
170         BMEdge *e;
171         BLI_array_staticdeclare(edges, BM_NGON_STACK_SIZE);
172         int i, totedge;
173         
174         e = e_first = vdata[BM_elem_index_get(v)].e;
175         do {
176                 BLI_array_append(edges, e);
177                 e = rotsys_nextedge(e, v, edata, vdata);
178         } while (e != e_first);
179         
180         totedge = BLI_array_count(edges);
181         for (i = 0; i < totedge / 2; i++) {
182                 SWAP(BMEdge *, edges[i], edges[totedge - 1 - i]);
183         }
184         
185         vdata[BM_elem_index_get(v)].e = NULL;
186         for (i = 0; i < totedge; i++) {
187                 rotsys_append_edge(edges[i], v, edata, vdata);
188         }
189         
190         BLI_array_free(edges);
191 }
192
193 static int UNUSED_FUNCTION(rotsys_count)(BMVert *v, EdgeData *edata, VertData *vdata)
194 {
195         BMEdge *e = vdata[BM_elem_index_get(v)].e;
196         int i = 0;
197
198         if (!e)
199                 return 0;
200
201         do {
202                 if (!e)
203                         return 0;
204                 e =  rotsys_nextedge(e, v, edata, vdata);
205
206                 if (i >= (1 << 20)) {
207                         printf("bmesh error: infinite loop in disk cycle!\n");
208                         return 0;
209                 }
210
211                 i += 1;
212         } while (e != vdata[BM_elem_index_get(v)].e);
213
214         return i;
215 }
216
217 static int UNUSED_FUNCTION(rotsys_fill_faces)(BMesh *bm, EdgeData *edata, VertData *vdata)
218 {
219         BMIter iter;
220         BMEdge *e, **edges = NULL;
221         BLI_array_declare(edges);
222         BMVert *v, **verts = NULL;
223         BMFace *f;
224         BLI_array_declare(verts);
225         SmallHash visithash, *hash = &visithash;
226         int i;
227         
228         BM_ITER_MESH (e, &iter, bm, BM_EDGES_OF_MESH) {
229                 BMEdge *e2, *starte;
230                 BMVert *startv;
231                 int rad, ok;
232                 
233                 rad = count_edge_faces(bm, e);
234                 
235                 if (rad < 2) {
236                         starte = e;
237                 }
238                 else {
239                         continue;
240                 }
241
242                 /* do two passes, going forward then backward */
243                 for (i = 0; i < 2; i++) {
244                         BLI_smallhash_init(hash);
245                         
246                         BLI_array_empty(verts);
247                         BLI_array_empty(edges);
248
249                         startv = v = starte->v1;
250                         e2 = starte;
251                         ok = 1;
252                         if (!v || !e2)
253                                 continue;
254
255                         do {
256                                 if (BLI_smallhash_haskey(hash, (intptr_t)e2) ||
257                                     BLI_smallhash_haskey(hash, (intptr_t)v))
258                                 {
259                                         ok = 0;
260                                         break;
261                                 }
262                                 
263                                 BLI_array_append(verts, v);
264                                 BLI_array_append(edges, e2);
265                                 
266                                 BLI_smallhash_insert(hash, (intptr_t)e2, NULL);
267
268                                 v = BM_edge_other_vert(e2, v);
269                                 e2 = i ? rotsys_prevedge(e2, v, edata, vdata) : rotsys_nextedge(e2, v, edata, vdata);
270                         } while (e2 != starte && v != startv);
271                         
272                         BLI_smallhash_release(hash);
273                         
274                         if (!ok || BLI_array_count(edges) < 3)
275                                 continue;
276                         
277                         f = BM_face_create_ngon(bm, verts[0], verts[1], edges, BLI_array_count(edges), TRUE);
278                         if (!f)
279                                 continue;
280                 }
281         }
282         
283         return 0;
284 }
285
286 static void rotsys_make_consistent(BMesh *bm, EdgeData *edata, VertData *vdata)
287 {
288         BMIter iter;
289         BMEdge *e;
290         BMVert *v, **stack = NULL;
291         BLI_array_declare(stack);
292         int i;
293         
294         for (i = 0; i < bm->totvert; i++) {
295                 vdata[i].tag = 0;
296         }
297         
298         while (1) {
299                 VertData *vd;
300                 BMVert *startv = NULL;
301                 float dis;
302                 
303                 v = BM_iter_new(&iter, bm, BM_VERTS_OF_MESH, NULL);
304                 for (i = 0; i < bm->totvert; i++, BM_iter_step(&iter)) {
305                         vd = vdata + BM_elem_index_get(v);
306                         
307                         if (vd->tag)
308                                 continue;
309                         
310                         if (!startv || dot_v3v3(vd->offco, vd->offco) > dis) {
311                                 dis = dot_v3v3(vd->offco, vd->offco);
312                                 startv = v;
313                         }
314                 }
315                 
316                 if (!startv)
317                         break;
318                 
319                 vd = vdata + BM_elem_index_get(startv);
320                 
321                 BLI_array_empty(stack);
322                 BLI_array_append(stack, startv);
323                 
324                 vd->tag = 1;
325                 
326                 while (BLI_array_count(stack)) {
327                         v = BLI_array_pop(stack);
328                         vd = vdata + BM_elem_index_get(v);
329                         
330                         if (!vd->e)
331                                 continue;
332                         
333                         e = vd->e;
334                         do {
335                                 BMVert *v2 = BM_edge_other_vert(e, v);
336                                 VertData *vd2 = vdata + BM_elem_index_get(v2);
337                                 
338                                 if (dot_v3v3(vd->no, vd2->no) < 0.0f + FLT_EPSILON * 2) {
339                                         rotsys_reverse(e, v2, edata, vdata);
340                                         mul_v3_fl(vd2->no, -1.0f);
341                                 }
342
343                                 if (!vd2->tag) {
344                                         BLI_array_append(stack, v2);
345                                         vd2->tag = 1;
346                                 }
347                                 
348                                 e = rotsys_nextedge(e, v, edata, vdata);
349                         } while (e != vd->e);
350                 }
351         }
352
353         BLI_array_free(stack);
354 }
355
356 static void init_rotsys(BMesh *bm, EdgeData *edata, VertData *vdata)
357 {
358         BMIter iter;
359         BMEdge *e;
360         BMEdge **edges = NULL;
361         BLI_array_staticdeclare(edges, BM_NGON_STACK_SIZE);
362         BMVert *v;
363         /* BMVert **verts = NULL; */
364         /* BLI_array_staticdeclare(verts, BM_NGON_STACK_SIZE); */ /* UNUSE */
365         int i;
366         
367 #define SIGN(n) ((n)<0.0f)
368         
369         BM_ITER_MESH (v, &iter, bm, BM_VERTS_OF_MESH) {
370                 BMIter eiter;
371                 float no[3], cent[3];
372                 int j, k = 0, totedge = 0;
373                 
374                 if (BM_elem_index_get(v) == -1)
375                         continue;
376                 
377                 BLI_array_empty(edges);
378                 
379                 BM_ITER_ELEM (e, &eiter, v, BM_EDGES_OF_VERT) {
380                         if (BMO_elem_flag_test(bm, e, EDGE_MARK)) {
381                                 BLI_array_append(edges, e);
382                                 totedge++;
383                         }
384                 }
385                 
386                 copy_v3_v3(cent, v->co);
387                 
388                 zero_v3(no);
389                 for (i = 0; i < totedge; i++) {
390                         BMEdge *e1, *e2;
391                         float cno[3], vec1[3], vec2[3];
392                         
393                         e1 = edges[i];
394                         e2 = edges[(i + 1) % totedge];
395
396                         sub_v3_v3v3(vec1, (BM_edge_other_vert(e1, v))->co, v->co);
397                         sub_v3_v3v3(vec2, (BM_edge_other_vert(e2, v))->co, v->co);
398                         
399                         cross_v3_v3v3(cno, vec1, vec2);
400                         normalize_v3(cno);
401                         
402                         if (i && dot_v3v3(cno, no) < 0.0f + FLT_EPSILON * 10)
403                                 mul_v3_fl(cno, -1.0f);
404                         
405                         add_v3_v3(no, cno);
406                         normalize_v3(no);
407                 }
408                 
409                 /* generate plane-flattened coordinates */
410                 for (i = 0; i < totedge; i++) {
411                         BMEdge *e1;
412                         BMVert *v2;
413                         float cvec[3], vec1[3];
414                         
415                         e1 = edges[i];
416                         v2 = BM_edge_other_vert(e1, v);
417                         
418                         sub_v3_v3v3(vec1, v2->co, v->co);
419                         
420                         cross_v3_v3v3(cvec, vec1, no);
421                         cross_v3_v3v3(vec1, cvec, no);
422                         normalize_v3(vec1);
423                         
424                         mul_v3_fl(vec1, len_v3v3(v2->co, v->co));
425                         add_v3_v3(vec1, v->co);
426                         
427                         copy_v3_v3(vdata[BM_elem_index_get(v2)].sco, vec1);
428                 }
429                 
430                 BLI_srandom(0);
431                 
432                 /* first, ensure no 0 or 180 angles between adjacent
433                  * (and that adjacent's adjacent) edges */
434                 for (i = 0, k = 0; i < totedge; i++) {
435                         BMEdge *e1, *e2, *e3 = NULL;
436                         BMVert *v1, *v2, *v3;
437                         VertData *vd1, *vd2, *vd3;
438                         float vec1[3], vec2[3], vec3[3], size;
439                         int s1, s2, s3;
440                         
441                         if (totedge < 3)
442                                 continue;
443                         
444                         e1 = edges[(i + totedge - 1) % totedge];
445                         e2 = edges[i];
446                         e3 = edges[(i + 1) % totedge];
447                         
448                         v1 = BM_edge_other_vert(e1, v);
449                         v2 = BM_edge_other_vert(e2, v);
450                         v3 = BM_edge_other_vert(e3, v);
451
452                         vd1 = vdata + BM_elem_index_get(v1);
453                         vd2 = vdata + BM_elem_index_get(v2);
454                         vd3 = vdata + BM_elem_index_get(v3);
455                         
456                         sub_v3_v3v3(vec1, vd1->sco, cent);
457                         sub_v3_v3v3(vec2, vd2->sco, cent);
458                         sub_v3_v3v3(vec3, vd3->sco, cent);
459                         
460                         size = (len_v3(vec1) + len_v3(vec3)) * 0.01f;
461                         normalize_v3(vec1); normalize_v3(vec2); normalize_v3(vec3);
462                         
463 #ifdef STRAIGHT
464 #undef STRAIGHT
465 #endif
466 #define STRAIGHT(vec11, vec22) (fabsf(dot_v3v3((vec11), (vec22))) > 1.0f - ((float)FLT_EPSILON * 1000.0f))
467                         
468                         s1 = STRAIGHT(vec1, vec2); s2 = STRAIGHT(vec2, vec3); s3 = STRAIGHT(vec1, vec3);
469                         
470                         if (s1 || s2 || s3) {
471                                 copy_v3_v3(cent, v->co);
472
473                                 for (j = 0; j < 3; j++) {
474                                         float fac = (BLI_frand() - 0.5f) * size;
475                                         cent[j] += fac;
476                                 }
477                                 
478                                 if (k < 2000) {
479                                         i = 0;
480                                         k++;
481                                         continue;
482                                 }
483                                 else {
484                                         k++;
485                                         continue;
486                                 }
487
488                         }
489                 }
490                 
491                 copy_v3_v3(vdata[BM_elem_index_get(v)].offco, cent);
492                 //copy_v3_v3(v->co, cent);
493                 
494                 /* now, sort edges so the triangle fan of all edges
495                  * has a consistent normal.  this is the same as
496                  * sorting by polar coordinates along a group normal */
497                 for (j = 0; j < totedge; j++) {
498                         for (i = 0; i < totedge; i++) {
499                                 BMEdge *e1, *e2, *e3 = NULL;
500                                 BMVert *v1, *v2, *v3;
501                                 VertData *vd1, *vd2, *vd3;
502                                 float vec1[3], vec2[3], vec3[3], n1[3], n2[3], n3[3];
503                                 
504                                 e1 = edges[(i + totedge - 1) % totedge];
505                                 e2 = edges[i];
506                                 e3 = edges[(i + 1) % totedge];
507                                 
508                                 v1 = BM_edge_other_vert(e1, v);
509                                 v2 = BM_edge_other_vert(e2, v);
510                                 v3 = BM_edge_other_vert(e3, v);
511
512                                 vd1 = vdata + BM_elem_index_get(v1);
513                                 vd2 = vdata + BM_elem_index_get(v2);
514                                 vd3 = vdata + BM_elem_index_get(v3);
515
516                                 sub_v3_v3v3(vec1, vd1->sco, cent);
517                                 sub_v3_v3v3(vec2, vd2->sco, cent);
518                                 sub_v3_v3v3(vec3, vd3->sco, cent);
519                                 
520                                 cross_v3_v3v3(n1, vec1, vec2);
521                                 cross_v3_v3v3(n2, vec2, vec3);
522                                 cross_v3_v3v3(n3, vec1, vec3);
523
524                                 /* this case happens often enough and probably not worth bothering users with,
525                                  * maybe enable for debugging code but not for everyday use - campbell */
526 #if 0
527                                 /* Other way to determine if two vectors approach are (nearly) parallel: the
528                                  * cross product of the two vectors will approach zero */
529                                 {
530                                         int s1, s2, s3;
531                                         s1 = (dot_v3v3(n1, n1) < (0.0f + FLT_EPSILON * 10));
532                                         s2 = (dot_v3v3(n2, n2) < (0.0f + FLT_EPSILON * 10));
533                                         s3 = (totedge < 3) ? 0 : (dot_v3v3(n3, n3) < (0.0f + FLT_EPSILON * 10));
534
535                                         if (s1 || s2 || s3) {
536                                                 fprintf(stderr, "%s: s1: %d, s2: %d, s3: %dx (bmesh internal error)\n", __func__, s1, s2, s3);
537                                         }
538                                 }
539 #endif
540
541                                 normalize_v3(n1); normalize_v3(n2); normalize_v3(n3);
542
543
544                                 if (dot_v3v3(n1, n2) < 0.0f) {
545                                         if (dot_v3v3(n1, n3) >= 0.0f + FLT_EPSILON * 10) {
546                                                 SWAP(BMEdge *, edges[i], edges[(i + 1) % totedge]);
547                                         }
548                                         else {
549                                                 SWAP(BMEdge *, edges[(i + totedge - 1) % totedge], edges[(i + 1) % totedge]);
550                                                 SWAP(BMEdge *, edges[i], edges[(i + 1) % totedge]);
551                                         }
552                                 }
553                         }
554                 }
555                 
556 #undef STRAIGHT
557
558                 zero_v3(no);
559
560                 /* yay, edges are sorted */
561                 for (i = 0; i < totedge; i++) {
562                         BMEdge *e1 = edges[i], *e2 = edges[(i + 1) % totedge];
563                         float eno[3];
564                         
565                         normal_tri_v3(eno, BM_edge_other_vert(e1, v)->co, v->co, BM_edge_other_vert(e2, v)->co);
566                         add_v3_v3(no, eno);
567                         
568                         rotsys_append_edge(edges[i], v, edata, vdata);
569                 }
570                 
571                 normalize_v3(no);
572                 copy_v3_v3(vdata[BM_elem_index_get(v)].no, no);
573         }
574         
575         /* now, make sure rotation system is topologically consistent
576          * (e.g. vert normals consistently point either inside or outside) */
577         rotsys_make_consistent(bm, edata, vdata);
578
579         //rotsys_fill_faces(bm, edata, vdata);
580
581 #if 0
582         /* create visualizing geometr */
583         BMVert *lastv;
584         BM_ITER_MESH (v, &iter, bm, BM_VERTS_OF_MESH) {
585                 BMVert *v2;
586                 BMFace *f;
587                 int totedge = BM_vert_edge_count(v);
588
589                 if (BM_elem_index_get(v) == -1)
590                         continue;
591                 
592                 //cv = BM_vert_create(bm, cent, v);
593                 //BM_elem_index_set(cv, -1);  /* set_dirty! */
594                 i = 0;
595                 e = vdata[BM_elem_index_get(v)].e;
596                 lastv = NULL;
597                 do {
598                         BMEdge *e2;
599                         BMVert *v2;
600                         float f = ((float)i / (float)totedge) * 0.35 + 0.05;
601                         float co[3];
602                         
603                         if (!e)
604                                 break;
605
606                         if (!BM_edge_other_vert(e, v))
607                                 continue;
608                         
609                         sub_v3_v3v3(co, (BM_edge_other_vert(e, v))->co, vdata[BM_elem_index_get(v)].offco);
610                         mul_v3_fl(co, f);
611                         add_v3_v3(co, vdata[BM_elem_index_get(v)].offco);
612                         
613                         v2 = BM_vert_create(bm, co, NULL);
614                         BM_elem_index_set(v2, -1); /* set_dirty! */
615                         //BM_edge_create(bm, cv, v2, NULL, FALSE);
616                         
617                         BM_elem_select_set(bm, v2, TRUE);
618                         if (lastv) {
619                                 e2 = BM_edge_create(bm, lastv, v2, NULL, FALSE);
620                                 BM_elem_select_set(bm, e2, TRUE);
621                         }
622                         
623                         lastv = v2;
624                         
625                         e = rotsys_nextedge(e, v, edata, vdata);
626                         i++;
627                 } while (e != vdata[BM_elem_index_get(v)].e);
628         }
629 #endif
630
631         BLI_array_free(edges);
632 }
633
634 static PathBase *edge_pathbase_new(void)
635 {
636         PathBase *pb = MEM_callocN(sizeof(PathBase), "PathBase");
637
638         pb->nodepool = BLI_mempool_create(sizeof(EPathNode), 1, 512, BLI_MEMPOOL_SYSMALLOC);
639         pb->pathpool = BLI_mempool_create(sizeof(EPath), 1, 512, BLI_MEMPOOL_SYSMALLOC);
640
641         return pb;
642 }
643
644 static void edge_pathbase_free(PathBase *pathbase)
645 {
646         BLI_mempool_destroy(pathbase->nodepool);
647         BLI_mempool_destroy(pathbase->pathpool);
648         MEM_freeN(pathbase);
649 }
650
651 static EPath *edge_copy_add_path(PathBase *pb, EPath *path, BMVert *appendv, BMEdge *e)
652 {
653         EPath *path2;
654         EPathNode *node, *node2;
655
656         path2 = BLI_mempool_alloc(pb->pathpool);
657         path2->nodes.first = path2->nodes.last = NULL;
658         path2->weight = 0.0f;
659         path2->group = path->group;
660         
661         for (node = path->nodes.first; node; node = node->next) {
662                 node2 = BLI_mempool_alloc(pb->nodepool);
663                 *node2 = *node;
664                 BLI_addtail(&path2->nodes, node2);
665         }
666
667         node2 = BLI_mempool_alloc(pb->nodepool);
668         node2->v = appendv;
669         node2->e = e;
670         node2->cure = NULL;
671
672         BLI_addtail(&path2->nodes, node2);
673
674         return path2;
675 }
676
677 static EPath *edge_path_new(PathBase *pb, BMVert *start, BMEdge *starte)
678 {
679         EPath *path;
680         EPathNode *node;
681
682         path = BLI_mempool_alloc(pb->pathpool);
683         node = BLI_mempool_alloc(pb->nodepool);
684         
685         path->nodes.first = path->nodes.last = NULL;
686         
687         node->v = start;
688         node->e = starte;
689         node->cure = NULL;
690
691         BLI_addtail(&path->nodes, node);
692         path->weight = 0.0f;
693
694         return path;
695 }
696
697 static float edge_weight_path(EPath *path, EdgeData *edata, VertData *UNUSED(vdata))
698 {
699         EPathNode *node, *first = path->nodes.first;
700         float w = 0.0;
701
702         for (node = path->nodes.first; node; node = node->next) {
703                 if (node->e && node != path->nodes.first) {
704                         w += edata[BM_elem_index_get(node->e)].ftag;
705                         if (node->prev) {
706                                 /* BMESH_TOD */
707                                 (void)first;
708                                 //w += len_v3v3(node->v->co, first->e->v1->co) * 0.0001f;
709                                 //w += len_v3v3(node->v->co, first->e->v2->co) * 0.0001f;
710                         }
711                 }
712
713                 w += 1.0f;
714         }
715
716         return w;
717 }
718
719
720 static void edge_free_path(PathBase *pathbase, EPath *path)
721 {
722         EPathNode *node, *next;
723
724         for (node = path->nodes.first; node; node = next) {
725                 next = node->next;
726                 BLI_mempool_free(pathbase->nodepool, node);
727         }
728
729         BLI_mempool_free(pathbase->pathpool, path);
730 }
731
732 static EPath *edge_find_shortest_path(BMesh *bm, BMOperator *op, BMEdge *edge, EdgeData *edata,
733                                       VertData *vdata, PathBase *pathbase, int group)
734 {
735         BMEdge *e;
736         GHash *gh = BLI_ghash_new(BLI_ghashutil_ptrhash, BLI_ghashutil_ptrcmp, "createops find shortest path");
737         BMVert *v1, *v2;
738         BMVert **verts = NULL;
739         BLI_array_staticdeclare(verts, 1024);
740         Heap *heap = BLI_heap_new();
741         EPath *path = NULL, *path2;
742         BMVert *startv;
743         BMVert *endv;
744         EPathNode *node;
745         int i, use_restrict = BMO_slot_bool_get(op, "use_restrict");
746
747         startv = edata[BM_elem_index_get(edge)].ftag ? edge->v2 : edge->v1;
748         endv = edata[BM_elem_index_get(edge)].ftag ? edge->v1 : edge->v2;
749         
750         path = edge_path_new(pathbase, startv, edge);
751         BLI_ghash_insert(gh, startv, NULL);
752         BLI_heap_insert(heap, path->weight, path);
753         path->group = group;
754
755         while (BLI_heap_size(heap)) {
756                 VertData *vd;
757                 EPathNode *last;
758                 BMFace *f = NULL;
759                 
760                 path = BLI_heap_popmin(heap);
761                 last = path->nodes.last;
762                 v1 = last->v;
763                 
764                 if (v1 == endv) {
765                         /* make sure this path loop doesn't already exists */
766                         i = 0;
767                         BLI_array_empty(verts);
768                         for (i = 0, node = path->nodes.first; node; node = node->next, i++) {
769                                 BLI_array_growone(verts);
770                                 verts[i] = node->v;
771                         }
772
773                         if (BM_face_exists(bm, verts, i, &f)) {
774                                 if (!BMO_elem_flag_test(bm, f, FACE_IGNORE)) {
775                                         BLI_ghash_remove(gh, endv, NULL, NULL);
776                                         continue;
777                                 }
778                         }
779                         break;
780                 }
781                 
782                 vd = vdata + BM_elem_index_get(v1);
783                 if (!vd->e)
784                         continue;
785                 
786                 v2 = NULL;
787                 while (1) {
788                         if (!last->cure) {
789                                 last->cure = e = vdata[BM_elem_index_get(last->v)].e;
790                         }
791                         else {
792                                 last->cure = e = rotsys_nextedge(last->cure, last->v, edata, vdata);
793                                 if (last->cure == vdata[BM_elem_index_get(last->v)].e) {
794                                         v2 = NULL;
795                                         break;
796                                 }
797                         }
798                         
799                         if (e == edge || !BMO_elem_flag_test(bm, e, EDGE_MARK)) {
800                                 continue;
801                         }
802
803                         v2 = BM_edge_other_vert(e, last->v);
804                         
805                         if (BLI_ghash_haskey(gh, v2)) {
806                                 v2 = NULL;
807                                 continue;
808                         }
809                         
810                         if (use_restrict && BMO_slot_map_contains(bm, op, "restrict", e)) {
811                                 int group = BMO_slot_map_int_get(bm, op, "restrict", e);
812                                 
813                                 if (!(group & path->group)) {
814                                         v2 = NULL;
815                                         continue;
816                                 }
817                         }
818
819                         break;
820                 }
821                 
822                 if (!v2) {
823                         if (path) {
824                                 edge_free_path(pathbase, path);
825                                 path = NULL;
826                         }
827                         continue;
828                 }
829                 
830                 /* add path back into heap */
831                 BLI_heap_insert(heap, path->weight, path);
832                 
833                 /* put v2 in gh ma */
834                 BLI_ghash_insert(gh, v2, NULL);
835
836                 path2 = edge_copy_add_path(pathbase, path, v2, e);
837                 path2->weight = edge_weight_path(path2, edata, vdata);
838
839                 BLI_heap_insert(heap, path2->weight, path2);
840         }
841         
842         if (path && ((EPathNode *)path->nodes.last)->v != endv) {
843                 edge_free_path(pathbase, path);
844                 path = NULL;
845         }
846
847         BLI_array_free(verts);
848         BLI_heap_free(heap, NULL);
849         BLI_ghash_free(gh, NULL, NULL);
850
851         return path;
852 }
853
854 static int count_edge_faces(BMesh *bm, BMEdge *e)
855 {
856         int i = 0;
857         BMLoop *l = e->l;
858         
859         if (!l) {
860                 return 0;
861         }
862
863         do {
864                 if (!BMO_elem_flag_test(bm, l->f, FACE_IGNORE)) {
865                         i++;
866                 }
867
868                 l = l->radial_next;
869         } while (l != e->l);
870
871         return i;
872 }
873
874 BLI_INLINE void vote_on_winding(BMEdge *edge, EPathNode *node, unsigned int winding[2])
875 {
876         BMVert *test_v1, *test_v2;
877         /* we want to use the reverse winding to the existing order */
878         BM_edge_ordered_verts(edge, &test_v2, &test_v1);
879
880         /* edges vote on which winding wins out */
881         winding[(test_v1 == node->v)]++;
882 }
883
884 void bmo_edgenet_fill_exec(BMesh *bm, BMOperator *op)
885 {
886         BMIter iter;
887         BMOIter siter;
888         BMFace *f;
889         BMEdge *e, *edge;
890         BMVert **verts = NULL;
891         BLI_array_declare(verts);
892         EPath *path;
893         EPathNode *node;
894         EdgeData *edata;
895         VertData *vdata;
896         BMEdge **edges = NULL;
897         PathBase *pathbase;
898         BLI_array_declare(edges);
899         int use_restrict   = BMO_slot_bool_get(op, "use_restrict");
900         int use_fill_check = BMO_slot_bool_get(op, "use_fill_check");
901         const short mat_nr = BMO_slot_int_get(op, "mat_nr");
902         int i, j, group = 0;
903         unsigned int winding[2]; /* accumulte winding directions for each edge which has a face */
904
905         if (!bm->totvert || !bm->totedge)
906                 return;
907
908         pathbase = edge_pathbase_new();
909
910         edata = MEM_callocN(sizeof(EdgeData) * bm->totedge, "EdgeData");
911         vdata = MEM_callocN(sizeof(VertData) * bm->totvert, "VertData");
912         
913         BMO_slot_buffer_flag_enable(bm, op, "edges", BM_EDGE, EDGE_MARK);
914         BMO_slot_buffer_flag_enable(bm, op, "excludefaces", BM_FACE, FACE_IGNORE);
915         
916         BM_mesh_elem_index_ensure(bm, BM_VERT);
917
918         BM_ITER_MESH (f, &iter, bm, BM_FACES_OF_MESH) {
919                 BMO_elem_flag_enable(bm, f, ELE_ORIG);
920         }
921
922         i = 0;
923         BM_ITER_MESH (e, &iter, bm, BM_EDGES_OF_MESH) {
924                 BM_elem_index_set(e, i); /* set_inline */
925                 
926                 if (!BMO_elem_flag_test(bm, e, EDGE_MARK)) {
927                         edata[i].tag = 2;
928                 }
929
930                 i++;
931         }
932         bm->elem_index_dirty &= ~BM_EDGE;
933
934         init_rotsys(bm, edata, vdata);
935         
936         while (1) {
937                 edge = NULL;
938                 group = 0;
939                 
940                 BMO_ITER (e, &siter, bm, op, "edges", BM_EDGE) {
941                         /* if restrict is on, only start on faces in the restrict map */
942                         if (use_restrict && !BMO_slot_map_contains(bm, op, "restrict", e))
943                                 continue;
944
945                         if (edata[BM_elem_index_get(e)].tag < 2) {
946                                 edge = e;
947
948                                 if (use_restrict) {
949                                         int i = 0, j = 0, gi = 0;
950                                         
951                                         group = BMO_slot_map_int_get(bm, op, "restrict", e);
952                                         
953                                         for (i = 0; i < 30; i++) {
954                                                 if (group & (1 << i)) {
955                                                         j++;
956                                                         gi = i;
957
958                                                         if (j - 1 == edata[BM_elem_index_get(e)].tag) {
959                                                                 break;
960                                                         }
961                                                 }
962                                         }
963
964                                         group = (1 << gi);
965                                 }
966                                 
967                                 break;
968                         }
969                 }
970
971                 if (!edge)
972                         break;
973
974                 edata[BM_elem_index_get(edge)].tag += 1;
975
976                 path = edge_find_shortest_path(bm, op, edge, edata, vdata, pathbase, group);
977                 if (!path)
978                         continue;
979                 
980                 winding[0] = winding[1] = 0;
981
982                 BLI_array_empty(edges);
983                 BLI_array_empty(verts);
984                 i = 0;
985                 for (node = path->nodes.first; node; node = node->next) {
986                         if (!node->next)
987                                 continue;
988
989                         e = BM_edge_exists(node->v, node->next->v);
990                         
991                         /* this should never happe */
992                         if (!e)
993                                 break;
994                         
995                         /* check on the winding */
996                         if (e->l) {
997                                 vote_on_winding(e, node, winding);
998                         }
999
1000                         edata[BM_elem_index_get(e)].ftag++;
1001                         BLI_array_growone(edges);
1002                         edges[i++] = e;
1003
1004                         BLI_array_append(verts, node->v);
1005                 }
1006                 
1007                 if (edge->l) {
1008                         vote_on_winding(edge, path->nodes.last, winding);
1009                 }
1010
1011                 BLI_array_growone(edges);
1012                 edges[i++] = edge;
1013                 edata[BM_elem_index_get(edge)].ftag++;
1014                 
1015                 for (j = 0; j < i; j++) {
1016                         if (count_edge_faces(bm, edges[j]) >= 2) {
1017                                 edge_free_path(pathbase, path);
1018                                 break;
1019                         }
1020                 }
1021
1022                 if (j != i) {
1023                         continue;
1024                 }
1025
1026                 if (i) {
1027                         BMVert *v1, *v2;
1028
1029                         /* to define the winding order must select first edge,
1030                          * otherwise we could leave this as-is */
1031                         edge = edges[0];
1032
1033                         /* if these are even it doesn't really matter what to do,
1034                          * with consistent geometry one will be zero, the choice is clear */
1035                         if (winding[0] < winding[1]) {
1036                                 v1 = verts[0];
1037                                 v2 = verts[1];
1038                         }
1039                         else {
1040                                 v1 = verts[1];
1041                                 v2 = verts[0];
1042                         }
1043
1044                         if ((use_fill_check == FALSE) ||
1045                             /* fairly expensive check - see if there are already faces filling this area */
1046                             (BM_face_exists_multi_edge(edges, i) == FALSE))
1047                         {
1048                                 f = BM_face_create_ngon(bm, v1, v2, edges, i, TRUE);
1049                                 if (f && !BMO_elem_flag_test(bm, f, ELE_ORIG)) {
1050                                         BMO_elem_flag_enable(bm, f, FACE_NEW);
1051                                         f->mat_nr = mat_nr;
1052                                 }
1053
1054                                 if (use_restrict) {
1055                                         BMO_slot_map_int_insert(bm, op, "faceout_groupmap", f, path->group);
1056                                 }
1057                         }
1058                 }
1059                 
1060                 edge_free_path(pathbase, path);
1061         }
1062
1063         BMO_slot_buffer_from_enabled_flag(bm, op, "faceout", BM_FACE, FACE_NEW);
1064
1065         BLI_array_free(edges);
1066         BLI_array_free(verts);
1067         edge_pathbase_free(pathbase);
1068         MEM_freeN(edata);
1069         MEM_freeN(vdata);
1070 }
1071
1072 static BMEdge *edge_next(BMesh *bm, BMEdge *e)
1073 {
1074         BMIter iter;
1075         BMEdge *e2;
1076         int i;
1077
1078         for (i = 0; i < 2; i++) {
1079                 BM_ITER_ELEM (e2, &iter, i ? e->v2 : e->v1, BM_EDGES_OF_VERT) {
1080                         if ((BMO_elem_flag_test(bm, e2, EDGE_MARK)) &&
1081                             (!BMO_elem_flag_test(bm, e2, EDGE_VIS)) &&
1082                             (e2 != e))
1083                         {
1084                                 return e2;
1085                         }
1086                 }
1087         }
1088
1089         return NULL;
1090 }
1091
1092 void bmo_edgenet_prepare(BMesh *bm, BMOperator *op)
1093 {
1094         BMOIter siter;
1095         BMEdge *e;
1096         BMEdge **edges1 = NULL, **edges2 = NULL, **edges;
1097         BLI_array_declare(edges1);
1098         BLI_array_declare(edges2);
1099         BLI_array_declare(edges);
1100         int ok = 1;
1101         int i, count;
1102
1103         BMO_slot_buffer_flag_enable(bm, op, "edges", BM_EDGE, EDGE_MARK);
1104         
1105         /* validate that each edge has at most one other tagged edge in the
1106          * disk cycle around each of it's vertices */
1107         BMO_ITER (e, &siter, bm, op, "edges", BM_EDGE) {
1108                 for (i = 0; i < 2; i++) {
1109                         count = BMO_vert_edge_flags_count(bm, i ? e->v2 : e->v1, EDGE_MARK);
1110                         if (count > 2) {
1111                                 ok = 0;
1112                                 break;
1113                         }
1114                 }
1115
1116                 if (!ok) {
1117                         break;
1118                 }
1119         }
1120
1121         /* we don't have valid edge layouts, retur */
1122         if (!ok) {
1123                 return;
1124         }
1125
1126         /* find connected loops within the input edge */
1127         count = 0;
1128         while (1) {
1129                 BMO_ITER (e, &siter, bm, op, "edges", BM_EDGE) {
1130                         if (!BMO_elem_flag_test(bm, e, EDGE_VIS)) {
1131                                 if (BMO_vert_edge_flags_count(bm, e->v1, EDGE_MARK) == 1 ||
1132                                     BMO_vert_edge_flags_count(bm, e->v2, EDGE_MARK) == 1)
1133                                 {
1134                                         break;
1135                                 }
1136                         }
1137                 }
1138                 
1139                 if (!e) {
1140                         break;
1141                 }
1142
1143                 if (!count) {
1144                         edges = edges1;
1145                 }
1146                 else if (count == 1) {
1147                         edges = edges2;
1148                 }
1149                 else {
1150                         break;
1151                 }
1152                 
1153                 i = 0;
1154                 while (e) {
1155                         BMO_elem_flag_enable(bm, e, EDGE_VIS);
1156                         BLI_array_growone(edges);
1157                         edges[i] = e;
1158
1159                         e = edge_next(bm, e);
1160                         i++;
1161                 }
1162
1163                 if (!count) {
1164                         edges1 = edges;
1165                         BLI_array_set_length(edges1, BLI_array_count(edges));
1166                 }
1167                 else {
1168                         edges2 = edges;
1169                         BLI_array_set_length(edges2, BLI_array_count(edges));
1170                 }
1171
1172                 BLI_array_empty(edges);
1173                 count++;
1174         }
1175
1176         if (edges1 && BLI_array_count(edges1) > 2 &&
1177             BM_edge_share_vert_count(edges1[0], edges1[BLI_array_count(edges1) - 1]))
1178         {
1179                 if (edges2 && BLI_array_count(edges2) > 2 &&
1180                     BM_edge_share_vert_count(edges2[0], edges2[BLI_array_count(edges2) - 1]))
1181                 {
1182                         BLI_array_free(edges1);
1183                         BLI_array_free(edges2);
1184                         return;
1185                 }
1186                 else {
1187                         edges1 = edges2;
1188                         edges2 = NULL;
1189                 }
1190         }
1191
1192         if (edges2 && BLI_array_count(edges2) > 2 &&
1193             BM_edge_share_vert_count(edges2[0], edges2[BLI_array_count(edges2) - 1]))
1194         {
1195                 edges2 = NULL;
1196         }
1197
1198         /* two unconnected loops, connect the */
1199         if (edges1 && edges2) {
1200                 BMVert *v1, *v2, *v3, *v4;
1201                 float dvec1[3];
1202                 float dvec2[3];
1203
1204                 if (BLI_array_count(edges1) == 1) {
1205                         v1 = edges1[0]->v1;
1206                         v2 = edges1[0]->v2;
1207                 }
1208                 else {
1209                         v1 = BM_vert_in_edge(edges1[1], edges1[0]->v1) ? edges1[0]->v2 : edges1[0]->v1;
1210                         i  = BLI_array_count(edges1) - 1;
1211                         v2 = BM_vert_in_edge(edges1[i - 1], edges1[i]->v1) ? edges1[i]->v2 : edges1[i]->v1;
1212                 }
1213
1214                 if (BLI_array_count(edges2) == 1) {
1215                         v3 = edges2[0]->v1;
1216                         v4 = edges2[0]->v2;
1217                 }
1218                 else {
1219                         v3 = BM_vert_in_edge(edges2[1], edges2[0]->v1) ? edges2[0]->v2 : edges2[0]->v1;
1220                         i  = BLI_array_count(edges2) - 1;
1221                         v4 = BM_vert_in_edge(edges2[i - 1], edges2[i]->v1) ? edges2[i]->v2 : edges2[i]->v1;
1222                 }
1223
1224                 /* if there is ever bowtie quads between two edges the problem is here! [#30367] */
1225 #if 0
1226                 normal_tri_v3(dvec1, v1->co, v2->co, v4->co);
1227                 normal_tri_v3(dvec2, v1->co, v4->co, v3->co);
1228 #else
1229                 {
1230                         /* save some CPU cycles and skip the sqrt and 1 subtraction */
1231                         float a1[3], a2[3], a3[3];
1232                         sub_v3_v3v3(a1, v1->co, v2->co);
1233                         sub_v3_v3v3(a2, v1->co, v4->co);
1234                         sub_v3_v3v3(a3, v1->co, v3->co);
1235                         cross_v3_v3v3(dvec1, a1, a2);
1236                         cross_v3_v3v3(dvec2, a2, a3);
1237                 }
1238 #endif
1239                 if (dot_v3v3(dvec1, dvec2) < 0.0f) {
1240                         SWAP(BMVert *, v3, v4);
1241                 }
1242
1243                 e = BM_edge_create(bm, v1, v3, NULL, TRUE);
1244                 BMO_elem_flag_enable(bm, e, ELE_NEW);
1245                 e = BM_edge_create(bm, v2, v4, NULL, TRUE);
1246                 BMO_elem_flag_enable(bm, e, ELE_NEW);
1247         }
1248         else if (edges1) {
1249                 BMVert *v1, *v2;
1250
1251                 if (BLI_array_count(edges1) > 1) {
1252                         v1 = BM_vert_in_edge(edges1[1], edges1[0]->v1) ? edges1[0]->v2 : edges1[0]->v1;
1253                         i  = BLI_array_count(edges1) - 1;
1254                         v2 = BM_vert_in_edge(edges1[i - 1], edges1[i]->v1) ? edges1[i]->v2 : edges1[i]->v1;
1255                         e  = BM_edge_create(bm, v1, v2, NULL, TRUE);
1256                         BMO_elem_flag_enable(bm, e, ELE_NEW);
1257                 }
1258         }
1259         
1260         BMO_slot_buffer_from_enabled_flag(bm, op, "edgeout", BM_EDGE, ELE_NEW);
1261
1262         BLI_array_free(edges1);
1263         BLI_array_free(edges2);
1264 }
1265
1266 /* This is what runs when pressing the F key
1267  * doing the best thing here isn't always easy create vs dissolve, its nice to support
1268  * but it it _really_ gives issues we might have to not call dissolve. - campbell
1269  */
1270 void bmo_contextual_create_exec(BMesh *bm, BMOperator *op)
1271 {
1272         BMOperator op2;
1273         BMOIter oiter;
1274         BMIter iter;
1275         BMHeader *h;
1276         BMVert *v, *verts[4];
1277         BMEdge *e;
1278         BMFace *f;
1279         int totv = 0, tote = 0, totf = 0, amount;
1280         const short mat_nr = BMO_slot_int_get(op, "mat_nr");
1281
1282         /* count number of each element type we were passe */
1283         BMO_ITER (h, &oiter, bm, op, "geom", BM_VERT|BM_EDGE|BM_FACE) {
1284                 switch (h->htype) {
1285                         case BM_VERT: totv++; break;
1286                         case BM_EDGE: tote++; break;
1287                         case BM_FACE: totf++; break;
1288                 }
1289
1290                 BMO_elem_flag_enable(bm, (BMElemF *)h, ELE_NEW);
1291         }
1292         
1293         /* --- Support for Special Case ---
1294          * where there is a contiguous edge ring with one isolated vertex.
1295          *
1296          * This example shows 2 edges created from 3 verts
1297          * with 1 free standing vertex. Dotted lines denote the 2 edges that are created.
1298          *
1299          * note that this works for any sided shape.
1300          *
1301          * +--------+
1302          * |        .
1303          * |        .
1304          * |        .
1305          * |        .
1306          * +........+ <-- starts out free standing.
1307          *
1308          */
1309
1310         /* Here we check for consistancy and create 2 edges */
1311         if (totf == 0 && totv >= 4 && totv == tote + 2) {
1312                 /* find a free standing vertex and 2 endpoint verts */
1313                 BMVert *v_free = NULL, *v_a = NULL, *v_b = NULL;
1314                 int ok = TRUE;
1315
1316
1317                 BMO_ITER (v, &oiter, bm, op, "geom", BM_VERT) {
1318                         /* count how many flagged edges this vertex uses */
1319                         int tot_edges = 0;
1320                         BM_ITER_ELEM (e, &iter, v, BM_EDGES_OF_VERT) {
1321                                 if (BMO_elem_flag_test(bm, e, ELE_NEW)) {
1322                                         tot_edges++;
1323                                         if (tot_edges > 2) {
1324                                                 break;
1325                                         }
1326                                 }
1327                         }
1328
1329                         if (tot_edges == 0) {
1330                                 /* only accept 1 free vert */
1331                                 if (v_free == NULL)  v_free = v;
1332                                 else                 ok = FALSE;  /* only ever want one of these */
1333                         }
1334                         else if (tot_edges == 1) {
1335                                 if (v_a == NULL)       v_a = v;
1336                                 else if (v_b == NULL)  v_b = v;
1337                                 else                   ok = FALSE;  /* only ever want 2 of these */
1338                         }
1339                         else if (tot_edges == 2) {
1340                                 /* do nothing, regular case */
1341                         }
1342                         else {
1343                                 ok = FALSE; /* if a vertex has 3+ edge users then cancel - this is only simple cases */
1344                         }
1345
1346                         if (ok == FALSE) {
1347                                 break;
1348                         }
1349                 }
1350
1351                 if (ok == TRUE && v_free && v_a && v_b) {
1352                         e = BM_edge_create(bm, v_free, v_a, NULL, TRUE);
1353                         BMO_elem_flag_enable(bm, e, ELE_NEW);
1354
1355                         e = BM_edge_create(bm, v_free, v_b, NULL, TRUE);
1356                         BMO_elem_flag_enable(bm, e, ELE_NEW);
1357                 }
1358         }
1359         /* --- end special case support, continue as normal --- */
1360
1361         /* call edgenet create */
1362         /* call edgenet prepare op so additional face creation cases wore */
1363         BMO_op_initf(bm, &op2, "edgenet_prepare edges=%fe", ELE_NEW);
1364         BMO_op_exec(bm, &op2);
1365         BMO_slot_buffer_flag_enable(bm, &op2, "edgeout", BM_EDGE, ELE_NEW);
1366         BMO_op_finish(bm, &op2);
1367
1368         BMO_op_initf(bm, &op2, "edgenet_fill edges=%fe use_fill_check=%b mat_nr=%i", ELE_NEW, TRUE, mat_nr);
1369         BMO_op_exec(bm, &op2);
1370
1371         /* return if edge net create did something */
1372         if (BMO_slot_buffer_count(bm, &op2, "faceout")) {
1373                 BMO_slot_copy(&op2, op, "faceout", "faceout");
1374                 BMO_op_finish(bm, &op2);
1375                 return;
1376         }
1377
1378         BMO_op_finish(bm, &op2);
1379         
1380         /* now call dissolve face */
1381         BMO_op_initf(bm, &op2, "dissolve_faces faces=%ff", ELE_NEW);
1382         BMO_op_exec(bm, &op2);
1383         
1384         /* if we dissolved anything, then return */
1385         if (BMO_slot_buffer_count(bm, &op2, "regionout")) {
1386                 BMO_slot_copy(&op2, op, "regionout", "faceout");
1387                 BMO_op_finish(bm, &op2);
1388                 return;
1389         }
1390
1391         BMO_op_finish(bm, &op2);
1392
1393         /* now, count how many verts we have */
1394         amount = 0;
1395         BM_ITER_MESH (v, &iter, bm, BM_VERTS_OF_MESH) {
1396                 if (BMO_elem_flag_test(bm, v, ELE_NEW)) {
1397                         verts[amount] = v;
1398                         amount++;
1399
1400                         if (amount > 4) break;
1401                 }
1402         }
1403
1404         if (amount == 2) {
1405                 /* create edge */
1406                 e = BM_edge_create(bm, verts[0], verts[1], NULL, TRUE);
1407                 BMO_elem_flag_enable(bm, e, ELE_OUT);
1408         }
1409         else if (0) { /* nice feature but perhaps it should be a different tool? */
1410
1411                 /* tricky feature for making a line/edge from selection history...
1412                  *
1413                  * Rather then do nothing, when 5+ verts are selected, check if they are in our history,
1414                  * when this is so, we can make edges from them, but _not_ a face,
1415                  * if it is the intention to make a face the user can just hit F again since there will be edges next
1416                  * time around.
1417                  *
1418                  * if all history verts have ELE_NEW flagged and the total number of history verts == totv,
1419                  * then we know the history contains all verts here and we can continue...
1420                  */
1421
1422                 BMEditSelection *ese;
1423                 int tot_ese_v = 0;
1424
1425                 for (ese = bm->selected.first; ese; ese = ese->next) {
1426                         if (ese->htype == BM_VERT) {
1427                                 if (BMO_elem_flag_test(bm, (BMElemF *)ese->ele, ELE_NEW)) {
1428                                         tot_ese_v++;
1429                                 }
1430                                 else {
1431                                         /* unflagged vert means we are not in sync */
1432                                         tot_ese_v = -1;
1433                                         break;
1434                                 }
1435                         }
1436                 }
1437
1438                 if (tot_ese_v == totv) {
1439                         BMVert *v_prev = NULL;
1440                         /* yes, all select-history verts are accounted for, now make edges */
1441
1442                         for (ese = bm->selected.first; ese; ese = ese->next) {
1443                                 if (ese->htype == BM_VERT) {
1444                                         v = (BMVert *)ese->ele;
1445                                         if (v_prev) {
1446                                                 e = BM_edge_create(bm, v, v_prev, NULL, TRUE);
1447                                                 BMO_elem_flag_enable(bm, e, ELE_OUT);
1448                                         }
1449                                         v_prev = v;
1450                                 }
1451                         }
1452                 }
1453                 /* done creating edges */
1454         }
1455         else if (amount > 2) {
1456                 /* TODO, all these verts may be connected by edges.
1457                  * we should check on this before assuming they are a random set of verts */
1458
1459                 BMVert **vert_arr = MEM_mallocN(sizeof(BMVert **) * totv, __func__);
1460                 int i = 0;
1461
1462                 BMO_ITER (v, &oiter, bm, op, "geom", BM_VERT) {
1463                         vert_arr[i] = v;
1464                         i++;
1465                 }
1466
1467                 f = BM_face_create_ngon_vcloud(bm, vert_arr, totv, TRUE);
1468
1469                 if (f) {
1470                         BMO_elem_flag_enable(bm, f, ELE_OUT);
1471                         f->mat_nr = mat_nr;
1472                 }
1473
1474                 MEM_freeN(vert_arr);
1475         }
1476 }