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