Blender V5.3
editmesh_rip_edge.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 "DNA_mesh_types.h"
12#include "DNA_object_types.h"
13
14#include "BKE_context.hh"
15#include "BKE_editmesh.hh"
16#include "BKE_layer.hh"
17
18#include "BLI_math_geom_c.hh"
19#include "BLI_math_matrix.hh"
20
21#include "WM_types.hh"
22
23#include "ED_mesh.hh"
24#include "ED_transform.hh"
25#include "ED_view3d.hh"
26#include "RNA_access.hh"
27#include "RNA_define.hh"
28
29#include "bmesh.hh"
30
31#include "mesh_intern.hh" /* own include */
32
33namespace blender {
34
35/* uses total number of selected edges around a vertex to choose how to extend */
36#define USE_TRICKY_EXTEND
37
39{
40 const Main *bmain = CTX_data_main(C);
41 const Scene *scene = CTX_data_scene(C);
42 ViewLayer *view_layer = CTX_data_view_layer(C);
44 *bmain, scene, view_layer, CTX_wm_view3d(C));
45
46 float3 ray_loc;
47 float3 ray_dir;
48 RNA_float_get_array(op->ptr, "location", ray_loc);
49 RNA_float_get_array(op->ptr, "direction", ray_dir);
50 normalize_v3(ray_dir);
51
52 for (Object *obedit : objects) {
54
55 BMIter viter;
56 BMVert *v;
57 bool changed = false;
58
59 if (bm->totvertsel == 0) {
60 continue;
61 }
62
63 /* clear tags. */
64 BM_ITER_MESH (v, &viter, bm, BM_VERTS_OF_MESH) {
66 }
67
68 /* operate on selected verts */
69 BM_ITER_MESH (v, &viter, bm, BM_VERTS_OF_MESH) {
70 BMIter eiter;
71 BMEdge *e;
72
74 float angle_best = FLT_MAX;
75 BMEdge *e_best = nullptr;
76
77#ifdef USE_TRICKY_EXTEND
78 /* first check if we can select the edge to split based on selection-only */
79 int tot_sel = 0;
80
81 BM_ITER_ELEM (e, &eiter, v, BM_EDGES_OF_VERT) {
84 e_best = e;
85 tot_sel += 1;
86 }
87 }
88 }
89 if (tot_sel != 1) {
90 e_best = nullptr;
91 }
92
93 /* only one edge selected, operate on that */
94 if (e_best) {
95 goto found_edge;
96 }
97 /* none selected, fall through and find one */
98 else if (tot_sel == 0) {
99 /* pass */
100 }
101 /* selection not 0 or 1, do nothing */
102 else {
103 goto found_edge;
104 }
105#endif
106
107 BM_ITER_ELEM (e, &eiter, v, BM_EDGES_OF_VERT) {
109 BMVert *v_other = BM_edge_other_vert(e, v);
110
111 const float4x4 &object_to_world = obedit->object_to_world();
112 const float3 v_world = math::transform_point(object_to_world, float3(v->co));
113 float3 projected_point;
114 closest_to_line_v3(projected_point, v_world, ray_loc, ray_loc + ray_dir);
115
116 float3 v_ray_dir = math::normalize(projected_point - v_world);
117 const float3 v_other_world = math::transform_point(object_to_world,
118 float3(v_other->co));
119 const float3 v_edge_dir = math::normalize(v_other_world - v_world);
120
121 float angle_test = angle_normalized_v3v3(v_ray_dir, v_edge_dir);
122 if (angle_test < angle_best) {
123 angle_best = angle_test;
124 e_best = e;
125 }
126 }
127 }
128
129#ifdef USE_TRICKY_EXTEND
130 found_edge:
131#endif
132 if (e_best) {
133 const bool e_select = BM_elem_flag_test_bool(e_best, BM_ELEM_SELECT);
134 BMVert *v_new;
135 BMEdge *e_new;
136
137 v_new = BM_edge_split(bm, e_best, v, &e_new, 0.0f);
138
139 BM_vert_select_set(bm, v, false);
140 BM_edge_select_set(bm, e_new, false);
141
142 BM_vert_select_set(bm, v_new, true);
143 if (e_select) {
144 BM_edge_select_set(bm, e_best, true);
145 }
146 BM_elem_flag_enable(v_new, BM_ELEM_TAG); /* prevent further splitting */
147
148 /* When UV sync select is enabled, the wrong UV's will be selected
149 * because the existing loops will have the selection and the new ones won't.
150 * transfer the selection state to the new loops. */
152 if (e_best->l) {
153 BMLoop *l_iter, *l_first;
154 l_iter = l_first = e_best->l;
155 do {
156 bool was_select = false;
157 if (l_iter->next->e == e_new) {
159 BM_loop_edge_uvselect_set(bm, l_iter->next, false);
160 was_select = true;
161 }
162 }
163 else {
164 BLI_assert(l_iter->prev->e == e_new);
166 BM_loop_edge_uvselect_set(bm, l_iter->prev, false);
167 was_select = true;
168 }
169 }
170 if (was_select) {
171 BM_loop_edge_uvselect_set(bm, l_iter, true);
172 }
173
174 } while ((l_iter = l_iter->radial_next) != l_first);
175 }
176 }
177
178 changed = true;
179 }
180 }
181 }
182
183 if (changed) {
185
187
188 EDBMUpdate_Params params{};
189 params.calc_looptris = true;
190 params.calc_normals = false;
191 params.is_destructive = true;
192 EDBM_update(id_cast<Mesh *>(obedit->data), &params);
193 }
194 }
195
196 return OPERATOR_FINISHED;
197}
198
200{
201 ARegion *region = CTX_wm_region(C);
202 const float2 mval_fl = {float(event->mval[0]), float(event->mval[1])};
203
204 float3 ray_start, ray_dir;
205 ED_view3d_win_to_ray(region, mval_fl, ray_start, ray_dir);
206 normalize_v3(ray_dir);
207
208 float3 ray_start_proj;
209 /* Project ray_start onto a plane defined by the ray direction to avoid precision
210 * issues especially for orthographic views where the far-clipping is used to calculate
211 * the ray_start. */
212 project_plane_normalized_v3_v3v3(ray_start_proj, ray_start, ray_dir);
213
214 RNA_float_set_array(op->ptr, "location", ray_start_proj);
215 RNA_float_set_array(op->ptr, "direction", ray_dir);
216
217 return edbm_rip_edge_exec(C, op);
218}
219
221{
222 /* identifiers */
223 ot->name = "Extend Vertices";
224 ot->idname = "MESH_OT_rip_edge";
225 ot->description = "Extend vertices along the edge closest to the cursor";
226
227 /* API callbacks. */
231
232 /* flags */
234
235 PropertyRNA *prop;
236
237 prop = RNA_def_float_vector(ot->srna,
238 "location",
239 3,
240 nullptr,
241 -FLT_MAX,
242 FLT_MAX,
243 "Location",
244 "World-space ray origin for extending vertices",
245 -1.0f,
246 1.0f);
248
249 prop = RNA_def_float_vector(ot->srna,
250 "direction",
251 3,
252 nullptr,
253 -FLT_MAX,
254 FLT_MAX,
255 "Direction",
256 "World-space direction vector for extending vertices",
257 -1.0f,
258 1.0f);
260}
261
262} // namespace blender
Object is a sort of wrapper for general info.
#define C
Definition RandGen.cpp:29
#define BM_elem_flag_disable(ele, hflag)
#define BM_elem_flag_test(ele, hflag)
#define BM_elem_flag_test_bool(ele, hflag)
#define BM_elem_flag_enable(ele, hflag)
#define BM_ITER_ELEM(ele, iter, data, itype)
#define BM_ITER_MESH(ele, iter, bm, itype)
#define FLT_MAX
MatBase< T, NumCol, NumRow > normalize(const MatBase< T, NumCol, NumRow > &a)
VecBase< T, 3 > transform_point(const CartesianBasis &basis, const VecBase< T, 3 > &v)
BMesh Iterators.
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
void RNA_float_set_array(PointerRNA *ptr, const char *name, const float *values)
float angle_normalized_v3v3(const float v1[3], const float v2[3]) ATTR_WARN_UNUSED_RESULT
void ED_view3d_win_to_ray(const ARegion *region, const float mval[2], float r_ray_start[3], float r_ray_normal[3])
BMesh * BKE_editmesh_bmesh_get_for_write(Mesh *mesh)
Definition editmesh.cc:79
bool EDBM_view3d_poll(bContext *C)
void EDBM_update(Mesh *mesh, const EDBMUpdate_Params *params)
void project_plane_normalized_v3_v3v3(float out[3], const float p[3], const float v_plane[3])
ATTR_WARN_UNUSED_RESULT const BMVert * v
MatBase< float, 4, 4 > float4x4
void BM_mesh_select_mode_flush(BMesh *bm)
BLI_INLINE BMVert * BM_edge_other_vert(BMEdge *e, const BMVert *v) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
ViewLayer * CTX_data_view_layer(const bContext *C)
PropertyRNA * RNA_def_float_vector(StructOrFunctionRNA *cont_, const char *identifier, const int len, const float *default_value, const float hardmin, const float hardmax, const char *ui_name, const char *ui_description, const float softmin, const float softmax)
void MESH_OT_rip_edge(wmOperatorType *ot)
VecBase< float, 2 > float2
static wmOperatorStatus edbm_rip_edge_invoke(bContext *C, wmOperator *op, const wmEvent *event)
void BM_vert_select_set(BMesh *bm, BMVert *v, const bool select)
Select Vert.
MINLINE float normalize_v3(float n[3])
void RNA_def_property_flag(PropertyRNA *prop, PropertyFlag flag)
BMVert * BM_edge_split(BMesh *bm, BMEdge *e, BMVert *v, BMEdge **r_e, float fac)
Edge Split.
Main * CTX_data_main(const bContext *C)
static wmOperatorStatus edbm_rip_edge_exec(bContext *C, wmOperator *op)
float closest_to_line_v3(float r_close[3], const float p[3], const float l1[3], const float l2[3])
@ PROP_SKIP_SAVE
Definition RNA_types.hh:414
@ PROP_HIDDEN
Definition RNA_types.hh:408
Vector< Object * > BKE_view_layer_array_from_objects_in_edit_mode_unique_data(const Main &bmain, const Scene *scene, ViewLayer *view_layer, const View3D *v3d)
void BM_select_history_clear(BMesh *bm)
Scene * CTX_data_scene(const bContext *C)
@ OPTYPE_REGISTER
Definition WM_types.hh:188
@ OPTYPE_UNDO
Definition WM_types.hh:190
@ OPTYPE_DEPENDS_ON_CURSOR
Definition WM_types.hh:228
VecBase< float, 3 > float3
@ BM_ELEM_SELECT_UV
ARegion * CTX_wm_region(const bContext *C)
void BM_loop_edge_uvselect_set(BMesh *bm, BMLoop *l, bool select)
View3D * CTX_wm_view3d(const bContext *C)
ATTR_WARN_UNUSED_RESULT const BMVert const BMEdge * e
void RNA_float_get_array(PointerRNA *ptr, const char *name, float *values)
void BM_edge_select_set(BMesh *bm, BMEdge *e, const bool select)
Select Edge.
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
struct BMLoop * l
struct BMLoop * next
struct BMLoop * prev
struct BMLoop * radial_next
wmOperatorStatus(*) exec(bContext *C, wmOperator *op) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1125
const char * description
Definition WM_types.hh:1115
bool(*) poll(bContext *C) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1165
wmOperatorStatus(*) invoke(bContext *C, wmOperator *op, const wmEvent *event) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1141