Blender V5.3
bmo_bisect_plane.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
10
11#include "MEM_guardedalloc.h"
12
13#include "BLI_math_geom_c.hh"
14#include "BLI_math_vector_c.hh"
16
17#include "bmesh.hh"
18#include "bmesh_tools.hh"
19
20#include "intern/bmesh_operators_private.hh" /* own include */
21
22namespace blender {
23
24#define ELE_NEW 1
25#define ELE_CUT 2
26#define ELE_INPUT 4
27
29{
30 const float dist = BMO_slot_float_get(op->slots_in, "dist");
31 const bool use_snap_center = BMO_slot_bool_get(op->slots_in, "use_snap_center");
32 const bool clear_outer = BMO_slot_bool_get(op->slots_in, "clear_outer");
33 const bool clear_inner = BMO_slot_bool_get(op->slots_in, "clear_inner");
34
35 float plane_co[3];
36 float plane_no[3];
37 float plane[4];
38
39 BMO_slot_vec_get(op->slots_in, "plane_co", plane_co);
40 BMO_slot_vec_get(op->slots_in, "plane_no", plane_no);
41
42 if (is_zero_v3(plane_no)) {
43 BMO_error_raise(bm, op, BMO_ERROR_CANCEL, "Zero normal given");
44 return;
45 }
46
47 plane_from_point_normal_v3(plane, plane_co, plane_no);
48
49 /* Tag geometry to bisect, include vertices so loose vertices on the plane are handled. */
52 bm, op->slots_in, "geom", BM_VERT | BM_EDGE | BM_FACE, BM_ELEM_TAG, false);
53
55
56 BM_mesh_bisect_plane(bm, plane, use_snap_center, true, ELE_CUT, ELE_NEW, dist);
57
58 if (clear_outer || clear_inner) {
59 /* Use an array of vertices because 'geom' contains both verts and edges that may use them.
60 * Removing a vert may remove and edge which is later checked by #BMO_ITER.
61 * over-allocate the total possible vert count. */
62 const int vert_arr_max = min_ii(bm->totvert, BMO_slot_buffer_len(op->slots_in, "geom"));
63 BMVert **vert_arr = MEM_new_array_uninitialized<BMVert *>(vert_arr_max, __func__);
64 BMOIter siter;
65 BMVert *v;
66 float plane_inner[4];
67 float plane_outer[4];
68
69 STACK_DECLARE(vert_arr);
70
71 copy_v3_v3(plane_outer, plane);
72 copy_v3_v3(plane_inner, plane);
73 plane_outer[3] = plane[3] - dist;
74 plane_inner[3] = plane[3] + dist;
75
76 STACK_INIT(vert_arr, vert_arr_max);
77
78 BMO_ITER (v, &siter, op->slots_in, "geom", BM_VERT) {
79 if ((clear_outer && plane_point_side_v3(plane_outer, v->co) > 0.0f) ||
80 (clear_inner && plane_point_side_v3(plane_inner, v->co) < 0.0f))
81 {
82 STACK_PUSH(vert_arr, v);
83 }
84 }
85
86 while ((v = STACK_POP(vert_arr))) {
88 }
89
90 MEM_delete(vert_arr);
91 }
92
94 bm, op, op->slots_out, "geom.out", BM_ALL_NOLOOP, ELE_NEW | ELE_INPUT);
96 bm, op, op->slots_out, "geom_cut.out", BM_VERT | BM_EDGE, ELE_CUT);
97}
98
99} // namespace blender
#define STACK_POP(stack)
#define STACK_PUSH(stack, val)
#define STACK_DECLARE(stack)
#define STACK_INIT(stack, stack_num)
Read Blender memory allocation and freeing.
#define BM_ALL_NOLOOP
#define BMO_ITER(ele, iter, slot_args, slot_name, restrict_flag)
#define ELE_NEW
#define ELE_CUT
#define ELE_INPUT
BMesh Iterators.
int BMO_slot_buffer_len(BMOpSlot slot_args[BMO_OP_MAX_SLOTS], const char *slot_name)
MINLINE void copy_v3_v3(float r[3], const float a[3])
void * MEM_new_array_uninitialized(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
void BMO_slot_buffer_hflag_enable(BMesh *bm, BMOpSlot slot_args[BMO_OP_MAX_SLOTS], const char *slot_name, char htype, char hflag, bool do_flush)
BMO_FLAG_BUFFER.
void bmo_bisect_plane_exec(BMesh *bm, BMOperator *op)
void BM_vert_kill(BMesh *bm, BMVert *v)
float BMO_slot_float_get(BMOpSlot slot_args[BMO_OP_MAX_SLOTS], const char *slot_name)
void BMO_slot_buffer_flag_enable(BMesh *bm, BMOpSlot slot_args[BMO_OP_MAX_SLOTS], const char *slot_name, char htype, short oflag)
BMO_FLAG_BUFFER.
void BM_mesh_elem_hflag_disable_all(BMesh *bm, const char htype, const char hflag, const bool respecthide)
ATTR_WARN_UNUSED_RESULT const BMVert * v
void plane_from_point_normal_v3(float r_plane[4], const float plane_co[3], const float plane_no[3])
Definition math_geom.cc:225
void BM_mesh_bisect_plane(BMesh *bm, const float plane[4], const bool use_snap_center, const bool use_tag, const short oflag_center, const short oflag_new, const float eps)
void BMO_slot_vec_get(BMOpSlot slot_args[BMO_OP_MAX_SLOTS], const char *slot_name, float r_vec[3])
void BMO_slot_buffer_from_enabled_flag(BMesh *bm, BMOperator *op, BMOpSlot slot_args[BMO_OP_MAX_SLOTS], const char *slot_name, char htype, short oflag)
MINLINE float plane_point_side_v3(const float plane[4], const float co[3])
bool BMO_slot_bool_get(BMOpSlot slot_args[BMO_OP_MAX_SLOTS], const char *slot_name)
void BMO_error_raise(BMesh *bm, BMOperator *owner, eBMOpErrorLevel level, const char *msg) ATTR_NONNULL(1
@ BMO_ERROR_CANCEL
MINLINE int min_ii(int a, int b)
MINLINE bool is_zero_v3(const float v[3]) ATTR_WARN_UNUSED_RESULT
struct BMOpSlot slots_in[BMO_OP_MAX_SLOTS]
struct BMOpSlot slots_out[BMO_OP_MAX_SLOTS]