bmo_mirror.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. * ***** BEGIN GPL LICENSE BLOCK *****
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version 2
  7. * of the License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software Foundation,
  16. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  17. *
  18. * Contributor(s): Joseph Eagar.
  19. *
  20. * ***** END GPL LICENSE BLOCK *****
  21. */
  22. /** \file blender/bmesh/operators/bmo_mirror.c
  23. * \ingroup bmesh
  24. *
  25. * Basic mirror, optionally with UVs's.
  26. */
  27. #include "MEM_guardedalloc.h"
  28. #include "DNA_meshdata_types.h"
  29. #include "BLI_math.h"
  30. #include "BKE_customdata.h"
  31. #include "bmesh.h"
  32. #include "intern/bmesh_operators_private.h" /* own include */
  33. #define ELE_NEW 1
  34. void bmo_mirror_exec(BMesh *bm, BMOperator *op)
  35. {
  36. BMOperator dupeop, weldop;
  37. BMOIter siter;
  38. BMIter iter;
  39. BMVert *v, **vmap;
  40. int vmap_size = 0;
  41. float mtx[4][4];
  42. float imtx[4][4];
  43. float scale[3] = {1.0f, 1.0f, 1.0f};
  44. float dist = BMO_slot_float_get(op->slots_in, "merge_dist");
  45. int i, ototvert;
  46. int axis = BMO_slot_int_get(op->slots_in, "axis");
  47. bool mirror_u = BMO_slot_bool_get(op->slots_in, "mirror_u");
  48. bool mirror_v = BMO_slot_bool_get(op->slots_in, "mirror_v");
  49. BMOpSlot *slot_targetmap;
  50. ototvert = bm->totvert;
  51. BMO_slot_mat4_get(op->slots_in, "matrix", mtx);
  52. invert_m4_m4(imtx, mtx);
  53. BMO_op_initf(bm, &dupeop, op->flag, "duplicate geom=%s", op, "geom");
  54. BMO_op_exec(bm, &dupeop);
  55. BMO_slot_buffer_flag_enable(bm, dupeop.slots_out, "geom.out", BM_ALL_NOLOOP, ELE_NEW);
  56. /* create old -> new mappin */
  57. vmap = BMO_iter_as_arrayN(dupeop.slots_out, "geom.out", BM_VERT, &vmap_size, NULL, 0);
  58. /* feed old data to transform bmo */
  59. scale[axis] = -1.0f;
  60. BMO_op_callf(bm, op->flag, "transform verts=%fv matrix=%m4", ELE_NEW, mtx);
  61. BMO_op_callf(bm, op->flag, "scale verts=%fv vec=%v", ELE_NEW, scale);
  62. BMO_op_callf(bm, op->flag, "transform verts=%fv matrix=%m4", ELE_NEW, imtx);
  63. BMO_op_init(bm, &weldop, op->flag, "weld_verts");
  64. slot_targetmap = BMO_slot_get(weldop.slots_in, "targetmap");
  65. v = BM_iter_new(&iter, bm, BM_VERTS_OF_MESH, NULL);
  66. for (i = 0; i < ototvert; i++) {
  67. if (fabsf(v->co[axis]) <= dist) {
  68. BLI_assert(vmap_size >= i);
  69. BMO_slot_map_elem_insert(&weldop, slot_targetmap, vmap[i], v);
  70. }
  71. v = BM_iter_step(&iter);
  72. }
  73. if (mirror_u || mirror_v) {
  74. BMFace *f;
  75. BMLoop *l;
  76. MLoopUV *luv;
  77. const int totlayer = CustomData_number_of_layers(&bm->ldata, CD_MLOOPUV);
  78. BMIter liter;
  79. BMO_ITER (f, &siter, dupeop.slots_out, "geom.out", BM_FACE) {
  80. BM_ITER_ELEM (l, &liter, f, BM_LOOPS_OF_FACE) {
  81. for (i = 0; i < totlayer; i++) {
  82. luv = CustomData_bmesh_get_n(&bm->ldata, l->head.data, CD_MLOOPUV, i);
  83. if (mirror_u)
  84. luv->uv[0] = 1.0f - luv->uv[0];
  85. if (mirror_v)
  86. luv->uv[1] = 1.0f - luv->uv[1];
  87. }
  88. }
  89. }
  90. }
  91. BMO_op_exec(bm, &weldop);
  92. BMO_op_finish(bm, &weldop);
  93. BMO_op_finish(bm, &dupeop);
  94. BMO_slot_buffer_from_enabled_flag(bm, op, op->slots_out, "geom.out", BM_ALL_NOLOOP, ELE_NEW);
  95. if (vmap)
  96. MEM_freeN(vmap);
  97. }