Blender V4.3
bmo_edgenet.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
11#include "MEM_guardedalloc.h"
12
13#include "BLI_math_vector.h"
14#include "BLI_vector.hh"
15
16#include "bmesh.hh"
17#include "bmesh_tools.hh"
18
19#include "intern/bmesh_operators_private.hh" /* own include */
20
21using blender::Vector;
22
23#define EDGE_MARK 1
24#define EDGE_VIS 2
25
26#define ELE_NEW 1
27
29{
30 BMOperator op_attr;
31 BMOIter siter;
32 BMFace *f;
33 const short mat_nr = BMO_slot_int_get(op->slots_in, "mat_nr");
34 const bool use_smooth = BMO_slot_bool_get(op->slots_in, "use_smooth");
35 // const int sides = BMO_slot_int_get(op->slots_in, "sides");
36
37 if (!bm->totvert || !bm->totedge) {
38 return;
39 }
40
43
45 BM_mesh_edgenet(bm, true, true); /* TODO: sides. */
46
48
49 BMO_ITER (f, &siter, op->slots_out, "faces.out", BM_FACE) {
50 f->mat_nr = mat_nr;
51 if (use_smooth) {
53 }
54 /* normals are zero'd */
56 }
57
58 /* --- Attribute Fill --- */
59 /* may as well since we have the faces already in a buffer */
61 &op_attr,
62 op->flag,
63 "face_attribute_fill faces=%S use_normals=%b use_data=%b",
64 op,
65 "faces.out",
66 true,
67 true);
68
69 BMO_op_exec(bm, &op_attr);
70
71 /* check if some faces couldn't be touched */
72 if (BMO_slot_buffer_len(op_attr.slots_out, "faces_fail.out")) {
73 BMO_op_callf(bm, op->flag, "recalc_face_normals faces=%S", &op_attr, "faces_fail.out");
74 }
75 BMO_op_finish(bm, &op_attr);
76}
77
79{
80 BMIter iter;
81 BMEdge *e2;
82 int i;
83
84 for (i = 0; i < 2; i++) {
85 BM_ITER_ELEM (e2, &iter, i ? e->v2 : e->v1, BM_EDGES_OF_VERT) {
87 (BMO_edge_flag_test(bm, e2, EDGE_VIS) == false) && (e2 != e))
88 {
89 return e2;
90 }
91 }
92 }
93
94 return nullptr;
95}
96
98{
99 BMOIter siter;
100 BMEdge *e;
101 bool ok = true;
102 int i, count;
103
105
106 /* validate that each edge has at most one other tagged edge in the
107 * disk cycle around each of its vertices */
108 BMO_ITER (e, &siter, op->slots_in, "edges", BM_EDGE) {
109 for (i = 0; i < 2; i++) {
110 count = BMO_iter_elem_count_flag(bm, BM_EDGES_OF_VERT, (i ? e->v2 : e->v1), EDGE_MARK, true);
111 if (count > 2) {
112 ok = false;
113 break;
114 }
115 }
116
117 if (!ok) {
118 break;
119 }
120 }
121
122 /* we don't have valid edge layouts, return */
123 if (!ok) {
124 return;
125 }
126
127 Vector<BMEdge *> edges1;
128 Vector<BMEdge *> edges2;
129 Vector<BMEdge *> *edges;
130
131 /* find connected loops within the input edge */
132 count = 0;
133 while (true) {
134 BMO_ITER (e, &siter, op->slots_in, "edges", BM_EDGE) {
136 if (BMO_iter_elem_count_flag(bm, BM_EDGES_OF_VERT, e->v1, EDGE_MARK, true) == 1 ||
138 {
139 break;
140 }
141 }
142 }
143
144 if (!e) {
145 break;
146 }
147
148 if (!count) {
149 edges = &edges1;
150 }
151 else if (count == 1) {
152 edges = &edges2;
153 }
154 else {
155 break;
156 }
157
158 i = 0;
159 while (e) {
161 edges->append(e);
162
163 e = edge_next(bm, e);
164 i++;
165 }
166
167 count++;
168 }
169
170 if (edges1.size() > 2 && BM_edge_share_vert_check(edges1.first(), edges1.last())) {
171 if (edges2.size() > 2 && BM_edge_share_vert_check(edges2.first(), edges2.last())) {
172 return;
173 }
174 edges1 = edges2;
175 edges2.clear();
176 }
177
178 if (edges2.size() > 2 && BM_edge_share_vert_check(edges2.first(), edges2.last())) {
179 edges2.clear();
180 }
181
182 /* two unconnected loops, connect the */
183 if (!edges1.is_empty() && !edges2.is_empty()) {
184 BMVert *v1, *v2, *v3, *v4;
185 float dvec1[3];
186 float dvec2[3];
187
188 if (edges1.size() == 1) {
189 v1 = edges1[0]->v1;
190 v2 = edges1[0]->v2;
191 }
192 else {
193 v1 = BM_vert_in_edge(edges1[1], edges1[0]->v1) ? edges1[0]->v2 : edges1[0]->v1;
194 i = edges1.size() - 1;
195 v2 = BM_vert_in_edge(edges1[i - 1], edges1[i]->v1) ? edges1[i]->v2 : edges1[i]->v1;
196 }
197
198 if (edges2.size() == 1) {
199 v3 = edges2[0]->v1;
200 v4 = edges2[0]->v2;
201 }
202 else {
203 v3 = BM_vert_in_edge(edges2[1], edges2[0]->v1) ? edges2[0]->v2 : edges2[0]->v1;
204 i = edges2.size() - 1;
205 v4 = BM_vert_in_edge(edges2[i - 1], edges2[i]->v1) ? edges2[i]->v2 : edges2[i]->v1;
206 }
207
208/* if there is ever bow-tie quads between two edges the problem is here! #30367. */
209#if 0
210 normal_tri_v3(dvec1, v1->co, v2->co, v4->co);
211 normal_tri_v3(dvec2, v1->co, v4->co, v3->co);
212#else
213 {
214 /* Save some CPU cycles and skip the `sqrt` and 1 subtraction. */
215 float a1[3], a2[3], a3[3];
216 sub_v3_v3v3(a1, v1->co, v2->co);
217 sub_v3_v3v3(a2, v1->co, v4->co);
218 sub_v3_v3v3(a3, v1->co, v3->co);
219 cross_v3_v3v3(dvec1, a1, a2);
220 cross_v3_v3v3(dvec2, a2, a3);
221 }
222#endif
223 if (dot_v3v3(dvec1, dvec2) < 0.0f) {
224 std::swap(v3, v4);
225 }
226
227 e = BM_edge_create(bm, v1, v3, nullptr, BM_CREATE_NO_DOUBLE);
229 e = BM_edge_create(bm, v2, v4, nullptr, BM_CREATE_NO_DOUBLE);
231 }
232 else if (!edges1.is_empty()) {
233 BMVert *v1, *v2;
234
235 if (edges1.size() > 1) {
236 v1 = BM_vert_in_edge(edges1[1], edges1[0]->v1) ? edges1[0]->v2 : edges1[0]->v1;
237 i = edges1.size() - 1;
238 v2 = BM_vert_in_edge(edges1[i - 1], edges1[i]->v1) ? edges1[i]->v2 : edges1[i]->v1;
239 e = BM_edge_create(bm, v1, v2, nullptr, BM_CREATE_NO_DOUBLE);
241 }
242 }
243
245}
float normal_tri_v3(float n[3], const float v1[3], const float v2[3], const float v3[3])
Definition math_geom.cc:39
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])
Read Guarded memory(de)allocation.
@ BM_ELEM_SMOOTH
@ BM_ELEM_TAG
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.
@ BM_CREATE_NO_DOUBLE
Definition bmesh_core.hh:26
void BM_mesh_edgenet(BMesh *bm, const bool use_edge_tag, const bool use_new_face_tag)
#define BM_elem_flag_enable(ele, hflag)
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)
#define BM_FACE
#define BM_EDGE
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)
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
static BMEdge * edge_next(BMesh *bm, BMEdge *e)
void bmo_edgenet_fill_exec(BMesh *bm, BMOperator *op)
#define EDGE_MARK
void bmo_edgenet_prepare_exec(BMesh *bm, BMOperator *op)
#define EDGE_VIS
int64_t size() const
const T & last(const int64_t n=0) const
bool is_empty() const
const T & first() const
int count
short mat_nr
struct BMOpSlot slots_out[BMO_OP_MAX_SLOTS]
struct BMOpSlot slots_in[BMO_OP_MAX_SLOTS]
float co[3]
int totvert
int totedge