problem with own changes to triabgulate: calling beauty fill directly would re-alloca...
[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                         BMVert *v1, *v2, *v3, *v4;
79                         
80                         if (!BM_edge_is_manifold(e) || !BMO_elem_flag_test(bm, e, EDGE_MARK)) {
81                                 continue;
82                         }
83
84                         if (!BMO_elem_flag_test(bm, e->l->f, FACE_MARK) ||
85                             !BMO_elem_flag_test(bm, e->l->radial_next->f, FACE_MARK))
86                         {
87                                 continue;
88                         }
89                         
90                         v1 = e->l->prev->v;
91                         v2 = e->l->v;
92                         v3 = e->l->radial_next->prev->v;
93                         v4 = e->l->next->v;
94                         
95                         if (is_quad_convex_v3(v1->co, v2->co, v3->co, v4->co)) {
96                                 float len1, len2, len3, len4, len5, len6, opp1, opp2, fac1, fac2;
97                                 /* testing rule:
98                                  * the area divided by the total edge lengths
99                                  */
100                                 len1 = len_v3v3(v1->co, v2->co);
101                                 len2 = len_v3v3(v2->co, v3->co);
102                                 len3 = len_v3v3(v3->co, v4->co);
103                                 len4 = len_v3v3(v4->co, v1->co);
104                                 len5 = len_v3v3(v1->co, v3->co);
105                                 len6 = len_v3v3(v2->co, v4->co);
106
107                                 opp1 = area_tri_v3(v1->co, v2->co, v3->co);
108                                 opp2 = area_tri_v3(v1->co, v3->co, v4->co);
109
110                                 fac1 = opp1 / (len1 + len2 + len5) + opp2 / (len3 + len4 + len5);
111
112                                 opp1 = area_tri_v3(v2->co, v3->co, v4->co);
113                                 opp2 = area_tri_v3(v2->co, v4->co, v1->co);
114
115                                 fac2 = opp1 / (len2 + len3 + len6) + opp2 / (len4 + len1 + len6);
116                                 
117                                 if (fac1 > fac2) {
118                                         e = BM_edge_rotate(bm, e, false, BM_EDGEROT_CHECK_EXISTS);
119                                         if (e) {
120                                                 BMO_elem_flag_enable(bm, e, ELE_NEW | EDGE_MARK);
121
122                                                 BMO_elem_flag_enable(bm, e->l->f, FACE_MARK | ELE_NEW);
123                                                 BMO_elem_flag_enable(bm, e->l->radial_next->f, FACE_MARK | ELE_NEW);
124                                                 stop = 0;
125                                         }
126                                 }
127                         }
128                 }
129         }
130         
131         BMO_slot_buffer_from_enabled_flag(bm, op, op->slots_out, "geom.out", BM_EDGE | BM_FACE, ELE_NEW);
132 }
133
134 void bmo_triangle_fill_exec(BMesh *bm, BMOperator *op)
135 {
136         BMOIter siter;
137         BMEdge *e;
138         BMOperator bmop;
139         ScanFillContext sf_ctx;
140         /* ScanFillEdge *sf_edge; */ /* UNUSED */
141         ScanFillVert *sf_vert, *sf_vert_1, *sf_vert_2;
142         ScanFillFace *sf_tri;
143         SmallHash hash;
144
145         BLI_smallhash_init(&hash);
146         
147         BLI_scanfill_begin(&sf_ctx);
148         
149         BMO_ITER (e, &siter, op->slots_in, "edges", BM_EDGE) {
150                 BMO_elem_flag_enable(bm, e, EDGE_MARK);
151                 
152                 if (!BLI_smallhash_haskey(&hash, (uintptr_t)e->v1)) {
153                         sf_vert = BLI_scanfill_vert_add(&sf_ctx, e->v1->co);
154                         sf_vert->tmp.p = e->v1;
155                         BLI_smallhash_insert(&hash, (uintptr_t)e->v1, sf_vert);
156                 }
157                 
158                 if (!BLI_smallhash_haskey(&hash, (uintptr_t)e->v2)) {
159                         sf_vert = BLI_scanfill_vert_add(&sf_ctx, e->v2->co);
160                         sf_vert->tmp.p = e->v2;
161                         BLI_smallhash_insert(&hash, (uintptr_t)e->v2, sf_vert);
162                 }
163                 
164                 sf_vert_1 = BLI_smallhash_lookup(&hash, (uintptr_t)e->v1);
165                 sf_vert_2 = BLI_smallhash_lookup(&hash, (uintptr_t)e->v2);
166                 /* sf_edge = */ BLI_scanfill_edge_add(&sf_ctx, sf_vert_1, sf_vert_2);
167                 /* sf_edge->tmp.p = e; */ /* UNUSED */
168         }
169         
170         BLI_scanfill_calc(&sf_ctx, BLI_SCANFILL_CALC_HOLES);
171         
172         for (sf_tri = sf_ctx.fillfacebase.first; sf_tri; sf_tri = sf_tri->next) {
173                 BMFace *f = BM_face_create_quad_tri(bm,
174                                                     sf_tri->v1->tmp.p, sf_tri->v2->tmp.p, sf_tri->v3->tmp.p, NULL,
175                                                     NULL, true);
176                 BMLoop *l;
177                 BMIter liter;
178                 
179                 BMO_elem_flag_enable(bm, f, ELE_NEW);
180                 BM_ITER_ELEM (l, &liter, f, BM_LOOPS_OF_FACE) {
181                         if (!BMO_elem_flag_test(bm, l->e, EDGE_MARK)) {
182                                 BMO_elem_flag_enable(bm, l->e, ELE_NEW);
183                         }
184                 }
185         }
186         
187         BLI_scanfill_end(&sf_ctx);
188         BLI_smallhash_release(&hash);
189         
190         /* clean up fill */
191         BMO_op_initf(bm, &bmop, op->flag, "beautify_fill faces=%ff edges=%Fe", ELE_NEW, EDGE_MARK);
192         BMO_op_exec(bm, &bmop);
193         BMO_slot_buffer_flag_enable(bm, bmop.slots_out, "geom.out", BM_FACE | BM_EDGE, ELE_NEW);
194         BMO_op_finish(bm, &bmop);
195         
196         BMO_slot_buffer_from_enabled_flag(bm, op, op->slots_out, "geom.out", BM_EDGE | BM_FACE, ELE_NEW);
197 }