Blender V5.3
editmesh_preselect_edgering.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
8
9#include "MEM_guardedalloc.h"
10
11#include "DNA_userdef_types.h"
12
13#include "BLI_math_vector_c.hh"
14#include "BLI_stack_c.hh"
15
16#include "GPU_immediate.hh"
17#include "GPU_matrix.hh"
18#include "GPU_state.hh"
19
20#include "ED_mesh.hh"
21
22#include "UI_resources.hh"
23
24#include "bmesh.hh"
25
26namespace blender {
27
28/* -------------------------------------------------------------------- */
39
40static void edgering_vcos_get(BMVert *v[2][2],
41 float r_cos[2][2][3],
42 const Span<float3> vert_positions)
43{
44 if (!vert_positions.is_empty()) {
45 int j, k;
46 for (j = 0; j < 2; j++) {
47 for (k = 0; k < 2; k++) {
48 copy_v3_v3(r_cos[j][k], vert_positions[BM_elem_index_get(v[j][k])]);
49 }
50 }
51 }
52 else {
53 int j, k;
54 for (j = 0; j < 2; j++) {
55 for (k = 0; k < 2; k++) {
56 copy_v3_v3(r_cos[j][k], v[j][k]->co);
57 }
58 }
59 }
60}
61
63 float r_cos[2][3],
64 const Span<float3> vert_positions)
65{
66 if (!vert_positions.is_empty()) {
67 int j;
68 for (j = 0; j < 2; j++) {
69 copy_v3_v3(r_cos[j], vert_positions[BM_elem_index_get(v[j])]);
70 }
71 }
72 else {
73 int j;
74 for (j = 0; j < 2; j++) {
75 copy_v3_v3(r_cos[j], v[j]->co);
76 }
77 }
78}
79
84static void edgering_find_order(BMEdge *eed_last, BMEdge *eed, BMVert *eve_last, BMVert *v[2][2])
85{
86 BMLoop *l = eed->l;
87
88 /* find correct order for v[1] */
89 if (!(BM_edge_in_face(eed, l->f) && BM_edge_in_face(eed_last, l->f))) {
90 BMIter liter;
91 BM_ITER_ELEM (l, &liter, l, BM_LOOPS_OF_LOOP) {
92 if (BM_edge_in_face(eed, l->f) && BM_edge_in_face(eed_last, l->f)) {
93 break;
94 }
95 }
96 }
97
98 /* this should never happen */
99 if (!l) {
100 v[0][0] = eed->v1;
101 v[0][1] = eed->v2;
102 v[1][0] = eed_last->v1;
103 v[1][1] = eed_last->v2;
104 return;
105 }
106
107 BMLoop *l_other = BM_loop_other_edge_loop(l, eed->v1);
108 const bool rev = (l_other == l->prev);
109 while (!ELEM(l_other->v, eed_last->v1, eed_last->v2)) {
110 l_other = rev ? l_other->prev : l_other->next;
111 }
112
113 if (l_other->v == eve_last) {
114 v[0][0] = eed->v1;
115 v[0][1] = eed->v2;
116 }
117 else {
118 v[0][0] = eed->v2;
119 v[0][1] = eed->v1;
120 }
121}
122
130
135
137{
139 MEM_delete(psel);
140}
141
143{
144 MEM_SAFE_DELETE(psel->edges);
145 psel->edges_len = 0;
146
147 MEM_SAFE_DELETE(psel->verts);
148 psel->verts_len = 0;
149}
150
151void EDBM_preselect_edgering_draw(EditMesh_PreSelEdgeRing *psel, const float matrix[4][4])
152{
153 if ((psel->edges_len == 0) && (psel->verts_len == 0)) {
154 return;
155 }
156
159
161 GPU_matrix_mul(matrix);
162
163 uint pos = GPU_vertformat_attr_add(immVertexFormat(), "pos", gpu::VertAttrType::SFLOAT_32_32_32);
164
165 if (psel->edges_len > 0) {
166 float viewport[4];
167 GPU_viewport_size_get_f(viewport);
168
170 immUniform2fv("viewportSize", &viewport[2]);
172 immUniform1f("lineWidth", U.pixelsize);
174
175 for (int i = 0; i < psel->edges_len; i++) {
176 immVertex3fv(pos, psel->edges[i][0]);
177 immVertex3fv(pos, psel->edges[i][1]);
178 }
179
180 immEnd();
182 }
183
184 if (psel->verts_len > 0) {
188
189 /* Same size as an edit mode vertex */
190 immUniform1f("size",
191 2.0 * U.pixelsize *
192 max_ff(1.0f, ui::theme::get_value_f(TH_VERTEX_SIZE) * float(M_SQRT2) / 2.0f));
193
195
196 for (int i = 0; i < psel->verts_len; i++) {
197 immVertex3fv(pos, psel->verts[i]);
198 }
199
200 immEnd();
203 }
204
206
207 /* Reset default */
210}
211
214 BMesh * /*bm*/,
215 BMEdge *eed_start,
216 int previewlines,
217 const Span<float3> vert_positions)
218{
219 float v_cos[2][3];
220 float (*verts)[3];
221 int i, tot = 0;
222
223 verts = MEM_new_array_uninitialized<float[3]>(previewlines, __func__);
224
225 edgering_vcos_get_pair(&eed_start->v1, v_cos, vert_positions);
226
227 for (i = 1; i <= previewlines; i++) {
228 const float fac = (i / (float(previewlines) + 1));
229 interp_v3_v3v3(verts[tot], v_cos[0], v_cos[1], fac);
230 tot++;
231 }
232
233 psel->verts = verts;
234 psel->verts_len = previewlines;
235}
236
239 BMesh *bm,
240 BMEdge *eed_start,
241 int previewlines,
242 const Span<float3> vert_positions)
243{
244 BMWalker walker;
245 BMEdge *eed, *eed_last;
246 BMVert *v[2][2] = {{nullptr}}, *eve_last;
247 float (*edges)[2][3] = nullptr;
248 BLI_Stack *edge_stack;
249
250 int i, tot = 0;
251
252 BMW_init(&walker,
253 bm,
261
262 edge_stack = BLI_stack_new(sizeof(BMEdge *), __func__);
263
264 eed_last = nullptr;
265 for (eed = eed_last = static_cast<BMEdge *>(BMW_begin(&walker, eed_start)); eed;
266 eed = static_cast<BMEdge *>(BMW_step(&walker)))
267 {
268 BLI_stack_push(edge_stack, &eed);
269 }
270 BMW_end(&walker);
271
272 eed_start = *static_cast<BMEdge **>(BLI_stack_peek(edge_stack));
273
274 edges = static_cast<float (*)[2][3]>(MEM_new_uninitialized(
275 (sizeof(*edges) * (BLI_stack_count(edge_stack) + (eed_last != eed_start))) * previewlines,
276 __func__));
277
278 eve_last = nullptr;
279 eed_last = nullptr;
280
281 while (!BLI_stack_is_empty(edge_stack)) {
282 BLI_stack_pop(edge_stack, &eed);
283
284 if (eed_last) {
285 if (eve_last) {
286 v[1][0] = v[0][0];
287 v[1][1] = v[0][1];
288 }
289 else {
290 v[1][0] = eed_last->v1;
291 v[1][1] = eed_last->v2;
292 eve_last = eed_last->v1;
293 }
294
295 edgering_find_order(eed_last, eed, eve_last, v);
296 eve_last = v[0][0];
297
298 for (i = 1; i <= previewlines; i++) {
299 const float fac = (i / (float(previewlines) + 1));
300 float v_cos[2][2][3];
301
302 edgering_vcos_get(v, v_cos, vert_positions);
303
304 interp_v3_v3v3(edges[tot][0], v_cos[0][0], v_cos[0][1], fac);
305 interp_v3_v3v3(edges[tot][1], v_cos[1][0], v_cos[1][1], fac);
306 tot++;
307 }
308 }
309 eed_last = eed;
310 }
311
312 if ((eed_last != eed_start) &&
313#ifdef BMW_EDGERING_NGON
314 BM_edge_share_face_check(eed_last, eed_start)
315#else
316 BM_edge_share_quad_check(eed_last, eed_start)
317#endif
318 )
319 {
320 v[1][0] = v[0][0];
321 v[1][1] = v[0][1];
322
323 edgering_find_order(eed_last, eed_start, eve_last, v);
324
325 for (i = 1; i <= previewlines; i++) {
326 const float fac = (i / (float(previewlines) + 1));
327 float v_cos[2][2][3];
328
329 if (!v[0][0] || !v[0][1] || !v[1][0] || !v[1][1]) {
330 continue;
331 }
332
333 edgering_vcos_get(v, v_cos, vert_positions);
334
335 interp_v3_v3v3(edges[tot][0], v_cos[0][0], v_cos[0][1], fac);
336 interp_v3_v3v3(edges[tot][1], v_cos[1][0], v_cos[1][1], fac);
337 tot++;
338 }
339 }
340
341 BLI_stack_free(edge_stack);
342
343 psel->edges = edges;
344 psel->edges_len = tot;
345}
346
348 BMesh *bm,
349 BMEdge *eed_start,
350 int previewlines,
351 const Span<float3> vert_positions)
352{
354
355 if (!vert_positions.is_empty()) {
357 }
358
359 if (BM_edge_is_any_face_len_test(eed_start, 4)) {
361 psel, bm, eed_start, previewlines, vert_positions);
362 }
363 else {
365 psel, bm, eed_start, previewlines, vert_positions);
366 }
367}
368
370
371} // namespace blender
#define M_SQRT2
Read Blender memory allocation and freeing.
#define ELEM(...)
#define BM_elem_index_get(ele)
#define BM_ITER_ELEM(ele, iter, data, itype)
#define BMW_NIL_LAY
#define BMW_MASK_NOP
constexpr bool is_empty() const
Definition BLI_span.hh:264
float get_value_f(int colorid)
BMesh Iterators.
void * MEM_new_uninitialized(size_t len, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)
UserDef U
Definition blender.cc:58
static void edgering_vcos_get_pair(BMVert *v[2], float r_cos[2][3], const Span< float3 > vert_positions)
void BM_mesh_elem_index_ensure(BMesh *bm, const char htype)
EditMesh_PreSelEdgeRing * EDBM_preselect_edgering_create()
@ BMW_FLAG_TEST_HIDDEN
bool BM_edge_in_face(const BMEdge *e, const BMFace *f)
void immEnd()
bool BM_edge_is_any_face_len_test(const BMEdge *e, const int len)
bool BLI_stack_is_empty(const BLI_Stack *stack) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition stack.cc:253
MINLINE void copy_v3_v3(float r[3], const float a[3])
unsigned int uint
Definition BKE_text.h:55
void * MEM_new_array_uninitialized(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
bool BM_edge_share_face_check(BMEdge *e1, BMEdge *e2)
void BLI_stack_free(BLI_Stack *stack) ATTR_NONNULL()
Definition stack.cc:99
ATTR_WARN_UNUSED_RESULT const BMLoop * l
void * BMW_begin(BMWalker *walker, void *start)
GPUVertFormat * immVertexFormat()
void BMW_init(BMWalker *walker, BMesh *bm, int type, short mask_vert, short mask_edge, short mask_face, BMWFlag flag, int layer, BMWDelimitFlag delimit)
Initialize Walker.
void BLI_stack_pop(BLI_Stack *stack, void *dst) ATTR_NONNULL()
Definition stack.cc:141
static void view3d_preselect_mesh_edgering_update_edges_from_edge(EditMesh_PreSelEdgeRing *psel, BMesh *bm, BMEdge *eed_start, int previewlines, const Span< float3 > vert_positions)
void immBegin(GPUPrimType, uint vertex_len)
ATTR_WARN_UNUSED_RESULT const BMVert * v
void immUnbindProgram()
void EDBM_preselect_edgering_update_from_edge(EditMesh_PreSelEdgeRing *psel, BMesh *bm, BMEdge *eed_start, int previewlines, Span< float3 > vert_positions)
void GPU_matrix_mul(const float m[4][4])
@ GPU_BLEND_NONE
Definition GPU_state.hh:88
@ GPU_BLEND_ALPHA
Definition GPU_state.hh:90
void immBindBuiltinProgram(GPUBuiltinShader shader_id)
void EDBM_preselect_edgering_clear(EditMesh_PreSelEdgeRing *psel)
void immUniform1f(const char *name, float x)
@ GPU_DEPTH_NONE
Definition GPU_state.hh:132
@ GPU_DEPTH_LESS_EQUAL
Definition GPU_state.hh:135
void * MEM_new_zeroed(size_t len, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)
void BMW_end(BMWalker *walker)
End Walker.
size_t BLI_stack_count(const BLI_Stack *stack) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition stack.cc:231
void GPU_viewport_size_get_f(float coords[4])
Definition gpu_state.cc:258
@ GPU_SHADER_3D_POLYLINE_UNIFORM_COLOR
@ GPU_SHADER_3D_POINT_UNIFORM_SIZE_UNIFORM_COLOR_AA
void immUniformThemeColor3(int color_id)
void GPU_program_point_size(bool enable)
Definition gpu_state.cc:165
void interp_v3_v3v3(float r[3], const float a[3], const float b[3], float t)
void * BLI_stack_peek(BLI_Stack *stack) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition stack.cc:172
static void edgering_vcos_get(BMVert *v[2][2], float r_cos[2][2][3], const Span< float3 > vert_positions)
uint GPU_vertformat_attr_add(GPUVertFormat *format, StringRef name, gpu::VertAttrType type)
void immVertex3fv(uint attr_id, const float data[3])
bool BM_edge_share_quad_check(BMEdge *e1, BMEdge *e2)
static void edgering_find_order(BMEdge *eed_last, BMEdge *eed, BMVert *eve_last, BMVert *v[2][2])
static void view3d_preselect_mesh_edgering_update_verts_from_edge(EditMesh_PreSelEdgeRing *psel, BMesh *, BMEdge *eed_start, int previewlines, const Span< float3 > vert_positions)
MINLINE float max_ff(float a, float b)
void EDBM_preselect_edgering_destroy(EditMesh_PreSelEdgeRing *psel)
void * BMW_step(BMWalker *walker)
Step Walker.
void BLI_stack_push(BLI_Stack *stack, const void *src) ATTR_NONNULL()
Definition stack.cc:135
void immUniform2fv(const char *name, const float data[2])
void EDBM_preselect_edgering_draw(EditMesh_PreSelEdgeRing *psel, const float matrix[4][4])
void GPU_blend(GPUBlend blend)
Definition gpu_state.cc:44
void GPU_matrix_pop()
BLI_Stack * BLI_stack_new(size_t elem_size, const char *description) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition stack.cc:85
void GPU_depth_test(GPUDepthTest test)
Definition gpu_state.cc:70
@ BMW_DELIMIT_EDGE_RING_NGONS
void GPU_matrix_push()
BMLoop * BM_loop_other_edge_loop(BMLoop *l, BMVert *v)
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
struct BMLoop * l
struct BMLoop * next
struct BMLoop * prev
struct BMVert * v