Welcome to mirror list, hosted at ThFree Co, Russian Federation.

bmo_mirror.c « operators « bmesh « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b5b56f4432d4dd48ed4354d1c976541e46963fb3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

/** \file
 * \ingroup bmesh
 *
 * Basic mirror, optionally with UVs's.
 */

#include "MEM_guardedalloc.h"

#include "DNA_meshdata_types.h"

#include "BLI_math.h"

#include "BKE_customdata.h"

#include "bmesh.h"
#include "intern/bmesh_operators_private.h" /* own include */

#define ELE_NEW 1

void bmo_mirror_exec(BMesh *bm, BMOperator *op)
{
  BMOperator dupeop, weldop;
  BMOIter siter;
  BMIter iter;
  BMVert *v, **vmap;
  int vmap_size = 0;
  float mtx[4][4];
  float imtx[4][4];
  float scale[3] = {1.0f, 1.0f, 1.0f};
  float dist = BMO_slot_float_get(op->slots_in, "merge_dist");
  int i, ototvert;
  int axis = BMO_slot_int_get(op->slots_in, "axis");
  bool mirror_u = BMO_slot_bool_get(op->slots_in, "mirror_u");
  bool mirror_v = BMO_slot_bool_get(op->slots_in, "mirror_v");
  bool mirror_udim = BMO_slot_bool_get(op->slots_in, "mirror_udim");
  BMOpSlot *slot_targetmap;

  ototvert = bm->totvert;

  BMO_slot_mat4_get(op->slots_in, "matrix", mtx);
  invert_m4_m4(imtx, mtx);

  BMO_op_initf(bm, &dupeop, op->flag, "duplicate geom=%s", op, "geom");
  BMO_op_exec(bm, &dupeop);

  BMO_slot_buffer_flag_enable(bm, dupeop.slots_out, "geom.out", BM_ALL_NOLOOP, ELE_NEW);

  /* create old -> new mappin */
  vmap = BMO_iter_as_arrayN(dupeop.slots_out, "geom.out", BM_VERT, &vmap_size, NULL, 0);

  /* feed old data to transform bmo */
  scale[axis] = -1.0f;
  BMO_op_callf(bm, op->flag, "transform verts=%fv matrix=%m4", ELE_NEW, mtx);
  BMO_op_callf(bm, op->flag, "scale verts=%fv vec=%v", ELE_NEW, scale);
  BMO_op_callf(bm, op->flag, "transform verts=%fv matrix=%m4", ELE_NEW, imtx);

  BMO_op_init(bm, &weldop, op->flag, "weld_verts");

  slot_targetmap = BMO_slot_get(weldop.slots_in, "targetmap");

  v = BM_iter_new(&iter, bm, BM_VERTS_OF_MESH, NULL);
  for (i = 0; i < ototvert; i++) {
    if (fabsf(v->co[axis]) <= dist) {
      BLI_assert(vmap_size >= i);
      BMO_slot_map_elem_insert(&weldop, slot_targetmap, vmap[i], v);
    }
    v = BM_iter_step(&iter);
  }

  if (mirror_u || mirror_v) {
    BMFace *f;
    BMLoop *l;
    MLoopUV *luv;
    const int totlayer = CustomData_number_of_layers(&bm->ldata, CD_MLOOPUV);
    BMIter liter;

    BMO_ITER (f, &siter, dupeop.slots_out, "geom.out", BM_FACE) {
      BM_ITER_ELEM (l, &liter, f, BM_LOOPS_OF_FACE) {
        for (i = 0; i < totlayer; i++) {
          luv = CustomData_bmesh_get_n(&bm->ldata, l->head.data, CD_MLOOPUV, i);
          if (mirror_u) {
            float uv_u = luv->uv[0];
            if (mirror_udim) {
              luv->uv[0] = ceilf(uv_u) - fmodf(uv_u, 1.0f);
            }
            else {
              luv->uv[0] = 1.0f - uv_u;
            }
          }
          if (mirror_v) {
            float uv_v = luv->uv[1];
            if (mirror_udim) {
              luv->uv[1] = ceilf(uv_v) - fmodf(uv_v, 1.0f);
            }
            else {
              luv->uv[1] = 1.0f - uv_v;
            }
          }
        }
      }
    }
  }

  BMO_op_exec(bm, &weldop);

  BMO_op_finish(bm, &weldop);
  BMO_op_finish(bm, &dupeop);

  BMO_slot_buffer_from_enabled_flag(bm, op, op->slots_out, "geom.out", BM_ALL_NOLOOP, ELE_NEW);

  if (vmap) {
    MEM_freeN(vmap);
  }
}