2 * ***** BEGIN GPL LICENSE BLOCK *****
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19 * All rights reserved.
21 * The Original Code is: all of this file.
23 * Contributor(s): Martin Poirier
25 * ***** END GPL LICENSE BLOCK *****
28 /** \file blender/editors/transform/transform_snap.c
29 * \ingroup edtransform
39 #include "DNA_scene_types.h"
40 #include "DNA_object_types.h"
41 #include "DNA_meshdata_types.h" /* Temporary, for snapping to other unselected meshes */
42 #include "DNA_node_types.h"
43 #include "DNA_space_types.h"
44 #include "DNA_screen_types.h"
45 #include "DNA_view3d_types.h"
46 #include "DNA_windowmanager_types.h"
49 #include "BLI_blenlib.h"
50 #include "BLI_utildefines.h"
52 #include "GPU_immediate.h"
54 #include "BKE_DerivedMesh.h"
55 #include "BKE_global.h"
56 #include "BKE_object.h"
57 #include "BKE_anim.h" /* for duplis */
58 #include "BKE_context.h"
59 #include "BKE_editmesh.h"
60 #include "BKE_sequencer.h"
63 #include "RNA_access.h"
69 #include "ED_uvedit.h"
70 #include "ED_view3d.h"
71 #include "ED_transform_snap_object_context.h"
73 #include "UI_resources.h"
74 #include "UI_view2d.h"
76 #include "MEM_guardedalloc.h"
78 #include "transform.h"
80 /* this should be passed as an arg for use in snap functions */
83 /* use half of flt-max so we can scale up without an exception */
85 /********************* PROTOTYPES ***********************/
87 static void setSnappingCallback(TransInfo *t);
89 static void ApplySnapTranslation(TransInfo *t, float vec[3]);
90 static void ApplySnapRotation(TransInfo *t, float *vec);
91 static void ApplySnapResize(TransInfo *t, float vec[2]);
93 /* static void CalcSnapGrid(TransInfo *t, float *vec); */
94 static void CalcSnapGeometry(TransInfo *t, float *vec);
96 static void TargetSnapMedian(TransInfo *t);
97 static void TargetSnapCenter(TransInfo *t);
98 static void TargetSnapClosest(TransInfo *t);
99 static void TargetSnapActive(TransInfo *t);
101 static float RotationBetween(TransInfo *t, const float p1[3], const float p2[3]);
102 static float TranslationBetween(TransInfo *t, const float p1[3], const float p2[3]);
103 static float ResizeBetween(TransInfo *t, const float p1[3], const float p2[3]);
106 /****************** IMPLEMENTATIONS *********************/
108 static bool snapNodeTest(View2D *v2d, bNode *node, SnapSelect snap_select);
109 static NodeBorder snapNodeBorder(int snap_node_mode);
112 int BIF_snappingSupported(Object *obedit)
116 /* only support object mesh, armature, curves */
117 if (obedit == NULL || ELEM(obedit->type, OB_MESH, OB_ARMATURE, OB_CURVE, OB_LATTICE, OB_MBALL)) {
125 bool validSnap(TransInfo *t)
127 return (t->tsnap.status & (POINT_INIT | TARGET_INIT)) == (POINT_INIT | TARGET_INIT) ||
128 (t->tsnap.status & (MULTI_POINTS | TARGET_INIT)) == (MULTI_POINTS | TARGET_INIT);
131 bool activeSnap(TransInfo *t)
133 return ((t->modifiers & (MOD_SNAP | MOD_SNAP_INVERT)) == MOD_SNAP) ||
134 ((t->modifiers & (MOD_SNAP | MOD_SNAP_INVERT)) == MOD_SNAP_INVERT);
137 void drawSnapping(const struct bContext *C, TransInfo *t)
139 unsigned char col[4], selectedCol[4], activeCol[4];
144 UI_GetThemeColor3ubv(TH_TRANSFORM, col);
147 UI_GetThemeColor3ubv(TH_SELECT, selectedCol);
148 selectedCol[3] = 128;
150 UI_GetThemeColor3ubv(TH_ACTIVE, activeCol);
153 if (t->spacetype == SPACE_VIEW3D) {
156 View3D *v3d = CTX_wm_view3d(C);
157 RegionView3D *rv3d = CTX_wm_region_view3d(C);
161 glDisable(GL_DEPTH_TEST);
163 size = 2.5f * UI_GetThemeValuef(TH_VERTEX_SIZE);
165 invert_m4_m4(imat, rv3d->viewmat);
167 unsigned int pos = GWN_vertformat_attr_add(immVertexFormat(), "pos", GWN_COMP_F32, 3, GWN_FETCH_FLOAT);
169 immBindBuiltinProgram(GPU_SHADER_3D_UNIFORM_COLOR);
171 for (p = t->tsnap.points.first; p; p = p->next) {
172 if (p == t->tsnap.selectedPoint) {
173 immUniformColor4ubv(selectedCol);
176 immUniformColor4ubv(col);
179 imm_drawcircball(p->co, ED_view3d_pixel_size(rv3d, p->co) * size * 0.75f, imat, pos);
182 if (t->tsnap.status & POINT_INIT) {
183 immUniformColor4ubv(activeCol);
185 imm_drawcircball(t->tsnap.snapPoint, ED_view3d_pixel_size(rv3d, t->tsnap.snapPoint) * size, imat, pos);
188 /* draw normal if needed */
189 if (usingSnappingNormal(t) && validSnappingNormal(t)) {
190 immUniformColor4ubv(activeCol);
192 immBegin(GWN_PRIM_LINES, 2);
193 immVertex3f(pos, t->tsnap.snapPoint[0], t->tsnap.snapPoint[1], t->tsnap.snapPoint[2]);
194 immVertex3f(pos, t->tsnap.snapPoint[0] + t->tsnap.snapNormal[0],
195 t->tsnap.snapPoint[1] + t->tsnap.snapNormal[1],
196 t->tsnap.snapPoint[2] + t->tsnap.snapNormal[2]);
203 glEnable(GL_DEPTH_TEST);
206 else if (t->spacetype == SPACE_IMAGE) {
208 /* This will not draw, and Im nor sure why - campbell */
209 /* TODO: see 2.7x for non-working code */
212 else if (t->spacetype == SPACE_NODE) {
214 ARegion *ar = CTX_wm_region(C);
218 size = 2.5f * UI_GetThemeValuef(TH_VERTEX_SIZE);
222 unsigned int pos = GWN_vertformat_attr_add(immVertexFormat(), "pos", GWN_COMP_F32, 2, GWN_FETCH_FLOAT);
224 immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR);
226 for (p = t->tsnap.points.first; p; p = p->next) {
227 if (p == t->tsnap.selectedPoint) {
228 immUniformColor4ubv(selectedCol);
231 immUniformColor4ubv(col);
234 ED_node_draw_snap(&ar->v2d, p->co, size, 0, pos);
237 if (t->tsnap.status & POINT_INIT) {
238 immUniformColor4ubv(activeCol);
240 ED_node_draw_snap(&ar->v2d, t->tsnap.snapPoint, size, t->tsnap.snapNodeBorder, pos);
250 eRedrawFlag handleSnapping(TransInfo *t, const wmEvent *event)
252 eRedrawFlag status = TREDRAW_NOTHING;
254 #if 0 // XXX need a proper selector for all snap mode
255 if (BIF_snappingSupported(t->obedit) && event->type == TABKEY && event->shift) {
256 /* toggle snap and reinit */
257 t->settings->snap_flag ^= SCE_SNAP;
258 initSnapping(t, NULL);
259 status = TREDRAW_HARD;
262 if (event->type == MOUSEMOVE) {
263 status |= updateSelectedSnapPoint(t);
269 void applyProject(TransInfo *t)
271 /* XXX FLICKER IN OBJECT MODE */
272 if ((t->tsnap.project) && activeSnap(t) && (t->flag & T_NO_PROJECT) == 0) {
273 TransData *td = t->data;
278 if (t->flag & (T_EDIT | T_POSE)) {
279 Object *ob = t->obedit ? t->obedit : t->poseobj;
280 invert_m4_m4(imat, ob->obmat);
283 for (i = 0; i < t->total; i++, td++) {
284 float iloc[3], loc[3], no[3];
286 float dist_px = TRANSFORM_DIST_MAX_PX;
288 if (td->flag & TD_NOACTION)
291 if (td->flag & TD_SKIP)
294 if ((t->flag & T_PROP_EDIT) && (td->factor == 0.0f))
297 copy_v3_v3(iloc, td->loc);
298 if (t->flag & (T_EDIT | T_POSE)) {
299 Object *ob = t->obedit ? t->obedit : t->poseobj;
300 mul_m4_v3(ob->obmat, iloc);
302 else if (t->flag & T_OBJECT) {
303 /* TODO(sergey): Ideally force update is not needed here. */
304 td->ob->recalc |= OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME;
305 BKE_object_handle_update(G.main->eval_ctx, t->scene, td->ob);
306 copy_v3_v3(iloc, td->ob->obmat[3]);
309 if (ED_view3d_project_float_global(t->ar, iloc, mval_fl, V3D_PROJ_TEST_NOP) == V3D_PROJ_RET_OK) {
310 if (snapObjectsTransform(
311 t, mval_fl, &dist_px,
314 // if (t->flag & (T_EDIT|T_POSE)) {
315 // mul_m4_v3(imat, loc);
318 sub_v3_v3v3(tvec, loc, iloc);
320 mul_m3_v3(td->smtx, tvec);
322 add_v3_v3(td->loc, tvec);
324 if (t->tsnap.align && (t->flag & T_OBJECT)) {
325 /* handle alignment as well */
326 const float *original_normal;
329 /* In pose mode, we want to align normals with Y axis of bones... */
330 original_normal = td->axismtx[2];
332 rotation_between_vecs_to_mat3(mat, original_normal, no);
334 transform_data_ext_rotate(td, mat, true);
336 /* TODO support constraints for rotation too? see ElementRotation */
341 //XXX constraintTransLim(t, td);
346 void applyGridAbsolute(TransInfo *t)
348 float grid_size = 0.0f;
349 GearsType grid_action;
351 float (*obmat)[4] = NULL;
352 bool use_obmat = false;
355 if (!(activeSnap(t) && (ELEM(t->tsnap.mode, SCE_SNAP_MODE_INCREMENT, SCE_SNAP_MODE_GRID))))
358 grid_action = BIG_GEARS;
359 if (t->modifiers & MOD_PRECISION)
360 grid_action = SMALL_GEARS;
362 switch (grid_action) {
363 case NO_GEARS: grid_size = t->snap_spatial[0]; break;
364 case BIG_GEARS: grid_size = t->snap_spatial[1]; break;
365 case SMALL_GEARS: grid_size = t->snap_spatial[2]; break;
367 /* early exit on unusable grid size */
368 if (grid_size == 0.0f)
371 if (t->flag & (T_EDIT | T_POSE)) {
372 Object *ob = t->obedit ? t->obedit : t->poseobj;
377 for (i = 0, td = t->data; i < t->total; i++, td++) {
378 float iloc[3], loc[3], tvec[3];
380 if (td->flag & TD_NOACTION)
383 if (td->flag & TD_SKIP)
386 if ((t->flag & T_PROP_EDIT) && (td->factor == 0.0f))
389 copy_v3_v3(iloc, td->loc);
391 mul_m4_v3(obmat, iloc);
393 else if (t->flag & T_OBJECT) {
394 td->ob->recalc |= OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME;
395 BKE_object_handle_update(G.main->eval_ctx, t->scene, td->ob);
396 copy_v3_v3(iloc, td->ob->obmat[3]);
399 mul_v3_v3fl(loc, iloc, 1.0f / grid_size);
400 loc[0] = roundf(loc[0]);
401 loc[1] = roundf(loc[1]);
402 loc[2] = roundf(loc[2]);
403 mul_v3_fl(loc, grid_size);
405 sub_v3_v3v3(tvec, loc, iloc);
406 mul_m3_v3(td->smtx, tvec);
407 add_v3_v3(td->loc, tvec);
411 void applySnapping(TransInfo *t, float *vec)
413 /* project is not applied this way */
414 if (t->tsnap.project)
417 if (t->tsnap.status & SNAP_FORCED) {
418 t->tsnap.targetSnap(t);
420 t->tsnap.applySnap(t, vec);
422 else if (!ELEM(t->tsnap.mode, SCE_SNAP_MODE_INCREMENT, SCE_SNAP_MODE_GRID) && activeSnap(t)) {
423 double current = PIL_check_seconds_timer();
425 // Time base quirky code to go around findnearest slowness
426 /* !TODO! add exception for object mode, no need to slow it down then */
427 if (current - t->tsnap.last >= 0.01) {
428 t->tsnap.calcSnap(t, vec);
429 t->tsnap.targetSnap(t);
431 t->tsnap.last = current;
434 t->tsnap.applySnap(t, vec);
439 void resetSnapping(TransInfo *t)
442 t->tsnap.align = false;
443 t->tsnap.project = 0;
445 t->tsnap.modeSelect = 0;
448 t->tsnap.applySnap = NULL;
450 t->tsnap.snapNormal[0] = 0;
451 t->tsnap.snapNormal[1] = 0;
452 t->tsnap.snapNormal[2] = 0;
454 t->tsnap.snapNodeBorder = 0;
457 bool usingSnappingNormal(TransInfo *t)
459 return t->tsnap.align;
462 bool validSnappingNormal(TransInfo *t)
465 if (!is_zero_v3(t->tsnap.snapNormal)) {
473 static bool bm_edge_is_snap_target(BMEdge *e, void *UNUSED(user_data))
475 if (BM_elem_flag_test(e, BM_ELEM_SELECT | BM_ELEM_HIDDEN) ||
476 BM_elem_flag_test(e->v1, BM_ELEM_SELECT) ||
477 BM_elem_flag_test(e->v2, BM_ELEM_SELECT))
485 static bool bm_face_is_snap_target(BMFace *f, void *UNUSED(user_data))
487 if (BM_elem_flag_test(f, BM_ELEM_SELECT | BM_ELEM_HIDDEN)) {
491 BMLoop *l_iter, *l_first;
492 l_iter = l_first = BM_FACE_FIRST_LOOP(f);
494 if (BM_elem_flag_test(l_iter->v, BM_ELEM_SELECT)) {
497 } while ((l_iter = l_iter->next) != l_first);
502 static void initSnappingMode(TransInfo *t)
504 ToolSettings *ts = t->settings;
505 Object *obedit = t->obedit;
506 SceneLayer *sl = t->scene_layer;
507 Base *base_act = sl->basact;
509 if (t->spacetype == SPACE_NODE) {
510 /* force project off when not supported */
511 t->tsnap.project = 0;
513 t->tsnap.mode = ts->snap_node_mode;
515 else if (t->spacetype == SPACE_IMAGE) {
516 /* force project off when not supported */
517 t->tsnap.project = 0;
519 t->tsnap.mode = ts->snap_uv_mode;
522 /* force project off when not supported */
523 if (ts->snap_mode != SCE_SNAP_MODE_FACE)
524 t->tsnap.project = 0;
526 t->tsnap.mode = ts->snap_mode;
529 if ((t->spacetype == SPACE_VIEW3D || t->spacetype == SPACE_IMAGE) && /* Only 3D view or UV */
530 (t->flag & T_CAMERA) == 0) /* Not with camera selected in camera view */
532 setSnappingCallback(t);
535 if (t->tsnap.applySnap != NULL && // A snapping function actually exist
536 (obedit != NULL && ELEM(obedit->type, OB_MESH, OB_ARMATURE, OB_CURVE, OB_LATTICE, OB_MBALL)) ) // Temporary limited to edit mode meshes, armature, curves, mballs
538 /* Exclude editmesh if using proportional edit */
539 if ((obedit->type == OB_MESH) && (t->flag & T_PROP_EDIT)) {
540 t->tsnap.modeSelect = SNAP_NOT_ACTIVE;
543 t->tsnap.modeSelect = t->tsnap.snap_self ? SNAP_ALL : SNAP_NOT_ACTIVE;
546 /* Particles edit mode*/
547 else if (t->tsnap.applySnap != NULL && // A snapping function actually exist
548 (obedit == NULL && base_act && base_act->object && base_act->object->mode & OB_MODE_PARTICLE_EDIT))
550 t->tsnap.modeSelect = SNAP_ALL;
553 else if (t->tsnap.applySnap != NULL && // A snapping function actually exist
554 (obedit == NULL) ) // Object Mode
556 /* In "Edit Strokes" mode, Snap tool can perform snap to selected or active objects (see T49632)
557 * TODO: perform self snap in gpencil_strokes */
558 t->tsnap.modeSelect = ((t->options & CTX_GPENCIL_STROKES) != 0) ? SNAP_ALL : SNAP_NOT_SELECTED;
561 /* Grid if snap is not possible */
562 t->tsnap.mode = SCE_SNAP_MODE_INCREMENT;
565 else if (t->spacetype == SPACE_NODE) {
566 setSnappingCallback(t);
568 if (t->tsnap.applySnap != NULL) {
569 t->tsnap.modeSelect = SNAP_NOT_SELECTED;
572 /* Grid if snap is not possible */
573 t->tsnap.mode = SCE_SNAP_MODE_INCREMENT;
576 else if (t->spacetype == SPACE_SEQ) {
577 /* We do our own snapping currently, so nothing here */
578 t->tsnap.mode = SCE_SNAP_MODE_GRID; /* Dummy, should we rather add a NOP mode? */
581 /* Always grid outside of 3D view */
582 t->tsnap.mode = SCE_SNAP_MODE_INCREMENT;
585 if (t->spacetype == SPACE_VIEW3D) {
586 if (t->tsnap.object_context == NULL) {
587 t->tsnap.object_context = ED_transform_snap_object_context_create_view3d(
588 G.main, t->scene, t->scene_layer, 0,
591 ED_transform_snap_object_context_set_editmesh_callbacks(
592 t->tsnap.object_context,
593 (bool (*)(BMVert *, void *))BM_elem_cb_check_hflag_disabled,
594 bm_edge_is_snap_target,
595 bm_face_is_snap_target,
596 SET_UINT_IN_POINTER((BM_ELEM_SELECT | BM_ELEM_HIDDEN)));
601 void initSnapping(TransInfo *t, wmOperator *op)
603 ToolSettings *ts = t->settings;
604 short snap_target = t->settings->snap_target;
608 /* if snap property exists */
609 if (op && RNA_struct_find_property(op->ptr, "snap") && RNA_struct_property_is_set(op->ptr, "snap")) {
610 if (RNA_boolean_get(op->ptr, "snap")) {
611 t->modifiers |= MOD_SNAP;
613 if (RNA_struct_property_is_set(op->ptr, "snap_target")) {
614 snap_target = RNA_enum_get(op->ptr, "snap_target");
617 if (RNA_struct_property_is_set(op->ptr, "snap_point")) {
618 RNA_float_get_array(op->ptr, "snap_point", t->tsnap.snapPoint);
619 t->tsnap.status |= SNAP_FORCED | POINT_INIT;
622 /* snap align only defined in specific cases */
623 if (RNA_struct_find_property(op->ptr, "snap_align")) {
624 t->tsnap.align = RNA_boolean_get(op->ptr, "snap_align");
625 RNA_float_get_array(op->ptr, "snap_normal", t->tsnap.snapNormal);
626 normalize_v3(t->tsnap.snapNormal);
629 if (RNA_struct_find_property(op->ptr, "use_snap_project")) {
630 t->tsnap.project = RNA_boolean_get(op->ptr, "use_snap_project");
633 if (RNA_struct_find_property(op->ptr, "use_snap_self")) {
634 t->tsnap.snap_self = RNA_boolean_get(op->ptr, "use_snap_self");
638 /* use scene defaults only when transform is modal */
639 else if (t->flag & T_MODAL) {
640 if (ELEM(t->spacetype, SPACE_VIEW3D, SPACE_IMAGE, SPACE_NODE)) {
641 if (ts->snap_flag & SCE_SNAP) {
642 t->modifiers |= MOD_SNAP;
645 t->tsnap.align = ((t->settings->snap_flag & SCE_SNAP_ROTATE) != 0);
646 t->tsnap.project = ((t->settings->snap_flag & SCE_SNAP_PROJECT) != 0);
647 t->tsnap.snap_self = !((t->settings->snap_flag & SCE_SNAP_NO_SELF) != 0);
648 t->tsnap.peel = ((t->settings->snap_flag & SCE_SNAP_PROJECT) != 0);
651 /* for now only 3d view (others can be added if we want) */
652 if (t->spacetype == SPACE_VIEW3D) {
653 t->tsnap.snap_spatial_grid = ((t->settings->snap_flag & SCE_SNAP_ABS_GRID) != 0);
657 t->tsnap.target = snap_target;
662 void freeSnapping(TransInfo *t)
664 if (t->tsnap.object_context) {
665 ED_transform_snap_object_context_destroy(t->tsnap.object_context);
666 t->tsnap.object_context = NULL;
670 static void setSnappingCallback(TransInfo *t)
672 t->tsnap.calcSnap = CalcSnapGeometry;
674 switch (t->tsnap.target) {
675 case SCE_SNAP_TARGET_CLOSEST:
676 t->tsnap.targetSnap = TargetSnapClosest;
678 case SCE_SNAP_TARGET_CENTER:
679 t->tsnap.targetSnap = TargetSnapCenter;
681 case SCE_SNAP_TARGET_MEDIAN:
682 t->tsnap.targetSnap = TargetSnapMedian;
684 case SCE_SNAP_TARGET_ACTIVE:
685 t->tsnap.targetSnap = TargetSnapActive;
691 case TFM_TRANSLATION:
692 t->tsnap.applySnap = ApplySnapTranslation;
693 t->tsnap.distance = TranslationBetween;
696 t->tsnap.applySnap = ApplySnapRotation;
697 t->tsnap.distance = RotationBetween;
699 // Can't do TARGET_CENTER with rotation, use TARGET_MEDIAN instead
700 if (t->tsnap.target == SCE_SNAP_TARGET_CENTER) {
701 t->tsnap.target = SCE_SNAP_TARGET_MEDIAN;
702 t->tsnap.targetSnap = TargetSnapMedian;
706 t->tsnap.applySnap = ApplySnapResize;
707 t->tsnap.distance = ResizeBetween;
709 // Can't do TARGET_CENTER with resize, use TARGET_MEDIAN instead
710 if (t->tsnap.target == SCE_SNAP_TARGET_CENTER) {
711 t->tsnap.target = SCE_SNAP_TARGET_MEDIAN;
712 t->tsnap.targetSnap = TargetSnapMedian;
716 t->tsnap.applySnap = NULL;
721 void addSnapPoint(TransInfo *t)
723 /* Currently only 3D viewport works for snapping points. */
724 if (t->tsnap.status & POINT_INIT && t->spacetype == SPACE_VIEW3D) {
725 TransSnapPoint *p = MEM_callocN(sizeof(TransSnapPoint), "SnapPoint");
727 t->tsnap.selectedPoint = p;
729 copy_v3_v3(p->co, t->tsnap.snapPoint);
731 BLI_addtail(&t->tsnap.points, p);
733 t->tsnap.status |= MULTI_POINTS;
737 eRedrawFlag updateSelectedSnapPoint(TransInfo *t)
739 eRedrawFlag status = TREDRAW_NOTHING;
741 if (t->tsnap.status & MULTI_POINTS) {
742 TransSnapPoint *p, *closest_p = NULL;
743 float dist_min_sq = TRANSFORM_SNAP_MAX_PX;
744 const float mval_fl[2] = {t->mval[0], t->mval[1]};
747 for (p = t->tsnap.points.first; p; p = p->next) {
750 if (ED_view3d_project_float_global(t->ar, p->co, screen_loc, V3D_PROJ_TEST_NOP) != V3D_PROJ_RET_OK) {
754 dist_sq = len_squared_v2v2(mval_fl, screen_loc);
756 if (dist_sq < dist_min_sq) {
758 dist_min_sq = dist_sq;
763 if (t->tsnap.selectedPoint != closest_p) {
764 status = TREDRAW_HARD;
767 t->tsnap.selectedPoint = closest_p;
774 void removeSnapPoint(TransInfo *t)
776 if (t->tsnap.status & MULTI_POINTS) {
777 updateSelectedSnapPoint(t);
779 if (t->tsnap.selectedPoint) {
780 BLI_freelinkN(&t->tsnap.points, t->tsnap.selectedPoint);
782 if (BLI_listbase_is_empty(&t->tsnap.points)) {
783 t->tsnap.status &= ~MULTI_POINTS;
786 t->tsnap.selectedPoint = NULL;
792 void getSnapPoint(TransInfo *t, float vec[3])
794 if (t->tsnap.points.first) {
798 vec[0] = vec[1] = vec[2] = 0;
800 for (p = t->tsnap.points.first; p; p = p->next, total++) {
801 add_v3_v3(vec, p->co);
804 if (t->tsnap.status & POINT_INIT) {
805 add_v3_v3(vec, t->tsnap.snapPoint);
809 mul_v3_fl(vec, 1.0f / total);
812 copy_v3_v3(vec, t->tsnap.snapPoint);
816 /********************** APPLY **************************/
818 static void ApplySnapTranslation(TransInfo *t, float vec[3])
821 getSnapPoint(t, point);
823 if (t->spacetype == SPACE_NODE) {
824 char border = t->tsnap.snapNodeBorder;
825 if (border & (NODE_LEFT | NODE_RIGHT))
826 vec[0] = point[0] - t->tsnap.snapTarget[0];
827 if (border & (NODE_BOTTOM | NODE_TOP))
828 vec[1] = point[1] - t->tsnap.snapTarget[1];
831 if (t->spacetype == SPACE_VIEW3D) {
832 if (t->options & CTX_PAINT_CURVE) {
833 if (ED_view3d_project_float_global(t->ar, point, point, V3D_PROJ_TEST_NOP) != V3D_PROJ_RET_OK) {
834 zero_v3(point); /* no good answer here... */
839 sub_v3_v3v3(vec, point, t->tsnap.snapTarget);
843 static void ApplySnapRotation(TransInfo *t, float *value)
846 getSnapPoint(t, point);
848 float dist = RotationBetween(t, t->tsnap.snapTarget, point);
852 static void ApplySnapResize(TransInfo *t, float vec[3])
855 getSnapPoint(t, point);
857 float dist = ResizeBetween(t, t->tsnap.snapTarget, point);
858 copy_v3_fl(vec, dist);
861 /********************** DISTANCE **************************/
863 static float TranslationBetween(TransInfo *UNUSED(t), const float p1[3], const float p2[3])
865 return len_squared_v3v3(p1, p2);
868 static float RotationBetween(TransInfo *t, const float p1[3], const float p2[3])
870 float angle, start[3], end[3];
872 sub_v3_v3v3(start, p1, t->center_global);
873 sub_v3_v3v3(end, p2, t->center_global);
875 // Angle around a constraint axis (error prone, will need debug)
876 if (t->con.applyRot != NULL && (t->con.mode & CON_APPLY)) {
877 float axis[3], tmp[3];
879 t->con.applyRot(t, NULL, axis, NULL);
881 project_v3_v3v3(tmp, end, axis);
882 sub_v3_v3v3(end, end, tmp);
884 project_v3_v3v3(tmp, start, axis);
885 sub_v3_v3v3(start, start, tmp);
890 cross_v3_v3v3(tmp, start, end);
892 if (dot_v3v3(tmp, axis) < 0.0f)
893 angle = -acosf(dot_v3v3(start, end));
895 angle = acosf(dot_v3v3(start, end));
900 copy_m3_m4(mtx, t->viewmat);
903 mul_m3_v3(mtx, start);
905 angle = atan2f(start[1], start[0]) - atan2f(end[1], end[0]);
908 if (angle > (float)M_PI) {
909 angle = angle - 2 * (float)M_PI;
911 else if (angle < -((float)M_PI)) {
912 angle = 2.0f * (float)M_PI + angle;
918 static float ResizeBetween(TransInfo *t, const float p1[3], const float p2[3])
920 float d1[3], d2[3], len_d1;
922 sub_v3_v3v3(d1, p1, t->center_global);
923 sub_v3_v3v3(d2, p2, t->center_global);
925 if (t->con.applyRot != NULL && (t->con.mode & CON_APPLY)) {
926 mul_m3_v3(t->con.pmtx, d1);
927 mul_m3_v3(t->con.pmtx, d2);
930 project_v3_v3v3(d1, d1, d2);
934 /* Use 'invalid' dist when `center == p1` (after projecting),
935 * in this case scale will _never_ move the point in relation to the center,
936 * so it makes no sense to take it into account when scaling. see: T46503 */
937 return len_d1 != 0.0f ? len_v3(d2) / len_d1 : TRANSFORM_DIST_INVALID;
940 /********************** CALC **************************/
942 static void UNUSED_FUNCTION(CalcSnapGrid) (TransInfo *t, float *UNUSED(vec))
944 snapGridIncrementAction(t, t->tsnap.snapPoint, BIG_GEARS);
947 static void CalcSnapGeometry(TransInfo *t, float *UNUSED(vec))
949 if (t->spacetype == SPACE_VIEW3D) {
954 float dist_px = SNAP_MIN_DISTANCE; // Use a user defined value here
956 mval[0] = t->mval[0];
957 mval[1] = t->mval[1];
959 if (t->tsnap.mode == SCE_SNAP_MODE_VOLUME) {
960 found = peelObjectsTransform(
962 (t->settings->snap_flag & SCE_SNAP_PEEL_OBJECT) != 0,
966 zero_v3(no); /* objects won't set this */
967 found = snapObjectsTransform(
973 copy_v3_v3(t->tsnap.snapPoint, loc);
974 copy_v3_v3(t->tsnap.snapNormal, no);
976 t->tsnap.status |= POINT_INIT;
979 t->tsnap.status &= ~POINT_INIT;
982 else if (t->spacetype == SPACE_IMAGE && t->obedit != NULL && t->obedit->type == OB_MESH) {
983 /* same as above but for UV's */
984 Image *ima = ED_space_image(t->sa->spacedata.first);
987 UI_view2d_region_to_view(&t->ar->v2d, t->mval[0], t->mval[1], &co[0], &co[1]);
989 if (ED_uvedit_nearest_uv(t->scene, t->obedit, ima, co, t->tsnap.snapPoint)) {
990 t->tsnap.snapPoint[0] *= t->aspect[0];
991 t->tsnap.snapPoint[1] *= t->aspect[1];
993 t->tsnap.status |= POINT_INIT;
996 t->tsnap.status &= ~POINT_INIT;
999 else if (t->spacetype == SPACE_NODE) {
1001 float dist_px = SNAP_MIN_DISTANCE; // Use a user defined value here
1004 if (snapNodesTransform(t, t->mval, t->tsnap.modeSelect, loc, &dist_px, &node_border)) {
1005 copy_v2_v2(t->tsnap.snapPoint, loc);
1006 t->tsnap.snapNodeBorder = node_border;
1008 t->tsnap.status |= POINT_INIT;
1011 t->tsnap.status &= ~POINT_INIT;
1016 /********************** TARGET **************************/
1018 static void TargetSnapOffset(TransInfo *t, TransData *td)
1020 if (t->spacetype == SPACE_NODE && td != NULL) {
1021 bNode *node = td->extra;
1022 char border = t->tsnap.snapNodeBorder;
1023 float width = BLI_rctf_size_x(&node->totr);
1024 float height = BLI_rctf_size_y(&node->totr);
1026 #ifdef USE_NODE_CENTER
1027 if (border & NODE_LEFT)
1028 t->tsnap.snapTarget[0] -= 0.5f * width;
1029 if (border & NODE_RIGHT)
1030 t->tsnap.snapTarget[0] += 0.5f * width;
1031 if (border & NODE_BOTTOM)
1032 t->tsnap.snapTarget[1] -= 0.5f * height;
1033 if (border & NODE_TOP)
1034 t->tsnap.snapTarget[1] += 0.5f * height;
1036 if (border & NODE_LEFT)
1037 t->tsnap.snapTarget[0] -= 0.0f;
1038 if (border & NODE_RIGHT)
1039 t->tsnap.snapTarget[0] += width;
1040 if (border & NODE_BOTTOM)
1041 t->tsnap.snapTarget[1] -= height;
1042 if (border & NODE_TOP)
1043 t->tsnap.snapTarget[1] += 0.0f;
1048 static void TargetSnapCenter(TransInfo *t)
1050 /* Only need to calculate once */
1051 if ((t->tsnap.status & TARGET_INIT) == 0) {
1052 copy_v3_v3(t->tsnap.snapTarget, t->center_global);
1053 TargetSnapOffset(t, NULL);
1055 t->tsnap.status |= TARGET_INIT;
1059 static void TargetSnapActive(TransInfo *t)
1061 /* Only need to calculate once */
1062 if ((t->tsnap.status & TARGET_INIT) == 0) {
1063 if (calculateCenterActive(t, true, t->tsnap.snapTarget)) {
1064 if (t->flag & (T_EDIT | T_POSE)) {
1065 Object *ob = t->obedit ? t->obedit : t->poseobj;
1066 mul_m4_v3(ob->obmat, t->tsnap.snapTarget);
1069 TargetSnapOffset(t, NULL);
1071 t->tsnap.status |= TARGET_INIT;
1073 /* No active, default to median */
1075 t->tsnap.target = SCE_SNAP_TARGET_MEDIAN;
1076 t->tsnap.targetSnap = TargetSnapMedian;
1077 TargetSnapMedian(t);
1082 static void TargetSnapMedian(TransInfo *t)
1084 // Only need to calculate once
1085 if ((t->tsnap.status & TARGET_INIT) == 0) {
1086 TransData *td = NULL;
1089 t->tsnap.snapTarget[0] = 0;
1090 t->tsnap.snapTarget[1] = 0;
1091 t->tsnap.snapTarget[2] = 0;
1093 for (td = t->data, i = 0; i < t->total && td->flag & TD_SELECTED; i++, td++) {
1094 add_v3_v3(t->tsnap.snapTarget, td->center);
1097 mul_v3_fl(t->tsnap.snapTarget, 1.0 / i);
1099 if (t->flag & (T_EDIT | T_POSE)) {
1100 Object *ob = t->obedit ? t->obedit : t->poseobj;
1101 mul_m4_v3(ob->obmat, t->tsnap.snapTarget);
1104 TargetSnapOffset(t, NULL);
1106 t->tsnap.status |= TARGET_INIT;
1110 static void TargetSnapClosest(TransInfo *t)
1112 // Only valid if a snap point has been selected
1113 if (t->tsnap.status & POINT_INIT) {
1114 float dist_closest = 0.0f;
1115 TransData *closest = NULL, *td = NULL;
1118 if (t->flag & T_OBJECT) {
1120 for (td = t->data, i = 0; i < t->total && td->flag & TD_SELECTED; i++, td++) {
1121 struct BoundBox *bb = BKE_object_boundbox_get(td->ob);
1123 /* use boundbox if possible */
1127 for (j = 0; j < 8; j++) {
1131 copy_v3_v3(loc, bb->vec[j]);
1132 mul_m4_v3(td->ext->obmat, loc);
1134 dist = t->tsnap.distance(t, loc, t->tsnap.snapPoint);
1136 if ((dist != TRANSFORM_DIST_INVALID) &&
1137 (closest == NULL || fabsf(dist) < fabsf(dist_closest)))
1139 copy_v3_v3(t->tsnap.snapTarget, loc);
1141 dist_closest = dist;
1145 /* use element center otherwise */
1150 copy_v3_v3(loc, td->center);
1152 dist = t->tsnap.distance(t, loc, t->tsnap.snapPoint);
1154 if ((dist != TRANSFORM_DIST_INVALID) &&
1155 (closest == NULL || fabsf(dist) < fabsf(dist_closest)))
1157 copy_v3_v3(t->tsnap.snapTarget, loc);
1165 for (td = t->data, i = 0; i < t->total && td->flag & TD_SELECTED; i++, td++) {
1169 copy_v3_v3(loc, td->center);
1171 if (t->flag & (T_EDIT | T_POSE)) {
1172 Object *ob = t->obedit ? t->obedit : t->poseobj;
1173 mul_m4_v3(ob->obmat, loc);
1176 dist = t->tsnap.distance(t, loc, t->tsnap.snapPoint);
1178 if ((dist != TRANSFORM_DIST_INVALID) &&
1179 (closest == NULL || fabsf(dist) < fabsf(dist_closest)))
1181 copy_v3_v3(t->tsnap.snapTarget, loc);
1183 dist_closest = dist;
1188 TargetSnapOffset(t, closest);
1190 t->tsnap.status |= TARGET_INIT;
1194 bool snapObjectsTransform(
1195 TransInfo *t, const float mval[2],
1197 float r_loc[3], float r_no[3])
1199 return ED_transform_snap_object_project_view3d_ex(
1200 t->tsnap.object_context,
1201 t->scene->toolsettings->snap_mode,
1202 &(const struct SnapObjectParams){
1203 .snap_select = t->tsnap.modeSelect,
1204 .use_object_edit_cage = (t->flag & T_EDIT) != 0,
1206 mval, dist_px, NULL,
1211 /******************** PEELING *********************************/
1213 bool peelObjectsSnapContext(
1214 SnapObjectContext *sctx,
1215 const float mval[2],
1216 const struct SnapObjectParams *params,
1217 const bool use_peel_object,
1219 float r_loc[3], float r_no[3], float *r_thickness)
1221 ListBase depths_peel = {0};
1222 ED_transform_snap_object_project_all_view3d_ex(
1228 if (!BLI_listbase_is_empty(&depths_peel)) {
1229 /* At the moment we only use the hits of the first object */
1230 struct SnapObjectHitDepth *hit_min = depths_peel.first;
1231 for (struct SnapObjectHitDepth *iter = hit_min->next; iter; iter = iter->next) {
1232 if (iter->depth < hit_min->depth) {
1236 struct SnapObjectHitDepth *hit_max = NULL;
1238 if (use_peel_object) {
1239 /* if peeling objects, take the first and last from each object */
1241 for (struct SnapObjectHitDepth *iter = depths_peel.first; iter; iter = iter->next) {
1242 if ((iter->depth > hit_max->depth) && (iter->ob_uuid == hit_min->ob_uuid)) {
1248 /* otherwise, pair first with second and so on */
1249 for (struct SnapObjectHitDepth *iter = depths_peel.first; iter; iter = iter->next) {
1250 if ((iter != hit_min) && (iter->ob_uuid == hit_min->ob_uuid)) {
1251 if (hit_max == NULL) {
1254 else if (iter->depth < hit_max->depth) {
1259 /* in this case has only one hit. treat as raycast */
1260 if (hit_max == NULL) {
1265 mid_v3_v3v3(r_loc, hit_min->co, hit_max->co);
1268 *r_thickness = hit_max->depth - hit_min->depth;
1271 /* XXX, is there a correct normal in this case ???, for now just z up */
1276 BLI_freelistN(&depths_peel);
1282 bool peelObjectsTransform(
1284 const float mval[2],
1285 const bool use_peel_object,
1287 float r_loc[3], float r_no[3], float *r_thickness)
1289 return peelObjectsSnapContext(
1290 t->tsnap.object_context,
1292 &(const struct SnapObjectParams){
1293 .snap_select = t->tsnap.modeSelect,
1294 .use_object_edit_cage = (t->flag & T_EDIT) != 0,
1297 r_loc, r_no, r_thickness);
1300 /******************** NODES ***********************************/
1302 static bool snapNodeTest(View2D *v2d, bNode *node, SnapSelect snap_select)
1304 /* node is use for snapping only if a) snap mode matches and b) node is inside the view */
1305 return ((snap_select == SNAP_NOT_SELECTED && !(node->flag & NODE_SELECT)) ||
1306 (snap_select == SNAP_ALL && !(node->flag & NODE_ACTIVE))) &&
1307 (node->totr.xmin < v2d->cur.xmax && node->totr.xmax > v2d->cur.xmin &&
1308 node->totr.ymin < v2d->cur.ymax && node->totr.ymax > v2d->cur.ymin);
1311 static NodeBorder snapNodeBorder(int snap_node_mode)
1313 switch (snap_node_mode) {
1314 case SCE_SNAP_MODE_NODE_X:
1315 return NODE_LEFT | NODE_RIGHT;
1316 case SCE_SNAP_MODE_NODE_Y:
1317 return NODE_TOP | NODE_BOTTOM;
1318 case SCE_SNAP_MODE_NODE_XY:
1319 return NODE_LEFT | NODE_RIGHT | NODE_TOP | NODE_BOTTOM;
1324 static bool snapNode(
1325 ToolSettings *ts, SpaceNode *UNUSED(snode), ARegion *ar, bNode *node, const int mval[2],
1326 float r_loc[2], float *r_dist_px, char *r_node_border)
1328 View2D *v2d = &ar->v2d;
1329 NodeBorder border = snapNodeBorder(ts->snap_node_mode);
1330 bool retval = false;
1334 UI_view2d_view_to_region_rcti(v2d, &node->totr, &totr);
1336 if (border & NODE_LEFT) {
1337 new_dist = abs(totr.xmin - mval[0]);
1338 if (new_dist < *r_dist_px) {
1339 UI_view2d_region_to_view(v2d, totr.xmin, mval[1], &r_loc[0], &r_loc[1]);
1340 *r_dist_px = new_dist;
1341 *r_node_border = NODE_LEFT;
1346 if (border & NODE_RIGHT) {
1347 new_dist = abs(totr.xmax - mval[0]);
1348 if (new_dist < *r_dist_px) {
1349 UI_view2d_region_to_view(v2d, totr.xmax, mval[1], &r_loc[0], &r_loc[1]);
1350 *r_dist_px = new_dist;
1351 *r_node_border = NODE_RIGHT;
1356 if (border & NODE_BOTTOM) {
1357 new_dist = abs(totr.ymin - mval[1]);
1358 if (new_dist < *r_dist_px) {
1359 UI_view2d_region_to_view(v2d, mval[0], totr.ymin, &r_loc[0], &r_loc[1]);
1360 *r_dist_px = new_dist;
1361 *r_node_border = NODE_BOTTOM;
1366 if (border & NODE_TOP) {
1367 new_dist = abs(totr.ymax - mval[1]);
1368 if (new_dist < *r_dist_px) {
1369 UI_view2d_region_to_view(v2d, mval[0], totr.ymax, &r_loc[0], &r_loc[1]);
1370 *r_dist_px = new_dist;
1371 *r_node_border = NODE_TOP;
1379 static bool snapNodes(
1380 ToolSettings *ts, SpaceNode *snode, ARegion *ar,
1381 const int mval[2], SnapSelect snap_select,
1382 float r_loc[2], float *r_dist_px, char *r_node_border)
1384 bNodeTree *ntree = snode->edittree;
1386 bool retval = false;
1390 for (node = ntree->nodes.first; node; node = node->next) {
1391 if (snapNodeTest(&ar->v2d, node, snap_select)) {
1392 retval |= snapNode(ts, snode, ar, node, mval, r_loc, r_dist_px, r_node_border);
1399 bool snapNodesTransform(
1400 TransInfo *t, const int mval[2], SnapSelect snap_select,
1401 float r_loc[2], float *r_dist_px, char *r_node_border)
1404 t->settings, t->sa->spacedata.first, t->ar, mval, snap_select,
1405 r_loc, r_dist_px, r_node_border);
1408 bool snapNodesContext(
1409 bContext *C, const int mval[2], SnapSelect snap_select,
1410 float r_loc[2], float *r_dist_px, char *r_node_border)
1412 Scene *scene = CTX_data_scene(C);
1413 ARegion *ar = CTX_wm_region(C);
1415 scene->toolsettings, CTX_wm_space_node(C), ar, mval, snap_select,
1416 r_loc, r_dist_px, r_node_border);
1419 /*================================================================*/
1421 static void applyGridIncrement(TransInfo *t, float *val, int max_index, const float fac[3], GearsType action);
1424 void snapGridIncrementAction(TransInfo *t, float *val, GearsType action)
1428 fac[NO_GEARS] = t->snap[0];
1429 fac[BIG_GEARS] = t->snap[1];
1430 fac[SMALL_GEARS] = t->snap[2];
1432 applyGridIncrement(t, val, t->idx_max, fac, action);
1436 void snapGridIncrement(TransInfo *t, float *val)
1440 /* only do something if using absolute or incremental grid snapping */
1441 if (!ELEM(t->tsnap.mode, SCE_SNAP_MODE_INCREMENT, SCE_SNAP_MODE_GRID))
1444 action = activeSnap(t) ? BIG_GEARS : NO_GEARS;
1446 if (action == BIG_GEARS && (t->modifiers & MOD_PRECISION)) {
1447 action = SMALL_GEARS;
1450 snapGridIncrementAction(t, val, action);
1453 void snapSequenceBounds(TransInfo *t, const int mval[2])
1455 float xmouse, ymouse;
1458 TransSeq *ts = t->custom.type.data;
1459 /* reuse increment, strictly speaking could be another snap mode, but leave as is */
1460 if (!(t->modifiers & MOD_SNAP_INVERT))
1463 /* convert to frame range */
1464 UI_view2d_region_to_view(&t->ar->v2d, mval[0], mval[1], &xmouse, &ymouse);
1465 mframe = iroundf(xmouse);
1466 /* now find the closest sequence */
1467 frame = BKE_sequencer_find_next_prev_edit(t->scene, mframe, SEQ_SIDE_BOTH, true, false, true);
1470 frame = frame - (ts->max - ts->min);
1472 t->values[0] = frame - ts->min;
1475 static void applyGridIncrement(TransInfo *t, float *val, int max_index, const float fac[3], GearsType action)
1477 float asp_local[3] = {1, 1, 1};
1478 const bool use_aspect = ELEM(t->mode, TFM_TRANSLATION);
1479 const float *asp = use_aspect ? t->aspect : asp_local;
1482 BLI_assert(ELEM(t->tsnap.mode, SCE_SNAP_MODE_INCREMENT, SCE_SNAP_MODE_GRID));
1483 BLI_assert(max_index <= 2);
1485 /* Early bailing out if no need to snap */
1486 if (fac[action] == 0.0f) {
1491 /* custom aspect for fcurve */
1492 if (t->spacetype == SPACE_IPO) {
1493 View2D *v2d = &t->ar->v2d;
1495 SpaceIpo *sipo = t->sa->spacedata.first;
1496 int unity = V2D_UNIT_VALUES;
1497 int unitx = (sipo->flag & SIPO_DRAWTIME) ? V2D_UNIT_SECONDS : V2D_UNIT_FRAMESCALE;
1500 grid = UI_view2d_grid_calc(t->scene, v2d, unitx, V2D_GRID_NOCLAMP, unity, V2D_GRID_NOCLAMP, t->ar->winx, t->ar->winy);
1502 UI_view2d_grid_size(grid, &asp_local[0], &asp_local[1]);
1503 UI_view2d_grid_free(grid);
1509 /* absolute snapping on grid based on global center */
1510 if ((t->tsnap.snap_spatial_grid) && (t->mode == TFM_TRANSLATION)) {
1511 const float *center_global = t->center_global;
1513 /* use a fallback for cursor selection,
1514 * this isn't useful as a global center for absolute grid snapping
1515 * since its not based on the position of the selection. */
1516 if (t->around == V3D_AROUND_CURSOR) {
1517 const TransCenterData *cd = transformCenter_from_type(t, V3D_AROUND_CENTER_MEAN);
1518 center_global = cd->global;
1521 for (i = 0; i <= max_index; i++) {
1522 /* do not let unconstrained axis jump to absolute grid increments */
1523 if (!(t->con.mode & CON_APPLY) || t->con.mode & (CON_AXIS0 << i)) {
1524 const float iter_fac = fac[action] * asp[i];
1525 val[i] = iter_fac * roundf((val[i] + center_global[i]) / iter_fac) - center_global[i];
1530 /* relative snapping in fixed increments */
1531 for (i = 0; i <= max_index; i++) {
1532 const float iter_fac = fac[action] * asp[i];
1533 val[i] = iter_fac * roundf(val[i] / iter_fac);