doxygen: add newline after \file
[blender.git] / source / blender / bmesh / operators / bmo_connect_nonplanar.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
18  * \ingroup bmesh
19  *
20  * Connect verts non-planer faces iteratively (splits faces).
21  */
22
23 #include "BLI_math.h"
24 #include "BLI_utildefines.h"
25 #include "BLI_alloca.h"
26 #include "BLI_linklist_stack.h"
27
28 #include "bmesh.h"
29
30 #include "intern/bmesh_operators_private.h" /* own include */
31
32 #define EDGE_OUT        (1 << 0)
33 #define FACE_OUT        (1 << 1)
34
35 /**
36  * Calculates how non-planar the face subset is.
37  */
38 static float bm_face_subset_calc_planar(BMLoop *l_first, BMLoop *l_last, const float no[3])
39 {
40         float axis_mat[3][3];
41         float z_prev, z_curr;
42         float delta_z = 0.0f;
43
44         /* Newell's Method */
45         BMLoop *l_iter = l_first;
46         BMLoop *l_term = l_last->next;
47
48         axis_dominant_v3_to_m3(axis_mat, no);
49
50         z_prev = dot_m3_v3_row_z(axis_mat, l_last->v->co);
51         do {
52                 z_curr = dot_m3_v3_row_z(axis_mat, l_iter->v->co);
53                 delta_z += fabsf(z_curr - z_prev);
54                 z_prev = z_curr;
55         } while ((l_iter = l_iter->next) != l_term);
56
57         return delta_z;
58 }
59
60 static bool bm_face_split_find(BMesh *bm, BMFace *f, BMLoop *l_pair[2], float *r_angle_cos)
61 {
62         BMLoop *l_iter, *l_first;
63         BMLoop **l_arr = BLI_array_alloca(l_arr, f->len);
64         const uint f_len = f->len;
65         uint i_a, i_b;
66         bool found = false;
67
68         /* angle finding */
69         float err_best = FLT_MAX;
70         float angle_best_cos = -FLT_MAX;
71
72         l_iter = l_first = BM_FACE_FIRST_LOOP(f);
73         i_a = 0;
74         do {
75                 l_arr[i_a++] = l_iter;
76         } while ((l_iter = l_iter->next) != l_first);
77
78         /* now for the big search, O(N^2), however faces normally aren't so large */
79         for (i_a = 0; i_a < f_len; i_a++) {
80                 BMLoop *l_a = l_arr[i_a];
81                 for (i_b = i_a + 2; i_b < f_len; i_b++) {
82                         BMLoop *l_b = l_arr[i_b];
83                         /* check these are not touching
84                          * (we could be smarter here) */
85                         if (!BM_loop_is_adjacent(l_a, l_b)) {
86                                 /* first calculate normals */
87                                 float no_a[3], no_b[3];
88
89                                 if (BM_face_calc_normal_subset(l_a, l_b, no_a) != 0.0f &&
90                                     BM_face_calc_normal_subset(l_b, l_a, no_b) != 0.0f)
91                                 {
92                                         const float err_a = bm_face_subset_calc_planar(l_a, l_b, no_a);
93                                         const float err_b = bm_face_subset_calc_planar(l_b, l_a, no_b);
94                                         const float err_test = err_a + err_b;
95
96                                         if (err_test < err_best) {
97                                                 /* check we're legal (we could batch this) */
98                                                 BMLoop *l_split[2] = {l_a, l_b};
99                                                 BM_face_splits_check_legal(bm, f, &l_split, 1);
100                                                 if (l_split[0]) {
101                                                         err_best = err_test;
102                                                         l_pair[0] = l_a;
103                                                         l_pair[1] = l_b;
104
105                                                         angle_best_cos = dot_v3v3(no_a, no_b);
106                                                         found = true;
107                                                 }
108                                         }
109                                 }
110                         }
111                 }
112         }
113
114         *r_angle_cos = angle_best_cos;
115
116         return found;
117 }
118
119 static bool bm_face_split_by_angle(BMesh *bm, BMFace *f, BMFace *r_f_pair[2], const float angle_limit_cos)
120 {
121         BMLoop *l_pair[2];
122         float angle_cos;
123
124         if (bm_face_split_find(bm, f, l_pair, &angle_cos) && (angle_cos < angle_limit_cos)) {
125                 BMFace *f_new;
126                 BMLoop *l_new;
127
128                 f_new = BM_face_split(bm, f, l_pair[0], l_pair[1], &l_new, NULL, false);
129                 if (f_new) {
130                         r_f_pair[0] = f;
131                         r_f_pair[1] = f_new;
132
133                         BMO_face_flag_enable(bm, f, FACE_OUT);
134                         BMO_face_flag_enable(bm, f_new, FACE_OUT);
135                         BMO_edge_flag_enable(bm, l_new->e, EDGE_OUT);
136                         return true;
137                 }
138         }
139
140         return false;
141
142 }
143
144 void bmo_connect_verts_nonplanar_exec(BMesh *bm, BMOperator *op)
145 {
146         BMOIter siter;
147         BMFace *f;
148         bool changed = false;
149         BLI_LINKSTACK_DECLARE(fstack, BMFace *);
150
151         const float angle_limit_cos = cosf(BMO_slot_float_get(op->slots_in, "angle_limit"));
152
153         BLI_LINKSTACK_INIT(fstack);
154
155         BMO_ITER (f, &siter, op->slots_in, "faces", BM_FACE) {
156                 if (f->len > 3) {
157                         BLI_LINKSTACK_PUSH(fstack, f);
158                 }
159         }
160
161         while ((f = BLI_LINKSTACK_POP(fstack))) {
162                 BMFace *f_pair[2];
163                 if (bm_face_split_by_angle(bm, f, f_pair, angle_limit_cos)) {
164                         int j;
165                         for (j = 0; j < 2; j++) {
166                                 BM_face_normal_update(f_pair[j]);
167                                 if (f_pair[j]->len > 3) {
168                                         BLI_LINKSTACK_PUSH(fstack, f_pair[j]);
169                                 }
170                         }
171                         changed = true;
172                 }
173         }
174
175         BLI_LINKSTACK_FREE(fstack);
176
177         if (changed) {
178                 BMO_slot_buffer_from_enabled_flag(bm, op, op->slots_out, "edges.out", BM_EDGE, EDGE_OUT);
179                 BMO_slot_buffer_from_enabled_flag(bm, op, op->slots_out, "faces.out", BM_FACE, FACE_OUT);
180         }
181 }