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