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