Blender V5.0
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
10
11#include "BLI_math_geom.h"
12#include "BLI_math_vector.h"
13#include "BLI_vector.hh"
14
15#include "bmesh.hh"
16#include "bmesh_tools.hh"
17
18#include "intern/bmesh_operators_private.hh" /* own include */
19
20using blender::Vector;
21
22#define EDGE_MARK 1
23#define EDGE_VIS 2
24
25#define ELE_NEW 1
26
28{
29 BMOperator op_attr;
30 BMOIter siter;
31 BMFace *f;
32 const short mat_nr = BMO_slot_int_get(op->slots_in, "mat_nr");
33 const bool use_smooth = BMO_slot_bool_get(op->slots_in, "use_smooth");
34 // const int sides = BMO_slot_int_get(op->slots_in, "sides");
35
36 if (!bm->totvert || !bm->totedge) {
37 return;
38 }
39
42
44 BM_mesh_edgenet(bm, true, true); /* TODO: sides. */
45
47
48 BMO_ITER (f, &siter, op->slots_out, "faces.out", BM_FACE) {
49 f->mat_nr = mat_nr;
50 if (use_smooth) {
52 }
53 /* Normals are zeroed. */
55 }
56
57 /* --- Attribute Fill --- */
58 /* may as well since we have the faces already in a buffer */
60 &op_attr,
61 op->flag,
62 "face_attribute_fill faces=%S use_normals=%b use_data=%b",
63 op,
64 "faces.out",
65 true,
66 true);
67
68 BMO_op_exec(bm, &op_attr);
69
70 /* check if some faces couldn't be touched */
71 if (BMO_slot_buffer_len(op_attr.slots_out, "faces_fail.out")) {
72 BMO_op_callf(bm, op->flag, "recalc_face_normals faces=%S", &op_attr, "faces_fail.out");
73 }
74 BMO_op_finish(bm, &op_attr);
75}
76
78{
79 BMIter iter;
80 BMEdge *e2;
81 int i;
82
83 for (i = 0; i < 2; i++) {
84 BM_ITER_ELEM (e2, &iter, i ? e->v2 : e->v1, BM_EDGES_OF_VERT) {
86 (BMO_edge_flag_test(bm, e2, EDGE_VIS) == false) && (e2 != e))
87 {
88 return e2;
89 }
90 }
91 }
92
93 return nullptr;
94}
95
97{
98 BMOIter siter;
99 BMEdge *e;
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++) {
110 if (count > 2) {
111 ok = false;
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 Vector<BMEdge *> edges1;
127 Vector<BMEdge *> edges2;
128 Vector<BMEdge *> *edges;
129
130 /* find connected loops within the input edge */
131 count = 0;
132 while (true) {
133 BMO_ITER (e, &siter, op->slots_in, "edges", BM_EDGE) {
135 if (BMO_iter_elem_count_flag(bm, BM_EDGES_OF_VERT, e->v1, EDGE_MARK, true) == 1 ||
137 {
138 break;
139 }
140 }
141 }
142
143 if (!e) {
144 break;
145 }
146
147 if (!count) {
148 edges = &edges1;
149 }
150 else if (count == 1) {
151 edges = &edges2;
152 }
153 else {
154 break;
155 }
156
157 i = 0;
158 while (e) {
160 edges->append(e);
161
162 e = edge_next(bm, e);
163 i++;
164 }
165
166 count++;
167 }
168
169 if (edges1.size() > 2 && BM_edge_share_vert_check(edges1.first(), edges1.last())) {
170 if (edges2.size() > 2 && BM_edge_share_vert_check(edges2.first(), edges2.last())) {
171 return;
172 }
173 edges1 = edges2;
174 edges2.clear();
175 }
176
177 if (edges2.size() > 2 && BM_edge_share_vert_check(edges2.first(), edges2.last())) {
178 edges2.clear();
179 }
180
181 /* two unconnected loops, connect the */
182 if (!edges1.is_empty() && !edges2.is_empty()) {
183 BMVert *v1, *v2, *v3, *v4;
184 float dvec1[3];
185 float dvec2[3];
186
187 if (edges1.size() == 1) {
188 v1 = edges1[0]->v1;
189 v2 = edges1[0]->v2;
190 }
191 else {
192 v1 = BM_vert_in_edge(edges1[1], edges1[0]->v1) ? edges1[0]->v2 : edges1[0]->v1;
193 i = edges1.size() - 1;
194 v2 = BM_vert_in_edge(edges1[i - 1], edges1[i]->v1) ? edges1[i]->v2 : edges1[i]->v1;
195 }
196
197 if (edges2.size() == 1) {
198 v3 = edges2[0]->v1;
199 v4 = edges2[0]->v2;
200 }
201 else {
202 v3 = BM_vert_in_edge(edges2[1], edges2[0]->v1) ? edges2[0]->v2 : edges2[0]->v1;
203 i = edges2.size() - 1;
204 v4 = BM_vert_in_edge(edges2[i - 1], edges2[i]->v1) ? edges2[i]->v2 : edges2[i]->v1;
205 }
206
207 /* Avoid bow tie quads using most planar the triangle pair, see: #30367 & #143905. */
208 normal_tri_v3(dvec1, v1->co, v2->co, v4->co);
209 normal_tri_v3(dvec2, v1->co, v4->co, v3->co);
210 const float dot_24 = dot_v3v3(dvec1, dvec2);
211
212 normal_tri_v3(dvec1, v1->co, v2->co, v3->co);
213 normal_tri_v3(dvec2, v1->co, v3->co, v4->co);
214 const float dot_13 = dot_v3v3(dvec1, dvec2);
215 if (dot_24 < dot_13) {
216 std::swap(v3, v4);
217 }
218
219 e = BM_edge_create(bm, v1, v3, nullptr, BM_CREATE_NO_DOUBLE);
221 e = BM_edge_create(bm, v2, v4, nullptr, BM_CREATE_NO_DOUBLE);
223 }
224 else if (!edges1.is_empty()) {
225 BMVert *v1, *v2;
226
227 if (edges1.size() > 1) {
228 v1 = BM_vert_in_edge(edges1[1], edges1[0]->v1) ? edges1[0]->v2 : edges1[0]->v1;
229 i = edges1.size() - 1;
230 v2 = BM_vert_in_edge(edges1[i - 1], edges1[i]->v1) ? edges1[i]->v2 : edges1[i]->v1;
231 e = BM_edge_create(bm, v1, v2, nullptr, BM_CREATE_NO_DOUBLE);
233 }
234 }
235
237}
float normal_tri_v3(float n[3], const float v1[3], const float v2[3], const float v3[3])
Definition math_geom.cc:41
MINLINE float dot_v3v3(const float a[3], const float b[3]) ATTR_WARN_UNUSED_RESULT
@ 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:30
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
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
#define EDGE_MARK
Definition bmo_bridge.cc:26
static BMEdge * edge_next(BMesh *bm, BMEdge *e)
void bmo_edgenet_fill_exec(BMesh *bm, BMOperator *op)
void bmo_edgenet_prepare_exec(BMesh *bm, BMOperator *op)
#define EDGE_VIS
int64_t size() const
void append(const T &value)
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]
i
Definition text_draw.cc:230