svn merge ^/trunk/blender -r44118:44136
[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 #include "MEM_guardedalloc.h"
24
25 #include "BLI_scanfill.h"
26 #include "BLI_math.h"
27 #include "BLI_array.h"
28 #include "BLI_editVert.h"
29 #include "BLI_smallhash.h"
30
31 #include "bmesh.h"
32 #include "bmesh_private.h"
33
34 #include "bmesh_operators_private.h" /* own include */
35
36 #define EDGE_NEW        1
37 #define FACE_NEW        1
38
39 #define ELE_NEW         1
40 #define FACE_MARK       2
41 #define EDGE_MARK       4
42
43 void triangulate_exec(BMesh *bm, BMOperator *op)
44 {
45         BMOIter siter;
46         BMFace *face, **newfaces = NULL;
47         BLI_array_declare(newfaces);
48         float (*projectverts)[3] = NULL;
49         BLI_array_declare(projectverts);
50         int i, lastlen = 0 /* , count = 0 */;
51         
52         face = BMO_iter_new(&siter, bm, op, "faces", BM_FACE);
53         for ( ; face; face = BMO_iter_step(&siter)) {
54                 if (lastlen < face->len) {
55                         BLI_array_empty(projectverts);
56                         BLI_array_empty(newfaces);
57                         for (lastlen = 0; lastlen < face->len; lastlen++) {
58                                 BLI_array_growone(projectverts);
59                                 BLI_array_growone(projectverts);
60                                 BLI_array_growone(projectverts);
61                                 BLI_array_growone(newfaces);
62                         }
63                 }
64
65                 BM_face_triangulate(bm, face, projectverts, EDGE_NEW, FACE_NEW, newfaces);
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_from_flag(bm, op, "edgeout", EDGE_NEW, BM_EDGE);
76         BMO_slot_from_flag(bm, op, "faceout", FACE_NEW, BM_FACE);
77         
78         BLI_array_free(projectverts);
79         BLI_array_free(newfaces);
80 }
81
82 void bmesh_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", EDGE_MARK, BM_EDGE);
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         while (!stop) {
98                 stop = 1;
99                 
100                 BM_ITER(e, &iter, bm, BM_EDGES_OF_MESH, NULL) {
101                         BMVert *v1, *v2, *v3, *v4;
102                         
103                         if (BM_edge_face_count(e) != 2 || 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, 0);
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_from_flag(bm, op, "geomout", ELE_NEW, BM_EDGE|BM_FACE);
155 }
156
157 void bmesh_triangle_fill_exec(BMesh *bm, BMOperator *op)
158 {
159         BMOIter siter;
160         BMEdge *e;
161         BMOperator bmop;
162         EditEdge *eed;
163         EditVert *eve, *v1, *v2;
164         EditFace *efa;
165         SmallHash hash;
166
167         BLI_smallhash_init(&hash);
168         
169         BLI_begin_edgefill();
170         
171         BMO_ITER(e, &siter, bm, op, "edges", BM_EDGE) {
172                 BMO_elem_flag_enable(bm, e, EDGE_MARK);
173                 
174                 if (!BLI_smallhash_haskey(&hash, (uintptr_t)e->v1)) {
175                         eve = BLI_addfillvert(e->v1->co);
176                         eve->tmp.p = e->v1;
177                         BLI_smallhash_insert(&hash, (uintptr_t)e->v1, eve);
178                 }
179                 
180                 if (!BLI_smallhash_haskey(&hash, (uintptr_t)e->v2)) {
181                         eve = BLI_addfillvert(e->v2->co);
182                         eve->tmp.p = e->v2;
183                         BLI_smallhash_insert(&hash, (uintptr_t)e->v2, eve);
184                 }
185                 
186                 v1 = BLI_smallhash_lookup(&hash, (uintptr_t)e->v1);
187                 v2 = BLI_smallhash_lookup(&hash, (uintptr_t)e->v2);
188                 eed = BLI_addfilledge(v1, v2);
189                 eed->tmp.p = e;
190         }
191         
192         BLI_edgefill(0);
193         
194         for (efa = fillfacebase.first; efa; efa = efa->next) {
195                 BMFace *f = BM_face_create_quad_tri(bm,
196                                                     efa->v1->tmp.p, efa->v2->tmp.p, efa->v3->tmp.p, NULL,
197                                                     NULL, TRUE);
198                 BMLoop *l;
199                 BMIter liter;
200                 
201                 BMO_elem_flag_enable(bm, f, ELE_NEW);
202                 BM_ITER(l, &liter, bm, BM_LOOPS_OF_FACE, f) {
203                         if (!BMO_elem_flag_test(bm, l->e, EDGE_MARK)) {
204                                 BMO_elem_flag_enable(bm, l->e, ELE_NEW);
205                         }
206                 }
207         }
208         
209         BLI_end_edgefill();
210         BLI_smallhash_release(&hash);
211         
212         /* clean up fill */
213         BMO_op_initf(bm, &bmop, "beautify_fill faces=%ff constrain_edges=%fe", ELE_NEW, EDGE_MARK);
214         BMO_op_exec(bm, &bmop);
215         BMO_slot_buffer_flag_enable(bm, &bmop, "geomout", ELE_NEW, BM_FACE|BM_EDGE);
216         BMO_op_finish(bm, &bmop);
217         
218         BMO_slot_from_flag(bm, op, "geomout", ELE_NEW, BM_EDGE|BM_FACE);
219 }