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