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