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