Blender  V3.3
bmo_mirror.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 
9 #include "MEM_guardedalloc.h"
10 
11 #include "DNA_meshdata_types.h"
12 
13 #include "BLI_math.h"
14 
15 #include "BKE_customdata.h"
16 
17 #include "bmesh.h"
18 #include "intern/bmesh_operators_private.h" /* own include */
19 
20 #define ELE_NEW 1
21 
23 {
24  BMOperator dupeop, weldop;
25  BMOIter siter;
26  BMVert *v;
27  float scale[3] = {1.0f, 1.0f, 1.0f};
28  float dist = BMO_slot_float_get(op->slots_in, "merge_dist");
29  int i;
30  int axis = BMO_slot_int_get(op->slots_in, "axis");
31  bool mirror_u = BMO_slot_bool_get(op->slots_in, "mirror_u");
32  bool mirror_v = BMO_slot_bool_get(op->slots_in, "mirror_v");
33  bool mirror_udim = BMO_slot_bool_get(op->slots_in, "mirror_udim");
34  BMOpSlot *slot_targetmap;
35  BMOpSlot *slot_vertmap;
36 
37  BMO_op_initf(bm, &dupeop, op->flag, "duplicate geom=%s", op, "geom");
38  BMO_op_exec(bm, &dupeop);
39 
41 
42  /* feed old data to transform bmo */
43  scale[axis] = -1.0f;
45  op->flag,
46  "scale verts=%fv vec=%v space=%s use_shapekey=%s",
47  ELE_NEW,
48  scale,
49  op,
50  "matrix",
51  op,
52  "use_shapekey");
53 
54  BMO_op_init(bm, &weldop, op->flag, "weld_verts");
55 
56  slot_targetmap = BMO_slot_get(weldop.slots_in, "targetmap");
57  slot_vertmap = BMO_slot_get(dupeop.slots_out, "vert_map.out");
58 
59  BMO_ITER (v, &siter, op->slots_in, "geom", BM_VERT) {
60  if (fabsf(v->co[axis]) <= dist) {
61  BMVert *v_new = BMO_slot_map_elem_get(slot_vertmap, v);
62  BLI_assert(v_new != NULL);
63  BMO_slot_map_elem_insert(&weldop, slot_targetmap, v_new, v);
64  }
65  }
66 
67  if (mirror_u || mirror_v) {
68  BMFace *f;
69  BMLoop *l;
70  MLoopUV *luv;
71  const int totlayer = CustomData_number_of_layers(&bm->ldata, CD_MLOOPUV);
72  BMIter liter;
73 
74  BMO_ITER (f, &siter, dupeop.slots_out, "geom.out", BM_FACE) {
75  BM_ITER_ELEM (l, &liter, f, BM_LOOPS_OF_FACE) {
76  for (i = 0; i < totlayer; i++) {
78  if (mirror_u) {
79  float uv_u = luv->uv[0];
80  if (mirror_udim) {
81  luv->uv[0] = ceilf(uv_u) - fmodf(uv_u, 1.0f);
82  }
83  else {
84  luv->uv[0] = 1.0f - uv_u;
85  }
86  }
87  if (mirror_v) {
88  float uv_v = luv->uv[1];
89  if (mirror_udim) {
90  luv->uv[1] = ceilf(uv_v) - fmodf(uv_v, 1.0f);
91  }
92  else {
93  luv->uv[1] = 1.0f - uv_v;
94  }
95  }
96  }
97  }
98  }
99  }
100 
101  BMO_op_exec(bm, &weldop);
102 
103  BMO_op_finish(bm, &weldop);
104  BMO_op_finish(bm, &dupeop);
105 
107 }
CustomData interface, see also DNA_customdata_types.h.
int CustomData_number_of_layers(const struct CustomData *data, int type)
void * CustomData_bmesh_get_n(const struct CustomData *data, void *block, int type, int n)
#define BLI_assert(a)
Definition: BLI_assert.h:46
@ CD_MLOOPUV
Read Guarded memory(de)allocation.
#define BM_ALL_NOLOOP
Definition: bmesh_class.h:411
@ BM_FACE
Definition: bmesh_class.h:386
@ BM_VERT
Definition: bmesh_class.h:383
#define BM_ITER_ELEM(ele, iter, data, itype)
@ BM_LOOPS_OF_FACE
ATTR_WARN_UNUSED_RESULT BMesh * bm
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.
float BMO_slot_float_get(BMOpSlot slot_args[BMO_OP_MAX_SLOTS], const char *slot_name)
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)
void BMO_op_init(BMesh *bm, BMOperator *op, int flag, const char *opname)
BMESH OPSTACK INIT OP.
#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.
bool BMO_op_callf(BMesh *bm, int flag, const char *fmt,...)
BMOpSlot * BMO_slot_get(BMOpSlot slot_args[BMO_OP_MAX_SLOTS], const char *identifier)
BMESH OPSTACK GET SLOT.
bool BMO_slot_bool_get(BMOpSlot slot_args[BMO_OP_MAX_SLOTS], const char *slot_name)
BLI_INLINE void BMO_slot_map_elem_insert(BMOperator *op, BMOpSlot *slot, const void *element, void *val)
ATTR_WARN_UNUSED_RESULT const BMLoop * l
ATTR_WARN_UNUSED_RESULT const BMVert * v
void bmo_mirror_exec(BMesh *bm, BMOperator *op)
Definition: bmo_mirror.c:22
#define ELE_NEW
Definition: bmo_mirror.c:20
#define ceilf(x)
Definition: metal/compat.h:225
#define fmodf(x, y)
Definition: metal/compat.h:230
#define fabsf(x)
Definition: metal/compat.h:219
void * data
Definition: bmesh_class.h:51
BMHeader head
Definition: bmesh_class.h:145
struct BMOpSlot slots_out[BMO_OP_MAX_SLOTS]
struct BMOpSlot slots_in[BMO_OP_MAX_SLOTS]
float co[3]
Definition: bmesh_class.h:87
CustomData ldata
Definition: bmesh_class.h:337