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"
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 for (p = t->tsnap.points.first; p; p = p->next) {
168 if (p == t->tsnap.selectedPoint) {
169 glColor4ubv(selectedCol);
175 drawcircball(GL_LINE_LOOP, p->co, ED_view3d_pixel_size(rv3d, p->co) * size * 0.75f, imat);
178 if (t->tsnap.status & POINT_INIT) {
179 glColor4ubv(activeCol);
181 drawcircball(GL_LINE_LOOP, t->tsnap.snapPoint, ED_view3d_pixel_size(rv3d, t->tsnap.snapPoint) * size, imat);
184 /* draw normal if needed */
185 if (usingSnappingNormal(t) && validSnappingNormal(t)) {
186 glColor4ubv(activeCol);
189 glVertex3f(t->tsnap.snapPoint[0], t->tsnap.snapPoint[1], t->tsnap.snapPoint[2]);
190 glVertex3f(t->tsnap.snapPoint[0] + t->tsnap.snapNormal[0],
191 t->tsnap.snapPoint[1] + t->tsnap.snapNormal[1],
192 t->tsnap.snapPoint[2] + t->tsnap.snapNormal[2]);
197 glEnable(GL_DEPTH_TEST);
200 else if (t->spacetype == SPACE_IMAGE) {
202 /* This will not draw, and Im nor sure why - campbell */
204 float xuser_asp, yuser_asp;
208 calc_image_view(G.sima, 'f'); // float
209 myortho2(G.v2d->cur.xmin, G.v2d->cur.xmax, G.v2d->cur.ymin, G.v2d->cur.ymax);
212 ED_space_image_get_aspect(t->sa->spacedata.first, &xuser_aspx, &yuser_asp);
213 ED_space_image_width(t->sa->spacedata.first, &wi, &hi);
214 w = (((float)wi) / IMG_SIZE_FALLBACK) * G.sima->zoom * xuser_asp;
215 h = (((float)hi) / IMG_SIZE_FALLBACK) * G.sima->zoom * yuser_asp;
218 glTranslate2fv(t->tsnap.snapPoint);
220 //glRectf(0, 0, 1, 1);
224 fdrawline(-0.020 / w, 0, -0.1 / w, 0);
225 fdrawline(0.1 / w, 0, 0.020 / w, 0);
226 fdrawline(0, -0.020 / h, 0, -0.1 / h);
227 fdrawline(0, 0.1 / h, 0, 0.020 / h);
229 glTranslatef(-t->tsnap.snapPoint[0], -t->tsnap.snapPoint[1], 0.0f);
234 else if (t->spacetype == SPACE_NODE) {
236 ARegion *ar = CTX_wm_region(C);
240 size = 2.5f * UI_GetThemeValuef(TH_VERTEX_SIZE);
244 for (p = t->tsnap.points.first; p; p = p->next) {
245 if (p == t->tsnap.selectedPoint) {
246 glColor4ubv(selectedCol);
252 ED_node_draw_snap(&ar->v2d, p->co, size, 0);
255 if (t->tsnap.status & POINT_INIT) {
256 glColor4ubv(activeCol);
258 ED_node_draw_snap(&ar->v2d, t->tsnap.snapPoint, size, t->tsnap.snapNodeBorder);
266 eRedrawFlag handleSnapping(TransInfo *t, const wmEvent *event)
268 eRedrawFlag status = TREDRAW_NOTHING;
270 #if 0 // XXX need a proper selector for all snap mode
271 if (BIF_snappingSupported(t->obedit) && event->type == TABKEY && event->shift) {
272 /* toggle snap and reinit */
273 t->settings->snap_flag ^= SCE_SNAP;
274 initSnapping(t, NULL);
275 status = TREDRAW_HARD;
278 if (event->type == MOUSEMOVE) {
279 status |= updateSelectedSnapPoint(t);
285 void applyProject(TransInfo *t)
287 /* XXX FLICKER IN OBJECT MODE */
288 if ((t->tsnap.project) && activeSnap(t) && (t->flag & T_NO_PROJECT) == 0) {
289 TransData *td = t->data;
294 if (t->flag & (T_EDIT | T_POSE)) {
295 Object *ob = t->obedit ? t->obedit : t->poseobj;
296 invert_m4_m4(imat, ob->obmat);
299 for (i = 0; i < t->total; i++, td++) {
300 float iloc[3], loc[3], no[3];
302 float dist_px = TRANSFORM_DIST_MAX_PX;
304 if (td->flag & TD_NOACTION)
307 if (td->flag & TD_SKIP)
310 if ((t->flag & T_PROP_EDIT) && (td->factor == 0.0f))
313 copy_v3_v3(iloc, td->loc);
314 if (t->flag & (T_EDIT | T_POSE)) {
315 Object *ob = t->obedit ? t->obedit : t->poseobj;
316 mul_m4_v3(ob->obmat, iloc);
318 else if (t->flag & T_OBJECT) {
319 /* TODO(sergey): Ideally force update is not needed here. */
320 td->ob->recalc |= OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME;
321 BKE_object_handle_update(G.main->eval_ctx, t->scene, td->ob);
322 copy_v3_v3(iloc, td->ob->obmat[3]);
325 if (ED_view3d_project_float_global(t->ar, iloc, mval_fl, V3D_PROJ_TEST_NOP) == V3D_PROJ_RET_OK) {
326 if (snapObjectsTransform(
327 t, mval_fl, &dist_px,
330 // if (t->flag & (T_EDIT|T_POSE)) {
331 // mul_m4_v3(imat, loc);
334 sub_v3_v3v3(tvec, loc, iloc);
336 mul_m3_v3(td->smtx, tvec);
338 add_v3_v3(td->loc, tvec);
340 if (t->tsnap.align && (t->flag & T_OBJECT)) {
341 /* handle alignment as well */
342 const float *original_normal;
345 /* In pose mode, we want to align normals with Y axis of bones... */
346 original_normal = td->axismtx[2];
348 rotation_between_vecs_to_mat3(mat, original_normal, no);
350 transform_data_ext_rotate(td, mat, true);
352 /* TODO support constraints for rotation too? see ElementRotation */
357 //XXX constraintTransLim(t, td);
362 void applyGridAbsolute(TransInfo *t)
364 float grid_size = 0.0f;
365 GearsType grid_action;
367 float (*obmat)[4] = NULL;
368 bool use_obmat = false;
371 if (!(activeSnap(t) && (ELEM(t->tsnap.mode, SCE_SNAP_MODE_INCREMENT, SCE_SNAP_MODE_GRID))))
374 grid_action = BIG_GEARS;
375 if (t->modifiers & MOD_PRECISION)
376 grid_action = SMALL_GEARS;
378 switch (grid_action) {
379 case NO_GEARS: grid_size = t->snap_spatial[0]; break;
380 case BIG_GEARS: grid_size = t->snap_spatial[1]; break;
381 case SMALL_GEARS: grid_size = t->snap_spatial[2]; break;
383 /* early exit on unusable grid size */
384 if (grid_size == 0.0f)
387 if (t->flag & (T_EDIT | T_POSE)) {
388 Object *ob = t->obedit ? t->obedit : t->poseobj;
393 for (i = 0, td = t->data; i < t->total; i++, td++) {
394 float iloc[3], loc[3], tvec[3];
396 if (td->flag & TD_NOACTION)
399 if (td->flag & TD_SKIP)
402 if ((t->flag & T_PROP_EDIT) && (td->factor == 0.0f))
405 copy_v3_v3(iloc, td->loc);
407 mul_m4_v3(obmat, iloc);
409 else if (t->flag & T_OBJECT) {
410 td->ob->recalc |= OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME;
411 BKE_object_handle_update(G.main->eval_ctx, t->scene, td->ob);
412 copy_v3_v3(iloc, td->ob->obmat[3]);
415 mul_v3_v3fl(loc, iloc, 1.0f / grid_size);
416 loc[0] = roundf(loc[0]);
417 loc[1] = roundf(loc[1]);
418 loc[2] = roundf(loc[2]);
419 mul_v3_fl(loc, grid_size);
421 sub_v3_v3v3(tvec, loc, iloc);
422 mul_m3_v3(td->smtx, tvec);
423 add_v3_v3(td->loc, tvec);
427 void applySnapping(TransInfo *t, float *vec)
429 /* project is not applied this way */
430 if (t->tsnap.project)
433 if (t->tsnap.status & SNAP_FORCED) {
434 t->tsnap.targetSnap(t);
436 t->tsnap.applySnap(t, vec);
438 else if (!ELEM(t->tsnap.mode, SCE_SNAP_MODE_INCREMENT, SCE_SNAP_MODE_GRID) && activeSnap(t)) {
439 double current = PIL_check_seconds_timer();
441 // Time base quirky code to go around findnearest slowness
442 /* !TODO! add exception for object mode, no need to slow it down then */
443 if (current - t->tsnap.last >= 0.01) {
444 t->tsnap.calcSnap(t, vec);
445 t->tsnap.targetSnap(t);
447 t->tsnap.last = current;
450 t->tsnap.applySnap(t, vec);
455 void resetSnapping(TransInfo *t)
458 t->tsnap.align = false;
459 t->tsnap.project = 0;
461 t->tsnap.modeSelect = 0;
464 t->tsnap.applySnap = NULL;
466 t->tsnap.snapNormal[0] = 0;
467 t->tsnap.snapNormal[1] = 0;
468 t->tsnap.snapNormal[2] = 0;
470 t->tsnap.snapNodeBorder = 0;
473 bool usingSnappingNormal(TransInfo *t)
475 return t->tsnap.align;
478 bool validSnappingNormal(TransInfo *t)
481 if (!is_zero_v3(t->tsnap.snapNormal)) {
489 static bool bm_edge_is_snap_target(BMEdge *e, void *UNUSED(user_data))
491 if (BM_elem_flag_test(e, BM_ELEM_SELECT | BM_ELEM_HIDDEN) ||
492 BM_elem_flag_test(e->v1, BM_ELEM_SELECT) ||
493 BM_elem_flag_test(e->v2, BM_ELEM_SELECT))
501 static bool bm_face_is_snap_target(BMFace *f, void *UNUSED(user_data))
503 if (BM_elem_flag_test(f, BM_ELEM_SELECT | BM_ELEM_HIDDEN)) {
507 BMLoop *l_iter, *l_first;
508 l_iter = l_first = BM_FACE_FIRST_LOOP(f);
510 if (BM_elem_flag_test(l_iter->v, BM_ELEM_SELECT)) {
513 } while ((l_iter = l_iter->next) != l_first);
518 static void initSnappingMode(TransInfo *t)
520 ToolSettings *ts = t->settings;
521 Object *obedit = t->obedit;
523 if (t->spacetype == SPACE_NODE) {
524 /* force project off when not supported */
525 t->tsnap.project = 0;
527 t->tsnap.mode = ts->snap_node_mode;
529 else if (t->spacetype == SPACE_IMAGE) {
530 /* force project off when not supported */
531 t->tsnap.project = 0;
533 t->tsnap.mode = ts->snap_uv_mode;
536 /* force project off when not supported */
537 if (ts->snap_mode != SCE_SNAP_MODE_FACE)
538 t->tsnap.project = 0;
540 t->tsnap.mode = ts->snap_mode;
543 if ((t->spacetype == SPACE_VIEW3D || t->spacetype == SPACE_IMAGE) && /* Only 3D view or UV */
544 (t->flag & T_CAMERA) == 0) /* Not with camera selected in camera view */
546 setSnappingCallback(t);
549 if (t->tsnap.applySnap != NULL && // A snapping function actually exist
550 (obedit != NULL && ELEM(obedit->type, OB_MESH, OB_ARMATURE, OB_CURVE, OB_LATTICE, OB_MBALL)) ) // Temporary limited to edit mode meshes, armature, curves, mballs
552 /* Exclude editmesh if using proportional edit */
553 if ((obedit->type == OB_MESH) && (t->flag & T_PROP_EDIT)) {
554 t->tsnap.modeSelect = SNAP_NOT_ACTIVE;
557 t->tsnap.modeSelect = t->tsnap.snap_self ? SNAP_ALL : SNAP_NOT_ACTIVE;
561 else if (t->tsnap.applySnap != NULL && // A snapping function actually exist
562 (obedit == NULL) ) // Object Mode
564 t->tsnap.modeSelect = SNAP_NOT_SELECTED;
567 /* Grid if snap is not possible */
568 t->tsnap.mode = SCE_SNAP_MODE_INCREMENT;
571 else if (t->spacetype == SPACE_NODE) {
572 setSnappingCallback(t);
574 if (t->tsnap.applySnap != NULL) {
575 t->tsnap.modeSelect = SNAP_NOT_SELECTED;
578 /* Grid if snap is not possible */
579 t->tsnap.mode = SCE_SNAP_MODE_INCREMENT;
582 else if (t->spacetype == SPACE_SEQ) {
583 /* We do our own snapping currently, so nothing here */
584 t->tsnap.mode = SCE_SNAP_MODE_GRID; /* Dummy, should we rather add a NOP mode? */
587 /* Always grid outside of 3D view */
588 t->tsnap.mode = SCE_SNAP_MODE_INCREMENT;
591 if (t->spacetype == SPACE_VIEW3D) {
592 if (t->tsnap.object_context == NULL) {
593 t->tsnap.object_context = ED_transform_snap_object_context_create_view3d(
594 G.main, t->scene, SNAP_OBJECT_USE_CACHE,
597 ED_transform_snap_object_context_set_editmesh_callbacks(
598 t->tsnap.object_context,
599 (bool (*)(BMVert *, void *))BM_elem_cb_check_hflag_disabled,
600 bm_edge_is_snap_target,
601 bm_face_is_snap_target,
602 SET_UINT_IN_POINTER((BM_ELEM_SELECT | BM_ELEM_HIDDEN)));
607 void initSnapping(TransInfo *t, wmOperator *op)
609 ToolSettings *ts = t->settings;
610 short snap_target = t->settings->snap_target;
614 /* if snap property exists */
615 if (op && RNA_struct_find_property(op->ptr, "snap") && RNA_struct_property_is_set(op->ptr, "snap")) {
616 if (RNA_boolean_get(op->ptr, "snap")) {
617 t->modifiers |= MOD_SNAP;
619 if (RNA_struct_property_is_set(op->ptr, "snap_target")) {
620 snap_target = RNA_enum_get(op->ptr, "snap_target");
623 if (RNA_struct_property_is_set(op->ptr, "snap_point")) {
624 RNA_float_get_array(op->ptr, "snap_point", t->tsnap.snapPoint);
625 t->tsnap.status |= SNAP_FORCED | POINT_INIT;
628 /* snap align only defined in specific cases */
629 if (RNA_struct_find_property(op->ptr, "snap_align")) {
630 t->tsnap.align = RNA_boolean_get(op->ptr, "snap_align");
631 RNA_float_get_array(op->ptr, "snap_normal", t->tsnap.snapNormal);
632 normalize_v3(t->tsnap.snapNormal);
635 if (RNA_struct_find_property(op->ptr, "use_snap_project")) {
636 t->tsnap.project = RNA_boolean_get(op->ptr, "use_snap_project");
639 if (RNA_struct_find_property(op->ptr, "use_snap_self")) {
640 t->tsnap.snap_self = RNA_boolean_get(op->ptr, "use_snap_self");
644 /* use scene defaults only when transform is modal */
645 else if (t->flag & T_MODAL) {
646 if (ELEM(t->spacetype, SPACE_VIEW3D, SPACE_IMAGE, SPACE_NODE)) {
647 if (ts->snap_flag & SCE_SNAP) {
648 t->modifiers |= MOD_SNAP;
651 t->tsnap.align = ((t->settings->snap_flag & SCE_SNAP_ROTATE) != 0);
652 t->tsnap.project = ((t->settings->snap_flag & SCE_SNAP_PROJECT) != 0);
653 t->tsnap.snap_self = !((t->settings->snap_flag & SCE_SNAP_NO_SELF) != 0);
654 t->tsnap.peel = ((t->settings->snap_flag & SCE_SNAP_PROJECT) != 0);
657 /* for now only 3d view (others can be added if we want) */
658 if (t->spacetype == SPACE_VIEW3D) {
659 t->tsnap.snap_spatial_grid = ((t->settings->snap_flag & SCE_SNAP_ABS_GRID) != 0);
663 t->tsnap.target = snap_target;
668 void freeSnapping(TransInfo *t)
670 if (t->tsnap.object_context) {
671 ED_transform_snap_object_context_destroy(t->tsnap.object_context);
672 t->tsnap.object_context = NULL;
676 static void setSnappingCallback(TransInfo *t)
678 t->tsnap.calcSnap = CalcSnapGeometry;
680 switch (t->tsnap.target) {
681 case SCE_SNAP_TARGET_CLOSEST:
682 t->tsnap.targetSnap = TargetSnapClosest;
684 case SCE_SNAP_TARGET_CENTER:
685 t->tsnap.targetSnap = TargetSnapCenter;
687 case SCE_SNAP_TARGET_MEDIAN:
688 t->tsnap.targetSnap = TargetSnapMedian;
690 case SCE_SNAP_TARGET_ACTIVE:
691 t->tsnap.targetSnap = TargetSnapActive;
697 case TFM_TRANSLATION:
698 t->tsnap.applySnap = ApplySnapTranslation;
699 t->tsnap.distance = TranslationBetween;
702 t->tsnap.applySnap = ApplySnapRotation;
703 t->tsnap.distance = RotationBetween;
705 // Can't do TARGET_CENTER with rotation, use TARGET_MEDIAN instead
706 if (t->tsnap.target == SCE_SNAP_TARGET_CENTER) {
707 t->tsnap.target = SCE_SNAP_TARGET_MEDIAN;
708 t->tsnap.targetSnap = TargetSnapMedian;
712 t->tsnap.applySnap = ApplySnapResize;
713 t->tsnap.distance = ResizeBetween;
715 // Can't do TARGET_CENTER with resize, use TARGET_MEDIAN instead
716 if (t->tsnap.target == SCE_SNAP_TARGET_CENTER) {
717 t->tsnap.target = SCE_SNAP_TARGET_MEDIAN;
718 t->tsnap.targetSnap = TargetSnapMedian;
722 t->tsnap.applySnap = NULL;
727 void addSnapPoint(TransInfo *t)
729 /* Currently only 3D viewport works for snapping points. */
730 if (t->tsnap.status & POINT_INIT && t->spacetype == SPACE_VIEW3D) {
731 TransSnapPoint *p = MEM_callocN(sizeof(TransSnapPoint), "SnapPoint");
733 t->tsnap.selectedPoint = p;
735 copy_v3_v3(p->co, t->tsnap.snapPoint);
737 BLI_addtail(&t->tsnap.points, p);
739 t->tsnap.status |= MULTI_POINTS;
743 eRedrawFlag updateSelectedSnapPoint(TransInfo *t)
745 eRedrawFlag status = TREDRAW_NOTHING;
747 if (t->tsnap.status & MULTI_POINTS) {
748 TransSnapPoint *p, *closest_p = NULL;
749 float dist_min_sq = TRANSFORM_SNAP_MAX_PX;
750 const float mval_fl[2] = {t->mval[0], t->mval[1]};
753 for (p = t->tsnap.points.first; p; p = p->next) {
756 if (ED_view3d_project_float_global(t->ar, p->co, screen_loc, V3D_PROJ_TEST_NOP) != V3D_PROJ_RET_OK) {
760 dist_sq = len_squared_v2v2(mval_fl, screen_loc);
762 if (dist_sq < dist_min_sq) {
764 dist_min_sq = dist_sq;
769 if (t->tsnap.selectedPoint != closest_p) {
770 status = TREDRAW_HARD;
773 t->tsnap.selectedPoint = closest_p;
780 void removeSnapPoint(TransInfo *t)
782 if (t->tsnap.status & MULTI_POINTS) {
783 updateSelectedSnapPoint(t);
785 if (t->tsnap.selectedPoint) {
786 BLI_freelinkN(&t->tsnap.points, t->tsnap.selectedPoint);
788 if (BLI_listbase_is_empty(&t->tsnap.points)) {
789 t->tsnap.status &= ~MULTI_POINTS;
792 t->tsnap.selectedPoint = NULL;
798 void getSnapPoint(TransInfo *t, float vec[3])
800 if (t->tsnap.points.first) {
804 vec[0] = vec[1] = vec[2] = 0;
806 for (p = t->tsnap.points.first; p; p = p->next, total++) {
807 add_v3_v3(vec, p->co);
810 if (t->tsnap.status & POINT_INIT) {
811 add_v3_v3(vec, t->tsnap.snapPoint);
815 mul_v3_fl(vec, 1.0f / total);
818 copy_v3_v3(vec, t->tsnap.snapPoint);
822 /********************** APPLY **************************/
824 static void ApplySnapTranslation(TransInfo *t, float vec[3])
827 getSnapPoint(t, point);
829 if (t->spacetype == SPACE_NODE) {
830 char border = t->tsnap.snapNodeBorder;
831 if (border & (NODE_LEFT | NODE_RIGHT))
832 vec[0] = point[0] - t->tsnap.snapTarget[0];
833 if (border & (NODE_BOTTOM | NODE_TOP))
834 vec[1] = point[1] - t->tsnap.snapTarget[1];
837 sub_v3_v3v3(vec, point, t->tsnap.snapTarget);
841 static void ApplySnapRotation(TransInfo *t, float *value)
844 getSnapPoint(t, point);
846 float dist = RotationBetween(t, t->tsnap.snapTarget, point);
850 static void ApplySnapResize(TransInfo *t, float vec[3])
853 getSnapPoint(t, point);
855 float dist = ResizeBetween(t, t->tsnap.snapTarget, point);
856 copy_v3_fl(vec, dist);
859 /********************** DISTANCE **************************/
861 static float TranslationBetween(TransInfo *UNUSED(t), const float p1[3], const float p2[3])
863 return len_squared_v3v3(p1, p2);
866 static float RotationBetween(TransInfo *t, const float p1[3], const float p2[3])
868 float angle, start[3], end[3];
870 sub_v3_v3v3(start, p1, t->center_global);
871 sub_v3_v3v3(end, p2, t->center_global);
873 // Angle around a constraint axis (error prone, will need debug)
874 if (t->con.applyRot != NULL && (t->con.mode & CON_APPLY)) {
875 float axis[3], tmp[3];
877 t->con.applyRot(t, NULL, axis, NULL);
879 project_v3_v3v3(tmp, end, axis);
880 sub_v3_v3v3(end, end, tmp);
882 project_v3_v3v3(tmp, start, axis);
883 sub_v3_v3v3(start, start, tmp);
888 cross_v3_v3v3(tmp, start, end);
890 if (dot_v3v3(tmp, axis) < 0.0f)
891 angle = -acosf(dot_v3v3(start, end));
893 angle = acosf(dot_v3v3(start, end));
898 copy_m3_m4(mtx, t->viewmat);
901 mul_m3_v3(mtx, start);
903 angle = atan2f(start[1], start[0]) - atan2f(end[1], end[0]);
906 if (angle > (float)M_PI) {
907 angle = angle - 2 * (float)M_PI;
909 else if (angle < -((float)M_PI)) {
910 angle = 2.0f * (float)M_PI + angle;
916 static float ResizeBetween(TransInfo *t, const float p1[3], const float p2[3])
918 float d1[3], d2[3], len_d1;
920 sub_v3_v3v3(d1, p1, t->center_global);
921 sub_v3_v3v3(d2, p2, t->center_global);
923 if (t->con.applyRot != NULL && (t->con.mode & CON_APPLY)) {
924 mul_m3_v3(t->con.pmtx, d1);
925 mul_m3_v3(t->con.pmtx, d2);
928 project_v3_v3v3(d1, d1, d2);
932 /* Use 'invalid' dist when `center == p1` (after projecting),
933 * in this case scale will _never_ move the point in relation to the center,
934 * so it makes no sense to take it into account when scaling. see: T46503 */
935 return len_d1 != 0.0f ? len_v3(d2) / len_d1 : TRANSFORM_DIST_INVALID;
938 /********************** CALC **************************/
940 static void UNUSED_FUNCTION(CalcSnapGrid) (TransInfo *t, float *UNUSED(vec))
942 snapGridIncrementAction(t, t->tsnap.snapPoint, BIG_GEARS);
945 static void CalcSnapGeometry(TransInfo *t, float *UNUSED(vec))
947 if (t->spacetype == SPACE_VIEW3D) {
952 float dist_px = SNAP_MIN_DISTANCE; // Use a user defined value here
954 mval[0] = t->mval[0];
955 mval[1] = t->mval[1];
957 if (t->tsnap.mode == SCE_SNAP_MODE_VOLUME) {
958 found = peelObjectsTransform(
960 (t->settings->snap_flag & SCE_SNAP_PEEL_OBJECT) != 0,
964 zero_v3(no); /* objects won't set this */
965 found = snapObjectsTransform(
973 sub_v2_v2v2(tangent, loc, t->tsnap.snapPoint);
976 if (!is_zero_v3(tangent)) {
977 copy_v3_v3(t->tsnap.snapTangent, tangent);
980 copy_v3_v3(t->tsnap.snapPoint, loc);
981 copy_v3_v3(t->tsnap.snapNormal, no);
983 t->tsnap.status |= POINT_INIT;
986 t->tsnap.status &= ~POINT_INIT;
989 else if (t->spacetype == SPACE_IMAGE && t->obedit != NULL && t->obedit->type == OB_MESH) {
990 /* same as above but for UV's */
991 Image *ima = ED_space_image(t->sa->spacedata.first);
994 UI_view2d_region_to_view(&t->ar->v2d, t->mval[0], t->mval[1], &co[0], &co[1]);
996 if (ED_uvedit_nearest_uv(t->scene, t->obedit, ima, co, t->tsnap.snapPoint)) {
997 t->tsnap.snapPoint[0] *= t->aspect[0];
998 t->tsnap.snapPoint[1] *= t->aspect[1];
1000 t->tsnap.status |= POINT_INIT;
1003 t->tsnap.status &= ~POINT_INIT;
1006 else if (t->spacetype == SPACE_NODE) {
1008 float dist_px = SNAP_MIN_DISTANCE; // Use a user defined value here
1011 if (snapNodesTransform(t, t->mval, t->tsnap.modeSelect, loc, &dist_px, &node_border)) {
1012 copy_v2_v2(t->tsnap.snapPoint, loc);
1013 t->tsnap.snapNodeBorder = node_border;
1015 t->tsnap.status |= POINT_INIT;
1018 t->tsnap.status &= ~POINT_INIT;
1023 /********************** TARGET **************************/
1025 static void TargetSnapOffset(TransInfo *t, TransData *td)
1027 if (t->spacetype == SPACE_NODE && td != NULL) {
1028 bNode *node = td->extra;
1029 char border = t->tsnap.snapNodeBorder;
1030 float width = BLI_rctf_size_x(&node->totr);
1031 float height = BLI_rctf_size_y(&node->totr);
1033 #ifdef USE_NODE_CENTER
1034 if (border & NODE_LEFT)
1035 t->tsnap.snapTarget[0] -= 0.5f * width;
1036 if (border & NODE_RIGHT)
1037 t->tsnap.snapTarget[0] += 0.5f * width;
1038 if (border & NODE_BOTTOM)
1039 t->tsnap.snapTarget[1] -= 0.5f * height;
1040 if (border & NODE_TOP)
1041 t->tsnap.snapTarget[1] += 0.5f * height;
1043 if (border & NODE_LEFT)
1044 t->tsnap.snapTarget[0] -= 0.0f;
1045 if (border & NODE_RIGHT)
1046 t->tsnap.snapTarget[0] += width;
1047 if (border & NODE_BOTTOM)
1048 t->tsnap.snapTarget[1] -= height;
1049 if (border & NODE_TOP)
1050 t->tsnap.snapTarget[1] += 0.0f;
1055 static void TargetSnapCenter(TransInfo *t)
1057 /* Only need to calculate once */
1058 if ((t->tsnap.status & TARGET_INIT) == 0) {
1059 copy_v3_v3(t->tsnap.snapTarget, t->center_global);
1060 TargetSnapOffset(t, NULL);
1062 t->tsnap.status |= TARGET_INIT;
1066 static void TargetSnapActive(TransInfo *t)
1068 /* Only need to calculate once */
1069 if ((t->tsnap.status & TARGET_INIT) == 0) {
1070 if (calculateCenterActive(t, true, t->tsnap.snapTarget)) {
1071 if (t->flag & (T_EDIT | T_POSE)) {
1072 Object *ob = t->obedit ? t->obedit : t->poseobj;
1073 mul_m4_v3(ob->obmat, t->tsnap.snapTarget);
1076 TargetSnapOffset(t, NULL);
1078 t->tsnap.status |= TARGET_INIT;
1080 /* No active, default to median */
1082 t->tsnap.target = SCE_SNAP_TARGET_MEDIAN;
1083 t->tsnap.targetSnap = TargetSnapMedian;
1084 TargetSnapMedian(t);
1089 static void TargetSnapMedian(TransInfo *t)
1091 // Only need to calculate once
1092 if ((t->tsnap.status & TARGET_INIT) == 0) {
1093 TransData *td = NULL;
1096 t->tsnap.snapTarget[0] = 0;
1097 t->tsnap.snapTarget[1] = 0;
1098 t->tsnap.snapTarget[2] = 0;
1100 for (td = t->data, i = 0; i < t->total && td->flag & TD_SELECTED; i++, td++) {
1101 add_v3_v3(t->tsnap.snapTarget, td->center);
1104 mul_v3_fl(t->tsnap.snapTarget, 1.0 / i);
1106 if (t->flag & (T_EDIT | T_POSE)) {
1107 Object *ob = t->obedit ? t->obedit : t->poseobj;
1108 mul_m4_v3(ob->obmat, t->tsnap.snapTarget);
1111 TargetSnapOffset(t, NULL);
1113 t->tsnap.status |= TARGET_INIT;
1117 static void TargetSnapClosest(TransInfo *t)
1119 // Only valid if a snap point has been selected
1120 if (t->tsnap.status & POINT_INIT) {
1121 float dist_closest = 0.0f;
1122 TransData *closest = NULL, *td = NULL;
1125 if (t->flag & T_OBJECT) {
1127 for (td = t->data, i = 0; i < t->total && td->flag & TD_SELECTED; i++, td++) {
1128 struct BoundBox *bb = BKE_object_boundbox_get(td->ob);
1130 /* use boundbox if possible */
1134 for (j = 0; j < 8; j++) {
1138 copy_v3_v3(loc, bb->vec[j]);
1139 mul_m4_v3(td->ext->obmat, loc);
1141 dist = t->tsnap.distance(t, loc, t->tsnap.snapPoint);
1143 if ((dist != TRANSFORM_DIST_INVALID) &&
1144 (closest == NULL || fabsf(dist) < fabsf(dist_closest)))
1146 copy_v3_v3(t->tsnap.snapTarget, loc);
1148 dist_closest = dist;
1152 /* use element center otherwise */
1157 copy_v3_v3(loc, td->center);
1159 dist = t->tsnap.distance(t, loc, t->tsnap.snapPoint);
1161 if ((dist != TRANSFORM_DIST_INVALID) &&
1162 (closest == NULL || fabsf(dist) < fabsf(dist_closest)))
1164 copy_v3_v3(t->tsnap.snapTarget, loc);
1172 for (td = t->data, i = 0; i < t->total && td->flag & TD_SELECTED; i++, td++) {
1176 copy_v3_v3(loc, td->center);
1178 if (t->flag & (T_EDIT | T_POSE)) {
1179 Object *ob = t->obedit ? t->obedit : t->poseobj;
1180 mul_m4_v3(ob->obmat, loc);
1183 dist = t->tsnap.distance(t, loc, t->tsnap.snapPoint);
1185 if ((dist != TRANSFORM_DIST_INVALID) &&
1186 (closest == NULL || fabsf(dist) < fabsf(dist_closest)))
1188 copy_v3_v3(t->tsnap.snapTarget, loc);
1190 dist_closest = dist;
1195 TargetSnapOffset(t, closest);
1197 t->tsnap.status |= TARGET_INIT;
1201 bool snapObjectsTransform(
1202 TransInfo *t, const float mval[2],
1204 float r_loc[3], float r_no[3])
1206 return ED_transform_snap_object_project_view3d_ex(
1207 t->tsnap.object_context,
1208 t->scene->toolsettings->snap_mode,
1209 &(const struct SnapObjectParams){
1210 .snap_select = ((t->options & CTX_GPENCIL_STROKES) != 0) ? SNAP_NOT_ACTIVE : t->tsnap.modeSelect,
1211 .use_object_edit_cage = (t->flag & T_EDIT) != 0,
1213 mval, dist_px, NULL,
1218 /******************** PEELING *********************************/
1220 bool peelObjectsSnapContext(
1221 SnapObjectContext *sctx,
1222 const float mval[2],
1223 const struct SnapObjectParams *params,
1224 const bool use_peel_object,
1226 float r_loc[3], float r_no[3], float *r_thickness)
1228 ListBase depths_peel = {0};
1229 ED_transform_snap_object_project_all_view3d_ex(
1235 if (!BLI_listbase_is_empty(&depths_peel)) {
1236 /* At the moment we only use the hits of the first object */
1237 struct SnapObjectHitDepth *hit_min = depths_peel.first;
1238 for (struct SnapObjectHitDepth *iter = hit_min->next; iter; iter = iter->next) {
1239 if (iter->depth < hit_min->depth) {
1243 struct SnapObjectHitDepth *hit_max = NULL;
1245 if (use_peel_object) {
1246 /* if peeling objects, take the first and last from each object */
1248 for (struct SnapObjectHitDepth *iter = depths_peel.first; iter; iter = iter->next) {
1249 if ((iter->depth > hit_max->depth) && (iter->ob_uuid == hit_min->ob_uuid)) {
1255 /* otherwise, pair first with second and so on */
1256 for (struct SnapObjectHitDepth *iter = depths_peel.first; iter; iter = iter->next) {
1257 if ((iter != hit_min) && (iter->ob_uuid == hit_min->ob_uuid)) {
1258 if (hit_max == NULL) {
1261 else if (iter->depth < hit_max->depth) {
1266 /* in this case has only one hit. treat as raycast */
1267 if (hit_max == NULL) {
1272 mid_v3_v3v3(r_loc, hit_min->co, hit_max->co);
1275 *r_thickness = hit_max->depth - hit_min->depth;
1278 /* XXX, is there a correct normal in this case ???, for now just z up */
1283 BLI_freelistN(&depths_peel);
1289 bool peelObjectsTransform(
1291 const float mval[2],
1292 const bool use_peel_object,
1294 float r_loc[3], float r_no[3], float *r_thickness)
1296 return peelObjectsSnapContext(
1297 t->tsnap.object_context,
1299 &(const struct SnapObjectParams){
1300 .snap_select = ((t->options & CTX_GPENCIL_STROKES) != 0) ? SNAP_NOT_ACTIVE : t->tsnap.modeSelect,
1301 .use_object_edit_cage = (t->flag & T_EDIT) != 0,
1304 r_loc, r_no, r_thickness);
1307 /******************** NODES ***********************************/
1309 static bool snapNodeTest(View2D *v2d, bNode *node, SnapSelect snap_select)
1311 /* node is use for snapping only if a) snap mode matches and b) node is inside the view */
1312 return ((snap_select == SNAP_NOT_SELECTED && !(node->flag & NODE_SELECT)) ||
1313 (snap_select == SNAP_ALL && !(node->flag & NODE_ACTIVE))) &&
1314 (node->totr.xmin < v2d->cur.xmax && node->totr.xmax > v2d->cur.xmin &&
1315 node->totr.ymin < v2d->cur.ymax && node->totr.ymax > v2d->cur.ymin);
1318 static NodeBorder snapNodeBorder(int snap_node_mode)
1320 switch (snap_node_mode) {
1321 case SCE_SNAP_MODE_NODE_X:
1322 return NODE_LEFT | NODE_RIGHT;
1323 case SCE_SNAP_MODE_NODE_Y:
1324 return NODE_TOP | NODE_BOTTOM;
1325 case SCE_SNAP_MODE_NODE_XY:
1326 return NODE_LEFT | NODE_RIGHT | NODE_TOP | NODE_BOTTOM;
1331 static bool snapNode(
1332 ToolSettings *ts, SpaceNode *UNUSED(snode), ARegion *ar, bNode *node, const int mval[2],
1333 float r_loc[2], float *r_dist_px, char *r_node_border)
1335 View2D *v2d = &ar->v2d;
1336 NodeBorder border = snapNodeBorder(ts->snap_node_mode);
1337 bool retval = false;
1341 UI_view2d_view_to_region_rcti(v2d, &node->totr, &totr);
1343 if (border & NODE_LEFT) {
1344 new_dist = abs(totr.xmin - mval[0]);
1345 if (new_dist < *r_dist_px) {
1346 UI_view2d_region_to_view(v2d, totr.xmin, mval[1], &r_loc[0], &r_loc[1]);
1347 *r_dist_px = new_dist;
1348 *r_node_border = NODE_LEFT;
1353 if (border & NODE_RIGHT) {
1354 new_dist = abs(totr.xmax - mval[0]);
1355 if (new_dist < *r_dist_px) {
1356 UI_view2d_region_to_view(v2d, totr.xmax, mval[1], &r_loc[0], &r_loc[1]);
1357 *r_dist_px = new_dist;
1358 *r_node_border = NODE_RIGHT;
1363 if (border & NODE_BOTTOM) {
1364 new_dist = abs(totr.ymin - mval[1]);
1365 if (new_dist < *r_dist_px) {
1366 UI_view2d_region_to_view(v2d, mval[0], totr.ymin, &r_loc[0], &r_loc[1]);
1367 *r_dist_px = new_dist;
1368 *r_node_border = NODE_BOTTOM;
1373 if (border & NODE_TOP) {
1374 new_dist = abs(totr.ymax - mval[1]);
1375 if (new_dist < *r_dist_px) {
1376 UI_view2d_region_to_view(v2d, mval[0], totr.ymax, &r_loc[0], &r_loc[1]);
1377 *r_dist_px = new_dist;
1378 *r_node_border = NODE_TOP;
1386 static bool snapNodes(
1387 ToolSettings *ts, SpaceNode *snode, ARegion *ar,
1388 const int mval[2], SnapSelect snap_select,
1389 float r_loc[2], float *r_dist_px, char *r_node_border)
1391 bNodeTree *ntree = snode->edittree;
1393 bool retval = false;
1397 for (node = ntree->nodes.first; node; node = node->next) {
1398 if (snapNodeTest(&ar->v2d, node, snap_select)) {
1399 retval |= snapNode(ts, snode, ar, node, mval, r_loc, r_dist_px, r_node_border);
1406 bool snapNodesTransform(
1407 TransInfo *t, const int mval[2], SnapSelect snap_select,
1408 float r_loc[2], float *r_dist_px, char *r_node_border)
1411 t->settings, t->sa->spacedata.first, t->ar, mval, snap_select,
1412 r_loc, r_dist_px, r_node_border);
1415 bool snapNodesContext(
1416 bContext *C, const int mval[2], SnapSelect snap_select,
1417 float r_loc[2], float *r_dist_px, char *r_node_border)
1419 Scene *scene = CTX_data_scene(C);
1420 ARegion *ar = CTX_wm_region(C);
1422 scene->toolsettings, CTX_wm_space_node(C), ar, mval, snap_select,
1423 r_loc, r_dist_px, r_node_border);
1426 /*================================================================*/
1428 static void applyGridIncrement(TransInfo *t, float *val, int max_index, const float fac[3], GearsType action);
1431 void snapGridIncrementAction(TransInfo *t, float *val, GearsType action)
1435 fac[NO_GEARS] = t->snap[0];
1436 fac[BIG_GEARS] = t->snap[1];
1437 fac[SMALL_GEARS] = t->snap[2];
1439 applyGridIncrement(t, val, t->idx_max, fac, action);
1443 void snapGridIncrement(TransInfo *t, float *val)
1447 /* only do something if using absolute or incremental grid snapping */
1448 if (!ELEM(t->tsnap.mode, SCE_SNAP_MODE_INCREMENT, SCE_SNAP_MODE_GRID))
1451 action = activeSnap(t) ? BIG_GEARS : NO_GEARS;
1453 if (action == BIG_GEARS && (t->modifiers & MOD_PRECISION)) {
1454 action = SMALL_GEARS;
1457 snapGridIncrementAction(t, val, action);
1460 void snapSequenceBounds(TransInfo *t, const int mval[2])
1462 float xmouse, ymouse;
1465 TransSeq *ts = t->custom.type.data;
1466 /* reuse increment, strictly speaking could be another snap mode, but leave as is */
1467 if (!(t->modifiers & MOD_SNAP_INVERT))
1470 /* convert to frame range */
1471 UI_view2d_region_to_view(&t->ar->v2d, mval[0], mval[1], &xmouse, &ymouse);
1472 mframe = iroundf(xmouse);
1473 /* now find the closest sequence */
1474 frame = BKE_sequencer_find_next_prev_edit(t->scene, mframe, SEQ_SIDE_BOTH, true, false, true);
1477 frame = frame - (ts->max - ts->min);
1479 t->values[0] = frame - ts->min;
1482 static void applyGridIncrement(TransInfo *t, float *val, int max_index, const float fac[3], GearsType action)
1484 float asp_local[3] = {1, 1, 1};
1485 const bool use_aspect = ELEM(t->mode, TFM_TRANSLATION);
1486 const float *asp = use_aspect ? t->aspect : asp_local;
1489 BLI_assert(ELEM(t->tsnap.mode, SCE_SNAP_MODE_INCREMENT, SCE_SNAP_MODE_GRID));
1490 BLI_assert(max_index <= 2);
1492 /* Early bailing out if no need to snap */
1493 if (fac[action] == 0.0f) {
1498 /* custom aspect for fcurve */
1499 if (t->spacetype == SPACE_IPO) {
1500 View2D *v2d = &t->ar->v2d;
1502 SpaceIpo *sipo = t->sa->spacedata.first;
1503 int unity = V2D_UNIT_VALUES;
1504 int unitx = (sipo->flag & SIPO_DRAWTIME) ? V2D_UNIT_SECONDS : V2D_UNIT_FRAMESCALE;
1507 grid = UI_view2d_grid_calc(t->scene, v2d, unitx, V2D_GRID_NOCLAMP, unity, V2D_GRID_NOCLAMP, t->ar->winx, t->ar->winy);
1509 UI_view2d_grid_size(grid, &asp_local[0], &asp_local[1]);
1510 UI_view2d_grid_free(grid);
1516 /* absolute snapping on grid based on global center */
1517 if ((t->tsnap.snap_spatial_grid) && (t->mode == TFM_TRANSLATION)) {
1518 const float *center_global = t->center_global;
1520 /* use a fallback for cursor selection,
1521 * this isn't useful as a global center for absolute grid snapping
1522 * since its not based on the position of the selection. */
1523 if (t->around == V3D_AROUND_CURSOR) {
1524 const TransCenterData *cd = transformCenter_from_type(t, V3D_AROUND_CENTER_MEAN);
1525 center_global = cd->global;
1528 for (i = 0; i <= max_index; i++) {
1529 /* do not let unconstrained axis jump to absolute grid increments */
1530 if (!(t->con.mode & CON_APPLY) || t->con.mode & (CON_AXIS0 << i)) {
1531 const float iter_fac = fac[action] * asp[i];
1532 val[i] = iter_fac * roundf((val[i] + center_global[i]) / iter_fac) - center_global[i];
1537 /* relative snapping in fixed increments */
1538 for (i = 0; i <= max_index; i++) {
1539 const float iter_fac = fac[action] * asp[i];
1540 val[i] = iter_fac * roundf(val[i] / iter_fac);