building without python works again, cleanup bmesh include paths (cmake and scons).
[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 "intern/bmesh_private.h"
32
33 #include "intern/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 bmo_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         const int use_beauty = BMO_slot_bool_get(op, "use_beauty");
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, 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_flag(bm, op, "edgeout", EDGE_NEW, BM_EDGE);
76         BMO_slot_buffer_from_flag(bm, op, "faceout", FACE_NEW, BM_FACE);
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", 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
98         while (!stop) {
99                 stop = 1;
100                 
101                 BM_ITER(e, &iter, bm, BM_EDGES_OF_MESH, NULL) {
102                         BMVert *v1, *v2, *v3, *v4;
103                         
104                         if (BM_edge_face_count(e) != 2 || 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_flag(bm, op, "geomout", ELE_NEW, BM_EDGE|BM_FACE);
156 }
157
158 void bmo_triangle_fill_exec(BMesh *bm, BMOperator *op)
159 {
160         BMOIter siter;
161         BMEdge *e;
162         BMOperator bmop;
163         /* ScanFillEdge *eed; */ /* UNUSED */
164         ScanFillVert *eve, *v1, *v2;
165         ScanFillFace *efa;
166         SmallHash hash;
167
168         BLI_smallhash_init(&hash);
169         
170         BLI_begin_edgefill();
171         
172         BMO_ITER(e, &siter, bm, op, "edges", BM_EDGE) {
173                 BMO_elem_flag_enable(bm, e, EDGE_MARK);
174                 
175                 if (!BLI_smallhash_haskey(&hash, (uintptr_t)e->v1)) {
176                         eve = BLI_addfillvert(e->v1->co);
177                         eve->tmp.p = e->v1;
178                         BLI_smallhash_insert(&hash, (uintptr_t)e->v1, eve);
179                 }
180                 
181                 if (!BLI_smallhash_haskey(&hash, (uintptr_t)e->v2)) {
182                         eve = BLI_addfillvert(e->v2->co);
183                         eve->tmp.p = e->v2;
184                         BLI_smallhash_insert(&hash, (uintptr_t)e->v2, eve);
185                 }
186                 
187                 v1 = BLI_smallhash_lookup(&hash, (uintptr_t)e->v1);
188                 v2 = BLI_smallhash_lookup(&hash, (uintptr_t)e->v2);
189                 /* eed = */ BLI_addfilledge(v1, v2);
190                 /* eed->tmp.p = e; */ /* UNUSED */
191         }
192         
193         BLI_edgefill(0);
194         
195         for (efa = fillfacebase.first; efa; efa = efa->next) {
196                 BMFace *f = BM_face_create_quad_tri(bm,
197                                                     efa->v1->tmp.p, efa->v2->tmp.p, efa->v3->tmp.p, NULL,
198                                                     NULL, TRUE);
199                 BMLoop *l;
200                 BMIter liter;
201                 
202                 BMO_elem_flag_enable(bm, f, ELE_NEW);
203                 BM_ITER(l, &liter, bm, BM_LOOPS_OF_FACE, f) {
204                         if (!BMO_elem_flag_test(bm, l->e, EDGE_MARK)) {
205                                 BMO_elem_flag_enable(bm, l->e, ELE_NEW);
206                         }
207                 }
208         }
209         
210         BLI_end_edgefill();
211         BLI_smallhash_release(&hash);
212         
213         /* clean up fill */
214         BMO_op_initf(bm, &bmop, "beautify_fill faces=%ff constrain_edges=%fe", ELE_NEW, EDGE_MARK);
215         BMO_op_exec(bm, &bmop);
216         BMO_slot_buffer_flag_enable(bm, &bmop, "geomout", ELE_NEW, BM_FACE|BM_EDGE);
217         BMO_op_finish(bm, &bmop);
218         
219         BMO_slot_buffer_from_flag(bm, op, "geomout", ELE_NEW, BM_EDGE|BM_FACE);
220 }