bmo_fill_edgeloop.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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): Campbell Barton.
  19. *
  20. * ***** END GPL LICENSE BLOCK *****
  21. */
  22. /** \file blender/bmesh/operators/bmo_fill_edgeloop.c
  23. * \ingroup bmesh
  24. *
  25. * Fill discreet edge loop(s) with faces.
  26. */
  27. #include "MEM_guardedalloc.h"
  28. #include "BLI_listbase.h"
  29. #include "bmesh.h"
  30. #include "intern/bmesh_operators_private.h" /* own include */
  31. #define VERT_USED 1
  32. #define EDGE_MARK 2
  33. #define ELE_OUT 4
  34. void bmo_edgeloop_fill_exec(BMesh *bm, BMOperator *op)
  35. {
  36. /* first collect an array of unique from the edges */
  37. const int tote = BMO_slot_buffer_count(op->slots_in, "edges");
  38. const int totv = tote; /* these should be the same */
  39. BMVert **verts = MEM_mallocN(sizeof(*verts) * totv, __func__);
  40. BMVert *v;
  41. BMEdge *e;
  42. int i;
  43. bool ok = true;
  44. BMOIter oiter;
  45. const short mat_nr = BMO_slot_int_get(op->slots_in, "mat_nr");
  46. const bool use_smooth = BMO_slot_bool_get(op->slots_in, "use_smooth");
  47. /* 'VERT_USED' will be disabled, so enable and fill the array */
  48. i = 0;
  49. BMO_ITER (e, &oiter, op->slots_in, "edges", BM_EDGE) {
  50. BMIter viter;
  51. BMO_edge_flag_enable(bm, e, EDGE_MARK);
  52. BM_ITER_ELEM (v, &viter, e, BM_VERTS_OF_EDGE) {
  53. if (BMO_vert_flag_test(bm, v, VERT_USED) == false) {
  54. if (i == tote) {
  55. goto cleanup;
  56. }
  57. BMO_vert_flag_enable(bm, v, VERT_USED);
  58. verts[i++] = v;
  59. }
  60. }
  61. }
  62. /* we have a different number of verts to edges */
  63. if (i != tote) {
  64. goto cleanup;
  65. }
  66. /* loop over connected flagged edges and fill in faces, this is made slightly more
  67. * complicated because there may be multiple disconnected loops to fill. */
  68. /* sanity check - that each vertex has 2 edge users */
  69. for (i = 0; i < totv; i++) {
  70. v = verts[i];
  71. /* count how many flagged edges this vertex uses */
  72. if (BMO_iter_elem_count_flag(bm, BM_EDGES_OF_VERT, v, EDGE_MARK, true) != 2) {
  73. ok = false;
  74. break;
  75. }
  76. }
  77. if (ok) {
  78. /* note: in the case of multiple loops, this over-allocs (which is fine) */
  79. BMVert **f_verts = MEM_mallocN(sizeof(*verts) * totv, __func__);
  80. BMIter eiter;
  81. /* build array of connected verts and edges */
  82. BMEdge *e_prev = NULL;
  83. BMEdge *e_next = NULL;
  84. int totv_used = 0;
  85. while (totv_used < totv) {
  86. for (i = 0; i < totv; i++) {
  87. v = verts[i];
  88. if (BMO_vert_flag_test(bm, v, VERT_USED)) {
  89. break;
  90. }
  91. }
  92. /* this should never fail, as long as (totv_used < totv)
  93. * we should have marked verts available */
  94. BLI_assert(BMO_vert_flag_test(bm, v, VERT_USED));
  95. /* watch it, 'i' is used for final face length */
  96. i = 0;
  97. do {
  98. /* we know that there are 2 edges per vertex so no need to check */
  99. BM_ITER_ELEM (e, &eiter, v, BM_EDGES_OF_VERT) {
  100. if (BMO_edge_flag_test(bm, e, EDGE_MARK)) {
  101. if (e != e_prev) {
  102. e_next = e;
  103. break;
  104. }
  105. }
  106. }
  107. /* fill in the array */
  108. f_verts[i] = v;
  109. BMO_vert_flag_disable(bm, v, VERT_USED);
  110. totv_used++;
  111. /* step over the edges */
  112. v = BM_edge_other_vert(e_next, v);
  113. e_prev = e_next;
  114. i++;
  115. } while ((v != f_verts[0]));
  116. if (!BM_face_exists(f_verts, i)) {
  117. BMFace *f;
  118. /* don't use calc_edges option because we already have the edges */
  119. f = BM_face_create_ngon_verts(bm, f_verts, i, NULL, BM_CREATE_NOP, true, false);
  120. BMO_face_flag_enable(bm, f, ELE_OUT);
  121. f->mat_nr = mat_nr;
  122. if (use_smooth) {
  123. BM_elem_flag_enable(f, BM_ELEM_SMOOTH);
  124. }
  125. }
  126. }
  127. MEM_freeN(f_verts);
  128. BMO_slot_buffer_from_enabled_flag(bm, op, op->slots_out, "faces.out", BM_FACE, ELE_OUT);
  129. }
  130. cleanup:
  131. MEM_freeN(verts);
  132. }