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