Blender V5.3
transform_snap.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 "BLI_bounds.hh"
10#include "BLI_listbase.hh"
11#include "BLI_math_matrix_c.hh"
13#include "BLI_math_vector_c.hh"
14#include "BLI_time.hh"
15
16#include "DNA_userdef_types.h"
17
18#include "GPU_immediate.hh"
19#include "GPU_matrix.hh"
20#include "GPU_state.hh"
21
22#include "BKE_layer.hh"
23#include "BKE_object.hh"
24#include "BKE_scene.hh"
25
26#include "RNA_access.hh"
27#include "RNA_prototypes.hh"
28
29#include "WM_api.hh"
30
31#include "ED_image.hh"
32#include "ED_node.hh"
34#include "ED_uvedit.hh"
35
36#include "UI_resources.hh"
37#include "UI_view2d.hh"
38
39#include "SEQ_sequencer.hh"
40
41#include "transform.hh"
43#include "transform_convert.hh"
44#include "transform_mode.hh"
45#include "transform_snap.hh"
46
47namespace blender::ed::transform {
48
49/* Use half of flt-max so we can scale up without an exception. */
50
51/* -------------------------------------------------------------------- */
54
55static void setSnappingCallback(TransInfo *t);
56
57static void snap_target_view3d_fn(TransInfo *t, float *vec);
58static void snap_target_uv_fn(TransInfo *t, float *vec);
59static void snap_target_sequencer_fn(TransInfo *t, float *vec);
60static void snap_target_nla_fn(TransInfo *t, float *vec);
61
62static void snap_source_median_fn(TransInfo *t);
63static void snap_source_center_fn(TransInfo *t);
64static void snap_source_closest_fn(TransInfo *t);
65static void snap_source_active_fn(TransInfo *t);
66
68 TransInfo *t, const float mval[2], float *dist_px, float r_loc[3], float r_no[3]);
69
71
72/* -------------------------------------------------------------------- */
75
76#if 0
77int BIF_snappingSupported(Object *obedit)
78{
79 int status = 0;
80
81 /* Only support object mesh, armature, curves. */
82 if (obedit == nullptr ||
84 {
85 status = 1;
86 }
87
88 return status;
89}
90#endif
91
93{
95 View3D *v3d = static_cast<View3D *>(t->view);
97 return true;
98 }
99 if (v3d->shading.type == OB_RENDER &&
102 {
103 return true;
104 }
106 return true;
107 }
108 return false;
109}
110
118
120{
122 /* Those space-types define their own invert behavior instead of toggling it on/off. */
123 return;
124 }
125 if (t->spacetype == SPACE_GRAPH) {
126 /* This is to stay consistent with the behavior from 3.6. */
127 if (t->modifiers & MOD_SNAP_INVERT) {
129 }
130 else {
132 }
133 /* In 3.6 when snapping was disabled, pressing the invert button would turn on snapping.
134 * But it wouldn't turn it off when it was enabled. */
135 if ((t->modifiers & MOD_SNAP) || (t->modifiers & MOD_SNAP_INVERT)) {
136 t->tsnap.flag |= SCE_SNAP;
137 }
138 else {
139 t->tsnap.flag &= ~SCE_SNAP;
140 }
141 return;
142 }
144 (((t->modifiers & (MOD_SNAP | MOD_SNAP_INVERT)) == MOD_SNAP) ||
146 SCE_SNAP);
147}
148
150{
151 return (t->tsnap.flag & SCE_SNAP) != 0;
152}
153
155{
156 /* The animation editors should not depend on the snapping options of the 3D viewport. */
158 return true;
159 }
160
161 /* In the VSE preview, for rotate/scale, we want snap disabled by default, with increment snap on
162 * `ctrl`. Do this by returning `false` here, which sets/unsets the necessary flags elsewhere. */
165 {
166 return false;
167 }
168
169 ToolSettings *ts = t->settings;
170 if (t->mode == TFM_TRANSLATION) {
172 }
173 if (t->mode == TFM_ROTATION) {
175 }
176 if (t->mode == TFM_RESIZE) {
178 }
179 if (ELEM(t->mode,
185 {
186 return true;
187 }
188
189 return false;
190}
191
192static bool doForceIncrementSnap(const TransInfo *t)
193{
195 /* These spaces don't support increment snapping. */
196 return false;
197 }
198
199 if (t->modifiers & MOD_SNAP_FORCED) {
200 return false;
201 }
202
203 return !transformModeUseSnap(t);
204}
205
207{
208 uchar col[4], selectedCol[4], activeCol[4];
210 return;
211 }
212
213 const bool draw_source = (t->flag & T_DRAW_SNAP_SOURCE) &&
215 const bool draw_target = (t->tsnap.status & (SNAP_TARGET_FOUND | SNAP_MULTI_POINTS));
216
217 if (!(draw_source || draw_target)) {
218 return;
219 }
220
221 if (t->spacetype == SPACE_SEQ) {
223 col[3] = 128;
224 }
225 else {
227 col[3] = 128;
228
230 selectedCol[3] = 128;
231
233 activeCol[3] = 192;
234 }
235
236 if (t->spacetype == SPACE_VIEW3D) {
237 const float *source_loc = nullptr;
238 const float *target_loc = nullptr;
239
241
242 RegionView3D *rv3d = static_cast<RegionView3D *>(t->region->regiondata);
243 if (!t->tsnap.points.is_empty()) {
244 /* Draw snap points. */
245
246 float size = 2.0f * ui::theme::get_value_f(TH_VERTEX_SIZE);
247 float view_inv[4][4];
248 copy_m4_m4(view_inv, rv3d->viewinv);
249
251 immVertexFormat(), "pos", gpu::VertAttrType::SFLOAT_32_32_32);
252
254
255 if (!t->tsnap.points.is_empty()) {
256 for (TransSnapPoint &p : t->tsnap.points) {
257 if (&p == t->tsnap.selectedPoint) {
258 immUniformColor4ubv(selectedCol);
259 }
260 else {
262 }
263 imm_drawcircball(p.co, ED_view3d_pixel_size(rv3d, p.co) * size, view_inv, pos);
264 }
265 }
266
268 }
269
270 if (draw_source) {
271 source_loc = t->tsnap.snap_source;
272 }
273
274 if (t->tsnap.status & SNAP_TARGET_FOUND) {
275 target_loc = t->tsnap.snap_target;
276 }
277
279 rv3d, source_loc, target_loc, t->tsnap.source_type, t->tsnap.target_type, col, activeCol);
280
281 /* Draw normal if needed. */
282 if (target_loc && usingSnappingNormal(t) && validSnappingNormal(t)) {
284 immVertexFormat(), "pos", gpu::VertAttrType::SFLOAT_32_32_32);
285
287 immUniformColor4ubv(activeCol);
289 immVertex3fv(pos, target_loc);
290 immVertex3f(pos,
291 target_loc[0] + t->tsnap.snapNormal[0],
292 target_loc[1] + t->tsnap.snapNormal[1],
293 target_loc[2] + t->tsnap.snapNormal[2]);
294 immEnd();
296 }
297
299 }
300 else if (t->spacetype == SPACE_IMAGE) {
301 uint pos = GPU_vertformat_attr_add(immVertexFormat(), "pos", gpu::VertAttrType::SFLOAT_32_32);
302
305 float radius = 2.5f * ui::theme::get_value_f(TH_VERTEX_SIZE) * U.pixelsize;
307
308 if (!t->tsnap.points.is_empty()) {
310 for (TransSnapPoint &p : t->tsnap.points) {
311 if (&p == t->tsnap.selectedPoint) {
312 immUniformColor4ubv(selectedCol);
313 }
314 else {
316 }
317 float x, y;
318 const float2 snap_point = float2(p.co) / float2(t->aspect);
319 ui::view2d_view_to_region_fl(&t->region->v2d, snap_point.x, snap_point.y, &x, &y);
320 imm_draw_circle_wire_2d(pos, x, y, radius, 8);
321 }
323 }
324
325 float x, y;
326 const float snap_point[2] = {
327 t->tsnap.snap_target[0] / t->aspect[0],
328 t->tsnap.snap_target[1] / t->aspect[1],
329 };
330 ui::view2d_view_to_region_fl(&t->region->v2d, UNPACK2(snap_point), &x, &y);
331
333 immUniformColor3ub(255, 255, 255);
334 imm_draw_circle_wire_2d(pos, x, y, radius, 8);
336
339 }
340 else if (t->spacetype == SPACE_SEQ) {
341 const ARegion *region = t->region;
343 uint pos = GPU_vertformat_attr_add(immVertexFormat(), "pos", gpu::VertAttrType::SFLOAT_32_32);
345 float pixelx = ui::view2d_pixel_size_get_x(&region->v2d);
346
347 const float target_x = t->tsnap.snap_target[0];
348 const float target_y = t->tsnap.snap_target[1];
349
350 auto draw_vline = [&](float ymin, float ymax, uchar alpha) {
351 immUniformColor4ub(col[0], col[1], col[2], alpha);
352 immRectf(pos, target_x - pixelx, ymin, target_x + pixelx, ymax);
353 };
354
355 auto draw_hline = [&](float xmin, float xmax, uchar alpha) {
356 immUniformColor4ub(col[0], col[1], col[2], alpha);
357 immRectf(pos, xmin, target_y - pixelx, xmax, target_y + pixelx);
358 };
359
360 if (region->regiontype == RGN_TYPE_PREVIEW) {
361 if (t->tsnap.direction & DIR_GLOBAL_X) {
362 draw_vline(region->v2d.cur.ymin, region->v2d.cur.ymax, col[3]);
363 }
364 if (t->tsnap.direction & DIR_GLOBAL_Y) {
365 draw_hline(region->v2d.cur.xmin, region->v2d.cur.xmax, col[3]);
366 }
367 }
368 else {
369 const short snap_flag = seq::tool_settings_snap_flag_get(t->scene);
370 if ((snap_flag & SEQ_SNAP_TO_ALL_CHANNEL_STRIPS) || target_y == 0) {
371 draw_vline(region->v2d.cur.ymin, region->v2d.cur.ymax, col[3]);
372 }
373 else {
374 /* Extend fully opaque line a half-channel below,
375 * through current channel, and a half-channel above. */
376 draw_vline(target_y - 0.5, target_y + 1.5, col[3]);
377 /* Draw more transparent line from top to bottom. */
378 draw_vline(region->v2d.cur.ymin, region->v2d.cur.ymax, col[3] / 8);
379 }
380 }
381
384 }
385}
386
388{
390
391#if 0 /* XXX: need a proper selector for all snap mode. */
392 if (BIF_snappingSupported(t->obedit) && (event->type == EVT_TABKEY) &&
393 (event->modifier & KM_SHIFT))
394 {
395 /* Toggle snap and reinitialize. */
397 initSnapping(t, nullptr);
398 status = TREDRAW_HARD;
399 }
400#endif
401 if (event->type == MOUSEMOVE) {
402 status |= updateSelectedSnapPoint(t);
403 }
404
405 return status;
406}
407
410 TransData *td,
411 TransDataExtension *td_ext)
412{
413 if (t->spacetype != SPACE_VIEW3D) [[unlikely]] {
414 return false;
415 }
416
417 float iloc[3], loc[3], no[3];
418 float mval_fl[2];
419
420 copy_v3_v3(iloc, td->loc);
421 if (tc->use_local_mat) {
422 mul_m4_v3(tc->mat, iloc);
423 }
424 else if (t->options & CTX_OBJECT) {
425 Object *ob = static_cast<Object *>(td->extra);
427 copy_v3_v3(iloc, ob->object_to_world().location());
428 }
429
432 {
433 return false;
434 }
435
436 SnapObjectParams snap_object_params{};
437 snap_object_params.snap_target_select = t->tsnap.target_operation;
438 snap_object_params.edit_mode_type = (t->flag & T_EDIT) != 0 ? SNAP_GEOM_EDIT : SNAP_GEOM_FINAL;
439 snap_object_params.occlusion_test = SNAP_OCCLUSION_ALWAYS;
440 snap_object_params.use_backface_culling = (t->tsnap.flag & SCE_SNAP_BACKFACE_CULLING) != 0;
441
443 t->depsgraph,
444 t->region,
445 static_cast<const View3D *>(t->view),
447 &snap_object_params,
448 nullptr,
449 mval_fl,
450 nullptr,
451 nullptr,
452 loc,
453 no);
454 if (hit != SCE_SNAP_TO_FACE) {
455 return false;
456 }
457
458 float tvec[3];
459 sub_v3_v3v3(tvec, loc, iloc);
460
461 mul_m3_v3(td->smtx, tvec);
462
463 add_v3_v3(td->loc, tvec);
464
465 if ((t->tsnap.flag & SCE_SNAP_ROTATE) && (t->options & CTX_OBJECT)) {
466 /* Handle alignment as well. */
467 const float *original_normal;
468 float mat[3][3];
469
470 /* In pose mode, we want to align normals with Y axis of bones. */
471 original_normal = td->axismtx[2];
472
473 rotation_between_vecs_to_mat3(mat, original_normal, no);
474
475 transform_data_ext_rotate(td, td_ext, mat, true);
476
477 /* TODO: support constraints for rotation too? see #ElementRotation. */
478 }
479 return true;
480}
481
483{
484 float init_loc[3];
485 float prev_loc[3];
486 float snap_loc[3], snap_no[3];
487
488 copy_v3_v3(init_loc, td->iloc);
489 copy_v3_v3(prev_loc, td->loc);
490 if (tc->use_local_mat) {
491 mul_m4_v3(tc->mat, init_loc);
492 mul_m4_v3(tc->mat, prev_loc);
493 }
494 else if (t->options & CTX_OBJECT) {
495 Object *ob = static_cast<Object *>(td->extra);
497 copy_v3_v3(init_loc, ob->object_to_world().location());
498 }
499
500 SnapObjectParams snap_object_params{};
501 snap_object_params.snap_target_select = t->tsnap.target_operation;
502 snap_object_params.edit_mode_type = (t->flag & T_EDIT) != 0 ? SNAP_GEOM_EDIT : SNAP_GEOM_FINAL;
503 snap_object_params.occlusion_test = SNAP_OCCLUSION_ALWAYS;
504 snap_object_params.use_backface_culling = false;
505 snap_object_params.face_nearest_steps = t->tsnap.face_nearest_steps;
507
509 t->depsgraph,
510 t->region,
511 static_cast<const View3D *>(t->view),
513 &snap_object_params,
514 init_loc,
515 nullptr,
516 prev_loc,
517 nullptr,
518 snap_loc,
519 snap_no);
520
521 if (hit != SCE_SNAP_INDIVIDUAL_NEAREST) {
522 return;
523 }
524
525 float tvec[3];
526 sub_v3_v3v3(tvec, snap_loc, prev_loc);
527 mul_m3_v3(td->smtx, tvec);
528 add_v3_v3(td->loc, tvec);
529
530 /* TODO: support snap alignment similar to #SCE_SNAP_INDIVIDUAL_PROJECT? */
531}
532
534{
535 if (!transform_snap_is_active(t)) {
536 return false;
537 }
538
540}
541
543{
545 return;
546 }
547
548 /* XXX: flickers in object mode. */
550 TransData *td = tc->data;
551 TransDataExtension *td_ext = tc->data_ext;
552 for (int i = 0; i < tc->data_len; i++, td++) {
553 if (td->flag & TD_SKIP) {
554 continue;
555 }
556
557 if ((t->flag & T_PROP_EDIT) && (td->factor == 0.0f)) {
558 continue;
559 }
560
561 /* If both face ray-cast and face nearest methods are enabled, start with face ray-cast and
562 * fall back to face nearest ray-cast does not hit. */
563 bool hit = false;
565 hit = applyFaceProject(t, tc, td, td_ext);
566 if (td_ext) {
567 td_ext++;
568 }
569 }
570
571 if (!hit && t->tsnap.mode & SCE_SNAP_INDIVIDUAL_NEAREST) {
572 applyFaceNearest(t, tc, td);
573 }
574#if 0 /* TODO: support this? */
575 constraintTransLim(t, td);
576#endif
577 }
578 }
579}
580
592
594{
596 return;
597 }
598
599 if (t->tsnap.mode != SCE_SNAP_TO_INCREMENT) {
600 double current = BLI_time_now_seconds();
601
602 /* Time base quirky code to go around find-nearest slowness. */
603 /* TODO: add exception for object mode, no need to slow it down then. */
604 if (current - t->tsnap.last >= 0.01) {
605 if (t->tsnap.snap_target_fn) {
606 t->tsnap.snap_target_fn(t, vec);
607 }
608 if (t->tsnap.snap_source_fn) {
609 t->tsnap.snap_source_fn(t);
610 }
611
612 t->tsnap.last = current;
613 }
614
615 if (validSnap(t)) {
616 t->mode_info->snap_apply_fn(t, vec);
617 }
618 }
619}
620
635
637{
638 return (t->tsnap.flag & SCE_SNAP_ROTATE) != 0;
639}
640
642{
643 if (validSnap(t)) {
644 if (!is_zero_v3(t->tsnap.snapNormal)) {
645 return true;
646 }
647 }
648
649 return false;
650}
651
653 const PropertyRNA **r_prop = nullptr)
654{
655 ToolSettings *ts = t->settings;
656 switch (t->spacetype) {
657 case SPACE_VIEW3D:
658 if (r_prop) {
659 *r_prop = &rna_ToolSettings_use_snap;
660 }
661 return &ts->snap_flag;
662 case SPACE_NODE:
663 if (r_prop) {
664 *r_prop = &rna_ToolSettings_use_snap_node;
665 }
666 return &ts->snap_flag_node;
667 case SPACE_IMAGE:
668 if (r_prop) {
669 *r_prop = &rna_ToolSettings_use_snap_uv;
670 }
671 return &ts->snap_uv_flag;
672 case SPACE_SEQ:
673 if (r_prop) {
674 *r_prop = &rna_ToolSettings_use_snap_sequencer;
675 }
676 return &ts->snap_flag_seq;
677 case SPACE_ACTION:
678 case SPACE_NLA:
679 if (r_prop) {
680 *r_prop = &rna_ToolSettings_use_snap_anim;
681 }
682 return &ts->snap_flag_anim;
683 case SPACE_GRAPH: {
684 SpaceGraph *graph_editor = t->area->spacedata.first_as<SpaceGraph>();
685 switch (graph_editor->mode) {
687 /* The driver editor has a separate snapping flag so it can be kept disabled while
688 * keeping it enabled in the Graph Editor. */
689 return &ts->snap_flag_driver;
690
691 case SIPO_MODE_ANIMATION: {
692 if (r_prop) {
693 *r_prop = &rna_ToolSettings_use_snap_anim;
694 }
695 return &ts->snap_flag_anim;
696 }
697 default:
699 break;
700 }
701 }
702 }
703 /* #SPACE_EMPTY.
704 * It can happen when the operator is called via a handle in `bpy.app.handlers`. */
705 return nullptr;
706}
707
709{
711 return *snap_flag;
712 }
713
714 /* #SPACE_EMPTY.
715 * It can happen when the operator is called via a handle in `bpy.app.handlers`. */
716 return eSnapFlag{};
717}
718
720{
721 ToolSettings *ts = t->settings;
722
723 if (t->spacetype == SPACE_NODE) {
724 return eSnapMode(ts->snap_node_mode);
725 }
726
727 if (t->spacetype == SPACE_IMAGE) {
728 return eSnapMode(ts->snap_uv_mode);
729 }
730
731 if (t->spacetype == SPACE_SEQ) {
733 }
734
735 if (t->spacetype == SPACE_VIEW3D) {
738 }
739
740 return eSnapMode(ts->snap_mode);
741 }
742
744 return eSnapMode(ts->snap_anim_mode);
745 }
746
747 if (t->spacetype == SPACE_GRAPH) {
748 SpaceGraph *graph_editor = t->area->spacedata.first_as<SpaceGraph>();
749 switch (graph_editor->mode) {
751 /* Snapping to full values is the only mode that currently makes
752 * sense for the driver editor. */
753 return SCE_SNAP_TO_FRAME;
754
756 return eSnapMode(ts->snap_anim_mode);
757
758 default:
760 break;
761 }
762 }
763
765}
766
768{
769 /* `t->tsnap.target_operation` not initialized yet. */
771
772 eSnapTargetOP target_operation = SCE_SNAP_TARGET_ALL;
773
777 const int obedit_type = t->obedit_type;
778 if (base_act && (base_act->object->mode & OB_MODE_PARTICLE_EDIT)) {
779 /* Particles edit mode. */
780 }
782 /* In "Edit Strokes" mode,
783 * snap tool can perform snap to selected or active objects (see #49632)
784 * TODO: perform self snap in gpencil_strokes.
785 *
786 * When we're moving the origins, allow snapping onto our own geometry (see #69132). */
787 }
788 else if (obedit_type != -1) {
789 /* Edit mode. */
790 if (obedit_type == OB_MESH) {
791 /* Editing a mesh. */
792 if ((t->flag & T_PROP_EDIT) != 0) {
793 /* Exclude editmesh when using proportional edit. */
794 target_operation |= SCE_SNAP_TARGET_NOT_EDITED;
795 }
796 /* UV editing must never snap to the selection as this is what is transformed. */
797 if (t->spacetype == SPACE_IMAGE) {
798 target_operation |= SCE_SNAP_TARGET_NOT_SELECTED;
799 }
800 }
801 else if (ELEM(obedit_type, OB_ARMATURE, OB_CURVES_LEGACY, OB_SURF, OB_LATTICE, OB_MBALL)) {
802 /* Temporary limited to edit mode armature, curves, surfaces, lattices, and meta-balls.
803 */
804 target_operation |= SCE_SNAP_TARGET_NOT_SELECTED;
805 }
806 }
807 else {
808 /* Object or pose mode. */
810 }
811 }
812 else if (ELEM(t->spacetype, SPACE_NODE, SPACE_SEQ)) {
813 target_operation |= SCE_SNAP_TARGET_NOT_SELECTED;
814 }
815
816 /* Use scene defaults only when transform is modal. */
817 if (t->flag & T_MODAL) {
818 ToolSettings *ts = t->settings;
821 SET_FLAG_FROM_TEST(target_operation,
824 SET_FLAG_FROM_TEST(target_operation,
827 SET_FLAG_FROM_TEST(target_operation,
830 }
831
832 return target_operation;
833}
834
836{
837 /* When editing a mesh, the selected elements are the ones being transformed, so snapping to
838 * them is meaningless. */
840 t->tsnap.object_context,
844}
845
847{
848 if (!transformModeUseSnap(t)) {
849 /* In this case, snapping is always disabled by default. */
850 t->modifiers &= ~MOD_SNAP;
851 }
852 else if (t->flag & T_MODAL) {
853 /* Use scene defaults only when transform is modal. */
854 if (t->tsnap.flag & SCE_SNAP) {
855 t->modifiers |= MOD_SNAP;
856 }
857 }
858
859 if (doForceIncrementSnap(t)) {
861 }
862
863 if (!ELEM(t->spacetype, SPACE_VIEW3D, SPACE_SEQ) || (t->flag & T_NO_PROJECT)) {
864 /* Force project off when not supported. */
866 }
867
870 }
871}
872
873void transform_snap_grid_init(const TransInfo *t, float r_snap[3], float *r_snap_precision)
874{
875 /* Default values. */
876 r_snap[0] = r_snap[1] = 1.0f;
877 r_snap[2] = 0.0f;
878 *r_snap_precision = 0.1f;
879
880 if (t->spacetype == SPACE_VIEW3D) {
881 /* Used by incremental snap. */
882 if (t->region && t->region->regiontype == RGN_TYPE_WINDOW) {
883 View3D *v3d = t->area->spacedata.first_as<View3D>();
884 r_snap[0] = r_snap[1] = r_snap[2] = ED_view3d_grid_view_scale(
885 t->scene, v3d, t->region, nullptr);
886 }
887 }
888 else if (t->spacetype == SPACE_IMAGE) {
889 if (t->region && t->region->regiontype == RGN_TYPE_WINDOW) {
890 SpaceImage *sima = t->area->spacedata.first_as<SpaceImage>();
891 const View2D *v2d = &t->region->v2d;
892 int grid_size = SI_GRID_STEPS_LEN;
893 float zoom_factor = ED_space_image_zoom_level(v2d, grid_size);
894 float grid_steps_x[SI_GRID_STEPS_LEN];
895 float grid_steps_y[SI_GRID_STEPS_LEN];
896
897 ED_space_image_grid_steps(sima, grid_steps_x, grid_steps_y, grid_size);
898 /* Snapping value based on what type of grid is used (adaptive-subdividing or custom-grid).
899 */
900 r_snap[0] = ED_space_image_increment_snap_value(grid_size, grid_steps_x, zoom_factor);
901 r_snap[1] = ED_space_image_increment_snap_value(grid_size, grid_steps_y, zoom_factor);
902 *r_snap_precision = 0.5f;
903 }
904 }
905 else if (t->spacetype == SPACE_CLIP) {
906 r_snap[0] = r_snap[1] = 0.125f;
907 *r_snap_precision = 0.5f;
908 }
909 else if (t->spacetype == SPACE_NODE) {
910 r_snap[0] = r_snap[1] = space_node::grid_size_get();
911 }
912}
913
915{
916 ToolSettings *ts = t->settings;
917 eSnapSourceOP snap_source = eSnapSourceOP(ts->snap_target);
918
919 resetSnapping(t);
920
925
927
928 /* Overwrite defaults with values ​​in properties. */
929 PropertyRNA *prop;
930 if (op && (prop = RNA_struct_find_property(op->ptr, "snap"))) {
931 if (RNA_property_is_set(op->ptr, prop)) {
933 }
934
935 if ((prop = RNA_struct_find_property(op->ptr, "snap_elements")) &&
936 RNA_property_is_set(op->ptr, prop))
937 {
939 }
940
941 /* TODO(@gfxcoder): Rename `snap_target` to `snap_source` to avoid previous ambiguity of
942 * "target" (now, "source" is geometry to be moved and "target" is geometry to which moved
943 * geometry is snapped). */
944 if ((prop = RNA_struct_find_property(op->ptr, "snap_target")) &&
945 RNA_property_is_set(op->ptr, prop))
946 {
947 snap_source = eSnapSourceOP(RNA_property_enum_get(op->ptr, prop));
948 }
949
950 if ((prop = RNA_struct_find_property(op->ptr, "snap_point")) &&
951 RNA_property_is_set(op->ptr, prop))
952 {
956 }
957
958 /* Snap align only defined in specific cases. */
959 if ((prop = RNA_struct_find_property(op->ptr, "snap_align")) &&
960 RNA_property_is_set(op->ptr, prop))
961 {
963
964 RNA_float_get_array(op->ptr, "snap_normal", t->tsnap.snapNormal);
966 }
967
968 if ((prop = RNA_struct_find_property(op->ptr, "use_snap_project")) &&
969 RNA_property_is_set(op->ptr, prop))
970 {
973 }
974
975 /* Use_snap_self is misnamed and should be use_snap_active. */
976 if ((prop = RNA_struct_find_property(op->ptr, "use_snap_self")) &&
977 RNA_property_is_set(op->ptr, prop))
978 {
980 !RNA_property_boolean_get(op->ptr, prop),
982 }
983
984 if ((prop = RNA_struct_find_property(op->ptr, "use_snap_edit")) &&
985 RNA_property_is_set(op->ptr, prop))
986 {
988 !RNA_property_boolean_get(op->ptr, prop),
990 }
991
992 if ((prop = RNA_struct_find_property(op->ptr, "use_snap_nonedit")) &&
993 RNA_property_is_set(op->ptr, prop))
994 {
996 !RNA_property_boolean_get(op->ptr, prop),
998 }
999
1000 if ((prop = RNA_struct_find_property(op->ptr, "use_snap_selectable")) &&
1001 RNA_property_is_set(op->ptr, prop))
1002 {
1004 RNA_property_boolean_get(op->ptr, prop),
1006 }
1007 }
1008
1009 t->tsnap.source_operation = snap_source;
1012}
1013
1015{
1019
1020 if (t->spacetype == SPACE_VIEW3D) {
1021 if (t->tsnap.object_context == nullptr) {
1023 t->tsnap.object_context = snap_object_context_create();
1025 }
1026 }
1027 else if (t->spacetype == SPACE_SEQ) {
1028 if (t->tsnap.seq_context == nullptr) {
1029 t->tsnap.seq_context = snap_sequencer_data_build(t);
1030 }
1031 }
1032
1033 /* Default increment values. */
1034 t->increment = float3(1.0f);
1035 t->increment_precision = 0.1f;
1036}
1037
1039{
1040 if ((t->spacetype == SPACE_SEQ) && t->tsnap.seq_context) {
1041 snap_sequencer_data_free(t->tsnap.seq_context);
1042 t->tsnap.seq_context = nullptr;
1043 }
1044 else if (t->tsnap.object_context) {
1046 t->tsnap.object_context = nullptr;
1047
1049 }
1050}
1051
1053{
1054 /* The final value of increment with precision is `t->increment[0] * t->increment_precision`.
1055 * Therefore, we divide `snap_angle_increment_*_precision` by `snap_angle_increment_*`
1056 * to compute `increment_precision`. */
1057 float increment;
1058 float increment_precision;
1059 if (t->spacetype == SPACE_VIEW3D) {
1060 increment = t->settings->snap_angle_increment_3d;
1061 increment_precision = t->settings->snap_angle_increment_3d_precision;
1062 }
1063 else {
1064 increment = t->settings->snap_angle_increment_2d;
1065 increment_precision = t->settings->snap_angle_increment_2d_precision;
1066 }
1067
1068 t->increment[0] = increment;
1069 if (increment != 0.0f) {
1070 t->increment_precision = float(double(increment_precision) / double(increment));
1071 }
1072 else {
1073 t->increment_precision = 1.0f;
1074 }
1075}
1076
1078{
1079 if (t->spacetype == SPACE_VIEW3D) {
1080 if (t->options & CTX_CAMERA) {
1081 /* Not with camera selected in camera view. */
1082 return;
1083 }
1085 }
1086 else if (t->spacetype == SPACE_IMAGE) {
1087 SpaceImage *sima = t->area->spacedata.first_as<SpaceImage>();
1090
1091 const bool is_uv_editor = sima->mode == SI_MODE_UV;
1092 const bool has_edit_object = obact && BKE_object_is_in_editmode(obact);
1093 if (is_uv_editor && has_edit_object) {
1095 }
1096 }
1097 else if (t->spacetype == SPACE_NODE) {
1098 /* Pass. */
1099 }
1100 else if (t->spacetype == SPACE_SEQ) {
1102 /* The target is calculated along with the snap point. */
1103 return;
1104 }
1105 else if (t->spacetype == SPACE_NLA) {
1107 /* The target is calculated along with the snap point. */
1108 return;
1109 }
1110 else {
1111 return;
1112 }
1113
1114 switch (t->tsnap.source_operation) {
1117 break;
1119 if (!ELEM(t->mode, TFM_ROTATION, TFM_RESIZE)) {
1121 break;
1122 }
1123 /* Can't do TARGET_CENTER with these modes,
1124 * use TARGET_MEDIAN instead. */
1128 break;
1131
1132 /* XXX, workaround: active needs to be calculated before transforming, otherwise
1133 * `t->tsnap.snap_source` will be calculated with the transformed data since we're not
1134 * reading from 'td->center' in this case. (See: #40241 and #40348). */
1136 break;
1137 }
1138}
1139
1141{
1144
1145 t->tsnap.selectedPoint = p;
1146
1148
1149 BLI_addtail(&t->tsnap.points, p);
1150
1152 }
1153}
1154
1156{
1158
1159 if (t->tsnap.status & SNAP_MULTI_POINTS) {
1160 TransSnapPoint *closest_p = nullptr;
1161 float dist_min_sq = TRANSFORM_SNAP_MAX_PX;
1162 float screen_loc[2];
1163
1164 for (TransSnapPoint &p : t->tsnap.points) {
1165 float dist_sq;
1166
1167 if (t->spacetype == SPACE_VIEW3D) {
1170 {
1171 continue;
1172 }
1173 }
1174 else if (t->spacetype == SPACE_IMAGE) {
1175 float x, y;
1176 const float2 snap_point = float2(p.co) / float2(t->aspect);
1177 ui::view2d_view_to_region_fl(&t->region->v2d, snap_point.x, snap_point.y, &x, &y);
1178 screen_loc[0] = x;
1179 screen_loc[1] = y;
1180 }
1181 else {
1183 continue;
1184 }
1185
1186 dist_sq = len_squared_v2v2(t->mval, screen_loc);
1187
1188 if (dist_sq < dist_min_sq) {
1189 closest_p = &p;
1190 dist_min_sq = dist_sq;
1191 }
1192 }
1193
1194 if (closest_p) {
1195 if (t->tsnap.selectedPoint != closest_p) {
1196 status = TREDRAW_HARD;
1197 }
1198
1199 t->tsnap.selectedPoint = closest_p;
1200 }
1201 }
1202
1203 return status;
1204}
1205
1207{
1208 if (t->tsnap.status & SNAP_MULTI_POINTS) {
1210
1211 if (t->tsnap.selectedPoint) {
1213
1214 if (t->tsnap.points.is_empty()) {
1216 }
1217
1218 t->tsnap.selectedPoint = nullptr;
1219 }
1220 }
1221}
1222
1223void getSnapPoint(const TransInfo *t, float vec[3])
1224{
1225 if (t->tsnap.points.first_) {
1226 TransSnapPoint *p;
1227 int total = 0;
1228
1229 vec[0] = vec[1] = vec[2] = 0;
1230
1231 for (p = t->tsnap.points.first(); p; p = p->next, total++) {
1232 add_v3_v3(vec, p->co);
1233 }
1234
1235 if (t->tsnap.status & SNAP_TARGET_FOUND) {
1236 add_v3_v3(vec, t->tsnap.snap_target);
1237 total++;
1238 }
1239
1240 mul_v3_fl(vec, 1.0f / total);
1241 }
1242 else {
1243 copy_v3_v3(vec, t->tsnap.snap_target);
1244 }
1245}
1246
1248{
1249 if (t->tsnap.status & SNAP_MULTI_POINTS) {
1250 t->tsnap.points.free_no_destruct();
1252 t->tsnap.selectedPoint = nullptr;
1253 }
1254}
1255
1257
1258/* -------------------------------------------------------------------- */
1261
1262static void snap_grid_uv_apply(TransInfo *t, const float grid_dist[2], float r_out[2])
1263{
1264 float3 in;
1265 convertViewVec(t, in, t->mval[0] - t->center2d[0], t->mval[1] - t->center2d[1]);
1266
1267 if (t->con.mode & CON_APPLY) {
1268 /* We need to clear the previous Snap to Grid result,
1269 * otherwise #t->con.applyVec will have no effect. */
1273 }
1274
1275 const float *center_global = t->center_global;
1276 for (int i = 0; i < 2; i++) {
1277 const float iter_fac = grid_dist[i];
1278 r_out[i] = iter_fac * roundf((in[i] + center_global[i]) / iter_fac);
1279 }
1280}
1281
1282static bool snap_grid_uv(TransInfo *t, float r_val[2])
1283{
1284 float grid_dist[2];
1285 mul_v2_v2v2(grid_dist, t->snap_spatial, t->aspect);
1286 if (t->modifiers & MOD_PRECISION) {
1287 mul_v2_fl(grid_dist, t->snap_spatial_precision);
1288 }
1289
1290 /* Early bailing out if no need to snap */
1291 if (is_zero_v2(grid_dist)) {
1292 return false;
1293 }
1294
1295 snap_grid_uv_apply(t, grid_dist, r_val);
1297 return true;
1298}
1299
1301
1302/* -------------------------------------------------------------------- */
1305
1306static void snap_target_view3d_fn(TransInfo *t, float * /*vec*/)
1307{
1309 float loc[3];
1310 float no[3];
1311 bool found = false;
1312 eSnapMode snap_elem = SCE_SNAP_TO_NONE;
1313 float dist_px = SNAP_MIN_DISTANCE; /* Use a user defined value here. */
1314
1316 zero_v3(no); /* objects won't set this */
1317 snap_elem = snapObjectsTransform(t, t->mval, &dist_px, loc, no);
1318 found = (snap_elem != SCE_SNAP_TO_NONE);
1319 }
1320 if ((found == false) && (t->tsnap.mode & SCE_SNAP_TO_VOLUME)) {
1321 bool use_peel = (t->settings->snap_flag & SCE_SNAP_PEEL_OBJECT) != 0;
1322 found = peelObjectsTransform(t, t->mval, use_peel, loc, no, nullptr);
1323
1324 if (found) {
1325 snap_elem = SCE_SNAP_TO_VOLUME;
1326 }
1327 }
1328
1329 if (found == true) {
1330 copy_v3_v3(t->tsnap.snap_target, loc);
1331 copy_v3_v3(t->tsnap.snapNormal, no);
1332
1334
1335 if (snap_elem == SCE_SNAP_TO_GRID && t->mode_info != &TransMode_translate) {
1336 /* Change it to #SCE_SNAP_TO_POINT so we can see the symbol for other modes. */
1337 snap_elem = SCE_SNAP_TO_POINT;
1338 }
1339 }
1340 else {
1342 }
1343
1344 t->tsnap.target_type = snap_elem;
1345}
1346
1347static void snap_target_uv_fn(TransInfo *t, float * /*vec*/)
1348{
1350 bool found = false;
1351 if (t->tsnap.mode & SCE_SNAP_TO_VERTEX) {
1352 const Vector<Object *> objects =
1354 *t->bmain, t->scene, t->view_layer, nullptr);
1355
1356 float dist_sq = square_f(float(SNAP_MIN_DISTANCE));
1358 t->scene,
1359 objects,
1360 t->mval,
1362 &dist_sq,
1363 t->tsnap.snap_target))
1364 {
1365 t->tsnap.snap_target[0] *= t->aspect[0];
1366 t->tsnap.snap_target[1] *= t->aspect[1];
1368 found = true;
1369 }
1370 }
1371
1372 if (!found && (t->tsnap.mode & SCE_SNAP_TO_GRID)) {
1373 found = snap_grid_uv(t, t->tsnap.snap_target);
1374 }
1375
1377}
1378
1379static void snap_target_sequencer_fn(TransInfo *t, float * /*vec*/)
1380{
1382 if (snap_sequencer_calc(t)) {
1384 }
1385 else {
1387 }
1388}
1389
1390static void snap_target_nla_fn(TransInfo *t, float *vec)
1391{
1393 if (transform_snap_nla_calc(t, vec)) {
1395 }
1396 else {
1398 }
1399}
1400
1402
1403/* -------------------------------------------------------------------- */
1406
1407void tranform_snap_target_median_calc(const TransInfo *t, float r_median[3])
1408{
1409 int i_accum = 0;
1410
1411 zero_v3(r_median);
1412
1414 float v[3];
1415 zero_v3(v);
1416
1417 int num_selected = 0;
1418 tc->foreach_index_selected([&](const int i) {
1419 add_v3_v3(v, tc->data[i].center);
1420 num_selected++;
1421 });
1422
1423 if (num_selected == 0) {
1424 /* Is this possible? */
1425 continue;
1426 }
1427
1428 mul_v3_fl(v, 1.0 / num_selected);
1429
1430 if (tc->use_local_mat) {
1431 mul_m4_v3(tc->mat, v);
1432 }
1433
1434 add_v3_v3(r_median, v);
1435 i_accum++;
1436 }
1437
1438 mul_v3_fl(r_median, 1.0 / i_accum);
1439}
1440
1442{
1443 /* Only need to calculate once. */
1444 if ((t->tsnap.status & SNAP_SOURCE_FOUND) == 0) {
1446
1449 }
1450}
1451
1453{
1454 /* Only need to calculate once. */
1455 if ((t->tsnap.status & SNAP_SOURCE_FOUND) == 0) {
1456 if (calculateCenterActive(t, true, t->tsnap.snap_source)) {
1459 }
1460 else {
1461 /* No active, default to median, */
1465 }
1466 }
1467}
1468
1470{
1471 /* Only need to calculate once. */
1472 if ((t->tsnap.status & SNAP_SOURCE_FOUND) == 0) {
1476 }
1477}
1478
1480{
1481 /* Only valid if a snap point has been selected. */
1482 if (!(t->tsnap.status & SNAP_TARGET_FOUND)) {
1483 return;
1484 }
1485
1487 /* Previously Snap to Grid had its own snap source which was always the result of
1488 * #snap_source_median_fn. Now this mode shares the same code, so to not change the behavior
1489 * too much when using Closest, use the transform pivot as the snap source in this case. */
1492 /* Use #SCE_SNAP_TO_POINT to differentiate from 'Closest' bounds and thus avoid recalculating
1493 * the median center. */
1495 }
1496 }
1497 else {
1498 float dist_closest = 0.0f;
1499 TransData *closest = nullptr;
1500
1501 /* Object mode. */
1502 if (t->options & CTX_OBJECT) {
1504 tc->foreach_index_selected([&](const int i) {
1505 TransData *td = &tc->data[i];
1506
1507 std::optional<Bounds<float3>> bounds;
1508
1509 if ((t->options & CTX_OBMODE_XFORM_OBDATA) == 0) {
1510 Object *ob = static_cast<Object *>(td->extra);
1512 }
1513
1514 /* Use bound-box if possible. */
1515 if (bounds) {
1516 TransDataExtension *td_ext = &tc->data_ext[i];
1517 const std::array<float3, 8> bounds_corners = bounds::corners(*bounds);
1518 int j;
1519
1520 for (j = 0; j < 8; j++) {
1521 float loc[3];
1522 float dist;
1523
1524 copy_v3_v3(loc, bounds_corners[j]);
1525 mul_m4_v3(td_ext->obmat, loc);
1526
1527 dist = t->mode_info->snap_distance_fn(t, loc, t->tsnap.snap_target);
1528
1529 if ((dist != TRANSFORM_DIST_INVALID) &&
1530 (closest == nullptr || fabsf(dist) < fabsf(dist_closest)))
1531 {
1532 copy_v3_v3(t->tsnap.snap_source, loc);
1533 closest = td;
1534 dist_closest = dist;
1535 }
1536 }
1537 }
1538 /* Use element center otherwise. */
1539 else {
1540 float loc[3];
1541 float dist;
1542
1543 copy_v3_v3(loc, td->center);
1544
1545 dist = t->mode_info->snap_distance_fn(t, loc, t->tsnap.snap_target);
1546
1547 if ((dist != TRANSFORM_DIST_INVALID) &&
1548 (closest == nullptr || fabsf(dist) < fabsf(dist_closest)))
1549 {
1550 copy_v3_v3(t->tsnap.snap_source, loc);
1551 closest = td;
1552 }
1553 }
1554 });
1555 }
1556 }
1557 else {
1559 tc->foreach_index_selected([&](const int i) {
1560 TransData *td = &tc->data[i];
1561
1562 float loc[3];
1563 float dist;
1564
1565 copy_v3_v3(loc, td->center);
1566
1567 if (tc->use_local_mat) {
1568 mul_m4_v3(tc->mat, loc);
1569 }
1570
1571 dist = t->mode_info->snap_distance_fn(t, loc, t->tsnap.snap_target);
1572
1573 if ((dist != TRANSFORM_DIST_INVALID) &&
1574 (closest == nullptr || fabsf(dist) < fabsf(dist_closest)))
1575 {
1576 copy_v3_v3(t->tsnap.snap_source, loc);
1577 closest = td;
1578 dist_closest = dist;
1579 }
1580 });
1581 }
1582 }
1583 }
1584
1586}
1587
1589
1590/* -------------------------------------------------------------------- */
1593
1595 TransInfo *t, const float mval[2], float *dist_px, float r_loc[3], float r_no[3])
1596{
1597 SnapObjectParams snap_object_params{};
1598 snap_object_params.snap_target_select = t->tsnap.target_operation;
1599 snap_object_params.grid_size = (t->modifiers & MOD_PRECISION) ?
1601 t->snap_spatial[0];
1602 snap_object_params.edit_mode_type = (t->flag & T_EDIT) != 0 ? SNAP_GEOM_EDIT : SNAP_GEOM_FINAL;
1603 snap_object_params.occlusion_test = SNAP_OCCLUSION_AS_SEEM;
1604 snap_object_params.use_backface_culling = (t->tsnap.flag & SCE_SNAP_BACKFACE_CULLING) != 0;
1605
1606 float *prev_co = (t->tsnap.status & SNAP_SOURCE_FOUND) ? t->tsnap.snap_source : t->center_global;
1607 float *grid_co = nullptr, grid_co_stack[3];
1608 if ((t->tsnap.mode & SCE_SNAP_TO_GRID) && (t->con.mode & CON_APPLY) && t->mode != TFM_ROTATION) {
1609 /* Without this position adjustment, the snap may be far from the expected constraint point. */
1610 grid_co = grid_co_stack;
1611 convertViewVec(t, grid_co, mval[0] - t->center2d[0], mval[1] - t->center2d[1]);
1613 transform_constraint_get_nearest(t, grid_co, grid_co);
1614 add_v3_v3(grid_co, t->center_global);
1615 }
1616
1617 return ed::transform::snap_object_project_view3d(t->tsnap.object_context,
1618 t->depsgraph,
1619 t->region,
1620 static_cast<const View3D *>(t->view),
1621 t->tsnap.mode,
1622 &snap_object_params,
1623 grid_co,
1624 mval,
1625 prev_co,
1626 dist_px,
1627 r_loc,
1628 r_no);
1629}
1630
1632
1633/* -------------------------------------------------------------------- */
1636
1638 const float mval[2],
1639 const bool use_peel_object,
1640 /* Return args. */
1641 float r_loc[3],
1642 float r_no[3],
1643 float *r_thickness)
1644{
1645 SnapObjectParams snap_object_params{};
1646 snap_object_params.snap_target_select = t->tsnap.target_operation;
1647 snap_object_params.edit_mode_type = (t->flag & T_EDIT) != 0 ? SNAP_GEOM_EDIT : SNAP_GEOM_FINAL;
1648
1649 ListBaseT<SnapObjectHitDepth> depths_peel = {nullptr};
1651 t->depsgraph,
1652 t->region,
1653 static_cast<const View3D *>(t->view),
1654 &snap_object_params,
1655 mval,
1656 -1.0f,
1657 false,
1658 &depths_peel);
1659
1660 if (!depths_peel.is_empty()) {
1661 /* At the moment we only use the hits of the first object. */
1662 SnapObjectHitDepth *hit_min = depths_peel.first();
1663 for (SnapObjectHitDepth *iter = hit_min->next; iter; iter = iter->next) {
1664 if (iter->depth < hit_min->depth) {
1665 hit_min = iter;
1666 }
1667 }
1668 SnapObjectHitDepth *hit_max = nullptr;
1669
1670 if (use_peel_object) {
1671 /* If peeling objects, take the first and last from each object. */
1672 hit_max = hit_min;
1673 for (SnapObjectHitDepth &iter : depths_peel) {
1674 if ((iter.depth > hit_max->depth) && (iter.ob_uuid == hit_min->ob_uuid)) {
1675 hit_max = &iter;
1676 }
1677 }
1678 }
1679 else {
1680 /* Otherwise, pair first with second and so on. */
1681 for (SnapObjectHitDepth &iter : depths_peel) {
1682 if ((&iter != hit_min) && (iter.ob_uuid == hit_min->ob_uuid)) {
1683 if (hit_max == nullptr) {
1684 hit_max = &iter;
1685 }
1686 else if (iter.depth < hit_max->depth) {
1687 hit_max = &iter;
1688 }
1689 }
1690 }
1691 /* In this case has only one hit. treat as ray-cast. */
1692 if (hit_max == nullptr) {
1693 hit_max = hit_min;
1694 }
1695 }
1696
1697 mid_v3_v3v3(r_loc, hit_min->co, hit_max->co);
1698
1699 if (r_thickness) {
1700 *r_thickness = hit_max->depth - hit_min->depth;
1701 }
1702
1703 /* XXX, is there a correct normal in this case ???, for now just z up. */
1704 r_no[0] = 0.0;
1705 r_no[1] = 0.0;
1706 r_no[2] = 1.0;
1707
1708 for (SnapObjectHitDepth &link : depths_peel.items_mutable()) {
1709 MEM_delete(&link);
1710 }
1711 return true;
1712 }
1713 return false;
1714}
1715
1717
1718/* -------------------------------------------------------------------- */
1721
1722static void snap_increment_apply(const TransInfo *t, const float loc[3], float r_out[3])
1723{
1724 bool use_precision = (t->modifiers & MOD_PRECISION) != 0;
1725
1726 /* Relative snapping in fixed increments. */
1727 for (int i = 0; i <= t->idx_max; i++) {
1728 const float iter_fac = use_precision ? t->increment[i] * t->increment_precision :
1729 t->increment[i];
1730 if (iter_fac != 0.0f) {
1731 r_out[i] = iter_fac * roundf(loc[i] / iter_fac);
1732 }
1733 }
1734}
1735
1736bool transform_snap_increment_ex(const TransInfo *t, bool use_local_space, float *r_val)
1737{
1738 if (!transform_snap_is_active(t)) {
1739 return false;
1740 }
1741
1742 if (!(t->tsnap.mode & SCE_SNAP_TO_INCREMENT)) {
1743 return false;
1744 }
1745
1746 if (t->spacetype != SPACE_VIEW3D && validSnap(t)) {
1747 /* Only do something if using absolute or incremental grid snapping
1748 * and there is no valid snap point. */
1749 return false;
1750 }
1751
1752 if (use_local_space) {
1753 mul_m3_v3(t->spacemtx_inv, r_val);
1754 }
1755
1756 snap_increment_apply(t, r_val, r_val);
1757
1758 if (use_local_space) {
1759 mul_m3_v3(t->spacemtx, r_val);
1760 }
1761
1762 return true;
1763}
1764
1765bool transform_snap_increment(const TransInfo *t, float *r_val)
1766{
1767 return transform_snap_increment_ex(t, false, r_val);
1768}
1769
1771{
1773 {
1774 return (t->modifiers & MOD_PRECISION) ? t->increment[0] * t->increment_precision :
1775 t->increment[0];
1776 }
1777
1778 return 0.0f;
1779}
1780
1788
1790
1791/* -------------------------------------------------------------------- */
1794
1796 const float p1[3],
1797 const float p2[3])
1798{
1799 return len_squared_v3v3(p1, p2);
1800}
1801
1803
1804} // namespace blender::ed::transform
General operations, lookup, etc. for blender objects.
#define ATTR_FALLTHROUGH
Platform independent time functions.
#define SCE_SNAP_TO_GEOM
#define SCE_SNAP_TO_VERTEX
#define SI_GRID_STEPS_LEN
#define ED_transform_snap_object_time_average_print()
#define UNPACK2(a)
#define BLI_assert_unreachable()
#define SET_FLAG_FROM_TEST(value, test, flag)
#define ELEM(...)
bool closest(btVector3 &v)
#define roundf(x)
std::array< VecBase< T, 3 >, 8 > corners(const Bounds< VecBase< T, 3 > > &bounds)
TransConvertTypeInfo TransConvertType_Mesh
static void snap_source_active_fn(TransInfo *t)
void transform_snap_reset_from_mode(TransInfo *t, wmOperator *op)
void snap_object_context_destroy(SnapObjectContext *sctx)
static void snap_target_sequencer_fn(TransInfo *t, float *vec)
SnapObjectContext * snap_object_context_create()
void transform_snap_flag_from_modifiers_set(TransInfo *t)
bool peelObjectsTransform(TransInfo *t, const float mval[2], const bool use_peel_object, float r_loc[3], float r_no[3], float *r_thickness)
void getSnapPoint(const TransInfo *t, float vec[3])
void resetSnapping(TransInfo *t)
bool calculateCenterActive(TransInfo *t, bool select_only, float r_center[3])
static void snap_source_center_fn(TransInfo *t)
bool validSnappingNormal(const TransInfo *t)
static bool snap_use_backface_culling(const TransInfo *t)
static void snap_target_view3d_fn(TransInfo *t, float *vec)
static void snap_target_uv_fn(TransInfo *t, float *vec)
static bool transform_snap_mixed_is_active(const TransInfo *t)
bool transform_snap_increment(const TransInfo *t, float *r_val)
bool usingSnappingNormal(const TransInfo *t)
static void initSnappingMode(TransInfo *t)
void removeSnapPoint(TransInfo *t)
void transform_snap_mixed_apply(TransInfo *t, float *vec)
bool transformModeUseSnap(const TransInfo *t)
void initSnapping(TransInfo *t, wmOperator *op)
void tranform_snap_source_restore_context(TransInfo *t)
bool validSnap(const TransInfo *t)
void transform_snap_project_individual_apply(TransInfo *t)
float transform_snap_distance_len_squared_fn(TransInfo *, const float p1[3], const float p2[3])
static void snap_multipoints_free(TransInfo *t)
void convertViewVec(TransInfo *t, float r_vec[3], double dx, double dy)
static void applyFaceNearest(TransInfo *t, TransDataContainer *tc, TransData *td)
static eSnapMode snap_mode_from_spacetype(TransInfo *t)
static void snap_increment_apply(const TransInfo *t, const float loc[3], float r_out[3])
static bool doForceIncrementSnap(const TransInfo *t)
void snap_object_context_set_editmesh_target(SnapObjectContext *sctx, SnapEditMeshTarget target)
eRedrawFlag handleSnapping(TransInfo *t, const wmEvent *event)
bool transform_snap_project_individual_is_active(const TransInfo *t)
eRedrawFlag updateSelectedSnapPoint(TransInfo *t)
TransSeqSnapData * snap_sequencer_data_build(const TransInfo *t)
eSnapMode snap_object_project_view3d(SnapObjectContext *sctx, Depsgraph *depsgraph, const ARegion *region, const View3D *v3d, const eSnapMode snap_to, const SnapObjectParams *params, const float init_co[3], const float mval[2], const float prev_co[3], float *dist_px, float r_loc[3], float r_no[3])
static void snap_source_median_fn(TransInfo *t)
static void snap_grid_uv_apply(TransInfo *t, const float grid_dist[2], float r_out[2])
void transform_snap_grid_init(const TransInfo *t, float r_snap[3], float *r_snap_precision)
static eSnapMode snapObjectsTransform(TransInfo *t, const float mval[2], float *dist_px, float r_loc[3], float r_no[3])
void addSnapPoint(TransInfo *t)
void transform_constraint_get_nearest(const TransInfo *t, const float3 &vec, float r_vec[3])
void drawSnapping(TransInfo *t)
static void setSnappingCallback(TransInfo *t)
bool transform_snap_is_active(const TransInfo *t)
void tranform_snap_target_median_calc(const TransInfo *t, float r_median[3])
void initSnapAngleIncrements(TransInfo *t)
static bool applyFaceProject(TransInfo *t, TransDataContainer *tc, TransData *td, TransDataExtension *td_ext)
eSnapFlag * transform_snap_flag_from_spacetype_ptr(TransInfo *t, const PropertyRNA **r_prop=nullptr)
void transform_data_ext_rotate(TransData *td, TransDataExtension *td_ext, float mat[3][3], bool use_drot)
static void snap_target_nla_fn(TransInfo *t, float *vec)
void freeSnapping(TransInfo *t)
static eSnapFlag snap_flag_from_spacetype(TransInfo *t)
void snap_sequencer_data_free(TransSeqSnapData *data)
void constraintTransLim(const TransInfo *t, const TransDataContainer *tc, TransData *td)
static void snap_source_closest_fn(TransInfo *t)
static void snap_object_context_init(TransInfo *t)
bool transform_snap_increment_ex(const TransInfo *t, bool use_local_space, float *r_val)
bool transform_snap_nla_calc(TransInfo *t, float *vec)
static eSnapTargetOP snap_target_select_from_spacetype_and_tool_settings(TransInfo *t)
bool object_project_all_view3d_ex(SnapObjectContext *sctx, Depsgraph *depsgraph, const ARegion *region, const View3D *v3d, const SnapObjectParams *params, const float mval[2], float ray_depth, bool sort, ListBaseT< SnapObjectHitDepth > *r_hit_list)
static bool snap_grid_uv(TransInfo *t, float r_val[2])
float transform_snap_increment_get(const TransInfo *t)
short tool_settings_snap_flag_get(Scene *scene)
Definition sequencer.cc:421
short tool_settings_snap_mode_get(Scene *scene)
Definition sequencer.cc:415
float get_value_f(int colorid)
void get_color_3ubv(int colorid, unsigned char col[3])
void view2d_view_to_region_fl(const View2D *v2d, float x, float y, float *r_region_x, float *r_region_y) ATTR_NONNULL()
Definition view2d.cc:1720
float view2d_pixel_size_get_x(const View2D *v2d)
Definition view2d.cc:1920
UserDef U
Definition blender.cc:58
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
MINLINE float len_squared_v3v3(const float a[3], const float b[3]) ATTR_WARN_UNUSED_RESULT
void mul_m3_v3(const float M[3][3], float r[3])
@ V3D_PROJ_RET_OK
Definition ED_view3d.hh:273
void wmOrtho2_region_pixelspace(const ARegion *region)
MINLINE bool is_zero_v2(const float v[2]) ATTR_WARN_UNUSED_RESULT
Object * BKE_view_layer_active_object_get(const ViewLayer *view_layer)
void RNA_property_float_get_array(PointerRNA *ptr, PropertyRNA *prop, float *values)
void immEnd()
PropertyRNA * RNA_struct_find_property(PointerRNA *ptr, const char *identifier)
MINLINE void copy_v3_v3(float r[3], const float a[3])
unsigned int uint
Definition BKE_text.h:55
void immUniformColor4ub(unsigned char r, unsigned char g, unsigned char b, unsigned char a)
MINLINE void mul_v2_fl(float r[2], float f)
@ SCE_SNAP_SOURCE_CLOSEST
float ED_view3d_pixel_size(const RegionView3D *rv3d, const float co[3])
GPUVertFormat * immVertexFormat()
int RNA_property_enum_get(PointerRNA *ptr, PropertyRNA *prop)
void immBegin(GPUPrimType, uint vertex_len)
Base * BKE_view_layer_active_base_get(ViewLayer *view_layer)
MINLINE int max_ii(int a, int b)
ATTR_WARN_UNUSED_RESULT const BMVert * v
void imm_drawcircball(const float cent[3], float radius, const float tmat[4][4], uint pos)
@ V3D_SHADING_BACKFACE_CULLING
void immUnbindProgram()
MINLINE void mul_v3_fl(float r[3], float f)
@ GPU_BLEND_NONE
Definition GPU_state.hh:88
@ GPU_BLEND_ALPHA
Definition GPU_state.hh:90
bool RNA_property_boolean_get(PointerRNA *ptr, PropertyRNA *prop)
void immBindBuiltinProgram(GPUBuiltinShader shader_id)
void imm_draw_circle_wire_2d(uint shdr_pos, float x, float y, float radius, int nsegments)
void mul_m4_v3(const float M[4][4], float r[3])
@ SCE_SNAP_TARGET_NOT_ACTIVE
@ SCE_SNAP_TARGET_NOT_NONEDITED
@ SCE_SNAP_TARGET_ONLY_SELECTABLE
@ SCE_SNAP_TARGET_NOT_SELECTED
@ SCE_SNAP_TARGET_NOT_EDITED
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)
unsigned char uchar
Definition BKE_text.h:58
float ED_space_image_zoom_level(const View2D *v2d, int grid_dimension)
@ OB_MODE_PARTICLE_EDIT
void ED_view3d_cursor_snap_draw_util(RegionView3D *rv3d, const float source_loc[3], const float target_loc[3], const eSnapMode source_type, const eSnapMode target_type, const uchar source_color[4], const uchar target_color[4])
eV3DProjStatus ED_view3d_project_float_global(const ARegion *region, const float co[3], float r_co[2], eV3DProjTest flag)
@ SCE_SNAP_TO_EDGE_MIDPOINT
@ SCE_SNAP_TO_EDGE_PERPENDICULAR
@ SCE_SNAP_TO_FACE_MIDPOINT
@ SCE_SNAP_INDIVIDUAL_NEAREST
@ SCE_SNAP_INDIVIDUAL_PROJECT
@ SCE_SNAP_TO_EDGE_ENDPOINT
float ED_view3d_grid_view_scale(const Scene *scene, const View3D *v3d, const ARegion *region, const char **r_grid_unit)
void BLI_freelinkN(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:277
bool BKE_view_layer_synced_ensure(const Main &bmain, const Scene *scene, ViewLayer *view_layer)
void immUniformColor3ub(unsigned char r, unsigned char g, unsigned char b)
MINLINE void zero_v3(float r[3])
uint GPU_vertformat_attr_add(GPUVertFormat *format, StringRef name, gpu::VertAttrType type)
@ SEQ_SNAP_TO_ALL_CHANNEL_STRIPS
bool BKE_object_is_in_editmode(const Object *ob)
Definition object.cc:1898
MINLINE float normalize_v3(float n[3])
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])
void mid_v3_v3v3(float r[3], const float a[3], const float b[3])
void BLI_addtail(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:118
bool BKE_scene_uses_blender_workbench(const Scene *scene)
Definition scene.cc:3082
void GPU_matrix_pop_projection()
std::optional< Bounds< float3 > > BKE_object_boundbox_eval_cached_get(const Object *ob)
Definition object.cc:3758
@ V3D_PROJ_TEST_NOP
Definition ED_view3d.hh:295
@ SCE_SNAP_TRANSFORM_MODE_TRANSLATE
@ SCE_SNAP_TRANSFORM_MODE_SCALE
@ SCE_SNAP_TRANSFORM_MODE_ROTATE
MINLINE float square_f(float a)
double BLI_time_now_seconds()
Definition time.cc:30
void immVertex3f(uint attr_id, float x, float y, float z)
MINLINE void mul_v2_v2v2(float r[2], const float a[2], const float b[2])
float ED_space_image_increment_snap_value(int grid_dimensions, const float grid_steps[SI_GRID_STEPS_LEN], float zoom_factor)
@ SCE_SNAP_BACKFACE_CULLING
@ SCE_SNAP_TO_INCLUDE_EDITED
@ SCE_SNAP_KEEP_ON_SAME_OBJECT
@ SCE_SNAP_TO_INCLUDE_NONEDITED
@ SCE_SNAP_TO_ONLY_SELECTABLE
void GPU_blend(GPUBlend blend)
Definition gpu_state.cc:44
void copy_m4_m4(float m1[4][4], const float m2[4][4])
VecBase< float, 3 > float3
Vector< Object * > BKE_view_layer_array_from_objects_in_edit_mode_unique_data_with_uvs(const Main &bmain, const Scene *scene, ViewLayer *view_layer, const View3D *v3d)
MINLINE float len_squared_v2v2(const float a[2], const float b[2]) ATTR_WARN_UNUSED_RESULT
void immUniformColor4ubv(const unsigned char rgba[4])
void GPU_matrix_push_projection()
void GPU_depth_test(GPUDepthTest test)
Definition gpu_state.cc:70
void immRectf(uint pos, float x1, float y1, float x2, float y2)
MINLINE bool is_zero_v3(const float v[3]) ATTR_WARN_UNUSED_RESULT
bool RNA_property_is_set(PointerRNA *ptr, PropertyRNA *prop)
void rotation_between_vecs_to_mat3(float m[3][3], const float v1[3], const float v2[3])
void BKE_object_eval_transform_all(Depsgraph *depsgraph, Scene *scene, Object *object)
bool ED_uvedit_nearest_uv_multi(const View2D *v2d, const Scene *scene, Span< Object * > objects, const float mval_fl[2], const bool ignore_selected, float *dist_sq, float r_uv[2])
void RNA_float_get_array(PointerRNA *ptr, const char *name, float *values)
void ED_space_image_grid_steps(SpaceImage *sima, float grid_steps_x[SI_GRID_STEPS_LEN], float grid_steps_y[SI_GRID_STEPS_LEN], int grid_dimension)
#define fabsf
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
const NodeType * type
Definition graph/node.h:179
eRegion_Type regiontype
struct Object * object
struct SceneDisplay display
ListBaseT< SpaceLink > spacedata
eGraphEdit_Mode mode
eSpaceImage_Mode mode
eSnapTransformMode snap_transform_mode_flag
eView3DShading_Flag flag
View3DShading shading
TransConvertTypeInfo * data_type
Definition transform.hh:836
float(*) snap_distance_fn(TransInfo *t, const float p1[3], const float p2[3])
void(*) snap_apply_fn(TransInfo *, float *)
ListBaseT< TransSnapPoint > points
Definition transform.hh:576
void(*) snap_source_fn(TransInfo *)
Definition transform.hh:580
void(*) snap_target_fn(TransInfo *, float *)
Definition transform.hh:579
wmEventType type
Definition WM_types.hh:817
wmEventModifierFlag modifier
Definition WM_types.hh:834
#define TRANSFORM_SNAP_MAX_PX
Definition transform.hh:40
#define TRANSFORM_DIST_INVALID
Definition transform.hh:41
#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.
#define SNAP_MIN_DISTANCE