bmo_bisect_plane.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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):
  19. *
  20. * ***** END GPL LICENSE BLOCK *****
  21. */
  22. /** \file blender/bmesh/operators/bmo_bisect_plane.c
  23. * \ingroup bmesh
  24. *
  25. * Wrapper around #BM_mesh_bisect_plane
  26. */
  27. #include "MEM_guardedalloc.h"
  28. #include "BLI_utildefines.h"
  29. #include "BLI_stackdefines.h"
  30. #include "BLI_math.h"
  31. #include "bmesh.h"
  32. #include "bmesh_tools.h"
  33. #include "intern/bmesh_operators_private.h" /* own include */
  34. #define ELE_NEW 1
  35. #define ELE_CUT 2
  36. #define ELE_INPUT 4
  37. void bmo_bisect_plane_exec(BMesh *bm, BMOperator *op)
  38. {
  39. const float dist = BMO_slot_float_get(op->slots_in, "dist");
  40. const bool use_snap_center = BMO_slot_bool_get(op->slots_in, "use_snap_center");
  41. const bool clear_outer = BMO_slot_bool_get(op->slots_in, "clear_outer");
  42. const bool clear_inner = BMO_slot_bool_get(op->slots_in, "clear_inner");
  43. float plane_co[3];
  44. float plane_no[3];
  45. float plane[4];
  46. BMO_slot_vec_get(op->slots_in, "plane_co", plane_co);
  47. BMO_slot_vec_get(op->slots_in, "plane_no", plane_no);
  48. if (is_zero_v3(plane_no)) {
  49. BMO_error_raise(bm, op, BMERR_MESH_ERROR, "Zero normal given");
  50. return;
  51. }
  52. plane_from_point_normal_v3(plane, plane_co, plane_no);
  53. /* tag geometry to bisect */
  54. BM_mesh_elem_hflag_disable_all(bm, BM_EDGE | BM_FACE, BM_ELEM_TAG, false);
  55. BMO_slot_buffer_hflag_enable(bm, op->slots_in, "geom", BM_EDGE | BM_FACE, BM_ELEM_TAG, false);
  56. BMO_slot_buffer_flag_enable(bm, op->slots_in, "geom", BM_ALL_NOLOOP, ELE_INPUT);
  57. BM_mesh_bisect_plane(bm, plane, use_snap_center, true,
  58. ELE_CUT, ELE_NEW, dist);
  59. if (clear_outer || clear_inner) {
  60. /* Use an array of vertices because 'geom' contains both vers and edges that may use them.
  61. * Removing a vert may remove and edge which is later checked by BMO_ITER.
  62. * over-alloc the total possible vert count */
  63. const int vert_arr_max = min_ii(bm->totvert, BMO_slot_buffer_count(op->slots_in, "geom"));
  64. BMVert **vert_arr = MEM_mallocN(sizeof(*vert_arr) * (size_t)vert_arr_max, __func__);
  65. BMOIter siter;
  66. BMVert *v;
  67. float plane_inner[4];
  68. float plane_outer[4];
  69. STACK_DECLARE(vert_arr);
  70. copy_v3_v3(plane_outer, plane);
  71. copy_v3_v3(plane_inner, plane);
  72. plane_outer[3] = plane[3] - dist;
  73. plane_inner[3] = plane[3] + dist;
  74. STACK_INIT(vert_arr, vert_arr_max);
  75. BMO_ITER (v, &siter, op->slots_in, "geom", BM_VERT) {
  76. if ((clear_outer && plane_point_side_v3(plane_outer, v->co) > 0.0f) ||
  77. (clear_inner && plane_point_side_v3(plane_inner, v->co) < 0.0f))
  78. {
  79. STACK_PUSH(vert_arr, v);
  80. }
  81. }
  82. while ((v = STACK_POP(vert_arr))) {
  83. BM_vert_kill(bm, v);
  84. }
  85. MEM_freeN(vert_arr);
  86. }
  87. BMO_slot_buffer_from_enabled_flag(bm, op, op->slots_out, "geom.out", BM_ALL_NOLOOP, ELE_NEW | ELE_INPUT);
  88. BMO_slot_buffer_from_enabled_flag(bm, op, op->slots_out, "geom_cut.out", BM_VERT | BM_EDGE, ELE_CUT);
  89. }