Blender V5.3
transform_mode_edge_slide.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2001-2002 NaN Holding BV. All rights reserved.
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <algorithm>
10
11#include "BLI_math_geom_c.hh"
12#include "BLI_math_matrix.hh"
13#include "BLI_math_matrix_c.hh"
14#include "BLI_string_utf8.hh"
15
16#include "BKE_editmesh.hh"
17#include "BKE_editmesh_bvh.hh"
18#include "BKE_report.hh"
19#include "BKE_unit.hh"
20
21#include "GPU_immediate.hh"
22#include "GPU_matrix.hh"
23#include "GPU_state.hh"
24
26
27#include "ED_mesh.hh"
28#include "ED_screen.hh"
29
30#include "RNA_access.hh"
31
32#include "UI_interface.hh"
33#include "UI_view2d.hh"
34
35#include "BLT_translation.hh"
36
37#include "transform.hh"
39#include "transform_convert.hh"
40#include "transform_mode.hh"
41#include "transform_snap.hh"
42
43namespace blender::ed::transform {
44
45/* -------------------------------------------------------------------- */
48
51
52 int mval_start[2], mval_end[2];
54
55 private:
56 float4x4 proj_mat;
57 float2 win_half;
58
59 public:
61 {
62 ARegion *region = t->region;
63 if (region == nullptr) [[unlikely]] {
64 this->win_half = {1.0f, 1.0f};
65 this->proj_mat = float4x4::identity();
66 return;
67 }
68
69 this->win_half = {region->winx / 2.0f, region->winy / 2.0f};
70
71 if (t->spacetype == SPACE_VIEW3D) {
72 RegionView3D *rv3d = static_cast<RegionView3D *>(region->regiondata);
73 this->proj_mat = ED_view3d_ob_project_mat_get(rv3d, tc->obedit);
74
75 for (int i = 0; i < 4; i++) {
76 this->proj_mat[i][0] *= this->win_half[0];
77 this->proj_mat[i][1] *= this->win_half[1];
78 }
79 }
80 else {
81 const View2D *v2d = static_cast<View2D *>(t->view);
82 ui::view2d_view_to_region_m4(v2d, this->proj_mat.ptr());
83 this->proj_mat.location()[0] -= this->win_half[0];
84 this->proj_mat.location()[1] -= this->win_half[1];
85 }
86 }
87
88 void project(const TransDataEdgeSlideVert *svert, float2 &r_sco_a, float2 &r_sco_b) const
89 {
90 float3 iloc = svert->v_co_orig();
91 r_sco_a = math::project_point(this->proj_mat, iloc + svert->dir_side[0]).xy() + this->win_half;
92 r_sco_b = math::project_point(this->proj_mat, iloc + svert->dir_side[1]).xy() + this->win_half;
93 }
94};
95
106
108{
110 return static_cast<EdgeSlideData *>(tc->custom.mode.data);
111}
112
114{
116
117 setCustomPoints(t, &t->mouse, sld->mval_end, sld->mval_start);
118
119 /* #setCustomPoints isn't normally changing as the mouse moves,
120 * in this case apply mouse input immediately so we don't refresh
121 * with the value from the previous points. */
122 applyMouseInput(t, &t->mouse, t->mval, t->values);
123}
124
125/* Interpolates along a line made up of 2 segments (used for edge slide). */
127 float p[3], const float v1[3], const float v2[3], const float v3[3], float t)
128{
129 float t_mid, t_delta;
130
131 /* Could be pre-calculated. */
132 t_mid = line_point_factor_v3(v2, v1, v3);
133
134 t_delta = t - t_mid;
135 if (t_delta < 0.0f) {
136 if (fabsf(t_mid) < FLT_EPSILON) [[unlikely]] {
137 copy_v3_v3(p, v2);
138 }
139 else {
140 interp_v3_v3v3(p, v1, v2, t / t_mid);
141 }
142 }
143 else {
144 t = t - t_mid;
145 t_mid = 1.0f - t_mid;
146
147 if (fabsf(t_mid) < FLT_EPSILON) [[unlikely]] {
148 copy_v3_v3(p, v3);
149 }
150 else {
151 interp_v3_v3v3(p, v2, v3, t / t_mid);
152 }
153 }
154}
155
156static void edge_slide_data_init_mval(MouseInput *mi, EdgeSlideData *sld, float *mval_dir)
157{
158 /* Possible all of the edge loops are pointing directly at the view. */
159 if (len_squared_v2(mval_dir) < 0.1f) [[unlikely]] {
160 mval_dir[0] = 0.0f;
161 mval_dir[1] = 100.0f;
162 }
163
164 float mval_start[2], mval_end[2];
165
166 /* Zero out Start. */
167 zero_v2(mval_start);
168
169 /* `mval_dir` holds a vector along edge loop. */
170 copy_v2_v2(mval_end, mval_dir);
171 mul_v2_fl(mval_end, 0.5f);
172
173 sld->mval_start[0] = mi->imval[0] + mval_start[0];
174 sld->mval_start[1] = mi->imval[1] + mval_start[1];
175
176 sld->mval_end[0] = mi->imval[0] + mval_end[0];
177 sld->mval_end[1] = mi->imval[1] + mval_end[1];
178}
179
182 const View3D *v3d,
183 const BMBVHTree *bmbvh,
185{
186 /* NOTE: */
187 BMIter iter_other;
188 BMEdge *e;
189
190 BMVert *v = static_cast<BMVert *>(sv->td->extra);
191 BM_ITER_ELEM (e, &iter_other, v, BM_EDGES_OF_VERT) {
193 continue;
194 }
195
196 if (BMBVH_EdgeVisible(bmbvh, e, t->depsgraph, t->region, v3d, tc->obedit)) {
197 return true;
198 }
199 }
200 return false;
201}
202
208 EdgeSlideData *sld,
209 const int loop_nr,
210 const float2 &mval,
211 const bool use_calc_direction)
212{
213 View3D *v3d = nullptr;
214
215 /* Use for visibility checks. */
216 bool use_occlude_geometry = false;
217 if (t->spacetype == SPACE_VIEW3D) {
218 v3d = static_cast<View3D *>(t->area ? t->area->spacedata.first_ : nullptr);
219 if (v3d) {
220 if (tc->obedit->type == OB_MESH) {
221 use_occlude_geometry = (tc->obedit->dt > OB_WIRE && !XRAY_ENABLED(v3d));
222 }
223 }
224 }
225
226 /* NOTE(@ideasman42): At the moment this is only needed for meshes.
227 * In principle we could use a generic ray-cast test.
228 *
229 * Prefer #BMBVHTree over generic snap: #SnapObjectContext
230 * or any method that considers all other objects in the scene.
231 *
232 * While generic snapping is technically "correct" there are multiple reasons not to use this.
233 *
234 * - Performance, where generic snapping would consider all other objects for every-vertex.
235 * This can cause lockups when #DupliObject have to be created multiple times for each vertex.
236 * - In practice it's acceptable (even preferable) to skip back-facing vertices
237 * based on each meshes own faces that doesn't take other scene objects into account,
238 * especially since this includes instances objects from particles or nodes.
239 * - The #BMBVH_EdgeVisible check skips faces that the edge is connected to,
240 * unlike generic ray-casts where an edge can (under some conditions) overlap it self.
241 *
242 * See: #125646 for details.
243 */
244 BMBVHTree *bmbvh = nullptr;
245 Array<float3> bmbvh_coord_storage;
246 if (use_occlude_geometry) {
247 Scene *scene_eval = DEG_get_evaluated(t->depsgraph, t->scene);
248 Object *obedit_eval = DEG_get_evaluated(t->depsgraph, tc->obedit);
250
252 t->depsgraph, scene_eval, obedit_eval, bmbvh_coord_storage);
253
257 vert_positions.is_empty() ? nullptr :
258 vert_positions.data(),
259 false);
260 }
261
262 /* Find mouse vectors, the global one, and one per loop in case we have
263 * multiple loops selected, in case they are oriented different. */
264 float2 mval_dir = float2(0);
265 float dist_best_sq = FLT_MAX;
266
267 /* Only for use_calc_direction. */
268 float2 *loop_dir = nullptr;
269 float *loop_maxdist = nullptr;
270
271 if (use_calc_direction) {
272 loop_dir = MEM_new_array_zeroed<float2>(loop_nr, "sv loop_dir");
273 loop_maxdist = MEM_new_array_uninitialized<float>(loop_nr, "sv loop_maxdist");
274 std::fill_n(loop_maxdist, loop_nr, FLT_MAX);
275 }
276
277 for (int i : sld->sv.index_range()) {
278 TransDataEdgeSlideVert *sv = &sld->sv[i];
279 bool is_visible = !use_occlude_geometry || is_vert_slide_visible_bmesh(t, tc, v3d, bmbvh, sv);
280
281 /* This test is only relevant if object is not wire-drawn! See #32068. */
282 if (!is_visible && !use_calc_direction) {
283 continue;
284 }
285
286 /* Search cross edges for visible edge to the mouse cursor,
287 * then use the shared vertex to calculate screen vector. */
288 /* Screen-space coords. */
289 float2 sco_a, sco_b;
290 sld->project(sv, sco_a, sco_b);
291
292 /* Global direction. */
293 float dist_sq = dist_squared_to_line_segment_v2(mval, sco_b, sco_a);
294 if (is_visible) {
295 if (dist_sq < dist_best_sq && (len_squared_v2v2(sco_b, sco_a) > 0.1f)) {
296 dist_best_sq = dist_sq;
297 mval_dir = sco_b - sco_a;
298 sld->curr_sv_index = i;
299 }
300 }
301
302 if (use_calc_direction) {
303 /* Per loop direction. */
304 int l_nr = sv->loop_nr;
305 if (dist_sq < loop_maxdist[l_nr]) {
306 loop_maxdist[l_nr] = dist_sq;
307 loop_dir[l_nr] = sco_b - sco_a;
308 }
309 }
310 }
311
312 if (use_calc_direction) {
313 for (TransDataEdgeSlideVert &sv : sld->sv) {
314 /* Switch a/b if loop direction is different from global direction. */
315 int l_nr = sv.loop_nr;
316 if (math::dot(loop_dir[l_nr], mval_dir) < 0.0f) {
317 swap_v3_v3(sv.dir_side[0], sv.dir_side[1]);
318 }
319 }
320
321 MEM_delete(loop_dir);
322 MEM_delete(loop_maxdist);
323 }
324
325 edge_slide_data_init_mval(&t->mouse, sld, mval_dir);
326
327 if (bmbvh) {
328 BKE_bmbvh_free(bmbvh);
329 }
330}
331
334 const bool use_double_side)
335{
336 int group_len;
337 EdgeSlideData *sld = MEM_new<EdgeSlideData>("sld");
339 sld->sv = transform_mesh_uv_edge_slide_data_create(t, tc, &group_len);
340 }
341 else {
342 sld->sv = transform_mesh_edge_slide_data_create(tc, &group_len);
343 }
344
345 if (sld->sv.is_empty()) {
346 MEM_delete(sld);
347 return nullptr;
348 }
349
350 if (!use_double_side) {
351 /* Single Side Case.
352 * Used by #MESH_OT_offset_edge_loops_slide.
353 * It only slides to the side with the longest length. */
354 struct TMP {
355 float2 accum;
356 int count;
357 } zero{};
358
359 Array<TMP> array_len(group_len, zero);
360 for (TransDataEdgeSlideVert &sv : sld->sv) {
361 array_len[sv.loop_nr].accum += float2(math::length(sv.dir_side[0]),
362 math::length(sv.dir_side[1]));
363 array_len[sv.loop_nr].count++;
364 }
365
366 for (TMP &accum : array_len) {
367 accum.accum /= accum.count;
368 }
369
370 for (TransDataEdgeSlideVert &sv : sld->sv) {
371 if (array_len[sv.loop_nr].accum[1] > array_len[sv.loop_nr].accum[0]) {
372 sv.dir_side[0] = sv.dir_side[1];
373 }
374 sv.dir_side[1] = float3(0);
375 sv.edge_len = math::length(sv.dir_side[0]);
376 }
377 }
378
379 sld->curr_sv_index = 0;
380 sld->update_proj_mat(t, tc);
381
382 calcEdgeSlide_mval_range(t, tc, sld, group_len, t->mval, use_double_side);
383
384 return sld;
385}
386
387static void freeEdgeSlideVerts(TransInfo * /*t*/,
388 TransDataContainer * /*tc*/,
389 TransCustomData *custom_data)
390{
391 EdgeSlideData *sld = static_cast<EdgeSlideData *>(custom_data->data);
392
393 if (sld == nullptr) {
394 return;
395 }
396
397 MEM_delete(sld);
398
399 custom_data->data = nullptr;
400}
401
403{
404 EdgeSlideParams *slp = static_cast<EdgeSlideParams *>(t->custom.mode.data);
405
406 if (slp) {
407 bool is_event_handled = t->redraw && (event->type != MOUSEMOVE);
408 slp->update_status_bar |= is_event_handled;
409 switch (event->type) {
410 case EVT_EKEY:
411 if (event->val == KM_PRESS) {
412 slp->use_even = !slp->use_even;
414 slp->update_status_bar = true;
415 return TREDRAW_HARD;
416 }
417 break;
418 case EVT_FKEY:
419 if (event->val == KM_PRESS) {
420 slp->flipped = !slp->flipped;
422 slp->update_status_bar = true;
423 return TREDRAW_HARD;
424 }
425 break;
426 case EVT_CKEY:
427 /* Use like a modifier key. */
428 if (event->val == KM_PRESS) {
429 t->flag ^= T_ALT_TRANSFORM;
431 slp->update_status_bar = true;
432 return TREDRAW_HARD;
433 }
434 break;
435 case MOUSEMOVE:
437 break;
438 default:
439 break;
440 }
441 }
442 return TREDRAW_NOTHING;
443}
444
446{
448 if (sld == nullptr) {
449 return;
450 }
451
452 const EdgeSlideParams *slp = static_cast<const EdgeSlideParams *>(t->custom.mode.data);
453 const bool is_clamp = !(t->flag & T_ALT_TRANSFORM);
454
455 const float line_size = ui::theme::get_value_f(TH_OUTLINE_WIDTH) + 0.5f;
456
458
460
461 if (t->spacetype == SPACE_VIEW3D) {
463 GPU_matrix_mul(TRANS_DATA_CONTAINER_FIRST_OK(t)->obedit->object_to_world().ptr());
464 }
465
466 uint pos = GPU_vertformat_attr_add(immVertexFormat(), "pos", gpu::VertAttrType::SFLOAT_32_32_32);
467
469
470 TransDataEdgeSlideVert *curr_sv = &sld->sv[sld->curr_sv_index];
471 const float3 curr_sv_co_orig = curr_sv->v_co_orig();
472
473 if (slp->use_even == true) {
474 /* Even mode. */
475 float co_a[3], co_b[3], co_mark[3];
476 const float fac = (slp->perc + 1.0f) / 2.0f;
477 const float ctrl_size = ui::theme::get_value_f(TH_FACEDOT_SIZE) + 1.5f;
478 const float guide_size = ctrl_size - 0.5f;
479 const int alpha_shade = -30;
480
481 add_v3_v3v3(co_a, curr_sv_co_orig, curr_sv->dir_side[0]);
482 add_v3_v3v3(co_b, curr_sv_co_orig, curr_sv->dir_side[1]);
483
484 GPU_line_width(line_size);
487 if (!math::is_zero(curr_sv->dir_side[0])) {
488 immVertex3fv(pos, co_a);
489 immVertex3fv(pos, curr_sv_co_orig);
490 }
491 if (!math::is_zero(curr_sv->dir_side[1])) {
492 immVertex3fv(pos, co_b);
493 immVertex3fv(pos, curr_sv_co_orig);
494 }
495 immEnd();
497
499 {
500 float *co_test = nullptr;
501 if (slp->flipped) {
502 if (!math::is_zero(curr_sv->dir_side[1])) {
503 co_test = co_b;
504 }
505 }
506 else {
507 if (!math::is_zero(curr_sv->dir_side[0])) {
508 co_test = co_a;
509 }
510 }
511
512 if (co_test != nullptr) {
513 immUniformThemeColorShadeAlpha(TH_SELECT, -30, alpha_shade);
514 GPU_point_size(ctrl_size);
516 immVertex3fv(pos, co_test);
517 immEnd();
518 }
519 }
520
521 immUniformThemeColorShadeAlpha(TH_SELECT, 255, alpha_shade);
522 GPU_point_size(guide_size);
524 interp_line_v3_v3v3v3(co_mark, co_b, curr_sv_co_orig, co_a, fac);
525 immVertex3fv(pos, co_mark);
526 immEnd();
527 }
528 else if (is_clamp == false) {
529 const int side_index = slp->curr_side_unclamp;
530 const int alpha_shade = -160;
531
532 GPU_line_width(line_size);
534 immBegin(GPU_PRIM_LINES, sld->sv.size() * 2);
535
536 /* TODO(@ideasman42): Loop over all verts. */
537 for (TransDataEdgeSlideVert &sv : sld->sv) {
538 float a[3], b[3];
539
540 if (!is_zero_v3(sv.dir_side[side_index])) {
541 copy_v3_v3(a, sv.dir_side[side_index]);
542 }
543 else {
544 copy_v3_v3(a, sv.dir_side[!side_index]);
545 }
546
547 mul_v3_fl(a, 100.0f);
548 negate_v3_v3(b, a);
549
550 const float3 sv_co_orig = sv.v_co_orig();
551 add_v3_v3(a, sv_co_orig);
552 add_v3_v3(b, sv_co_orig);
553
554 immVertex3fv(pos, a);
555 immVertex3fv(pos, b);
556 }
557 immEnd();
558 }
559 else {
560 /* Common case. */
561 const int alpha_shade = -160;
562
563 float co_dir[3];
564 add_v3_v3v3(co_dir, curr_sv_co_orig, curr_sv->dir_side[slp->curr_side_unclamp]);
565
566 GPU_line_width(line_size);
569 immVertex3fv(pos, curr_sv_co_orig);
570 immVertex3fv(pos, co_dir);
571 immEnd();
572 }
573
575
576 if (t->spacetype == SPACE_VIEW3D) {
579 }
580
582}
583
584static void edge_slide_snap_apply(TransInfo *t, float *value)
585{
587 EdgeSlideParams *slp = static_cast<EdgeSlideParams *>(t->custom.mode.data);
588 EdgeSlideData *sld_active = static_cast<EdgeSlideData *>(tc->custom.mode.data);
589 TransDataEdgeSlideVert *sv = &sld_active->sv[sld_active->curr_sv_index];
590 float3 co_orig, co_dest[2], dvec, snap_point;
591 co_orig = sv->v_co_orig();
592 co_dest[0] = co_orig + sv->dir_side[0];
593 co_dest[1] = co_orig + sv->dir_side[1];
594
595 if (tc->use_local_mat) {
596 mul_m4_v3(tc->mat, co_orig);
597 mul_m4_v3(tc->mat, co_dest[0]);
598 mul_m4_v3(tc->mat, co_dest[1]);
599 }
600
601 getSnapPoint(t, dvec);
602 sub_v3_v3(dvec, t->tsnap.snap_source);
603 add_v3_v3v3(snap_point, co_orig, dvec);
604
605 float perc = *value;
606 int side_index;
607 float t_mid;
608 if (slp->use_even == false) {
609 const bool is_clamp = !(t->flag & T_ALT_TRANSFORM);
610 if (is_clamp) {
611 side_index = perc < 0.0f;
612 }
613 else {
614 /* Use the side indicated in `EdgeSlideParams::curr_side_unclamp` as long as that side is not
615 * zero length. */
616 side_index = int(slp->curr_side_unclamp ==
618 }
619 }
620 else {
621 /* Could be pre-calculated. */
622 t_mid = line_point_factor_v3(float3{0.0f, 0.0f, 0.0f}, sv->dir_side[0], sv->dir_side[1]);
623
624 float t_snap = line_point_factor_v3(snap_point, co_dest[0], co_dest[1]);
625 side_index = t_snap >= t_mid;
626 }
627
629 float co_dir[3];
630 sub_v3_v3v3(co_dir, co_dest[side_index], co_orig);
631 normalize_v3(co_dir);
634 }
635 else {
637 }
638 add_v3_v3v3(snap_point, co_orig, dvec);
639 }
640
641 perc = line_point_factor_v3(snap_point, co_orig, co_dest[side_index]);
642 if (slp->use_even == false) {
643 if (side_index) {
644 perc *= -1;
645 }
646 }
647 else {
648 if (!side_index) {
649 perc = (1.0f - perc) * t_mid;
650 }
651 else {
652 perc = perc * (1.0f - t_mid) + t_mid;
653 }
654
655 if (slp->flipped) {
656 perc = 1.0f - perc;
657 }
658
659 perc = (2 * perc) - 1.0f;
660
661 if (!slp->flipped) {
662 perc *= -1;
663 }
664 }
665
666 *value = perc;
667}
668
670 const float fac,
671 const float curr_length_fac,
672 const int curr_side_unclamp,
673 const bool use_clamp,
674 const bool use_even,
675 const bool use_flip,
676 float r_co[3])
677{
678 copy_v3_v3(r_co, sv.v_co_orig());
679
680 if (use_even == false) {
681 if (use_clamp) {
682 const int side_index = (fac < 0.0f);
683 const float fac_final = fabsf(fac);
684 madd_v3_v3fl(r_co, sv.dir_side[side_index], fac_final);
685 }
686 else {
687 int side_index = curr_side_unclamp;
688 if (is_zero_v3(sv.dir_side[side_index])) {
689 side_index = int(!side_index);
690 }
691 const float fac_final = (side_index == (fac < 0.0f) ? fabsf(fac) : -fabsf(fac));
692 madd_v3_v3fl(r_co, sv.dir_side[side_index], fac_final);
693 }
694 }
695 else {
706 if (sv.edge_len > FLT_EPSILON) {
707 float co_a[3], co_b[3];
708 const float fac_final = min_ff(sv.edge_len, curr_length_fac) / sv.edge_len;
709
710 add_v3_v3v3(co_a, r_co, sv.dir_side[0]);
711 add_v3_v3v3(co_b, r_co, sv.dir_side[1]);
712
713 if (use_flip) {
714 interp_line_v3_v3v3v3(r_co, co_b, r_co, co_a, fac_final);
715 }
716 else {
717 interp_line_v3_v3v3v3(r_co, co_a, r_co, co_b, fac_final);
718 }
719 }
720 }
721}
722
723static void doEdgeSlide(TransInfo *t, float perc)
724{
725 EdgeSlideParams *slp = static_cast<EdgeSlideParams *>(t->custom.mode.data);
726 EdgeSlideData *sld_active = edgeSlideFirstGet(t);
727
728 slp->perc = perc;
729
730 const bool use_clamp = !(t->flag & T_ALT_TRANSFORM);
731 const bool use_even = slp->use_even;
732 const bool use_flip = slp->flipped;
733
734 const int curr_side_unclamp = slp->curr_side_unclamp;
735 float curr_length_fac = 0.0f;
736 if (use_even) {
737 TransDataEdgeSlideVert *sv_active = &sld_active->sv[sld_active->curr_sv_index];
738 curr_length_fac = sv_active->edge_len * (((use_flip ? perc : -perc) + 1.0f) / 2.0f);
739 }
740 else if (use_clamp) {
741 slp->curr_side_unclamp = (perc < 0.0f);
742 }
743
745 EdgeSlideData *sld = static_cast<EdgeSlideData *>(tc->custom.mode.data);
746
747 if (sld == nullptr) {
748 continue;
749 }
750
751 for (TransDataEdgeSlideVert &sv : sld->sv) {
753 sv, perc, curr_length_fac, curr_side_unclamp, use_clamp, use_even, use_flip, sv.td->loc);
754 }
755 }
756}
757
759{
760 char str[UI_MAX_DRAW_STR];
761 size_t ofs = 0;
762 float final;
763 const bool is_clamp = !(t->flag & T_ALT_TRANSFORM);
764 const bool is_constrained = !(is_clamp == false || hasNumInput(&t->num));
765
766 final = t->values[0] + t->values_modal_offset[0];
767
769 if (!validSnap(t)) {
770 transform_snap_increment(t, &final);
771 }
772
773 /* Only do this so out of range values are not displayed. */
774 if (is_constrained) {
775 CLAMP(final, -1.0f, 1.0f);
776 }
777
778 applyNumInput(&t->num, &final);
779
780 t->values_final[0] = final;
781
782 /* Header string. */
783 ofs += BLI_strncpy_utf8_rlen(str + ofs, RPT_("Edge Slide: "), sizeof(str) - ofs);
784 if (hasNumInput(&t->num)) {
785 char c[NUM_STR_REP_LEN];
786 outputNumInput(&(t->num), c, t->scene->unit);
787 ofs += BLI_strncpy_utf8_rlen(str + ofs, &c[0], sizeof(str) - ofs);
788 }
789 else {
790 ofs += BLI_snprintf_utf8_rlen(str + ofs, sizeof(str) - ofs, "%.4f ", final);
791 }
792 /* Done with header string. */
793
794 /* Do stuff here. */
795 doEdgeSlide(t, final);
796
797 recalc_data(t);
798
800}
801
803{
804 if (t->keymap == nullptr) {
805 return;
806 }
807 const wmKeyMap &keymap = *t->keymap;
808
809 EdgeSlideParams *slp = static_cast<EdgeSlideParams *>(t->custom.mode.data);
810 if (!slp->update_status_bar) {
811 return;
812 }
813 slp->update_status_bar = false;
814
815 const bool flipped = slp->flipped;
816 const bool use_even = slp->use_even;
817 const bool is_clamp = !(t->flag & T_ALT_TRANSFORM);
818 const bool is_precision = t->modifiers & MOD_PRECISION;
819 const bool is_snap = t->modifiers & MOD_SNAP;
820 const bool is_snap_invert = t->modifiers & MOD_SNAP_INVERT;
821
822 WorkspaceStatus status(t->context);
823 status.modal_keymap(IFACE_("Confirm"), keymap, TFM_MODAL_CONFIRM);
824 status.modal_keymap(IFACE_("Cancel"), keymap, TFM_MODAL_CANCEL);
825 status.modal_keymap(IFACE_("Snap"), keymap, TFM_MODAL_SNAP_TOGGLE, is_snap);
826 status.modal_keymap(IFACE_("Snap Invert"), keymap, TFM_MODAL_SNAP_INV_ON, is_snap_invert);
827 status.modal_keymap(IFACE_("Set Snap Base"), keymap, TFM_MODAL_EDIT_SNAP_SOURCE_ON);
828 status.modal_keymap(IFACE_("Move"), keymap, TFM_MODAL_TRANSLATE);
829 status.modal_keymap(IFACE_("Rotate"), keymap, TFM_MODAL_ROTATE);
830 status.modal_keymap(IFACE_("Resize"), keymap, TFM_MODAL_RESIZE);
831 status.modal_keymap(IFACE_("Precision Mode"), keymap, TFM_MODAL_PRECISION, is_precision);
832 status.item_bool(IFACE_("Clamp"), is_clamp, ICON_EVENT_C, ICON_EVENT_ALT);
833 status.item_bool(IFACE_("Even"), use_even, ICON_EVENT_E);
834 if (use_even) {
835 status.item_bool(IFACE_("Flipped"), flipped, ICON_EVENT_F);
836 }
837}
838
839static void edge_slide_transform_matrix_fn(TransInfo *t, float mat_xform[4][4])
840{
841 float delta[3], orig_co[3], final_co[3];
842
843 EdgeSlideParams *slp = static_cast<EdgeSlideParams *>(t->custom.mode.data);
845 EdgeSlideData *sld_active = static_cast<EdgeSlideData *>(tc->custom.mode.data);
846 TransDataEdgeSlideVert &sv_active = sld_active->sv[sld_active->curr_sv_index];
847
848 copy_v3_v3(orig_co, sv_active.v_co_orig());
849
850 const float fac = t->values_final[0];
851 float curr_length_fac = 0.0f;
852 if (slp->use_even) {
853 curr_length_fac = sv_active.edge_len * (((slp->flipped ? fac : -fac) + 1.0f) / 2.0f);
854 }
855
856 edge_slide_apply_elem(sv_active,
857 fac,
858 curr_length_fac,
860 !(t->flag & T_ALT_TRANSFORM),
861 slp->use_even,
862 slp->flipped,
863 final_co);
864
865 if (tc->use_local_mat) {
866 mul_m4_v3(tc->mat, orig_co);
867 mul_m4_v3(tc->mat, final_co);
868 }
869
870 sub_v3_v3v3(delta, final_co, orig_co);
871 add_v3_v3(mat_xform[3], delta);
872}
873
875 TransInfo *t, bool use_double_side, bool use_even, bool flipped, bool use_clamp)
876{
877 EdgeSlideData *sld;
878 bool ok = false;
879
880 if ((t->flag & T_EDIT) == 0 || (t->obedit_type != OB_MESH)) {
881 BKE_report(t->reports, RPT_ERROR, "'Edge Slide' is only supported in mesh edit mode");
882 t->state = TRANS_CANCEL;
883 return;
884 }
885
886 t->mode = TFM_EDGE_SLIDE;
887
888 {
890 slp->use_even = use_even;
891 slp->flipped = flipped;
892 /* Happens to be best for single-sided. */
893 if (use_double_side == false) {
894 slp->flipped = !flipped;
895 }
896 slp->perc = 0.0f;
897 slp->update_status_bar = true;
898
899 if (!use_clamp) {
900 t->flag |= T_ALT_TRANSFORM;
901 }
902
903 t->custom.mode.data = slp;
904 t->custom.mode.use_free = true;
905 }
906
908 sld = createEdgeSlideVerts(t, tc, use_double_side);
909 if (sld) {
910 tc->custom.mode.data = sld;
911 tc->custom.mode.free_cb = freeEdgeSlideVerts;
912 ok = true;
913 }
914 }
915
916 if (!ok) {
917 t->state = TRANS_CANCEL;
918 return;
919 }
920
921 /* Set custom point first if you want value to be initialized by init. */
924
925 t->idx_max = 0;
926 t->num.idx_max = 0;
927 t->increment[0] = 0.1f;
928 t->increment_precision = 0.1f;
929
930 copy_v3_fl(t->num.val_inc, t->increment[0]);
931 t->num.unit_sys = t->scene->unit.system;
932 t->num.unit_type[0] = B_UNIT_NONE;
933}
934
936{
937 bool use_double_side = true;
938 bool use_even = false;
939 bool flipped = false;
940 bool use_clamp = true;
941 if (op) {
942 PropertyRNA *prop;
943 /* The following properties could be unset when transitioning from this
944 * operator to another and back. For example pressing "G" to move, and
945 * then "G" again to go back to edge slide. */
946 prop = RNA_struct_find_property(op->ptr, "single_side");
947 use_double_side = (prop) ? !RNA_property_boolean_get(op->ptr, prop) : true;
948 prop = RNA_struct_find_property(op->ptr, "use_even");
949 use_even = (prop) ? RNA_property_boolean_get(op->ptr, prop) : false;
950 prop = RNA_struct_find_property(op->ptr, "flipped");
951 flipped = (prop) ? RNA_property_boolean_get(op->ptr, prop) : false;
952 prop = RNA_struct_find_property(op->ptr, "use_clamp");
953 use_clamp = (prop) ? RNA_property_boolean_get(op->ptr, prop) : true;
954 }
955 initEdgeSlide_ex(t, use_double_side, use_even, flipped, use_clamp);
956}
957
959
960/* -------------------------------------------------------------------- */
963
965{
967 EdgeSlideData *sld = static_cast<EdgeSlideData *>(tc->custom.mode.data);
968 if (sld) {
969 sld->update_proj_mat(t, tc);
970 TransDataEdgeSlideVert *curr_sv = &sld->sv[sld->curr_sv_index];
971
972 float2 sco_a, sco_b;
973 sld->project(curr_sv, sco_a, sco_b);
974 float2 mval_dir = sco_b - sco_a;
975 edge_slide_data_init_mval(&t->mouse, sld, mval_dir);
976 }
977 }
978
980 setCustomPoints(t, &t->mouse, sld->mval_end, sld->mval_start);
981}
982
984
986 /*flags*/ T_NO_CONSTRAINT,
987 /*init_fn*/ initEdgeSlide,
988 /*transform_fn*/ applyEdgeSlide,
989 /*transform_matrix_fn*/ edge_slide_transform_matrix_fn,
990 /*handle_event_fn*/ handleEventEdgeSlide,
991 /*snap_distance_fn*/ transform_snap_distance_len_squared_fn,
992 /*snap_apply_fn*/ edge_slide_snap_apply,
993 /*draw_fn*/ drawEdgeSlide,
994 /*status_fn*/ edge_slide_status,
995};
996
997} // namespace blender::ed::transform
#define RPT_(msgid)
#define IFACE_(msgid)
#define NUM_STR_REP_LEN
#define XRAY_ENABLED(v3d)
#define UI_MAX_DRAW_STR
#define CLAMP(a, b, c)
#define BM_elem_flag_test(ele, hflag)
#define BM_ITER_ELEM(ele, iter, data, itype)
constexpr const T * data() const
Definition BLI_span.hh:219
constexpr bool is_empty() const
Definition BLI_span.hh:264
void modal_keymap(std::string text, const wmKeyMap &keymap, int propvalue, bool inverted=false)
Definition area.cc:1119
void item_bool(std::string text, bool inverted, int icon1, int icon2=0)
Definition area.cc:1097
#define str(s)
#define FLT_MAX
#define FLT_EPSILON
TransDataContainer * transform_mode_container_first_ok(TransInfo *t)
void initMouseInputMode(TransInfo *t, MouseInput *mi, MouseInputMode mode)
void recalc_data(TransInfo *t)
static void edge_slide_snap_apply(TransInfo *t, float *value)
static EdgeSlideData * edgeSlideFirstGet(TransInfo *t)
void getSnapPoint(const TransInfo *t, float vec[3])
Array< TransDataEdgeSlideVert > transform_mesh_edge_slide_data_create(const TransDataContainer *tc, int *r_group_len)
static void interp_line_v3_v3v3v3(float p[3], const float v1[3], const float v2[3], const float v3[3], float t)
bool transform_snap_increment(const TransInfo *t, float *r_val)
void transform_snap_mixed_apply(TransInfo *t, float *vec)
static void applyEdgeSlide(TransInfo *t)
static void edge_slide_apply_elem(const TransDataEdgeSlideVert &sv, const float fac, const float curr_length_fac, const int curr_side_unclamp, const bool use_clamp, const bool use_even, const bool use_flip, float r_co[3])
bool validSnap(const TransInfo *t)
static void calcEdgeSlide_mval_range(TransInfo *t, TransDataContainer *tc, EdgeSlideData *sld, const int loop_nr, const float2 &mval, const bool use_calc_direction)
static void doEdgeSlide(TransInfo *t, float perc)
void transform_constraint_snap_axis_to_face(const TransInfo *t, const float axis[3], float r_out[3])
void transform_constraint_snap_axis_to_edge(const TransInfo *t, const float axis[3], float r_out[3])
Array< TransDataEdgeSlideVert > transform_mesh_uv_edge_slide_data_create(const TransInfo *t, TransDataContainer *tc, int *r_group_len)
static void freeEdgeSlideVerts(TransInfo *, TransDataContainer *, TransCustomData *custom_data)
static void calcEdgeSlideCustomPoints(TransInfo *t)
float transform_snap_distance_len_squared_fn(TransInfo *, const float p1[3], const float p2[3])
void setCustomPoints(TransInfo *t, MouseInput *mi, const int mval_start[2], const int mval_end[2])
static void initEdgeSlide(TransInfo *t, wmOperator *op)
static EdgeSlideData * createEdgeSlideVerts(TransInfo *t, TransDataContainer *tc, const bool use_double_side)
void applyMouseInput(TransInfo *t, MouseInput *mi, const float2 &mval, float output[3])
static void edge_slide_status(TransInfo *t)
static void drawEdgeSlide(TransInfo *t)
static eRedrawFlag handleEventEdgeSlide(TransInfo *t, const wmEvent *event)
static void edge_slide_data_init_mval(MouseInput *mi, EdgeSlideData *sld, float *mval_dir)
static void edge_slide_transform_matrix_fn(TransInfo *t, float mat_xform[4][4])
TransConvertTypeInfo TransConvertType_MeshUV
void transform_mode_edge_slide_reproject_input(TransInfo *t)
static void initEdgeSlide_ex(TransInfo *t, bool use_double_side, bool use_even, bool flipped, bool use_clamp)
static bool is_vert_slide_visible_bmesh(TransInfo *t, TransDataContainer *tc, const View3D *v3d, const BMBVHTree *bmbvh, TransDataEdgeSlideVert *sv)
T length(const VecBase< T, Size > &a)
T dot(const QuaternionBase< T > &a, const QuaternionBase< T > &b)
bool is_zero(const T &a)
VectorT project_point(const MatT &mat, const VectorT &point)
float get_value_f(int colorid)
void view2d_view_to_region_m4(const View2D *v2d, float matrix[4][4]) ATTR_NONNULL()
Definition view2d.cc:1789
MINLINE float min_ff(float a, float b)
MINLINE void negate_v3_v3(float r[3], const float a[3])
void BKE_bmbvh_free(BMBVHTree *tree)
float line_point_factor_v3(const float p[3], const float l1[3], const float l2[3])
bool BMBVH_EdgeVisible(const BMBVHTree *tree, const BMEdge *e, const Depsgraph *depsgraph, const ARegion *region, const View3D *v3d, const Object *obedit)
void immEnd()
PropertyRNA * RNA_struct_find_property(PointerRNA *ptr, const char *identifier)
BMesh * BKE_editmesh_bmesh_get_for_write(Mesh *mesh)
Definition editmesh.cc:79
Span< float3 > BKE_editmesh_vert_coords_when_deformed(Depsgraph *depsgraph, Scene *scene, Object *obedit, Array< float3 > &r_alloc)
Definition editmesh.cc:214
MINLINE void copy_v3_v3(float r[3], const float a[3])
unsigned int uint
Definition BKE_text.h:55
void * MEM_new_array_uninitialized(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
MINLINE void add_v3_v3v3(float r[3], const float a[3], const float b[3])
MINLINE void mul_v2_fl(float r[2], float f)
void immBeginAtMost(GPUPrimType, uint max_vertex_len)
float4x4 ED_view3d_ob_project_mat_get(const RegionView3D *rv3d, const Object *ob)
MINLINE void copy_v2_v2(float r[2], const float a[2])
char size_t BLI_strncpy_utf8_rlen(char *__restrict dst, const char *__restrict src, size_t dst_maxncpy) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1
GPUVertFormat * immVertexFormat()
void GPU_point_size(float size)
Definition gpu_state.cc:157
void immBegin(GPUPrimType, uint vertex_len)
bool hasNumInput(const NumInput *n)
Definition numinput.cc:188
ATTR_WARN_UNUSED_RESULT const BMVert * v
MatBase< float, 4, 4 > float4x4
void immUnbindProgram()
void GPU_matrix_mul(const float m[4][4])
MINLINE void mul_v3_fl(float r[3], float f)
void GPU_line_width(float width)
Definition gpu_state.cc:151
@ GPU_BLEND_NONE
Definition GPU_state.hh:88
@ GPU_BLEND_ALPHA
Definition GPU_state.hh:90
float dist_squared_to_line_segment_v2(const float p[2], const float l1[2], const float l2[2])
Definition math_geom.cc:307
bool RNA_property_boolean_get(PointerRNA *ptr, PropertyRNA *prop)
MINLINE void sub_v3_v3(float r[3], const float a[3])
void immBindBuiltinProgram(GPUBuiltinShader shader_id)
void mul_m4_v3(const float M[4][4], float r[3])
MINLINE void madd_v3_v3fl(float r[3], const float a[3], float f)
VecBase< float, 2 > float2
@ GPU_DEPTH_NONE
Definition GPU_state.hh:132
@ GPU_DEPTH_LESS_EQUAL
Definition GPU_state.hh:135
void * MEM_new_zeroed(size_t len, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)
MINLINE void swap_v3_v3(float a[3], float b[3])
void BKE_report(ReportList *reports, eReportType type, const char *message)
Definition report.cc:162
T * DEG_get_evaluated(const Depsgraph *depsgraph, T *id)
@ GPU_SHADER_3D_POINT_UNIFORM_SIZE_UNIFORM_COLOR_AA
BMBVHTree * BKE_bmbvh_new_from_editmesh(struct BMEditMesh *em, struct BMesh *bm, int flag, const float3 *cos_cage, bool cos_cage_free)
void interp_v3_v3v3(float r[3], const float a[3], const float b[3], float t)
void ED_area_status_text(ScrArea *area, const char *str)
Definition area.cc:975
void immUniformThemeColorShadeAlpha(int color_id, int color_offset, int alpha_offset)
uint GPU_vertformat_attr_add(GPUVertFormat *format, StringRef name, gpu::VertAttrType type)
bool applyNumInput(NumInput *n, float *vec)
Definition numinput.cc:207
BMEditMesh * BKE_editmesh_from_object(Object *ob)
Return the BMEditMesh for a given object.
Definition editmesh.cc:59
MINLINE float normalize_v3(float n[3])
MINLINE float len_squared_v2(const float v[2]) ATTR_WARN_UNUSED_RESULT
MINLINE void add_v3_v3(float r[3], const float a[3])
void immVertex3fv(uint attr_id, const float data[3])
MINLINE void sub_v3_v3v3(float r[3], const float a[3], const float b[3])
ATTR_WARN_UNUSED_RESULT const BMVert * v2
MINLINE void copy_v3_fl(float r[3], float f)
void outputNumInput(NumInput *n, char *str, const UnitSettings &unit_settings)
Definition numinput.cc:104
void GPU_blend(GPUBlend blend)
Definition gpu_state.cc:44
VecBase< float, 3 > float3
MINLINE float len_squared_v2v2(const float a[2], const float b[2]) ATTR_WARN_UNUSED_RESULT
@ B_UNIT_NONE
Definition BKE_unit.hh:165
void GPU_matrix_pop()
size_t BLI_snprintf_utf8_rlen(char *__restrict dst, size_t dst_maxncpy, const char *__restrict format,...) ATTR_NONNULL(1
void GPU_depth_test(GPUDepthTest test)
Definition gpu_state.cc:70
MINLINE void zero_v2(float r[2])
MINLINE bool is_zero_v3(const float v[3]) ATTR_WARN_UNUSED_RESULT
ATTR_WARN_UNUSED_RESULT const BMVert const BMEdge * e
void GPU_matrix_push()
void * MEM_new_array_zeroed(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
#define fabsf
const c_style_mat & ptr() const
int unit_type[NUM_MAX_ELEMENTS]
float val_inc[NUM_MAX_ELEMENTS]
struct UnitSettings unit
ListBaseT< SpaceLink > spacedata
void project(const TransDataEdgeSlideVert *svert, float2 &r_sco_a, float2 &r_sco_b) const
void update_proj_mat(TransInfo *t, const TransDataContainer *tc)
TransConvertTypeInfo * data_type
Definition transform.hh:836
TransCustomDataContainer custom
wmEventType type
Definition WM_types.hh:817
#define TRANS_DATA_CONTAINER_FIRST_OK(t)
Definition transform.hh:43
#define FOREACH_TRANS_DATA_CONTAINER(t, th)
Definition transform.hh:48
conversion and adaptation of different datablocks to a common struct.
transform modes used by different operators.