code cleanup: operator headers
[blender.git] / source / blender / bmesh / operators / bmo_beautify.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_beautify.c
24  *  \ingroup bmesh
25  */
26
27 #include "BLI_math.h"
28
29 #include "bmesh.h"
30 #include "intern/bmesh_operators_private.h"
31
32
33 #define ELE_NEW         1
34 #define FACE_MARK       2
35 #define EDGE_MARK       4
36
37 void bmo_beautify_fill_exec(BMesh *bm, BMOperator *op)
38 {
39         BMOIter siter;
40         BMIter iter;
41         BMFace *f;
42         BMEdge *e;
43         int stop = 0;
44         
45         BMO_slot_buffer_flag_enable(bm, op->slots_in, "edges", BM_EDGE, EDGE_MARK);
46         
47         BMO_ITER (f, &siter, op->slots_in, "faces", BM_FACE) {
48                 if (f->len == 3) {
49                         BMO_elem_flag_enable(bm, f, FACE_MARK);
50                 }
51         }
52
53         while (!stop) {
54                 stop = 1;
55                 
56                 BM_ITER_MESH (e, &iter, bm, BM_EDGES_OF_MESH) {
57                         BMVert *v1, *v2, *v3, *v4;
58                         
59                         if (!BM_edge_is_manifold(e) || !BMO_elem_flag_test(bm, e, EDGE_MARK)) {
60                                 continue;
61                         }
62
63                         if (!BMO_elem_flag_test(bm, e->l->f, FACE_MARK) ||
64                             !BMO_elem_flag_test(bm, e->l->radial_next->f, FACE_MARK))
65                         {
66                                 continue;
67                         }
68
69                         v1 = e->l->prev->v;               /* first face vert not attached to 'e' */
70                         v2 = e->l->v;                     /* e->v1 or e->v2*/
71                         v3 = e->l->radial_next->prev->v;  /* second face vert not attached to 'e' */
72                         v4 = e->l->next->v;               /* e->v1 or e->v2*/
73
74                         if (UNLIKELY(v1 == v3)) {
75                                 // printf("This should never happen, but does sometimes!\n");
76                                 continue;
77                         }
78
79                         // printf("%p %p %p %p - %p %p\n", v1, v2, v3, v4, e->l->f, e->l->radial_next->f);
80                         BLI_assert((ELEM3(v1, v2, v3, v4) == false) &&
81                                    (ELEM3(v2, v1, v3, v4) == false) &&
82                                    (ELEM3(v3, v1, v2, v4) == false) &&
83                                    (ELEM3(v4, v1, v2, v3) == false));
84
85                         if (is_quad_convex_v3(v1->co, v2->co, v3->co, v4->co)) {
86                                 float len1, len2, len3, len4, len5, len6, opp1, opp2, fac1, fac2;
87                                 /* testing rule:
88                                  * the area divided by the total edge lengths
89                                  */
90                                 len1 = len_v3v3(v1->co, v2->co);
91                                 len2 = len_v3v3(v2->co, v3->co);
92                                 len3 = len_v3v3(v3->co, v4->co);
93                                 len4 = len_v3v3(v4->co, v1->co);
94                                 len5 = len_v3v3(v1->co, v3->co);
95                                 len6 = len_v3v3(v2->co, v4->co);
96
97                                 opp1 = area_tri_v3(v1->co, v2->co, v3->co);
98                                 opp2 = area_tri_v3(v1->co, v3->co, v4->co);
99
100                                 fac1 = opp1 / (len1 + len2 + len5) + opp2 / (len3 + len4 + len5);
101
102                                 opp1 = area_tri_v3(v2->co, v3->co, v4->co);
103                                 opp2 = area_tri_v3(v2->co, v4->co, v1->co);
104
105                                 fac2 = opp1 / (len2 + len3 + len6) + opp2 / (len4 + len1 + len6);
106                                 
107                                 if (fac1 > fac2) {
108                                         e = BM_edge_rotate(bm, e, false, BM_EDGEROT_CHECK_EXISTS);
109                                         if (e) {
110                                                 BMO_elem_flag_enable(bm, e, ELE_NEW | EDGE_MARK);
111
112                                                 BMO_elem_flag_enable(bm, e->l->f, FACE_MARK | ELE_NEW);
113                                                 BMO_elem_flag_enable(bm, e->l->radial_next->f, FACE_MARK | ELE_NEW);
114                                                 stop = 0;
115                                         }
116                                 }
117                         }
118                 }
119         }
120         
121         BMO_slot_buffer_from_enabled_flag(bm, op, op->slots_out, "geom.out", BM_EDGE | BM_FACE, ELE_NEW);
122 }