bmo_poke.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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): Francisco De La Cruz
  19. *
  20. * ***** END GPL LICENSE BLOCK *****
  21. */
  22. /** \file blender/bmesh/operators/bmo_poke.c
  23. * \ingroup bmesh
  24. *
  25. * Pokes a face.
  26. *
  27. * Splits a face into a triangle fan.
  28. */
  29. #include "BLI_math.h"
  30. #include "bmesh.h"
  31. #include "intern/bmesh_operators_private.h" /* own include */
  32. #include "BKE_customdata.h"
  33. #define ELE_NEW 1
  34. /**
  35. * Pokes a face
  36. *
  37. * Splits a face into a triangle fan.
  38. * Iterate over all selected faces, create a new center vertex and
  39. * create triangles between original face edges and new center vertex.
  40. */
  41. void bmo_poke_exec(BMesh *bm, BMOperator *op)
  42. {
  43. const int cd_loop_mdisp_offset = CustomData_get_offset(&bm->ldata, CD_MDISPS);
  44. BMOIter oiter;
  45. BMFace *f;
  46. const float offset = BMO_slot_float_get(op->slots_in, "offset");
  47. const bool use_relative_offset = BMO_slot_bool_get(op->slots_in, "use_relative_offset");
  48. const int center_mode = BMO_slot_int_get(op->slots_in, "center_mode");
  49. void (*bm_face_calc_center_fn)(const BMFace *f, float r_cent[3]);
  50. switch (center_mode) {
  51. case BMOP_POKE_MEAN_WEIGHTED:
  52. bm_face_calc_center_fn = BM_face_calc_center_mean_weighted;
  53. break;
  54. case BMOP_POKE_BOUNDS:
  55. bm_face_calc_center_fn = BM_face_calc_center_bounds;
  56. break;
  57. case BMOP_POKE_MEAN:
  58. bm_face_calc_center_fn = BM_face_calc_center_mean;
  59. break;
  60. default:
  61. BLI_assert(0);
  62. return;
  63. }
  64. BMO_ITER (f, &oiter, op->slots_in, "faces", BM_FACE) {
  65. BMFace *f_new;
  66. float f_center[3], f_center_mean[3];
  67. BMVert *v_center = NULL;
  68. BMLoop *l_iter, *l_first;
  69. /* only interpolate the central loop from the face once,
  70. * then copy to all others in the fan */
  71. BMLoop *l_center_example;
  72. /* 1.0 or the average length from the center to the face verts */
  73. float offset_fac;
  74. int i;
  75. bm_face_calc_center_fn(f, f_center);
  76. v_center = BM_vert_create(bm, f_center, NULL, BM_CREATE_NOP);
  77. BMO_vert_flag_enable(bm, v_center, ELE_NEW);
  78. if (cd_loop_mdisp_offset != -1) {
  79. if (center_mode == BMOP_POKE_MEAN) {
  80. copy_v3_v3(f_center_mean, f_center);
  81. }
  82. else {
  83. BM_face_calc_center_mean(f, f_center_mean);
  84. }
  85. }
  86. /* handled by BM_loop_interp_from_face */
  87. // BM_vert_interp_from_face(bm, v_center, f);
  88. if (use_relative_offset) {
  89. offset_fac = 0.0f;
  90. }
  91. else {
  92. offset_fac = 1.0f;
  93. }
  94. i = 0;
  95. l_iter = l_first = BM_FACE_FIRST_LOOP(f);
  96. do {
  97. BMLoop *l_new;
  98. f_new = BM_face_create_quad_tri(bm, l_iter->v, l_iter->next->v, v_center, NULL, f, BM_CREATE_NOP);
  99. l_new = BM_FACE_FIRST_LOOP(f_new);
  100. if (i == 0) {
  101. l_center_example = l_new->prev;
  102. BM_loop_interp_from_face(bm, l_center_example, f, true, false);
  103. }
  104. else {
  105. BM_elem_attrs_copy(bm, bm, l_center_example, l_new->prev);
  106. }
  107. /* Copy Loop Data */
  108. BM_elem_attrs_copy(bm, bm, l_iter, l_new);
  109. BM_elem_attrs_copy(bm, bm, l_iter->next, l_new->next);
  110. BMO_face_flag_enable(bm, f_new, ELE_NEW);
  111. if (cd_loop_mdisp_offset != -1) {
  112. float f_new_center[3];
  113. BM_face_calc_center_mean(f_new, f_new_center);
  114. BM_face_interp_multires_ex(bm, f_new, f, f_new_center, f_center, cd_loop_mdisp_offset);
  115. }
  116. if (use_relative_offset) {
  117. offset_fac += len_v3v3(f_center, l_iter->v->co);
  118. }
  119. } while ((void)i++, (l_iter = l_iter->next) != l_first);
  120. if (use_relative_offset) {
  121. offset_fac /= (float)f->len;
  122. }
  123. /* else remain at 1.0 */
  124. copy_v3_v3(v_center->no, f->no);
  125. madd_v3_v3fl(v_center->co, v_center->no, offset * offset_fac);
  126. /* Kill Face */
  127. BM_face_kill(bm, f);
  128. }
  129. BMO_slot_buffer_from_enabled_flag(bm, op, op->slots_out, "verts.out", BM_VERT, ELE_NEW);
  130. BMO_slot_buffer_from_enabled_flag(bm, op, op->slots_out, "faces.out", BM_FACE, ELE_NEW);
  131. }