remove unused vars
[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;
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         BMVert *lastv;
536         BM_ITER(v, &iter, bm, BM_VERTS_OF_MESH, NULL) {
537                 BMVert *v2;
538                 BMFace *f;
539                 int totedge = BM_Vert_EdgeCount(v);
540
541                 if (BM_GetIndex(v) == -1)
542                         continue;
543                 
544                 //cv = BM_Make_Vert(bm, cent, v);
545                 //BM_SetIndex(cv, -1);
546                 i = 0;
547                 e = vdata[BM_GetIndex(v)].e;
548                 lastv = NULL;
549                 do {
550                         BMEdge *e2;
551                         BMVert *v2;
552                         float f = ((float)i / (float)totedge)*0.35 + 0.05;
553                         float co[3];
554                         
555                         if (!e)
556                                 break;
557                                 
558                         if (!BM_OtherEdgeVert(e, v))
559                                 continue;
560                         
561                         sub_v3_v3v3(co, (BM_OtherEdgeVert(e, v))->co, vdata[BM_GetIndex(v)].offco);
562                         mul_v3_fl(co, f);
563                         add_v3_v3(co, vdata[BM_GetIndex(v)].offco);
564                         
565                         v2 = BM_Make_Vert(bm, co, NULL);
566                         BM_SetIndex(v2, -1);
567                         //BM_Make_Edge(bm, cv, v2, NULL, 0);
568                         
569                         BM_Select(bm, v2, 1);
570                         if (lastv) {
571                                 e2 =
572                                  BM_Make_Edge(bm, lastv, v2, NULL, 0);
573                                 BM_Select(bm, e2, 1);
574                         }
575                         
576                         lastv = v2;
577                         
578                         e = rotsys_nextedge(e, v, edata, vdata);
579                         i++;
580                 } while (e != vdata[BM_GetIndex(v)].e);
581         }
582 #endif
583
584         BLI_array_free(edges);
585 }
586
587 static PathBase *edge_pathbase_new(void)
588 {
589         PathBase *pb = MEM_callocN(sizeof(PathBase), "PathBase");
590
591         pb->nodepool = BLI_mempool_create(sizeof(EPathNode), 1, 512, 1, 0);
592         pb->pathpool = BLI_mempool_create(sizeof(EPath), 1, 512, 1, 0);
593
594         return pb;
595 }
596
597 static void edge_pathbase_free(PathBase *pathbase)
598 {
599         BLI_mempool_destroy(pathbase->nodepool);
600         BLI_mempool_destroy(pathbase->pathpool);
601         MEM_freeN(pathbase);
602 }
603
604 static EPath *edge_copy_add_path(PathBase *pb, EPath *path, BMVert *appendv, BMEdge *e)
605 {
606         EPath *path2;
607         EPathNode *node, *node2;
608
609         path2 = BLI_mempool_alloc(pb->pathpool);
610         path2->nodes.first = path2->nodes.last = NULL;
611         path2->weight = 0.0f;
612         path2->group = path->group;
613         
614         for (node=path->nodes.first; node; node=node->next) {
615                 node2 = BLI_mempool_alloc(pb->nodepool);
616                 *node2 = *node;
617                 BLI_addtail(&path2->nodes, node2);
618         }
619
620         node2 = BLI_mempool_alloc(pb->nodepool);
621         node2->v = appendv;
622         node2->e = e;
623         node2->cure = NULL;
624
625         BLI_addtail(&path2->nodes, node2);
626
627         return path2;
628 }
629
630 static EPath *edge_path_new(PathBase *pb, BMVert *start, BMEdge *starte)
631 {
632         EPath *path;
633         EPathNode *node;
634
635         path = BLI_mempool_alloc(pb->pathpool);
636         node = BLI_mempool_alloc(pb->nodepool);
637         
638         path->nodes.first = path->nodes.last = NULL;
639         
640         node->v = start;
641         node->e = starte;
642         node->cure = NULL;
643
644         BLI_addtail(&path->nodes, node);
645         path->weight = 0.0f;
646
647         return path;
648 }
649
650 static float edge_weight_path(EPath *path, EdgeData *edata, VertData *UNUSED(vdata))
651 {
652         EPathNode *node, *first=path->nodes.first;
653         float w = 0.0;
654
655         for (node=path->nodes.first; node; node=node->next) {
656                 if (node->e && node != path->nodes.first) {
657                         w += edata[BM_GetIndex(node->e)].ftag;
658                         if (node->prev) {
659                                 /*BMESH_TODO*/
660                                 (void)first;
661                                 //w += len_v3v3(node->v->co, first->e->v1->co)*0.0001f;
662                                 //w += len_v3v3(node->v->co, first->e->v2->co)*0.0001f;                         
663                         }
664                 }
665
666                 w += 1.0f;
667         }
668
669         return w;
670 }
671
672
673 static void edge_free_path(PathBase *pathbase, EPath *path)
674 {
675         EPathNode *node, *next;
676
677         for (node=path->nodes.first; node; node=next) {
678                 next = node->next;
679                 BLI_mempool_free(pathbase->nodepool, node);
680         }
681
682         BLI_mempool_free(pathbase->pathpool, path);
683 }
684
685 static EPath *edge_find_shortest_path(BMesh *bm, BMOperator *op, BMEdge *edge, EdgeData *edata, 
686                                                                 VertData *vdata, PathBase *pathbase, int group)
687 {
688         BMEdge *e;
689         GHash *gh = BLI_ghash_new(BLI_ghashutil_ptrhash, BLI_ghashutil_ptrcmp, "createops find shortest path");
690         BMVert *v1, *v2;
691         BMVert **verts = NULL;
692         BLI_array_staticdeclare(verts, 1024);
693         Heap *heap = BLI_heap_new();
694         EPath *path = NULL, *path2;
695         BMVert *startv;
696         BMVert *endv;
697         EPathNode *node;
698         int i, use_restrict = BMO_Get_Int(op, "use_restrict");
699
700         startv = edata[BM_GetIndex(edge)].ftag ? edge->v2 : edge->v1;
701         endv = edata[BM_GetIndex(edge)].ftag ? edge->v1 : edge->v2;
702         
703         path = edge_path_new(pathbase, startv, edge);
704         BLI_ghash_insert(gh, startv, NULL);
705         BLI_heap_insert(heap, path->weight, path);
706         path->group = group;
707
708         while (BLI_heap_size(heap)) {
709                 VertData *vd;
710                 EPathNode *last;
711                 BMFace *f = NULL;
712                 
713                 path = BLI_heap_popmin(heap);
714                 last = path->nodes.last;
715                 v1 = last->v;
716                 
717                 if (v1 == endv) {
718                         /*make sure this path loop doesn't already exist*/
719                         i = 0;
720                         BLI_array_empty(verts);
721                         for (i=0, node = path->nodes.first; node; node=node->next, i++) {
722                                 BLI_array_growone(verts);
723                                 verts[i] = node->v;
724                         }
725
726                         if (BM_Face_Exists(bm, verts, i, &f)) {
727                                 if (!BMO_TestFlag(bm, f, FACE_IGNORE)) {
728                                         BLI_ghash_remove(gh, endv, NULL, NULL);
729                                         continue;
730                                 }
731                         }
732                         break;
733                 }
734                 
735                 vd = vdata + BM_GetIndex(v1);
736                 if (!vd->e)
737                         continue;
738                 
739                 v2 = NULL;
740                 while (1) {             
741                         if (!last->cure) {
742                                 last->cure = e = vdata[BM_GetIndex(last->v)].e;
743                         } else {
744                                 last->cure = e = rotsys_nextedge(last->cure, last->v, edata, vdata);
745                                 if (last->cure == vdata[BM_GetIndex(last->v)].e) {
746                                         v2 = NULL;
747                                         break;
748                                 }
749                         }
750                         
751                         if (e == edge || !BMO_TestFlag(bm, e, EDGE_MARK)) {
752                                 continue;
753                         }
754                                 
755                         v2 = BM_OtherEdgeVert(e, last->v);
756                         
757                         if (BLI_ghash_haskey(gh, v2)) {
758                                 v2 = NULL;
759                                 continue;
760                         }
761                         
762                         if (use_restrict && BMO_InMap(bm, op, "restrict", e)) {
763                                 int group = BMO_Get_MapInt(bm, op, "restrict", e);
764                                 
765                                 if (!(group & path->group)) {
766                                         v2 = NULL;
767                                         continue;
768                                 }
769                         }
770
771                         break;
772                 }
773                 
774                 if (!v2) {
775                         if (path) {
776                                 edge_free_path(pathbase, path);
777                                 path = NULL;
778                         }
779                         continue;
780                 }
781                 
782                 /*add path back into heap*/
783                 BLI_heap_insert(heap, path->weight, path);
784                 
785                 /*put v2 in gh map*/
786                 BLI_ghash_insert(gh, v2, NULL);
787
788                 path2 = edge_copy_add_path(pathbase, path, v2, e);
789                 path2->weight = edge_weight_path(path2, edata, vdata);
790
791                 BLI_heap_insert(heap, path2->weight, path2);
792         }
793         
794         if (path && ((EPathNode*)path->nodes.last)->v != endv) {
795                 edge_free_path(pathbase, path);
796                 path = NULL;
797         }
798                 
799         BLI_array_free(verts);
800         BLI_heap_free(heap, NULL);
801         BLI_ghash_free(gh, NULL, NULL);
802
803         return path;
804 }
805
806 static int count_edge_faces(BMesh *bm, BMEdge *e) {
807         int i=0;
808         BMLoop *l = e->l;
809         
810         if (!l)
811                 return 0;
812         
813         do {
814                 if (!BMO_TestFlag(bm, l->f, FACE_IGNORE))
815                         i++;
816
817                 l = l->radial_next;
818         } while (l != e->l);
819         
820         return i;
821 }
822
823 void bmesh_edgenet_fill_exec(BMesh *bm, BMOperator *op)
824 {
825         BMIter iter;
826         BMOIter siter;
827         BMFace *f;
828         BMEdge *e, *edge;
829         BMVert *v, **verts = NULL;
830         BLI_array_declare(verts);
831         EPath *path;
832         EPathNode *node;
833         EdgeData *edata;
834         VertData *vdata;
835         BMEdge **edges = NULL;
836         PathBase *pathbase = edge_pathbase_new();
837         BLI_array_declare(edges);
838         int use_restrict = BMO_Get_Int(op, "use_restrict");
839         int i, j, group = 0;
840
841         if (!bm->totvert || !bm->totedge)
842                 return;
843
844         edata = MEM_callocN(sizeof(EdgeData)*bm->totedge, "EdgeData");
845         vdata = MEM_callocN(sizeof(VertData)*bm->totvert, "VertData");
846         
847         BMO_Flag_Buffer(bm, op, "edges", EDGE_MARK, BM_EDGE);
848         BMO_Flag_Buffer(bm, op, "excludefaces", FACE_IGNORE, BM_FACE);
849         
850         i = 0;
851         BM_ITER(v, &iter, bm, BM_VERTS_OF_MESH, NULL) {
852                 BM_SetIndex(v, i);
853                 i++;    
854         }
855
856         BM_ITER(f, &iter, bm, BM_FACES_OF_MESH, NULL) {
857                 BMO_SetFlag(bm, f, ELE_ORIG);
858         }
859
860         i = 0;
861         BM_ITER(e, &iter, bm, BM_EDGES_OF_MESH, NULL) {
862                 BM_SetIndex(e, i);
863                 
864                 if (!BMO_TestFlag(bm, e, EDGE_MARK)) {
865                         edata[i].tag = 2;
866                 }
867
868                 i += 1;
869         }
870
871         init_rotsys(bm, edata, vdata);
872         
873         while (1) {
874                 edge = NULL;
875                 group = 0;
876                 
877                 BMO_ITER(e, &siter, bm, op, "edges", BM_EDGE) {
878                         /*if restrict is on, only start on faces in the restrict map*/
879                         if (use_restrict && !BMO_InMap(bm, op, "restrict", e))
880                                 continue;
881                                 
882                         if (edata[BM_GetIndex(e)].tag < 2) {
883                                 edge = e;
884
885                                 if (use_restrict) {
886                                         int i=0, j=0, gi=0;
887                                         
888                                         group = BMO_Get_MapInt(bm, op, "restrict", e);                          
889                                         
890                                         for (i=0; i<30; i++) {
891                                                 if (group & (1<<i)) {
892                                                         j++;
893                                                         gi = i;
894
895                                                         if (j-1 == edata[BM_GetIndex(e)].tag)
896                                                                 break;
897                                                 }
898                                         }
899                                         
900                                         group = 1<<gi;
901                                 }
902                                 
903                                 break;
904                         }
905                 }
906
907                 if (!edge)
908                         break;
909
910                 edata[BM_GetIndex(edge)].tag += 1;
911
912                 path = edge_find_shortest_path(bm, op, edge, edata, vdata, pathbase, group);
913                 if (!path)
914                         continue;
915                 
916                 BLI_array_empty(edges);
917                 BLI_array_empty(verts);
918                 i = 0;
919                 for (node=path->nodes.first; node; node=node->next) {
920                         if (!node->next)
921                                 continue;
922
923                         e = BM_Edge_Exist(node->v, node->next->v);
924                         
925                         /*this should never happen*/
926                         if (!e)
927                                 break;
928                         
929                         edata[BM_GetIndex(e)].ftag++;
930                         BLI_array_growone(edges);
931                         edges[i++] = e;
932
933                         BLI_array_append(verts, node->v);
934                 }
935                 
936                 BLI_array_growone(edges);
937                 edges[i++] = edge;
938                 edata[BM_GetIndex(edge)].ftag++;
939                 
940                 for (j=0; j<i; j++) {
941                         if (count_edge_faces(bm, edges[j]) >= 2) {                      
942                                 edge_free_path(pathbase, path);
943                                 break;
944                         }
945                 }
946                 
947                 if (j != i)
948                         continue;
949                 
950                 if (i) {
951                         f = BM_Make_Ngon(bm, edge->v1, edge->v2, edges, i, 1);
952                         if (f && !BMO_TestFlag(bm, f, ELE_ORIG)) {
953                                 BMO_SetFlag(bm, f, FACE_NEW);
954                         }
955
956                         if (use_restrict)
957                                 BMO_Insert_MapInt(bm, op, "faceout_groupmap", f, path->group);
958                 }
959                 
960                 edge_free_path(pathbase, path);
961         }
962
963         BMO_Flag_To_Slot(bm, op, "faceout", FACE_NEW, BM_FACE);
964
965         BLI_array_free(edges);
966         BLI_array_free(verts);
967         edge_pathbase_free(pathbase);
968         MEM_freeN(edata);
969         MEM_freeN(vdata);
970 }
971
972 static BMEdge *edge_next(BMesh *bm, BMEdge *e)
973 {
974         BMIter iter;
975         BMEdge *e2;
976         int i;
977
978         for (i=0; i<2; i++) {
979                 BM_ITER(e2, &iter, bm, BM_EDGES_OF_VERT, i?e->v2:e->v1) {
980                         if (BMO_TestFlag(bm, e2, EDGE_MARK) 
981                             && !BMO_TestFlag(bm, e2, EDGE_VIS) && e2 != e)
982                         {
983                                 return e2;
984                         }
985                 }
986         }
987
988         return NULL;
989 }
990
991 void bmesh_edgenet_prepare(BMesh *bm, BMOperator *op)
992 {
993         BMOIter siter;
994         BMEdge *e;
995         BMEdge **edges1 = NULL, **edges2 = NULL, **edges;
996         BLI_array_declare(edges1);
997         BLI_array_declare(edges2);
998         BLI_array_declare(edges);
999         int ok = 1;
1000         int i, count;
1001
1002         BMO_Flag_Buffer(bm, op, "edges", EDGE_MARK, BM_EDGE);
1003         
1004         /*validate that each edge has at most one other tagged edge in the
1005           disk cycle around each of it's vertices*/
1006         BMO_ITER(e, &siter, bm, op, "edges", BM_EDGE) {
1007                 for (i=0; i<2; i++) {
1008                         count = BMO_Vert_CountEdgeFlags(bm, i?e->v2:e->v1, EDGE_MARK);
1009                         if (count > 2) {
1010                                 ok = 0;
1011                                 break;
1012                         }
1013                 }
1014
1015                 if (!ok) break;
1016         }
1017
1018         /*we don't have valid edge layouts, return*/
1019         if (!ok)
1020                 return;
1021
1022
1023         /*find connected loops within the input edges*/
1024         count = 0;
1025         while (1) {
1026                 BMO_ITER(e, &siter, bm, op, "edges", BM_EDGE) {
1027                         if (!BMO_TestFlag(bm, e, EDGE_VIS)) {
1028                                 if (BMO_Vert_CountEdgeFlags(bm, e->v1, EDGE_MARK)==1)
1029                                         break;
1030                                 if (BMO_Vert_CountEdgeFlags(bm, e->v2, EDGE_MARK)==1)
1031                                         break;
1032                         }
1033                 }
1034                 
1035                 if (!e) break;
1036                 
1037                 if (!count)
1038                         edges = edges1;
1039                 else if (count==1)
1040                         edges = edges2;
1041                 else break;
1042                 
1043                 i = 0;
1044                 while (e) {
1045                         BMO_SetFlag(bm, e, EDGE_VIS);
1046                         BLI_array_growone(edges);
1047                         edges[i] = e;
1048
1049                         e = edge_next(bm, e);
1050                         i++;
1051                 }
1052
1053                 if (!count) {
1054                         edges1 = edges;
1055                         BLI_array_set_length(edges1, BLI_array_count(edges));
1056                 } else {
1057                         edges2 = edges;
1058                         BLI_array_set_length(edges2, BLI_array_count(edges));
1059                 }
1060
1061                 BLI_array_empty(edges);
1062                 count++;
1063         }
1064
1065 #define EDGECON(e1, e2) (e1->v1 == e2->v1 || e1->v2 == e2->v2 || e1->v1 == e2->v2)
1066
1067         if (edges1 && BLI_array_count(edges1) > 2 && EDGECON(edges1[0], edges1[BLI_array_count(edges1)-1])) {
1068                 if (edges2 && BLI_array_count(edges2) > 2 && EDGECON(edges2[0], edges2[BLI_array_count(edges2)-1])) {
1069                         BLI_array_free(edges1);
1070                         BLI_array_free(edges2);
1071                         return;
1072                 } else {
1073                         edges1 = edges2;
1074                         edges2 = NULL;
1075                 }
1076         }
1077
1078         if (edges2 && BLI_array_count(edges2) > 2 && EDGECON(edges2[0], edges2[BLI_array_count(edges2)-1])) {
1079                 edges2 = NULL;
1080         }
1081
1082         /*two unconnected loops, connect them*/
1083         if (edges1 && edges2) {
1084                 BMVert *v1, *v2, *v3, *v4;
1085
1086                 if (BLI_array_count(edges1)==1) {
1087                         v1 = edges1[0]->v1;
1088                         v2 = edges1[0]->v2;
1089                 } else {
1090                         if (BM_Vert_In_Edge(edges1[1], edges1[0]->v1))
1091                                 v1 = edges1[0]->v2;
1092                         else v1 = edges1[0]->v1;
1093
1094                         i = BLI_array_count(edges1)-1;
1095                         if (BM_Vert_In_Edge(edges1[i-1], edges1[i]->v1))
1096                                 v2 = edges1[i]->v2;
1097                         else v2 = edges1[i]->v1;
1098                 }
1099
1100                 if (BLI_array_count(edges2)==1) {
1101                         v3 = edges2[0]->v1;
1102                         v4 = edges2[0]->v2;
1103                 } else {
1104                         if (BM_Vert_In_Edge(edges2[1], edges2[0]->v1))
1105                                 v3 = edges2[0]->v2;
1106                         else v3 = edges2[0]->v1;
1107
1108                         i = BLI_array_count(edges2)-1;
1109                         if (BM_Vert_In_Edge(edges2[i-1], edges2[i]->v1))
1110                                 v4 = edges2[i]->v2;
1111                         else v4 = edges2[i]->v1;
1112                 }
1113
1114                 if (len_v3v3(v1->co, v3->co) > len_v3v3(v1->co, v4->co)) {
1115                         BMVert *v;
1116                         v = v3;
1117                         v3 = v4;
1118                         v4 = v;
1119                 }
1120
1121                 e = BM_Make_Edge(bm, v1, v3, NULL, 1);
1122                 BMO_SetFlag(bm, e, ELE_NEW);
1123                 e = BM_Make_Edge(bm, v2, v4, NULL, 1);
1124                 BMO_SetFlag(bm, e, ELE_NEW);
1125         } else if (edges1) {
1126                 BMVert *v1, *v2;
1127                 
1128                 if (BLI_array_count(edges1) > 1) {
1129                         if (BM_Vert_In_Edge(edges1[1], edges1[0]->v1))
1130                                 v1 = edges1[0]->v2;
1131                         else v1 = edges1[0]->v1;
1132
1133                         i = BLI_array_count(edges1)-1;
1134                         if (BM_Vert_In_Edge(edges1[i-1], edges1[i]->v1))
1135                                 v2 = edges1[i]->v2;
1136                         else v2 = edges1[i]->v1;
1137
1138                         e = BM_Make_Edge(bm, v1, v2, NULL, 1);
1139                         BMO_SetFlag(bm, e, ELE_NEW);
1140                 }
1141         }
1142         
1143         BMO_Flag_To_Slot(bm, op, "edgeout", ELE_NEW, BM_EDGE);
1144
1145         BLI_array_free(edges1);
1146         BLI_array_free(edges2);
1147
1148 #undef EDGECON
1149 }
1150
1151 /*this is essentially new fkey*/
1152 void bmesh_contextual_create_exec(BMesh *bm, BMOperator *op)
1153 {
1154         BMOperator op2;
1155         BMOIter oiter;
1156         BMIter iter;
1157         BMHeader *h;
1158         BMVert *v, *verts[4];
1159         BMEdge *e;
1160         BMFace *f;
1161         int totv=0, tote=0, totf=0, amount;
1162
1163         /*count number of each element type we were passed*/
1164         BMO_ITER(h, &oiter, bm, op, "geom", BM_VERT|BM_EDGE|BM_FACE) {
1165                 switch (h->type) {
1166                         case BM_VERT: totv++; break;
1167                         case BM_EDGE: tote++; break;
1168                         case BM_FACE: totf++; break;
1169                 }
1170
1171                 BMO_SetFlag(bm, h, ELE_NEW);
1172         }
1173         
1174         /*call edgenet create*/
1175         /*  call edgenet prepare op so additional face creation cases work*/
1176         BMO_InitOpf(bm, &op2, "edgenet_prepare edges=%fe", ELE_NEW);
1177         BMO_Exec_Op(bm, &op2);
1178         BMO_Flag_Buffer(bm, &op2, "edgeout", ELE_NEW, BM_EDGE);
1179         BMO_Finish_Op(bm, &op2);
1180
1181         BMO_InitOpf(bm, &op2, "edgenet_fill edges=%fe", ELE_NEW);
1182         BMO_Exec_Op(bm, &op2);
1183
1184         /*return if edge net create did something*/
1185         if (BMO_CountSlotBuf(bm, &op2, "faceout")) {
1186                 BMO_CopySlot(&op2, op, "faceout", "faceout");
1187                 BMO_Finish_Op(bm, &op2);
1188                 return;
1189         }
1190
1191         BMO_Finish_Op(bm, &op2);
1192         
1193         /*now call dissolve faces*/
1194         BMO_InitOpf(bm, &op2, "dissolvefaces faces=%ff", ELE_NEW);
1195         BMO_Exec_Op(bm, &op2);
1196         
1197         /*if we dissolved anything, then return.*/
1198         if (BMO_CountSlotBuf(bm, &op2, "regionout")) {
1199                 BMO_CopySlot(&op2, op, "regionout", "faceout");
1200                 BMO_Finish_Op(bm, &op2);
1201                 return;
1202         }
1203
1204         BMO_Finish_Op(bm, &op2);
1205
1206         /*now, count how many verts we have*/
1207         amount = 0;
1208         BM_ITER(v, &iter, bm, BM_VERTS_OF_MESH, NULL) {
1209                 if (BMO_TestFlag(bm, v, ELE_NEW)) {
1210                         verts[amount] = v;
1211                         amount++;
1212
1213                         if (amount > 4) break;
1214                 }
1215         }
1216
1217         if (amount == 2) {
1218                 /*create edge*/
1219                 e = BM_Make_Edge(bm, verts[0], verts[1], NULL, 1);
1220                 BMO_SetFlag(bm, e, ELE_OUT);            
1221         } else if (amount == 3) {
1222                 /*create triangle*/
1223                 BM_Make_QuadTri(bm, verts[0], verts[1], verts[2], NULL, NULL, 1);
1224         } else if (amount == 4) {
1225                 f = NULL;
1226
1227                 /* the order of vertices can be anything, 6 cases to check */
1228                 if( is_quad_convex_v3(verts[0]->co, verts[1]->co, verts[2]->co, verts[3]->co) ) {
1229                         f= BM_Make_QuadTri(bm, verts[0], verts[1], verts[2], verts[3], NULL, 1);
1230                 }
1231                 else if( is_quad_convex_v3(verts[0]->co, verts[2]->co, verts[3]->co, verts[1]->co) ) {
1232                         f= BM_Make_QuadTri(bm, verts[0], verts[2], verts[3], verts[1], NULL, 1);
1233                 }
1234                 else if( is_quad_convex_v3(verts[0]->co, verts[2]->co, verts[1]->co, verts[3]->co) ) {
1235                         f= BM_Make_QuadTri(bm, verts[0], verts[2], verts[1], verts[3], NULL, 1);
1236                 }
1237                 else if( is_quad_convex_v3(verts[0]->co, verts[1]->co, verts[3]->co, verts[2]->co) ) {
1238                         f= BM_Make_QuadTri(bm, verts[0], verts[1], verts[3], verts[2], NULL, 1);
1239                 }
1240                 else if( is_quad_convex_v3(verts[0]->co, verts[3]->co, verts[2]->co, verts[1]->co) ) {
1241                         f= BM_Make_QuadTri(bm, verts[0], verts[3], verts[2], verts[1], NULL, 1);
1242                 }
1243                 else if( is_quad_convex_v3(verts[0]->co, verts[3]->co, verts[1]->co, verts[2]->co) ) {
1244                         f= BM_Make_QuadTri(bm, verts[0], verts[3], verts[1], verts[2], NULL, 1);
1245                 }
1246                 else {
1247                         printf("cannot find nice quad from concave set of vertices\n");
1248                 }
1249
1250                 if (f) BMO_SetFlag(bm, f, ELE_OUT);
1251         }
1252 }