Blender  V2.93
bmo_mirror.c
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  */
16 
23 #include "MEM_guardedalloc.h"
24 
25 #include "DNA_meshdata_types.h"
26 
27 #include "BLI_math.h"
28 
29 #include "BKE_customdata.h"
30 
31 #include "bmesh.h"
32 #include "intern/bmesh_operators_private.h" /* own include */
33 
34 #define ELE_NEW 1
35 
37 {
38  BMOperator dupeop, weldop;
39  BMOIter siter;
40  BMVert *v;
41  float scale[3] = {1.0f, 1.0f, 1.0f};
42  float dist = BMO_slot_float_get(op->slots_in, "merge_dist");
43  int i;
44  int axis = BMO_slot_int_get(op->slots_in, "axis");
45  bool mirror_u = BMO_slot_bool_get(op->slots_in, "mirror_u");
46  bool mirror_v = BMO_slot_bool_get(op->slots_in, "mirror_v");
47  bool mirror_udim = BMO_slot_bool_get(op->slots_in, "mirror_udim");
48  BMOpSlot *slot_targetmap;
49  BMOpSlot *slot_vertmap;
50 
51  BMO_op_initf(bm, &dupeop, op->flag, "duplicate geom=%s", op, "geom");
52  BMO_op_exec(bm, &dupeop);
53 
55 
56  /* feed old data to transform bmo */
57  scale[axis] = -1.0f;
59  op->flag,
60  "scale verts=%fv vec=%v space=%s use_shapekey=%s",
61  ELE_NEW,
62  scale,
63  op,
64  "matrix",
65  op,
66  "use_shapekey");
67 
68  BMO_op_init(bm, &weldop, op->flag, "weld_verts");
69 
70  slot_targetmap = BMO_slot_get(weldop.slots_in, "targetmap");
71  slot_vertmap = BMO_slot_get(dupeop.slots_out, "vert_map.out");
72 
73  BMO_ITER (v, &siter, op->slots_in, "geom", BM_VERT) {
74  if (fabsf(v->co[axis]) <= dist) {
75  BMVert *v_new = BMO_slot_map_elem_get(slot_vertmap, v);
76  BLI_assert(v_new != NULL);
77  BMO_slot_map_elem_insert(&weldop, slot_targetmap, v_new, v);
78  }
79  }
80 
81  if (mirror_u || mirror_v) {
82  BMFace *f;
83  BMLoop *l;
84  MLoopUV *luv;
85  const int totlayer = CustomData_number_of_layers(&bm->ldata, CD_MLOOPUV);
86  BMIter liter;
87 
88  BMO_ITER (f, &siter, dupeop.slots_out, "geom.out", BM_FACE) {
89  BM_ITER_ELEM (l, &liter, f, BM_LOOPS_OF_FACE) {
90  for (i = 0; i < totlayer; i++) {
92  if (mirror_u) {
93  float uv_u = luv->uv[0];
94  if (mirror_udim) {
95  luv->uv[0] = ceilf(uv_u) - fmodf(uv_u, 1.0f);
96  }
97  else {
98  luv->uv[0] = 1.0f - uv_u;
99  }
100  }
101  if (mirror_v) {
102  float uv_v = luv->uv[1];
103  if (mirror_udim) {
104  luv->uv[1] = ceilf(uv_v) - fmodf(uv_v, 1.0f);
105  }
106  else {
107  luv->uv[1] = 1.0f - uv_v;
108  }
109  }
110  }
111  }
112  }
113  }
114 
115  BMO_op_exec(bm, &weldop);
116 
117  BMO_op_finish(bm, &weldop);
118  BMO_op_finish(bm, &dupeop);
119 
121 }
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:58
@ 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, const char htype, const 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.
bool BMO_op_initf(BMesh *bm, BMOperator *op, const int flag, const char *fmt,...)
#define BMO_ITER(ele, iter, slot_args, slot_name, restrict_flag)
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.
BMOpSlot * BMO_slot_get(BMOpSlot slot_args[BMO_OP_MAX_SLOTS], const char *identifier)
BMESH OPSTACK GET SLOT.
void BMO_slot_buffer_from_enabled_flag(BMesh *bm, BMOperator *op, BMOpSlot slot_args[BMO_OP_MAX_SLOTS], const char *slot_name, const char htype, const short oflag)
bool BMO_slot_bool_get(BMOpSlot slot_args[BMO_OP_MAX_SLOTS], const char *slot_name)
void BMO_op_init(BMesh *bm, BMOperator *op, const int flag, const char *opname)
BMESH OPSTACK INIT OP.
bool BMO_op_callf(BMesh *bm, const int flag, const char *fmt,...)
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:36
#define ELE_NEW
Definition: bmo_mirror.c:34
#define ceilf(x)
#define fmodf(x, y)
#define fabsf(x)
void * data
Definition: bmesh_class.h:63
BMHeader head
Definition: bmesh_class.h:157
struct BMOpSlot slots_out[BMO_OP_MAX_SLOTS]
struct BMOpSlot slots_in[BMO_OP_MAX_SLOTS]
float co[3]
Definition: bmesh_class.h:99
CustomData ldata
Definition: bmesh_class.h:337