aa1c0397404b839bb9649627305f693e49170e7d
[blender.git] / source / blender / bmesh / operators / createops.c
1 #include "MEM_guardedalloc.h"
2
3 #include "BLI_utildefines.h"
4
5 #include "BLI_memarena.h"
6 #include "BLI_mempool.h"
7 #include "BLI_heap.h"
8 #include "BLI_ghash.h"
9 #include "BLI_blenlib.h"
10 #include "BLI_math.h"
11 #include "BLI_array.h"
12 #include "BLI_smallhash.h"
13 #include "BLI_rand.h"
14
15 #include "bmesh.h"
16 #include "bmesh_operators_private.h"
17
18 #define EDGE_MARK       1
19 #define EDGE_VIS        2
20
21 #define FACE_NEW        1
22
23 #define ELE_NEW         1
24 #define ELE_OUT         2
25 #define ELE_ORIG        4
26
27 #define FACE_IGNORE     16
28
29 typedef struct EPathNode {
30         struct EPathNode *next, *prev;
31         BMVert *v;
32         BMEdge *e;
33         BMEdge *cure;
34 } EPathNode;
35
36 typedef struct EPath {
37         ListBase nodes;
38         float weight;
39         int group;
40 } EPath;
41
42 typedef struct PathBase {
43         BLI_mempool *nodepool, *pathpool;
44 } PathBase;
45
46 typedef struct EdgeData {
47         int tag;
48         int ftag;
49         
50         struct {
51                 struct BMEdge *next, *prev;
52         } dlink1;
53         struct {
54                 struct BMEdge *next, *prev;
55         } dlink2;
56 } EdgeData;
57
58 typedef struct VertData {
59         BMEdge *e;
60         float no[3], offco[3], sco[3]; /*offco is vertex coordinate slightly offset randomly*/
61         int tag;
62 } VertData;
63
64 static int count_edge_faces(BMesh *bm, BMEdge *e);
65
66 /****  rotation system code ***/
67
68 #define rs_get_edge_link(e, v, ed) ((v) == ((BMEdge*)(e))->v1 ? (Link*)&(((EdgeData*)(ed))->dlink1) : (Link*)&(((EdgeData*)(ed))->dlink2))
69
70 static int rotsys_append_edge(struct BMEdge *e, struct BMVert *v, 
71                                                 EdgeData *edata, VertData *vdata)
72 {
73         EdgeData *ed = &edata[BM_GetIndex(e)];
74         VertData *vd = &vdata[BM_GetIndex(v)];
75         
76         if (!vd->e) {
77                 Link *e1 = (Link*)rs_get_edge_link(e, v, ed);
78
79                 vd->e = e;
80                 e1->next = e1->prev = (Link*)e;
81         } else {
82                 Link *e1, *e2, *e3;
83                 EdgeData *ved = &edata[BM_GetIndex(vd->e)];
84
85                 e1 = rs_get_edge_link(e, v, ed);
86                 e2 = rs_get_edge_link(vd->e, v, ved);
87                 e3 = e2->prev ? rs_get_edge_link(e2->prev, v, &edata[BM_GetIndex(e2->prev)]) : NULL;
88
89                 e1->next = (Link*)vd->e;
90                 e1->prev = e2->prev;
91
92                 e2->prev = (Link*)e;
93                 if (e3)
94                         e3->next = (Link*)e;
95         }
96
97         return 1;
98 }
99
100 static void rotsys_remove_edge(struct BMEdge *e, struct BMVert *v, 
101                                                 EdgeData *edata, VertData *vdata)
102 {
103         EdgeData *ed = edata + BM_GetIndex(e);
104         VertData *vd = vdata + BM_GetIndex(v);
105         Link *e1, *e2;
106
107         e1 = rs_get_edge_link(e, v, ed);
108         if (e1->prev) {
109                 e2 = rs_get_edge_link(e1->prev, v, ed);
110                 e2->next = e1->next;
111         }
112
113         if (e1->next) {
114                 e2 = rs_get_edge_link(e1->next, v, ed);
115                 e2->prev = e1->prev;
116         }
117
118         if (vd->e == e)
119                 vd->e = (e != (BMEdge *)e1->next) ? (BMEdge*)e1->next : NULL;
120
121         e1->next = e1->prev = NULL;
122 }
123
124 static struct BMEdge *rotsys_nextedge(struct BMEdge *e, struct BMVert *v, 
125                                                                         EdgeData *edata, VertData *UNUSED(vdata))
126 {
127         if (v == e->v1)
128                 return edata[BM_GetIndex(e)].dlink1.next;
129         if (v == e->v2)
130                 return edata[BM_GetIndex(e)].dlink2.next;
131         return NULL;
132 }
133
134 static BMEdge *rotsys_prevedge(BMEdge *e, BMVert *v, 
135                                                 EdgeData *edata, VertData *UNUSED(vdata))
136 {
137         if (v == e->v1)
138                 return edata[BM_GetIndex(e)].dlink1.prev;
139         if (v == e->v2)
140                 return edata[BM_GetIndex(e)].dlink2.prev;
141         return NULL;
142 }
143
144 static struct BMEdge *rotsys_reverse(struct BMEdge *UNUSED(e), struct BMVert *v, EdgeData *edata, VertData *vdata)
145 {
146         BMEdge **edges = NULL;
147         BMEdge *e2;
148         BLI_array_staticdeclare(edges, 256);
149         int i, totedge;
150         
151         e2 = vdata[BM_GetIndex(v)].e;
152         do {
153                 BLI_array_append(edges, e2);
154                 e2 = rotsys_nextedge(e2, v, edata, vdata);
155         } while (e2 != vdata[BM_GetIndex(v)].e);
156         
157         totedge = BLI_array_count(edges);
158         for (i=0; i<totedge/2; i++) {
159                 SWAP(BMEdge*, edges[i], edges[totedge-1-i]);
160         }
161         
162         vdata[BM_GetIndex(v)].e = NULL;
163         for (i=0; i<totedge; i++) {
164                 rotsys_append_edge(edges[i], v, edata, vdata);
165         }
166         
167         BLI_array_free(edges);
168         
169         return 0;
170 }
171
172 static int rotsys_count(struct BMVert *v, EdgeData *edata, VertData *vdata)
173 {
174         BMEdge *e = vdata[BM_GetIndex(v)].e;
175         int i=0;
176
177         if (!e)
178                 return 0;
179
180         do {
181                 if (!e)
182                         return 0;
183                 e =  rotsys_nextedge(e, v, edata, vdata);
184
185                 if (i >= (1<<20)) {
186                         printf("bmesh error: infinite loop in disk cycle!\n");
187                         return 0;
188                 }
189
190                 i += 1;
191         } while (e != vdata[BM_GetIndex(v)].e);
192
193         return i;
194 }
195
196 static int rotsys_fill_faces(BMesh *bm, EdgeData *edata, VertData *vdata)
197 {
198         BMIter iter;
199         BMEdge *e, **edges = NULL;
200         BLI_array_declare(edges);
201         BMVert *v, **verts = NULL;
202         BMFace *f;
203         BLI_array_declare(verts);
204         SmallHash visithash, *hash=&visithash;
205         int i;
206         
207         BM_ITER(e, &iter, bm, BM_EDGES_OF_MESH, NULL) {
208                 BMEdge *e2, *starte;
209                 BMVert *startv;
210                 int rad, ok;
211                 
212                 rad = count_edge_faces(bm, e);
213                 
214                 if (rad < 2)
215                         starte = e;
216                 else
217                         continue;
218                 
219                 /*do two passes, going forward then backward*/
220                 for (i=0; i<2; i++) {
221                         BLI_smallhash_init(hash);
222                         
223                         BLI_array_empty(verts);
224                         BLI_array_empty(edges);
225
226                         startv = v = starte->v1;
227                         e2 = starte;
228                         ok = 1;
229                         if (!v || !e2)
230                                 continue;
231                                 
232                         do {
233                                 if (BLI_smallhash_haskey(hash, (intptr_t)e2) 
234                                  || BLI_smallhash_haskey(hash, (intptr_t)v)) {
235                                         ok = 0;
236                                         break;
237                                 }
238                                 
239                                 BLI_array_append(verts, v);
240                                 BLI_array_append(edges, e2);
241                                 
242                                 BLI_smallhash_insert(hash, (intptr_t)e2, NULL);
243         
244                                 v = BM_OtherEdgeVert(e2, v);
245                                 e2 = i ? rotsys_prevedge(e2, v, edata, vdata) : rotsys_nextedge(e2, v, edata, vdata);
246                         } while (e2 != starte && v != startv);
247                         
248                         BLI_smallhash_release(hash);
249                         
250                         if (!ok || BLI_array_count(edges) < 3)
251                                 continue;
252                         
253                         f = BM_Make_Ngon(bm, verts[0], verts[1], edges, BLI_array_count(edges), 1);
254                         if (!f)
255                                 continue;
256                 }
257         }
258         
259         return 0;
260 }
261
262 static void rotsys_make_consistent(BMesh *bm, EdgeData *edata, VertData *vdata)
263 {
264         BMIter iter;
265         BMEdge *e;
266         BMVert *v, **stack=NULL;
267         BLI_array_declare(stack);
268         int i;
269         
270         for (i=0; i<bm->totvert; i++) {
271                 vdata[i].tag = 0;
272         }
273         
274         while (1) {
275                 VertData *vd;
276                 BMVert *startv = NULL;
277                 float dis;
278                 
279                 v = BMIter_New(&iter, bm, BM_VERTS_OF_MESH, NULL);
280                 for (i=0; i<bm->totvert; i++, BMIter_Step(&iter)) {
281                         vd = vdata + BM_GetIndex(v);
282                         
283                         if (vd->tag)
284                                 continue;
285                         
286                         if (!startv || dot_v3v3(vd->offco, vd->offco) > dis) {
287                                 dis = dot_v3v3(vd->offco, vd->offco);
288                                 startv = v;
289                         }
290                 }
291                 
292                 if (!startv)
293                         break;
294                 
295                 vd = vdata + BM_GetIndex(startv);
296                 
297                 BLI_array_empty(stack);
298                 BLI_array_append(stack, startv);
299                 
300                 vd->tag = 1;
301                 
302                 while (BLI_array_count(stack)) {
303                         v = BLI_array_pop(stack);
304                         vd = vdata + BM_GetIndex(v);
305                         
306                         if (!vd->e)
307                                 continue;
308                         
309                         e = vd->e;
310                         do {
311                                 BMVert *v2 = BM_OtherEdgeVert(e, v);
312                                 VertData *vd2 = vdata + BM_GetIndex(v2);
313                                 
314                                 if (dot_v3v3(vd->no, vd2->no) < 0.0f+FLT_EPSILON*2) {
315                                         rotsys_reverse(e, v2, edata, vdata);
316                                         mul_v3_fl(vd2->no, -1.0f);
317                                 }
318
319                                 if (!vd2->tag) {
320                                         BLI_array_append(stack, v2);
321                                         vd2->tag = 1;
322                                 }
323                                 
324                                 e = rotsys_nextedge(e, v, edata, vdata);
325                         } while (e != vd->e);
326                 }
327         }
328
329         BLI_array_free(stack);
330 }
331
332 static void init_rotsys(BMesh *bm, EdgeData *edata, VertData *vdata)
333 {
334         BMIter iter;
335         BMEdge *e;
336         BMEdge **edges = NULL;
337         BLI_array_staticdeclare(edges, 256);
338         BMVert *v, *lastv;
339         /*BMVert **verts = NULL; */
340         /*BLI_array_staticdeclare(verts, 256);*/ /*UNUSED*/
341         int i;
342         
343         #define SIGN(n) ((n)<0.0f)
344         
345         BM_ITER(v, &iter, bm, BM_VERTS_OF_MESH, NULL) {
346                 BMIter eiter;
347                 float no[3], cent[3];
348                 int j, k=0, totedge=0;
349                 
350                 if (BM_GetIndex(v) == -1)
351                         continue;
352                 
353                 BLI_array_empty(edges);
354                 
355                 BM_ITER(e, &eiter, bm, BM_EDGES_OF_VERT, v) {
356                         if (BMO_TestFlag(bm, e, EDGE_MARK)) {
357                                 BLI_array_append(edges, e);
358                                 totedge++;
359                         }
360                 }
361                 
362                 copy_v3_v3(cent, v->co);
363                 
364                 zero_v3(no);
365                 for (i=0; i<totedge; i++) {
366                         BMEdge *e1, *e2;
367                         float cno[3], vec1[3], vec2[3];
368                         
369                         e1 = edges[i];
370                         e2 = edges[(i+1)%totedge];
371
372                         sub_v3_v3v3(vec1, (BM_OtherEdgeVert(e1, v))->co, v->co);
373                         sub_v3_v3v3(vec2, (BM_OtherEdgeVert(e2, v))->co, v->co);
374                         
375                         cross_v3_v3v3(cno, vec1, vec2);
376                         normalize_v3(cno);
377                         
378                         if (i && dot_v3v3(cno, no) < 0.0f+FLT_EPSILON*10)
379                                 mul_v3_fl(cno, -1.0f);
380                         
381                         add_v3_v3(no, cno);
382                         normalize_v3(no);
383                 }
384                 
385                 /*generate plane-flattened coordinates*/
386                 for (i=0; i<totedge; i++) {
387                         BMEdge *e1;
388                         BMVert *v2;
389                         float cvec[3], vec1[3];
390                         
391                         e1 = edges[i];
392                         v2 = BM_OtherEdgeVert(e1, v);
393                         
394                         sub_v3_v3v3(vec1, v2->co, v->co);
395                         
396                         cross_v3_v3v3(cvec, vec1, no);
397                         cross_v3_v3v3(vec1, cvec, no);
398                         normalize_v3(vec1);
399                         
400                         mul_v3_fl(vec1, len_v3v3(v2->co, v->co));
401                         add_v3_v3(vec1, v->co);
402                         
403                         copy_v3_v3(vdata[BM_GetIndex(v2)].sco, vec1);
404                 }
405                 
406                 BLI_srandom(0);
407                 
408                 /*first, ensure no 0 or 180 angles between adjacent
409                   (and that adjacent's adjacent) edges*/
410                 for (i=0, k=0; i<totedge; i++) {
411                         BMEdge *e1, *e2, *e3=NULL;
412                         BMVert *v1, *v2, *v3;
413                         VertData *vd1, *vd2, *vd3;
414                         float vec1[3], vec2[3], vec3[3], size;
415                         int s1, s2, s3;
416                         
417                         if (totedge < 3)
418                                 continue;
419                         
420                         e1 = edges[(i+totedge-1) % totedge];
421                         e2 = edges[i];
422                         e3 = edges[(i+1) % totedge];
423                         
424                         v1 = BM_OtherEdgeVert(e1, v); v2 = BM_OtherEdgeVert(e2, v); v3 = BM_OtherEdgeVert(e3, v);
425                         vd1 = vdata+BM_GetIndex(v1); vd2 = vdata+BM_GetIndex(v2); vd3 = vdata+BM_GetIndex(v3);
426                         
427                         sub_v3_v3v3(vec1, vd1->sco, cent);
428                         sub_v3_v3v3(vec2, vd2->sco, cent);
429                         sub_v3_v3v3(vec3, vd3->sco, cent);
430                         
431                         size = (len_v3(vec1) + len_v3(vec3))*0.01;
432                         normalize_v3(vec1); normalize_v3(vec2); normalize_v3(vec3);
433                         
434                         #ifdef STRAIGHT
435                         #undef STRAIGHT
436                         #endif
437                         #define STRAIGHT(vec11, vec22) (fabs(dot_v3v3((vec11), (vec22))) > 1.0-FLT_EPSILON*1000)
438                         
439                         s1 = STRAIGHT(vec1, vec2); s2 = STRAIGHT(vec2, vec3); s3 = STRAIGHT(vec1, vec3);
440                         
441                         if (s1 || s2 || s3) {
442                                 copy_v3_v3(cent, v->co);
443
444                                 for (j=0; j<3; j++) {
445                                         float fac = (BLI_frand()-0.5f)*size;
446                                         cent[j] += fac;
447                                 }
448                                 
449                                 if (k < 2000) {
450                                         i = 0;
451                                         k++;
452                                         continue;
453                                 } else {
454                                         k++;
455                                         continue;
456                                 }
457
458                         }
459                 }
460                 
461                 copy_v3_v3(vdata[BM_GetIndex(v)].offco, cent);
462                 //copy_v3_v3(v->co, cent);
463                 
464                 /*now, sort edges so the triangle fan of all edges
465                   has a consistent normal.  this is the same as
466                   sorting by polar coordinates along a group normal*/
467                 for (j=0; j<totedge; j++) {
468                         for (i=0; i<totedge; i++) {
469                                 BMEdge *e1, *e2, *e3=NULL;
470                                 BMVert *v1, *v2, *v3;
471                                 VertData *vd1, *vd2, *vd3;
472                                 float vec1[3], vec2[3], vec3[3], n1[3], n2[3], n3[3];
473                                 int s1, s2, s3;
474                                 
475                                 e1 = edges[(i+totedge-1) % totedge];
476                                 e2 = edges[i];
477                                 e3 = edges[(i+1) % totedge];
478                                 
479                                 v1 = BM_OtherEdgeVert(e1, v); v2 = BM_OtherEdgeVert(e2, v); v3 = BM_OtherEdgeVert(e3, v);
480                                 vd1 = vdata+BM_GetIndex(v1); vd2 = vdata+BM_GetIndex(v2); vd3 = vdata+BM_GetIndex(v3);
481         
482                                 sub_v3_v3v3(vec1, vd1->sco, cent);
483                                 sub_v3_v3v3(vec2, vd2->sco, cent);
484                                 sub_v3_v3v3(vec3, vd3->sco, cent);
485                                 
486                                 cross_v3_v3v3(n1, vec1, vec2);
487                                 cross_v3_v3v3(n2, vec2, vec3);
488                                 cross_v3_v3v3(n3, vec1, vec3);
489                                 
490                                 normalize_v3(n1); normalize_v3(n2); normalize_v3(n3);
491                                 
492                                 s1 = STRAIGHT(vec1, vec2); s2 = STRAIGHT(vec2, vec3); s3 = STRAIGHT(vec1, vec3);
493                                                                 
494                                 if (s1 || s2 || s3) {
495                                         printf("yeek! s1: %d, s2: %d, s3: %dx\n", s1, s2, s3);
496                                 }
497                                 if (dot_v3v3(n1, n2) < 0.0f) {
498                                         if (dot_v3v3(n1, n3) >= 0.0f + FLT_EPSILON*10) {
499                                                 SWAP(BMEdge*, edges[i], edges[(i+1)%totedge]);
500                                         } else {
501                                                 SWAP(BMEdge*, edges[(i+totedge-1)%totedge], edges[(i+1)%totedge])
502                                                 SWAP(BMEdge*, edges[i], edges[(i+1)%totedge])
503                                         }
504                                 } 
505                         }
506                 }
507                 
508                 #undef STRAIGHT
509                                 
510                 zero_v3(no);
511                 
512                 /*yay, edges is sorted*/
513                 for (i=0; i<totedge; i++) {
514                         BMEdge *e1 = edges[i], *e2 = edges[(i+1)%totedge];
515                         float eno[3];
516                         
517                         normal_tri_v3(eno, BM_OtherEdgeVert(e1, v)->co, v->co, BM_OtherEdgeVert(e2, v)->co);
518                         add_v3_v3(no, eno);
519                         
520                         rotsys_append_edge(edges[i], v, edata, vdata);
521                 }
522                 
523                 normalize_v3(no);
524                 copy_v3_v3(vdata[BM_GetIndex(v)].no, no);
525         }
526         
527         /*now, make sure rotation system is topologically consistent
528           (e.g. vert normals consistently point either inside or outside)*/
529         rotsys_make_consistent(bm, edata, vdata);
530
531         //rotsys_fill_faces(bm, edata, vdata);
532
533 #if 0
534         /*create visualizing geometry*/
535         BM_ITER(v, &iter, bm, BM_VERTS_OF_MESH, NULL) {
536                 BMVert *v2;
537                 BMFace *f;
538                 int totedge = BM_Vert_EdgeCount(v);
539
540                 if (BM_GetIndex(v) == -1)
541                         continue;
542                 
543                 //cv = BM_Make_Vert(bm, cent, v);
544                 //BM_SetIndex(cv, -1);
545                 i = 0;
546                 e = vdata[BM_GetIndex(v)].e;
547                 lastv = NULL;
548                 do {
549                         BMEdge *e2;
550                         BMVert *v2;
551                         float f = ((float)i / (float)totedge)*0.35 + 0.05;
552                         float co[3];
553                         
554                         if (!e)
555                                 break;
556                                 
557                         if (!BM_OtherEdgeVert(e, v))
558                                 continue;
559                         
560                         sub_v3_v3v3(co, (BM_OtherEdgeVert(e, v))->co, vdata[BM_GetIndex(v)].offco);
561                         mul_v3_fl(co, f);
562                         add_v3_v3(co, vdata[BM_GetIndex(v)].offco);
563                         
564                         v2 = BM_Make_Vert(bm, co, NULL);
565                         BM_SetIndex(v2, -1);
566                         //BM_Make_Edge(bm, cv, v2, NULL, 0);
567                         
568                         BM_Select(bm, v2, 1);
569                         if (lastv) {
570                                 e2 =
571                                  BM_Make_Edge(bm, lastv, v2, NULL, 0);
572                                 BM_Select(bm, e2, 1);
573                         }
574                         
575                         lastv = v2;
576                         
577                         e = rotsys_nextedge(e, v, edata, vdata);
578                         i++;
579                 } while (e != vdata[BM_GetIndex(v)].e);
580         }
581 #endif
582
583         BLI_array_free(edges);
584 }
585
586 static PathBase *edge_pathbase_new(void)
587 {
588         PathBase *pb = MEM_callocN(sizeof(PathBase), "PathBase");
589
590         pb->nodepool = BLI_mempool_create(sizeof(EPathNode), 1, 512, 1, 0);
591         pb->pathpool = BLI_mempool_create(sizeof(EPath), 1, 512, 1, 0);
592
593         return pb;
594 }
595
596 static void edge_pathbase_free(PathBase *pathbase)
597 {
598         BLI_mempool_destroy(pathbase->nodepool);
599         BLI_mempool_destroy(pathbase->pathpool);
600         MEM_freeN(pathbase);
601 }
602
603 static EPath *edge_copy_add_path(PathBase *pb, EPath *path, BMVert *appendv, BMEdge *e)
604 {
605         EPath *path2;
606         EPathNode *node, *node2;
607
608         path2 = BLI_mempool_alloc(pb->pathpool);
609         path2->nodes.first = path2->nodes.last = NULL;
610         path2->weight = 0.0f;
611         path2->group = path->group;
612         
613         for (node=path->nodes.first; node; node=node->next) {
614                 node2 = BLI_mempool_alloc(pb->nodepool);
615                 *node2 = *node;
616                 BLI_addtail(&path2->nodes, node2);
617         }
618
619         node2 = BLI_mempool_alloc(pb->nodepool);
620         node2->v = appendv;
621         node2->e = e;
622         node2->cure = NULL;
623
624         BLI_addtail(&path2->nodes, node2);
625
626         return path2;
627 }
628
629 static EPath *edge_path_new(PathBase *pb, BMVert *start, BMEdge *starte)
630 {
631         EPath *path;
632         EPathNode *node;
633
634         path = BLI_mempool_alloc(pb->pathpool);
635         node = BLI_mempool_alloc(pb->nodepool);
636         
637         path->nodes.first = path->nodes.last = NULL;
638         
639         node->v = start;
640         node->e = starte;
641         node->cure = NULL;
642
643         BLI_addtail(&path->nodes, node);
644         path->weight = 0.0f;
645
646         return path;
647 }
648
649 static float edge_weight_path(EPath *path, EdgeData *edata, VertData *UNUSED(vdata))
650 {
651         EPathNode *node, *first=path->nodes.first;
652         float w = 0.0;
653
654         for (node=path->nodes.first; node; node=node->next) {
655                 if (node->e && node != path->nodes.first) {
656                         w += edata[BM_GetIndex(node->e)].ftag;
657                         if (node->prev) {
658                                 /*BMESH_TODO*/
659                                 (void)first;
660                                 //w += len_v3v3(node->v->co, first->e->v1->co)*0.0001f;
661                                 //w += len_v3v3(node->v->co, first->e->v2->co)*0.0001f;                         
662                         }
663                 }
664
665                 w += 1.0f;
666         }
667
668         return w;
669 }
670
671
672 static void edge_free_path(PathBase *pathbase, EPath *path)
673 {
674         EPathNode *node, *next;
675
676         for (node=path->nodes.first; node; node=next) {
677                 next = node->next;
678                 BLI_mempool_free(pathbase->nodepool, node);
679         }
680
681         BLI_mempool_free(pathbase->pathpool, path);
682 }
683
684 static EPath *edge_find_shortest_path(BMesh *bm, BMOperator *op, BMEdge *edge, EdgeData *edata, 
685                                                                 VertData *vdata, PathBase *pathbase, int group)
686 {
687         BMEdge *e;
688         GHash *gh = BLI_ghash_new(BLI_ghashutil_ptrhash, BLI_ghashutil_ptrcmp, "createops find shortest path");
689         BMVert *v1, *v2;
690         BMVert **verts = NULL;
691         BLI_array_staticdeclare(verts, 1024);
692         Heap *heap = BLI_heap_new();
693         EPath *path = NULL, *path2;
694         BMVert *startv;
695         BMVert *endv;
696         EPathNode *node;
697         int i, use_restrict = BMO_Get_Int(op, "use_restrict");
698
699         startv = edata[BM_GetIndex(edge)].ftag ? edge->v2 : edge->v1;
700         endv = edata[BM_GetIndex(edge)].ftag ? edge->v1 : edge->v2;
701         
702         path = edge_path_new(pathbase, startv, edge);
703         BLI_ghash_insert(gh, startv, NULL);
704         BLI_heap_insert(heap, path->weight, path);
705         path->group = group;
706
707         while (BLI_heap_size(heap)) {
708                 VertData *vd;
709                 EPathNode *last;
710                 BMFace *f = NULL;
711                 
712                 path = BLI_heap_popmin(heap);
713                 last = path->nodes.last;
714                 v1 = last->v;
715                 
716                 if (v1 == endv) {
717                         /*make sure this path loop doesn't already exist*/
718                         i = 0;
719                         BLI_array_empty(verts);
720                         for (i=0, node = path->nodes.first; node; node=node->next, i++) {
721                                 BLI_array_growone(verts);
722                                 verts[i] = node->v;
723                         }
724
725                         if (BM_Face_Exists(bm, verts, i, &f)) {
726                                 if (!BMO_TestFlag(bm, f, FACE_IGNORE)) {
727                                         BLI_ghash_remove(gh, endv, NULL, NULL);
728                                         continue;
729                                 }
730                         }
731                         break;
732                 }
733                 
734                 vd = vdata + BM_GetIndex(v1);
735                 if (!vd->e)
736                         continue;
737                 
738                 v2 = NULL;
739                 while (1) {             
740                         if (!last->cure) {
741                                 last->cure = e = vdata[BM_GetIndex(last->v)].e;
742                         } else {
743                                 last->cure = e = rotsys_nextedge(last->cure, last->v, edata, vdata);
744                                 if (last->cure == vdata[BM_GetIndex(last->v)].e) {
745                                         v2 = NULL;
746                                         break;
747                                 }
748                         }
749                         
750                         if (e == edge || !BMO_TestFlag(bm, e, EDGE_MARK)) {
751                                 continue;
752                         }
753                                 
754                         v2 = BM_OtherEdgeVert(e, last->v);
755                         
756                         if (BLI_ghash_haskey(gh, v2)) {
757                                 v2 = NULL;
758                                 continue;
759                         }
760                         
761                         if (use_restrict && BMO_InMap(bm, op, "restrict", e)) {
762                                 int group = BMO_Get_MapInt(bm, op, "restrict", e);
763                                 
764                                 if (!(group & path->group)) {
765                                         v2 = NULL;
766                                         continue;
767                                 }
768                         }
769
770                         break;
771                 }
772                 
773                 if (!v2) {
774                         if (path) {
775                                 edge_free_path(pathbase, path);
776                                 path = NULL;
777                         }
778                         continue;
779                 }
780                 
781                 /*add path back into heap*/
782                 BLI_heap_insert(heap, path->weight, path);
783                 
784                 /*put v2 in gh map*/
785                 BLI_ghash_insert(gh, v2, NULL);
786
787                 path2 = edge_copy_add_path(pathbase, path, v2, e);
788                 path2->weight = edge_weight_path(path2, edata, vdata);
789
790                 BLI_heap_insert(heap, path2->weight, path2);
791         }
792         
793         if (path && ((EPathNode*)path->nodes.last)->v != endv) {
794                 edge_free_path(pathbase, path);
795                 path = NULL;
796         }
797                 
798         BLI_array_free(verts);
799         BLI_heap_free(heap, NULL);
800         BLI_ghash_free(gh, NULL, NULL);
801
802         return path;
803 }
804
805 static int count_edge_faces(BMesh *bm, BMEdge *e) {
806         int i=0;
807         BMLoop *l = e->l;
808         
809         if (!l)
810                 return 0;
811         
812         do {
813                 if (!BMO_TestFlag(bm, l->f, FACE_IGNORE))
814                         i++;
815
816                 l = l->radial_next;
817         } while (l != e->l);
818         
819         return i;
820 }
821
822 void bmesh_edgenet_fill_exec(BMesh *bm, BMOperator *op)
823 {
824         BMIter iter;
825         BMOIter siter;
826         BMFace *f;
827         BMEdge *e, *edge;
828         BMVert *v, **verts = NULL;
829         BLI_array_declare(verts);
830         EPath *path;
831         EPathNode *node;
832         EdgeData *edata;
833         VertData *vdata;
834         BMEdge **edges = NULL;
835         PathBase *pathbase = edge_pathbase_new();
836         BLI_array_declare(edges);
837         int use_restrict = BMO_Get_Int(op, "use_restrict");
838         int i, j, group = 0;
839
840         if (!bm->totvert || !bm->totedge)
841                 return;
842
843         edata = MEM_callocN(sizeof(EdgeData)*bm->totedge, "EdgeData");
844         vdata = MEM_callocN(sizeof(VertData)*bm->totvert, "VertData");
845         
846         BMO_Flag_Buffer(bm, op, "edges", EDGE_MARK, BM_EDGE);
847         BMO_Flag_Buffer(bm, op, "excludefaces", FACE_IGNORE, BM_FACE);
848         
849         i = 0;
850         BM_ITER(v, &iter, bm, BM_VERTS_OF_MESH, NULL) {
851                 BM_SetIndex(v, i);
852                 i++;    
853         }
854
855         BM_ITER(f, &iter, bm, BM_FACES_OF_MESH, NULL) {
856                 BMO_SetFlag(bm, f, ELE_ORIG);
857         }
858
859         i = 0;
860         BM_ITER(e, &iter, bm, BM_EDGES_OF_MESH, NULL) {
861                 BM_SetIndex(e, i);
862                 
863                 if (!BMO_TestFlag(bm, e, EDGE_MARK)) {
864                         edata[i].tag = 2;
865                 }
866
867                 i += 1;
868         }
869
870         init_rotsys(bm, edata, vdata);
871         
872         while (1) {
873                 edge = NULL;
874                 group = 0;
875                 
876                 BMO_ITER(e, &siter, bm, op, "edges", BM_EDGE) {
877                         /*if restrict is on, only start on faces in the restrict map*/
878                         if (use_restrict && !BMO_InMap(bm, op, "restrict", e))
879                                 continue;
880                                 
881                         if (edata[BM_GetIndex(e)].tag < 2) {
882                                 edge = e;
883
884                                 if (use_restrict) {
885                                         int i=0, j=0, gi=0;
886                                         
887                                         group = BMO_Get_MapInt(bm, op, "restrict", e);                          
888                                         
889                                         for (i=0; i<30; i++) {
890                                                 if (group & (1<<i)) {
891                                                         j++;
892                                                         gi = i;
893
894                                                         if (j-1 == edata[BM_GetIndex(e)].tag)
895                                                                 break;
896                                                 }
897                                         }
898                                         
899                                         group = 1<<gi;
900                                 }
901                                 
902                                 break;
903                         }
904                 }
905
906                 if (!edge)
907                         break;
908
909                 edata[BM_GetIndex(edge)].tag += 1;
910
911                 path = edge_find_shortest_path(bm, op, edge, edata, vdata, pathbase, group);
912                 if (!path)
913                         continue;
914                 
915                 BLI_array_empty(edges);
916                 BLI_array_empty(verts);
917                 i = 0;
918                 for (node=path->nodes.first; node; node=node->next) {
919                         if (!node->next)
920                                 continue;
921
922                         e = BM_Edge_Exist(node->v, node->next->v);
923                         
924                         /*this should never happen*/
925                         if (!e)
926                                 break;
927                         
928                         edata[BM_GetIndex(e)].ftag++;
929                         BLI_array_growone(edges);
930                         edges[i++] = e;
931
932                         BLI_array_append(verts, node->v);
933                 }
934                 
935                 BLI_array_growone(edges);
936                 edges[i++] = edge;
937                 edata[BM_GetIndex(edge)].ftag++;
938                 
939                 for (j=0; j<i; j++) {
940                         if (count_edge_faces(bm, edges[j]) >= 2) {                      
941                                 edge_free_path(pathbase, path);
942                                 break;
943                         }
944                 }
945                 
946                 if (j != i)
947                         continue;
948                 
949                 if (i) {
950                         f = BM_Make_Ngon(bm, edge->v1, edge->v2, edges, i, 1);
951                         if (f && !BMO_TestFlag(bm, f, ELE_ORIG)) {
952                                 BMO_SetFlag(bm, f, FACE_NEW);
953                         }
954
955                         if (use_restrict)
956                                 BMO_Insert_MapInt(bm, op, "faceout_groupmap", f, path->group);
957                 }
958                 
959                 edge_free_path(pathbase, path);
960         }
961
962         BMO_Flag_To_Slot(bm, op, "faceout", FACE_NEW, BM_FACE);
963
964         BLI_array_free(edges);
965         BLI_array_free(verts);
966         edge_pathbase_free(pathbase);
967         MEM_freeN(edata);
968         MEM_freeN(vdata);
969 }
970
971 static BMEdge *edge_next(BMesh *bm, BMEdge *e)
972 {
973         BMIter iter;
974         BMEdge *e2;
975         int i;
976
977         for (i=0; i<2; i++) {
978                 BM_ITER(e2, &iter, bm, BM_EDGES_OF_VERT, i?e->v2:e->v1) {
979                         if (BMO_TestFlag(bm, e2, EDGE_MARK) 
980                             && !BMO_TestFlag(bm, e2, EDGE_VIS) && e2 != e)
981                         {
982                                 return e2;
983                         }
984                 }
985         }
986
987         return NULL;
988 }
989
990 void bmesh_edgenet_prepare(BMesh *bm, BMOperator *op)
991 {
992         BMOIter siter;
993         BMEdge *e;
994         BMEdge **edges1 = NULL, **edges2 = NULL, **edges;
995         BLI_array_declare(edges1);
996         BLI_array_declare(edges2);
997         BLI_array_declare(edges);
998         int ok = 1;
999         int i, count;
1000
1001         BMO_Flag_Buffer(bm, op, "edges", EDGE_MARK, BM_EDGE);
1002         
1003         /*validate that each edge has at most one other tagged edge in the
1004           disk cycle around each of it's vertices*/
1005         BMO_ITER(e, &siter, bm, op, "edges", BM_EDGE) {
1006                 for (i=0; i<2; i++) {
1007                         count = BMO_Vert_CountEdgeFlags(bm, i?e->v2:e->v1, EDGE_MARK);
1008                         if (count > 2) {
1009                                 ok = 0;
1010                                 break;
1011                         }
1012                 }
1013
1014                 if (!ok) break;
1015         }
1016
1017         /*we don't have valid edge layouts, return*/
1018         if (!ok)
1019                 return;
1020
1021
1022         /*find connected loops within the input edges*/
1023         count = 0;
1024         while (1) {
1025                 BMO_ITER(e, &siter, bm, op, "edges", BM_EDGE) {
1026                         if (!BMO_TestFlag(bm, e, EDGE_VIS)) {
1027                                 if (BMO_Vert_CountEdgeFlags(bm, e->v1, EDGE_MARK)==1)
1028                                         break;
1029                                 if (BMO_Vert_CountEdgeFlags(bm, e->v2, EDGE_MARK)==1)
1030                                         break;
1031                         }
1032                 }
1033                 
1034                 if (!e) break;
1035                 
1036                 if (!count)
1037                         edges = edges1;
1038                 else if (count==1)
1039                         edges = edges2;
1040                 else break;
1041                 
1042                 i = 0;
1043                 while (e) {
1044                         BMO_SetFlag(bm, e, EDGE_VIS);
1045                         BLI_array_growone(edges);
1046                         edges[i] = e;
1047
1048                         e = edge_next(bm, e);
1049                         i++;
1050                 }
1051
1052                 if (!count) {
1053                         edges1 = edges;
1054                         BLI_array_set_length(edges1, BLI_array_count(edges));
1055                 } else {
1056                         edges2 = edges;
1057                         BLI_array_set_length(edges2, BLI_array_count(edges));
1058                 }
1059
1060                 BLI_array_empty(edges);
1061                 count++;
1062         }
1063
1064 #define EDGECON(e1, e2) (e1->v1 == e2->v1 || e1->v2 == e2->v2 || e1->v1 == e2->v2)
1065
1066         if (edges1 && BLI_array_count(edges1) > 2 && EDGECON(edges1[0], edges1[BLI_array_count(edges1)-1])) {
1067                 if (edges2 && BLI_array_count(edges2) > 2 && EDGECON(edges2[0], edges2[BLI_array_count(edges2)-1])) {
1068                         BLI_array_free(edges1);
1069                         BLI_array_free(edges2);
1070                         return;
1071                 } else {
1072                         edges1 = edges2;
1073                         edges2 = NULL;
1074                 }
1075         }
1076
1077         if (edges2 && BLI_array_count(edges2) > 2 && EDGECON(edges2[0], edges2[BLI_array_count(edges2)-1])) {
1078                 edges2 = NULL;
1079         }
1080
1081         /*two unconnected loops, connect them*/
1082         if (edges1 && edges2) {
1083                 BMVert *v1, *v2, *v3, *v4;
1084
1085                 if (BLI_array_count(edges1)==1) {
1086                         v1 = edges1[0]->v1;
1087                         v2 = edges1[0]->v2;
1088                 } else {
1089                         if (BM_Vert_In_Edge(edges1[1], edges1[0]->v1))
1090                                 v1 = edges1[0]->v2;
1091                         else v1 = edges1[0]->v1;
1092
1093                         i = BLI_array_count(edges1)-1;
1094                         if (BM_Vert_In_Edge(edges1[i-1], edges1[i]->v1))
1095                                 v2 = edges1[i]->v2;
1096                         else v2 = edges1[i]->v1;
1097                 }
1098
1099                 if (BLI_array_count(edges2)==1) {
1100                         v3 = edges2[0]->v1;
1101                         v4 = edges2[0]->v2;
1102                 } else {
1103                         if (BM_Vert_In_Edge(edges2[1], edges2[0]->v1))
1104                                 v3 = edges2[0]->v2;
1105                         else v3 = edges2[0]->v1;
1106
1107                         i = BLI_array_count(edges2)-1;
1108                         if (BM_Vert_In_Edge(edges2[i-1], edges2[i]->v1))
1109                                 v4 = edges2[i]->v2;
1110                         else v4 = edges2[i]->v1;
1111                 }
1112
1113                 if (len_v3v3(v1->co, v3->co) > len_v3v3(v1->co, v4->co)) {
1114                         BMVert *v;
1115                         v = v3;
1116                         v3 = v4;
1117                         v4 = v;
1118                 }
1119
1120                 e = BM_Make_Edge(bm, v1, v3, NULL, 1);
1121                 BMO_SetFlag(bm, e, ELE_NEW);
1122                 e = BM_Make_Edge(bm, v2, v4, NULL, 1);
1123                 BMO_SetFlag(bm, e, ELE_NEW);
1124         } else if (edges1) {
1125                 BMVert *v1, *v2;
1126                 
1127                 if (BLI_array_count(edges1) > 1) {
1128                         if (BM_Vert_In_Edge(edges1[1], edges1[0]->v1))
1129                                 v1 = edges1[0]->v2;
1130                         else v1 = edges1[0]->v1;
1131
1132                         i = BLI_array_count(edges1)-1;
1133                         if (BM_Vert_In_Edge(edges1[i-1], edges1[i]->v1))
1134                                 v2 = edges1[i]->v2;
1135                         else v2 = edges1[i]->v1;
1136
1137                         e = BM_Make_Edge(bm, v1, v2, NULL, 1);
1138                         BMO_SetFlag(bm, e, ELE_NEW);
1139                 }
1140         }
1141         
1142         BMO_Flag_To_Slot(bm, op, "edgeout", ELE_NEW, BM_EDGE);
1143
1144         BLI_array_free(edges1);
1145         BLI_array_free(edges2);
1146
1147 #undef EDGECON
1148 }
1149
1150 /*this is essentially new fkey*/
1151 void bmesh_contextual_create_exec(BMesh *bm, BMOperator *op)
1152 {
1153         BMOperator op2;
1154         BMOIter oiter;
1155         BMIter iter;
1156         BMHeader *h;
1157         BMVert *v, *verts[4];
1158         BMEdge *e;
1159         BMFace *f;
1160         int totv=0, tote=0, totf=0, amount;
1161
1162         /*count number of each element type we were passed*/
1163         BMO_ITER(h, &oiter, bm, op, "geom", BM_VERT|BM_EDGE|BM_FACE) {
1164                 switch (h->type) {
1165                         case BM_VERT: totv++; break;
1166                         case BM_EDGE: tote++; break;
1167                         case BM_FACE: totf++; break;
1168                 }
1169
1170                 BMO_SetFlag(bm, h, ELE_NEW);
1171         }
1172         
1173         /*call edgenet create*/
1174         /*  call edgenet prepare op so additional face creation cases work*/
1175         BMO_InitOpf(bm, &op2, "edgenet_prepare edges=%fe", ELE_NEW);
1176         BMO_Exec_Op(bm, &op2);
1177         BMO_Flag_Buffer(bm, &op2, "edgeout", ELE_NEW, BM_EDGE);
1178         BMO_Finish_Op(bm, &op2);
1179
1180         BMO_InitOpf(bm, &op2, "edgenet_fill edges=%fe", ELE_NEW);
1181         BMO_Exec_Op(bm, &op2);
1182
1183         /*return if edge net create did something*/
1184         if (BMO_CountSlotBuf(bm, &op2, "faceout")) {
1185                 BMO_CopySlot(&op2, op, "faceout", "faceout");
1186                 BMO_Finish_Op(bm, &op2);
1187                 return;
1188         }
1189
1190         BMO_Finish_Op(bm, &op2);
1191         
1192         /*now call dissolve faces*/
1193         BMO_InitOpf(bm, &op2, "dissolvefaces faces=%ff", ELE_NEW);
1194         BMO_Exec_Op(bm, &op2);
1195         
1196         /*if we dissolved anything, then return.*/
1197         if (BMO_CountSlotBuf(bm, &op2, "regionout")) {
1198                 BMO_CopySlot(&op2, op, "regionout", "faceout");
1199                 BMO_Finish_Op(bm, &op2);
1200                 return;
1201         }
1202
1203         BMO_Finish_Op(bm, &op2);
1204
1205         /*now, count how many verts we have*/
1206         amount = 0;
1207         BM_ITER(v, &iter, bm, BM_VERTS_OF_MESH, NULL) {
1208                 if (BMO_TestFlag(bm, v, ELE_NEW)) {
1209                         verts[amount] = v;
1210                         amount++;
1211
1212                         if (amount > 4) break;
1213                 }
1214         }
1215
1216         if (amount == 2) {
1217                 /*create edge*/
1218                 e = BM_Make_Edge(bm, verts[0], verts[1], NULL, 1);
1219                 BMO_SetFlag(bm, e, ELE_OUT);            
1220         } else if (amount == 3) {
1221                 /*create triangle*/
1222                 BM_Make_QuadTri(bm, verts[0], verts[1], verts[2], NULL, NULL, 1);
1223         } else if (amount == 4) {
1224                 f = NULL;
1225
1226                 /* the order of vertices can be anything, 6 cases to check */
1227                 if( is_quad_convex_v3(verts[0]->co, verts[1]->co, verts[2]->co, verts[3]->co) ) {
1228                         f= BM_Make_QuadTri(bm, verts[0], verts[1], verts[2], verts[3], NULL, 1);
1229                 }
1230                 else if( is_quad_convex_v3(verts[0]->co, verts[2]->co, verts[3]->co, verts[1]->co) ) {
1231                         f= BM_Make_QuadTri(bm, verts[0], verts[2], verts[3], verts[1], NULL, 1);
1232                 }
1233                 else if( is_quad_convex_v3(verts[0]->co, verts[2]->co, verts[1]->co, verts[3]->co) ) {
1234                         f= BM_Make_QuadTri(bm, verts[0], verts[2], verts[1], verts[3], NULL, 1);
1235                 }
1236                 else if( is_quad_convex_v3(verts[0]->co, verts[1]->co, verts[3]->co, verts[2]->co) ) {
1237                         f= BM_Make_QuadTri(bm, verts[0], verts[1], verts[3], verts[2], NULL, 1);
1238                 }
1239                 else if( is_quad_convex_v3(verts[0]->co, verts[3]->co, verts[2]->co, verts[1]->co) ) {
1240                         f= BM_Make_QuadTri(bm, verts[0], verts[3], verts[2], verts[1], NULL, 1);
1241                 }
1242                 else if( is_quad_convex_v3(verts[0]->co, verts[3]->co, verts[1]->co, verts[2]->co) ) {
1243                         f= BM_Make_QuadTri(bm, verts[0], verts[3], verts[1], verts[2], NULL, 1);
1244                 }
1245                 else {
1246                         printf("cannot find nice quad from concave set of vertices\n");
1247                 }
1248
1249                 if (f) BMO_SetFlag(bm, f, ELE_OUT);
1250         }
1251 }