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