move bmesh tools into their own include,
[blender.git] / source / blender / bmesh / tools / bmesh_edgenet.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * Contributor(s): Campbell Barton
19  *
20  * ***** END GPL LICENSE BLOCK *****
21  */
22
23 /** \file blender/bmesh/tools/bmesh_edgenet.c
24  *  \ingroup bmesh
25  *
26  * Edgenet Fill.
27  *
28  */
29
30 #include <limits.h>
31
32 #include "MEM_guardedalloc.h"
33
34 #include "BLI_utildefines.h"
35 #include "BLI_alloca.h"
36 #include "BLI_mempool.h"
37 #include "BLI_linklist.h"
38
39 #include "bmesh.h"
40 #include "bmesh_edgenet.h"  /* own include */
41
42 #ifdef __GNUC__
43 #  pragma GCC diagnostic error "-Wsign-conversion"
44 #  if (__GNUC__ * 100 + __GNUC_MINOR__) >= 406  /* gcc4.6+ only */
45 #    pragma GCC diagnostic error "-Wsign-compare"
46 #    pragma GCC diagnostic error "-Wconversion"
47 #  endif
48 #endif
49
50 /* Data for one end of an edge involved in a bevel */
51 typedef struct VertNetInfo {
52         BMVert *prev;               /* previous vertex */
53         int pass;                   /* path scanning pass value, for internal calculation */
54         int face;                   /* face index connected to the edge between this and the previous vert */
55         int flag;                   /* flag */
56 } VertNetInfo;
57
58 enum {
59         VNINFO_FLAG_IS_MIXFACE = (1 << 0),
60 };
61
62 /**
63  * Check if this edge can be used in a path.
64  */
65 static bool bm_edge_step_ok(BMEdge *e)
66 {
67         return BM_elem_flag_test(e, BM_ELEM_TAG) && ((e->l == NULL) || (e->l->radial_next == e->l));
68 }
69
70 static int bm_edge_face(BMEdge *e)
71 {
72         return e->l ? BM_elem_index_get(e->l->f) : -1;
73 }
74
75 /**
76  * Get the next available edge we can use to attempt tp calculate a path from.
77  */
78 static BMEdge *bm_edgenet_edge_get_next(
79         BMesh *bm,
80         LinkNode **edge_queue, BLI_mempool *edge_queue_pool)
81 {
82         BMEdge *e;
83         BMIter iter;
84
85         while (*edge_queue) {
86                 e = BLI_linklist_pop_pool(edge_queue, edge_queue_pool);
87                 if (bm_edge_step_ok(e)) {
88                         return e;
89                 }
90         }
91
92         BM_ITER_MESH (e, &iter, bm, BM_EDGES_OF_MESH) {
93                 if (bm_edge_step_ok(e)) {
94                         return e;
95                 }
96         }
97
98         return NULL;
99 }
100
101
102 /**
103  * Edge loops are built up using links to the 'prev' member.
104  * with each side of the loop having its own pass (negated from the other).
105  *
106  * This function returns half a loop, the caller needs to run twice to get both sides.
107  */
108 static unsigned int bm_edgenet_path_from_pass(
109         BMVert *v, LinkNode **v_ls,
110         VertNetInfo *vnet_info, BLI_mempool *path_pool)
111 {
112         VertNetInfo *vn = &vnet_info[BM_elem_index_get(v)];
113         const int pass = vn->pass;
114         unsigned int v_ls_tot = 0;
115
116         do {
117                 BLI_linklist_prepend_pool(v_ls, v, path_pool);
118                 v_ls_tot += 1;
119                 v = vn->prev;
120                 vn = &vnet_info[BM_elem_index_get(v)];
121         } while (vn->pass == pass);
122
123         return v_ls_tot;
124 }
125
126 /**
127  * Specialized wrapper for #BM_face_exists_overlap_subset
128  * that gets the verts from a path before we allocate it in the correct order.
129  */
130 static bool bm_edgenet_path_check_overlap(
131         BMVert *v1, BMVert *v2,
132         VertNetInfo *vnet_info)
133 {
134         /* vert order doesn't matter */
135         unsigned int v_ls_tot = 0;
136         LinkNode *v_ls = NULL;
137         BMVert *v_pair[2] = {v1, v2};
138         unsigned int i;
139
140         for (i = 0; i < 2; i++) {
141                 BMVert *v = v_pair[i];
142                 VertNetInfo *vn = &vnet_info[BM_elem_index_get(v)];
143                 const int pass = vn->pass;
144                 do {
145                         BLI_linklist_prepend_alloca(&v_ls, v);
146                         v_ls_tot += 1;
147                         v = vn->prev;
148                         vn = &vnet_info[BM_elem_index_get(v)];
149                 } while (vn->pass == pass);
150         }
151
152         if (v_ls_tot) {
153                 BMVert **vert_arr = BLI_array_alloca(vert_arr, v_ls_tot);
154                 LinkNode *v_lnk;
155                 for (i = 0, v_lnk = v_ls; i < v_ls_tot; v_lnk = v_lnk->next, i++) {
156                         vert_arr[i] = v_lnk->link;
157                 }
158
159                 return BM_face_exists_overlap_subset(vert_arr, (int)v_ls_tot);
160         }
161         else {
162                 return false;
163         }
164 }
165
166 /**
167  * Create a face from the path.
168  */
169 static BMFace *bm_edgenet_face_from_path(
170         BMesh *bm, LinkNode *path, const unsigned int path_len)
171 {
172         BMFace *f;
173         LinkNode *v_lnk;
174         unsigned int i;
175         unsigned int i_prev;
176
177         BMVert **vert_arr = BLI_array_alloca(vert_arr, path_len);
178         BMEdge **edge_arr = BLI_array_alloca(edge_arr, path_len);
179
180         for (v_lnk = path, i = 0; v_lnk; v_lnk = v_lnk->next, i++) {
181                 vert_arr[i] = v_lnk->link;
182         }
183
184         i_prev = path_len - 1;
185         for (i = 0; i < path_len; i++) {
186                 edge_arr[i_prev] = BM_edge_exists(vert_arr[i], vert_arr[i_prev]);
187                 i_prev = i;
188         }
189
190         /* no need for this, we do overlap checks before allowing the path to be used */
191 #if 0
192         if (BM_face_exists_multi(vert_arr, edge_arr, path_len)) {
193                 return NULL;
194         }
195 #endif
196
197         f = BM_face_create(bm, vert_arr, edge_arr, (int)path_len, NULL, BM_CREATE_NOP);
198
199         return f;
200 }
201
202 /**
203  * Step along the path from \a v_curr to any vert not already in the path.
204  *
205  * \return The connecting edge if the path is found, otherwise NULL.
206  */
207 static BMEdge *bm_edgenet_path_step(
208         BMVert *v_curr, LinkNode **v_ls,
209         VertNetInfo *vnet_info, BLI_mempool *path_pool)
210 {
211         const VertNetInfo *vn_curr = &vnet_info[BM_elem_index_get(v_curr)];
212
213         BMEdge *e;
214         BMIter iter;
215         unsigned int tot = 0;
216         unsigned int v_ls_tot = 0;
217
218         BM_ITER_ELEM (e, &iter, v_curr, BM_EDGES_OF_VERT) {
219                 BMVert *v_next = BM_edge_other_vert(e, v_curr);
220                 if (v_next != vn_curr->prev) {
221                         if (bm_edge_step_ok(e)) {
222                                 VertNetInfo *vn_next = &vnet_info[BM_elem_index_get(v_next)];
223
224                                 /* check we're not looping back on ourselves */
225                                 if (vn_curr->pass != vn_next->pass) {
226
227                                         if (vn_curr->pass == -vn_next->pass) {
228                                                 if ((vn_curr->flag & VNINFO_FLAG_IS_MIXFACE) ||
229                                                         (vn_next->flag & VNINFO_FLAG_IS_MIXFACE))
230                                                 {
231                                                         /* found connecting edge */
232                                                         if (bm_edgenet_path_check_overlap(v_curr, v_next, vnet_info) == false) {
233                                                                 return e;
234                                                         }
235                                                 }
236                                         }
237                                         else {
238                                                 vn_next->face = bm_edge_face(e);
239                                                 vn_next->pass = vn_curr->pass;
240                                                 vn_next->prev = v_curr;
241
242                                                 /* flush flag down the path */
243                                                 vn_next->flag &= ~VNINFO_FLAG_IS_MIXFACE;
244                                                 if ((vn_curr->flag & VNINFO_FLAG_IS_MIXFACE) ||
245                                                         (vn_next->face == -1) ||
246                                                         (vn_next->face != vn_curr->face))
247                                                 {
248                                                         vn_next->flag |= VNINFO_FLAG_IS_MIXFACE;
249                                                 }
250
251                                                 /* add to the list! */
252                                                 BLI_linklist_prepend_pool(v_ls, v_next, path_pool);
253                                                 v_ls_tot += 1;
254                                         }
255                                 }
256                         }
257                         tot += 1;
258                 }
259         }
260
261         /* trick to walk along wire-edge paths */
262         if (v_ls_tot == 1 && tot == 1) {
263                 v_curr = BLI_linklist_pop_pool(v_ls, path_pool);
264                 bm_edgenet_path_step(v_curr, v_ls, vnet_info, path_pool);
265         }
266
267         return NULL;
268 }
269
270 /**
271  * Given an edge, find the first path that can form a face.
272  *
273  * \return A linked list of verts.
274  */
275 static LinkNode *bm_edgenet_path_calc(
276         BMEdge *e, const int pass_nr, const unsigned int path_cost_max,
277         unsigned int *r_path_len, unsigned int *r_path_cost,
278         VertNetInfo *vnet_info, BLI_mempool *path_pool)
279 {
280         VertNetInfo *vn_1, *vn_2;
281         const int f_index = bm_edge_face(e);
282         bool found;
283
284         LinkNode *v_ls_prev = NULL;
285         LinkNode *v_ls_next = NULL;
286
287         unsigned int path_cost_accum = 0;
288
289         BLI_assert(bm_edge_step_ok(e));
290
291         *r_path_len = 0;
292         *r_path_cost = 0;
293
294         vn_1 = &vnet_info[BM_elem_index_get(e->v1)];
295         vn_2 = &vnet_info[BM_elem_index_get(e->v2)];
296
297         vn_1->pass =  pass_nr;
298         vn_2->pass = -pass_nr;
299
300         vn_1->prev = e->v2;
301         vn_2->prev = e->v1;
302
303         vn_1->face =
304         vn_2->face = f_index;
305
306         vn_1->flag =
307         vn_2->flag = (f_index == -1) ? VNINFO_FLAG_IS_MIXFACE : 0;
308
309         /* prime the searchlist */
310         BLI_linklist_prepend_pool(&v_ls_prev, e->v1, path_pool);
311         BLI_linklist_prepend_pool(&v_ls_prev, e->v2, path_pool);
312
313         do {
314                 found = false;
315
316                 /* no point to continue, we're over budget */
317                 if (path_cost_accum >= path_cost_max) {
318                         BLI_linklist_free_pool(v_ls_next, NULL, path_pool);
319                         BLI_linklist_free_pool(v_ls_prev, NULL, path_pool);
320                         return NULL;
321                 }
322
323                 while (v_ls_prev) {
324                         const LinkNode *v_ls_next_old = v_ls_next;
325                         BMVert *v = BLI_linklist_pop_pool(&v_ls_prev, path_pool);
326                         BMEdge *e_found = bm_edgenet_path_step(v, &v_ls_next, vnet_info, path_pool);
327
328                         if (e_found) {
329                                 LinkNode *path = NULL;
330                                 unsigned int path_len;
331                                 BLI_linklist_free_pool(v_ls_next, NULL, path_pool);
332                                 BLI_linklist_free_pool(v_ls_prev, NULL, path_pool);
333
334                                 // BLI_assert(BLI_mempool_count(path_pool) == 0);
335
336                                 path_len = bm_edgenet_path_from_pass(e_found->v1, &path, vnet_info, path_pool);
337                                 BLI_linklist_reverse(&path);
338                                 path_len += bm_edgenet_path_from_pass(e_found->v2, &path, vnet_info, path_pool);
339                                 *r_path_len = path_len;
340                                 *r_path_cost = path_cost_accum;
341                                 return path;
342                         }
343                         else {
344                                 /* check if a change was made */
345                                 if (v_ls_next_old != v_ls_next) {
346                                         found = true;
347                                 }
348                         }
349
350                 }
351                 BLI_assert(v_ls_prev == NULL);
352
353                 path_cost_accum++;
354
355                 /* swap */
356                 v_ls_prev = v_ls_next;
357                 v_ls_next = NULL;
358
359         } while (found);
360
361         BLI_assert(v_ls_prev == NULL);
362         BLI_assert(v_ls_next == NULL);
363
364         /* tag not to search again */
365         BM_elem_flag_disable(e, BM_ELEM_TAG);
366
367         return NULL;
368 }
369
370 /**
371  * Wrapper for #bm_edgenet_path_calc which ensures all included edges
372  * _don't_ have a better option.
373  */
374 static LinkNode *bm_edgenet_path_calc_best(
375         BMEdge *e, int *pass_nr, unsigned int path_cost_max,
376         unsigned int *r_path_len, unsigned int *r_path_cost,
377         VertNetInfo *vnet_info, BLI_mempool *path_pool)
378 {
379         LinkNode *path;
380         unsigned int path_cost;
381
382         path = bm_edgenet_path_calc(e, *pass_nr, path_cost_max,
383                                     r_path_len, &path_cost,
384                                     vnet_info, path_pool);
385         (*pass_nr)++;
386
387         if (path == NULL) {
388                 return NULL;
389         }
390         else if (path_cost <= 1) {
391                 /* any face that takes 1-2 iterations to find we consider valid */
392                 return path;
393         }
394         else {
395                 /* Check every edge to see if any can give a better path.
396                  * This avoids very strange/long paths from being created. */
397
398                 const unsigned int path_len = *r_path_len;
399                 unsigned int i, i_prev;
400                 BMVert **vert_arr = BLI_array_alloca(vert_arr, path_len);
401                 LinkNode *v_lnk;
402
403                 for (v_lnk = path, i = 0; v_lnk; v_lnk = v_lnk->next, i++) {
404                         vert_arr[i] = v_lnk->link;
405                 }
406
407                 i_prev = path_len - 1;
408                 for (i = 0; i < path_len; i++) {
409                         BMEdge *e_other = BM_edge_exists(vert_arr[i], vert_arr[i_prev]);
410                         if (e_other != e) {
411                                 LinkNode *path_test;
412                                 unsigned int path_len_test;
413                                 unsigned int path_cost_test;
414
415                                 path_test = bm_edgenet_path_calc(e_other, *pass_nr, path_cost,
416                                                                  &path_len_test, &path_cost_test,
417                                                                  vnet_info, path_pool);
418                                 (*pass_nr)++;
419
420                                 if (path_test) {
421                                         BLI_assert(path_cost_test < path_cost);
422
423                                         BLI_linklist_free_pool(path, NULL, path_pool);
424                                         path = path_test;
425                                         *r_path_len = path_len_test;
426                                         *r_path_cost = path_cost_test;
427                                         path_cost = path_cost_test;
428                                 }
429                         }
430
431                         i_prev = i;
432                 }
433         }
434         return path;
435 }
436
437 /**
438  * Fill in faces from an edgenet made up of boundary and wire edges.
439  *
440  * \note New faces currently don't have their normals calculated and are flipped randomly.
441  *       The caller needs to flip faces correctly.
442  *
443  * \param bm  The mesh to operate on.
444  * \param use_edge_tag  Only fill tagged edges.
445  * \param face_oflag  if nonzero, apply all new faces with this bmo flag.
446  */
447 void BM_mesh_edgenet(BMesh *bm,
448                      const bool use_edge_tag, const bool use_new_face_tag)
449 {
450         VertNetInfo *vnet_info = MEM_callocN(sizeof(*vnet_info) * (size_t)bm->totvert, __func__);
451         BLI_mempool *edge_queue_pool = BLI_mempool_create(sizeof(LinkNode), 1, 512, 0);
452         BLI_mempool *path_pool = BLI_mempool_create(sizeof(LinkNode), 1, 512, 0);
453         LinkNode *edge_queue = NULL;
454
455         BMEdge *e;
456         BMIter iter;
457
458         int pass_nr = 1;
459
460         if (use_edge_tag == false) {
461                 BM_ITER_MESH (e, &iter, bm, BM_EDGES_OF_MESH) {
462                         BM_elem_flag_enable(e, BM_ELEM_TAG);
463                         BM_elem_flag_set(e, BM_ELEM_TAG, bm_edge_step_ok(e));
464                 }
465         }
466
467         BM_mesh_elem_index_ensure(bm, BM_VERT | BM_FACE);
468
469         while (true) {
470                 LinkNode *path = NULL;
471                 unsigned int path_len;
472                 unsigned int path_cost;
473
474                 e = bm_edgenet_edge_get_next(bm, &edge_queue, edge_queue_pool);
475                 if (e == NULL) {
476                         break;
477                 }
478
479                 BLI_assert(bm_edge_step_ok(e) == true);
480
481                 path = bm_edgenet_path_calc_best(e, &pass_nr, UINT_MAX,
482                                                  &path_len, &path_cost,
483                                                  vnet_info, path_pool);
484
485                 if (path) {
486                         BMFace *f = bm_edgenet_face_from_path(bm, path, path_len);
487                         /* queue edges to operate on */
488                         BMLoop *l_first, *l_iter;
489                         l_iter = l_first = BM_FACE_FIRST_LOOP(f);
490                         do {
491                                 if (bm_edge_step_ok(l_iter->e)) {
492                                         BLI_linklist_prepend_pool(&edge_queue, l_iter->e, edge_queue_pool);
493                                 }
494                         } while ((l_iter = l_iter->next) != l_first);
495
496                         if (use_new_face_tag) {
497                                 BM_elem_flag_enable(f, BM_ELEM_TAG);
498                         }
499
500                         /* the face index only needs to be unique, not kept valid */
501                         BM_elem_index_set(f, bm->totface - 1);  /* set_dirty */
502                 }
503
504                 BLI_linklist_free_pool(path, NULL, path_pool);
505                 BLI_assert(BLI_mempool_count(path_pool) == 0);
506         }
507
508         bm->elem_index_dirty |= BM_FACE;
509
510         BLI_mempool_destroy(edge_queue_pool);
511         BLI_mempool_destroy(path_pool);
512         MEM_freeN(vnet_info);
513 }