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