code cleanup: redundant includes and add minor comments.
[blender.git] / source / blender / bmesh / operators / bmo_fill_attribute.c
1
2 /*
3  * ***** BEGIN GPL LICENSE BLOCK *****
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  *
19  * Contributor(s): Campbell Barton.
20  *
21  * ***** END GPL LICENSE BLOCK *****
22  */
23
24 /** \file blender/bmesh/operators/bmo_fill_attribute.c
25  *  \ingroup bmesh
26  *
27  * Fill in geometry with the attributes of their adjacent data.
28  */
29
30 #include "BLI_utildefines.h"
31 #include "BLI_linklist_stack.h"
32
33 #include "bmesh.h"
34
35 #include "intern/bmesh_operators_private.h" /* own include */
36
37 /**
38  * Check if all other loops are tagged.
39  */
40 static bool bm_loop_is_all_radial_tag(BMLoop *l)
41 {
42         BMLoop *l_iter;
43         l_iter = l->radial_next;
44         do {
45                 if (BM_elem_flag_test(l_iter->f, BM_ELEM_TAG) == 0) {
46                         return false;
47                 }
48         } while ((l_iter = l_iter->radial_next) != l);
49
50         return true;
51 }
52
53 /**
54  * Callback to run on source-loops for #BM_face_copy_shared
55  */
56 static bool bm_loop_is_face_untag(BMElem *ele, void *UNUSED(user_data))
57 {
58         return (BM_elem_flag_test(((BMLoop *)ele)->f, BM_ELEM_TAG) == 0);
59 }
60
61 /**
62  * Copy all attributes from adjacent untagged faces.
63  */
64 static void bm_face_copy_shared_all(BMesh *bm, BMLoop *l,
65                                     const bool use_normals, const bool use_data)
66 {
67         BMLoop *l_other = l->radial_next;
68         BMFace *f = l->f, *f_other;
69         while (BM_elem_flag_test(l_other->f, BM_ELEM_TAG)) {
70                 l_other = l_other->radial_next;
71         }
72         f_other = l_other->f;
73
74         if (use_data) {
75                 /* copy face-attrs */
76                 BM_elem_attrs_copy(bm, bm, f_other, f);
77
78                 /* copy loop-attrs */
79                 BM_face_copy_shared(bm, f, bm_loop_is_face_untag, NULL);
80         }
81
82         if (use_normals) {
83                 /* copy winding (flipping) */
84                 if (l->v == l_other->v) {
85                         BM_face_normal_flip(bm, f);
86                 }
87         }
88 }
89
90 /**
91  * Flood fill attributes.
92  */
93 static unsigned int bmesh_face_attribute_fill(BMesh *bm,
94                                               const bool use_normals, const bool use_data)
95 {
96         BLI_LINKSTACK_DECLARE(loop_queue_prev, BMLoop *);
97         BLI_LINKSTACK_DECLARE(loop_queue_next, BMLoop *);
98
99         BMFace *f;
100         BMIter iter;
101         BMLoop *l;
102
103         unsigned int face_tot = 0;
104
105
106         BLI_LINKSTACK_INIT(loop_queue_prev);
107         BLI_LINKSTACK_INIT(loop_queue_next);
108
109         BM_ITER_MESH (f, &iter, bm, BM_FACES_OF_MESH) {
110                 if (BM_elem_flag_test(f, BM_ELEM_TAG)) {
111                         BMLoop *l_iter, *l_first;
112                         l_iter = l_first = BM_FACE_FIRST_LOOP(f);
113                         do {
114                                 if (bm_loop_is_all_radial_tag(l_iter) == false) {
115                                         BLI_LINKSTACK_PUSH(loop_queue_prev, l_iter);
116                                 }
117                         } while ((l_iter = l_iter->next) != l_first);
118                 }
119         }
120
121         while (BLI_LINKSTACK_SIZE(loop_queue_prev)) {
122                 while ((l = BLI_LINKSTACK_POP(loop_queue_prev))) {
123                         /* check we're still un-assigned */
124                         if (BM_elem_flag_test(l->f, BM_ELEM_TAG)) {
125                                 BMLoop *l_iter;
126
127                                 BM_elem_flag_disable(l->f, BM_ELEM_TAG);
128
129                                 l_iter = l->next;
130                                 do {
131                                         BMLoop *l_radial_iter = l_iter->radial_next;
132                                         if (l_radial_iter != l_iter) {
133                                                 do {
134                                                         if (BM_elem_flag_test(l_radial_iter->f, BM_ELEM_TAG)) {
135                                                                 BLI_LINKSTACK_PUSH(loop_queue_next, l_radial_iter);
136                                                         }
137                                                 } while ((l_radial_iter = l_radial_iter->radial_next) != l_iter);
138                                         }
139                                 } while ((l_iter = l_iter->next) != l);
140
141                                 /* do last because of face flipping */
142                                 bm_face_copy_shared_all(bm, l,
143                                                         use_normals, use_data);
144                                 face_tot += 1;
145                         }
146                 }
147
148                 BLI_LINKSTACK_SWAP(loop_queue_prev, loop_queue_next);
149         }
150
151         BLI_LINKSTACK_FREE(loop_queue_prev);
152         BLI_LINKSTACK_FREE(loop_queue_next);
153
154         return face_tot;
155 }
156
157 void bmo_face_attribute_fill_exec(BMesh *bm, BMOperator *op)
158 {
159         const bool use_normals = BMO_slot_bool_get(op->slots_in, "use_normals");
160         const bool use_data = BMO_slot_bool_get(op->slots_in, "use_data");
161
162         int face_tot;
163
164         BM_mesh_elem_hflag_disable_all(bm, BM_FACE, BM_ELEM_TAG, false);
165         BMO_slot_buffer_hflag_enable(bm, op->slots_in, "faces", BM_FACE, BM_ELEM_TAG, false);  /* do inline */
166
167         /* now we can copy adjacent data */
168         face_tot = bmesh_face_attribute_fill(bm, use_normals, use_data);
169
170         if (face_tot != BMO_slot_buffer_count(op->slots_in, "faces")) {
171                 /* any remaining tags will be skipped */
172                 BMO_slot_buffer_from_enabled_hflag(bm, op, op->slots_out, "faces_fail.out", BM_FACE, BM_ELEM_TAG);
173         }
174 }