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