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