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