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