Blender V5.3
meshtools.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2004 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
11
12#include <algorithm>
13
14#include "MEM_guardedalloc.h"
15
16#include "BLI_listbase.hh"
17#include "BLI_math_vector_c.hh"
18#include "BLI_vector.hh"
19#include "BLI_virtual_array.hh"
20
21#include "DNA_key_types.h"
22#include "DNA_mesh_types.h"
23#include "DNA_meshdata_types.h"
24#include "DNA_object_types.h"
25#include "DNA_scene_types.h"
26#include "DNA_screen_types.h"
27#include "DNA_view3d_types.h"
28
29#include "BKE_attribute.hh"
30#include "BKE_context.hh"
31#include "BKE_customdata.hh"
32#include "BKE_editmesh.hh"
33#include "BKE_key.hh"
34#include "BKE_layer.hh"
35#include "BKE_lib_id.hh"
36#include "BKE_material.hh"
37#include "BKE_mesh.hh"
38#include "BKE_mesh_iterators.hh"
39#include "BKE_mesh_runtime.hh"
40#include "BKE_multires.hh"
41#include "BKE_object.hh"
42#include "BKE_paint.hh"
43#include "BKE_paint_bvh.hh"
44#include "BKE_report.hh"
45
46#include "DEG_depsgraph.hh"
49
50#include "DRW_select_buffer.hh"
51
52#include "ED_mesh.hh"
53#include "ED_object.hh"
54#include "ED_sculpt.hh"
55#include "ED_view3d.hh"
56
57#include "WM_api.hh"
58#include "WM_types.hh"
59
60#include "mesh_intern.hh"
61
62namespace blender {
63
64/* -------------------------------------------------------------------- */
69
70static std::string create_mirrored_name(const StringRefNull object_name, const bool mirror)
71{
72 if (!mirror) {
73 return object_name;
74 }
75 if (object_name.endswith(".L")) {
76 return StringRef(object_name).drop_suffix(2) + ".R";
77 }
78 if (object_name.endswith(".R")) {
79 return StringRef(object_name).drop_suffix(2) + ".L";
80 }
81 return object_name;
82}
83
85 const bool ensure_keys_exist,
86 const bool mirror,
87 ReportList *reports)
88{
89 Main *bmain = CTX_data_main(C);
90 Object &active_object = *CTX_data_active_object(C);
91 Depsgraph &depsgraph = *CTX_data_ensure_evaluated_depsgraph(C);
92 Mesh &active_mesh = *id_cast<Mesh *>(active_object.data);
93
94 struct ObjectInfo {
95 StringRefNull name;
96 const Mesh &mesh;
97 };
98
99 auto topology_count_matches = [](const Mesh &a, const Mesh &b) {
100 return a.verts_num == b.verts_num;
101 };
102
103 bool found_object = false;
104 bool found_non_equal_count = false;
105 Vector<ObjectInfo> compatible_objects;
106 CTX_DATA_BEGIN (C, Object *, ob_iter, selected_editable_objects) {
107 if (ob_iter == &active_object) {
108 continue;
109 }
110 if (ob_iter->type != OB_MESH) {
111 continue;
112 }
113 const Object *object_eval = DEG_get_evaluated(&depsgraph, ob_iter);
114 if (!object_eval) {
115 continue;
116 }
117 found_object = true;
118 if (const Mesh *mesh = BKE_object_get_evaluated_mesh(object_eval)) {
119 if (topology_count_matches(*mesh, active_mesh)) {
120 compatible_objects.append({BKE_id_name(ob_iter->id), *mesh});
121 continue;
122 }
123 }
124 /* Fall back to the original mesh. */
125 const Mesh &mesh_orig = *id_cast<const Mesh *>(ob_iter->data);
126 if (topology_count_matches(mesh_orig, active_mesh)) {
127 compatible_objects.append({BKE_id_name(ob_iter->id), mesh_orig});
128 continue;
129 }
130 found_non_equal_count = true;
131 }
133
134 if (!found_object) {
135 BKE_report(reports, RPT_WARNING, "No source mesh objects selected");
136 return OPERATOR_CANCELLED;
137 }
138
139 if (found_non_equal_count) {
140 BKE_report(reports, RPT_WARNING, "Selected meshes must have equal numbers of vertices");
141 return OPERATOR_CANCELLED;
142 }
143
144 if (compatible_objects.is_empty()) {
146 reports, RPT_WARNING, "No additional selected meshes with equal vertex count to join");
147 return OPERATOR_CANCELLED;
148 }
149
150 if (mirror) {
151 for (const ObjectInfo &info : compatible_objects) {
152 if (!info.name.endswith(".L") && !info.name.endswith(".R")) {
153 BKE_report(reports, RPT_ERROR, "Selected objects' names must use .L or .R suffix");
154 return OPERATOR_CANCELLED;
155 }
156 }
157 }
158
159 if (!active_mesh.key) {
160 /* Initialize basis shape key with existing mesh. */
161 active_mesh.key = BKE_key_add(bmain, &active_mesh.id);
162 active_mesh.key->type = KEY_RELATIVE;
164 &active_mesh, active_mesh.key, BKE_keyblock_add(active_mesh.key, nullptr));
165 }
166
167 int mirror_count = 0;
168 int mirror_fail_count = 0;
169 int keys_changed = 0;
170 bool any_keys_added = false;
171 for (const ObjectInfo &info : compatible_objects) {
172 const std::string name = create_mirrored_name(info.name, mirror);
173 if (ensure_keys_exist) {
174 KeyBlock *kb = BKE_keyblock_add(active_mesh.key, name.c_str());
175 BKE_keyblock_convert_from_mesh(&info.mesh, active_mesh.key, kb);
176 any_keys_added = true;
177 if (mirror) {
178 ed::object::shape_key_mirror(&active_object, kb, false, mirror_count, mirror_fail_count);
179 }
180 }
181 else if (KeyBlock *kb = BKE_keyblock_find_name(active_mesh.key, name.c_str())) {
182 keys_changed++;
183 BKE_keyblock_update_from_mesh(&info.mesh, kb);
184 if (mirror) {
185 ed::object::shape_key_mirror(&active_object, kb, false, mirror_count, mirror_fail_count);
186 }
187 }
188 }
189
190 if (!ensure_keys_exist) {
191 if (keys_changed == 0) {
192 BKE_report(reports, RPT_ERROR, "No name matches between selected objects and shape keys");
193 return OPERATOR_FINISHED;
194 }
195 BKE_reportf(reports, RPT_INFO, "Updated %d shape key(s)", keys_changed);
196 }
197
198 if (mirror) {
199 ED_mesh_report_mirror_ex(*reports, mirror_count, mirror_fail_count, SCE_SELECT_VERTEX);
200 }
201
203 WM_main_add_notifier(NC_GEOM | ND_DATA, &active_mesh.id);
204
205 if (any_keys_added && bmain) {
206 /* Adding a new shape key should trigger a rebuild of relationships. */
208 }
209
210 return OPERATOR_FINISHED;
211}
212
214
215/* -------------------------------------------------------------------- */
218
219static MirrTopoStore_t mesh_topo_store = {nullptr, -1, -1, false};
220
222 Mesh *mesh_eval,
223 Mesh **r_mesh_mirror,
224 BMesh **r_bm_mirror)
225{
226 Mesh *mesh_mirror = nullptr;
227 BMesh *bm_mirror = nullptr;
228
229 Mesh *mesh = id_cast<Mesh *>(ob->data);
230 if (mesh_eval != nullptr) {
231 mesh_mirror = mesh_eval;
232 }
233 else if (mesh->runtime->edit_mesh) {
234 bm_mirror = BKE_editmesh_bmesh_get_for_write(ob);
235 }
236 else {
237 mesh_mirror = mesh;
238 }
239
240 *r_mesh_mirror = mesh_mirror;
241 *r_bm_mirror = bm_mirror;
242}
243
245{
246 Mesh *mesh_mirror;
247 BMesh *bm_mirror;
248 mesh_mirror_topo_table_get_meshes(ob, mesh_eval, &mesh_mirror, &bm_mirror);
249
250 ED_mesh_mirrtopo_init(bm_mirror, mesh_mirror, &mesh_topo_store, false);
251}
252
254{
255 /* TODO: store this in object/object-data (keep unused argument for now). */
257}
258
259/* Returns true on success. */
260static bool ed_mesh_mirror_topo_table_update(Object *ob, Mesh *mesh_eval)
261{
262 Mesh *mesh_mirror;
263 BMesh *bm_mirror;
264 mesh_mirror_topo_table_get_meshes(ob, mesh_eval, &mesh_mirror, &bm_mirror);
265
266 if (ED_mesh_mirrtopo_recalc_check(bm_mirror, mesh_mirror, &mesh_topo_store)) {
267 ED_mesh_mirror_topo_table_begin(ob, mesh_eval);
268 }
269 return true;
270}
271
273
274static int mesh_get_x_mirror_vert_spatial(Object *ob, Mesh *mesh_eval, int index)
275{
276 Mesh *mesh = id_cast<Mesh *>(ob->data);
277 const Span<float3> positions = mesh_eval ? mesh_eval->vert_positions() : mesh->vert_positions();
278
279 float vec[3];
280
281 vec[0] = -positions[index][0];
282 vec[1] = positions[index][1];
283 vec[2] = positions[index][2];
284
285 return ED_mesh_mirror_spatial_table_lookup(ob, mesh_eval, vec);
286}
287
289{
291 return -1;
292 }
293
295}
296
297int mesh_get_x_mirror_vert(Object *ob, Mesh *mesh_eval, int index, const bool use_topology)
298{
299 if (use_topology) {
300 return mesh_get_x_mirror_vert_topo(ob, mesh_eval, index);
301 }
302 return mesh_get_x_mirror_vert_spatial(ob, mesh_eval, index);
303}
304
306{
307 float vec[3];
308 int i;
309
310 /* ignore nan verts */
311 if ((isfinite(co[0]) == false) || (isfinite(co[1]) == false) || (isfinite(co[2]) == false)) {
312 return nullptr;
313 }
314
315 vec[0] = -co[0];
316 vec[1] = co[1];
317 vec[2] = co[2];
318
319 i = ED_mesh_mirror_spatial_table_lookup(ob, nullptr, vec);
320 if (i != -1) {
321 return BM_vert_at_index(bm, i);
322 }
323 return nullptr;
324}
325
327{
328 intptr_t poinval;
329 if (!ed_mesh_mirror_topo_table_update(ob, nullptr)) {
330 return nullptr;
331 }
332
333 if (index == -1) {
334 BMIter iter;
335 BMVert *v;
336
337 index = 0;
338 BM_ITER_MESH (v, &iter, bm, BM_VERTS_OF_MESH) {
339 if (v == eve) {
340 break;
341 }
342 index++;
343 }
344
345 if (index == bm->totvert) {
346 return nullptr;
347 }
348 }
349
351
352 if (poinval != -1) {
353 return reinterpret_cast<BMVert *>(poinval);
354 }
355 return nullptr;
356}
357
359 Object *ob, BMesh *bm, BMVert *eve, const float co[3], int index, const bool use_topology)
360{
361 if (use_topology) {
362 return editbmesh_get_x_mirror_vert_topo(ob, bm, eve, index);
363 }
365}
366
368{
369 Mesh *mesh = id_cast<Mesh *>(ob->data);
370 bool use_topology = (mesh->editflag & ME_EDIT_MIRROR_TOPO) != 0;
371 int index_mirr;
372
374 BMVert *eve, *eve_mirr;
375 eve = BM_vert_at_index(bm, index);
376 eve_mirr = editbmesh_get_x_mirror_vert(ob, bm, eve, eve->co, index, use_topology);
377 index_mirr = eve_mirr ? BM_elem_index_get(eve_mirr) : -1;
378 }
379 else {
380 index_mirr = mesh_get_x_mirror_vert(ob, nullptr, index, use_topology);
381 }
382
383 return index_mirr;
384}
385
386#if 0
387
388static float *editmesh_get_mirror_uv(
389 BMesh *bm, int axis, float *uv, float *mirrCent, float *face_cent)
390{
391 float vec[2];
392 float cent_vec[2];
393 float cent[2];
394
395 /* ignore nan verts */
396 if (isnan(uv[0]) || !isfinite(uv[0]) || isnan(uv[1]) || !isfinite(uv[1])) {
397 return nullptr;
398 }
399
400 if (axis) {
401 vec[0] = uv[0];
402 vec[1] = -((uv[1]) - mirrCent[1]) + mirrCent[1];
403
404 cent_vec[0] = face_cent[0];
405 cent_vec[1] = -((face_cent[1]) - mirrCent[1]) + mirrCent[1];
406 }
407 else {
408 vec[0] = -((uv[0]) - mirrCent[0]) + mirrCent[0];
409 vec[1] = uv[1];
410
411 cent_vec[0] = -((face_cent[0]) - mirrCent[0]) + mirrCent[0];
412 cent_vec[1] = face_cent[1];
413 }
414
415 /* TODO: Optimize. */
416 {
417 BMIter iter;
418 BMFace *efa;
419
420 BM_ITER_MESH (efa, &iter, bm, BM_FACES_OF_MESH) {
421 BM_face_uv_calc_center_median(efa, cd_loop_uv_offset, cent);
422
423 if ((fabsf(cent[0] - cent_vec[0]) < 0.001f) && (fabsf(cent[1] - cent_vec[1]) < 0.001f)) {
424 BMIter liter;
425 BMLoop *l;
426
427 BM_ITER_ELEM (l, &liter, efa, BM_LOOPS_OF_FACE) {
428 float *luv2 = BM_ELEM_CD_GET_FLOAT_P(l, cd_loop_uv_offset);
429 if ((fabsf(luv[0] - vec[0]) < 0.001f) && (fabsf(luv[1] - vec[1]) < 0.001f)) {
430 return luv;
431 }
432 }
433 }
434 }
435 }
436
437 return nullptr;
438}
439
440#endif
441
442static uint mirror_facehash(const void *ptr)
443{
444 const MFace *mf = static_cast<const MFace *>(ptr);
445 uint v0, v1;
446
447 if (mf->v4) {
448 v0 = std::min({mf->v1, mf->v2, mf->v3, mf->v4});
449 v1 = std::max({mf->v1, mf->v2, mf->v3, mf->v4});
450 }
451 else {
452 v0 = std::min({mf->v1, mf->v2, mf->v3});
453 v1 = std::min({mf->v1, mf->v2, mf->v3});
454 }
455
456 return ((v0 * 39) ^ (v1 * 31));
457}
458
459static int mirror_facerotation(const MFace *a, const MFace *b)
460{
461 if (b->v4) {
462 if (a->v1 == b->v1 && a->v2 == b->v2 && a->v3 == b->v3 && a->v4 == b->v4) {
463 return 0;
464 }
465 if (a->v4 == b->v1 && a->v1 == b->v2 && a->v2 == b->v3 && a->v3 == b->v4) {
466 return 1;
467 }
468 if (a->v3 == b->v1 && a->v4 == b->v2 && a->v1 == b->v3 && a->v2 == b->v4) {
469 return 2;
470 }
471 if (a->v2 == b->v1 && a->v3 == b->v2 && a->v4 == b->v3 && a->v1 == b->v4) {
472 return 3;
473 }
474 }
475 else {
476 if (a->v1 == b->v1 && a->v2 == b->v2 && a->v3 == b->v3) {
477 return 0;
478 }
479 if (a->v3 == b->v1 && a->v1 == b->v2 && a->v2 == b->v3) {
480 return 1;
481 }
482 if (a->v2 == b->v1 && a->v3 == b->v2 && a->v1 == b->v3) {
483 return 2;
484 }
485 }
486
487 return -1;
488}
489
490static bool mirror_facecmp(const void *a, const void *b)
491{
492 return (mirror_facerotation(static_cast<MFace *>(const_cast<void *>(a)),
493 static_cast<MFace *>(const_cast<void *>(b))) == -1);
494}
495
497{
498 Mesh *mesh = id_cast<Mesh *>(ob->data);
499 MFace mirrormf;
500 const MFace *mf, *hashmf;
501 GHash *fhash;
502 int *mirrorverts, *mirrorfaces;
503
504 const bool use_topology = (mesh->editflag & ME_EDIT_MIRROR_TOPO) != 0;
505 const int totvert = mesh_eval ? mesh_eval->verts_num : mesh->verts_num;
506 const int totface = mesh_eval ? mesh_eval->totface_legacy : mesh->totface_legacy;
507 int a;
508
509 mirrorverts = MEM_new_array_zeroed<int>(totvert, "MirrorVerts");
510 mirrorfaces = MEM_new_array_zeroed<int>(2 * totface, "MirrorFaces");
511
512 const Span<float3> vert_positions = mesh_eval ? mesh_eval->vert_positions() :
513 mesh->vert_positions();
514 const MFace *mface = static_cast<const MFace *>(
515 CustomData_get_layer(&(mesh_eval ? mesh_eval : mesh)->fdata_legacy, CD_MFACE));
516
518
519 for (const int i : vert_positions.index_range()) {
520 mirrorverts[i] = mesh_get_x_mirror_vert(ob, mesh_eval, i, use_topology);
521 }
522
524
525 fhash = BLI_ghash_new_ex(
526 mirror_facehash, mirror_facecmp, "mirror_facehash gh", mesh->totface_legacy);
527 for (a = 0, mf = mface; a < totface; a++, mf++) {
528 BLI_ghash_insert(fhash, (void *)mf, (void *)mf);
529 }
530
531 for (a = 0, mf = mface; a < totface; a++, mf++) {
532 mirrormf.v1 = mirrorverts[mf->v3];
533 mirrormf.v2 = mirrorverts[mf->v2];
534 mirrormf.v3 = mirrorverts[mf->v1];
535 mirrormf.v4 = (mf->v4) ? mirrorverts[mf->v4] : 0;
536
537 /* make sure v4 is not 0 if a quad */
538 if (mf->v4 && mirrormf.v4 == 0) {
539 std::swap(mirrormf.v1, mirrormf.v3);
540 std::swap(mirrormf.v2, mirrormf.v4);
541 }
542
543 hashmf = static_cast<const MFace *>(BLI_ghash_lookup(fhash, &mirrormf));
544 if (hashmf) {
545 mirrorfaces[a * 2] = hashmf - mface;
546 mirrorfaces[a * 2 + 1] = mirror_facerotation(&mirrormf, hashmf);
547 }
548 else {
549 mirrorfaces[a * 2] = -1;
550 }
551 }
552
553 BLI_ghash_free(fhash, nullptr, nullptr);
554 MEM_delete(mirrorverts);
555
556 return mirrorfaces;
557}
558
559/* Selection (vertex and face). */
560
561bool ED_mesh_pick_face(bContext *C, Object *ob, const int mval[2], uint dist_px, uint *r_index)
562{
563 Mesh *mesh = id_cast<Mesh *>(ob->data);
564
565 BLI_assert(mesh && GS(mesh->id.name) == ID_ME);
566
567 if (!mesh || mesh->faces_num == 0) {
568 return false;
569 }
570
571 Depsgraph *depsgraph = CTX_data_ensure_evaluated_depsgraph(C);
574
575 if (dist_px) {
576 /* Sample rect to increase chances of selecting, so that when clicking
577 * on an edge in the back-buffer, we can still select a face. */
579 vc.depsgraph, vc.region, vc.v3d, mval, 1, mesh->faces_num + 1, &dist_px);
580 }
581 else {
582 /* sample only on the exact position */
583 *r_index = DRW_select_buffer_sample_point(vc.depsgraph, vc.region, vc.v3d, mval);
584 }
585
586 if ((*r_index) == 0 || (*r_index) > uint(mesh->faces_num)) {
587 return false;
588 }
589
590 (*r_index)--;
591
592 return true;
593}
594
596 /* context */
597 ARegion *region,
598 const float mval[2],
599 /* mesh data (evaluated) */
600 const IndexRange face,
601 const Span<float3> vert_positions,
602 const int *corner_verts,
603 /* return values */
604 float *r_len_best,
605 int *r_v_idx_best)
606{
607 for (int j = face.size(); j--;) {
608 float sco[2];
609 const int v_idx = corner_verts[face[j]];
610 if (ED_view3d_project_float_object(region, vert_positions[v_idx], sco, V3D_PROJ_TEST_NOP) ==
612 {
613 const float len_test = len_manhattan_v2v2(mval, sco);
614 if (len_test < *r_len_best) {
615 *r_len_best = len_test;
616 *r_v_idx_best = v_idx;
617 }
618 }
619 }
620}
622 bContext *C, Object *ob, const int mval[2], uint dist_px, uint *r_index)
623{
624 Depsgraph *depsgraph = CTX_data_ensure_evaluated_depsgraph(C);
625 uint face_index;
626 Mesh *mesh = id_cast<Mesh *>(ob->data);
627
628 BLI_assert(mesh && GS(mesh->id.name) == ID_ME);
629
630 if (ED_mesh_pick_face(C, ob, mval, dist_px, &face_index)) {
631 const Object *ob_eval = DEG_get_evaluated(depsgraph, ob);
632 const Mesh *mesh_eval = BKE_object_get_evaluated_mesh(ob_eval);
633 if (!mesh_eval) {
634 return false;
635 }
636 ARegion *region = CTX_wm_region(C);
637
638 int v_idx_best = ORIGINDEX_NONE;
639
640 /* find the vert closest to 'mval' */
641 const float mval_f[2] = {float(mval[0]), float(mval[1])};
642 float len_best = FLT_MAX;
643
644 const Span<float3> vert_positions = mesh_eval->vert_positions();
645 const OffsetIndices faces = mesh_eval->faces();
646 const Span<int> corner_verts = mesh_eval->corner_verts();
647
648 const int *index_mp_to_orig = static_cast<const int *>(
650
651 /* tag all verts using this face */
652 if (index_mp_to_orig) {
653 for (const int i : faces.index_range()) {
654 if (index_mp_to_orig[i] == face_index) {
656 mval_f,
657 faces[i],
658 vert_positions,
659 corner_verts.data(),
660 &len_best,
661 &v_idx_best);
662 }
663 }
664 }
665 else {
666 if (face_index < faces.size()) {
668 mval_f,
669 faces[face_index],
670 vert_positions,
671 corner_verts.data(),
672 &len_best,
673 &v_idx_best);
674 }
675 }
676
677 /* Map the `dm` to `mesh`, setting the `r_index` if possible. */
678 if (v_idx_best != ORIGINDEX_NONE) {
679 const int *index_mv_to_orig = static_cast<const int *>(
681 if (index_mv_to_orig) {
682 v_idx_best = index_mv_to_orig[v_idx_best];
683 }
684 }
685
686 if ((v_idx_best != ORIGINDEX_NONE) && (v_idx_best < mesh->verts_num)) {
687 *r_index = v_idx_best;
688 return true;
689 }
690 }
691
692 return false;
693}
694
695bool ED_mesh_pick_edge(bContext *C, Object *ob, const int mval[2], uint dist_px, uint *r_index)
696{
697 Depsgraph *depsgraph = CTX_data_ensure_evaluated_depsgraph(C);
698 Mesh *mesh = id_cast<Mesh *>(ob->data);
699
700 BLI_assert(mesh && GS(mesh->id.name) == ID_ME);
701
702 if (!mesh || mesh->edges_num == 0) {
703 return false;
704 }
705
710
711 uint edge_idx_best = ORIGINDEX_NONE;
712
713 if (dist_px) {
714 /* Sample rect to increase chances of selecting, so that when clicking
715 * on an edge in the back-buffer, we can still select a face. */
717 vc.depsgraph, vc.region, vc.v3d, mval, 1, mesh->edges_num + 1, &dist_px);
718 }
719 else {
720 /* sample only on the exact position */
721 edge_idx_best = DRW_select_buffer_sample_point(vc.depsgraph, vc.region, vc.v3d, mval);
722 }
723
724 if (edge_idx_best == 0 || edge_idx_best > uint(mesh->edges_num)) {
725 return false;
726 }
727
728 edge_idx_best--;
729
730 if (edge_idx_best != ORIGINDEX_NONE) {
731 *r_index = edge_idx_best;
732 return true;
733 }
734
735 return false;
736}
737
746 const float *mval_f; /* [2] */
748
749 /* runtime */
750 float len_best;
752};
753
754static void ed_mesh_pick_vert__mapFunc(void *user_data,
755 int index,
756 const float co[3],
757 const float /*no*/[3])
758{
759 VertPickData *data = static_cast<VertPickData *>(user_data);
760 if (!data->hide_vert.is_empty() && data->hide_vert[index]) {
761 return;
762 }
763 float sco[2];
766 {
767 const float len = len_manhattan_v2v2(data->mval_f, sco);
768 if (len < data->len_best) {
769 data->len_best = len;
770 data->v_idx_best = index;
771 }
772 }
773}
775 bContext *C, Object *ob, const int mval[2], uint dist_px, bool use_zbuf, uint *r_index)
776{
777 Mesh *mesh = id_cast<Mesh *>(ob->data);
778
779 BLI_assert(mesh && GS(mesh->id.name) == ID_ME);
780
781 if (!mesh || mesh->verts_num == 0) {
782 return false;
783 }
784
785 Depsgraph *depsgraph = CTX_data_ensure_evaluated_depsgraph(C);
788
789 if (use_zbuf) {
790 if (dist_px > 0) {
791 /* Sample rectangle to increase chances of selecting, so that when clicking
792 * on an face in the back-buffer, we can still select a vert. */
794 vc.depsgraph, vc.region, vc.v3d, mval, 1, mesh->verts_num + 1, &dist_px);
795 }
796 else {
797 /* sample only on the exact position */
798 *r_index = DRW_select_buffer_sample_point(vc.depsgraph, vc.region, vc.v3d, mval);
799 }
800
801 if ((*r_index) == 0 || (*r_index) > uint(mesh->verts_num)) {
802 return false;
803 }
804
805 (*r_index)--;
806 }
807 else {
808 const Object *ob_eval = DEG_get_evaluated(vc.depsgraph, ob);
809 const Mesh *mesh_eval = BKE_object_get_evaluated_mesh(ob_eval);
810 ARegion *region = vc.region;
811 RegionView3D *rv3d = static_cast<RegionView3D *>(region->regiondata);
812
813 /* find the vert closest to 'mval' */
814 const float mval_f[2] = {float(mval[0]), float(mval[1])};
815
817
818 ED_view3d_init_mats_rv3d(ob, rv3d);
819
820 if (mesh_eval == nullptr) {
821 return false;
822 }
823
824 const bke::AttributeAccessor attributes = mesh->attributes();
825
826 /* setup data */
827 data.region = region;
828 data.mval_f = mval_f;
829 data.len_best = FLT_MAX;
830 data.v_idx_best = -1;
831 data.hide_vert = *attributes.lookup<bool>(".hide_vert", bke::AttrDomain::Point);
832
834
835 if (data.v_idx_best == -1) {
836 return false;
837 }
838
839 *r_index = data.v_idx_best;
840 }
841
842 return true;
843}
844
846{
847 if (ob->mode & OB_MODE_EDIT && ob->type == OB_MESH) {
848 Mesh *mesh = id_cast<Mesh *>(ob->data);
849 if (!mesh->vertex_group_names.is_empty()) {
851 const int cd_dvert_offset = CustomData_get_offset(&bm->vdata, CD_MDEFORMVERT);
852
853 if (cd_dvert_offset != -1) {
855
856 if (eve) {
857 if (r_eve) {
858 *r_eve = eve;
859 }
860 return static_cast<MDeformVert *>(BM_ELEM_CD_GET_VOID_P(eve, cd_dvert_offset));
861 }
862 }
863 }
864 }
865
866 if (r_eve) {
867 *r_eve = nullptr;
868 }
869 return nullptr;
870}
871
873{
874 Mesh *mesh = id_cast<Mesh *>(ob->data);
876 if (r_index) {
877 *r_index = index;
878 }
879 if (index == -1 || mesh->deform_verts().is_empty()) {
880 return nullptr;
881 }
882 MutableSpan<MDeformVert> dverts = mesh->deform_verts_for_write();
883 return &dverts[index];
884}
885
887{
888 if (ob->type == OB_MESH) {
889 if (ob->mode & OB_MODE_EDIT) {
890 return ED_mesh_active_dvert_get_em(ob, nullptr);
891 }
892 return ED_mesh_active_dvert_get_ob(ob, nullptr);
893 }
894 return nullptr;
895}
896
897void EDBM_mesh_stats_multi(const Span<Object *> objects, int totelem[3], int totelem_sel[3])
898{
899 if (totelem) {
900 totelem[0] = 0;
901 totelem[1] = 0;
902 totelem[2] = 0;
903 }
904 if (totelem_sel) {
905 totelem_sel[0] = 0;
906 totelem_sel[1] = 0;
907 totelem_sel[2] = 0;
908 }
909
910 for (Object *obedit : objects) {
911 const BMesh *bm = BKE_editmesh_bmesh_get(obedit);
912 if (totelem) {
913 totelem[0] += bm->totvert;
914 totelem[1] += bm->totedge;
915 totelem[2] += bm->totface;
916 }
917 if (totelem_sel) {
918 totelem_sel[0] += bm->totvertsel;
919 totelem_sel[1] += bm->totedgesel;
920 totelem_sel[2] += bm->totfacesel;
921 }
922 }
923}
924
925void EDBM_mesh_elem_index_ensure_multi(const Span<Object *> objects, const char htype)
926{
927 int elem_offset[4] = {0, 0, 0, 0};
928 for (Object *obedit : objects) {
930 BM_mesh_elem_index_ensure_ex(bm, htype, elem_offset);
931 }
932}
934{
936
937 Mesh *mesh = id_cast<Mesh *>(ob->data);
938 Scene *scene = CTX_data_scene(C);
939
940 if (ob->mode == OB_MODE_SCULPT && mesh->flag & ME_SCULPT_DYNAMIC_TOPOLOGY) {
941 /* Dyntopo not supported. */
942 BKE_report(op->reports, RPT_INFO, "Not supported in dynamic topology sculpting");
943 return OPERATOR_CANCELLED;
944 }
945
946 if (mesh->faces_num == 0 || mesh->verts_num == 0) {
947 return OPERATOR_CANCELLED;
948 }
949
950 if (ob->mode == OB_MODE_SCULPT) {
952 }
953
955
956 if (ob->mode == OB_MODE_SCULPT) {
958 }
959
962
963 BKE_report(op->reports, RPT_INFO, "Mesh faces and vertices reordered spatially");
964
965 return OPERATOR_FINISHED;
966}
967
969{
971 if (!ob || ob->type != OB_MESH) {
972 return false;
973 }
974
975 return true;
976}
977
979{
980 ot->name = "Reorder Mesh Spatially";
981 ot->idname = "MESH_OT_reorder_vertices_spatial";
982 ot->description =
983 "Reorder mesh faces and vertices based on their spatial position for better BVH building "
984 "and sculpting performance.";
985
988
990}
991
992} // namespace blender
#define CTX_DATA_BEGIN(C, Type, instance, member)
#define CTX_DATA_END
CustomData interface, see also DNA_customdata_types.h.
#define ORIGINDEX_NONE
General operations, lookup, etc. for materials.
General operations, lookup, etc. for blender objects.
A BVH for high poly meshes.
Object is a sort of wrapper for general info.
#define V3D_PROJ_TEST_CLIP_DEFAULT
Definition ED_view3d.hh:325
Read Blender memory allocation and freeing.
#define C
Definition RandGen.cpp:29
#define NC_GEOM
Definition WM_types.hh:406
#define ND_DATA
Definition WM_types.hh:552
#define ND_MODIFIER
Definition WM_types.hh:503
#define NC_OBJECT
Definition WM_types.hh:392
#define BLI_INLINE
#define BM_ELEM_CD_GET_FLOAT_P(ele, offset)
#define BM_ELEM_CD_GET_VOID_P(ele, offset)
#define BM_elem_index_get(ele)
#define BM_ITER_ELEM(ele, iter, data, itype)
#define BM_ITER_MESH(ele, iter, bm, itype)
constexpr int64_t size() const
constexpr const T * data() const
Definition BLI_span.hh:219
constexpr IndexRange index_range() const
Definition BLI_span.hh:405
constexpr StringRef()
void append(const T &value)
bool is_empty() const
GAttributeReader lookup(const StringRef name) const
constexpr bool endswith(StringRef suffix) const
#define FLT_MAX
#define isnan
ccl_device_inline dual1 len(const ccl_private dual< T > &a)
Definition math_dual.h:263
void mesh_apply_spatial_organization(Mesh &mesh)
Object * context_active_object(const bContext *C)
void shape_key_mirror(Object *ob, KeyBlock *kb, bool use_topology, int &totmirr, int &totfail)
void geometry_begin(const Scene &scene, Object &ob, const wmOperator *op)
BMesh Iterators.
void WM_main_add_notifier(uint type, void *reference)
int CustomData_get_offset(const CustomData *data, eCustomDataType type)
void EDBM_mesh_elem_index_ensure_multi(Span< Object * > objects, char htype)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
static int mesh_get_x_mirror_vert_spatial(Object *ob, Mesh *mesh_eval, int index)
Definition meshtools.cc:274
void ED_mesh_mirror_topo_table_end(Object *ob)
Definition meshtools.cc:253
@ V3D_PROJ_RET_OK
Definition ED_view3d.hh:273
bool ED_mesh_pick_face_vert(bContext *C, Object *ob, const int mval[2], uint dist_px, uint *r_index)
Definition meshtools.cc:621
uint DRW_select_buffer_find_nearest_to_point(Depsgraph *depsgraph, ARegion *region, View3D *v3d, const int center[2], uint id_min, uint id_max, uint *dist)
@ RPT_WARNING
Definition BKE_report.hh:40
void ED_mesh_mirrtopo_init(BMesh *bm, Mesh *mesh, MirrTopoStore_t *mesh_topo_store, bool skip_bm_vert_array_init)
GHash * BLI_ghash_new_ex(GHashHashFP hashfp, GHashCmpFP cmpfp, const char *info, unsigned int nentries_reserve) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT
Definition BLI_ghash.cc:682
MDeformVert * ED_mesh_active_dvert_get_em(Object *ob, BMVert **r_eve)
Definition meshtools.cc:845
BMesh * BKE_editmesh_bmesh_get_for_write(Mesh *mesh)
Definition editmesh.cc:79
ViewContext ED_view3d_viewcontext_init(bContext *C, Depsgraph *depsgraph)
unsigned int uint
Definition BKE_text.h:55
int ED_mesh_mirror_spatial_table_lookup(Object *ob, Mesh *mesh_eval, const float co[3])
ATTR_WARN_UNUSED_RESULT const BMLoop * l
eV3DProjStatus ED_view3d_project_float_object(const ARegion *region, const float co[3], float r_co[2], eV3DProjTest flag)
void * BLI_ghash_lookup(const GHash *gh, const void *key) ATTR_WARN_UNUSED_RESULT
Definition BLI_ghash.cc:735
static int mirror_facerotation(const MFace *a, const MFace *b)
Definition meshtools.cc:459
static int mesh_get_x_mirror_vert_topo(Object *ob, Mesh *mesh, int index)
Definition meshtools.cc:288
void BKE_keyblock_update_from_mesh(const Mesh *mesh, KeyBlock *kb)
Definition key.cc:1668
void ED_mesh_mirror_spatial_table_end(Object *ob)
MDeformVert * ED_mesh_active_dvert_get_ob(Object *ob, int *r_index)
Definition meshtools.cc:872
int mesh_get_x_mirror_vert(Object *ob, Mesh *mesh_eval, int index, bool use_topology)
Definition meshtools.cc:297
KeyBlock * BKE_keyblock_find_name(Key *key, const char name[])
Definition key.cc:1434
bool ED_mesh_mirrtopo_recalc_check(BMesh *bm, Mesh *mesh, MirrTopoStore_t *mesh_topo_store)
ATTR_WARN_UNUSED_RESULT const BMVert * v
void MESH_OT_reorder_vertices_spatial(wmOperatorType *ot)
Definition meshtools.cc:978
int ED_mesh_mirror_get_vert(Object *ob, int index)
Definition meshtools.cc:367
const char * BKE_id_name(const ID &id)
static void ed_mesh_pick_face_vert__mpoly_find(ARegion *region, const float mval[2], const IndexRange face, const Span< float3 > vert_positions, const int *corner_verts, float *r_len_best, int *r_v_idx_best)
Definition meshtools.cc:595
uint DRW_select_buffer_sample_point(Depsgraph *depsgraph, ARegion *region, View3D *v3d, const int center[2])
static BMVert * editbmesh_get_x_mirror_vert_spatial(Object *ob, BMesh *bm, const float co[3])
Definition meshtools.cc:305
static BMVert * editbmesh_get_x_mirror_vert_topo(Object *ob, BMesh *bm, BMVert *eve, int index)
Definition meshtools.cc:326
void BLI_ghash_insert(GHash *gh, void *key, void *val)
Definition BLI_ghash.cc:711
int * mesh_get_x_mirror_faces(Object *ob, Mesh *mesh_eval)
Definition meshtools.cc:496
static wmOperatorStatus mesh_reorder_vertices_spatial_exec(bContext *C, wmOperator *op)
Definition meshtools.cc:933
BLI_INLINE void mesh_mirror_topo_table_get_meshes(Object *ob, Mesh *mesh_eval, Mesh **r_mesh_mirror, BMesh **r_bm_mirror)
Definition meshtools.cc:221
bool ED_mesh_pick_face(bContext *C, Object *ob, const int mval[2], uint dist_px, uint *r_index)
Definition meshtools.cc:561
void DRW_select_buffer_context_create(Depsgraph *depsgraph, Span< Base * > bases, short select_mode)
@ ME_EDIT_MIRROR_TOPO
void BLI_ghash_free(GHash *gh, GHashKeyFreeFP keyfreefp, GHashValFreeFP valfreefp)
Definition BLI_ghash.cc:866
static void ed_mesh_pick_vert__mapFunc(void *user_data, int index, const float co[3], const float[3])
Definition meshtools.cc:754
Key * BKE_key_add(Main *bmain, ID *id)
Definition key.cc:228
void DEG_id_tag_update(ID *id, unsigned int flags)
void BKE_report(ReportList *reports, eReportType type, const char *message)
Definition report.cc:162
BMesh const char void * data
T * DEG_get_evaluated(const Depsgraph *depsgraph, T *id)
Depsgraph * CTX_data_ensure_evaluated_depsgraph(const bContext *C, bool rna_write_check=false)
void ED_mesh_mirrtopo_free(MirrTopoStore_t *mesh_topo_store)
void BM_face_uv_calc_center_median(const BMFace *f, const int cd_loop_uv_offset, float r_cent[2])
int BKE_mesh_mselect_active_get(const Mesh *mesh, eMSelect_Type type)
bool ED_mesh_pick_edge(bContext *C, Object *ob, const int mval[2], uint dist_px, uint *r_index)
Definition meshtools.cc:695
const void * CustomData_get_layer(const CustomData *data, eCustomDataType type)
static bool mirror_facecmp(const void *a, const void *b)
Definition meshtools.cc:490
void ED_view3d_init_mats_rv3d(const Object *ob, RegionView3D *rv3d)
void ED_mesh_report_mirror_ex(ReportList &reports, int totmirr, int totfail, char selectmode)
@ ME_SCULPT_DYNAMIC_TOPOLOGY
BLI_INLINE BMVert * BM_vert_at_index(BMesh *bm, const int index)
void WM_event_add_notifier(const bContext *C, uint type, void *reference)
void ED_view3d_select_id_validate(const ViewContext *vc)
MINLINE float len_manhattan_v2v2(const float a[2], const float b[2]) ATTR_WARN_UNUSED_RESULT
void EDBM_mesh_stats_multi(Span< Object * > objects, int totelem[3], int totelem_sel[3])
static bool mesh_reorder_vertices_spatial_poll(bContext *C)
Definition meshtools.cc:968
void BKE_reportf(ReportList *reports, eReportType type, const char *format,...) ATTR_PRINTF_FORMAT(3
Definition report.cc:191
static MirrTopoStore_t mesh_topo_store
Definition meshtools.cc:219
Main * CTX_data_main(const bContext *C)
static bool ed_mesh_mirror_topo_table_update(Object *ob, Mesh *mesh_eval)
Definition meshtools.cc:260
KeyBlock * BKE_keyblock_add(Key *key, const char *name)
Definition key.cc:1322
@ ID_RECALC_GEOMETRY
Definition DNA_ID.h:1074
ID_Type GS(const char *name)
Definition DNA_ID.h:410
@ V3D_PROJ_TEST_NOP
Definition ED_view3d.hh:295
wmOperatorStatus ED_mesh_shapes_join_objects_exec(bContext *C, bool ensure_keys_exist, bool mirror, ReportList *reports)
Definition meshtools.cc:84
Base * BKE_view_layer_base_find(ViewLayer *view_layer, Object *ob)
void BKE_mesh_foreach_mapped_vert(const Mesh *mesh, void(*func)(void *user_data, int index, const float co[3], const float no[3]), void *user_data, MeshForeachFlag flag)
const BMesh * BKE_editmesh_bmesh_get(const Object *ob)
Definition editmesh.cc:65
void ED_mesh_mirror_spatial_table_begin(Object *ob, Mesh *mesh_eval)
void ED_mesh_mirror_topo_table_begin(Object *ob, Mesh *mesh_eval)
Definition meshtools.cc:244
BMVert * editbmesh_get_x_mirror_vert(Object *ob, BMesh *bm, BMVert *eve, const float co[3], int index, bool use_topology)
Definition meshtools.cc:358
void DEG_relations_tag_update(Main *bmain)
Scene * CTX_data_scene(const bContext *C)
MDeformVert * ED_mesh_active_dvert_get_only(Object *ob)
Definition meshtools.cc:886
@ OPTYPE_REGISTER
Definition WM_types.hh:188
@ OPTYPE_UNDO
Definition WM_types.hh:190
bool ED_mesh_pick_vert(bContext *C, Object *ob, const int mval[2], uint dist_px, bool use_zbuf, uint *r_index)
Definition meshtools.cc:774
static uint mirror_facehash(const void *ptr)
Definition meshtools.cc:442
void BKE_keyblock_convert_from_mesh(const Mesh *mesh, const Key *key, KeyBlock *kb)
Definition key.cc:1681
void BM_mesh_elem_index_ensure_ex(BMesh *bm, const char htype, int elem_offset[4])
Object * CTX_data_active_object(const bContext *C)
ARegion * CTX_wm_region(const bContext *C)
BMVert * BM_mesh_active_vert_get(BMesh *bm)
void * MEM_new_array_zeroed(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
Mesh * BKE_object_get_evaluated_mesh(const Object *object_eval)
Definition object.cc:4436
_W64 int intptr_t
Definition BKE_text.h:119
static std::string create_mirrored_name(const StringRefNull object_name, const bool mirror)
Definition meshtools.cc:70
#define fabsf
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
ShapekeyContainerType type
struct Key * key
CustomData face_data
CustomData vert_data
const float * mval_f
Definition meshtools.cc:746
VArraySpan< bool > hide_vert
Definition meshtools.cc:745
ViewLayer * view_layer
Definition ED_view3d.hh:80
Depsgraph * depsgraph
Definition ED_view3d.hh:78
wmOperatorStatus(*) exec(bContext *C, wmOperator *op) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1125
const char * description
Definition WM_types.hh:1115
bool(*) poll(bContext *C) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1165
struct ReportList * reports