e1cf743623668207013c7cccd992cf69df0ead1c
[blender-staging.git] / source / blender / editors / transform / transform_snap.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
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.
8  *
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.
13  *
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.
17  *
18  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19  * All rights reserved.
20  *
21  * The Original Code is: all of this file.
22  *
23  * Contributor(s): Martin Poirier
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 /** \file blender/editors/transform/transform_snap.c
29  *  \ingroup edtransform
30  */
31
32 #include <stdlib.h>
33 #include <math.h>
34 #include <float.h>
35 #include <stdio.h>
36
37 #include "PIL_time.h"
38
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"
47
48 #include "BLI_math.h"
49 #include "BLI_blenlib.h"
50 #include "BLI_utildefines.h"
51
52 #include "BIF_gl.h"
53
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"
61 #include "BKE_main.h"
62
63 #include "RNA_access.h"
64
65 #include "WM_types.h"
66
67 #include "ED_image.h"
68 #include "ED_node.h"
69 #include "ED_uvedit.h"
70 #include "ED_view3d.h"
71 #include "ED_transform_snap_object_context.h"
72
73 #include "UI_resources.h"
74 #include "UI_view2d.h"
75
76 #include "MEM_guardedalloc.h"
77
78 #include "transform.h"
79
80 /* this should be passed as an arg for use in snap functions */
81 #undef BASACT
82
83 /* use half of flt-max so we can scale up without an exception */
84
85 /********************* PROTOTYPES ***********************/
86
87 static void setSnappingCallback(TransInfo *t);
88
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]);
92
93 /* static void CalcSnapGrid(TransInfo *t, float *vec); */
94 static void CalcSnapGeometry(TransInfo *t, float *vec);
95
96 static void TargetSnapMedian(TransInfo *t);
97 static void TargetSnapCenter(TransInfo *t);
98 static void TargetSnapClosest(TransInfo *t);
99 static void TargetSnapActive(TransInfo *t);
100
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]);
104
105
106 /****************** IMPLEMENTATIONS *********************/
107
108 static bool snapNodeTest(View2D *v2d, bNode *node, SnapSelect snap_select);
109 static NodeBorder snapNodeBorder(int snap_node_mode);
110
111 #if 0
112 int BIF_snappingSupported(Object *obedit)
113 {
114         int status = 0;
115         
116         /* only support object mesh, armature, curves */
117         if (obedit == NULL || ELEM(obedit->type, OB_MESH, OB_ARMATURE, OB_CURVE, OB_LATTICE, OB_MBALL)) {
118                 status = 1;
119         }
120         
121         return status;
122 }
123 #endif
124
125 bool validSnap(TransInfo *t)
126 {
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);
129 }
130
131 bool activeSnap(TransInfo *t)
132 {
133         return ((t->modifiers & (MOD_SNAP | MOD_SNAP_INVERT)) == MOD_SNAP) ||
134                ((t->modifiers & (MOD_SNAP | MOD_SNAP_INVERT)) == MOD_SNAP_INVERT);
135 }
136
137 void drawSnapping(const struct bContext *C, TransInfo *t)
138 {
139         unsigned char col[4], selectedCol[4], activeCol[4];
140         
141         if (!activeSnap(t))
142                 return;
143         
144         UI_GetThemeColor3ubv(TH_TRANSFORM, col);
145         col[3] = 128;
146         
147         UI_GetThemeColor3ubv(TH_SELECT, selectedCol);
148         selectedCol[3] = 128;
149         
150         UI_GetThemeColor3ubv(TH_ACTIVE, activeCol);
151         activeCol[3] = 192;
152         
153         if (t->spacetype == SPACE_VIEW3D) {
154                 if (validSnap(t)) {
155                         TransSnapPoint *p;
156                         View3D *v3d = CTX_wm_view3d(C);
157                         RegionView3D *rv3d = CTX_wm_region_view3d(C);
158                         float imat[4][4];
159                         float size;
160                         
161                         glDisable(GL_DEPTH_TEST);
162                         
163                         size = 2.5f * UI_GetThemeValuef(TH_VERTEX_SIZE);
164                         
165                         invert_m4_m4(imat, rv3d->viewmat);
166                         
167                         for (p = t->tsnap.points.first; p; p = p->next) {
168                                 if (p == t->tsnap.selectedPoint) {
169                                         glColor4ubv(selectedCol);
170                                 }
171                                 else {
172                                         glColor4ubv(col);
173                                 }
174                                 
175                                 drawcircball(GL_LINE_LOOP, p->co, ED_view3d_pixel_size(rv3d, p->co) * size * 0.75f, imat);
176                         }
177                         
178                         if (t->tsnap.status & POINT_INIT) {
179                                 glColor4ubv(activeCol);
180                                 
181                                 drawcircball(GL_LINE_LOOP, t->tsnap.snapPoint, ED_view3d_pixel_size(rv3d, t->tsnap.snapPoint) * size, imat);
182                         }
183                         
184                         /* draw normal if needed */
185                         if (usingSnappingNormal(t) && validSnappingNormal(t)) {
186                                 glColor4ubv(activeCol);
187                                 
188                                 glBegin(GL_LINES);
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]);
193                                 glEnd();
194                         }
195                         
196                         if (v3d->zbuf)
197                                 glEnable(GL_DEPTH_TEST);
198                 }
199         }
200         else if (t->spacetype == SPACE_IMAGE) {
201                 if (validSnap(t)) {
202                         /* This will not draw, and Im nor sure why - campbell */
203 #if 0
204                         float xuser_asp, yuser_asp;
205                         int wi, hi;
206                         float w, h;
207                         
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);
210                         glLoadIdentity();
211                         
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;
216                         
217                         cpack(0xFFFFFF);
218                         glTranslate2fv(t->tsnap.snapPoint);
219                         
220                         //glRectf(0, 0, 1, 1);
221                         
222                         setlinestyle(0);
223                         cpack(0x0);
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);
228                         
229                         glTranslatef(-t->tsnap.snapPoint[0], -t->tsnap.snapPoint[1], 0.0f);
230                         setlinestyle(0);
231 #endif
232                 }
233         }
234         else if (t->spacetype == SPACE_NODE) {
235                 if (validSnap(t)) {
236                         ARegion *ar = CTX_wm_region(C);
237                         TransSnapPoint *p;
238                         float size;
239                         
240                         size = 2.5f * UI_GetThemeValuef(TH_VERTEX_SIZE);
241                         
242                         glEnable(GL_BLEND);
243                         
244                         for (p = t->tsnap.points.first; p; p = p->next) {
245                                 if (p == t->tsnap.selectedPoint) {
246                                         glColor4ubv(selectedCol);
247                                 }
248                                 else {
249                                         glColor4ubv(col);
250                                 }
251                                 
252                                 ED_node_draw_snap(&ar->v2d, p->co, size, 0);
253                         }
254                         
255                         if (t->tsnap.status & POINT_INIT) {
256                                 glColor4ubv(activeCol);
257                                 
258                                 ED_node_draw_snap(&ar->v2d, t->tsnap.snapPoint, size, t->tsnap.snapNodeBorder);
259                         }
260                         
261                         glDisable(GL_BLEND);
262                 }
263         }
264 }
265
266 eRedrawFlag handleSnapping(TransInfo *t, const wmEvent *event)
267 {
268         eRedrawFlag status = TREDRAW_NOTHING;
269
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;
276         }
277 #endif
278         if (event->type == MOUSEMOVE) {
279                 status |= updateSelectedSnapPoint(t);
280         }
281         
282         return status;
283 }
284
285 void applyProject(TransInfo *t)
286 {
287         /* XXX FLICKER IN OBJECT MODE */
288         if ((t->tsnap.project) && activeSnap(t) && (t->flag & T_NO_PROJECT) == 0) {
289                 TransData *td = t->data;
290                 float tvec[3];
291                 float imat[4][4];
292                 int i;
293         
294                 if (t->flag & (T_EDIT | T_POSE)) {
295                         Object *ob = t->obedit ? t->obedit : t->poseobj;
296                         invert_m4_m4(imat, ob->obmat);
297                 }
298
299                 for (i = 0; i < t->total; i++, td++) {
300                         float iloc[3], loc[3], no[3];
301                         float mval_fl[2];
302                         float dist_px = TRANSFORM_DIST_MAX_PX;
303                         
304                         if (td->flag & TD_NOACTION)
305                                 break;
306                         
307                         if (td->flag & TD_SKIP)
308                                 continue;
309
310                         if ((t->flag & T_PROP_EDIT) && (td->factor == 0.0f))
311                                 continue;
312                         
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);
317                         }
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]);
323                         }
324                         
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,
328                                         loc, no))
329                                 {
330 //                                      if (t->flag & (T_EDIT|T_POSE)) {
331 //                                              mul_m4_v3(imat, loc);
332 //                                      }
333
334                                         sub_v3_v3v3(tvec, loc, iloc);
335
336                                         mul_m3_v3(td->smtx, tvec);
337
338                                         add_v3_v3(td->loc, tvec);
339
340                                         if (t->tsnap.align && (t->flag & T_OBJECT)) {
341                                                 /* handle alignment as well */
342                                                 const float *original_normal;
343                                                 float mat[3][3];
344
345                                                 /* In pose mode, we want to align normals with Y axis of bones... */
346                                                 original_normal = td->axismtx[2];
347
348                                                 rotation_between_vecs_to_mat3(mat, original_normal, no);
349
350                                                 transform_data_ext_rotate(td, mat, true);
351
352                                                 /* TODO support constraints for rotation too? see ElementRotation */
353                                         }
354                                 }
355                         }
356                         
357                         //XXX constraintTransLim(t, td);
358                 }
359         }
360 }
361
362 void applyGridAbsolute(TransInfo *t)
363 {
364         float grid_size = 0.0f;
365         GearsType grid_action;
366         TransData *td;
367         float (*obmat)[4] = NULL;
368         bool use_obmat = false;
369         int i;
370         
371         if (!(activeSnap(t) && (ELEM(t->tsnap.mode, SCE_SNAP_MODE_INCREMENT, SCE_SNAP_MODE_GRID))))
372                 return;
373         
374         grid_action = BIG_GEARS;
375         if (t->modifiers & MOD_PRECISION)
376                 grid_action = SMALL_GEARS;
377         
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;
382         }
383         /* early exit on unusable grid size */
384         if (grid_size == 0.0f)
385                 return;
386         
387         if (t->flag & (T_EDIT | T_POSE)) {
388                 Object *ob = t->obedit ? t->obedit : t->poseobj;
389                 obmat = ob->obmat;
390                 use_obmat = true;
391         }
392         
393         for (i = 0, td = t->data; i < t->total; i++, td++) {
394                 float iloc[3], loc[3], tvec[3];
395                 
396                 if (td->flag & TD_NOACTION)
397                         break;
398                 
399                 if (td->flag & TD_SKIP)
400                         continue;
401                 
402                 if ((t->flag & T_PROP_EDIT) && (td->factor == 0.0f))
403                         continue;
404                 
405                 copy_v3_v3(iloc, td->loc);
406                 if (use_obmat) {
407                         mul_m4_v3(obmat, iloc);
408                 }
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]);
413                 }
414                 
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);
420
421                 sub_v3_v3v3(tvec, loc, iloc);
422                 mul_m3_v3(td->smtx, tvec);
423                 add_v3_v3(td->loc, tvec);
424         }
425 }
426
427 void applySnapping(TransInfo *t, float *vec)
428 {
429         /* project is not applied this way */
430         if (t->tsnap.project)
431                 return;
432         
433         if (t->tsnap.status & SNAP_FORCED) {
434                 t->tsnap.targetSnap(t);
435         
436                 t->tsnap.applySnap(t, vec);
437         }
438         else if (!ELEM(t->tsnap.mode, SCE_SNAP_MODE_INCREMENT, SCE_SNAP_MODE_GRID) && activeSnap(t)) {
439                 double current = PIL_check_seconds_timer();
440                 
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);
446         
447                         t->tsnap.last = current;
448                 }
449                 if (validSnap(t)) {
450                         t->tsnap.applySnap(t, vec);
451                 }
452         }
453 }
454
455 void resetSnapping(TransInfo *t)
456 {
457         t->tsnap.status = 0;
458         t->tsnap.align = false;
459         t->tsnap.project = 0;
460         t->tsnap.mode = 0;
461         t->tsnap.modeSelect = 0;
462         t->tsnap.target = 0;
463         t->tsnap.last = 0;
464         t->tsnap.applySnap = NULL;
465
466         t->tsnap.snapNormal[0] = 0;
467         t->tsnap.snapNormal[1] = 0;
468         t->tsnap.snapNormal[2] = 0;
469         
470         t->tsnap.snapNodeBorder = 0;
471 }
472
473 bool usingSnappingNormal(TransInfo *t)
474 {
475         return t->tsnap.align;
476 }
477
478 bool validSnappingNormal(TransInfo *t)
479 {
480         if (validSnap(t)) {
481                 if (!is_zero_v3(t->tsnap.snapNormal)) {
482                         return true;
483                 }
484         }
485         
486         return false;
487 }
488
489 static bool bm_edge_is_snap_target(BMEdge *e, void *UNUSED(user_data))
490 {
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))
494         {
495                 return false;
496         }
497
498         return true;
499 }
500
501 static bool bm_face_is_snap_target(BMFace *f, void *UNUSED(user_data))
502 {
503         if (BM_elem_flag_test(f, BM_ELEM_SELECT | BM_ELEM_HIDDEN)) {
504                 return false;
505         }
506
507         BMLoop *l_iter, *l_first;
508         l_iter = l_first = BM_FACE_FIRST_LOOP(f);
509         do {
510                 if (BM_elem_flag_test(l_iter->v, BM_ELEM_SELECT)) {
511                         return false;
512                 }
513         } while ((l_iter = l_iter->next) != l_first);
514
515         return true;
516 }
517
518 static void initSnappingMode(TransInfo *t)
519 {
520         ToolSettings *ts = t->settings;
521         Object *obedit = t->obedit;
522         Scene *scene = t->scene;
523         Base *base_act = scene->basact;
524
525         if (t->spacetype == SPACE_NODE) {
526                 /* force project off when not supported */
527                 t->tsnap.project = 0;
528                 
529                 t->tsnap.mode = ts->snap_node_mode;
530         }
531         else if (t->spacetype == SPACE_IMAGE) {
532                 /* force project off when not supported */
533                 t->tsnap.project = 0;
534                 
535                 t->tsnap.mode = ts->snap_uv_mode;
536         }
537         else {
538                 /* force project off when not supported */
539                 if (ts->snap_mode != SCE_SNAP_MODE_FACE)
540                         t->tsnap.project = 0;
541                 
542                 t->tsnap.mode = ts->snap_mode;
543         }
544
545         if ((t->spacetype == SPACE_VIEW3D || t->spacetype == SPACE_IMAGE) &&  /* Only 3D view or UV */
546             (t->flag & T_CAMERA) == 0)  /* Not with camera selected in camera view */
547         {
548                 setSnappingCallback(t);
549
550                 /* Edit mode */
551                 if (t->tsnap.applySnap != NULL && // A snapping function actually exist
552                     (obedit != NULL && ELEM(obedit->type, OB_MESH, OB_ARMATURE, OB_CURVE, OB_LATTICE, OB_MBALL)) ) // Temporary limited to edit mode meshes, armature, curves, mballs
553                 {
554                         /* Exclude editmesh if using proportional edit */
555                         if ((obedit->type == OB_MESH) && (t->flag & T_PROP_EDIT)) {
556                                 t->tsnap.modeSelect = SNAP_NOT_ACTIVE;
557                         }
558                         else {
559                                 t->tsnap.modeSelect = t->tsnap.snap_self ? SNAP_ALL : SNAP_NOT_ACTIVE;
560                         }
561                 }
562                 /* Particles edit mode*/
563                 else if (t->tsnap.applySnap != NULL && // A snapping function actually exist
564                          (obedit == NULL && base_act && base_act->object && base_act->object->mode & OB_MODE_PARTICLE_EDIT))
565                 {
566                         t->tsnap.modeSelect = SNAP_ALL;
567                 }
568                 /* Object mode */
569                 else if (t->tsnap.applySnap != NULL && // A snapping function actually exist
570                          (obedit == NULL) ) // Object Mode
571                 {
572                         t->tsnap.modeSelect = SNAP_NOT_SELECTED;
573                 }
574                 else {
575                         /* Grid if snap is not possible */
576                         t->tsnap.mode = SCE_SNAP_MODE_INCREMENT;
577                 }
578         }
579         else if (t->spacetype == SPACE_NODE) {
580                 setSnappingCallback(t);
581                 
582                 if (t->tsnap.applySnap != NULL) {
583                         t->tsnap.modeSelect = SNAP_NOT_SELECTED;
584                 }
585                 else {
586                         /* Grid if snap is not possible */
587                         t->tsnap.mode = SCE_SNAP_MODE_INCREMENT;
588                 }
589         }
590         else if (t->spacetype == SPACE_SEQ) {
591                 /* We do our own snapping currently, so nothing here */
592                 t->tsnap.mode = SCE_SNAP_MODE_GRID;  /* Dummy, should we rather add a NOP mode? */
593         }
594         else {
595                 /* Always grid outside of 3D view */
596                 t->tsnap.mode = SCE_SNAP_MODE_INCREMENT;
597         }
598
599         if (t->spacetype == SPACE_VIEW3D) {
600                 if (t->tsnap.object_context == NULL) {
601                         t->tsnap.object_context = ED_transform_snap_object_context_create_view3d(
602                                         G.main, t->scene, SNAP_OBJECT_USE_CACHE,
603                                         t->ar, t->view);
604
605                         ED_transform_snap_object_context_set_editmesh_callbacks(
606                                         t->tsnap.object_context,
607                                         (bool (*)(BMVert *, void *))BM_elem_cb_check_hflag_disabled,
608                                         bm_edge_is_snap_target,
609                                         bm_face_is_snap_target,
610                                         SET_UINT_IN_POINTER((BM_ELEM_SELECT | BM_ELEM_HIDDEN)));
611                 }
612         }
613 }
614
615 void initSnapping(TransInfo *t, wmOperator *op)
616 {
617         ToolSettings *ts = t->settings;
618         short snap_target = t->settings->snap_target;
619         
620         resetSnapping(t);
621         
622         /* if snap property exists */
623         if (op && RNA_struct_find_property(op->ptr, "snap") && RNA_struct_property_is_set(op->ptr, "snap")) {
624                 if (RNA_boolean_get(op->ptr, "snap")) {
625                         t->modifiers |= MOD_SNAP;
626
627                         if (RNA_struct_property_is_set(op->ptr, "snap_target")) {
628                                 snap_target = RNA_enum_get(op->ptr, "snap_target");
629                         }
630                         
631                         if (RNA_struct_property_is_set(op->ptr, "snap_point")) {
632                                 RNA_float_get_array(op->ptr, "snap_point", t->tsnap.snapPoint);
633                                 t->tsnap.status |= SNAP_FORCED | POINT_INIT;
634                         }
635                         
636                         /* snap align only defined in specific cases */
637                         if (RNA_struct_find_property(op->ptr, "snap_align")) {
638                                 t->tsnap.align = RNA_boolean_get(op->ptr, "snap_align");
639                                 RNA_float_get_array(op->ptr, "snap_normal", t->tsnap.snapNormal);
640                                 normalize_v3(t->tsnap.snapNormal);
641                         }
642
643                         if (RNA_struct_find_property(op->ptr, "use_snap_project")) {
644                                 t->tsnap.project = RNA_boolean_get(op->ptr, "use_snap_project");
645                         }
646
647                         if (RNA_struct_find_property(op->ptr, "use_snap_self")) {
648                                 t->tsnap.snap_self = RNA_boolean_get(op->ptr, "use_snap_self");
649                         }
650                 }
651         }
652         /* use scene defaults only when transform is modal */
653         else if (t->flag & T_MODAL) {
654                 if (ELEM(t->spacetype, SPACE_VIEW3D, SPACE_IMAGE, SPACE_NODE)) {
655                         if (ts->snap_flag & SCE_SNAP) {
656                                 t->modifiers |= MOD_SNAP;
657                         }
658
659                         t->tsnap.align = ((t->settings->snap_flag & SCE_SNAP_ROTATE) != 0);
660                         t->tsnap.project = ((t->settings->snap_flag & SCE_SNAP_PROJECT) != 0);
661                         t->tsnap.snap_self = !((t->settings->snap_flag & SCE_SNAP_NO_SELF) != 0);
662                         t->tsnap.peel = ((t->settings->snap_flag & SCE_SNAP_PROJECT) != 0);
663                 }
664
665                 /* for now only 3d view (others can be added if we want) */
666                 if (t->spacetype == SPACE_VIEW3D) {
667                         t->tsnap.snap_spatial_grid = ((t->settings->snap_flag & SCE_SNAP_ABS_GRID) != 0);
668                 }
669         }
670         
671         t->tsnap.target = snap_target;
672
673         initSnappingMode(t);
674 }
675
676 void freeSnapping(TransInfo *t)
677 {
678         if (t->tsnap.object_context) {
679                 ED_transform_snap_object_context_destroy(t->tsnap.object_context);
680                 t->tsnap.object_context = NULL;
681         }
682 }
683
684 static void setSnappingCallback(TransInfo *t)
685 {
686         t->tsnap.calcSnap = CalcSnapGeometry;
687
688         switch (t->tsnap.target) {
689                 case SCE_SNAP_TARGET_CLOSEST:
690                         t->tsnap.targetSnap = TargetSnapClosest;
691                         break;
692                 case SCE_SNAP_TARGET_CENTER:
693                         t->tsnap.targetSnap = TargetSnapCenter;
694                         break;
695                 case SCE_SNAP_TARGET_MEDIAN:
696                         t->tsnap.targetSnap = TargetSnapMedian;
697                         break;
698                 case SCE_SNAP_TARGET_ACTIVE:
699                         t->tsnap.targetSnap = TargetSnapActive;
700                         break;
701
702         }
703
704         switch (t->mode) {
705                 case TFM_TRANSLATION:
706                         t->tsnap.applySnap = ApplySnapTranslation;
707                         t->tsnap.distance = TranslationBetween;
708                         break;
709                 case TFM_ROTATION:
710                         t->tsnap.applySnap = ApplySnapRotation;
711                         t->tsnap.distance = RotationBetween;
712
713                         // Can't do TARGET_CENTER with rotation, use TARGET_MEDIAN instead
714                         if (t->tsnap.target == SCE_SNAP_TARGET_CENTER) {
715                                 t->tsnap.target = SCE_SNAP_TARGET_MEDIAN;
716                                 t->tsnap.targetSnap = TargetSnapMedian;
717                         }
718                         break;
719                 case TFM_RESIZE:
720                         t->tsnap.applySnap = ApplySnapResize;
721                         t->tsnap.distance = ResizeBetween;
722
723                         // Can't do TARGET_CENTER with resize, use TARGET_MEDIAN instead
724                         if (t->tsnap.target == SCE_SNAP_TARGET_CENTER) {
725                                 t->tsnap.target = SCE_SNAP_TARGET_MEDIAN;
726                                 t->tsnap.targetSnap = TargetSnapMedian;
727                         }
728                         break;
729                 default:
730                         t->tsnap.applySnap = NULL;
731                         break;
732         }
733 }
734
735 void addSnapPoint(TransInfo *t)
736 {
737         /* Currently only 3D viewport works for snapping points. */
738         if (t->tsnap.status & POINT_INIT && t->spacetype == SPACE_VIEW3D) {
739                 TransSnapPoint *p = MEM_callocN(sizeof(TransSnapPoint), "SnapPoint");
740
741                 t->tsnap.selectedPoint = p;
742
743                 copy_v3_v3(p->co, t->tsnap.snapPoint);
744
745                 BLI_addtail(&t->tsnap.points, p);
746
747                 t->tsnap.status |= MULTI_POINTS;
748         }
749 }
750
751 eRedrawFlag updateSelectedSnapPoint(TransInfo *t)
752 {
753         eRedrawFlag status = TREDRAW_NOTHING;
754
755         if (t->tsnap.status & MULTI_POINTS) {
756                 TransSnapPoint *p, *closest_p = NULL;
757                 float dist_min_sq = TRANSFORM_SNAP_MAX_PX;
758                 const float mval_fl[2] = {t->mval[0], t->mval[1]};
759                 float screen_loc[2];
760
761                 for (p = t->tsnap.points.first; p; p = p->next) {
762                         float dist_sq;
763
764                         if (ED_view3d_project_float_global(t->ar, p->co, screen_loc, V3D_PROJ_TEST_NOP) != V3D_PROJ_RET_OK) {
765                                 continue;
766                         }
767
768                         dist_sq = len_squared_v2v2(mval_fl, screen_loc);
769
770                         if (dist_sq < dist_min_sq) {
771                                 closest_p = p;
772                                 dist_min_sq = dist_sq;
773                         }
774                 }
775
776                 if (closest_p) {
777                         if (t->tsnap.selectedPoint != closest_p) {
778                                 status = TREDRAW_HARD;
779                         }
780
781                         t->tsnap.selectedPoint = closest_p;
782                 }
783         }
784
785         return status;
786 }
787
788 void removeSnapPoint(TransInfo *t)
789 {
790         if (t->tsnap.status & MULTI_POINTS) {
791                 updateSelectedSnapPoint(t);
792
793                 if (t->tsnap.selectedPoint) {
794                         BLI_freelinkN(&t->tsnap.points, t->tsnap.selectedPoint);
795
796                         if (BLI_listbase_is_empty(&t->tsnap.points)) {
797                                 t->tsnap.status &= ~MULTI_POINTS;
798                         }
799
800                         t->tsnap.selectedPoint = NULL;
801                 }
802
803         }
804 }
805
806 void getSnapPoint(TransInfo *t, float vec[3])
807 {
808         if (t->tsnap.points.first) {
809                 TransSnapPoint *p;
810                 int total = 0;
811
812                 vec[0] = vec[1] = vec[2] = 0;
813
814                 for (p = t->tsnap.points.first; p; p = p->next, total++) {
815                         add_v3_v3(vec, p->co);
816                 }
817
818                 if (t->tsnap.status & POINT_INIT) {
819                         add_v3_v3(vec, t->tsnap.snapPoint);
820                         total++;
821                 }
822
823                 mul_v3_fl(vec, 1.0f / total);
824         }
825         else {
826                 copy_v3_v3(vec, t->tsnap.snapPoint);
827         }
828 }
829
830 /********************** APPLY **************************/
831
832 static void ApplySnapTranslation(TransInfo *t, float vec[3])
833 {
834         float point[3];
835         getSnapPoint(t, point);
836
837         if (t->spacetype == SPACE_NODE) {
838                 char border = t->tsnap.snapNodeBorder;
839                 if (border & (NODE_LEFT | NODE_RIGHT))
840                         vec[0] = point[0] - t->tsnap.snapTarget[0];
841                 if (border & (NODE_BOTTOM | NODE_TOP))
842                         vec[1] = point[1] - t->tsnap.snapTarget[1];
843         }
844         else {
845                 sub_v3_v3v3(vec, point, t->tsnap.snapTarget);
846         }
847 }
848
849 static void ApplySnapRotation(TransInfo *t, float *value)
850 {
851         float point[3];
852         getSnapPoint(t, point);
853
854         float dist = RotationBetween(t, t->tsnap.snapTarget, point);
855         *value = dist;
856 }
857
858 static void ApplySnapResize(TransInfo *t, float vec[3])
859 {
860         float point[3];
861         getSnapPoint(t, point);
862
863         float dist = ResizeBetween(t, t->tsnap.snapTarget, point);
864         copy_v3_fl(vec, dist);
865 }
866
867 /********************** DISTANCE **************************/
868
869 static float TranslationBetween(TransInfo *UNUSED(t), const float p1[3], const float p2[3])
870 {
871         return len_squared_v3v3(p1, p2);
872 }
873
874 static float RotationBetween(TransInfo *t, const float p1[3], const float p2[3])
875 {
876         float angle, start[3], end[3];
877
878         sub_v3_v3v3(start, p1, t->center_global);
879         sub_v3_v3v3(end,   p2, t->center_global);
880                 
881         // Angle around a constraint axis (error prone, will need debug)
882         if (t->con.applyRot != NULL && (t->con.mode & CON_APPLY)) {
883                 float axis[3], tmp[3];
884                 
885                 t->con.applyRot(t, NULL, axis, NULL);
886
887                 project_v3_v3v3(tmp, end, axis);
888                 sub_v3_v3v3(end, end, tmp);
889                 
890                 project_v3_v3v3(tmp, start, axis);
891                 sub_v3_v3v3(start, start, tmp);
892                 
893                 normalize_v3(end);
894                 normalize_v3(start);
895                 
896                 cross_v3_v3v3(tmp, start, end);
897                 
898                 if (dot_v3v3(tmp, axis) < 0.0f)
899                         angle = -acosf(dot_v3v3(start, end));
900                 else
901                         angle = acosf(dot_v3v3(start, end));
902         }
903         else {
904                 float mtx[3][3];
905                 
906                 copy_m3_m4(mtx, t->viewmat);
907
908                 mul_m3_v3(mtx, end);
909                 mul_m3_v3(mtx, start);
910                 
911                 angle = atan2f(start[1], start[0]) - atan2f(end[1], end[0]);
912         }
913         
914         if (angle > (float)M_PI) {
915                 angle = angle - 2 * (float)M_PI;
916         }
917         else if (angle < -((float)M_PI)) {
918                 angle = 2.0f * (float)M_PI + angle;
919         }
920         
921         return angle;
922 }
923
924 static float ResizeBetween(TransInfo *t, const float p1[3], const float p2[3])
925 {
926         float d1[3], d2[3], len_d1;
927
928         sub_v3_v3v3(d1, p1, t->center_global);
929         sub_v3_v3v3(d2, p2, t->center_global);
930
931         if (t->con.applyRot != NULL && (t->con.mode & CON_APPLY)) {
932                 mul_m3_v3(t->con.pmtx, d1);
933                 mul_m3_v3(t->con.pmtx, d2);
934         }
935
936         project_v3_v3v3(d1, d1, d2);
937         
938         len_d1 = len_v3(d1);
939
940         /* Use 'invalid' dist when `center == p1` (after projecting),
941          * in this case scale will _never_ move the point in relation to the center,
942          * so it makes no sense to take it into account when scaling. see: T46503 */
943         return len_d1 != 0.0f ? len_v3(d2) / len_d1 : TRANSFORM_DIST_INVALID;
944 }
945
946 /********************** CALC **************************/
947
948 static void UNUSED_FUNCTION(CalcSnapGrid) (TransInfo *t, float *UNUSED(vec))
949 {
950         snapGridIncrementAction(t, t->tsnap.snapPoint, BIG_GEARS);
951 }
952
953 static void CalcSnapGeometry(TransInfo *t, float *UNUSED(vec))
954 {
955         if (t->spacetype == SPACE_VIEW3D) {
956                 float loc[3];
957                 float no[3];
958                 float mval[2];
959                 bool found = false;
960                 float dist_px = SNAP_MIN_DISTANCE; // Use a user defined value here
961                 
962                 mval[0] = t->mval[0];
963                 mval[1] = t->mval[1];
964                 
965                 if (t->tsnap.mode == SCE_SNAP_MODE_VOLUME) {
966                         found = peelObjectsTransform(
967                                 t, mval,
968                                 (t->settings->snap_flag & SCE_SNAP_PEEL_OBJECT) != 0,
969                                 loc, no, NULL);
970                 }
971                 else {
972                         zero_v3(no);  /* objects won't set this */
973                         found = snapObjectsTransform(
974                                 t, mval, &dist_px,
975                                 loc, no);
976                 }
977                 
978                 if (found == true) {
979                         float tangent[3];
980                         
981                         sub_v2_v2v2(tangent, loc, t->tsnap.snapPoint);
982                         tangent[2] = 0.0f;
983                         
984                         if (!is_zero_v3(tangent)) {
985                                 copy_v3_v3(t->tsnap.snapTangent, tangent);
986                         }
987                         
988                         copy_v3_v3(t->tsnap.snapPoint, loc);
989                         copy_v3_v3(t->tsnap.snapNormal, no);
990
991                         t->tsnap.status |=  POINT_INIT;
992                 }
993                 else {
994                         t->tsnap.status &= ~POINT_INIT;
995                 }
996         }
997         else if (t->spacetype == SPACE_IMAGE && t->obedit != NULL && t->obedit->type == OB_MESH) {
998                 /* same as above but for UV's */
999                 Image *ima = ED_space_image(t->sa->spacedata.first);
1000                 float co[2];
1001                 
1002                 UI_view2d_region_to_view(&t->ar->v2d, t->mval[0], t->mval[1], &co[0], &co[1]);
1003
1004                 if (ED_uvedit_nearest_uv(t->scene, t->obedit, ima, co, t->tsnap.snapPoint)) {
1005                         t->tsnap.snapPoint[0] *= t->aspect[0];
1006                         t->tsnap.snapPoint[1] *= t->aspect[1];
1007
1008                         t->tsnap.status |=  POINT_INIT;
1009                 }
1010                 else {
1011                         t->tsnap.status &= ~POINT_INIT;
1012                 }
1013         }
1014         else if (t->spacetype == SPACE_NODE) {
1015                 float loc[2];
1016                 float dist_px = SNAP_MIN_DISTANCE; // Use a user defined value here
1017                 char node_border;
1018                 
1019                 if (snapNodesTransform(t, t->mval, t->tsnap.modeSelect, loc, &dist_px, &node_border)) {
1020                         copy_v2_v2(t->tsnap.snapPoint, loc);
1021                         t->tsnap.snapNodeBorder = node_border;
1022                         
1023                         t->tsnap.status |=  POINT_INIT;
1024                 }
1025                 else {
1026                         t->tsnap.status &= ~POINT_INIT;
1027                 }
1028         }
1029 }
1030
1031 /********************** TARGET **************************/
1032
1033 static void TargetSnapOffset(TransInfo *t, TransData *td)
1034 {
1035         if (t->spacetype == SPACE_NODE && td != NULL) {
1036                 bNode *node = td->extra;
1037                 char border = t->tsnap.snapNodeBorder;
1038                 float width  = BLI_rctf_size_x(&node->totr);
1039                 float height = BLI_rctf_size_y(&node->totr);
1040                 
1041 #ifdef USE_NODE_CENTER
1042                 if (border & NODE_LEFT)
1043                         t->tsnap.snapTarget[0] -= 0.5f * width;
1044                 if (border & NODE_RIGHT)
1045                         t->tsnap.snapTarget[0] += 0.5f * width;
1046                 if (border & NODE_BOTTOM)
1047                         t->tsnap.snapTarget[1] -= 0.5f * height;
1048                 if (border & NODE_TOP)
1049                         t->tsnap.snapTarget[1] += 0.5f * height;
1050 #else
1051                 if (border & NODE_LEFT)
1052                         t->tsnap.snapTarget[0] -= 0.0f;
1053                 if (border & NODE_RIGHT)
1054                         t->tsnap.snapTarget[0] += width;
1055                 if (border & NODE_BOTTOM)
1056                         t->tsnap.snapTarget[1] -= height;
1057                 if (border & NODE_TOP)
1058                         t->tsnap.snapTarget[1] += 0.0f;
1059 #endif
1060         }
1061 }
1062
1063 static void TargetSnapCenter(TransInfo *t)
1064 {
1065         /* Only need to calculate once */
1066         if ((t->tsnap.status & TARGET_INIT) == 0) {
1067                 copy_v3_v3(t->tsnap.snapTarget, t->center_global);
1068                 TargetSnapOffset(t, NULL);
1069                 
1070                 t->tsnap.status |= TARGET_INIT;
1071         }
1072 }
1073
1074 static void TargetSnapActive(TransInfo *t)
1075 {
1076         /* Only need to calculate once */
1077         if ((t->tsnap.status & TARGET_INIT) == 0) {
1078                 if (calculateCenterActive(t, true, t->tsnap.snapTarget)) {
1079                         if (t->flag & (T_EDIT | T_POSE)) {
1080                                 Object *ob = t->obedit ? t->obedit : t->poseobj;
1081                                 mul_m4_v3(ob->obmat, t->tsnap.snapTarget);
1082                         }
1083
1084                         TargetSnapOffset(t, NULL);
1085
1086                         t->tsnap.status |= TARGET_INIT;
1087                 }
1088                 /* No active, default to median */
1089                 else {
1090                         t->tsnap.target = SCE_SNAP_TARGET_MEDIAN;
1091                         t->tsnap.targetSnap = TargetSnapMedian;
1092                         TargetSnapMedian(t);
1093                 }
1094         }
1095 }
1096
1097 static void TargetSnapMedian(TransInfo *t)
1098 {
1099         // Only need to calculate once
1100         if ((t->tsnap.status & TARGET_INIT) == 0) {
1101                 TransData *td = NULL;
1102                 int i;
1103
1104                 t->tsnap.snapTarget[0] = 0;
1105                 t->tsnap.snapTarget[1] = 0;
1106                 t->tsnap.snapTarget[2] = 0;
1107                 
1108                 for (td = t->data, i = 0; i < t->total && td->flag & TD_SELECTED; i++, td++) {
1109                         add_v3_v3(t->tsnap.snapTarget, td->center);
1110                 }
1111                 
1112                 mul_v3_fl(t->tsnap.snapTarget, 1.0 / i);
1113                 
1114                 if (t->flag & (T_EDIT | T_POSE)) {
1115                         Object *ob = t->obedit ? t->obedit : t->poseobj;
1116                         mul_m4_v3(ob->obmat, t->tsnap.snapTarget);
1117                 }
1118                 
1119                 TargetSnapOffset(t, NULL);
1120                 
1121                 t->tsnap.status |= TARGET_INIT;
1122         }
1123 }
1124
1125 static void TargetSnapClosest(TransInfo *t)
1126 {
1127         // Only valid if a snap point has been selected
1128         if (t->tsnap.status & POINT_INIT) {
1129                 float dist_closest = 0.0f;
1130                 TransData *closest = NULL, *td = NULL;
1131                 
1132                 /* Object mode */
1133                 if (t->flag & T_OBJECT) {
1134                         int i;
1135                         for (td = t->data, i = 0; i < t->total && td->flag & TD_SELECTED; i++, td++) {
1136                                 struct BoundBox *bb = BKE_object_boundbox_get(td->ob);
1137                                 
1138                                 /* use boundbox if possible */
1139                                 if (bb) {
1140                                         int j;
1141                                         
1142                                         for (j = 0; j < 8; j++) {
1143                                                 float loc[3];
1144                                                 float dist;
1145                                                 
1146                                                 copy_v3_v3(loc, bb->vec[j]);
1147                                                 mul_m4_v3(td->ext->obmat, loc);
1148                                                 
1149                                                 dist = t->tsnap.distance(t, loc, t->tsnap.snapPoint);
1150
1151                                                 if ((dist != TRANSFORM_DIST_INVALID) &&
1152                                                     (closest == NULL || fabsf(dist) < fabsf(dist_closest)))
1153                                                 {
1154                                                         copy_v3_v3(t->tsnap.snapTarget, loc);
1155                                                         closest = td;
1156                                                         dist_closest = dist;
1157                                                 }
1158                                         }
1159                                 }
1160                                 /* use element center otherwise */
1161                                 else {
1162                                         float loc[3];
1163                                         float dist;
1164                                         
1165                                         copy_v3_v3(loc, td->center);
1166                                         
1167                                         dist = t->tsnap.distance(t, loc, t->tsnap.snapPoint);
1168
1169                                         if ((dist != TRANSFORM_DIST_INVALID) &&
1170                                             (closest == NULL || fabsf(dist) < fabsf(dist_closest)))
1171                                         {
1172                                                 copy_v3_v3(t->tsnap.snapTarget, loc);
1173                                                 closest = td;
1174                                         }
1175                                 }
1176                         }
1177                 }
1178                 else {
1179                         int i;
1180                         for (td = t->data, i = 0; i < t->total && td->flag & TD_SELECTED; i++, td++) {
1181                                 float loc[3];
1182                                 float dist;
1183                                 
1184                                 copy_v3_v3(loc, td->center);
1185                                 
1186                                 if (t->flag & (T_EDIT | T_POSE)) {
1187                                         Object *ob = t->obedit ? t->obedit : t->poseobj;
1188                                         mul_m4_v3(ob->obmat, loc);
1189                                 }
1190                                 
1191                                 dist = t->tsnap.distance(t, loc, t->tsnap.snapPoint);
1192                                 
1193                                 if ((dist != TRANSFORM_DIST_INVALID) &&
1194                                     (closest == NULL || fabsf(dist) < fabsf(dist_closest)))
1195                                 {
1196                                         copy_v3_v3(t->tsnap.snapTarget, loc);
1197                                         closest = td;
1198                                         dist_closest = dist;
1199                                 }
1200                         }
1201                 }
1202                 
1203                 TargetSnapOffset(t, closest);
1204                 
1205                 t->tsnap.status |= TARGET_INIT;
1206         }
1207 }
1208
1209 bool snapObjectsTransform(
1210         TransInfo *t, const float mval[2],
1211         float *dist_px,
1212         float r_loc[3], float r_no[3])
1213 {
1214         return ED_transform_snap_object_project_view3d_ex(
1215                 t->tsnap.object_context,
1216                 t->scene->toolsettings->snap_mode,
1217                 &(const struct SnapObjectParams){
1218                     .snap_select = ((t->options & CTX_GPENCIL_STROKES) != 0) ? SNAP_NOT_ACTIVE : t->tsnap.modeSelect,
1219                     .use_object_edit_cage = (t->flag & T_EDIT) != 0,
1220                 },
1221                 mval, dist_px, NULL,
1222                 r_loc, r_no, NULL);
1223 }
1224
1225
1226 /******************** PEELING *********************************/
1227
1228 bool peelObjectsSnapContext(
1229         SnapObjectContext *sctx,
1230         const float mval[2],
1231         const struct SnapObjectParams *params,
1232         const bool use_peel_object,
1233         /* return args */
1234         float r_loc[3], float r_no[3], float *r_thickness)
1235 {
1236         ListBase depths_peel = {0};
1237         ED_transform_snap_object_project_all_view3d_ex(
1238                 sctx,
1239                 params,
1240                 mval, -1.0f, false,
1241                 &depths_peel);
1242
1243         if (!BLI_listbase_is_empty(&depths_peel)) {
1244                 /* At the moment we only use the hits of the first object */
1245                 struct SnapObjectHitDepth *hit_min = depths_peel.first;
1246                 for (struct SnapObjectHitDepth *iter = hit_min->next; iter; iter = iter->next) {
1247                         if (iter->depth < hit_min->depth) {
1248                                 hit_min = iter;
1249                         }
1250                 }
1251                 struct SnapObjectHitDepth *hit_max = NULL;
1252
1253                 if (use_peel_object) {
1254                         /* if peeling objects, take the first and last from each object */
1255                         hit_max = hit_min;
1256                         for (struct SnapObjectHitDepth *iter = depths_peel.first; iter; iter = iter->next) {
1257                                 if ((iter->depth > hit_max->depth) && (iter->ob_uuid == hit_min->ob_uuid)) {
1258                                         hit_max = iter;
1259                                 }
1260                         }
1261                 }
1262                 else {
1263                         /* otherwise, pair first with second and so on */
1264                         for (struct SnapObjectHitDepth *iter = depths_peel.first; iter; iter = iter->next) {
1265                                 if ((iter != hit_min) && (iter->ob_uuid == hit_min->ob_uuid)) {
1266                                         if (hit_max == NULL) {
1267                                                 hit_max = iter;
1268                                         }
1269                                         else if (iter->depth < hit_max->depth) {
1270                                                 hit_max = iter;
1271                                         }
1272                                 }
1273                         }
1274                         /* in this case has only one hit. treat as raycast */
1275                         if (hit_max == NULL) {
1276                                 hit_max = hit_min;
1277                         }
1278                 }
1279
1280                 mid_v3_v3v3(r_loc, hit_min->co, hit_max->co);
1281
1282                 if (r_thickness) {
1283                         *r_thickness = hit_max->depth - hit_min->depth;
1284                 }
1285
1286                 /* XXX, is there a correct normal in this case ???, for now just z up */
1287                 r_no[0] = 0.0;
1288                 r_no[1] = 0.0;
1289                 r_no[2] = 1.0;
1290
1291                 BLI_freelistN(&depths_peel);
1292                 return true;
1293         }
1294         return false;
1295 }
1296
1297 bool peelObjectsTransform(
1298         TransInfo *t,
1299         const float mval[2],
1300         const bool use_peel_object,
1301         /* return args */
1302         float r_loc[3], float r_no[3], float *r_thickness)
1303 {
1304         return peelObjectsSnapContext(
1305                 t->tsnap.object_context,
1306                 mval,
1307                 &(const struct SnapObjectParams){
1308                     .snap_select = ((t->options & CTX_GPENCIL_STROKES) != 0) ? SNAP_NOT_ACTIVE : t->tsnap.modeSelect,
1309                     .use_object_edit_cage = (t->flag & T_EDIT) != 0,
1310                 },
1311                 use_peel_object,
1312                 r_loc, r_no, r_thickness);
1313 }
1314
1315 /******************** NODES ***********************************/
1316
1317 static bool snapNodeTest(View2D *v2d, bNode *node, SnapSelect snap_select)
1318 {
1319         /* node is use for snapping only if a) snap mode matches and b) node is inside the view */
1320         return ((snap_select == SNAP_NOT_SELECTED && !(node->flag & NODE_SELECT)) ||
1321                 (snap_select == SNAP_ALL          && !(node->flag & NODE_ACTIVE))) &&
1322                 (node->totr.xmin < v2d->cur.xmax && node->totr.xmax > v2d->cur.xmin &&
1323                  node->totr.ymin < v2d->cur.ymax && node->totr.ymax > v2d->cur.ymin);
1324 }
1325
1326 static NodeBorder snapNodeBorder(int snap_node_mode)
1327 {
1328         switch (snap_node_mode) {
1329                 case SCE_SNAP_MODE_NODE_X:
1330                         return NODE_LEFT | NODE_RIGHT;
1331                 case SCE_SNAP_MODE_NODE_Y:
1332                         return NODE_TOP | NODE_BOTTOM;
1333                 case SCE_SNAP_MODE_NODE_XY:
1334                         return NODE_LEFT | NODE_RIGHT | NODE_TOP | NODE_BOTTOM;
1335         }
1336         return 0;
1337 }
1338
1339 static bool snapNode(
1340         ToolSettings *ts, SpaceNode *UNUSED(snode), ARegion *ar, bNode *node, const int mval[2],
1341         float r_loc[2], float *r_dist_px, char *r_node_border)
1342 {
1343         View2D *v2d = &ar->v2d;
1344         NodeBorder border = snapNodeBorder(ts->snap_node_mode);
1345         bool retval = false;
1346         rcti totr;
1347         int new_dist;
1348         
1349         UI_view2d_view_to_region_rcti(v2d, &node->totr, &totr);
1350         
1351         if (border & NODE_LEFT) {
1352                 new_dist = abs(totr.xmin - mval[0]);
1353                 if (new_dist < *r_dist_px) {
1354                         UI_view2d_region_to_view(v2d, totr.xmin, mval[1], &r_loc[0], &r_loc[1]);
1355                         *r_dist_px = new_dist;
1356                         *r_node_border = NODE_LEFT;
1357                         retval = true;
1358                 }
1359         }
1360         
1361         if (border & NODE_RIGHT) {
1362                 new_dist = abs(totr.xmax - mval[0]);
1363                 if (new_dist < *r_dist_px) {
1364                         UI_view2d_region_to_view(v2d, totr.xmax, mval[1], &r_loc[0], &r_loc[1]);
1365                         *r_dist_px = new_dist;
1366                         *r_node_border = NODE_RIGHT;
1367                         retval = true;
1368                 }
1369         }
1370         
1371         if (border & NODE_BOTTOM) {
1372                 new_dist = abs(totr.ymin - mval[1]);
1373                 if (new_dist < *r_dist_px) {
1374                         UI_view2d_region_to_view(v2d, mval[0], totr.ymin, &r_loc[0], &r_loc[1]);
1375                         *r_dist_px = new_dist;
1376                         *r_node_border = NODE_BOTTOM;
1377                         retval = true;
1378                 }
1379         }
1380         
1381         if (border & NODE_TOP) {
1382                 new_dist = abs(totr.ymax - mval[1]);
1383                 if (new_dist < *r_dist_px) {
1384                         UI_view2d_region_to_view(v2d, mval[0], totr.ymax, &r_loc[0], &r_loc[1]);
1385                         *r_dist_px = new_dist;
1386                         *r_node_border = NODE_TOP;
1387                         retval = true;
1388                 }
1389         }
1390         
1391         return retval;
1392 }
1393
1394 static bool snapNodes(
1395         ToolSettings *ts, SpaceNode *snode, ARegion *ar,
1396         const int mval[2], SnapSelect snap_select,
1397         float r_loc[2], float *r_dist_px, char *r_node_border)
1398 {
1399         bNodeTree *ntree = snode->edittree;
1400         bNode *node;
1401         bool retval = false;
1402         
1403         *r_node_border = 0;
1404         
1405         for (node = ntree->nodes.first; node; node = node->next) {
1406                 if (snapNodeTest(&ar->v2d, node, snap_select)) {
1407                         retval |= snapNode(ts, snode, ar, node, mval, r_loc, r_dist_px, r_node_border);
1408                 }
1409         }
1410         
1411         return retval;
1412 }
1413
1414 bool snapNodesTransform(
1415         TransInfo *t, const int mval[2], SnapSelect snap_select,
1416         float r_loc[2], float *r_dist_px, char *r_node_border)
1417 {
1418         return snapNodes(
1419                 t->settings, t->sa->spacedata.first, t->ar, mval, snap_select,
1420                 r_loc, r_dist_px, r_node_border);
1421 }
1422
1423 bool snapNodesContext(
1424         bContext *C, const int mval[2], SnapSelect snap_select,
1425         float r_loc[2], float *r_dist_px, char *r_node_border)
1426 {
1427         Scene *scene = CTX_data_scene(C);
1428         ARegion *ar = CTX_wm_region(C);
1429         return snapNodes(
1430                 scene->toolsettings, CTX_wm_space_node(C), ar, mval, snap_select,
1431                 r_loc, r_dist_px, r_node_border);
1432 }
1433
1434 /*================================================================*/
1435
1436 static void applyGridIncrement(TransInfo *t, float *val, int max_index, const float fac[3], GearsType action);
1437
1438
1439 void snapGridIncrementAction(TransInfo *t, float *val, GearsType action)
1440 {
1441         float fac[3];
1442
1443         fac[NO_GEARS]    = t->snap[0];
1444         fac[BIG_GEARS]   = t->snap[1];
1445         fac[SMALL_GEARS] = t->snap[2];
1446         
1447         applyGridIncrement(t, val, t->idx_max, fac, action);
1448 }
1449
1450
1451 void snapGridIncrement(TransInfo *t, float *val)
1452 {
1453         GearsType action;
1454
1455         /* only do something if using absolute or incremental grid snapping */
1456         if (!ELEM(t->tsnap.mode, SCE_SNAP_MODE_INCREMENT, SCE_SNAP_MODE_GRID))
1457                 return;
1458
1459         action = activeSnap(t) ? BIG_GEARS : NO_GEARS;
1460
1461         if (action == BIG_GEARS && (t->modifiers & MOD_PRECISION)) {
1462                 action = SMALL_GEARS;
1463         }
1464
1465         snapGridIncrementAction(t, val, action);
1466 }
1467
1468 void snapSequenceBounds(TransInfo *t, const int mval[2])
1469 {
1470         float xmouse, ymouse;
1471         int frame;
1472         int mframe;
1473         TransSeq *ts = t->custom.type.data;
1474         /* reuse increment, strictly speaking could be another snap mode, but leave as is */
1475         if (!(t->modifiers & MOD_SNAP_INVERT))
1476                 return;
1477
1478         /* convert to frame range */
1479         UI_view2d_region_to_view(&t->ar->v2d, mval[0], mval[1], &xmouse, &ymouse);
1480         mframe = iroundf(xmouse);
1481         /* now find the closest sequence */
1482         frame = BKE_sequencer_find_next_prev_edit(t->scene, mframe, SEQ_SIDE_BOTH, true, false, true);
1483
1484         if (!ts->snap_left)
1485                 frame = frame - (ts->max - ts->min);
1486
1487         t->values[0] = frame - ts->min;
1488 }
1489
1490 static void applyGridIncrement(TransInfo *t, float *val, int max_index, const float fac[3], GearsType action)
1491 {
1492         float asp_local[3] = {1, 1, 1};
1493         const bool use_aspect = ELEM(t->mode, TFM_TRANSLATION);
1494         const float *asp = use_aspect ? t->aspect : asp_local;
1495         int i;
1496
1497         BLI_assert(ELEM(t->tsnap.mode, SCE_SNAP_MODE_INCREMENT, SCE_SNAP_MODE_GRID));
1498         BLI_assert(max_index <= 2);
1499
1500         /* Early bailing out if no need to snap */
1501         if (fac[action] == 0.0f) {
1502                 return;
1503         }
1504
1505         if (use_aspect) {
1506                 /* custom aspect for fcurve */
1507                 if (t->spacetype == SPACE_IPO) {
1508                         View2D *v2d = &t->ar->v2d;
1509                         View2DGrid *grid;
1510                         SpaceIpo *sipo = t->sa->spacedata.first;
1511                         int unity = V2D_UNIT_VALUES;
1512                         int unitx = (sipo->flag & SIPO_DRAWTIME) ? V2D_UNIT_SECONDS : V2D_UNIT_FRAMESCALE;
1513
1514                         /* grid */
1515                         grid = UI_view2d_grid_calc(t->scene, v2d, unitx, V2D_GRID_NOCLAMP, unity, V2D_GRID_NOCLAMP, t->ar->winx, t->ar->winy);
1516
1517                         UI_view2d_grid_size(grid, &asp_local[0], &asp_local[1]);
1518                         UI_view2d_grid_free(grid);
1519
1520                         asp = asp_local;
1521                 }
1522         }
1523
1524         /* absolute snapping on grid based on global center */
1525         if ((t->tsnap.snap_spatial_grid) && (t->mode == TFM_TRANSLATION)) {
1526                 const float *center_global = t->center_global;
1527
1528                 /* use a fallback for cursor selection,
1529                  * this isn't useful as a global center for absolute grid snapping
1530                  * since its not based on the position of the selection. */
1531                 if (t->around == V3D_AROUND_CURSOR) {
1532                         const TransCenterData *cd = transformCenter_from_type(t, V3D_AROUND_CENTER_MEAN);
1533                         center_global = cd->global;
1534                 }
1535
1536                 for (i = 0; i <= max_index; i++) {
1537                         /* do not let unconstrained axis jump to absolute grid increments */
1538                         if (!(t->con.mode & CON_APPLY) || t->con.mode & (CON_AXIS0 << i)) {
1539                                 const float iter_fac = fac[action] * asp[i];
1540                                 val[i] = iter_fac * roundf((val[i] + center_global[i]) / iter_fac) - center_global[i];
1541                         }
1542                 }
1543         }
1544         else {
1545                 /* relative snapping in fixed increments */
1546                 for (i = 0; i <= max_index; i++) {
1547                         const float iter_fac = fac[action] * asp[i];
1548                         val[i] = iter_fac * roundf(val[i] / iter_fac);
1549                 }
1550         }
1551 }