Cleanup: remove redundant doxygen \file argument
[blender.git] / source / blender / bmesh / operators / bmo_planar_faces.c
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  */
16
17 /** \file \ingroup bmesh
18  *
19  * Iteratively flatten 4+ sided faces.
20  */
21
22 #include "MEM_guardedalloc.h"
23
24 #include "BLI_math.h"
25 #include "BLI_ghash.h"
26
27 #include "bmesh.h"
28
29 #include "intern/bmesh_operators_private.h" /* own include */
30
31 #define ELE_VERT_ADJUST (1 << 0)
32 #define ELE_FACE_ADJUST (1 << 1)
33
34 struct VertAccum {
35         float co[3];
36         int co_tot;
37 };
38
39 void bmo_planar_faces_exec(BMesh *bm, BMOperator *op)
40 {
41         const float fac = BMO_slot_float_get(op->slots_in, "factor");
42         const int iterations = BMO_slot_int_get(op->slots_in, "iterations");
43         const int faces_num = BMO_slot_buffer_count(op->slots_in, "faces");
44
45         const float eps = 0.00001f;
46         const float eps_sq = SQUARE(eps);
47
48         BMOIter oiter;
49         BMFace *f;
50         BLI_mempool *vert_accum_pool;
51         GHash *vaccum_map;
52         float (*faces_center)[3];
53         int i, iter_step, shared_vert_num;
54
55         faces_center = MEM_mallocN(sizeof(*faces_center) * faces_num, __func__);
56
57         shared_vert_num = 0;
58         BMO_ITER_INDEX (f, &oiter, op->slots_in, "faces", BM_FACE, i) {
59                 BMLoop *l_iter, *l_first;
60
61                 if (f->len == 3) {
62                         continue;
63                 }
64
65                 BM_face_calc_center_median_weighted(f, faces_center[i]);
66
67                 l_iter = l_first = BM_FACE_FIRST_LOOP(f);
68                 do {
69                         if (!BMO_vert_flag_test(bm, l_iter->v, ELE_VERT_ADJUST)) {
70                                 BMO_vert_flag_enable(bm, l_iter->v, ELE_VERT_ADJUST);
71                                 shared_vert_num += 1;
72                         }
73                 } while ((l_iter = l_iter->next) != l_first);
74
75                 BMO_face_flag_enable(bm, f, ELE_FACE_ADJUST);
76         }
77
78         vert_accum_pool = BLI_mempool_create(sizeof(struct VertAccum), 0, 512, BLI_MEMPOOL_NOP);
79         vaccum_map = BLI_ghash_ptr_new_ex(__func__, shared_vert_num);
80
81         for (iter_step = 0; iter_step < iterations; iter_step++) {
82                 GHashIterator gh_iter;
83                 bool changed = false;
84
85                 BMO_ITER_INDEX (f, &oiter, op->slots_in, "faces", BM_FACE, i) {
86                         BMLoop *l_iter, *l_first;
87                         float plane[4];
88
89                         if (!BMO_face_flag_test(bm, f, ELE_FACE_ADJUST)) {
90                                 continue;
91                         }
92                         BMO_face_flag_disable(bm, f, ELE_FACE_ADJUST);
93
94                         BLI_assert(f->len != 3);
95
96                         /* keep original face data (else we 'move' the face) */
97 #if 0
98                         BM_face_normal_update(f);
99                         BM_face_calc_center_median_weighted(f, f_center);
100 #endif
101
102                         plane_from_point_normal_v3(plane, faces_center[i], f->no);
103
104                         l_iter = l_first = BM_FACE_FIRST_LOOP(f);
105                         do {
106                                 struct VertAccum *va;
107                                 void **va_p;
108                                 float co[3];
109
110                                 if (!BLI_ghash_ensure_p(vaccum_map, l_iter->v, &va_p)) {
111                                         *va_p = BLI_mempool_calloc(vert_accum_pool);
112                                 }
113                                 va = *va_p;
114
115                                 closest_to_plane_normalized_v3(co, plane, l_iter->v->co);
116                                 va->co_tot += 1;
117
118                                 interp_v3_v3v3(va->co, va->co, co, 1.0f / (float)va->co_tot);
119                         } while ((l_iter = l_iter->next) != l_first);
120                 }
121
122                 GHASH_ITER (gh_iter, vaccum_map) {
123                         BMVert *v = BLI_ghashIterator_getKey(&gh_iter);
124                         struct VertAccum *va = BLI_ghashIterator_getValue(&gh_iter);
125                         BMIter iter;
126
127                         if (len_squared_v3v3(v->co, va->co) > eps_sq) {
128                                 BMO_vert_flag_enable(bm, v, ELE_VERT_ADJUST);
129                                 interp_v3_v3v3(v->co, v->co, va->co, fac);
130                                 changed = true;
131                         }
132
133                         /* tag for re-calculation */
134                         BM_ITER_ELEM (f, &iter, v, BM_FACES_OF_VERT) {
135                                 if (f->len != 3) {
136                                         BMO_face_flag_enable(bm, f, ELE_FACE_ADJUST);
137                                 }
138                         }
139                 }
140
141                 /* if nothing changed, break out early */
142                 if (changed == false) {
143                         break;
144                 }
145
146                 BLI_ghash_clear(vaccum_map, NULL, NULL);
147                 BLI_mempool_clear(vert_accum_pool);
148         }
149
150         MEM_freeN(faces_center);
151         BLI_ghash_free(vaccum_map, NULL, NULL);
152         BLI_mempool_destroy(vert_accum_pool);
153 }