fix for own mistake in recent changes to scanfill, bmo_triangle_fill_exec() (alt...
[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 EDGE_NEW        1
41 #define FACE_NEW        1
42
43 #define ELE_NEW         1
44 #define FACE_MARK       2
45 #define EDGE_MARK       4
46
47 void bmo_triangulate_exec(BMesh *bm, BMOperator *op)
48 {
49         BMOIter siter;
50         BMFace *face, **newfaces = NULL;
51         BLI_array_declare(newfaces);
52         float (*projectverts)[3] = NULL;
53         BLI_array_declare(projectverts);
54         int i;
55         const int use_beauty = BMO_slot_bool_get(op->slots_in, "use_beauty");
56         BMOpSlot *slot_facemap_out = BMO_slot_get(op->slots_out, "face_map.out");
57
58         for (face = BMO_iter_new(&siter, op->slots_in, "faces", BM_FACE); face; face = BMO_iter_step(&siter)) {
59
60                 BLI_array_empty(projectverts);
61                 BLI_array_empty(newfaces);
62
63                 BLI_array_grow_items(projectverts, face->len * 3);
64                 BLI_array_grow_items(newfaces, face->len);
65
66                 BM_face_triangulate(bm, face, projectverts, EDGE_NEW, FACE_NEW, newfaces, use_beauty);
67
68                 BMO_slot_map_elem_insert(op, slot_facemap_out, face, face);
69                 for (i = 0; newfaces[i]; i++) {
70                         BMO_slot_map_elem_insert(op, slot_facemap_out, newfaces[i], face);
71                 }
72         }
73         
74         BMO_slot_buffer_from_enabled_flag(bm, op, op->slots_out, "edges.out", BM_EDGE, EDGE_NEW);
75         BMO_slot_buffer_from_enabled_flag(bm, op, op->slots_out, "faces.out", BM_FACE, FACE_NEW);
76         
77         BLI_array_free(projectverts);
78         BLI_array_free(newfaces);
79 }
80
81 void bmo_beautify_fill_exec(BMesh *bm, BMOperator *op)
82 {
83         BMOIter siter;
84         BMIter iter;
85         BMFace *f;
86         BMEdge *e;
87         int stop = 0;
88         
89         BMO_slot_buffer_flag_enable(bm, op->slots_in, "constrain_edges", BM_EDGE, EDGE_MARK);
90         
91         BMO_ITER (f, &siter, op->slots_in, "faces", BM_FACE) {
92                 if (f->len == 3) {
93                         BMO_elem_flag_enable(bm, f, FACE_MARK);
94                 }
95         }
96
97         while (!stop) {
98                 stop = 1;
99                 
100                 BM_ITER_MESH (e, &iter, bm, BM_EDGES_OF_MESH) {
101                         BMVert *v1, *v2, *v3, *v4;
102                         
103                         if (!BM_edge_is_manifold(e) || BMO_elem_flag_test(bm, e, EDGE_MARK)) {
104                                 continue;
105                         }
106
107                         if (!BMO_elem_flag_test(bm, e->l->f, FACE_MARK) ||
108                             !BMO_elem_flag_test(bm, e->l->radial_next->f, FACE_MARK))
109                         {
110                                 continue;
111                         }
112                         
113                         v1 = e->l->prev->v;
114                         v2 = e->l->v;
115                         v3 = e->l->radial_next->prev->v;
116                         v4 = e->l->next->v;
117                         
118                         if (is_quad_convex_v3(v1->co, v2->co, v3->co, v4->co)) {
119                                 float len1, len2, len3, len4, len5, len6, opp1, opp2, fac1, fac2;
120                                 /* testing rule:
121                                  * the area divided by the total edge lengths
122                                  */
123                                 len1 = len_v3v3(v1->co, v2->co);
124                                 len2 = len_v3v3(v2->co, v3->co);
125                                 len3 = len_v3v3(v3->co, v4->co);
126                                 len4 = len_v3v3(v4->co, v1->co);
127                                 len5 = len_v3v3(v1->co, v3->co);
128                                 len6 = len_v3v3(v2->co, v4->co);
129
130                                 opp1 = area_tri_v3(v1->co, v2->co, v3->co);
131                                 opp2 = area_tri_v3(v1->co, v3->co, v4->co);
132
133                                 fac1 = opp1 / (len1 + len2 + len5) + opp2 / (len3 + len4 + len5);
134
135                                 opp1 = area_tri_v3(v2->co, v3->co, v4->co);
136                                 opp2 = area_tri_v3(v2->co, v4->co, v1->co);
137
138                                 fac2 = opp1 / (len2 + len3 + len6) + opp2 / (len4 + len1 + len6);
139                                 
140                                 if (fac1 > fac2) {
141                                         e = BM_edge_rotate(bm, e, FALSE, BM_EDGEROT_CHECK_EXISTS);
142                                         if (e) {
143                                                 BMO_elem_flag_enable(bm, e, ELE_NEW);
144
145                                                 BMO_elem_flag_enable(bm, e->l->f, FACE_MARK | ELE_NEW);
146                                                 BMO_elem_flag_enable(bm, e->l->radial_next->f, FACE_MARK | ELE_NEW);
147                                                 stop = 0;
148                                         }
149                                 }
150                         }
151                 }
152         }
153         
154         BMO_slot_buffer_from_enabled_flag(bm, op, op->slots_out, "geom.out", BM_EDGE | BM_FACE, ELE_NEW);
155 }
156
157 void bmo_triangle_fill_exec(BMesh *bm, BMOperator *op)
158 {
159         BMOIter siter;
160         BMEdge *e;
161         BMOperator bmop;
162         ScanFillContext sf_ctx;
163         /* ScanFillEdge *sf_edge; */ /* UNUSED */
164         ScanFillVert *sf_vert, *sf_vert_1, *sf_vert_2;
165         ScanFillFace *sf_tri;
166         SmallHash hash;
167
168         BLI_smallhash_init(&hash);
169         
170         BLI_scanfill_begin(&sf_ctx);
171         
172         BMO_ITER (e, &siter, op->slots_in, "edges", BM_EDGE) {
173                 BMO_elem_flag_enable(bm, e, EDGE_MARK);
174                 
175                 if (!BLI_smallhash_haskey(&hash, (uintptr_t)e->v1)) {
176                         sf_vert = BLI_scanfill_vert_add(&sf_ctx, e->v1->co);
177                         sf_vert->tmp.p = e->v1;
178                         BLI_smallhash_insert(&hash, (uintptr_t)e->v1, sf_vert);
179                 }
180                 
181                 if (!BLI_smallhash_haskey(&hash, (uintptr_t)e->v2)) {
182                         sf_vert = BLI_scanfill_vert_add(&sf_ctx, e->v2->co);
183                         sf_vert->tmp.p = e->v2;
184                         BLI_smallhash_insert(&hash, (uintptr_t)e->v2, sf_vert);
185                 }
186                 
187                 sf_vert_1 = BLI_smallhash_lookup(&hash, (uintptr_t)e->v1);
188                 sf_vert_2 = BLI_smallhash_lookup(&hash, (uintptr_t)e->v2);
189                 /* sf_edge = */ BLI_scanfill_edge_add(&sf_ctx, sf_vert_1, sf_vert_2);
190                 /* sf_edge->tmp.p = e; */ /* UNUSED */
191         }
192         
193         BLI_scanfill_calc(&sf_ctx, BLI_SCANFILL_CALC_HOLES);
194         
195         for (sf_tri = sf_ctx.fillfacebase.first; sf_tri; sf_tri = sf_tri->next) {
196                 BMFace *f = BM_face_create_quad_tri(bm,
197                                                     sf_tri->v1->tmp.p, sf_tri->v2->tmp.p, sf_tri->v3->tmp.p, NULL,
198                                                     NULL, TRUE);
199                 BMLoop *l;
200                 BMIter liter;
201                 
202                 BMO_elem_flag_enable(bm, f, ELE_NEW);
203                 BM_ITER_ELEM (l, &liter, f, BM_LOOPS_OF_FACE) {
204                         if (!BMO_elem_flag_test(bm, l->e, EDGE_MARK)) {
205                                 BMO_elem_flag_enable(bm, l->e, ELE_NEW);
206                         }
207                 }
208         }
209         
210         BLI_scanfill_end(&sf_ctx);
211         BLI_smallhash_release(&hash);
212         
213         /* clean up fill */
214         BMO_op_initf(bm, &bmop, op->flag, "beautify_fill faces=%ff constrain_edges=%fe", ELE_NEW, EDGE_MARK);
215         BMO_op_exec(bm, &bmop);
216         BMO_slot_buffer_flag_enable(bm, bmop.slots_out, "geom.out", BM_FACE | BM_EDGE, ELE_NEW);
217         BMO_op_finish(bm, &bmop);
218         
219         BMO_slot_buffer_from_enabled_flag(bm, op, op->slots_out, "geom.out", BM_EDGE | BM_FACE, ELE_NEW);
220 }