doxygen: add newline after \file
[blender.git] / source / blender / bmesh / operators / bmo_edgenet.c
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  */
16
17 /** \file
18  * \ingroup bmesh
19  *
20  * Edge-Net for filling in open edge-loops.
21  */
22
23 #include "MEM_guardedalloc.h"
24
25 #include "BLI_math.h"
26 #include "BLI_array.h"
27
28 #include "bmesh.h"
29 #include "bmesh_tools.h"
30
31 #include "intern/bmesh_operators_private.h" /* own include */
32
33 #define EDGE_MARK       1
34 #define EDGE_VIS        2
35
36 #define ELE_NEW         1
37
38 void bmo_edgenet_fill_exec(BMesh *bm, BMOperator *op)
39 {
40         BMOperator op_attr;
41         BMOIter siter;
42         BMFace *f;
43         const short mat_nr        = BMO_slot_int_get(op->slots_in,  "mat_nr");
44         const bool use_smooth     = BMO_slot_bool_get(op->slots_in, "use_smooth");
45 //      const int sides           = BMO_slot_int_get(op->slots_in,  "sides");
46
47         if (!bm->totvert || !bm->totedge)
48                 return;
49
50         BM_mesh_elem_hflag_disable_all(bm, BM_EDGE, BM_ELEM_TAG, false);
51         BMO_slot_buffer_hflag_enable(bm, op->slots_in, "edges", BM_EDGE, BM_ELEM_TAG, false);
52
53         BM_mesh_elem_hflag_disable_all(bm, BM_FACE, BM_ELEM_TAG, false);
54         BM_mesh_edgenet(bm, true, true); // TODO, sides
55
56         BMO_slot_buffer_from_enabled_hflag(bm, op, op->slots_out, "faces.out", BM_FACE, BM_ELEM_TAG);
57
58         BMO_ITER (f, &siter, op->slots_out, "faces.out", BM_FACE) {
59                 f->mat_nr = mat_nr;
60                 if (use_smooth) {
61                         BM_elem_flag_enable(f, BM_ELEM_SMOOTH);
62                 }
63                 /* normals are zero'd */
64                 BM_face_normal_update(f);
65         }
66
67         /* --- Attribute Fill --- */
68         /* may as well since we have the faces already in a buffer */
69         BMO_op_initf(bm, &op_attr, op->flag,
70                      "face_attribute_fill faces=%S use_normals=%b use_data=%b",
71                      op, "faces.out", true, true);
72
73         BMO_op_exec(bm, &op_attr);
74
75         /* check if some faces couldn't be touched */
76         if (BMO_slot_buffer_count(op_attr.slots_out, "faces_fail.out")) {
77                 BMO_op_callf(bm, op->flag, "recalc_face_normals faces=%S", &op_attr, "faces_fail.out");
78         }
79         BMO_op_finish(bm, &op_attr);
80
81 }
82
83 static BMEdge *edge_next(BMesh *bm, BMEdge *e)
84 {
85         BMIter iter;
86         BMEdge *e2;
87         int i;
88
89         for (i = 0; i < 2; i++) {
90                 BM_ITER_ELEM (e2, &iter, i ? e->v2 : e->v1, BM_EDGES_OF_VERT) {
91                         if ((BMO_edge_flag_test(bm, e2, EDGE_MARK)) &&
92                             (BMO_edge_flag_test(bm, e2, EDGE_VIS) == false) &&
93                             (e2 != e))
94                         {
95                                 return e2;
96                         }
97                 }
98         }
99
100         return NULL;
101 }
102
103 void bmo_edgenet_prepare_exec(BMesh *bm, BMOperator *op)
104 {
105         BMOIter siter;
106         BMEdge *e;
107         BMEdge **edges1 = NULL, **edges2 = NULL, **edges;
108         BLI_array_declare(edges1);
109         BLI_array_declare(edges2);
110         BLI_array_declare(edges);
111         bool ok = true;
112         int i, count;
113
114         BMO_slot_buffer_flag_enable(bm, op->slots_in, "edges", BM_EDGE, EDGE_MARK);
115
116         /* validate that each edge has at most one other tagged edge in the
117          * disk cycle around each of it's vertices */
118         BMO_ITER (e, &siter, op->slots_in, "edges", BM_EDGE) {
119                 for (i = 0; i < 2; i++) {
120                         count = BMO_iter_elem_count_flag(bm,  BM_EDGES_OF_VERT, (i ? e->v2 : e->v1), EDGE_MARK, true);
121                         if (count > 2) {
122                                 ok = 0;
123                                 break;
124                         }
125                 }
126
127                 if (!ok) {
128                         break;
129                 }
130         }
131
132         /* we don't have valid edge layouts, retur */
133         if (!ok) {
134                 return;
135         }
136
137         /* find connected loops within the input edge */
138         count = 0;
139         while (1) {
140                 BMO_ITER (e, &siter, op->slots_in, "edges", BM_EDGE) {
141                         if (!BMO_edge_flag_test(bm, e, EDGE_VIS)) {
142                                 if (BMO_iter_elem_count_flag(bm, BM_EDGES_OF_VERT, e->v1, EDGE_MARK, true) == 1 ||
143                                     BMO_iter_elem_count_flag(bm, BM_EDGES_OF_VERT, e->v2, EDGE_MARK, true) == 1)
144                                 {
145                                         break;
146                                 }
147                         }
148                 }
149
150                 if (!e) {
151                         break;
152                 }
153
154                 if (!count) {
155                         edges = edges1;
156                 }
157                 else if (count == 1) {
158                         edges = edges2;
159                 }
160                 else {
161                         break;
162                 }
163
164                 i = 0;
165                 while (e) {
166                         BMO_edge_flag_enable(bm, e, EDGE_VIS);
167                         BLI_array_grow_one(edges);
168                         edges[i] = e;
169
170                         e = edge_next(bm, e);
171                         i++;
172                 }
173
174                 if (!count) {
175                         edges1 = edges;
176                         BLI_array_len_set(edges1, BLI_array_len(edges));
177                 }
178                 else {
179                         edges2 = edges;
180                         BLI_array_len_set(edges2, BLI_array_len(edges));
181                 }
182
183                 BLI_array_clear(edges);
184                 count++;
185         }
186
187         if (edges1 && BLI_array_len(edges1) > 2 &&
188             BM_edge_share_vert_check(edges1[0], edges1[BLI_array_len(edges1) - 1]))
189         {
190                 if (edges2 && BLI_array_len(edges2) > 2 &&
191                     BM_edge_share_vert_check(edges2[0], edges2[BLI_array_len(edges2) - 1]))
192                 {
193                         BLI_array_free(edges1);
194                         BLI_array_free(edges2);
195                         return;
196                 }
197                 else {
198                         edges1 = edges2;
199                         edges2 = NULL;
200                 }
201         }
202
203         if (edges2 && BLI_array_len(edges2) > 2 &&
204             BM_edge_share_vert_check(edges2[0], edges2[BLI_array_len(edges2) - 1]))
205         {
206                 edges2 = NULL;
207         }
208
209         /* two unconnected loops, connect the */
210         if (edges1 && edges2) {
211                 BMVert *v1, *v2, *v3, *v4;
212                 float dvec1[3];
213                 float dvec2[3];
214
215                 if (BLI_array_len(edges1) == 1) {
216                         v1 = edges1[0]->v1;
217                         v2 = edges1[0]->v2;
218                 }
219                 else {
220                         v1 = BM_vert_in_edge(edges1[1], edges1[0]->v1) ? edges1[0]->v2 : edges1[0]->v1;
221                         i  = BLI_array_len(edges1) - 1;
222                         v2 = BM_vert_in_edge(edges1[i - 1], edges1[i]->v1) ? edges1[i]->v2 : edges1[i]->v1;
223                 }
224
225                 if (BLI_array_len(edges2) == 1) {
226                         v3 = edges2[0]->v1;
227                         v4 = edges2[0]->v2;
228                 }
229                 else {
230                         v3 = BM_vert_in_edge(edges2[1], edges2[0]->v1) ? edges2[0]->v2 : edges2[0]->v1;
231                         i  = BLI_array_len(edges2) - 1;
232                         v4 = BM_vert_in_edge(edges2[i - 1], edges2[i]->v1) ? edges2[i]->v2 : edges2[i]->v1;
233                 }
234
235                 /* if there is ever bow-tie quads between two edges the problem is here! [#30367] */
236 #if 0
237                 normal_tri_v3(dvec1, v1->co, v2->co, v4->co);
238                 normal_tri_v3(dvec2, v1->co, v4->co, v3->co);
239 #else
240                 {
241                         /* save some CPU cycles and skip the sqrt and 1 subtraction */
242                         float a1[3], a2[3], a3[3];
243                         sub_v3_v3v3(a1, v1->co, v2->co);
244                         sub_v3_v3v3(a2, v1->co, v4->co);
245                         sub_v3_v3v3(a3, v1->co, v3->co);
246                         cross_v3_v3v3(dvec1, a1, a2);
247                         cross_v3_v3v3(dvec2, a2, a3);
248                 }
249 #endif
250                 if (dot_v3v3(dvec1, dvec2) < 0.0f) {
251                         SWAP(BMVert *, v3, v4);
252                 }
253
254                 e = BM_edge_create(bm, v1, v3, NULL, BM_CREATE_NO_DOUBLE);
255                 BMO_edge_flag_enable(bm, e, ELE_NEW);
256                 e = BM_edge_create(bm, v2, v4, NULL, BM_CREATE_NO_DOUBLE);
257                 BMO_edge_flag_enable(bm, e, ELE_NEW);
258         }
259         else if (edges1) {
260                 BMVert *v1, *v2;
261
262                 if (BLI_array_len(edges1) > 1) {
263                         v1 = BM_vert_in_edge(edges1[1], edges1[0]->v1) ? edges1[0]->v2 : edges1[0]->v1;
264                         i  = BLI_array_len(edges1) - 1;
265                         v2 = BM_vert_in_edge(edges1[i - 1], edges1[i]->v1) ? edges1[i]->v2 : edges1[i]->v1;
266                         e  = BM_edge_create(bm, v1, v2, NULL, BM_CREATE_NO_DOUBLE);
267                         BMO_edge_flag_enable(bm, e, ELE_NEW);
268                 }
269         }
270
271         BMO_slot_buffer_from_enabled_flag(bm, op, op->slots_out, "edges.out", BM_EDGE, ELE_NEW);
272
273         BLI_array_free(edges1);
274         BLI_array_free(edges2);
275 }