Blender  V2.93
bmo_edgenet.c
Go to the documentation of this file.
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 
23 #include "MEM_guardedalloc.h"
24 
25 #include "BLI_array.h"
26 #include "BLI_math.h"
27 
28 #include "bmesh.h"
29 #include "bmesh_tools.h"
30 
31 #include "intern/bmesh_operators_private.h" /* own include */
32 
33 #define EDGE_MARK 1
34 #define EDGE_VIS 2
35 
36 #define ELE_NEW 1
37 
39 {
40  BMOperator op_attr;
41  BMOIter siter;
42  BMFace *f;
43  const short mat_nr = BMO_slot_int_get(op->slots_in, "mat_nr");
44  const bool use_smooth = BMO_slot_bool_get(op->slots_in, "use_smooth");
45  // const int sides = BMO_slot_int_get(op->slots_in, "sides");
46 
47  if (!bm->totvert || !bm->totedge) {
48  return;
49  }
50 
53 
55  BM_mesh_edgenet(bm, true, true); /* TODO, sides */
56 
58 
59  BMO_ITER (f, &siter, op->slots_out, "faces.out", BM_FACE) {
60  f->mat_nr = mat_nr;
61  if (use_smooth) {
63  }
64  /* normals are zero'd */
66  }
67 
68  /* --- Attribute Fill --- */
69  /* may as well since we have the faces already in a buffer */
71  &op_attr,
72  op->flag,
73  "face_attribute_fill faces=%S use_normals=%b use_data=%b",
74  op,
75  "faces.out",
76  true,
77  true);
78 
79  BMO_op_exec(bm, &op_attr);
80 
81  /* check if some faces couldn't be touched */
82  if (BMO_slot_buffer_count(op_attr.slots_out, "faces_fail.out")) {
83  BMO_op_callf(bm, op->flag, "recalc_face_normals faces=%S", &op_attr, "faces_fail.out");
84  }
85  BMO_op_finish(bm, &op_attr);
86 }
87 
89 {
90  BMIter iter;
91  BMEdge *e2;
92  int i;
93 
94  for (i = 0; i < 2; i++) {
95  BM_ITER_ELEM (e2, &iter, i ? e->v2 : e->v1, BM_EDGES_OF_VERT) {
96  if ((BMO_edge_flag_test(bm, e2, EDGE_MARK)) &&
97  (BMO_edge_flag_test(bm, e2, EDGE_VIS) == false) && (e2 != e)) {
98  return e2;
99  }
100  }
101  }
102 
103  return NULL;
104 }
105 
107 {
108  BMOIter siter;
109  BMEdge *e;
110  BMEdge **edges1 = NULL, **edges2 = NULL, **edges;
111  BLI_array_declare(edges1);
112  BLI_array_declare(edges2);
113  BLI_array_declare(edges);
114  bool ok = true;
115  int i, count;
116 
118 
119  /* validate that each edge has at most one other tagged edge in the
120  * disk cycle around each of its vertices */
121  BMO_ITER (e, &siter, op->slots_in, "edges", BM_EDGE) {
122  for (i = 0; i < 2; i++) {
123  count = BMO_iter_elem_count_flag(bm, BM_EDGES_OF_VERT, (i ? e->v2 : e->v1), EDGE_MARK, true);
124  if (count > 2) {
125  ok = 0;
126  break;
127  }
128  }
129 
130  if (!ok) {
131  break;
132  }
133  }
134 
135  /* we don't have valid edge layouts, return */
136  if (!ok) {
137  return;
138  }
139 
140  /* find connected loops within the input edge */
141  count = 0;
142  while (1) {
143  BMO_ITER (e, &siter, op->slots_in, "edges", BM_EDGE) {
144  if (!BMO_edge_flag_test(bm, e, EDGE_VIS)) {
145  if (BMO_iter_elem_count_flag(bm, BM_EDGES_OF_VERT, e->v1, EDGE_MARK, true) == 1 ||
147  break;
148  }
149  }
150  }
151 
152  if (!e) {
153  break;
154  }
155 
156  if (!count) {
157  edges = edges1;
158  }
159  else if (count == 1) {
160  edges = edges2;
161  }
162  else {
163  break;
164  }
165 
166  i = 0;
167  while (e) {
169  BLI_array_grow_one(edges);
170  edges[i] = e;
171 
172  e = edge_next(bm, e);
173  i++;
174  }
175 
176  if (!count) {
177  edges1 = edges;
178  BLI_array_len_set(edges1, BLI_array_len(edges));
179  }
180  else {
181  edges2 = edges;
182  BLI_array_len_set(edges2, BLI_array_len(edges));
183  }
184 
185  BLI_array_clear(edges);
186  count++;
187  }
188 
189  if (edges1 && BLI_array_len(edges1) > 2 &&
190  BM_edge_share_vert_check(edges1[0], edges1[BLI_array_len(edges1) - 1])) {
191  if (edges2 && BLI_array_len(edges2) > 2 &&
192  BM_edge_share_vert_check(edges2[0], edges2[BLI_array_len(edges2) - 1])) {
193  BLI_array_free(edges1);
194  BLI_array_free(edges2);
195  return;
196  }
197  edges1 = edges2;
198  edges2 = NULL;
199  }
200 
201  if (edges2 && BLI_array_len(edges2) > 2 &&
202  BM_edge_share_vert_check(edges2[0], edges2[BLI_array_len(edges2) - 1])) {
203  edges2 = NULL;
204  }
205 
206  /* two unconnected loops, connect the */
207  if (edges1 && edges2) {
208  BMVert *v1, *v2, *v3, *v4;
209  float dvec1[3];
210  float dvec2[3];
211 
212  if (BLI_array_len(edges1) == 1) {
213  v1 = edges1[0]->v1;
214  v2 = edges1[0]->v2;
215  }
216  else {
217  v1 = BM_vert_in_edge(edges1[1], edges1[0]->v1) ? edges1[0]->v2 : edges1[0]->v1;
218  i = BLI_array_len(edges1) - 1;
219  v2 = BM_vert_in_edge(edges1[i - 1], edges1[i]->v1) ? edges1[i]->v2 : edges1[i]->v1;
220  }
221 
222  if (BLI_array_len(edges2) == 1) {
223  v3 = edges2[0]->v1;
224  v4 = edges2[0]->v2;
225  }
226  else {
227  v3 = BM_vert_in_edge(edges2[1], edges2[0]->v1) ? edges2[0]->v2 : edges2[0]->v1;
228  i = BLI_array_len(edges2) - 1;
229  v4 = BM_vert_in_edge(edges2[i - 1], edges2[i]->v1) ? edges2[i]->v2 : edges2[i]->v1;
230  }
231 
232  /* if there is ever bow-tie quads between two edges the problem is here! T30367. */
233 #if 0
234  normal_tri_v3(dvec1, v1->co, v2->co, v4->co);
235  normal_tri_v3(dvec2, v1->co, v4->co, v3->co);
236 #else
237  {
238  /* save some CPU cycles and skip the sqrt and 1 subtraction */
239  float a1[3], a2[3], a3[3];
240  sub_v3_v3v3(a1, v1->co, v2->co);
241  sub_v3_v3v3(a2, v1->co, v4->co);
242  sub_v3_v3v3(a3, v1->co, v3->co);
243  cross_v3_v3v3(dvec1, a1, a2);
244  cross_v3_v3v3(dvec2, a2, a3);
245  }
246 #endif
247  if (dot_v3v3(dvec1, dvec2) < 0.0f) {
248  SWAP(BMVert *, v3, v4);
249  }
250 
255  }
256  else if (edges1) {
257  BMVert *v1, *v2;
258 
259  if (BLI_array_len(edges1) > 1) {
260  v1 = BM_vert_in_edge(edges1[1], edges1[0]->v1) ? edges1[0]->v2 : edges1[0]->v1;
261  i = BLI_array_len(edges1) - 1;
262  v2 = BM_vert_in_edge(edges1[i - 1], edges1[i]->v1) ? edges1[i]->v2 : edges1[i]->v1;
265  }
266  }
267 
269 
270  BLI_array_free(edges1);
271  BLI_array_free(edges2);
272 }
A (mainly) macro array library.
#define BLI_array_grow_one(arr)
Definition: BLI_array.h:101
#define BLI_array_len_set(arr, len)
Definition: BLI_array.h:139
#define BLI_array_declare(arr)
Definition: BLI_array.h:62
#define BLI_array_len(arr)
Definition: BLI_array.h:74
#define BLI_array_clear(arr)
Definition: BLI_array.h:130
#define BLI_array_free(arr)
Definition: BLI_array.h:116
float normal_tri_v3(float n[3], const float v1[3], const float v2[3], const float v3[3])
Definition: math_geom.c:51
MINLINE void sub_v3_v3v3(float r[3], const float a[3], const float b[3])
MINLINE float dot_v3v3(const float a[3], const float b[3]) ATTR_WARN_UNUSED_RESULT
MINLINE void cross_v3_v3v3(float r[3], const float a[3], const float b[3])
#define SWAP(type, a, b)
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum const void *lists _GL_VOID_RET _GL_VOID const GLdouble *equation _GL_VOID_RET _GL_VOID GLdouble GLdouble blue _GL_VOID_RET _GL_VOID GLfloat GLfloat blue _GL_VOID_RET _GL_VOID GLint GLint blue _GL_VOID_RET _GL_VOID GLshort GLshort blue _GL_VOID_RET _GL_VOID GLubyte GLubyte blue _GL_VOID_RET _GL_VOID GLuint GLuint blue _GL_VOID_RET _GL_VOID GLushort GLushort blue _GL_VOID_RET _GL_VOID GLbyte GLbyte GLbyte alpha _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble alpha _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat alpha _GL_VOID_RET _GL_VOID GLint GLint GLint alpha _GL_VOID_RET _GL_VOID GLshort GLshort GLshort alpha _GL_VOID_RET _GL_VOID GLubyte GLubyte GLubyte alpha _GL_VOID_RET _GL_VOID GLuint GLuint GLuint alpha _GL_VOID_RET _GL_VOID GLushort GLushort GLushort alpha _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLint GLsizei GLsizei GLenum type _GL_VOID_RET _GL_VOID GLsizei GLenum GLenum const void *pixels _GL_VOID_RET _GL_VOID const void *pointer _GL_VOID_RET _GL_VOID GLdouble v _GL_VOID_RET _GL_VOID GLfloat v _GL_VOID_RET _GL_VOID GLint GLint i2 _GL_VOID_RET _GL_VOID GLint j _GL_VOID_RET _GL_VOID GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble GLdouble GLdouble zFar _GL_VOID_RET _GL_UINT GLdouble *equation _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLenum GLfloat *v _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLfloat *values _GL_VOID_RET _GL_VOID GLushort *values _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLenum GLdouble *params _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_BOOL GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLenum GLfloat param _GL_VOID_RET _GL_VOID GLenum GLint param _GL_VOID_RET _GL_VOID GLushort pattern _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint const GLdouble *points _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint GLdouble v1
Read Guarded memory(de)allocation.
@ BM_FACE
Definition: bmesh_class.h:386
@ BM_EDGE
Definition: bmesh_class.h:384
@ BM_ELEM_SMOOTH
Definition: bmesh_class.h:477
@ BM_ELEM_TAG
Definition: bmesh_class.h:484
BMEdge * BM_edge_create(BMesh *bm, BMVert *v1, BMVert *v2, const BMEdge *e_example, const eBMCreateFlag create_flag)
Main function for creating a new edge.
Definition: bmesh_core.c:147
@ BM_CREATE_NO_DOUBLE
Definition: bmesh_core.h:29
void BM_mesh_edgenet(BMesh *bm, const bool use_edge_tag, const bool use_new_face_tag)
#define BM_elem_flag_enable(ele, hflag)
Definition: bmesh_inline.h:28
int BMO_iter_elem_count_flag(BMesh *bm, const char itype, void *data, const short oflag, const bool value)
Elem Iter Tool Flag Count.
#define BM_ITER_ELEM(ele, iter, data, itype)
@ BM_EDGES_OF_VERT
ATTR_WARN_UNUSED_RESULT BMesh * bm
void BM_mesh_elem_hflag_disable_all(BMesh *bm, const char htype, const char hflag, const bool respecthide)
void BMO_slot_buffer_hflag_enable(BMesh *bm, BMOpSlot slot_args[BMO_OP_MAX_SLOTS], const char *slot_name, const char htype, const char hflag, const bool do_flush)
BMO_FLAG_BUFFER.
void BMO_slot_buffer_flag_enable(BMesh *bm, BMOpSlot slot_args[BMO_OP_MAX_SLOTS], const char *slot_name, const char htype, const short oflag)
BMO_FLAG_BUFFER.
void BMO_slot_buffer_from_enabled_hflag(BMesh *bm, BMOperator *op, BMOpSlot slot_args[BMO_OP_MAX_SLOTS], const char *slot_name, const char htype, const char hflag)
#define BMO_edge_flag_test(bm, e, oflag)
#define BMO_edge_flag_enable(bm, e, oflag)
void BMO_op_exec(BMesh *bm, BMOperator *op)
BMESH OPSTACK EXEC OP.
bool BMO_op_initf(BMesh *bm, BMOperator *op, const int flag, const char *fmt,...)
#define BMO_ITER(ele, iter, slot_args, slot_name, restrict_flag)
int BMO_slot_buffer_count(BMOpSlot slot_args[BMO_OP_MAX_SLOTS], const char *slot_name)
int BMO_slot_int_get(BMOpSlot slot_args[BMO_OP_MAX_SLOTS], const char *slot_name)
void BMO_op_finish(BMesh *bm, BMOperator *op)
BMESH OPSTACK FINISH OP.
void BMO_slot_buffer_from_enabled_flag(BMesh *bm, BMOperator *op, BMOpSlot slot_args[BMO_OP_MAX_SLOTS], const char *slot_name, const char htype, const short oflag)
bool BMO_slot_bool_get(BMOpSlot slot_args[BMO_OP_MAX_SLOTS], const char *slot_name)
bool BMO_op_callf(BMesh *bm, const int flag, const char *fmt,...)
void BM_face_normal_update(BMFace *f)
bool BM_edge_share_vert_check(BMEdge *e1, BMEdge *e2)
Definition: bmesh_query.c:1358
BLI_INLINE bool BM_vert_in_edge(const BMEdge *e, const BMVert *v) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
ATTR_WARN_UNUSED_RESULT const BMVert * v2
ATTR_WARN_UNUSED_RESULT const BMVert const BMEdge * e
#define ELE_NEW
Definition: bmo_edgenet.c:36
static BMEdge * edge_next(BMesh *bm, BMEdge *e)
Definition: bmo_edgenet.c:88
void bmo_edgenet_fill_exec(BMesh *bm, BMOperator *op)
Definition: bmo_edgenet.c:38
#define EDGE_MARK
Definition: bmo_edgenet.c:33
void bmo_edgenet_prepare_exec(BMesh *bm, BMOperator *op)
Definition: bmo_edgenet.c:106
#define EDGE_VIS
Definition: bmo_edgenet.c:34
int count
BMVert * v1
Definition: bmesh_class.h:134
BMVert * v2
Definition: bmesh_class.h:134
short mat_nr
Definition: bmesh_class.h:281
struct BMOpSlot slots_out[BMO_OP_MAX_SLOTS]
struct BMOpSlot slots_in[BMO_OP_MAX_SLOTS]
float co[3]
Definition: bmesh_class.h:99
int totvert
Definition: bmesh_class.h:297
int totedge
Definition: bmesh_class.h:297