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