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