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