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