code cleanup: redundant includes and add minor comments.
[blender.git] / source / blender / bmesh / operators / bmo_connect_nonplanar.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): Campbell Barton
19  *
20  * ***** END GPL LICENSE BLOCK *****
21  */
22
23 /** \file blender/bmesh/operators/bmo_connect_nonplanar.c
24  *  \ingroup bmesh
25  *
26  * Connect verts non-planer faces iteratively (splits faces).
27  */
28
29 #include "BLI_math.h"
30 #include "BLI_utildefines.h"
31 #include "BLI_alloca.h"
32 #include "BLI_linklist_stack.h"
33
34 #include "bmesh.h"
35
36 #include "intern/bmesh_operators_private.h" /* own include */
37
38 #define EDGE_OUT        (1 << 0)
39 #define FACE_OUT        (1 << 1)
40
41 /**
42  * Calculates the face subset normal.
43  */
44 static bool bm_face_subset_calc_normal(BMLoop *l_first, BMLoop *l_last, float r_no[3])
45 {
46         float const *v_prev, *v_curr;
47
48         /* Newell's Method */
49         BMLoop *l_iter = l_first;
50         BMLoop *l_term = l_last->next;
51
52         zero_v3(r_no);
53
54         v_prev = l_last->v->co;
55         do {
56                 v_curr = l_iter->v->co;
57                 add_newell_cross_v3_v3v3(r_no, v_prev, v_curr);
58                 v_prev = v_curr;
59         } while ((l_iter = l_iter->next) != l_term);
60
61         return (normalize_v3(r_no) != 0.0f);
62 }
63
64 /**
65  * Calculates how non-planar the face subset is.
66  */
67 static float bm_face_subset_calc_planar(BMLoop *l_first, BMLoop *l_last, const float no[3])
68 {
69         float axis_mat[3][3];
70         float z_prev, z_curr;
71         float delta_z = 0.0f;
72
73         /* Newell's Method */
74         BMLoop *l_iter = l_first;
75         BMLoop *l_term = l_last->next;
76
77         axis_dominant_v3_to_m3(axis_mat, no);
78
79         z_prev = dot_m3_v3_row_z(axis_mat, l_last->v->co);
80         do {
81                 z_curr = dot_m3_v3_row_z(axis_mat, l_iter->v->co);
82                 delta_z += fabsf(z_curr - z_prev);
83                 z_prev = z_curr;
84         } while ((l_iter = l_iter->next) != l_term);
85
86         return delta_z;
87 }
88
89 static bool bm_face_split_find(BMFace *f, BMVert *v_pair[2], float *r_angle)
90 {
91         BMLoop *l_iter, *l_first;
92         BMLoop **l_arr = BLI_array_alloca(l_arr, f->len);
93         const unsigned int f_len = f->len;
94         unsigned int i_a, i_b;
95         bool found = false;
96
97         /* angle finding */
98         float err_best = FLT_MAX;
99         float angle_best = FLT_MAX;
100
101         l_iter = l_first = BM_FACE_FIRST_LOOP(f);
102         i_a = 0;
103         do {
104                 l_arr[i_a++] = l_iter;
105         } while ((l_iter = l_iter->next) != l_first);
106
107         /* now for the big search, O(N^2), however faces normally aren't so large */
108         for (i_a = 0; i_a < f_len; i_a++) {
109                 BMLoop *l_a = l_arr[i_a];
110                 for (i_b = i_a + 2; i_b < f_len; i_b++) {
111                         BMLoop *l_b = l_arr[i_b];
112                         /* check these are not touching
113                          * (we could be smarter here) */
114                         if ((l_a->next != l_b) &&
115                             (l_a->prev != l_b))
116                         {
117                                 /* first calculate normals */
118                                 float no_a[3], no_b[3];
119
120                                 if (bm_face_subset_calc_normal(l_a, l_b, no_a) &&
121                                     bm_face_subset_calc_normal(l_b, l_a, no_b))
122                                 {
123                                         const float err_a = bm_face_subset_calc_planar(l_a, l_b, no_a);
124                                         const float err_b = bm_face_subset_calc_planar(l_b, l_a, no_b);
125                                         const float err_test = err_a + err_b;
126
127                                         if (err_test < err_best) {
128                                                 /* check we're legal (we could batch this) */
129                                                 BMLoop *l_split[2] = {l_a, l_b};
130                                                 BM_face_legal_splits(f, &l_split, 1);
131                                                 if (l_split[0]) {
132                                                         err_best = err_test;
133                                                         v_pair[0] = l_a->v;
134                                                         v_pair[1] = l_b->v;
135
136                                                         angle_best = angle_normalized_v3v3(no_a, no_b);
137                                                         found = true;
138                                                 }
139                                         }
140                                 }
141                         }
142                 }
143         }
144
145         *r_angle = angle_best;
146
147         return found;
148
149
150 }
151
152 static bool bm_face_split_by_angle(BMesh *bm, BMFace *f, BMFace *r_f_pair[2], const float angle_limit)
153 {
154         BMVert *v_pair[2];
155         float angle;
156
157         if (bm_face_split_find(f, v_pair, &angle) && (angle > angle_limit)) {
158                 BMFace *f_new;
159                 BMLoop *l_new;
160                 f_new = BM_face_split(bm, f, v_pair[0], v_pair[1], &l_new, NULL, false);
161                 if (f_new) {
162                         r_f_pair[0] = f;
163                         r_f_pair[1] = f_new;
164
165                         BMO_elem_flag_enable(bm, f, FACE_OUT);
166                         BMO_elem_flag_enable(bm, f_new, FACE_OUT);
167                         BMO_elem_flag_enable(bm, l_new->e, EDGE_OUT);
168                         return true;
169                 }
170         }
171
172         return false;
173
174 }
175
176 void bmo_connect_verts_nonplanar_exec(BMesh *bm, BMOperator *op)
177 {
178         BMOIter siter;
179         BMFace *f;
180         int totface = 0, totloop = 0;
181         BLI_LINKSTACK_DECLARE(fstack, BMFace *);
182
183         const float angle_limit = BMO_slot_float_get(op->slots_in, "angle_limit");
184
185
186         BMO_ITER (f, &siter, op->slots_in, "faces", BM_FACE) {
187                 if (f->len > 3) {
188                         totface += 1;
189                         totloop += f->len;
190                 }
191         }
192
193         if (totface == 0) {
194                 return;
195         }
196
197         BLI_LINKSTACK_INIT(fstack);
198
199         BMO_ITER (f, &siter, op->slots_in, "faces", BM_FACE) {
200                 if (f->len > 3) {
201                         BLI_LINKSTACK_PUSH(fstack, f);
202                 }
203         }
204
205         while ((f = BLI_LINKSTACK_POP(fstack))) {
206                 BMFace *f_pair[2];
207                 if (bm_face_split_by_angle(bm, f, f_pair, angle_limit)) {
208                         int j;
209                         for (j = 0; j < 2; j++) {
210                                 BM_face_normal_update(f_pair[j]);
211                                 if (f_pair[j]->len > 3) {
212                                         BLI_LINKSTACK_PUSH(fstack, f_pair[j]);
213                                 }
214                         }
215                 }
216         }
217
218         BLI_LINKSTACK_FREE(fstack);
219
220         BMO_slot_buffer_from_enabled_flag(bm, op, op->slots_out, "edges.out", BM_EDGE, EDGE_OUT);
221         BMO_slot_buffer_from_enabled_flag(bm, op, op->slots_out, "faces.out", BM_FACE, FACE_OUT);
222 }