Transform: snapping: normal-align: 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_armature_types.h"
40 #include "DNA_curve_types.h"
41 #include "DNA_scene_types.h"
42 #include "DNA_object_types.h"
43 #include "DNA_meshdata_types.h"  /* Temporary, for snapping to other unselected meshes */
44 #include "DNA_node_types.h"
45 #include "DNA_space_types.h"
46 #include "DNA_screen_types.h"
47 #include "DNA_view3d_types.h"
48 #include "DNA_windowmanager_types.h"
49
50 #include "BLI_math.h"
51 #include "BLI_blenlib.h"
52 #include "BLI_utildefines.h"
53
54 #include "BIF_gl.h"
55
56 #include "BKE_DerivedMesh.h"
57 #include "BKE_global.h"
58 #include "BKE_object.h"
59 #include "BKE_anim.h"  /* for duplis */
60 #include "BKE_context.h"
61 #include "BKE_editmesh.h"
62 #include "BKE_main.h"
63 #include "BKE_tracking.h"
64
65 #include "RNA_access.h"
66
67 #include "WM_types.h"
68
69 #include "ED_armature.h"
70 #include "ED_image.h"
71 #include "ED_node.h"
72 #include "ED_uvedit.h"
73 #include "ED_view3d.h"
74
75 #include "UI_resources.h"
76 #include "UI_view2d.h"
77
78 #include "MEM_guardedalloc.h"
79
80 #include "transform.h"
81
82 /* this should be passed as an arg for use in snap functions */
83 #undef BASACT
84
85 #define TRANSFORM_DIST_MAX_PX 1000.0f
86 #define TRANSFORM_SNAP_MAX_PX 100.0f
87 /* use half of flt-max so we can scale up without an exception */
88
89 /********************* PROTOTYPES ***********************/
90
91 static void setSnappingCallback(TransInfo *t);
92
93 static void ApplySnapTranslation(TransInfo *t, float vec[3]);
94 static void ApplySnapRotation(TransInfo *t, float *vec);
95 static void ApplySnapResize(TransInfo *t, float vec[2]);
96
97 /* static void CalcSnapGrid(TransInfo *t, float *vec); */
98 static void CalcSnapGeometry(TransInfo *t, float *vec);
99
100 static void TargetSnapMedian(TransInfo *t);
101 static void TargetSnapCenter(TransInfo *t);
102 static void TargetSnapClosest(TransInfo *t);
103 static void TargetSnapActive(TransInfo *t);
104
105 static float RotationBetween(TransInfo *t, const float p1[3], const float p2[3]);
106 static float TranslationBetween(TransInfo *t, const float p1[3], const float p2[3]);
107 static float ResizeBetween(TransInfo *t, const float p1[3], const float p2[3]);
108
109
110 /****************** IMPLEMENTATIONS *********************/
111
112 static bool snapNodeTest(View2D *v2d, bNode *node, SnapMode mode);
113 static NodeBorder snapNodeBorder(int snap_node_mode);
114
115 #if 0
116 int BIF_snappingSupported(Object *obedit)
117 {
118         int status = 0;
119         
120         /* only support object mesh, armature, curves */
121         if (obedit == NULL || ELEM(obedit->type, OB_MESH, OB_ARMATURE, OB_CURVE, OB_LATTICE, OB_MBALL)) {
122                 status = 1;
123         }
124         
125         return status;
126 }
127 #endif
128
129 bool validSnap(TransInfo *t)
130 {
131         return (t->tsnap.status & (POINT_INIT | TARGET_INIT)) == (POINT_INIT | TARGET_INIT) ||
132                (t->tsnap.status & (MULTI_POINTS | TARGET_INIT)) == (MULTI_POINTS | TARGET_INIT);
133 }
134
135 bool activeSnap(TransInfo *t)
136 {
137         return ((t->modifiers & (MOD_SNAP | MOD_SNAP_INVERT)) == MOD_SNAP) ||
138                ((t->modifiers & (MOD_SNAP | MOD_SNAP_INVERT)) == MOD_SNAP_INVERT);
139 }
140
141 void drawSnapping(const struct bContext *C, TransInfo *t)
142 {
143         unsigned char col[4], selectedCol[4], activeCol[4];
144         
145         if (!activeSnap(t))
146                 return;
147         
148         UI_GetThemeColor3ubv(TH_TRANSFORM, col);
149         col[3] = 128;
150         
151         UI_GetThemeColor3ubv(TH_SELECT, selectedCol);
152         selectedCol[3] = 128;
153         
154         UI_GetThemeColor3ubv(TH_ACTIVE, activeCol);
155         activeCol[3] = 192;
156         
157         if (t->spacetype == SPACE_VIEW3D) {
158                 if (validSnap(t)) {
159                         TransSnapPoint *p;
160                         View3D *v3d = CTX_wm_view3d(C);
161                         RegionView3D *rv3d = CTX_wm_region_view3d(C);
162                         float imat[4][4];
163                         float size;
164                         
165                         glDisable(GL_DEPTH_TEST);
166                         
167                         size = 2.5f * UI_GetThemeValuef(TH_VERTEX_SIZE);
168                         
169                         invert_m4_m4(imat, rv3d->viewmat);
170                         
171                         for (p = t->tsnap.points.first; p; p = p->next) {
172                                 if (p == t->tsnap.selectedPoint) {
173                                         glColor4ubv(selectedCol);
174                                 }
175                                 else {
176                                         glColor4ubv(col);
177                                 }
178                                 
179                                 drawcircball(GL_LINE_LOOP, p->co, ED_view3d_pixel_size(rv3d, p->co) * size * 0.75f, imat);
180                         }
181                         
182                         if (t->tsnap.status & POINT_INIT) {
183                                 glColor4ubv(activeCol);
184                                 
185                                 drawcircball(GL_LINE_LOOP, t->tsnap.snapPoint, ED_view3d_pixel_size(rv3d, t->tsnap.snapPoint) * size, imat);
186                         }
187                         
188                         /* draw normal if needed */
189                         if (usingSnappingNormal(t) && validSnappingNormal(t)) {
190                                 glColor4ubv(activeCol);
191                                 
192                                 glBegin(GL_LINES);
193                                 glVertex3f(t->tsnap.snapPoint[0], t->tsnap.snapPoint[1], t->tsnap.snapPoint[2]);
194                                 glVertex3f(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                                 glEnd();
198                         }
199                         
200                         if (v3d->zbuf)
201                                 glEnable(GL_DEPTH_TEST);
202                 }
203         }
204         else if (t->spacetype == SPACE_IMAGE) {
205                 if (validSnap(t)) {
206                         /* This will not draw, and Im nor sure why - campbell */
207 #if 0
208                         float xuser_asp, yuser_asp;
209                         int wi, hi;
210                         float w, h;
211                         
212                         calc_image_view(G.sima, 'f');   // float
213                         myortho2(G.v2d->cur.xmin, G.v2d->cur.xmax, G.v2d->cur.ymin, G.v2d->cur.ymax);
214                         glLoadIdentity();
215                         
216                         ED_space_image_get_aspect(t->sa->spacedata.first, &xuser_aspx, &yuser_asp);
217                         ED_space_image_width(t->sa->spacedata.first, &wi, &hi);
218                         w = (((float)wi) / IMG_SIZE_FALLBACK) * G.sima->zoom * xuser_asp;
219                         h = (((float)hi) / IMG_SIZE_FALLBACK) * G.sima->zoom * yuser_asp;
220                         
221                         cpack(0xFFFFFF);
222                         glTranslatef(t->tsnap.snapPoint[0], t->tsnap.snapPoint[1], 0.0f);
223                         
224                         //glRectf(0, 0, 1, 1);
225                         
226                         setlinestyle(0);
227                         cpack(0x0);
228                         fdrawline(-0.020 / w, 0, -0.1 / w, 0);
229                         fdrawline(0.1 / w, 0, 0.020 / w, 0);
230                         fdrawline(0, -0.020 / h, 0, -0.1 / h);
231                         fdrawline(0, 0.1 / h, 0, 0.020 / h);
232                         
233                         glTranslatef(-t->tsnap.snapPoint[0], -t->tsnap.snapPoint[1], 0.0f);
234                         setlinestyle(0);
235 #endif
236                 }
237         }
238         else if (t->spacetype == SPACE_NODE) {
239                 if (validSnap(t)) {
240                         ARegion *ar = CTX_wm_region(C);
241                         TransSnapPoint *p;
242                         float size;
243                         
244                         size = 2.5f * UI_GetThemeValuef(TH_VERTEX_SIZE);
245                         
246                         glEnable(GL_BLEND);
247                         
248                         for (p = t->tsnap.points.first; p; p = p->next) {
249                                 if (p == t->tsnap.selectedPoint) {
250                                         glColor4ubv(selectedCol);
251                                 }
252                                 else {
253                                         glColor4ubv(col);
254                                 }
255                                 
256                                 ED_node_draw_snap(&ar->v2d, p->co, size, 0);
257                         }
258                         
259                         if (t->tsnap.status & POINT_INIT) {
260                                 glColor4ubv(activeCol);
261                                 
262                                 ED_node_draw_snap(&ar->v2d, t->tsnap.snapPoint, size, t->tsnap.snapNodeBorder);
263                         }
264                         
265                         glDisable(GL_BLEND);
266                 }
267         }
268 }
269
270 eRedrawFlag handleSnapping(TransInfo *t, const wmEvent *event)
271 {
272         eRedrawFlag status = TREDRAW_NOTHING;
273
274 #if 0 // XXX need a proper selector for all snap mode
275         if (BIF_snappingSupported(t->obedit) && event->type == TABKEY && event->shift) {
276                 /* toggle snap and reinit */
277                 t->settings->snap_flag ^= SCE_SNAP;
278                 initSnapping(t, NULL);
279                 status = TREDRAW_HARD;
280         }
281 #endif
282         if (event->type == MOUSEMOVE) {
283                 status |= updateSelectedSnapPoint(t);
284         }
285         
286         return status;
287 }
288
289 void applyProject(TransInfo *t)
290 {
291         /* XXX FLICKER IN OBJECT MODE */
292         if ((t->tsnap.project) && activeSnap(t) && (t->flag & T_NO_PROJECT) == 0) {
293                 TransData *td = t->data;
294                 float tvec[3];
295                 float imat[4][4];
296                 int i;
297         
298                 if (t->flag & (T_EDIT | T_POSE)) {
299                         Object *ob = t->obedit ? t->obedit : t->poseobj;
300                         invert_m4_m4(imat, ob->obmat);
301                 }
302
303                 for (i = 0; i < t->total; i++, td++) {
304                         float iloc[3], loc[3], no[3];
305                         float mval_fl[2];
306                         float dist_px = TRANSFORM_DIST_MAX_PX;
307                         
308                         if (td->flag & TD_NOACTION)
309                                 break;
310                         
311                         if (td->flag & TD_SKIP)
312                                 continue;
313
314                         if ((t->flag & T_PROP_EDIT) && (td->factor == 0.0f))
315                                 continue;
316                         
317                         copy_v3_v3(iloc, td->loc);
318                         if (t->flag & (T_EDIT | T_POSE)) {
319                                 Object *ob = t->obedit ? t->obedit : t->poseobj;
320                                 mul_m4_v3(ob->obmat, iloc);
321                         }
322                         else if (t->flag & T_OBJECT) {
323                                 /* TODO(sergey): Ideally force update is not needed here. */
324                                 td->ob->recalc |= OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME;
325                                 BKE_object_handle_update(G.main->eval_ctx, t->scene, td->ob);
326                                 copy_v3_v3(iloc, td->ob->obmat[3]);
327                         }
328                         
329                         if (ED_view3d_project_float_global(t->ar, iloc, mval_fl, V3D_PROJ_TEST_NOP) == V3D_PROJ_RET_OK) {
330                                 if (snapObjectsTransform(t, mval_fl, &dist_px, loc, no, t->tsnap.modeSelect)) {
331 //                                      if (t->flag & (T_EDIT|T_POSE)) {
332 //                                              mul_m4_v3(imat, loc);
333 //                                      }
334
335                                         sub_v3_v3v3(tvec, loc, iloc);
336
337                                         mul_m3_v3(td->smtx, tvec);
338
339                                         add_v3_v3(td->loc, tvec);
340
341                                         if (t->tsnap.align && (t->flag & T_OBJECT)) {
342                                                 /* handle alignment as well */
343                                                 const float *original_normal;
344                                                 float mat[3][3];
345                                                 float totmat[3][3], smat[3][3];
346                                                 float eul[3], fmat[3][3];
347                                                 float obmat[3][3];
348
349                                                 /* In pose mode, we want to align normals with Y axis of bones... */
350                                                 original_normal = td->axismtx[2];
351
352                                                 rotation_between_vecs_to_mat3(mat, original_normal, no);
353
354                                                 mul_m3_m3m3(totmat, mat, td->mtx);
355                                                 mul_m3_m3m3(smat, td->smtx, totmat);
356
357                                                 /* calculate the total rotatation in eulers */
358                                                 add_v3_v3v3(eul, td->ext->irot, td->ext->drot); /* we have to correct for delta rot */
359                                                 eulO_to_mat3(obmat, eul, td->ext->rotOrder);
360                                                 /* mat = transform, obmat = object rotation */
361                                                 mul_m3_m3m3(fmat, smat, obmat);
362
363                                                 mat3_to_compatible_eulO(eul, td->ext->rot, td->ext->rotOrder, fmat);
364
365                                                 /* correct back for delta rot */
366                                                 sub_v3_v3v3(eul, eul, td->ext->drot);
367
368                                                 /* and apply */
369                                                 copy_v3_v3(td->ext->rot, eul);
370
371                                                 /* TODO support constraints for rotation too? see ElementRotation */
372                                         }
373                                 }
374                         }
375                         
376                         //XXX constraintTransLim(t, td);
377                 }
378         }
379 }
380
381 void applyGridAbsolute(TransInfo *t)
382 {
383         float grid_size = 0.0f;
384         GearsType grid_action;
385         TransData *td;
386         float imat[4][4];
387         int i;
388         
389         if (!(activeSnap(t) && (t->tsnap.mode == SCE_SNAP_MODE_GRID)))
390                 return;
391         
392         grid_action = BIG_GEARS;
393         if (t->modifiers & MOD_PRECISION)
394                 grid_action = SMALL_GEARS;
395         
396         switch (grid_action) {
397                 case NO_GEARS: grid_size = t->snap[0]; break;
398                 case BIG_GEARS: grid_size = t->snap[1]; break;
399                 case SMALL_GEARS: grid_size = t->snap[2]; break;
400         }
401         /* early exit on unusable grid size */
402         if (grid_size == 0.0f)
403                 return;
404         
405         if (t->flag & (T_EDIT | T_POSE)) {
406                 Object *ob = t->obedit ? t->obedit : t->poseobj;
407                 invert_m4_m4(imat, ob->obmat);
408         }
409         
410         for (i = 0, td = t->data; i < t->total; i++, td++) {
411                 float iloc[3], loc[3], tvec[3];
412                 
413                 if (td->flag & TD_NOACTION)
414                         break;
415                 
416                 if (td->flag & TD_SKIP)
417                         continue;
418                 
419                 if ((t->flag & T_PROP_EDIT) && (td->factor == 0.0f))
420                         continue;
421                 
422                 copy_v3_v3(iloc, td->loc);
423                 if (t->flag & (T_EDIT | T_POSE)) {
424                         Object *ob = t->obedit ? t->obedit : t->poseobj;
425                         mul_m4_v3(ob->obmat, iloc);
426                 }
427                 else if (t->flag & T_OBJECT) {
428                         td->ob->recalc |= OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME;
429                         BKE_object_handle_update(G.main->eval_ctx, t->scene, td->ob);
430                         copy_v3_v3(iloc, td->ob->obmat[3]);
431                 }
432                 
433                 mul_v3_v3fl(loc, iloc, 1.0f / grid_size);
434                 loc[0] = floorf(loc[0]);
435                 loc[1] = floorf(loc[1]);
436                 loc[2] = floorf(loc[2]);
437                 mul_v3_fl(loc, grid_size);
438                 
439                 sub_v3_v3v3(tvec, loc, iloc);
440                 mul_m3_v3(td->smtx, tvec);
441                 add_v3_v3(td->loc, tvec);
442         }
443 }
444
445 void applySnapping(TransInfo *t, float *vec)
446 {
447         /* project is not applied this way */
448         if (t->tsnap.project)
449                 return;
450         
451         if (t->tsnap.status & SNAP_FORCED) {
452                 t->tsnap.targetSnap(t);
453         
454                 t->tsnap.applySnap(t, vec);
455         }
456         else if ((t->tsnap.mode != SCE_SNAP_MODE_INCREMENT) && activeSnap(t)) {
457                 double current = PIL_check_seconds_timer();
458                 
459                 // Time base quirky code to go around findnearest slowness
460                 /* !TODO! add exception for object mode, no need to slow it down then */
461                 if (current - t->tsnap.last >= 0.01) {
462                         t->tsnap.calcSnap(t, vec);
463                         t->tsnap.targetSnap(t);
464         
465                         t->tsnap.last = current;
466                 }
467                 if (validSnap(t)) {
468                         t->tsnap.applySnap(t, vec);
469                 }
470         }
471 }
472
473 void resetSnapping(TransInfo *t)
474 {
475         t->tsnap.status = 0;
476         t->tsnap.align = false;
477         t->tsnap.project = 0;
478         t->tsnap.mode = 0;
479         t->tsnap.modeSelect = 0;
480         t->tsnap.target = 0;
481         t->tsnap.last = 0;
482         t->tsnap.applySnap = NULL;
483
484         t->tsnap.snapNormal[0] = 0;
485         t->tsnap.snapNormal[1] = 0;
486         t->tsnap.snapNormal[2] = 0;
487         
488         t->tsnap.snapNodeBorder = 0;
489 }
490
491 bool usingSnappingNormal(TransInfo *t)
492 {
493         return t->tsnap.align;
494 }
495
496 bool validSnappingNormal(TransInfo *t)
497 {
498         if (validSnap(t)) {
499                 if (!is_zero_v3(t->tsnap.snapNormal)) {
500                         return true;
501                 }
502         }
503         
504         return false;
505 }
506
507 static void initSnappingMode(TransInfo *t)
508 {
509         ToolSettings *ts = t->settings;
510         Object *obedit = t->obedit;
511         Scene *scene = t->scene;
512         Base *base_act = scene->basact;
513
514         if (t->spacetype == SPACE_NODE) {
515                 /* force project off when not supported */
516                 t->tsnap.project = 0;
517                 
518                 t->tsnap.mode = ts->snap_node_mode;
519         }
520         else if (t->spacetype == SPACE_IMAGE) {
521                 /* force project off when not supported */
522                 t->tsnap.project = 0;
523                 
524                 t->tsnap.mode = ts->snap_uv_mode;
525         }
526         else {
527                 /* force project off when not supported */
528                 if (ts->snap_mode != SCE_SNAP_MODE_FACE)
529                         t->tsnap.project = 0;
530                 
531                 t->tsnap.mode = ts->snap_mode;
532         }
533
534         if ((t->spacetype == SPACE_VIEW3D || t->spacetype == SPACE_IMAGE) &&  /* Only 3D view or UV */
535             (t->flag & T_CAMERA) == 0)  /* Not with camera selected in camera view */
536         {
537                 setSnappingCallback(t);
538
539                 /* Edit mode */
540                 if (t->tsnap.applySnap != NULL && // A snapping function actually exist
541                     (obedit != NULL && ELEM(obedit->type, OB_MESH, OB_ARMATURE, OB_CURVE, OB_LATTICE, OB_MBALL)) ) // Temporary limited to edit mode meshes, armature, curves, mballs
542                 {
543                         /* Exclude editmesh if using proportional edit */
544                         if ((obedit->type == OB_MESH) && (t->flag & T_PROP_EDIT)) {
545                                 t->tsnap.modeSelect = SNAP_NOT_OBEDIT;
546                         }
547                         else {
548                                 t->tsnap.modeSelect = t->tsnap.snap_self ? SNAP_ALL : SNAP_NOT_OBEDIT;
549                         }
550                 }
551                 /* Particles edit mode*/
552                 else if (t->tsnap.applySnap != NULL && // A snapping function actually exist
553                          (obedit == NULL && base_act && base_act->object && base_act->object->mode & OB_MODE_PARTICLE_EDIT))
554                 {
555                         t->tsnap.modeSelect = SNAP_ALL;
556                 }
557                 /* Object mode */
558                 else if (t->tsnap.applySnap != NULL && // A snapping function actually exist
559                          (obedit == NULL) ) // Object Mode
560                 {
561                         t->tsnap.modeSelect = SNAP_NOT_SELECTED;
562                 }
563                 else {
564                         /* Grid if snap is not possible */
565                         t->tsnap.mode = SCE_SNAP_MODE_INCREMENT;
566                 }
567         }
568         else if (t->spacetype == SPACE_NODE) {
569                 setSnappingCallback(t);
570                 
571                 if (t->tsnap.applySnap != NULL) {
572                         t->tsnap.modeSelect = SNAP_NOT_SELECTED;
573                 }
574                 else {
575                         /* Grid if snap is not possible */
576                         t->tsnap.mode = SCE_SNAP_MODE_INCREMENT;
577                 }
578         }
579         else {
580                 /* Always grid outside of 3D view */
581                 t->tsnap.mode = SCE_SNAP_MODE_INCREMENT;
582         }
583 }
584
585 void initSnapping(TransInfo *t, wmOperator *op)
586 {
587         ToolSettings *ts = t->settings;
588         short snap_target = t->settings->snap_target;
589         
590         resetSnapping(t);
591         
592         /* if snap property exists */
593         if (op && RNA_struct_find_property(op->ptr, "snap") && RNA_struct_property_is_set(op->ptr, "snap")) {
594                 if (RNA_boolean_get(op->ptr, "snap")) {
595                         t->modifiers |= MOD_SNAP;
596
597                         if (RNA_struct_property_is_set(op->ptr, "snap_target")) {
598                                 snap_target = RNA_enum_get(op->ptr, "snap_target");
599                         }
600                         
601                         if (RNA_struct_property_is_set(op->ptr, "snap_point")) {
602                                 RNA_float_get_array(op->ptr, "snap_point", t->tsnap.snapPoint);
603                                 t->tsnap.status |= SNAP_FORCED | POINT_INIT;
604                         }
605                         
606                         /* snap align only defined in specific cases */
607                         if (RNA_struct_find_property(op->ptr, "snap_align")) {
608                                 t->tsnap.align = RNA_boolean_get(op->ptr, "snap_align");
609                                 RNA_float_get_array(op->ptr, "snap_normal", t->tsnap.snapNormal);
610                                 normalize_v3(t->tsnap.snapNormal);
611                         }
612
613                         if (RNA_struct_find_property(op->ptr, "use_snap_project")) {
614                                 t->tsnap.project = RNA_boolean_get(op->ptr, "use_snap_project");
615                         }
616
617                         if (RNA_struct_find_property(op->ptr, "use_snap_self")) {
618                                 t->tsnap.snap_self = RNA_boolean_get(op->ptr, "use_snap_self");
619                         }
620                 }
621         }
622         /* use scene defaults only when transform is modal */
623         else if (t->flag & T_MODAL) {
624                 if (ELEM(t->spacetype, SPACE_VIEW3D, SPACE_IMAGE, SPACE_NODE)) {
625                         if (ts->snap_flag & SCE_SNAP) {
626                                 t->modifiers |= MOD_SNAP;
627                         }
628
629                         t->tsnap.align = ((t->settings->snap_flag & SCE_SNAP_ROTATE) != 0);
630                         t->tsnap.project = ((t->settings->snap_flag & SCE_SNAP_PROJECT) != 0);
631                         t->tsnap.snap_self = !((t->settings->snap_flag & SCE_SNAP_NO_SELF) != 0);
632                         t->tsnap.peel = ((t->settings->snap_flag & SCE_SNAP_PROJECT) != 0);
633                 }
634         }
635         
636         t->tsnap.target = snap_target;
637
638         initSnappingMode(t);
639 }
640
641 static void setSnappingCallback(TransInfo *t)
642 {
643         t->tsnap.calcSnap = CalcSnapGeometry;
644
645         switch (t->tsnap.target) {
646                 case SCE_SNAP_TARGET_CLOSEST:
647                         t->tsnap.targetSnap = TargetSnapClosest;
648                         break;
649                 case SCE_SNAP_TARGET_CENTER:
650                         t->tsnap.targetSnap = TargetSnapCenter;
651                         break;
652                 case SCE_SNAP_TARGET_MEDIAN:
653                         t->tsnap.targetSnap = TargetSnapMedian;
654                         break;
655                 case SCE_SNAP_TARGET_ACTIVE:
656                         t->tsnap.targetSnap = TargetSnapActive;
657                         break;
658
659         }
660
661         switch (t->mode) {
662                 case TFM_TRANSLATION:
663                         t->tsnap.applySnap = ApplySnapTranslation;
664                         t->tsnap.distance = TranslationBetween;
665                         break;
666                 case TFM_ROTATION:
667                         t->tsnap.applySnap = ApplySnapRotation;
668                         t->tsnap.distance = RotationBetween;
669
670                         // Can't do TARGET_CENTER with rotation, use TARGET_MEDIAN instead
671                         if (t->tsnap.target == SCE_SNAP_TARGET_CENTER) {
672                                 t->tsnap.target = SCE_SNAP_TARGET_MEDIAN;
673                                 t->tsnap.targetSnap = TargetSnapMedian;
674                         }
675                         break;
676                 case TFM_RESIZE:
677                         t->tsnap.applySnap = ApplySnapResize;
678                         t->tsnap.distance = ResizeBetween;
679
680                         // Can't do TARGET_CENTER with resize, use TARGET_MEDIAN instead
681                         if (t->tsnap.target == SCE_SNAP_TARGET_CENTER) {
682                                 t->tsnap.target = SCE_SNAP_TARGET_MEDIAN;
683                                 t->tsnap.targetSnap = TargetSnapMedian;
684                         }
685                         break;
686                 default:
687                         t->tsnap.applySnap = NULL;
688                         break;
689         }
690 }
691
692 void addSnapPoint(TransInfo *t)
693 {
694         /* Currently only 3D viewport works for snapping points. */
695         if (t->tsnap.status & POINT_INIT && t->spacetype == SPACE_VIEW3D) {
696                 TransSnapPoint *p = MEM_callocN(sizeof(TransSnapPoint), "SnapPoint");
697
698                 t->tsnap.selectedPoint = p;
699
700                 copy_v3_v3(p->co, t->tsnap.snapPoint);
701
702                 BLI_addtail(&t->tsnap.points, p);
703
704                 t->tsnap.status |= MULTI_POINTS;
705         }
706 }
707
708 eRedrawFlag updateSelectedSnapPoint(TransInfo *t)
709 {
710         eRedrawFlag status = TREDRAW_NOTHING;
711
712         if (t->tsnap.status & MULTI_POINTS) {
713                 TransSnapPoint *p, *closest_p = NULL;
714                 float dist_min_sq = TRANSFORM_SNAP_MAX_PX;
715                 const float mval_fl[2] = {t->mval[0], t->mval[1]};
716                 float screen_loc[2];
717
718                 for (p = t->tsnap.points.first; p; p = p->next) {
719                         float dist_sq;
720
721                         if (ED_view3d_project_float_global(t->ar, p->co, screen_loc, V3D_PROJ_TEST_NOP) != V3D_PROJ_RET_OK) {
722                                 continue;
723                         }
724
725                         dist_sq = len_squared_v2v2(mval_fl, screen_loc);
726
727                         if (dist_sq < dist_min_sq) {
728                                 closest_p = p;
729                                 dist_min_sq = dist_sq;
730                         }
731                 }
732
733                 if (closest_p) {
734                         if (t->tsnap.selectedPoint != closest_p) {
735                                 status = TREDRAW_HARD;
736                         }
737
738                         t->tsnap.selectedPoint = closest_p;
739                 }
740         }
741
742         return status;
743 }
744
745 void removeSnapPoint(TransInfo *t)
746 {
747         if (t->tsnap.status & MULTI_POINTS) {
748                 updateSelectedSnapPoint(t);
749
750                 if (t->tsnap.selectedPoint) {
751                         BLI_freelinkN(&t->tsnap.points, t->tsnap.selectedPoint);
752
753                         if (BLI_listbase_is_empty(&t->tsnap.points)) {
754                                 t->tsnap.status &= ~MULTI_POINTS;
755                         }
756
757                         t->tsnap.selectedPoint = NULL;
758                 }
759
760         }
761 }
762
763 void getSnapPoint(TransInfo *t, float vec[3])
764 {
765         if (t->tsnap.points.first) {
766                 TransSnapPoint *p;
767                 int total = 0;
768
769                 vec[0] = vec[1] = vec[2] = 0;
770
771                 for (p = t->tsnap.points.first; p; p = p->next, total++) {
772                         add_v3_v3(vec, p->co);
773                 }
774
775                 if (t->tsnap.status & POINT_INIT) {
776                         add_v3_v3(vec, t->tsnap.snapPoint);
777                         total++;
778                 }
779
780                 mul_v3_fl(vec, 1.0f / total);
781         }
782         else {
783                 copy_v3_v3(vec, t->tsnap.snapPoint);
784         }
785 }
786
787 /********************** APPLY **************************/
788
789 static void ApplySnapTranslation(TransInfo *t, float vec[3])
790 {
791         float point[3];
792         getSnapPoint(t, point);
793
794         if (t->spacetype == SPACE_NODE) {
795                 char border = t->tsnap.snapNodeBorder;
796                 if (border & (NODE_LEFT | NODE_RIGHT))
797                         vec[0] = point[0] - t->tsnap.snapTarget[0];
798                 if (border & (NODE_BOTTOM | NODE_TOP))
799                         vec[1] = point[1] - t->tsnap.snapTarget[1];
800         }
801         else {
802                 sub_v3_v3v3(vec, point, t->tsnap.snapTarget);
803         }
804 }
805
806 static void ApplySnapRotation(TransInfo *t, float *value)
807 {
808         if (t->tsnap.target == SCE_SNAP_TARGET_CLOSEST) {
809                 *value = t->tsnap.dist;
810         }
811         else {
812                 float point[3];
813                 getSnapPoint(t, point);
814                 *value = RotationBetween(t, t->tsnap.snapTarget, point);
815         }
816 }
817
818 static void ApplySnapResize(TransInfo *t, float vec[3])
819 {
820         if (t->tsnap.target == SCE_SNAP_TARGET_CLOSEST) {
821                 vec[0] = vec[1] = vec[2] = t->tsnap.dist;
822         }
823         else {
824                 float point[3];
825                 getSnapPoint(t, point);
826                 vec[0] = vec[1] = vec[2] = ResizeBetween(t, t->tsnap.snapTarget, point);
827         }
828 }
829
830 /********************** DISTANCE **************************/
831
832 static float TranslationBetween(TransInfo *UNUSED(t), const float p1[3], const float p2[3])
833 {
834         return len_v3v3(p1, p2);
835 }
836
837 static float RotationBetween(TransInfo *t, const float p1[3], const float p2[3])
838 {
839         float angle, start[3], end[3], center[3];
840         
841         copy_v3_v3(center, t->center);
842         if (t->flag & (T_EDIT | T_POSE)) {
843                 Object *ob = t->obedit ? t->obedit : t->poseobj;
844                 mul_m4_v3(ob->obmat, center);
845         }
846
847         sub_v3_v3v3(start, p1, center);
848         sub_v3_v3v3(end, p2, center);
849                 
850         // Angle around a constraint axis (error prone, will need debug)
851         if (t->con.applyRot != NULL && (t->con.mode & CON_APPLY)) {
852                 float axis[3], tmp[3];
853                 
854                 t->con.applyRot(t, NULL, axis, NULL);
855
856                 project_v3_v3v3(tmp, end, axis);
857                 sub_v3_v3v3(end, end, tmp);
858                 
859                 project_v3_v3v3(tmp, start, axis);
860                 sub_v3_v3v3(start, start, tmp);
861                 
862                 normalize_v3(end);
863                 normalize_v3(start);
864                 
865                 cross_v3_v3v3(tmp, start, end);
866                 
867                 if (dot_v3v3(tmp, axis) < 0.0f)
868                         angle = -acosf(dot_v3v3(start, end));
869                 else
870                         angle = acosf(dot_v3v3(start, end));
871         }
872         else {
873                 float mtx[3][3];
874                 
875                 copy_m3_m4(mtx, t->viewmat);
876
877                 mul_m3_v3(mtx, end);
878                 mul_m3_v3(mtx, start);
879                 
880                 angle = atan2f(start[1], start[0]) - atan2f(end[1], end[0]);
881         }
882         
883         if (angle > (float)M_PI) {
884                 angle = angle - 2 * (float)M_PI;
885         }
886         else if (angle < -((float)M_PI)) {
887                 angle = 2.0f * (float)M_PI + angle;
888         }
889         
890         return angle;
891 }
892
893 static float ResizeBetween(TransInfo *t, const float p1[3], const float p2[3])
894 {
895         float d1[3], d2[3], center[3], len_d1;
896         
897         copy_v3_v3(center, t->center);
898         if (t->flag & (T_EDIT | T_POSE)) {
899                 Object *ob = t->obedit ? t->obedit : t->poseobj;
900                 mul_m4_v3(ob->obmat, center);
901         }
902
903         sub_v3_v3v3(d1, p1, center);
904         sub_v3_v3v3(d2, p2, center);
905         
906         if (t->con.applyRot != NULL && (t->con.mode & CON_APPLY)) {
907                 mul_m3_v3(t->con.pmtx, d1);
908                 mul_m3_v3(t->con.pmtx, d2);
909         }
910         
911         len_d1 = len_v3(d1);
912
913         return len_d1 != 0.0f ? len_v3(d2) / len_d1 : 1;
914 }
915
916 /********************** CALC **************************/
917
918 static void UNUSED_FUNCTION(CalcSnapGrid) (TransInfo *t, float *UNUSED(vec))
919 {
920         snapGridIncrementAction(t, t->tsnap.snapPoint, BIG_GEARS);
921 }
922
923 static void CalcSnapGeometry(TransInfo *t, float *UNUSED(vec))
924 {
925         if (t->spacetype == SPACE_VIEW3D) {
926                 float loc[3];
927                 float no[3];
928                 float mval[2];
929                 bool found = false;
930                 float dist_px = SNAP_MIN_DISTANCE; // Use a user defined value here
931                 
932                 mval[0] = t->mval[0];
933                 mval[1] = t->mval[1];
934                 
935                 if (t->tsnap.mode == SCE_SNAP_MODE_VOLUME) {
936                         ListBase depth_peels;
937                         DepthPeel *p1, *p2;
938                         const float *last_p = NULL;
939                         float max_dist = FLT_MAX;
940                         float p[3] = {0.0f, 0.0f, 0.0f};
941                         
942                         BLI_listbase_clear(&depth_peels);
943                         
944                         peelObjectsTransForm(t, &depth_peels, mval, t->tsnap.modeSelect);
945                         
946 //                      if (LAST_SNAP_POINT_VALID)
947 //                      {
948 //                              last_p = LAST_SNAP_POINT;
949 //                      }
950 //                      else
951 //                      {
952                                 last_p = t->tsnap.snapPoint;
953 //                      }
954                         
955                         
956                         for (p1 = depth_peels.first; p1; p1 = p1->next) {
957                                 if (p1->flag == 0) {
958                                         float vec[3];
959                                         float new_dist;
960                                         
961                                         p2 = NULL;
962                                         p1->flag = 1;
963                 
964                                         /* if peeling objects, take the first and last from each object */
965                                         if (t->settings->snap_flag & SCE_SNAP_PEEL_OBJECT) {
966                                                 DepthPeel *peel;
967                                                 for (peel = p1->next; peel; peel = peel->next) {
968                                                         if (peel->ob == p1->ob) {
969                                                                 peel->flag = 1;
970                                                                 p2 = peel;
971                                                         }
972                                                 }
973                                         }
974                                         /* otherwise, pair first with second and so on */
975                                         else {
976                                                 for (p2 = p1->next; p2 && p2->ob != p1->ob; p2 = p2->next) {
977                                                         /* nothing to do here */
978                                                 }
979                                         }
980                                         
981                                         if (p2) {
982                                                 p2->flag = 1;
983                                                 
984                                                 add_v3_v3v3(vec, p1->p, p2->p);
985                                                 mul_v3_fl(vec, 0.5f);
986                                         }
987                                         else {
988                                                 copy_v3_v3(vec, p1->p);
989                                         }
990
991                                         if (last_p == NULL) {
992                                                 copy_v3_v3(p, vec);
993                                                 max_dist = 0;
994                                                 break;
995                                         }
996                                         
997                                         new_dist = len_v3v3(last_p, vec);
998                                         
999                                         if (new_dist < max_dist) {
1000                                                 copy_v3_v3(p, vec);
1001                                                 max_dist = new_dist;
1002                                         }
1003                                 }
1004                         }
1005                         
1006                         if (max_dist != FLT_MAX) {
1007                                 copy_v3_v3(loc, p);
1008                                 /* XXX, is there a correct normal in this case ???, for now just z up */
1009                                 no[0] = 0.0;
1010                                 no[1] = 0.0;
1011                                 no[2] = 1.0;
1012                                 found = true;
1013                         }
1014                         
1015                         BLI_freelistN(&depth_peels);
1016                 }
1017                 else {
1018                         found = snapObjectsTransform(t, mval, &dist_px, loc, no, t->tsnap.modeSelect);
1019                 }
1020                 
1021                 if (found == true) {
1022                         float tangent[3];
1023                         
1024                         sub_v2_v2v2(tangent, loc, t->tsnap.snapPoint);
1025                         tangent[2] = 0.0f;
1026                         
1027                         if (!is_zero_v3(tangent)) {
1028                                 copy_v3_v3(t->tsnap.snapTangent, tangent);
1029                         }
1030                         
1031                         copy_v3_v3(t->tsnap.snapPoint, loc);
1032                         copy_v3_v3(t->tsnap.snapNormal, no);
1033
1034                         t->tsnap.status |=  POINT_INIT;
1035                 }
1036                 else {
1037                         t->tsnap.status &= ~POINT_INIT;
1038                 }
1039         }
1040         else if (t->spacetype == SPACE_IMAGE && t->obedit != NULL && t->obedit->type == OB_MESH) {
1041                 /* same as above but for UV's */
1042                 Image *ima = ED_space_image(t->sa->spacedata.first);
1043                 float aspx, aspy, co[2];
1044                 
1045                 UI_view2d_region_to_view(&t->ar->v2d, t->mval[0], t->mval[1], &co[0], &co[1]);
1046
1047                 if (ED_uvedit_nearest_uv(t->scene, t->obedit, ima, co, t->tsnap.snapPoint)) {
1048                         ED_space_image_get_uv_aspect(t->sa->spacedata.first, &aspx, &aspy);
1049                         t->tsnap.snapPoint[0] *= aspx;
1050                         t->tsnap.snapPoint[1] *= aspy;
1051
1052                         t->tsnap.status |=  POINT_INIT;
1053                 }
1054                 else {
1055                         t->tsnap.status &= ~POINT_INIT;
1056                 }
1057         }
1058         else if (t->spacetype == SPACE_NODE) {
1059                 float loc[2];
1060                 float dist_px = SNAP_MIN_DISTANCE; // Use a user defined value here
1061                 char node_border;
1062                 
1063                 if (snapNodesTransform(t, t->mval, &dist_px, loc, &node_border, t->tsnap.modeSelect)) {
1064                         copy_v2_v2(t->tsnap.snapPoint, loc);
1065                         t->tsnap.snapNodeBorder = node_border;
1066                         
1067                         t->tsnap.status |=  POINT_INIT;
1068                 }
1069                 else {
1070                         t->tsnap.status &= ~POINT_INIT;
1071                 }
1072         }
1073 }
1074
1075 /********************** TARGET **************************/
1076
1077 static void TargetSnapOffset(TransInfo *t, TransData *td)
1078 {
1079         if (t->spacetype == SPACE_NODE && td != NULL) {
1080                 bNode *node = td->extra;
1081                 char border = t->tsnap.snapNodeBorder;
1082                 float width  = BLI_rctf_size_x(&node->totr);
1083                 float height = BLI_rctf_size_y(&node->totr);
1084                 
1085 #ifdef USE_NODE_CENTER
1086                 if (border & NODE_LEFT)
1087                         t->tsnap.snapTarget[0] -= 0.5f * width;
1088                 if (border & NODE_RIGHT)
1089                         t->tsnap.snapTarget[0] += 0.5f * width;
1090                 if (border & NODE_BOTTOM)
1091                         t->tsnap.snapTarget[1] -= 0.5f * height;
1092                 if (border & NODE_TOP)
1093                         t->tsnap.snapTarget[1] += 0.5f * height;
1094 #else
1095                 if (border & NODE_LEFT)
1096                         t->tsnap.snapTarget[0] -= 0.0f;
1097                 if (border & NODE_RIGHT)
1098                         t->tsnap.snapTarget[0] += width;
1099                 if (border & NODE_BOTTOM)
1100                         t->tsnap.snapTarget[1] -= height;
1101                 if (border & NODE_TOP)
1102                         t->tsnap.snapTarget[1] += 0.0f;
1103 #endif
1104         }
1105 }
1106
1107 static void TargetSnapCenter(TransInfo *t)
1108 {
1109         /* Only need to calculate once */
1110         if ((t->tsnap.status & TARGET_INIT) == 0) {
1111                 copy_v3_v3(t->tsnap.snapTarget, t->center);
1112                 
1113                 if (t->flag & (T_EDIT | T_POSE)) {
1114                         Object *ob = t->obedit ? t->obedit : t->poseobj;
1115                         mul_m4_v3(ob->obmat, t->tsnap.snapTarget);
1116                 }
1117                 
1118                 TargetSnapOffset(t, NULL);
1119                 
1120                 t->tsnap.status |= TARGET_INIT;
1121         }
1122 }
1123
1124 static void TargetSnapActive(TransInfo *t)
1125 {
1126         /* Only need to calculate once */
1127         if ((t->tsnap.status & TARGET_INIT) == 0) {
1128                 if (calculateCenterActive(t, true, t->tsnap.snapTarget)) {
1129                         if (t->flag & (T_EDIT | T_POSE)) {
1130                                 Object *ob = t->obedit ? t->obedit : t->poseobj;
1131                                 mul_m4_v3(ob->obmat, t->tsnap.snapTarget);
1132                         }
1133
1134                         TargetSnapOffset(t, NULL);
1135
1136                         t->tsnap.status |= TARGET_INIT;
1137                 }
1138                 /* No active, default to median */
1139                 else {
1140                         t->tsnap.target = SCE_SNAP_TARGET_MEDIAN;
1141                         t->tsnap.targetSnap = TargetSnapMedian;
1142                         TargetSnapMedian(t);
1143                 }
1144         }
1145 }
1146
1147 static void TargetSnapMedian(TransInfo *t)
1148 {
1149         // Only need to calculate once
1150         if ((t->tsnap.status & TARGET_INIT) == 0) {
1151                 TransData *td = NULL;
1152                 int i;
1153
1154                 t->tsnap.snapTarget[0] = 0;
1155                 t->tsnap.snapTarget[1] = 0;
1156                 t->tsnap.snapTarget[2] = 0;
1157                 
1158                 for (td = t->data, i = 0; i < t->total && td->flag & TD_SELECTED; i++, td++) {
1159                         add_v3_v3(t->tsnap.snapTarget, td->center);
1160                 }
1161                 
1162                 mul_v3_fl(t->tsnap.snapTarget, 1.0 / i);
1163                 
1164                 if (t->flag & (T_EDIT | T_POSE)) {
1165                         Object *ob = t->obedit ? t->obedit : t->poseobj;
1166                         mul_m4_v3(ob->obmat, t->tsnap.snapTarget);
1167                 }
1168                 
1169                 TargetSnapOffset(t, NULL);
1170                 
1171                 t->tsnap.status |= TARGET_INIT;
1172         }
1173 }
1174
1175 static void TargetSnapClosest(TransInfo *t)
1176 {
1177         // Only valid if a snap point has been selected
1178         if (t->tsnap.status & POINT_INIT) {
1179                 TransData *closest = NULL, *td = NULL;
1180                 
1181                 /* Object mode */
1182                 if (t->flag & T_OBJECT) {
1183                         int i;
1184                         for (td = t->data, i = 0; i < t->total && td->flag & TD_SELECTED; i++, td++) {
1185                                 struct BoundBox *bb = BKE_object_boundbox_get(td->ob);
1186                                 
1187                                 /* use boundbox if possible */
1188                                 if (bb) {
1189                                         int j;
1190                                         
1191                                         for (j = 0; j < 8; j++) {
1192                                                 float loc[3];
1193                                                 float dist;
1194                                                 
1195                                                 copy_v3_v3(loc, bb->vec[j]);
1196                                                 mul_m4_v3(td->ext->obmat, loc);
1197                                                 
1198                                                 dist = t->tsnap.distance(t, loc, t->tsnap.snapPoint);
1199                                                 
1200                                                 if (closest == NULL || fabsf(dist) < fabsf(t->tsnap.dist)) {
1201                                                         copy_v3_v3(t->tsnap.snapTarget, loc);
1202                                                         closest = td;
1203                                                         t->tsnap.dist = dist; 
1204                                                 }
1205                                         }
1206                                 }
1207                                 /* use element center otherwise */
1208                                 else {
1209                                         float loc[3];
1210                                         float dist;
1211                                         
1212                                         copy_v3_v3(loc, td->center);
1213                                         
1214                                         dist = t->tsnap.distance(t, loc, t->tsnap.snapPoint);
1215                                         
1216                                         if (closest == NULL || fabsf(dist) < fabsf(t->tsnap.dist)) {
1217                                                 copy_v3_v3(t->tsnap.snapTarget, loc);
1218                                                 closest = td;
1219                                                 t->tsnap.dist = dist; 
1220                                         }
1221                                 }
1222                         }
1223                 }
1224                 else {
1225                         int i;
1226                         for (td = t->data, i = 0; i < t->total && td->flag & TD_SELECTED; i++, td++) {
1227                                 float loc[3];
1228                                 float dist;
1229                                 
1230                                 copy_v3_v3(loc, td->center);
1231                                 
1232                                 if (t->flag & (T_EDIT | T_POSE)) {
1233                                         Object *ob = t->obedit ? t->obedit : t->poseobj;
1234                                         mul_m4_v3(ob->obmat, loc);
1235                                 }
1236                                 
1237                                 dist = t->tsnap.distance(t, loc, t->tsnap.snapPoint);
1238                                 
1239                                 if (closest == NULL || fabsf(dist) < fabsf(t->tsnap.dist)) {
1240                                         copy_v3_v3(t->tsnap.snapTarget, loc);
1241                                         closest = td;
1242                                         t->tsnap.dist = dist; 
1243                                 }
1244                         }
1245                 }
1246                 
1247                 TargetSnapOffset(t, closest);
1248                 
1249                 t->tsnap.status |= TARGET_INIT;
1250         }
1251 }
1252
1253 static bool snapEdge(ARegion *ar, const float v1co[3], const short v1no[3], const float v2co[3], const short v2no[3], float obmat[4][4], float timat[3][3],
1254                      const float ray_start[3], const float ray_start_local[3], const float ray_normal_local[3], const float mval_fl[2],
1255                      float r_loc[3], float r_no[3], float *r_dist_px, float *r_depth)
1256 {
1257         float intersect[3] = {0, 0, 0}, ray_end[3], dvec[3];
1258         int result;
1259         bool retval = false;
1260         
1261         copy_v3_v3(ray_end, ray_normal_local);
1262         mul_v3_fl(ray_end, 2000);
1263         add_v3_v3v3(ray_end, ray_start_local, ray_end);
1264         
1265         result = isect_line_line_v3(v1co, v2co, ray_start_local, ray_end, intersect, dvec); /* dvec used but we don't care about result */
1266         
1267         if (result) {
1268                 float edge_loc[3], vec[3];
1269                 float mul;
1270         
1271                 /* check for behind ray_start */
1272                 sub_v3_v3v3(dvec, intersect, ray_start_local);
1273                 
1274                 sub_v3_v3v3(edge_loc, v1co, v2co);
1275                 sub_v3_v3v3(vec, intersect, v2co);
1276                 
1277                 mul = dot_v3v3(vec, edge_loc) / dot_v3v3(edge_loc, edge_loc);
1278                 
1279                 if (mul > 1) {
1280                         mul = 1;
1281                         copy_v3_v3(intersect, v1co);
1282                 }
1283                 else if (mul < 0) {
1284                         mul = 0;
1285                         copy_v3_v3(intersect, v2co);
1286                 }
1287
1288                 if (dot_v3v3(ray_normal_local, dvec) > 0) {
1289                         float location[3];
1290                         float new_depth;
1291                         float screen_loc[2];
1292                         float new_dist;
1293                         
1294                         copy_v3_v3(location, intersect);
1295                         
1296                         mul_m4_v3(obmat, location);
1297                         
1298                         new_depth = len_v3v3(location, ray_start);
1299                         
1300                         if (ED_view3d_project_float_global(ar, location, screen_loc, V3D_PROJ_TEST_NOP) == V3D_PROJ_RET_OK) {
1301                                 new_dist = len_manhattan_v2v2(mval_fl, screen_loc);
1302                         }
1303                         else {
1304                                 new_dist = TRANSFORM_DIST_MAX_PX;
1305                         }
1306                         
1307                         /* 10% threshold if edge is closer but a bit further
1308                          * this takes care of series of connected edges a bit slanted w.r.t the viewport
1309                          * otherwise, it would stick to the verts of the closest edge and not slide along merrily 
1310                          * */
1311                         if (new_dist <= *r_dist_px && new_depth < *r_depth * 1.001f) {
1312                                 float n1[3], n2[3];
1313                                 
1314                                 *r_depth = new_depth;
1315                                 retval = true;
1316                                 
1317                                 sub_v3_v3v3(edge_loc, v1co, v2co);
1318                                 sub_v3_v3v3(vec, intersect, v2co);
1319                                 
1320                                 mul = dot_v3v3(vec, edge_loc) / dot_v3v3(edge_loc, edge_loc);
1321                                 
1322                                 if (r_no) {
1323                                         normal_short_to_float_v3(n1, v1no);
1324                                         normal_short_to_float_v3(n2, v2no);
1325                                         interp_v3_v3v3(r_no, n2, n1, mul);
1326                                         mul_m3_v3(timat, r_no);
1327                                         normalize_v3(r_no);
1328                                 }
1329
1330                                 copy_v3_v3(r_loc, location);
1331                                 
1332                                 *r_dist_px = new_dist;
1333                         }
1334                 }
1335         }
1336         
1337         return retval;
1338 }
1339
1340 static bool snapVertex(ARegion *ar, const float vco[3], const short vno[3], float obmat[4][4], float timat[3][3],
1341                        const float ray_start[3], const float ray_start_local[3], const float ray_normal_local[3], const float mval_fl[2],
1342                        float r_loc[3], float r_no[3], float *r_dist_px, float *r_depth)
1343 {
1344         bool retval = false;
1345         float dvec[3];
1346         
1347         sub_v3_v3v3(dvec, vco, ray_start_local);
1348         
1349         if (dot_v3v3(ray_normal_local, dvec) > 0) {
1350                 float location[3];
1351                 float new_depth;
1352                 float screen_loc[2];
1353                 float new_dist;
1354                 
1355                 copy_v3_v3(location, vco);
1356                 
1357                 mul_m4_v3(obmat, location);
1358                 
1359                 new_depth = len_v3v3(location, ray_start);
1360                 
1361                 if (ED_view3d_project_float_global(ar, location, screen_loc, V3D_PROJ_TEST_NOP) == V3D_PROJ_RET_OK) {
1362                         new_dist = len_manhattan_v2v2(mval_fl, screen_loc);
1363                 }
1364                 else {
1365                         new_dist = TRANSFORM_DIST_MAX_PX;
1366                 }
1367
1368                 
1369                 if (new_dist <= *r_dist_px && new_depth < *r_depth) {
1370                         *r_depth = new_depth;
1371                         retval = true;
1372                         
1373                         copy_v3_v3(r_loc, location);
1374                         
1375                         if (r_no) {
1376                                 normal_short_to_float_v3(r_no, vno);
1377                                 mul_m3_v3(timat, r_no);
1378                                 normalize_v3(r_no);
1379                         }
1380
1381                         *r_dist_px = new_dist;
1382                 }
1383         }
1384         
1385         return retval;
1386 }
1387
1388 static bool snapArmature(short snap_mode, ARegion *ar, Object *ob, bArmature *arm, float obmat[4][4],
1389                          const float ray_start[3], const float ray_normal[3], const float mval[2],
1390                          float r_loc[3], float *UNUSED(r_no), float *r_dist_px, float *r_depth)
1391 {
1392         float imat[4][4];
1393         float ray_start_local[3], ray_normal_local[3];
1394         bool retval = false;
1395
1396         invert_m4_m4(imat, obmat);
1397
1398         copy_v3_v3(ray_start_local, ray_start);
1399         copy_v3_v3(ray_normal_local, ray_normal);
1400         
1401         mul_m4_v3(imat, ray_start_local);
1402         mul_mat3_m4_v3(imat, ray_normal_local);
1403
1404         if (arm->edbo) {
1405                 EditBone *eBone;
1406
1407                 for (eBone = arm->edbo->first; eBone; eBone = eBone->next) {
1408                         if (eBone->layer & arm->layer) {
1409                                 /* skip hidden or moving (selected) bones */
1410                                 if ((eBone->flag & (BONE_HIDDEN_A | BONE_ROOTSEL | BONE_TIPSEL)) == 0) {
1411                                         switch (snap_mode) {
1412                                                 case SCE_SNAP_MODE_VERTEX:
1413                                                         retval |= snapVertex(ar, eBone->head, NULL, obmat, NULL, ray_start, ray_start_local, ray_normal_local, mval, r_loc, NULL, r_dist_px, r_depth);
1414                                                         retval |= snapVertex(ar, eBone->tail, NULL, obmat, NULL, ray_start, ray_start_local, ray_normal_local, mval, r_loc, NULL, r_dist_px, r_depth);
1415                                                         break;
1416                                                 case SCE_SNAP_MODE_EDGE:
1417                                                         retval |= snapEdge(ar, eBone->head, NULL, eBone->tail, NULL, obmat, NULL, ray_start, ray_start_local, ray_normal_local, mval, r_loc, NULL, r_dist_px, r_depth);
1418                                                         break;
1419                                         }
1420                                 }
1421                         }
1422                 }
1423         }
1424         else if (ob->pose && ob->pose->chanbase.first) {
1425                 bPoseChannel *pchan;
1426                 Bone *bone;
1427                 
1428                 for (pchan = ob->pose->chanbase.first; pchan; pchan = pchan->next) {
1429                         bone = pchan->bone;
1430                         /* skip hidden bones */
1431                         if (bone && !(bone->flag & (BONE_HIDDEN_P | BONE_HIDDEN_PG))) {
1432                                 const float *head_vec = pchan->pose_head;
1433                                 const float *tail_vec = pchan->pose_tail;
1434                                 
1435                                 switch (snap_mode) {
1436                                         case SCE_SNAP_MODE_VERTEX:
1437                                                 retval |= snapVertex(ar, head_vec, NULL, obmat, NULL, ray_start, ray_start_local, ray_normal_local, mval, r_loc, NULL, r_dist_px, r_depth);
1438                                                 retval |= snapVertex(ar, tail_vec, NULL, obmat, NULL, ray_start, ray_start_local, ray_normal_local, mval, r_loc, NULL, r_dist_px, r_depth);
1439                                                 break;
1440                                         case SCE_SNAP_MODE_EDGE:
1441                                                 retval |= snapEdge(ar, head_vec, NULL, tail_vec, NULL, obmat, NULL, ray_start, ray_start_local, ray_normal_local, mval, r_loc, NULL, r_dist_px, r_depth);
1442                                                 break;
1443                                 }
1444                         }
1445                 }
1446         }
1447
1448         return retval;
1449 }
1450
1451 static bool snapCurve(short snap_mode, ARegion *ar, Object *ob, Curve *cu, float obmat[4][4],
1452                       const float ray_start[3], const float ray_normal[3], const float mval[2],
1453                       float r_loc[3], float *UNUSED(r_no), float *r_dist_px, float *r_depth)
1454 {
1455         float imat[4][4];
1456         float ray_start_local[3], ray_normal_local[3];
1457         bool retval = false;
1458         int u;
1459
1460         Nurb *nu;
1461
1462         /* only vertex snapping mode (eg control points and handles) supported for now) */
1463         if (snap_mode != SCE_SNAP_MODE_VERTEX) {
1464                 return retval;
1465         }
1466
1467         invert_m4_m4(imat, obmat);
1468
1469         copy_v3_v3(ray_start_local, ray_start);
1470         copy_v3_v3(ray_normal_local, ray_normal);
1471
1472         mul_m4_v3(imat, ray_start_local);
1473         mul_mat3_m4_v3(imat, ray_normal_local);
1474
1475         for (nu = (ob->mode == OB_MODE_EDIT ? cu->editnurb->nurbs.first : cu->nurb.first); nu; nu = nu->next) {
1476                 for (u = 0; u < nu->pntsu; u++) {
1477                         switch (snap_mode) {
1478                                 case SCE_SNAP_MODE_VERTEX:
1479                                 {
1480                                         if (ob->mode == OB_MODE_EDIT) {
1481                                                 if (nu->bezt) {
1482                                                         /* don't snap to selected (moving) or hidden */
1483                                                         if (nu->bezt[u].f2 & SELECT || nu->bezt[u].hide != 0) {
1484                                                                 break;
1485                                                         }
1486                                                         retval |= snapVertex(ar, nu->bezt[u].vec[1], NULL, obmat, NULL, ray_start, ray_start_local, ray_normal_local, mval, r_loc, NULL, r_dist_px, r_depth);
1487                                                         /* don't snap if handle is selected (moving), or if it is aligning to a moving handle */
1488                                                         if (!(nu->bezt[u].f1 & SELECT) && !(nu->bezt[u].h1 & HD_ALIGN && nu->bezt[u].f3 & SELECT)) {
1489                                                                 retval |= snapVertex(ar, nu->bezt[u].vec[0], NULL, obmat, NULL, ray_start, ray_start_local, ray_normal_local, mval, r_loc, NULL, r_dist_px, r_depth);
1490                                                         }
1491                                                         if (!(nu->bezt[u].f3 & SELECT) && !(nu->bezt[u].h2 & HD_ALIGN && nu->bezt[u].f1 & SELECT)) {
1492                                                                 retval |= snapVertex(ar, nu->bezt[u].vec[2], NULL, obmat, NULL, ray_start, ray_start_local, ray_normal_local, mval, r_loc, NULL, r_dist_px, r_depth);
1493                                                         }
1494                                                 }
1495                                                 else {
1496                                                         /* don't snap to selected (moving) or hidden */
1497                                                         if (nu->bp[u].f1 & SELECT || nu->bp[u].hide != 0) {
1498                                                                 break;
1499                                                         }
1500                                                         retval |= snapVertex(ar, nu->bp[u].vec, NULL, obmat, NULL, ray_start, ray_start_local, ray_normal_local, mval, r_loc, NULL, r_dist_px, r_depth);
1501                                                 }
1502                                         }
1503                                         else {
1504                                                 /* curve is not visible outside editmode if nurb length less than two */
1505                                                 if (nu->pntsu > 1) {
1506                                                         if (nu->bezt) {
1507                                                                 retval |= snapVertex(ar, nu->bezt[u].vec[1], NULL, obmat, NULL, ray_start, ray_start_local, ray_normal_local, mval, r_loc, NULL, r_dist_px, r_depth);
1508                                                         }
1509                                                         else {
1510                                                                 retval |= snapVertex(ar, nu->bp[u].vec, NULL, obmat, NULL, ray_start, ray_start_local, ray_normal_local, mval, r_loc, NULL, r_dist_px, r_depth);
1511                                                         }
1512                                                 }
1513                                         }
1514                                         break;
1515                                 }
1516                                 default:
1517                                         break;
1518                         }
1519                 }
1520         }
1521         return retval;
1522 }
1523
1524 static bool snapDerivedMesh(short snap_mode, ARegion *ar, Object *ob, DerivedMesh *dm, BMEditMesh *em, float obmat[4][4],
1525                             const float ray_start[3], const float ray_normal[3], const float ray_origin[3],
1526                             const float mval[2], float r_loc[3], float r_no[3], float *r_dist_px, float *r_depth, bool do_bb)
1527 {
1528         bool retval = false;
1529         const bool do_ray_start_correction = (snap_mode == SCE_SNAP_MODE_FACE && ar &&
1530                                               !((RegionView3D *)ar->regiondata)->is_persp);
1531         int totvert = dm->getNumVerts(dm);
1532
1533         if (totvert > 0) {
1534                 float imat[4][4];
1535                 float timat[3][3]; /* transpose inverse matrix for normals */
1536                 float ray_start_local[3], ray_normal_local[3], local_scale, len_diff = TRANSFORM_DIST_MAX_RAY;
1537
1538                 invert_m4_m4(imat, obmat);
1539                 transpose_m3_m4(timat, imat);
1540
1541                 copy_v3_v3(ray_start_local, ray_start);
1542                 copy_v3_v3(ray_normal_local, ray_normal);
1543
1544                 mul_m4_v3(imat, ray_start_local);
1545                 mul_mat3_m4_v3(imat, ray_normal_local);
1546
1547                 /* local scale in normal direction */
1548                 local_scale = normalize_v3(ray_normal_local);
1549
1550                 if (do_bb) {
1551                         BoundBox *bb = BKE_object_boundbox_get(ob);
1552                         if (!BKE_boundbox_ray_hit_check(bb, ray_start_local, ray_normal_local, &len_diff)) {
1553                                 return retval;
1554                         }
1555                 }
1556                 else if (do_ray_start_correction) {
1557                         /* We *need* a reasonably valid len_diff in this case.
1558                          * Use BHVTree to find the closest face from ray_start_local.
1559                          */
1560                         BVHTreeFromMesh treeData;
1561                         BVHTreeNearest nearest;
1562                         len_diff = 0.0f;  /* In case BVHTree would fail for some reason... */
1563
1564                         treeData.em_evil = em;
1565                         bvhtree_from_mesh_faces(&treeData, dm, 0.0f, 2, 6);
1566                         if (treeData.tree != NULL) {
1567                                 nearest.index = -1;
1568                                 nearest.dist_sq = FLT_MAX;
1569                                 /* Compute and store result. */
1570                                 BLI_bvhtree_find_nearest(treeData.tree, ray_start_local, &nearest,
1571                                                          treeData.nearest_callback, &treeData);
1572                                 if (nearest.index != -1) {
1573                                         len_diff = sqrtf(nearest.dist_sq);
1574                                 }
1575                         }
1576                         free_bvhtree_from_mesh(&treeData);
1577                 }
1578
1579                 switch (snap_mode) {
1580                         case SCE_SNAP_MODE_FACE:
1581                         {
1582                                 BVHTreeRayHit hit;
1583                                 BVHTreeFromMesh treeData;
1584
1585                                 /* Only use closer ray_start in case of ortho view! In perspective one, ray_start may already
1586                                  * been *inside* boundbox, leading to snap failures (see T38409).
1587                                  * Note also ar might be null (see T38435), in this case we assume ray_start is ok!
1588                                  */
1589                                 if (do_ray_start_correction) {
1590                                         float ray_org_local[3];
1591
1592                                         copy_v3_v3(ray_org_local, ray_origin);
1593                                         mul_m4_v3(imat, ray_org_local);
1594
1595                                         /* We pass a temp ray_start, set from object's boundbox, to avoid precision issues with very far
1596                                          * away ray_start values (as returned in case of ortho view3d), see T38358.
1597                                          */
1598                                         len_diff -= local_scale;  /* make temp start point a bit away from bbox hit point. */
1599                                         madd_v3_v3v3fl(ray_start_local, ray_org_local, ray_normal_local,
1600                                                        len_diff - len_v3v3(ray_start_local, ray_org_local));
1601                                 }
1602                                 else {
1603                                         len_diff = 0.0f;
1604                                 }
1605
1606                                 treeData.em_evil = em;
1607                                 bvhtree_from_mesh_faces(&treeData, dm, 0.0f, 4, 6);
1608
1609                                 hit.index = -1;
1610                                 hit.dist = *r_depth;
1611                                 if (hit.dist != TRANSFORM_DIST_MAX_RAY) {
1612                                         hit.dist *= local_scale;
1613                                         hit.dist -= len_diff;
1614                                 }
1615
1616                                 if (treeData.tree &&
1617                                     BLI_bvhtree_ray_cast(treeData.tree, ray_start_local, ray_normal_local, 0.0f,
1618                                                          &hit, treeData.raycast_callback, &treeData) != -1)
1619                                 {
1620                                         hit.dist += len_diff;
1621                                         hit.dist /= local_scale;
1622                                         if (hit.dist <= *r_depth) {
1623                                                 *r_depth = hit.dist;
1624                                                 copy_v3_v3(r_loc, hit.co);
1625                                                 copy_v3_v3(r_no, hit.no);
1626
1627                                                 /* back to worldspace */
1628                                                 mul_m4_v3(obmat, r_loc);
1629                                                 mul_m3_v3(timat, r_no);
1630                                                 normalize_v3(r_no);
1631
1632                                                 retval = true;
1633                                         }
1634                                 }
1635                                 free_bvhtree_from_mesh(&treeData);
1636                                 break;
1637                         }
1638                         case SCE_SNAP_MODE_VERTEX:
1639                         {
1640                                 MVert *verts = dm->getVertArray(dm);
1641                                 const int *index_array = NULL;
1642                                 int index = 0;
1643                                 int i;
1644
1645                                 if (em != NULL) {
1646                                         index_array = dm->getVertDataArray(dm, CD_ORIGINDEX);
1647                                         BM_mesh_elem_table_ensure(em->bm, BM_VERT);
1648                                 }
1649
1650                                 for (i = 0; i < totvert; i++) {
1651                                         BMVert *eve = NULL;
1652                                         MVert *v = verts + i;
1653                                         bool test = true;
1654
1655                                         if (em != NULL) {
1656                                                 if (index_array) {
1657                                                         index = index_array[i];
1658                                                 }
1659                                                 else {
1660                                                         index = i;
1661                                                 }
1662                                                 
1663                                                 if (index == ORIGINDEX_NONE) {
1664                                                         test = false;
1665                                                 }
1666                                                 else {
1667                                                         eve = BM_vert_at_index(em->bm, index);
1668                                                         
1669                                                         if (BM_elem_flag_test(eve, BM_ELEM_HIDDEN) ||
1670                                                             BM_elem_flag_test(eve, BM_ELEM_SELECT))
1671                                                         {
1672                                                                 test = false;
1673                                                         }
1674                                                 }
1675                                         }
1676
1677                                         if (test) {
1678                                                 retval |= snapVertex(ar, v->co, v->no, obmat, timat, ray_start, ray_start_local,
1679                                                                      ray_normal_local, mval, r_loc, r_no, r_dist_px, r_depth);
1680                                         }
1681                                 }
1682
1683                                 break;
1684                         }
1685                         case SCE_SNAP_MODE_EDGE:
1686                         {
1687                                 MVert *verts = dm->getVertArray(dm);
1688                                 MEdge *edges = dm->getEdgeArray(dm);
1689                                 int totedge = dm->getNumEdges(dm);
1690                                 const int *index_array = NULL;
1691                                 int index = 0;
1692                                 int i;
1693
1694                                 if (em != NULL) {
1695                                         index_array = dm->getEdgeDataArray(dm, CD_ORIGINDEX);
1696                                         BM_mesh_elem_table_ensure(em->bm, BM_EDGE);
1697                                 }
1698
1699                                 for (i = 0; i < totedge; i++) {
1700                                         MEdge *e = edges + i;
1701                                         bool test = true;
1702
1703                                         if (em != NULL) {
1704                                                 if (index_array) {
1705                                                         index = index_array[i];
1706                                                 }
1707                                                 else {
1708                                                         index = i;
1709                                                 }
1710
1711                                                 if (index == ORIGINDEX_NONE) {
1712                                                         test = false;
1713                                                 }
1714                                                 else {
1715                                                         BMEdge *eed = BM_edge_at_index(em->bm, index);
1716
1717                                                         if (BM_elem_flag_test(eed, BM_ELEM_HIDDEN) ||
1718                                                             BM_elem_flag_test(eed->v1, BM_ELEM_SELECT) ||
1719                                                             BM_elem_flag_test(eed->v2, BM_ELEM_SELECT))
1720                                                         {
1721                                                                 test = false;
1722                                                         }
1723                                                 }
1724                                         }
1725
1726                                         if (test) {
1727                                                 retval |= snapEdge(ar, verts[e->v1].co, verts[e->v1].no, verts[e->v2].co, verts[e->v2].no,
1728                                                                    obmat, timat, ray_start, ray_start_local, ray_normal_local, mval,
1729                                                                    r_loc, r_no, r_dist_px, r_depth);
1730                                         }
1731                                 }
1732
1733                                 break;
1734                         }
1735                 }
1736         }
1737
1738         return retval;
1739
1740
1741 /* may extend later (for now just snaps to empty center) */
1742 static bool snapEmpty(short snap_mode, ARegion *ar, Object *ob, float obmat[4][4],
1743                       const float ray_start[3], const float ray_normal[3], const float mval[2],
1744                       float r_loc[3], float *UNUSED(r_no), float *r_dist_px, float *r_depth)
1745 {
1746         float imat[4][4];
1747         float ray_start_local[3], ray_normal_local[3];
1748         bool retval = false;
1749
1750         if (ob->transflag & OB_DUPLI) {
1751                 return retval;
1752         }
1753         /* for now only vertex supported */
1754         if (snap_mode != SCE_SNAP_MODE_VERTEX) {
1755                 return retval;
1756         }
1757
1758         invert_m4_m4(imat, obmat);
1759
1760         copy_v3_v3(ray_start_local, ray_start);
1761         copy_v3_v3(ray_normal_local, ray_normal);
1762
1763         mul_m4_v3(imat, ray_start_local);
1764         mul_mat3_m4_v3(imat, ray_normal_local);
1765
1766         switch (snap_mode) {
1767                 case SCE_SNAP_MODE_VERTEX:
1768                 {
1769                         const float zero_co[3] = {0.0f};
1770                         retval |= snapVertex(ar, zero_co, NULL, obmat, NULL, ray_start, ray_start_local, ray_normal_local, mval, r_loc, NULL, r_dist_px, r_depth);
1771                         break;
1772                 }
1773                 default:
1774                         break;
1775         }
1776
1777         return retval;
1778 }
1779
1780 static bool snapCamera(short snap_mode, ARegion *ar, Scene *scene, Object *object, float obmat[4][4],
1781                        const float ray_start[3], const float ray_normal[3], const float mval[2],
1782                        float r_loc[3], float *UNUSED(r_no), float *r_dist_px, float *r_depth)
1783 {
1784         float orig_camera_mat[4][4], orig_camera_imat[4][4], imat[4][4];
1785         bool retval = false;
1786         MovieClip *clip = BKE_object_movieclip_get(scene, object, false);
1787         MovieTracking *tracking;
1788         float ray_start_local[3], ray_normal_local[3];
1789
1790         if (clip == NULL) {
1791                 return retval;
1792         }
1793         if (object->transflag & OB_DUPLI) {
1794                 return retval;
1795         }
1796
1797         tracking = &clip->tracking;
1798
1799         BKE_tracking_get_camera_object_matrix(scene, object, orig_camera_mat);
1800
1801         invert_m4_m4(orig_camera_imat, orig_camera_mat);
1802         invert_m4_m4(imat, obmat);
1803
1804         switch (snap_mode) {
1805                 case SCE_SNAP_MODE_VERTEX:
1806                 {
1807                         MovieTrackingObject *tracking_object;
1808
1809                         for (tracking_object = tracking->objects.first;
1810                              tracking_object;
1811                              tracking_object = tracking_object->next)
1812                         {
1813                                 ListBase *tracksbase = BKE_tracking_object_get_tracks(tracking, tracking_object);
1814                                 MovieTrackingTrack *track;
1815                                 float reconstructed_camera_mat[4][4],
1816                                       reconstructed_camera_imat[4][4];
1817                                 float (*vertex_obmat)[4];
1818
1819                                 copy_v3_v3(ray_start_local, ray_start);
1820                                 copy_v3_v3(ray_normal_local, ray_normal);
1821
1822                                 if ((tracking_object->flag & TRACKING_OBJECT_CAMERA) == 0) {
1823                                         BKE_tracking_camera_get_reconstructed_interpolate(tracking, tracking_object,
1824                                                                                           CFRA, reconstructed_camera_mat);
1825
1826                                         invert_m4_m4(reconstructed_camera_imat, reconstructed_camera_mat);
1827                                 }
1828
1829                                 for (track = tracksbase->first; track; track = track->next) {
1830                                         float bundle_pos[3];
1831
1832                                         if ((track->flag & TRACK_HAS_BUNDLE) == 0) {
1833                                                 continue;
1834                                         }
1835
1836                                         copy_v3_v3(bundle_pos, track->bundle_pos);
1837                                         if (tracking_object->flag & TRACKING_OBJECT_CAMERA) {
1838                                                 mul_m4_v3(orig_camera_imat, ray_start_local);
1839                                                 mul_mat3_m4_v3(orig_camera_imat, ray_normal_local);
1840                                                 vertex_obmat = orig_camera_mat;
1841                                         }
1842                                         else {
1843                                                 mul_m4_v3(reconstructed_camera_imat, bundle_pos);
1844                                                 mul_m4_v3(imat, ray_start_local);
1845                                                 mul_mat3_m4_v3(imat, ray_normal_local);
1846                                                 vertex_obmat = obmat;
1847                                         }
1848
1849                                         retval |= snapVertex(ar, bundle_pos, NULL, vertex_obmat, NULL,
1850                                                              ray_start, ray_start_local, ray_normal_local, mval,
1851                                                              r_loc, NULL, r_dist_px, r_depth);
1852                                 }
1853                         }
1854
1855                         break;
1856                 }
1857                 default:
1858                         break;
1859         }
1860
1861         return retval;
1862 }
1863
1864 static bool snapObject(Scene *scene, short snap_mode, ARegion *ar, Object *ob, float obmat[4][4], bool use_obedit,
1865                        Object **r_ob, float r_obmat[4][4],
1866                        const float ray_start[3], const float ray_normal[3], const float ray_origin[3],
1867                        const float mval[2], float r_loc[3], float r_no[3], float *r_dist_px, float *r_depth)
1868 {
1869         bool retval = false;
1870         
1871         if (ob->type == OB_MESH) {
1872                 BMEditMesh *em;
1873                 DerivedMesh *dm;
1874                 bool do_bb = true;
1875                 
1876                 if (use_obedit) {
1877                         em = BKE_editmesh_from_object(ob);
1878                         dm = editbmesh_get_derived_cage(scene, ob, em, CD_MASK_BAREMESH);
1879                         do_bb = false;
1880                 }
1881                 else {
1882                         em = NULL;
1883                         dm = mesh_get_derived_final(scene, ob, CD_MASK_BAREMESH);
1884                 }
1885                 
1886                 retval = snapDerivedMesh(snap_mode, ar, ob, dm, em, obmat, ray_start, ray_normal, ray_origin, mval, r_loc, r_no, r_dist_px, r_depth, do_bb);
1887
1888                 dm->release(dm);
1889         }
1890         else if (ob->type == OB_ARMATURE) {
1891                 retval = snapArmature(snap_mode, ar, ob, ob->data, obmat, ray_start, ray_normal, mval, r_loc, r_no, r_dist_px, r_depth);
1892         }
1893         else if (ob->type == OB_CURVE) {
1894                 retval = snapCurve(snap_mode, ar, ob, ob->data, obmat, ray_start, ray_normal, mval, r_loc, r_no, r_dist_px, r_depth);
1895         }
1896         else if (ob->type == OB_EMPTY) {
1897                 retval = snapEmpty(snap_mode, ar, ob, obmat, ray_start, ray_normal, mval, r_loc, r_no, r_dist_px, r_depth);
1898         }
1899         else if (ob->type == OB_CAMERA) {
1900                 retval = snapCamera(snap_mode, ar, scene, ob, obmat, ray_start, ray_normal, mval, r_loc, r_no, r_dist_px, r_depth);
1901         }
1902         
1903         if (retval) {
1904                 if (r_ob) {
1905                         *r_ob = ob;
1906                         copy_m4_m4(r_obmat, obmat);
1907                 }
1908         }
1909
1910         return retval;
1911 }
1912
1913 static bool snapObjectsRay(Scene *scene, short snap_mode, Base *base_act, View3D *v3d, ARegion *ar, Object *obedit,
1914                            Object **r_ob, float r_obmat[4][4],
1915                            const float ray_start[3], const float ray_normal[3], const float ray_origin[3],
1916                            float *r_ray_dist,
1917                            const float mval[2], float *r_dist_px, float r_loc[3], float r_no[3], SnapMode mode)
1918 {
1919         Base *base;
1920         bool retval = false;
1921
1922         if (mode == SNAP_ALL && obedit) {
1923                 Object *ob = obedit;
1924
1925                 retval |= snapObject(scene, snap_mode, ar, ob, ob->obmat, true,
1926                                      r_ob, r_obmat,
1927                                      ray_start, ray_normal, ray_origin, mval, r_loc, r_no, r_dist_px, r_ray_dist);
1928         }
1929
1930         /* Need an exception for particle edit because the base is flagged with BA_HAS_RECALC_DATA
1931          * which makes the loop skip it, even the derived mesh will never change
1932          *
1933          * To solve that problem, we do it first as an exception. 
1934          * */
1935         base = base_act;
1936         if (base && base->object && base->object->mode & OB_MODE_PARTICLE_EDIT) {
1937                 Object *ob = base->object;
1938                 retval |= snapObject(scene, snap_mode, ar, ob, ob->obmat, false,
1939                                      r_ob, r_obmat,
1940                                      ray_start, ray_normal, ray_origin, mval, r_loc, r_no, r_dist_px, r_ray_dist);
1941         }
1942
1943         for (base = FIRSTBASE; base != NULL; base = base->next) {
1944                 if ((BASE_VISIBLE_BGMODE(v3d, scene, base)) &&
1945                     (base->flag & (BA_HAS_RECALC_OB | BA_HAS_RECALC_DATA)) == 0 &&
1946
1947                     ((mode == SNAP_NOT_SELECTED && (base->flag & (SELECT | BA_WAS_SEL)) == 0) ||
1948                      (ELEM(mode, SNAP_ALL, SNAP_NOT_OBEDIT) && base != base_act)))
1949                 {
1950                         Object *ob = base->object;
1951                         
1952                         if (ob->transflag & OB_DUPLI) {
1953                                 DupliObject *dupli_ob;
1954                                 ListBase *lb = object_duplilist(G.main->eval_ctx, scene, ob);
1955                                 
1956                                 for (dupli_ob = lb->first; dupli_ob; dupli_ob = dupli_ob->next) {
1957                                         retval |= snapObject(scene, snap_mode, ar, dupli_ob->ob, dupli_ob->mat, false,
1958                                                              r_ob, r_obmat,
1959                                                              ray_start, ray_normal, ray_origin, mval, r_loc, r_no, r_dist_px, r_ray_dist);
1960                                 }
1961                                 
1962                                 free_object_duplilist(lb);
1963                         }
1964                         
1965                         retval |= snapObject(scene, snap_mode, ar, ob, ob->obmat, false,
1966                                              r_ob, r_obmat,
1967                                              ray_start, ray_normal, ray_origin, mval, r_loc, r_no, r_dist_px, r_ray_dist);
1968                 }
1969         }
1970         
1971         return retval;
1972 }
1973 static bool snapObjects(Scene *scene, short snap_mode, Base *base_act, View3D *v3d, ARegion *ar, Object *obedit,
1974                         const float mval[2], float *r_dist_px,
1975                         float r_loc[3], float r_no[3], float *r_ray_dist, SnapMode mode)
1976 {
1977         float ray_start[3], ray_normal[3], ray_orgigin[3];
1978
1979         if (!ED_view3d_win_to_ray_ex(ar, v3d, mval, ray_orgigin, ray_normal, ray_start, true)) {
1980                 return false;
1981         }
1982
1983         return snapObjectsRay(scene, snap_mode, base_act, v3d, ar, obedit,
1984                               NULL, NULL,
1985                               ray_start, ray_normal, ray_orgigin, r_ray_dist,
1986                               mval, r_dist_px, r_loc, r_no, mode);
1987 }
1988
1989 bool snapObjectsTransform(TransInfo *t, const float mval[2], float *r_dist_px, float r_loc[3], float r_no[3], SnapMode mode)
1990 {
1991         float ray_dist = TRANSFORM_DIST_MAX_RAY;
1992         return snapObjects(t->scene, t->scene->toolsettings->snap_mode, t->scene->basact, t->view, t->ar, t->obedit,
1993                            mval, r_dist_px, r_loc, r_no, &ray_dist, mode);
1994 }
1995
1996 bool snapObjectsContext(bContext *C, const float mval[2], float *r_dist_px, float r_loc[3], float r_no[3], SnapMode mode)
1997 {
1998         ScrArea *sa = CTX_wm_area(C);
1999         View3D *v3d = sa->spacedata.first;
2000         Scene *scene = CTX_data_scene(C);
2001         ARegion *ar = CTX_wm_region(C);
2002         Object *obedit = CTX_data_edit_object(C);
2003         float ray_dist = TRANSFORM_DIST_MAX_RAY;
2004
2005         return snapObjects(scene, scene->toolsettings->snap_mode, scene->basact, v3d, ar, obedit,
2006                            mval, r_dist_px, r_loc, r_no, &ray_dist, mode);
2007 }
2008
2009 bool snapObjectsEx(Scene *scene, Base *base_act, View3D *v3d, ARegion *ar, Object *obedit, short snap_mode,
2010                    const float mval[2], float *r_dist_px,
2011                    float r_loc[3], float r_no[3], float *r_ray_dist, SnapMode mode)
2012 {
2013         return snapObjects(scene, snap_mode, base_act, v3d, ar, obedit,
2014                            mval, r_dist_px,
2015                            r_loc, r_no, r_ray_dist, mode);
2016 }
2017 bool snapObjectsRayEx(Scene *scene, Base *base_act, View3D *v3d, ARegion *ar, Object *obedit, short snap_mode,
2018                       Object **r_ob, float r_obmat[4][4],
2019                       const float ray_start[3], const float ray_normal[3], float *r_ray_dist,
2020                       const float mval[2], float *r_dist_px, float r_loc[3], float r_no[3], SnapMode mode)
2021 {
2022         return snapObjectsRay(scene, snap_mode, base_act, v3d, ar, obedit,
2023                               r_ob, r_obmat,
2024                               ray_start, ray_normal, ray_start, r_ray_dist,
2025                               mval, r_dist_px, r_loc, r_no, mode);
2026 }
2027
2028 /******************** PEELING *********************************/
2029
2030
2031 static int cmpPeel(const void *arg1, const void *arg2)
2032 {
2033         const DepthPeel *p1 = arg1;
2034         const DepthPeel *p2 = arg2;
2035         int val = 0;
2036         
2037         if (p1->depth < p2->depth) {
2038                 val = -1;
2039         }
2040         else if (p1->depth > p2->depth) {
2041                 val = 1;
2042         }
2043         
2044         return val;
2045 }
2046
2047 static void removeDoublesPeel(ListBase *depth_peels)
2048 {
2049         DepthPeel *peel;
2050         
2051         for (peel = depth_peels->first; peel; peel = peel->next) {
2052                 DepthPeel *next_peel = peel->next;
2053
2054                 if (next_peel && fabsf(peel->depth - next_peel->depth) < 0.0015f) {
2055                         peel->next = next_peel->next;
2056                         
2057                         if (next_peel->next) {
2058                                 next_peel->next->prev = peel;
2059                         }
2060                         
2061                         MEM_freeN(next_peel);
2062                 }
2063         }
2064 }
2065
2066 static void addDepthPeel(ListBase *depth_peels, float depth, float p[3], float no[3], Object *ob)
2067 {
2068         DepthPeel *peel = MEM_callocN(sizeof(DepthPeel), "DepthPeel");
2069         
2070         peel->depth = depth;
2071         peel->ob = ob;
2072         copy_v3_v3(peel->p, p);
2073         copy_v3_v3(peel->no, no);
2074         
2075         BLI_addtail(depth_peels, peel);
2076         
2077         peel->flag = 0;
2078 }
2079
2080 static bool peelDerivedMesh(Object *ob, DerivedMesh *dm, float obmat[4][4],
2081                             const float ray_start[3], const float ray_normal[3], const float UNUSED(mval[2]),
2082                             ListBase *depth_peels)
2083 {
2084         bool retval = false;
2085         int totvert = dm->getNumVerts(dm);
2086         int totface = dm->getNumTessFaces(dm);
2087         
2088         if (totvert > 0) {
2089                 float imat[4][4];
2090                 float timat[3][3]; /* transpose inverse matrix for normals */
2091                 float ray_start_local[3], ray_normal_local[3];
2092                 int test = 1;
2093
2094                 invert_m4_m4(imat, obmat);
2095
2096                 transpose_m3_m4(timat, imat);
2097                 
2098                 copy_v3_v3(ray_start_local, ray_start);
2099                 copy_v3_v3(ray_normal_local, ray_normal);
2100                 
2101                 mul_m4_v3(imat, ray_start_local);
2102                 mul_mat3_m4_v3(imat, ray_normal_local);
2103                 
2104                 
2105                 /* If number of vert is more than an arbitrary limit, 
2106                  * test against boundbox first
2107                  * */
2108                 if (totface > 16) {
2109                         struct BoundBox *bb = BKE_object_boundbox_get(ob);
2110                         test = BKE_boundbox_ray_hit_check(bb, ray_start_local, ray_normal_local, NULL);
2111                 }
2112                 
2113                 if (test == 1) {
2114                         MVert *verts = dm->getVertArray(dm);
2115                         MFace *faces = dm->getTessFaceArray(dm);
2116                         int i;
2117                         
2118                         for (i = 0; i < totface; i++) {
2119                                 MFace *f = faces + i;
2120                                 float lambda;
2121                                 int result;
2122                                 
2123                                 
2124                                 result = isect_ray_tri_threshold_v3(ray_start_local, ray_normal_local, verts[f->v1].co, verts[f->v2].co, verts[f->v3].co, &lambda, NULL, 0.001);
2125                                 
2126                                 if (result) {
2127                                         float location[3], normal[3];
2128                                         float intersect[3];
2129                                         float new_depth;
2130                                         
2131                                         copy_v3_v3(intersect, ray_normal_local);
2132                                         mul_v3_fl(intersect, lambda);
2133                                         add_v3_v3(intersect, ray_start_local);
2134                                         
2135                                         copy_v3_v3(location, intersect);
2136                                         
2137                                         if (f->v4)
2138                                                 normal_quad_v3(normal, verts[f->v1].co, verts[f->v2].co, verts[f->v3].co, verts[f->v4].co);
2139                                         else
2140                                                 normal_tri_v3(normal, verts[f->v1].co, verts[f->v2].co, verts[f->v3].co);
2141
2142                                         mul_m4_v3(obmat, location);
2143                                         
2144                                         new_depth = len_v3v3(location, ray_start);
2145                                         
2146                                         mul_m3_v3(timat, normal);
2147                                         normalize_v3(normal);
2148
2149                                         addDepthPeel(depth_peels, new_depth, location, normal, ob);
2150                                 }
2151                 
2152                                 if (f->v4 && result == 0) {
2153                                         result = isect_ray_tri_threshold_v3(ray_start_local, ray_normal_local, verts[f->v3].co, verts[f->v4].co, verts[f->v1].co, &lambda, NULL, 0.001);
2154                                         
2155                                         if (result) {
2156                                                 float location[3], normal[3];
2157                                                 float intersect[3];
2158                                                 float new_depth;
2159                                                 
2160                                                 copy_v3_v3(intersect, ray_normal_local);
2161                                                 mul_v3_fl(intersect, lambda);
2162                                                 add_v3_v3(intersect, ray_start_local);
2163                                                 
2164                                                 copy_v3_v3(location, intersect);
2165                                                 
2166                                                 if (f->v4)
2167                                                         normal_quad_v3(normal, verts[f->v1].co, verts[f->v2].co, verts[f->v3].co, verts[f->v4].co);
2168                                                 else
2169                                                         normal_tri_v3(normal, verts[f->v1].co, verts[f->v2].co, verts[f->v3].co);
2170
2171                                                 mul_m4_v3(obmat, location);
2172                                                 
2173                                                 new_depth = len_v3v3(location, ray_start);
2174                                                 
2175                                                 mul_m3_v3(timat, normal);
2176                                                 normalize_v3(normal);
2177         
2178                                                 addDepthPeel(depth_peels, new_depth, location, normal, ob);
2179                                         }
2180                                 }
2181                         }
2182                 }
2183         }
2184
2185         return retval;
2186
2187
2188 static bool peelObjects(Scene *scene, View3D *v3d, ARegion *ar, Object *obedit,
2189                         ListBase *depth_peels, const float mval[2], SnapMode mode)
2190 {
2191         Base *base;
2192         bool retval = false;
2193         float ray_start[3], ray_normal[3];
2194         
2195         if (ED_view3d_win_to_ray(ar, v3d, mval, ray_start, ray_normal, true) == false) {
2196                 return false;
2197         }
2198
2199         for (base = scene->base.first; base != NULL; base = base->next) {
2200                 if (BASE_SELECTABLE(v3d, base)) {
2201                         Object *ob = base->object;
2202
2203                         if (ob->transflag & OB_DUPLI) {
2204                                 DupliObject *dupli_ob;
2205                                 ListBase *lb = object_duplilist(G.main->eval_ctx, scene, ob);
2206                                 
2207                                 for (dupli_ob = lb->first; dupli_ob; dupli_ob = dupli_ob->next) {
2208                                         Object *dob = dupli_ob->ob;
2209                                         
2210                                         if (dob->type == OB_MESH) {
2211                                                 BMEditMesh *em;
2212                                                 DerivedMesh *dm = NULL;
2213                                                 bool val;
2214
2215                                                 if (dob != obedit) {
2216                                                         dm = mesh_get_derived_final(scene, dob, CD_MASK_BAREMESH);
2217                                                         
2218                                                         val = peelDerivedMesh(dob, dm, dob->obmat, ray_start, ray_normal, mval, depth_peels);
2219                                                 }
2220                                                 else {
2221                                                         em = BKE_editmesh_from_object(dob);
2222                                                         dm = editbmesh_get_derived_cage(scene, obedit, em, CD_MASK_BAREMESH);
2223                                                         
2224                                                         val = peelDerivedMesh(dob, dm, dob->obmat, ray_start, ray_normal, mval, depth_peels);
2225                                                 }
2226
2227                                                 retval = retval || val;
2228                                                 
2229                                                 dm->release(dm);
2230                                         }
2231                                 }
2232                                 
2233                                 free_object_duplilist(lb);
2234                         }
2235                         
2236                         if (ob->type == OB_MESH) {
2237                                 bool val = false;
2238
2239                                 if (ob != obedit && ((mode == SNAP_NOT_SELECTED && (base->flag & (SELECT | BA_WAS_SEL)) == 0) || ELEM(mode, SNAP_ALL, SNAP_NOT_OBEDIT))) {
2240                                         DerivedMesh *dm = mesh_get_derived_final(scene, ob, CD_MASK_BAREMESH);
2241                                         
2242                                         val = peelDerivedMesh(ob, dm, ob->obmat, ray_start, ray_normal, mval, depth_peels);
2243                                         dm->release(dm);
2244                                 }
2245                                 else if (ob == obedit && mode != SNAP_NOT_OBEDIT) {
2246                                         BMEditMesh *em = BKE_editmesh_from_object(ob);
2247                                         DerivedMesh *dm = editbmesh_get_derived_cage(scene, obedit, em, CD_MASK_BAREMESH);
2248                                         
2249                                         val = peelDerivedMesh(ob, dm, ob->obmat, ray_start, ray_normal, mval, depth_peels);
2250                                         dm->release(dm);
2251                                 }
2252                                         
2253                                 retval = retval || val;
2254                                 
2255                         }
2256                 }
2257         }
2258         
2259         BLI_sortlist(depth_peels, cmpPeel);
2260         removeDoublesPeel(depth_peels);
2261         
2262         return retval;
2263 }
2264
2265 bool peelObjectsTransForm(TransInfo *t, ListBase *depth_peels, const float mval[2], SnapMode mode)
2266 {
2267         return peelObjects(t->scene, t->view, t->ar, t->obedit, depth_peels, mval, mode);
2268 }
2269
2270 bool peelObjectsContext(bContext *C, ListBase *depth_peels, const float mval[2], SnapMode mode)
2271 {
2272         Scene *scene = CTX_data_scene(C);
2273         ScrArea *sa = CTX_wm_area(C);
2274         View3D *v3d = sa->spacedata.first;
2275         ARegion *ar = CTX_wm_region(C);
2276         Object *obedit = CTX_data_edit_object(C);
2277
2278         return peelObjects(scene, v3d, ar, obedit, depth_peels, mval, mode);
2279 }
2280
2281 /******************** NODES ***********************************/
2282
2283 static bool snapNodeTest(View2D *v2d, bNode *node, SnapMode mode)
2284 {
2285         /* node is use for snapping only if a) snap mode matches and b) node is inside the view */
2286         return ((mode == SNAP_NOT_SELECTED && !(node->flag & NODE_SELECT)) ||
2287                 (mode == SNAP_ALL && !(node->flag & NODE_ACTIVE))) &&
2288                 (node->totr.xmin < v2d->cur.xmax && node->totr.xmax > v2d->cur.xmin &&
2289                  node->totr.ymin < v2d->cur.ymax && node->totr.ymax > v2d->cur.ymin);
2290 }
2291
2292 static NodeBorder snapNodeBorder(int snap_node_mode)
2293 {
2294         switch (snap_node_mode) {
2295                 case SCE_SNAP_MODE_NODE_X:
2296                         return NODE_LEFT | NODE_RIGHT;
2297                 case SCE_SNAP_MODE_NODE_Y:
2298                         return NODE_TOP | NODE_BOTTOM;
2299                 case SCE_SNAP_MODE_NODE_XY:
2300                         return NODE_LEFT | NODE_RIGHT | NODE_TOP | NODE_BOTTOM;
2301         }
2302         return 0;
2303 }
2304
2305 static bool snapNode(ToolSettings *ts, SpaceNode *UNUSED(snode), ARegion *ar, bNode *node, const int mval[2],
2306                      float r_loc[2], float *r_dist_px, char *r_node_border)
2307 {
2308         View2D *v2d = &ar->v2d;
2309         NodeBorder border = snapNodeBorder(ts->snap_node_mode);
2310         bool retval = false;
2311         rcti totr;
2312         int new_dist;
2313         
2314         UI_view2d_view_to_region_rcti(v2d, &node->totr, &totr);
2315         
2316         if (border & NODE_LEFT) {
2317                 new_dist = abs(totr.xmin - mval[0]);
2318                 if (new_dist < *r_dist_px) {
2319                         UI_view2d_region_to_view(v2d, totr.xmin, mval[1], &r_loc[0], &r_loc[1]);
2320                         *r_dist_px = new_dist;
2321                         *r_node_border = NODE_LEFT;
2322                         retval = true;
2323                 }
2324         }
2325         
2326         if (border & NODE_RIGHT) {
2327                 new_dist = abs(totr.xmax - mval[0]);
2328                 if (new_dist < *r_dist_px) {
2329                         UI_view2d_region_to_view(v2d, totr.xmax, mval[1], &r_loc[0], &r_loc[1]);
2330                         *r_dist_px = new_dist;
2331                         *r_node_border = NODE_RIGHT;
2332                         retval = true;
2333                 }
2334         }
2335         
2336         if (border & NODE_BOTTOM) {
2337                 new_dist = abs(totr.ymin - mval[1]);
2338                 if (new_dist < *r_dist_px) {
2339                         UI_view2d_region_to_view(v2d, mval[0], totr.ymin, &r_loc[0], &r_loc[1]);
2340                         *r_dist_px = new_dist;
2341                         *r_node_border = NODE_BOTTOM;
2342                         retval = true;
2343                 }
2344         }
2345         
2346         if (border & NODE_TOP) {
2347                 new_dist = abs(totr.ymax - mval[1]);
2348                 if (new_dist < *r_dist_px) {
2349                         UI_view2d_region_to_view(v2d, mval[0], totr.ymax, &r_loc[0], &r_loc[1]);
2350                         *r_dist_px = new_dist;
2351                         *r_node_border = NODE_TOP;
2352                         retval = true;
2353                 }
2354         }
2355         
2356         return retval;
2357 }
2358
2359 static bool snapNodes(ToolSettings *ts, SpaceNode *snode, ARegion *ar, const int mval[2],
2360                      float *r_dist_px, float r_loc[2], char *r_node_border, SnapMode mode)
2361 {
2362         bNodeTree *ntree = snode->edittree;
2363         bNode *node;
2364         bool retval = false;
2365         
2366         *r_node_border = 0;
2367         
2368         for (node = ntree->nodes.first; node; node = node->next) {
2369                 if (snapNodeTest(&ar->v2d, node, mode))
2370                         retval |= snapNode(ts, snode, ar, node, mval, r_loc, r_dist_px, r_node_border);
2371         }
2372         
2373         return retval;
2374 }
2375
2376 bool snapNodesTransform(TransInfo *t, const int mval[2], float *r_dist_px, float r_loc[2], char *r_node_border, SnapMode mode)
2377 {
2378         return snapNodes(t->settings, t->sa->spacedata.first, t->ar, mval, r_dist_px, r_loc, r_node_border, mode);
2379 }
2380
2381 bool snapNodesContext(bContext *C, const int mval[2], float *r_dist_px, float r_loc[2], char *r_node_border, SnapMode mode)
2382 {
2383         Scene *scene = CTX_data_scene(C);
2384         ARegion *ar = CTX_wm_region(C);
2385         return snapNodes(scene->toolsettings, CTX_wm_space_node(C), ar, mval, r_dist_px, r_loc, r_node_border, mode);
2386 }
2387
2388 /*================================================================*/
2389
2390 static void applyGridIncrement(TransInfo *t, float *val, int max_index, const float fac[3], GearsType action);
2391
2392
2393 void snapGridIncrementAction(TransInfo *t, float *val, GearsType action)
2394 {
2395         float fac[3];
2396
2397         fac[NO_GEARS]    = t->snap[0];
2398         fac[BIG_GEARS]   = t->snap[1];
2399         fac[SMALL_GEARS] = t->snap[2];
2400         
2401         applyGridIncrement(t, val, t->idx_max, fac, action);
2402 }
2403
2404
2405 void snapGridIncrement(TransInfo *t, float *val)
2406 {
2407         GearsType action;
2408
2409         // Only do something if using Snap to Grid
2410         if (t->tsnap.mode != SCE_SNAP_MODE_INCREMENT)
2411                 return;
2412
2413         action = activeSnap(t) ? BIG_GEARS : NO_GEARS;
2414
2415         if (action == BIG_GEARS && (t->modifiers & MOD_PRECISION)) {
2416                 action = SMALL_GEARS;
2417         }
2418
2419         snapGridIncrementAction(t, val, action);
2420 }
2421
2422
2423 static void applyGridIncrement(TransInfo *t, float *val, int max_index, const float fac[3], GearsType action)
2424 {
2425         int i;
2426         float asp[3] = {1.0f, 1.0f, 1.0f}; // TODO: Remove hard coded limit here (3)
2427
2428         if (max_index > 2) {
2429                 printf("applyGridIncrement: invalid index %d, clamping\n", max_index);
2430                 max_index = 2;
2431         }
2432
2433         // Early bailing out if no need to snap
2434         if (fac[action] == 0.0f)
2435                 return;
2436         
2437         /* evil hack - snapping needs to be adapted for image aspect ratio */
2438         if ((t->spacetype == SPACE_IMAGE) && (t->mode == TFM_TRANSLATION)) {
2439                 if (t->options & CTX_MASK) {
2440                         ED_space_image_get_aspect(t->sa->spacedata.first, asp, asp + 1);
2441                 }
2442                 else if (t->options & CTX_PAINT_CURVE) {
2443                         asp[0] = asp[1] = 1.0;
2444                 }
2445                 else {
2446                         ED_space_image_get_uv_aspect(t->sa->spacedata.first, asp, asp + 1);
2447                 }
2448         }
2449
2450         for (i = 0; i <= max_index; i++) {
2451                 val[i] = fac[action] * asp[i] * floorf(val[i] / (fac[action] * asp[i]) + 0.5f);
2452         }
2453 }