Recent spinlock commit made scheduling unsafe for threading
[blender.git] / source / blender / bmesh / tools / bmesh_edgesplit.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/tools/bmesh_edgesplit.c
24  *  \ingroup bmesh
25  *
26  * Edge-Split.
27  *
28  */
29
30 #include "MEM_guardedalloc.h"
31
32 #include "BLI_utildefines.h"
33
34 #include "bmesh.h"
35
36 #include "bmesh_edgesplit.h"  /* own include */
37
38
39 /**
40  * Remove the BM_ELEM_TAG flag for edges we cant split
41  *
42  * un-tag edges not connected to other tagged edges,
43  * unless they are on a boundary
44  */
45 static void bm_edgesplit_validate_seams(BMesh *bm)
46 {
47         BMIter iter;
48         BMEdge *e;
49
50         unsigned char *vtouch;
51
52         BM_mesh_elem_index_ensure(bm, BM_VERT);
53
54         vtouch = MEM_callocN(sizeof(char) * bm->totvert, __func__);
55
56         /* tag all boundary verts so as not to untag an edge which is inbetween only 2 faces [] */
57         BM_ITER_MESH (e, &iter, bm, BM_EDGES_OF_MESH) {
58
59                 /* unrelated to flag assignment in this function - since this is the
60                  * only place we loop over all edges, disable tag */
61                 BM_elem_flag_disable(e, BM_ELEM_INTERNAL_TAG);
62
63                 if (e->l == NULL) {
64                         BM_elem_flag_disable(e, BM_ELEM_TAG);
65                 }
66                 else if (BM_edge_is_boundary(e)) {
67                         unsigned char *vt;
68                         vt = &vtouch[BM_elem_index_get(e->v1)]; if (*vt < 2) (*vt)++;
69                         vt = &vtouch[BM_elem_index_get(e->v2)]; if (*vt < 2) (*vt)++;
70
71                         /* while the boundary verts need to be tagged,
72                          * the edge its self can't be split */
73                         BM_elem_flag_disable(e, BM_ELEM_TAG);
74                 }
75         }
76
77         /* single marked edges unconnected to any other marked edges
78          * are illegal, go through and unmark them */
79         BM_ITER_MESH (e, &iter, bm, BM_EDGES_OF_MESH) {
80                 if (BM_elem_flag_test(e, BM_ELEM_TAG)) {
81                         /* lame, but we don't want the count to exceed 255,
82                          * so just count to 2, its all we need */
83                         unsigned char *vt;
84                         vt = &vtouch[BM_elem_index_get(e->v1)]; if (*vt < 2) (*vt)++;
85                         vt = &vtouch[BM_elem_index_get(e->v2)]; if (*vt < 2) (*vt)++;
86                 }
87         }
88         BM_ITER_MESH (e, &iter, bm, BM_EDGES_OF_MESH) {
89                 if (BM_elem_flag_test(e, BM_ELEM_TAG)) {
90                         if (vtouch[BM_elem_index_get(e->v1)] == 1 &&
91                                 vtouch[BM_elem_index_get(e->v2)] == 1)
92                         {
93                                 BM_elem_flag_disable(e, BM_ELEM_TAG);
94                         }
95                 }
96         }
97
98         MEM_freeN(vtouch);
99 }
100
101 void BM_mesh_edgesplit(BMesh *bm, const bool use_verts, const bool tag_only, const bool copy_select)
102 {
103         BMIter iter;
104         BMEdge *e;
105
106
107         if (tag_only == false) {
108                 BM_mesh_elem_hflag_enable_all(bm, BM_EDGE | (use_verts ? BM_VERT : 0), BM_ELEM_TAG, false);
109         }
110
111         if (use_verts) {
112                 /* prevent one edge having both verts unflagged
113                  * we could alternately disable these edges, either way its a corner case.
114                  *
115                  * This is needed so we don't split off the edge but then none of its verts which
116                  * would leave a duplicate edge.
117                  */
118                 BM_ITER_MESH (e, &iter, bm, BM_EDGES_OF_MESH) {
119                         if (BM_elem_flag_test(e, BM_ELEM_TAG)) {
120                                 if (UNLIKELY(((BM_elem_flag_test(e->v1, BM_ELEM_TAG) == false) &&
121                                               (BM_elem_flag_test(e->v2, BM_ELEM_TAG) == false))))
122                                 {
123                                         BM_elem_flag_enable(e->v1, BM_ELEM_TAG);
124                                         BM_elem_flag_enable(e->v2, BM_ELEM_TAG);
125                                 }
126                         }
127                 }
128         }
129
130         bm_edgesplit_validate_seams(bm);
131
132         BM_ITER_MESH (e, &iter, bm, BM_EDGES_OF_MESH) {
133                 if (BM_elem_flag_test(e, BM_ELEM_TAG)) {
134                         /* this flag gets copied so we can be sure duplicate edges get it too (important) */
135                         BM_elem_flag_enable(e, BM_ELEM_INTERNAL_TAG);
136
137                         /* keep splitting until each loop has its own edge */
138                         do {
139                                 bmesh_edge_separate(bm, e, e->l, copy_select);
140                         } while (!BM_edge_is_boundary(e));
141
142                         BM_elem_flag_enable(e->v1, BM_ELEM_TAG);
143                         BM_elem_flag_enable(e->v2, BM_ELEM_TAG);
144                 }
145         }
146
147         if (use_verts) {
148                 BM_ITER_MESH (e, &iter, bm, BM_EDGES_OF_MESH) {
149                         if (BM_elem_flag_test(e->v1, BM_ELEM_TAG) == false) {
150                                 BM_elem_flag_disable(e->v1, BM_ELEM_TAG);
151                         }
152                         if (BM_elem_flag_test(e->v2, BM_ELEM_TAG) == false) {
153                                 BM_elem_flag_disable(e->v2, BM_ELEM_TAG);
154                         }
155                 }
156         }
157
158         BM_ITER_MESH (e, &iter, bm, BM_EDGES_OF_MESH) {
159                 if (BM_elem_flag_test(e, BM_ELEM_TAG)) {
160                         if (BM_elem_flag_test(e->v1, BM_ELEM_TAG)) {
161                                 BM_elem_flag_disable(e->v1, BM_ELEM_TAG);
162                                 bmesh_vert_separate(bm, e->v1, NULL, NULL, copy_select);
163                         }
164                         if (BM_elem_flag_test(e->v2, BM_ELEM_TAG)) {
165                                 BM_elem_flag_disable(e->v2, BM_ELEM_TAG);
166                                 bmesh_vert_separate(bm, e->v2, NULL, NULL, copy_select);
167                         }
168                 }
169         }
170 }