Modifiers: add minimum number of vertices to triangulate modifier.
[blender.git] / source / blender / bmesh / operators / bmo_triangulate.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  * Triangulate faces, also defines triangle fill.
21  */
22
23 #include "MEM_guardedalloc.h"
24
25 #include "DNA_listBase.h"
26
27 #include "BLI_math.h"
28 #include "BLI_sort_utils.h"
29 #include "BLI_scanfill.h"
30
31 #include "bmesh.h"
32 #include "bmesh_tools.h"
33 #include "intern/bmesh_operators_private.h"
34
35
36 #define ELE_NEW         1
37 #define EDGE_MARK       4
38
39 void bmo_triangulate_exec(BMesh *bm, BMOperator *op)
40 {
41         const int quad_method = BMO_slot_int_get(op->slots_in, "quad_method");
42         const int ngon_method = BMO_slot_int_get(op->slots_in, "ngon_method");
43
44         BMOpSlot *slot_facemap_out = BMO_slot_get(op->slots_out, "face_map.out");
45         BMOpSlot *slot_facemap_double_out = BMO_slot_get(op->slots_out, "face_map_double.out");
46
47         BM_mesh_elem_hflag_disable_all(bm, BM_FACE | BM_EDGE, BM_ELEM_TAG, false);
48         BMO_slot_buffer_hflag_enable(bm, op->slots_in, "faces", BM_FACE, BM_ELEM_TAG, false);
49
50         BM_mesh_triangulate(bm, quad_method, ngon_method, 4, true, op, slot_facemap_out, slot_facemap_double_out);
51
52         BMO_slot_buffer_from_enabled_hflag(bm, op, op->slots_out, "edges.out", BM_EDGE, BM_ELEM_TAG);
53         BMO_slot_buffer_from_enabled_hflag(bm, op, op->slots_out, "faces.out", BM_FACE, BM_ELEM_TAG);
54
55 }
56
57 struct SortNormal {
58         float value;  /* keep first */
59         float no[3];
60 };
61
62 void bmo_triangle_fill_exec(BMesh *bm, BMOperator *op)
63 {
64         const bool use_beauty = BMO_slot_bool_get(op->slots_in, "use_beauty");
65         const bool use_dissolve = BMO_slot_bool_get(op->slots_in, "use_dissolve");
66         BMOIter siter;
67         BMEdge *e;
68         ScanFillContext sf_ctx;
69         /* ScanFillEdge *sf_edge; */ /* UNUSED */
70         ScanFillFace *sf_tri;
71         GHash *sf_vert_map;
72         float normal[3];
73         const int scanfill_flag = BLI_SCANFILL_CALC_HOLES | BLI_SCANFILL_CALC_POLYS | BLI_SCANFILL_CALC_LOOSE;
74         uint nors_tot;
75         bool calc_winding = false;
76
77         sf_vert_map = BLI_ghash_ptr_new_ex(__func__, BMO_slot_buffer_count(op->slots_in, "edges"));
78
79         BMO_slot_vec_get(op->slots_in, "normal", normal);
80
81         BLI_scanfill_begin(&sf_ctx);
82
83         BMO_ITER (e, &siter, op->slots_in, "edges", BM_EDGE) {
84                 ScanFillVert *sf_verts[2];
85                 BMVert **e_verts = &e->v1;
86                 uint i;
87
88                 BMO_edge_flag_enable(bm, e, EDGE_MARK);
89
90                 calc_winding = (calc_winding || BM_edge_is_boundary(e));
91
92                 for (i = 0; i < 2; i++) {
93                         if ((sf_verts[i] = BLI_ghash_lookup(sf_vert_map, e_verts[i])) == NULL) {
94                                 sf_verts[i] = BLI_scanfill_vert_add(&sf_ctx, e_verts[i]->co);
95                                 sf_verts[i]->tmp.p = e_verts[i];
96                                 BLI_ghash_insert(sf_vert_map, e_verts[i], sf_verts[i]);
97                         }
98                 }
99
100                 /* sf_edge = */ BLI_scanfill_edge_add(&sf_ctx, UNPACK2(sf_verts));
101                 /* sf_edge->tmp.p = e; */ /* UNUSED */
102         }
103         nors_tot = BLI_ghash_len(sf_vert_map);
104         BLI_ghash_free(sf_vert_map, NULL, NULL);
105
106
107         if (is_zero_v3(normal)) {
108                 /* calculate the normal from the cross product of vert-edge pairs.
109                  * Since we don't know winding, just accumulate */
110                 ScanFillVert *sf_vert;
111                 struct SortNormal *nors;
112                 uint i;
113                 bool is_degenerate = true;
114
115                 nors = MEM_mallocN(sizeof(*nors) * nors_tot, __func__);
116
117                 for (sf_vert = sf_ctx.fillvertbase.first, i = 0; sf_vert; sf_vert = sf_vert->next, i++) {
118                         BMVert *v = sf_vert->tmp.p;
119                         BMIter eiter;
120                         BMEdge *e_pair[2];
121                         uint e_index = 0;
122
123                         nors[i].value = -1.0f;
124
125                         /* only use if 'is_degenerate' stays true */
126                         add_v3_v3(normal, v->no);
127
128                         BM_ITER_ELEM (e, &eiter, v, BM_EDGES_OF_VERT) {
129                                 if (BMO_edge_flag_test(bm, e, EDGE_MARK)) {
130                                         if (e_index == 2) {
131                                                 e_index = 0;
132                                                 break;
133                                         }
134                                         e_pair[e_index++] = e;
135                                 }
136                         }
137
138                         if (e_index == 2) {
139                                 float dir_a[3], dir_b[3];
140
141                                 is_degenerate = false;
142
143                                 sub_v3_v3v3(dir_a, v->co, BM_edge_other_vert(e_pair[0], v)->co);
144                                 sub_v3_v3v3(dir_b, v->co, BM_edge_other_vert(e_pair[1], v)->co);
145
146                                 cross_v3_v3v3(nors[i].no, dir_a, dir_b);
147                                 nors[i].value = len_squared_v3(nors[i].no);
148
149                                 /* only to get deterministic behavior (for initial normal) */
150                                 if (len_squared_v3(dir_a) > len_squared_v3(dir_b)) {
151                                         negate_v3(nors[i].no);
152                                 }
153                         }
154                 }
155
156                 if (UNLIKELY(is_degenerate)) {
157                         /* no vertices have 2 edges?
158                          * in this case fall back to the average vertex normals */
159                 }
160                 else {
161                         qsort(nors, nors_tot, sizeof(*nors), BLI_sortutil_cmp_float_reverse);
162
163                         copy_v3_v3(normal, nors[0].no);
164                         for (i = 0; i < nors_tot; i++) {
165                                 if (UNLIKELY(nors[i].value == -1.0f)) {
166                                         break;
167                                 }
168                                 if (dot_v3v3(normal, nors[i].no) < 0.0f) {
169                                         negate_v3(nors[i].no);
170                                 }
171                                 add_v3_v3(normal, nors[i].no);
172                         }
173                         normalize_v3(normal);
174                 }
175
176                 MEM_freeN(nors);
177         }
178         else {
179                 calc_winding = false;
180         }
181
182         /* in this case we almost certainly have degenerate geometry,
183          * better set a fallback value as a last resort */
184         if (UNLIKELY(normalize_v3(normal) == 0.0f)) {
185                 normal[2] = 1.0f;
186         }
187
188         BLI_scanfill_calc_ex(&sf_ctx, scanfill_flag, normal);
189
190
191         /* if we have existing faces, base winding on those */
192         if (calc_winding) {
193                 int winding_votes = 0;
194                 for (sf_tri = sf_ctx.fillfacebase.first; sf_tri; sf_tri = sf_tri->next) {
195                         BMVert *v_tri[3] = {sf_tri->v1->tmp.p, sf_tri->v2->tmp.p, sf_tri->v3->tmp.p};
196                         uint i, i_prev;
197
198                         for (i = 0, i_prev = 2; i < 3; i_prev = i++) {
199                                 e = BM_edge_exists(v_tri[i], v_tri[i_prev]);
200                                 if (e && BM_edge_is_boundary(e) && BMO_edge_flag_test(bm, e, EDGE_MARK)) {
201                                         winding_votes += (e->l->v == v_tri[i]) ? 1 : -1;
202                                 }
203                         }
204                 }
205
206                 if (winding_votes < 0) {
207                         for (sf_tri = sf_ctx.fillfacebase.first; sf_tri; sf_tri = sf_tri->next) {
208                                 SWAP(struct ScanFillVert *, sf_tri->v2, sf_tri->v3);
209                         }
210                 }
211         }
212
213
214         for (sf_tri = sf_ctx.fillfacebase.first; sf_tri; sf_tri = sf_tri->next) {
215                 BMFace *f;
216                 BMLoop *l;
217                 BMIter liter;
218
219                 f = BM_face_create_quad_tri(bm,
220                                             sf_tri->v1->tmp.p, sf_tri->v2->tmp.p, sf_tri->v3->tmp.p, NULL,
221                                             NULL, BM_CREATE_NO_DOUBLE);
222
223                 BMO_face_flag_enable(bm, f, ELE_NEW);
224                 BM_ITER_ELEM (l, &liter, f, BM_LOOPS_OF_FACE) {
225                         if (!BMO_edge_flag_test(bm, l->e, EDGE_MARK)) {
226                                 BMO_edge_flag_enable(bm, l->e, ELE_NEW);
227                         }
228                 }
229         }
230
231         BLI_scanfill_end(&sf_ctx);
232
233         if (use_beauty) {
234                 BMOperator bmop;
235
236                 BMO_op_initf(bm, &bmop, op->flag, "beautify_fill faces=%ff edges=%Fe", ELE_NEW, EDGE_MARK);
237                 BMO_op_exec(bm, &bmop);
238                 BMO_slot_buffer_flag_enable(bm, bmop.slots_out, "geom.out", BM_FACE | BM_EDGE, ELE_NEW);
239                 BMO_op_finish(bm, &bmop);
240         }
241
242         if (use_dissolve) {
243                 BMEdge *e_next;
244                 BMIter iter;
245
246                 BM_ITER_MESH_MUTABLE (e, e_next, &iter, bm, BM_EDGES_OF_MESH) {
247                         if (BMO_edge_flag_test(bm, e, ELE_NEW)) {
248                                 /* in rare cases the edges face will have already been removed from the edge */
249                                 if (LIKELY(BM_edge_is_manifold(e))) {
250                                         BMFace *f_new = BM_faces_join_pair(bm, e->l, e->l->radial_next, false);
251                                         if (f_new) {
252                                                 BMO_face_flag_enable(bm, f_new, ELE_NEW);
253                                                 BM_edge_kill(bm, e);
254                                         }
255                                         else {
256                                                 BMO_error_clear(bm);
257                                         }
258                                 }
259                                 else if (e->l == NULL) {
260                                         BM_edge_kill(bm, e);
261                                 }
262                                 else {
263                                         /* Edges with 1 or 3+ faces attached,
264                                          * most likely caused by a degenerate mesh. */
265                                 }
266                         }
267                 }
268         }
269
270         BMO_slot_buffer_from_enabled_flag(bm, op, op->slots_out, "geom.out", BM_EDGE | BM_FACE, ELE_NEW);
271 }