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