Blender  V3.3
bmo_edgenet.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 
9 #include "MEM_guardedalloc.h"
10 
11 #include "BLI_array.h"
12 #include "BLI_math.h"
13 
14 #include "bmesh.h"
15 #include "bmesh_tools.h"
16 
17 #include "intern/bmesh_operators_private.h" /* own include */
18 
19 #define EDGE_MARK 1
20 #define EDGE_VIS 2
21 
22 #define ELE_NEW 1
23 
25 {
26  BMOperator op_attr;
27  BMOIter siter;
28  BMFace *f;
29  const short mat_nr = BMO_slot_int_get(op->slots_in, "mat_nr");
30  const bool use_smooth = BMO_slot_bool_get(op->slots_in, "use_smooth");
31  // const int sides = BMO_slot_int_get(op->slots_in, "sides");
32 
33  if (!bm->totvert || !bm->totedge) {
34  return;
35  }
36 
39 
41  BM_mesh_edgenet(bm, true, true); /* TODO: sides. */
42 
44 
45  BMO_ITER (f, &siter, op->slots_out, "faces.out", BM_FACE) {
46  f->mat_nr = mat_nr;
47  if (use_smooth) {
49  }
50  /* normals are zero'd */
52  }
53 
54  /* --- Attribute Fill --- */
55  /* may as well since we have the faces already in a buffer */
57  &op_attr,
58  op->flag,
59  "face_attribute_fill faces=%S use_normals=%b use_data=%b",
60  op,
61  "faces.out",
62  true,
63  true);
64 
65  BMO_op_exec(bm, &op_attr);
66 
67  /* check if some faces couldn't be touched */
68  if (BMO_slot_buffer_len(op_attr.slots_out, "faces_fail.out")) {
69  BMO_op_callf(bm, op->flag, "recalc_face_normals faces=%S", &op_attr, "faces_fail.out");
70  }
71  BMO_op_finish(bm, &op_attr);
72 }
73 
75 {
76  BMIter iter;
77  BMEdge *e2;
78  int i;
79 
80  for (i = 0; i < 2; i++) {
81  BM_ITER_ELEM (e2, &iter, i ? e->v2 : e->v1, BM_EDGES_OF_VERT) {
82  if (BMO_edge_flag_test(bm, e2, EDGE_MARK) &&
83  (BMO_edge_flag_test(bm, e2, EDGE_VIS) == false) && (e2 != e)) {
84  return e2;
85  }
86  }
87  }
88 
89  return NULL;
90 }
91 
93 {
94  BMOIter siter;
95  BMEdge *e;
96  BMEdge **edges1 = NULL, **edges2 = NULL, **edges;
97  BLI_array_declare(edges1);
98  BLI_array_declare(edges2);
99  BLI_array_declare(edges);
100  bool ok = true;
101  int i, count;
102 
104 
105  /* validate that each edge has at most one other tagged edge in the
106  * disk cycle around each of its vertices */
107  BMO_ITER (e, &siter, op->slots_in, "edges", BM_EDGE) {
108  for (i = 0; i < 2; i++) {
109  count = BMO_iter_elem_count_flag(bm, BM_EDGES_OF_VERT, (i ? e->v2 : e->v1), EDGE_MARK, true);
110  if (count > 2) {
111  ok = 0;
112  break;
113  }
114  }
115 
116  if (!ok) {
117  break;
118  }
119  }
120 
121  /* we don't have valid edge layouts, return */
122  if (!ok) {
123  return;
124  }
125 
126  /* find connected loops within the input edge */
127  count = 0;
128  while (1) {
129  BMO_ITER (e, &siter, op->slots_in, "edges", BM_EDGE) {
130  if (!BMO_edge_flag_test(bm, e, EDGE_VIS)) {
131  if (BMO_iter_elem_count_flag(bm, BM_EDGES_OF_VERT, e->v1, EDGE_MARK, true) == 1 ||
133  break;
134  }
135  }
136  }
137 
138  if (!e) {
139  break;
140  }
141 
142  if (!count) {
143  edges = edges1;
144  }
145  else if (count == 1) {
146  edges = edges2;
147  }
148  else {
149  break;
150  }
151 
152  i = 0;
153  while (e) {
155  BLI_array_grow_one(edges);
156  edges[i] = e;
157 
158  e = edge_next(bm, e);
159  i++;
160  }
161 
162  if (!count) {
163  edges1 = edges;
164  BLI_array_len_set(edges1, BLI_array_len(edges));
165  }
166  else {
167  edges2 = edges;
168  BLI_array_len_set(edges2, BLI_array_len(edges));
169  }
170 
171  BLI_array_clear(edges);
172  count++;
173  }
174 
175  if (edges1 && BLI_array_len(edges1) > 2 &&
176  BM_edge_share_vert_check(edges1[0], edges1[BLI_array_len(edges1) - 1])) {
177  if (edges2 && BLI_array_len(edges2) > 2 &&
178  BM_edge_share_vert_check(edges2[0], edges2[BLI_array_len(edges2) - 1])) {
179  BLI_array_free(edges1);
180  BLI_array_free(edges2);
181  return;
182  }
183  edges1 = edges2;
184  edges2 = NULL;
185  }
186 
187  if (edges2 && BLI_array_len(edges2) > 2 &&
188  BM_edge_share_vert_check(edges2[0], edges2[BLI_array_len(edges2) - 1])) {
189  edges2 = NULL;
190  }
191 
192  /* two unconnected loops, connect the */
193  if (edges1 && edges2) {
194  BMVert *v1, *v2, *v3, *v4;
195  float dvec1[3];
196  float dvec2[3];
197 
198  if (BLI_array_len(edges1) == 1) {
199  v1 = edges1[0]->v1;
200  v2 = edges1[0]->v2;
201  }
202  else {
203  v1 = BM_vert_in_edge(edges1[1], edges1[0]->v1) ? edges1[0]->v2 : edges1[0]->v1;
204  i = BLI_array_len(edges1) - 1;
205  v2 = BM_vert_in_edge(edges1[i - 1], edges1[i]->v1) ? edges1[i]->v2 : edges1[i]->v1;
206  }
207 
208  if (BLI_array_len(edges2) == 1) {
209  v3 = edges2[0]->v1;
210  v4 = edges2[0]->v2;
211  }
212  else {
213  v3 = BM_vert_in_edge(edges2[1], edges2[0]->v1) ? edges2[0]->v2 : edges2[0]->v1;
214  i = BLI_array_len(edges2) - 1;
215  v4 = BM_vert_in_edge(edges2[i - 1], edges2[i]->v1) ? edges2[i]->v2 : edges2[i]->v1;
216  }
217 
218  /* if there is ever bow-tie quads between two edges the problem is here! T30367. */
219 #if 0
220  normal_tri_v3(dvec1, v1->co, v2->co, v4->co);
221  normal_tri_v3(dvec2, v1->co, v4->co, v3->co);
222 #else
223  {
224  /* save some CPU cycles and skip the sqrt and 1 subtraction */
225  float a1[3], a2[3], a3[3];
226  sub_v3_v3v3(a1, v1->co, v2->co);
227  sub_v3_v3v3(a2, v1->co, v4->co);
228  sub_v3_v3v3(a3, v1->co, v3->co);
229  cross_v3_v3v3(dvec1, a1, a2);
230  cross_v3_v3v3(dvec2, a2, a3);
231  }
232 #endif
233  if (dot_v3v3(dvec1, dvec2) < 0.0f) {
234  SWAP(BMVert *, v3, v4);
235  }
236 
241  }
242  else if (edges1) {
243  BMVert *v1, *v2;
244 
245  if (BLI_array_len(edges1) > 1) {
246  v1 = BM_vert_in_edge(edges1[1], edges1[0]->v1) ? edges1[0]->v2 : edges1[0]->v1;
247  i = BLI_array_len(edges1) - 1;
248  v2 = BM_vert_in_edge(edges1[i - 1], edges1[i]->v1) ? edges1[i]->v2 : edges1[i]->v1;
251  }
252  }
253 
255 
256  BLI_array_free(edges1);
257  BLI_array_free(edges2);
258 }
A (mainly) macro array library.
#define BLI_array_grow_one(arr)
Definition: BLI_array.h:93
#define BLI_array_len_set(arr, len)
Definition: BLI_array.h:138
#define BLI_array_declare(arr)
Definition: BLI_array.h:50
#define BLI_array_len(arr)
Definition: BLI_array.h:63
#define BLI_array_clear(arr)
Definition: BLI_array.h:128
#define BLI_array_free(arr)
Definition: BLI_array.h:113
float normal_tri_v3(float n[3], const float v1[3], const float v2[3], const float v3[3])
Definition: math_geom.c:33
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:123
@ BM_CREATE_NO_DOUBLE
Definition: bmesh_core.h:14
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:14
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_flag_enable(BMesh *bm, BMOpSlot slot_args[BMO_OP_MAX_SLOTS], const char *slot_name, char htype, short oflag)
BMO_FLAG_BUFFER.
void BMO_slot_buffer_hflag_enable(BMesh *bm, BMOpSlot slot_args[BMO_OP_MAX_SLOTS], const char *slot_name, char htype, char hflag, bool do_flush)
BMO_FLAG_BUFFER.
#define BMO_edge_flag_test(bm, e, oflag)
#define BMO_edge_flag_enable(bm, e, oflag)
void BMO_slot_buffer_from_enabled_hflag(BMesh *bm, BMOperator *op, BMOpSlot slot_args[BMO_OP_MAX_SLOTS], const char *slot_name, char htype, char hflag)
void BMO_op_exec(BMesh *bm, BMOperator *op)
BMESH OPSTACK EXEC OP.
void BMO_slot_buffer_from_enabled_flag(BMesh *bm, BMOperator *op, BMOpSlot slot_args[BMO_OP_MAX_SLOTS], const char *slot_name, char htype, short oflag)
#define BMO_ITER(ele, iter, slot_args, slot_name, restrict_flag)
bool BMO_op_initf(BMesh *bm, BMOperator *op, int flag, const char *fmt,...)
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.
int BMO_slot_buffer_len(BMOpSlot slot_args[BMO_OP_MAX_SLOTS], const char *slot_name)
bool BMO_op_callf(BMesh *bm, int flag, const char *fmt,...)
bool BMO_slot_bool_get(BMOpSlot slot_args[BMO_OP_MAX_SLOTS], const char *slot_name)
void BM_face_normal_update(BMFace *f)
bool BM_edge_share_vert_check(BMEdge *e1, BMEdge *e2)
Definition: bmesh_query.c:1074
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:22
static BMEdge * edge_next(BMesh *bm, BMEdge *e)
Definition: bmo_edgenet.c:74
void bmo_edgenet_fill_exec(BMesh *bm, BMOperator *op)
Definition: bmo_edgenet.c:24
#define EDGE_MARK
Definition: bmo_edgenet.c:19
void bmo_edgenet_prepare_exec(BMesh *bm, BMOperator *op)
Definition: bmo_edgenet.c:92
#define EDGE_VIS
Definition: bmo_edgenet.c:20
int count
BMVert * v1
Definition: bmesh_class.h:122
BMVert * v2
Definition: bmesh_class.h:122
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:87
int totvert
Definition: bmesh_class.h:297
int totedge
Definition: bmesh_class.h:297