Blender  V3.3
editface.cc
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 
7 #include "MEM_guardedalloc.h"
8 
9 #include "BLI_bitmap.h"
10 #include "BLI_blenlib.h"
11 #include "BLI_math.h"
12 
13 #include "IMB_imbuf.h"
14 #include "IMB_imbuf_types.h"
15 
16 #include "DNA_mesh_types.h"
17 #include "DNA_meshdata_types.h"
18 #include "DNA_object_types.h"
19 
20 #include "BKE_context.h"
21 #include "BKE_customdata.h"
22 #include "BKE_global.h"
23 #include "BKE_mesh.h"
24 #include "BKE_object.h"
25 
26 #include "ED_mesh.h"
27 #include "ED_screen.h"
28 #include "ED_select_utils.h"
29 #include "ED_view3d.h"
30 
31 #include "WM_api.h"
32 #include "WM_types.h"
33 
34 #include "DEG_depsgraph.h"
35 #include "DEG_depsgraph_query.h"
36 
37 /* own include */
38 
39 void paintface_flush_flags(bContext *C, Object *ob, short flag)
40 {
41  Mesh *me = BKE_mesh_from_object(ob);
42  MPoly *polys, *mp_orig;
43  const int *index_array = nullptr;
44  int totpoly;
45 
46  BLI_assert((flag & ~(SELECT | ME_HIDE)) == 0);
47 
48  if (me == nullptr) {
49  return;
50  }
51 
52  /* NOTE: call #BKE_mesh_flush_hidden_from_verts_ex first when changing hidden flags. */
53 
54  /* we could call this directly in all areas that change selection,
55  * since this could become slow for realtime updates (circle-select for eg) */
56  if (flag & SELECT) {
58  }
59 
62 
63  if (ob_eval == nullptr) {
64  return;
65  }
66 
67  Mesh *me_orig = (Mesh *)ob_eval->runtime.data_orig;
68  Mesh *me_eval = (Mesh *)ob_eval->runtime.data_eval;
69  bool updated = false;
70 
71  if (me_orig != nullptr && me_eval != nullptr && me_orig->totpoly == me->totpoly) {
72  /* Update the COW copy of the mesh. */
73  for (int i = 0; i < me->totpoly; i++) {
74  me_orig->mpoly[i].flag = me->mpoly[i].flag;
75  }
76 
77  /* If the mesh has only deform modifiers, the evaluated mesh shares arrays. */
78  if (me_eval->mpoly == me_orig->mpoly) {
79  updated = true;
80  }
81  /* Mesh polys => Final derived polys */
82  else if ((index_array = (const int *)CustomData_get_layer(&me_eval->pdata, CD_ORIGINDEX))) {
83  polys = me_eval->mpoly;
84  totpoly = me_eval->totpoly;
85 
86  /* loop over final derived polys */
87  for (int i = 0; i < totpoly; i++) {
88  if (index_array[i] != ORIGINDEX_NONE) {
89  /* Copy flags onto the final derived poly from the original mesh poly */
90  mp_orig = me->mpoly + index_array[i];
91  polys[i].flag = mp_orig->flag;
92  }
93  }
94 
95  updated = true;
96  }
97  }
98 
99  if (updated) {
100  if (flag & ME_HIDE) {
102  }
103  else {
105  }
106 
107  DEG_id_tag_update(static_cast<ID *>(ob->data), ID_RECALC_SELECT);
108  }
109  else {
111  }
112 
114 }
115 
116 void paintface_hide(bContext *C, Object *ob, const bool unselected)
117 {
118  Mesh *me = BKE_mesh_from_object(ob);
119  if (me == nullptr || me->totpoly == 0) {
120  return;
121  }
122 
123  for (int i = 0; i < me->totpoly; i++) {
124  MPoly *mpoly = &me->mpoly[i];
125  if ((mpoly->flag & ME_HIDE) == 0) {
126  if (((mpoly->flag & ME_FACE_SEL) == 0) == unselected) {
127  mpoly->flag |= ME_HIDE;
128  }
129  }
130 
131  if (mpoly->flag & ME_HIDE) {
132  mpoly->flag &= ~ME_FACE_SEL;
133  }
134  }
135 
137 
139 }
140 
141 void paintface_reveal(bContext *C, Object *ob, const bool select)
142 {
143  Mesh *me = BKE_mesh_from_object(ob);
144  if (me == nullptr || me->totpoly == 0) {
145  return;
146  }
147 
148  for (int i = 0; i < me->totpoly; i++) {
149  MPoly *mpoly = &me->mpoly[i];
150  if (mpoly->flag & ME_HIDE) {
152  mpoly->flag &= ~ME_HIDE;
153  }
154  }
155 
157 
159 }
160 
161 /* Set object-mode face selection seams based on edge data, uses hash table to find seam edges. */
162 
163 static void select_linked_tfaces_with_seams(Mesh *me, const uint index, const bool select)
164 {
165  bool do_it = true;
166  bool mark = false;
167 
168  BLI_bitmap *edge_tag = BLI_BITMAP_NEW(me->totedge, __func__);
169  BLI_bitmap *poly_tag = BLI_BITMAP_NEW(me->totpoly, __func__);
170 
171  if (index != (uint)-1) {
172  /* only put face under cursor in array */
173  MPoly *mp = &me->mpoly[index];
174  BKE_mesh_poly_edgebitmap_insert(edge_tag, mp, me->mloop + mp->loopstart);
175  BLI_BITMAP_ENABLE(poly_tag, index);
176  }
177  else {
178  /* fill array by selection */
179  for (int i = 0; i < me->totpoly; i++) {
180  MPoly *mp = &me->mpoly[i];
181  if (mp->flag & ME_HIDE) {
182  /* pass */
183  }
184  else if (mp->flag & ME_FACE_SEL) {
185  BKE_mesh_poly_edgebitmap_insert(edge_tag, mp, me->mloop + mp->loopstart);
186  BLI_BITMAP_ENABLE(poly_tag, i);
187  }
188  }
189  }
190 
191  while (do_it) {
192  do_it = false;
193 
194  /* expand selection */
195  for (int i = 0; i < me->totpoly; i++) {
196  MPoly *mp = &me->mpoly[i];
197  if (mp->flag & ME_HIDE) {
198  continue;
199  }
200 
201  if (!BLI_BITMAP_TEST(poly_tag, i)) {
202  mark = false;
203 
204  MLoop *ml = me->mloop + mp->loopstart;
205  for (int b = 0; b < mp->totloop; b++, ml++) {
206  if ((me->medge[ml->e].flag & ME_SEAM) == 0) {
207  if (BLI_BITMAP_TEST(edge_tag, ml->e)) {
208  mark = true;
209  break;
210  }
211  }
212  }
213 
214  if (mark) {
215  BLI_BITMAP_ENABLE(poly_tag, i);
216  BKE_mesh_poly_edgebitmap_insert(edge_tag, mp, me->mloop + mp->loopstart);
217  do_it = true;
218  }
219  }
220  }
221  }
222 
223  MEM_freeN(edge_tag);
224 
225  for (int i = 0; i < me->totpoly; i++) {
226  MPoly *mp = &me->mpoly[i];
227  if (BLI_BITMAP_TEST(poly_tag, i)) {
229  }
230  }
231 
232  MEM_freeN(poly_tag);
233 }
234 
235 void paintface_select_linked(bContext *C, Object *ob, const int mval[2], const bool select)
236 {
237  uint index = (uint)-1;
238 
239  Mesh *me = BKE_mesh_from_object(ob);
240  if (me == nullptr || me->totpoly == 0) {
241  return;
242  }
243 
244  if (mval) {
245  if (!ED_mesh_pick_face(C, ob, mval, ED_MESH_PICK_DEFAULT_FACE_DIST, &index)) {
246  return;
247  }
248  }
249 
251 
253 }
254 
255 bool paintface_deselect_all_visible(bContext *C, Object *ob, int action, bool flush_flags)
256 {
257  Mesh *me = BKE_mesh_from_object(ob);
258  if (me == nullptr) {
259  return false;
260  }
261 
262  if (action == SEL_TOGGLE) {
263  action = SEL_SELECT;
264 
265  for (int i = 0; i < me->totpoly; i++) {
266  MPoly *mpoly = &me->mpoly[i];
267  if ((mpoly->flag & ME_HIDE) == 0 && mpoly->flag & ME_FACE_SEL) {
268  action = SEL_DESELECT;
269  break;
270  }
271  }
272  }
273 
274  bool changed = false;
275 
276  for (int i = 0; i < me->totpoly; i++) {
277  MPoly *mpoly = &me->mpoly[i];
278  if ((mpoly->flag & ME_HIDE) == 0) {
279  switch (action) {
280  case SEL_SELECT:
281  if ((mpoly->flag & ME_FACE_SEL) == 0) {
282  mpoly->flag |= ME_FACE_SEL;
283  changed = true;
284  }
285  break;
286  case SEL_DESELECT:
287  if ((mpoly->flag & ME_FACE_SEL) != 0) {
288  mpoly->flag &= ~ME_FACE_SEL;
289  changed = true;
290  }
291  break;
292  case SEL_INVERT:
293  mpoly->flag ^= ME_FACE_SEL;
294  changed = true;
295  break;
296  }
297  }
298  }
299 
300  if (changed) {
301  if (flush_flags) {
303  }
304  }
305  return changed;
306 }
307 
308 bool paintface_minmax(Object *ob, float r_min[3], float r_max[3])
309 {
310  bool ok = false;
311  float vec[3], bmat[3][3];
312 
313  const Mesh *me = BKE_mesh_from_object(ob);
314  if (!me || !me->mloopuv) {
315  return ok;
316  }
317  const MVert *mvert = me->mvert;
318 
319  copy_m3_m4(bmat, ob->obmat);
320 
321  for (int i = 0; i < me->totpoly; i++) {
322  MPoly *mp = &me->mpoly[i];
323  if (mp->flag & ME_HIDE || !(mp->flag & ME_FACE_SEL)) {
324  continue;
325  }
326 
327  const MLoop *ml = me->mloop + mp->loopstart;
328  for (int b = 0; b < mp->totloop; b++, ml++) {
329  mul_v3_m3v3(vec, bmat, mvert[ml->v].co);
330  add_v3_v3v3(vec, vec, ob->obmat[3]);
331  minmax_v3v3_v3(r_min, r_max, vec);
332  }
333 
334  ok = true;
335  }
336 
337  return ok;
338 }
339 
341  const int mval[2],
342  const SelectPick_Params *params,
343  Object *ob)
344 {
345  MPoly *mpoly_sel = nullptr;
346  uint index;
347  bool changed = false;
348  bool found = false;
349 
350  /* Get the face under the cursor */
351  Mesh *me = BKE_mesh_from_object(ob);
352 
353  if (ED_mesh_pick_face(C, ob, mval, ED_MESH_PICK_DEFAULT_FACE_DIST, &index)) {
354  if (index < me->totpoly) {
355  mpoly_sel = me->mpoly + index;
356  if ((mpoly_sel->flag & ME_HIDE) == 0) {
357  found = true;
358  }
359  }
360  }
361 
362  if (params->sel_op == SEL_OP_SET) {
363  if ((found && params->select_passthrough) && (mpoly_sel->flag & ME_FACE_SEL)) {
364  found = false;
365  }
366  else if (found || params->deselect_all) {
367  /* Deselect everything. */
368  changed |= paintface_deselect_all_visible(C, ob, SEL_DESELECT, false);
369  }
370  }
371 
372  if (found) {
373  me->act_face = (int)index;
374 
375  switch (params->sel_op) {
376  case SEL_OP_ADD: {
377  mpoly_sel->flag |= ME_FACE_SEL;
378  break;
379  }
380  case SEL_OP_SUB: {
381  mpoly_sel->flag &= ~ME_FACE_SEL;
382  break;
383  }
384  case SEL_OP_XOR: {
385  if (mpoly_sel->flag & ME_FACE_SEL) {
386  mpoly_sel->flag &= ~ME_FACE_SEL;
387  }
388  else {
389  mpoly_sel->flag |= ME_FACE_SEL;
390  }
391  break;
392  }
393  case SEL_OP_SET: {
394  mpoly_sel->flag |= ME_FACE_SEL;
395  break;
396  }
397  case SEL_OP_AND: {
398  BLI_assert_unreachable(); /* Doesn't make sense for picking. */
399  break;
400  }
401  }
402 
403  /* image window redraw */
404 
406  ED_region_tag_redraw(CTX_wm_region(C)); /* XXX: should redraw all 3D views. */
407  changed = true;
408  }
409  return changed || found;
410 }
411 
413 {
414  Mesh *me = BKE_mesh_from_object(ob);
415  Mesh *me_eval = BKE_object_get_evaluated_mesh(ob);
416  MVert *mvert_eval, *mv;
417  const int *index_array = nullptr;
418  int totvert;
419  int i;
420 
421  if (me == nullptr) {
422  return;
423  }
424 
425  /* we could call this directly in all areas that change selection,
426  * since this could become slow for realtime updates (circle-select for eg) */
428 
429  if (me_eval == nullptr) {
430  return;
431  }
432 
433  index_array = (const int *)CustomData_get_layer(&me_eval->vdata, CD_ORIGINDEX);
434 
435  mvert_eval = me_eval->mvert;
436  totvert = me_eval->totvert;
437 
438  mv = mvert_eval;
439 
440  if (index_array) {
441  int orig_index;
442  for (i = 0; i < totvert; i++, mv++) {
443  orig_index = index_array[i];
444  if (orig_index != ORIGINDEX_NONE) {
445  mv->flag = me->mvert[index_array[i]].flag;
446  }
447  }
448  }
449  else {
450  for (i = 0; i < totvert; i++, mv++) {
451  mv->flag = me->mvert[i].flag;
452  }
453  }
454 
456 }
457 
459 {
462 }
463 
464 bool paintvert_deselect_all_visible(Object *ob, int action, bool flush_flags)
465 {
466  Mesh *me = BKE_mesh_from_object(ob);
467  if (me == nullptr) {
468  return false;
469  }
470 
471  if (action == SEL_TOGGLE) {
472  action = SEL_SELECT;
473 
474  for (int i = 0; i < me->totvert; i++) {
475  MVert *mvert = &me->mvert[i];
476  if ((mvert->flag & ME_HIDE) == 0 && mvert->flag & SELECT) {
477  action = SEL_DESELECT;
478  break;
479  }
480  }
481  }
482 
483  bool changed = false;
484  for (int i = 0; i < me->totvert; i++) {
485  MVert *mvert = &me->mvert[i];
486  if ((mvert->flag & ME_HIDE) == 0) {
487  switch (action) {
488  case SEL_SELECT:
489  if ((mvert->flag & SELECT) == 0) {
490  mvert->flag |= SELECT;
491  changed = true;
492  }
493  break;
494  case SEL_DESELECT:
495  if ((mvert->flag & SELECT) != 0) {
496  mvert->flag &= ~SELECT;
497  changed = true;
498  }
499  break;
500  case SEL_INVERT:
501  mvert->flag ^= SELECT;
502  changed = true;
503  break;
504  }
505  }
506  }
507 
508  if (changed) {
509  /* handle mselect */
510  if (action == SEL_SELECT) {
511  /* pass */
512  }
513  else if (ELEM(action, SEL_DESELECT, SEL_INVERT)) {
515  }
516  else {
518  }
519 
520  if (flush_flags) {
522  }
523  }
524  return changed;
525 }
526 
527 void paintvert_select_ungrouped(Object *ob, bool extend, bool flush_flags)
528 {
529  Mesh *me = BKE_mesh_from_object(ob);
530 
531  if (me == nullptr || me->dvert == nullptr) {
532  return;
533  }
534 
535  if (!extend) {
537  }
538 
539  for (int i = 0; i < me->totvert; i++) {
540  MVert *mv = &me->mvert[i];
541  MDeformVert *dv = &me->dvert[i];
542  if ((mv->flag & ME_HIDE) == 0) {
543  if (dv->dw == nullptr) {
544  /* if null weight then not grouped */
545  mv->flag |= SELECT;
546  }
547  }
548  }
549 
550  if (flush_flags) {
552  }
553 }
554 
555 void paintvert_hide(bContext *C, Object *ob, const bool unselected)
556 {
557  Mesh *const me = BKE_mesh_from_object(ob);
558 
559  if (me == NULL || me->totvert == 0) {
560  return;
561  }
562 
563  for (int i = 0; i < me->totvert; i++) {
564  MVert *const mvert = &me->mvert[i];
565 
566  if ((mvert->flag & ME_HIDE) == 0) {
567  if (((mvert->flag & SELECT) == 0) == unselected) {
568  mvert->flag |= ME_HIDE;
569  }
570  }
571 
572  if (mvert->flag & ME_HIDE) {
573  mvert->flag &= ~SELECT;
574  }
575  }
576 
578 
581 }
582 
583 void paintvert_reveal(bContext *C, Object *ob, const bool select)
584 {
585  Mesh *const me = BKE_mesh_from_object(ob);
586 
587  if (me == NULL || me->totvert == 0) {
588  return;
589  }
590 
591  for (int i = 0; i < me->totvert; i++) {
592  MVert *const mvert = &me->mvert[i];
593 
594  if (mvert->flag & ME_HIDE) {
596  mvert->flag &= ~ME_HIDE;
597  }
598  }
599 
601 
604 }
struct Depsgraph * CTX_data_ensure_evaluated_depsgraph(const bContext *C)
Definition: context.c:1528
struct ARegion * CTX_wm_region(const bContext *C)
Definition: context.c:749
CustomData interface, see also DNA_customdata_types.h.
#define ORIGINDEX_NONE
void * CustomData_get_layer(const struct CustomData *data, int type)
void BKE_mesh_flush_select_from_verts(struct Mesh *me)
void BKE_mesh_poly_edgebitmap_insert(unsigned int *edge_bitmap, const struct MPoly *mp, const struct MLoop *mloop)
struct Mesh * BKE_mesh_from_object(struct Object *ob)
Definition: mesh.cc:1365
void BKE_mesh_mselect_validate(struct Mesh *me)
Definition: mesh.cc:1701
void BKE_mesh_batch_cache_dirty_tag(struct Mesh *me, eMeshBatchDirtyMode mode)
void BKE_mesh_flush_select_from_polys(struct Mesh *me)
void BKE_mesh_mselect_clear(struct Mesh *me)
Definition: mesh.cc:1695
void BKE_mesh_flush_hidden_from_verts(struct Mesh *me)
void BKE_mesh_flush_hidden_from_polys(struct Mesh *me)
@ BKE_MESH_BATCH_DIRTY_SELECT_PAINT
@ BKE_MESH_BATCH_DIRTY_ALL
General operations, lookup, etc. for blender objects.
struct Mesh * BKE_object_get_evaluated_mesh(const struct Object *object)
#define BLI_assert_unreachable()
Definition: BLI_assert.h:93
#define BLI_assert(a)
Definition: BLI_assert.h:46
#define BLI_BITMAP_NEW(_num, _alloc_string)
Definition: BLI_bitmap.h:40
#define BLI_BITMAP_TEST(_bitmap, _index)
Definition: BLI_bitmap.h:64
#define BLI_BITMAP_ENABLE(_bitmap, _index)
Definition: BLI_bitmap.h:81
unsigned int BLI_bitmap
Definition: BLI_bitmap.h:16
void copy_m3_m4(float m1[3][3], const float m2[4][4])
Definition: math_matrix.c:87
void mul_v3_m3v3(float r[3], const float M[3][3], const float a[3])
Definition: math_matrix.c:897
void minmax_v3v3_v3(float min[3], float max[3], const float vec[3])
Definition: math_vector.c:867
MINLINE void add_v3_v3v3(float r[3], const float a[3], const float b[3])
unsigned int uint
Definition: BLI_sys_types.h:67
#define SET_FLAG_FROM_TEST(value, test, flag)
#define ELEM(...)
struct Depsgraph Depsgraph
Definition: DEG_depsgraph.h:35
void DEG_id_tag_update(struct ID *id, int flag)
struct Object * DEG_get_evaluated_object(const struct Depsgraph *depsgraph, struct Object *object)
@ ID_RECALC_COPY_ON_WRITE
Definition: DNA_ID.h:834
@ ID_RECALC_SELECT
Definition: DNA_ID.h:818
@ CD_ORIGINDEX
@ ME_HIDE
@ ME_FACE_SEL
@ ME_SEAM
Object is a sort of wrapper for general info.
bool ED_mesh_pick_face(struct bContext *C, struct Object *ob, const int mval[2], uint dist_px, uint *r_index)
Definition: meshtools.cc:1179
#define ED_MESH_PICK_DEFAULT_FACE_DIST
Definition: ED_mesh.h:678
void ED_region_tag_redraw(struct ARegion *region)
Definition: area.c:655
@ SEL_SELECT
@ SEL_INVERT
@ SEL_DESELECT
@ SEL_TOGGLE
@ SEL_OP_ADD
@ SEL_OP_SUB
@ SEL_OP_SET
@ SEL_OP_AND
@ SEL_OP_XOR
Contains defines and structs used throughout the imbuf module.
Read Guarded memory(de)allocation.
#define C
Definition: RandGen.cpp:25
#define NC_GEOM
Definition: WM_types.h:343
#define ND_SELECT
Definition: WM_types.h:455
__forceinline const avxb select(const avxb &m, const avxb &t, const avxb &f)
Definition: avxb.h:154
#define SELECT
const Depsgraph * depsgraph
bool paintface_minmax(Object *ob, float r_min[3], float r_max[3])
Definition: editface.cc:308
void paintvert_flush_flags(Object *ob)
Definition: editface.cc:412
void paintface_reveal(bContext *C, Object *ob, const bool select)
Definition: editface.cc:141
void paintvert_tag_select_update(bContext *C, Object *ob)
Definition: editface.cc:458
void paintface_hide(bContext *C, Object *ob, const bool unselected)
Definition: editface.cc:116
void paintvert_select_ungrouped(Object *ob, bool extend, bool flush_flags)
Definition: editface.cc:527
bool paintface_mouse_select(bContext *C, const int mval[2], const SelectPick_Params *params, Object *ob)
Definition: editface.cc:340
void paintface_flush_flags(bContext *C, Object *ob, short flag)
Definition: editface.cc:39
bool paintvert_deselect_all_visible(Object *ob, int action, bool flush_flags)
Definition: editface.cc:464
void paintvert_hide(bContext *C, Object *ob, const bool unselected)
Definition: editface.cc:555
void paintvert_reveal(bContext *C, Object *ob, const bool select)
Definition: editface.cc:583
bool paintface_deselect_all_visible(bContext *C, Object *ob, int action, bool flush_flags)
Definition: editface.cc:255
static void select_linked_tfaces_with_seams(Mesh *me, const uint index, const bool select)
Definition: editface.cc:163
void paintface_select_linked(bContext *C, Object *ob, const int mval[2], const bool select)
Definition: editface.cc:235
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
void(* MEM_freeN)(void *vmemh)
Definition: mallocn.c:27
static const pxr::TfToken b("b", pxr::TfToken::Immortal)
Definition: DNA_ID.h:368
struct MDeformWeight * dw
unsigned int e
unsigned int v
float co[3]
struct MEdge * medge
CustomData vdata
struct MVert * mvert
struct MDeformVert * dvert
int totedge
struct MLoopUV * mloopuv
int act_face
int totvert
struct MLoop * mloop
int totpoly
struct MPoly * mpoly
struct ID * data_eval
struct ID * data_orig
Object_Runtime runtime
float obmat[4][4]
void * data
void WM_event_add_notifier(const bContext *C, uint type, void *reference)