add beauty option for triangle fill since you might want to use the initial scanfill...
[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
27 #include "MEM_guardedalloc.h"
28 #include "DNA_listBase.h"
29
30 #include "BLI_math.h"
31 #include "BLI_array.h"
32 #include "BLI_smallhash.h"
33 #include "BLI_scanfill.h"
34
35 #include "bmesh.h"
36 #include "intern/bmesh_private.h"
37
38 #include "intern/bmesh_operators_private.h" /* own include */
39
40 #define ELE_NEW         1
41 #define FACE_MARK       2
42 #define EDGE_MARK       4
43
44 void bmo_triangulate_exec(BMesh *bm, BMOperator *op)
45 {
46         const bool use_beauty = BMO_slot_bool_get(op->slots_in, "use_beauty");
47         BMOpSlot *slot_facemap_out = BMO_slot_get(op->slots_out, "face_map.out");
48
49         BM_mesh_elem_hflag_disable_all(bm, BM_FACE | BM_EDGE, BM_ELEM_TAG, false);
50         BMO_slot_buffer_hflag_enable(bm, op->slots_in, "faces", BM_FACE, BM_ELEM_TAG, false);
51
52         BM_mesh_triangulate(bm, use_beauty, true, op, slot_facemap_out);
53
54         BMO_slot_buffer_from_enabled_hflag(bm, op, op->slots_out, "edges.out", BM_EDGE, BM_ELEM_TAG);
55         BMO_slot_buffer_from_enabled_hflag(bm, op, op->slots_out, "faces.out", BM_FACE, BM_ELEM_TAG);
56 }
57
58 void bmo_beautify_fill_exec(BMesh *bm, BMOperator *op)
59 {
60         BMOIter siter;
61         BMIter iter;
62         BMFace *f;
63         BMEdge *e;
64         int stop = 0;
65         
66         BMO_slot_buffer_flag_enable(bm, op->slots_in, "edges", BM_EDGE, EDGE_MARK);
67         
68         BMO_ITER (f, &siter, op->slots_in, "faces", BM_FACE) {
69                 if (f->len == 3) {
70                         BMO_elem_flag_enable(bm, f, FACE_MARK);
71                 }
72         }
73
74         while (!stop) {
75                 stop = 1;
76                 
77                 BM_ITER_MESH (e, &iter, bm, BM_EDGES_OF_MESH) {
78                         float v1_xy[2], v2_xy[2], v3_xy[2], v4_xy[2];
79                         float no[3];
80                         float axis_mat[3][3];
81                         
82                         if (!BM_edge_is_manifold(e) || !BMO_elem_flag_test(bm, e, EDGE_MARK)) {
83                                 continue;
84                         }
85
86                         if (!BMO_elem_flag_test(bm, e->l->f, FACE_MARK) ||
87                             !BMO_elem_flag_test(bm, e->l->radial_next->f, FACE_MARK))
88                         {
89                                 continue;
90                         }
91                         
92                         {
93                                 float *v1, *v2, *v3, *v4;
94                                 float no_a[3], no_b[3];
95                                 v1 = e->l->prev->v->co;
96                                 v2 = e->l->v->co;
97                                 v3 = e->l->radial_next->prev->v->co;
98                                 v4 = e->l->next->v->co;
99
100                                 normal_tri_v3(no_a, v1, v2, v3);
101                                 normal_tri_v3(no_b, v1, v3, v4);
102                                 add_v3_v3v3(no, no_a, no_b);
103                                 normalize_v3(no);
104                                 axis_dominant_v3_to_m3(axis_mat, no);
105                                 mul_v2_m3v3(v1_xy, axis_mat, v1);
106                                 mul_v2_m3v3(v2_xy, axis_mat, v2);
107                                 mul_v2_m3v3(v3_xy, axis_mat, v3);
108                                 mul_v2_m3v3(v4_xy, axis_mat, v4);
109                         }
110
111                         if (is_quad_convex_v2(v1_xy, v2_xy, v3_xy, v4_xy)) {
112                                 float len1, len2, len3, len4, len5, len6, opp1, opp2, fac1, fac2;
113                                 /* testing rule:
114                                  * the area divided by the total edge lengths
115                                  */
116                                 len1 = len_v2v2(v1_xy, v2_xy);
117                                 len2 = len_v2v2(v2_xy, v3_xy);
118                                 len3 = len_v2v2(v3_xy, v4_xy);
119                                 len4 = len_v2v2(v4_xy, v1_xy);
120                                 len5 = len_v2v2(v1_xy, v3_xy);
121                                 len6 = len_v2v2(v2_xy, v4_xy);
122
123                                 opp1 = area_tri_v2(v1_xy, v2_xy, v3_xy);
124                                 opp2 = area_tri_v2(v1_xy, v3_xy, v4_xy);
125
126                                 fac1 = opp1 / (len1 + len2 + len5) + opp2 / (len3 + len4 + len5);
127
128                                 opp1 = area_tri_v2(v2_xy, v3_xy, v4_xy);
129                                 opp2 = area_tri_v2(v2_xy, v4_xy, v1_xy);
130
131                                 fac2 = opp1 / (len2 + len3 + len6) + opp2 / (len4 + len1 + len6);
132                                 
133                                 if (fac1 > fac2) {
134                                         e = BM_edge_rotate(bm, e, false, BM_EDGEROT_CHECK_EXISTS);
135                                         if (e) {
136                                                 BMO_elem_flag_enable(bm, e, ELE_NEW | EDGE_MARK);
137
138                                                 BMO_elem_flag_enable(bm, e->l->f, FACE_MARK | ELE_NEW);
139                                                 BMO_elem_flag_enable(bm, e->l->radial_next->f, FACE_MARK | ELE_NEW);
140                                                 stop = 0;
141                                         }
142                                 }
143                         }
144                 }
145         }
146         
147         BMO_slot_buffer_from_enabled_flag(bm, op, op->slots_out, "geom.out", BM_EDGE | BM_FACE, ELE_NEW);
148 }
149
150 void bmo_triangle_fill_exec(BMesh *bm, BMOperator *op)
151 {
152         const bool use_beauty = BMO_slot_bool_get(op->slots_in, "use_beauty");
153         BMOIter siter;
154         BMEdge *e;
155         ScanFillContext sf_ctx;
156         /* ScanFillEdge *sf_edge; */ /* UNUSED */
157         ScanFillVert *sf_vert, *sf_vert_1, *sf_vert_2;
158         ScanFillFace *sf_tri;
159         SmallHash hash;
160
161         BLI_smallhash_init(&hash);
162         
163         BLI_scanfill_begin(&sf_ctx);
164         
165         BMO_ITER (e, &siter, op->slots_in, "edges", BM_EDGE) {
166                 BMO_elem_flag_enable(bm, e, EDGE_MARK);
167                 
168                 if (!BLI_smallhash_haskey(&hash, (uintptr_t)e->v1)) {
169                         sf_vert = BLI_scanfill_vert_add(&sf_ctx, e->v1->co);
170                         sf_vert->tmp.p = e->v1;
171                         BLI_smallhash_insert(&hash, (uintptr_t)e->v1, sf_vert);
172                 }
173                 
174                 if (!BLI_smallhash_haskey(&hash, (uintptr_t)e->v2)) {
175                         sf_vert = BLI_scanfill_vert_add(&sf_ctx, e->v2->co);
176                         sf_vert->tmp.p = e->v2;
177                         BLI_smallhash_insert(&hash, (uintptr_t)e->v2, sf_vert);
178                 }
179                 
180                 sf_vert_1 = BLI_smallhash_lookup(&hash, (uintptr_t)e->v1);
181                 sf_vert_2 = BLI_smallhash_lookup(&hash, (uintptr_t)e->v2);
182                 /* sf_edge = */ BLI_scanfill_edge_add(&sf_ctx, sf_vert_1, sf_vert_2);
183                 /* sf_edge->tmp.p = e; */ /* UNUSED */
184         }
185         
186         BLI_scanfill_calc(&sf_ctx, BLI_SCANFILL_CALC_HOLES);
187         
188         for (sf_tri = sf_ctx.fillfacebase.first; sf_tri; sf_tri = sf_tri->next) {
189                 BMFace *f = BM_face_create_quad_tri(bm,
190                                                     sf_tri->v1->tmp.p, sf_tri->v2->tmp.p, sf_tri->v3->tmp.p, NULL,
191                                                     NULL, true);
192                 BMLoop *l;
193                 BMIter liter;
194                 
195                 BMO_elem_flag_enable(bm, f, ELE_NEW);
196                 BM_ITER_ELEM (l, &liter, f, BM_LOOPS_OF_FACE) {
197                         if (!BMO_elem_flag_test(bm, l->e, EDGE_MARK)) {
198                                 BMO_elem_flag_enable(bm, l->e, ELE_NEW);
199                         }
200                 }
201         }
202         
203         BLI_scanfill_end(&sf_ctx);
204         BLI_smallhash_release(&hash);
205         
206         if (use_beauty) {
207                 BMOperator bmop;
208
209                 BMO_op_initf(bm, &bmop, op->flag, "beautify_fill faces=%ff edges=%Fe", ELE_NEW, EDGE_MARK);
210                 BMO_op_exec(bm, &bmop);
211                 BMO_slot_buffer_flag_enable(bm, bmop.slots_out, "geom.out", BM_FACE | BM_EDGE, ELE_NEW);
212                 BMO_op_finish(bm, &bmop);
213         }
214         
215         BMO_slot_buffer_from_enabled_flag(bm, op, op->slots_out, "geom.out", BM_EDGE | BM_FACE, ELE_NEW);
216 }