Cleanup of particle object visualization code. No new features and hopefully no new...
[blender.git] / source / blender / src / edit.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): none yet.
26  *
27  * ***** END GPL LICENSE BLOCK *****
28  */
29
30 #include <math.h>
31 #include <string.h>
32
33 #ifdef HAVE_CONFIG_H
34 #include <config.h>
35 #endif
36
37 #ifndef WIN32
38 #include <unistd.h>
39 #else
40 #include <io.h>
41 #endif   
42
43 #include "MEM_guardedalloc.h"
44
45 #include "BMF_Api.h"
46
47 #include "PIL_time.h"
48
49 #include "DNA_action_types.h"
50 #include "DNA_armature_types.h"
51 #include "DNA_curve_types.h"
52 #include "DNA_group_types.h"
53 #include "DNA_ipo_types.h"
54 #include "DNA_lattice_types.h"
55 #include "DNA_meta_types.h"
56 #include "DNA_mesh_types.h"
57 #include "DNA_modifier_types.h"
58 #include "DNA_object_types.h"
59 #include "DNA_particle_types.h"
60 #include "DNA_screen_types.h"
61 #include "DNA_scene_types.h"
62 #include "DNA_space_types.h"
63 #include "DNA_view3d_types.h"
64 #include "DNA_userdef_types.h"  /* for U.dupflag */
65 #include "BLI_blenlib.h"
66 #include "BLI_arithb.h"
67 #include "BLI_editVert.h"
68 #include "BLI_linklist.h"
69
70 #include "BKE_action.h"
71 #include "BKE_armature.h"
72 #include "BKE_anim.h"
73 #include "BKE_curve.h"
74 #include "BKE_depsgraph.h"
75 #include "BKE_DerivedMesh.h"
76 #include "BKE_displist.h"
77 #include "BKE_global.h"
78 #include "BKE_ipo.h"
79 #include "BKE_lattice.h"
80 #include "BKE_mesh.h"
81 #include "BKE_modifier.h"
82 #include "BKE_object.h"
83 #include "BKE_particle.h"
84 #include "BKE_utildefines.h"
85
86 #ifdef WITH_VERSE
87 #include "BKE_verse.h"
88 #endif
89
90 #include "BIF_editmesh.h"
91 #include "BIF_editview.h"
92 #include "BIF_editarmature.h"
93 #include "BIF_editparticle.h"
94 #include "BIF_gl.h"
95 #include "BIF_glutil.h"
96 #include "BIF_interface.h"
97 #include "BIF_mywindow.h"
98 #include "BIF_resources.h"
99 #include "BIF_space.h"
100 #include "BIF_screen.h"
101 #include "BIF_toolbox.h"
102 #include "BIF_sketch.h"
103
104 #ifdef WITH_VERSE
105 #include "BIF_verse.h"
106 #endif
107
108 #include "BSE_edit.h"
109 #include "BSE_drawipo.h"
110 #include "BSE_drawview.h"
111 #include "BSE_trans_types.h"
112 #include "BSE_view.h"
113
114 #include "BDR_editobject.h"
115 #include "BDR_editmball.h"
116 #include "BDR_editcurve.h"
117
118 /* old stuff */
119 #include "blendef.h"
120 #include "mydevice.h"
121
122 /*#include "armature.h"*/
123 /*  #include "edit.h" */
124 #include "nla.h"
125 #include "transform.h"
126
127 #ifdef __NLA
128 #include "BIF_editarmature.h"
129 #endif
130
131
132 /* circle selection callback */
133 typedef void (*select_CBfunc)(short selecting, Object *editobj, short *mval, float rad);
134
135 extern void obedit_selectionCB(short selecting, Object *editobj, 
136                                short *mval, float rad);
137 extern void uvedit_selectionCB(short selecting, Object *editobj, 
138                                short *mval, float rad);
139
140 static void circle_selectCB(select_CBfunc func);
141
142
143 /* flag==2 only border, flag==3 cross+border
144    flag==5 cross + border + start&end frame
145  */
146 int get_border(rcti *rect, short flag)
147 {
148         float dvec[4], fac1, fac2;
149         int retval=1;
150         unsigned short event= 0;
151         short mval[2], mvalo[4], val, x1, y1;
152         char str[64];
153
154         mywinset(G.curscreen->mainwin);
155         
156         /* slightly larger, 1 pixel at the edge */
157         glReadBuffer(GL_FRONT);
158         glDrawBuffer(GL_FRONT);
159
160         /* removed my_get_frontbuffer, this crashes when it gets a part outside the screen */
161         /* solved it with just a redraw! */
162
163         mywinset(curarea->win);
164         
165         glDrawBuffer(GL_FRONT);
166         persp(PERSP_WIN);
167         initgrabz(0.0, 0.0, 0.0);
168         
169         if(flag & 1) {
170                 getmouseco_areawin(mvalo);
171
172                 /* draws the selection initial cross */
173                 sdrawXORline4(0, 0,  mvalo[1],  curarea->winx,  mvalo[1]);
174                 sdrawXORline4(1, mvalo[0],  0,  mvalo[0],  curarea->winy); 
175                 bglFlush();
176                 
177                 while(TRUE) {
178                 
179                         /* selection loop while mouse pressed */
180                         getmouseco_areawin(mval);
181                         
182                         if(mvalo[0]!=mval[0] || mvalo[1]!=mval[1]) {
183
184                                 /* aiming cross */
185                                 sdrawXORline4(0, 0,  mval[1],  curarea->winx,  mval[1]);
186                                 sdrawXORline4(1, mval[0],  0,  mval[0],  curarea->winy);
187                                 bglFlush();
188
189                                 mvalo[0]= mval[0];
190                                 mvalo[1]= mval[1];
191                         }
192                         event= extern_qread(&val);
193
194                         if(event && val) {
195
196                                 /* for when a renderwindow is open, and a mouse cursor activates it */
197                                 persp(PERSP_VIEW);
198                                 mywinset(curarea->win);
199                                 persp(PERSP_WIN);
200                                 
201                                 if(event==ESCKEY) {
202                                         retval= 0;
203                                         break;
204                                 }
205                                 else if(event==BKEY) {
206                                         /* b has been pressed twice: proceed with circle select */
207                                         retval= 0;
208                                         break;
209                                 }
210                                 else if(event==LEFTMOUSE) break;
211                                 else if(event==MIDDLEMOUSE) break;
212                                 else if(event==RIGHTMOUSE) break;
213                         }
214                         else PIL_sleep_ms(10);
215                         
216                 } /* end while (TRUE) */
217
218                 /* erase XORed lines */
219                 sdrawXORline4(-1, 0, 0, 0, 0);
220         }
221         else getmouseco_areawin(mval);
222         
223         if(retval) {
224                 /* box select */
225                 x1= mval[0];
226                 y1= mval[1];
227                 
228                 getmouseco_areawin(mvalo);
229
230                 sdrawXORline4(0, x1, y1, x1, mvalo[1]); 
231                 sdrawXORline4(1, x1, mvalo[1], mvalo[0], mvalo[1]); 
232                 sdrawXORline4(2, mvalo[0], mvalo[1], mvalo[0], y1); 
233                 sdrawXORline4(3,  mvalo[0], y1, x1, y1); 
234                 bglFlush();
235                         
236                 while(TRUE) {
237                         getmouseco_areawin(mval);
238                         if(mvalo[0]!=mval[0] || mvalo[1]!=mval[1]) {
239
240                                 sdrawXORline4(0, x1, y1, x1, mval[1]); 
241                                 sdrawXORline4(1, x1, mval[1], mval[0], mval[1]); 
242                                 sdrawXORline4(2, mval[0], mval[1], mval[0], y1); 
243                                 sdrawXORline4(3,  mval[0], y1, x1, y1); 
244                                 
245                                 /* draw size information in corner */
246                                 if(curarea->spacetype==SPACE_VIEW3D) {
247                                         BIF_ThemeColor(TH_BACK);
248                                         glRecti(10, 25, 250, 40);
249         
250                                         if(G.vd->persp==V3D_ORTHO) {
251                                                 window_to_3d(dvec, mvalo[0]-x1, mvalo[1]-y1);
252         
253                                                 sprintf(str, "X %.4f  Y %.4f  Z %.4f  Dia %.4f", dvec[0], dvec[1], dvec[2], sqrt(dvec[0]*dvec[0]+dvec[1]*dvec[1]+dvec[2]*dvec[2]));
254                                                 glColor3f(0.0, 0.0, 0.0); 
255                                                 glRasterPos2i(15,  27);
256                                                 BMF_DrawString(G.fonts, str);
257                                                 glColor3f(0.7, 0.7, 0.7); 
258                                                 glRasterPos2i(16,  28);
259                                                 BMF_DrawString(G.fonts, str);
260                                         }
261                                         else if(G.vd->persp==V3D_CAMOB) {
262                                                 rctf vb;
263         
264                                                 calc_viewborder(G.vd, &vb);
265         
266                                                 fac1= (mvalo[0]-x1)/( (float) (vb.xmax-vb.xmin) );
267                                                 fac1*= 0.01*G.scene->r.size*G.scene->r.xsch;
268                                                 
269                                                 fac2= (mvalo[1]-y1)/( (float) (vb.ymax-vb.ymin) );
270                                                 fac2*= 0.01*G.scene->r.size*G.scene->r.ysch;
271                                                 
272                                                 sprintf(str, "X %.1f  Y %.1f  Dia %.1f", fabs(fac1), fabs(fac2), sqrt(fac1*fac1 + fac2*fac2) );
273                                                 glColor3f(0.0, 0.0, 0.0); 
274                                                 glRasterPos2i(15,  27);
275                                                 BMF_DrawString(G.fonts, str);
276                                                 glColor3f(0.7, 0.7, 0.7); 
277                                                 glRasterPos2i(16,  28);
278                                                 BMF_DrawString(G.fonts, str);
279                                         }
280                                 }
281                                 else if(curarea->spacetype==SPACE_IPO) {
282                                         SpaceIpo *sipo= curarea->spacedata.first;
283         
284                                         BIF_ThemeColor(TH_BACK);
285                                         glRecti(20, 30, 170, 40);
286                                                                 
287                                         mvalo[2]= x1;
288                                         mvalo[3]= y1;
289                                         areamouseco_to_ipoco(&sipo->v2d, mval, dvec, dvec+1);
290                                         areamouseco_to_ipoco(&sipo->v2d, mvalo+2, dvec+2, dvec+3);
291
292                                         if (flag == 5) {
293                                                 /* setting preview range */
294                                                 if (dvec[0] < dvec[2])
295                                                         sprintf(str, "Preview Range: %d to %d", (int)dvec[0], (int)dvec[2]);
296                                                 else
297                                                         sprintf(str, "Preview Range: %d to %d", (int)dvec[2], (int)dvec[0]);
298                                         }
299                                         else
300                                                 sprintf(str, "Time: %.4f  Y %.4f", dvec[0]-dvec[2], dvec[1]-dvec[3]);
301                                         
302                                         BIF_ThemeColor(TH_BACK);
303                                         glRecti(14, 24, 165, 38);
304                                         
305                                         glColor3f(0.0, 0.0, 0.0); 
306                                         glRasterPos2i(15,  27);
307                                         BMF_DrawString(G.fonts, str);
308                                         glColor3f(0.8, 0.8, 0.8); 
309                                         glRasterPos2i(16,  28);
310                                         BMF_DrawString(G.fonts, str);
311                                 }
312                                 else if ((ELEM3(curarea->spacetype, SPACE_ACTION, SPACE_NLA, SPACE_TIME)) && flag==5) {
313                                         /* only while setting preview range */
314                                         View2D *v2d;
315                                         
316                                         switch (curarea->spacetype) 
317                                         {
318                                                 case SPACE_ACTION:
319                                                 {
320                                                         SpaceAction *saaction= curarea->spacedata.first;
321                                                         v2d= &saaction->v2d;
322                                                 }
323                                                         break;
324                                                 case SPACE_NLA:
325                                                 {
326                                                         SpaceNla *snla= curarea->spacedata.first;
327                                                         v2d= &snla->v2d;
328                                                 }
329                                                         break;
330                                                 default:
331                                                         v2d= G.v2d;
332                                                         break;
333                                         }
334                                         
335                                         mvalo[2]= x1;
336                                         mvalo[3]= y1;
337                                         areamouseco_to_ipoco(v2d, mval, dvec, dvec+1);
338                                         areamouseco_to_ipoco(v2d, mvalo+2, dvec+2, dvec+3);
339                                         
340                                         if (dvec[0] < dvec[2])
341                                                 sprintf(str, "Preview Range: %d to %d", (int)dvec[0], (int)dvec[2]);
342                                         else
343                                                 sprintf(str, "Preview Range: %d to %d", (int)dvec[2], (int)dvec[0]);
344                                         
345                                         BIF_ThemeColor(TH_BACK);
346                                         glRecti(14, 24, 165, 38);
347                                         
348                                         glColor3f(0.0, 0.0, 0.0); 
349                                         glRasterPos2i(15,  27);
350                                         BMF_DrawString(G.fonts, str);
351                                         glColor3f(0.8, 0.8, 0.8); 
352                                         glRasterPos2i(16,  28);
353                                         BMF_DrawString(G.fonts, str);
354                                 }
355
356                                 bglFlush();
357
358                                 mvalo[0]= mval[0];
359                                 mvalo[1]= mval[1];
360                         }
361                         
362                         event= extern_qread(&val);
363                         
364                         if(event && val==0) {
365                                 /* still because of the renderwindow... */
366                                 persp(PERSP_VIEW);
367                                 mywinset(curarea->win);
368                                 persp(PERSP_WIN);
369                                 
370                                 if(event==ESCKEY) {
371                                         retval= 0;
372                                         break;
373                                 }
374                                 else if(event==LEFTMOUSE) break;
375                                 else if(event==MIDDLEMOUSE) break;
376                                 else if(event==RIGHTMOUSE) break;
377                         }
378                         
379                 } /* end while (TRUE) */
380                 sdrawXORline4(-1, 0, 0, 0, 0);
381                 
382                 if(retval) {
383                         rect->xmin= x1;
384                         rect->ymin= y1;
385                         rect->xmax= mval[0];
386                         rect->ymax= mval[1];
387                         retval= event;
388
389                         /* normalize */
390                         if(rect->xmin>rect->xmax) SWAP(int, rect->xmin, rect->xmax);
391                         if(rect->ymin>rect->ymax) SWAP(int, rect->ymin, rect->ymax);
392                         
393                         if(rect->xmin==rect->xmax) retval= 0;
394                         if(rect->ymin==rect->ymax) retval= 0;
395                 }
396         }
397
398
399         /* clear */
400         if(event!=BKEY) {
401                 if ELEM(curarea->spacetype, SPACE_VIEW3D, SPACE_IPO) {
402                         scrarea_queue_winredraw(curarea);
403                 }
404                 else if ELEM3(curarea->spacetype, SPACE_ACTION, SPACE_NLA, SPACE_TIME) {
405                         scrarea_queue_winredraw(curarea); // only really needed for 
406                 }
407         }
408         
409         bglFlush();
410         glReadBuffer(GL_BACK);
411         glDrawBuffer(GL_BACK);
412
413         persp(PERSP_VIEW);
414         
415         /* pressed B again ? -> brush select */
416         if(event==BKEY) {
417                 setlinestyle(0);
418                 switch (curarea->spacetype) {
419                 case SPACE_VIEW3D:
420                         if (G.obedit) {
421                                 if ELEM4(G.obedit->type, OB_MESH, OB_CURVE, OB_SURF, OB_LATTICE) {
422                                         circle_selectCB(&obedit_selectionCB);
423                                 }
424                         }
425                         else if (FACESEL_PAINT_TEST) {
426                                 circle_selectCB(&obedit_selectionCB);
427                         }
428                         else if (G.f&G_PARTICLEEDIT) {
429                                 circle_selectCB(&PE_selectionCB);
430                         }
431                         return 0;
432                         
433                 case SPACE_IMAGE: // brush select in UV editor
434                         circle_selectCB(&uvedit_selectionCB);
435                         // this is a hack; we return 0 that the caller from get_border
436                         // doesn't execute the selection code for border select..
437                         return 0;
438                 }
439         }
440         return retval;
441 }
442
443 void draw_sel_circle(short *mval, short *mvalo, float rad, float rado, int selecting)
444 {
445         static short no_mvalo=0;
446
447         if(mval==NULL && mvalo==NULL) { /* signal */
448                 no_mvalo= 1;
449                 return;
450         }
451
452         persp(PERSP_WIN);
453         glReadBuffer(GL_FRONT);
454         glDrawBuffer(GL_FRONT);
455         //setlinestyle(2);
456
457         /* draw circle */
458         if(mvalo && no_mvalo==0) {
459                 fdrawXORcirc(mvalo[0], mvalo[1], rado);
460         }
461         
462         if(mval) {
463                 fdrawXORcirc(mval[0], mval[1], rad);
464         }
465         //setlinestyle(0);
466
467         bglFlush();
468         persp(PERSP_VIEW);
469         glDrawBuffer(GL_BACK);
470         glReadBuffer(GL_BACK);
471
472         no_mvalo= 0;
473 }
474
475 /** This function does the same as editview.c:circle_select(),
476   * but the selection actions are defined by a callback, making
477   * it (hopefully) reusable for other windows than the 3D view.
478   */
479
480 static void circle_selectCB(select_CBfunc callback)
481 {
482         static float rad= 40.0;
483         float rado= rad;
484         int firsttime=1;
485         int escape= 0;
486         unsigned short event;
487         short mvalo[2], mval[2], val;
488         short selecting=0;
489         Object *obj;
490         
491         if(G.obedit) obj = G.obedit;
492         else obj = OBACT;
493
494         mywinset(curarea->win);
495         
496         getmouseco_areawin(mvalo);
497         mval[0]= mvalo[0]; mval[1]= mvalo[1];
498
499         draw_sel_circle(mval, NULL, rad, 0.0, selecting); // draws frontbuffer, but sets backbuf again
500         
501         while(TRUE) {
502                 
503                 if(mval[0]!=mvalo[0] || mval[1]!=mvalo[1] || rado!=rad || firsttime) {
504                         firsttime= 0;
505                         
506                         if(selecting) {
507                                 callback(selecting, obj, mval, rad);
508                         }
509
510                         draw_sel_circle(mval, mvalo, rad, rado, selecting);
511                 
512                         mvalo[0]= mval[0];
513                         mvalo[1]= mval[1];
514                         rado= rad;
515
516                 }
517                 
518                 while(qtest()) {
519                         event= extern_qread(&val);
520                         if (event) {
521
522                                 /* for when another window is open and a mouse cursor activates it */
523                                 if(event!=MOUSEY && event!=MOUSEX) mywinset(curarea->win);
524                                 
525                                 getmouseco_areawin(mval);       // important to do here, trust events!
526                                 
527                                 switch(event) {
528                         
529                                 case LEFTMOUSE:
530                                 case MIDDLEMOUSE:
531                                         if(val) selecting= event;
532                                         else selecting= 0;
533                                         firsttime= 1;
534                                         
535                                         break;
536                                 case PAGEUPKEY:
537                                 case WHEELDOWNMOUSE:
538                                 case PADPLUSKEY:
539                                 case EQUALKEY:
540                                         if(val) if(rad<200.0) rad*= 1.2;
541                                         break;
542                                 case PAGEDOWNKEY:
543                                 case WHEELUPMOUSE:
544                                 case PADMINUS:
545                                 case MINUSKEY:
546                                         if(val) if(rad>5.0) rad/= 1.2;
547                                         break;
548                                 
549                                 case ESCKEY: case SPACEKEY: case RIGHTMOUSE: case INPUTCHANGE: 
550                                 case GKEY: case SKEY: case RKEY: case XKEY: case EKEY: case TABKEY:
551                                         escape= 1;
552                                         break;
553
554                                 }
555                                 
556                                 if(escape) break;
557                         }
558                 }
559                 PIL_sleep_ms(10);
560                 
561                 if(escape) break;
562         }
563         
564         /* clear circle */
565         draw_sel_circle(NULL, mvalo, 0, rad, 1);
566         BIF_undo_push("Circle Select");
567         countall();
568         allqueue(REDRAWINFO, 0);
569 }
570
571 static void count_object(Object *ob, int sel, int totob)
572 {
573         Mesh *me;
574         Curve *cu;
575         DerivedMesh *dm;
576         int tot=0, totf=0;
577
578         switch(ob->type) {
579         case OB_MESH:
580                         G.totmesh+=totob;
581                         me= get_mesh(ob);
582                         if(me) {
583                                         int totvert, totedge, totface;
584                                         dm = mesh_get_derived_final(ob, get_viewedit_datamask());
585                                         totvert = dm->getNumVerts(dm);
586                                         totedge = dm->getNumEdges(dm);
587                                         totface = dm->getNumFaces(dm);
588
589                                         G.totvert+= totvert*totob;
590                                         G.totedge+= totedge*totob;
591                                         G.totface+= totface*totob;
592                                         if(sel) {
593                                                         G.totvertsel+= totvert;
594                                                         G.totfacesel+= totface;
595                                         }
596                         }
597                         break;
598
599         case OB_LAMP:
600                 G.totlamp+=totob;
601                 break;
602         case OB_SURF:
603         case OB_CURVE:
604         case OB_FONT:
605                 G.totcurve+=totob;
606                 tot=totf= 0;
607                 cu= ob->data;
608                 if(cu->disp.first)
609                         count_displist( &cu->disp, &tot, &totf);
610                 tot*= totob;
611                 totf*= totob;
612                 G.totvert+= tot;
613                 G.totface+= totf;
614                 if(sel) {
615                         G.totvertsel+= tot;
616                         G.totfacesel+= totf;
617                 }
618                 break;
619         case OB_MBALL:
620                 count_displist( &ob->disp, &tot, &totf);
621                 tot*= totob;
622                 totf*= totob;
623                 G.totvert+= tot;
624                 G.totface+= totf;
625                 if(sel) {
626                         G.totvertsel+= tot;
627                         G.totfacesel+= totf;
628                 }
629                 break;
630         }
631         
632 }
633
634 /* countall does statistics */
635 /* is called on most actions, like select/add/delete/layermove */
636 void countall()
637 {
638         extern ListBase editNurb;
639         Base *base;
640         Object *ob= OBACT;
641         Mesh *me;
642         Nurb *nu;
643         BezTriple *bezt;
644         BPoint *bp;
645         MetaElem *ml;
646         struct EditBone *ebo;
647         int a;
648
649         G.totvert= G.totvertsel= G.totedge= G.totedgesel= G.totfacesel= G.totface= G.totobj= 
650             G.totmesh= G.totlamp= G.totcurve= G.totobjsel= G.totbone= G.totbonesel=  0;
651
652         if(G.obedit) {
653                 
654                 if(G.obedit->type==OB_MESH) {
655                         EditMesh *em = G.editMesh;
656                         EditVert *eve;
657                         EditEdge *eed;
658                         EditFace *efa;
659                         
660                         for(eve= em->verts.first; eve; eve= eve->next) {
661                                 G.totvert++;
662                                 if(eve->f & SELECT) G.totvertsel++;
663                         }
664                         for(eed= em->edges.first; eed; eed= eed->next) {
665                                 G.totedge++;
666                                 if(eed->f & SELECT) G.totedgesel++;
667                         }
668                         for(efa= em->faces.first; efa; efa= efa->next) {
669                                 G.totface++;
670                                 if(efa->f & SELECT) G.totfacesel++;
671                         }
672                         
673                         EM_validate_selections();
674                 }
675                 else if (G.obedit->type==OB_ARMATURE){
676                         for (ebo=G.edbo.first;ebo;ebo=ebo->next){
677                                 G.totbone++;
678                                 
679                                 /* Sync selection to parent for connected children */
680                                 if ((ebo->flag & BONE_CONNECTED) && ebo->parent){
681                                         G.totvert--;
682                                         if (ebo->parent->flag & BONE_TIPSEL)
683                                                 ebo->flag |= BONE_ROOTSEL;
684                                         else
685                                                 ebo->flag &= ~BONE_ROOTSEL;
686                                 }
687                                 
688                                 if (ebo->flag & BONE_TIPSEL)
689                                         G.totvertsel++;
690                                 if (ebo->flag & BONE_ROOTSEL)
691                                         G.totvertsel++;
692                                 
693                                 if ((ebo->flag & BONE_TIPSEL) && (ebo->flag & BONE_ROOTSEL))
694                                         ebo->flag |= BONE_SELECTED;
695                                 else
696                                         ebo->flag &= ~BONE_SELECTED;
697                                 
698                                 if(ebo->flag & BONE_SELECTED) G.totbonesel++;
699
700                                 //      If this is a connected child and it's parent is being moved, remove our root
701                                 if ((ebo->flag & BONE_CONNECTED)&& (ebo->flag & BONE_ROOTSEL) && ebo->parent && (ebo->parent->flag & BONE_TIPSEL)){
702                                         G.totvertsel--;
703                                 }
704
705                                 G.totvert+=2;
706                         }
707                 }
708                 else if ELEM3(G.obedit->type, OB_CURVE, OB_SURF, OB_FONT) {
709                         nu= editNurb.first;
710                         while(nu) {
711                                 if((nu->type & 7)==CU_BEZIER) {
712                                         bezt= nu->bezt;
713                                         a= nu->pntsu;
714                                         while(a--) {
715                                                 G.totvert+=3;
716                                                 if(bezt->f1) G.totvertsel++;
717                                                 if(bezt->f2) G.totvertsel++;
718                                                 if(bezt->f3) G.totvertsel++;
719                                                 bezt++;
720                                         }
721                                 }
722                                 else {
723                                         bp= nu->bp;
724                                         a= nu->pntsu*nu->pntsv;
725                                         while(a--) {
726                                                 G.totvert++;
727                                                 if(bp->f1 & SELECT) G.totvertsel++;
728                                                 bp++;
729                                         }
730                                 }
731                                 nu= nu->next;
732                         }
733                 }
734                 else if(G.obedit->type==OB_MBALL) {
735                         /* editmball.c */
736                         extern ListBase editelems;  /* go away ! */
737                         
738                         ml= editelems.first;
739                         while(ml) {
740                                 G.totvert++;
741                                 if(ml->flag & SELECT) G.totvertsel++;
742                                 ml= ml->next;
743                         }
744                 }
745                 else if(G.obedit->type==OB_LATTICE) {
746                         bp= editLatt->def;
747                         
748                         a= editLatt->pntsu*editLatt->pntsv*editLatt->pntsw;
749                         while(a--) {
750                                 G.totvert++;
751                                 if(bp->f1 & SELECT) G.totvertsel++;
752                                 bp++;
753                         }
754                 }
755                 
756                 allqueue(REDRAWINFO, 1);        /* 1, because header->win==0! */
757                 return;
758         }
759         else if(ob && (ob->flag & OB_POSEMODE)) {
760                 if(ob->pose) {
761                         bArmature *arm= ob->data;
762                         bPoseChannel *pchan;
763                         for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
764                                 G.totbone++;
765                                 if(pchan->bone && (pchan->bone->flag & BONE_SELECTED))
766                                         if(pchan->bone->layer & arm->layer)
767                                                 G.totbonesel++;
768                         }
769                 }
770                 allqueue(REDRAWINFO, 1);        /* 1, because header->win==0! */
771                 return;
772         }
773         else if(FACESEL_PAINT_TEST) {
774                 me= get_mesh((G.scene->basact) ? (G.scene->basact->object) : 0);
775                 if(me) {
776                         G.totface= me->totface;
777                         G.totvert= me->totvert;
778                 }
779                 allqueue(REDRAWINFO, 1);        /* 1, because header->win==0! */
780                 return;
781         }
782
783         if(G.scene==NULL) return;
784
785         base= (G.scene->base.first);
786         while(base) {
787                 if(G.scene->lay & base->lay) {
788                         ob= base->object;       /* warning, ob not is obact anymore */
789                         
790                         if(base->flag & SELECT) G.totobjsel++;
791
792                         if(ob->transflag & OB_DUPLIPARTS) {
793                                 ParticleSystem *psys;
794                                 ParticleSettings *part;
795
796                                 for(psys=ob->particlesystem.first; psys; psys=psys->next){
797                                         part=psys->part;
798
799                                         if(part->draw_as==PART_DRAW_OB && part->dup_ob){
800                                                 int tot=count_particles(psys);
801                                                 count_object(part->dup_ob, 0, tot);
802                                         }
803                                         else if(part->draw_as==PART_DRAW_GR && part->dup_group){
804                                                 GroupObject *go;
805                                                 int tot, totgroup=0, cur=0;
806                                                 
807                                                 go= part->dup_group->gobject.first;
808                                                 while(go){
809                                                         go=go->next;
810                                                         totgroup++;
811                                                 }
812                                                 go= part->dup_group->gobject.first;
813                                                 while(go){
814                                                         tot=count_particles_mod(psys,totgroup,cur);
815                                                         count_object(go->ob, 0, tot);
816                                                         cur++;
817                                                         go=go->next;
818                                                 }
819                                         }
820                                 }
821                                 
822                                 count_object(ob, base->flag & SELECT, 1);
823                                 G.totobj++;
824                         }
825                         else if(ob->parent && (ob->parent->transflag & (OB_DUPLIVERTS|OB_DUPLIFACES))) {
826                                 int tot= count_duplilist(ob->parent);
827                                 G.totobj+=tot;
828                                 count_object(ob, base->flag & SELECT, tot);
829                         }
830                         else if(ob->transflag & OB_DUPLIFRAMES) {
831                                 int tot= count_duplilist(ob);
832                                 G.totobj+=tot;
833                                 count_object(ob, base->flag & SELECT, tot);
834                         }
835                         else if((ob->transflag & OB_DUPLIGROUP) && ob->dup_group) {
836                                 int tot= count_duplilist(ob);
837                                 G.totobj+=tot;
838                                 count_object(ob, base->flag & SELECT, tot);
839                         }
840                         else {
841                                 count_object(ob, base->flag & SELECT, 1);
842                                 G.totobj++;
843                         }
844                 }
845                 base= base->next;
846         }
847         allqueue(REDRAWINFO, 1);        /* 1, because header->win==0! */
848 }
849
850 /* ************************************************** */
851 /* ********************* old transform stuff ******** */
852 /* ************************************************** */
853
854 static TransVert *transvmain=NULL;
855 static int tottrans= 0;
856
857 /* copied from editobject.c, now uses (almost) proper depgraph */
858 static void special_transvert_update(void)
859 {
860         
861         if(G.obedit) {
862                 
863                 DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);
864                 
865                 if(G.obedit->type==OB_MESH) {
866 #ifdef WITH_VERSE
867                         if(G.editMesh->vnode)
868                                 sync_all_verseverts_with_editverts((VNode*)G.editMesh->vnode);
869 #endif
870                         recalc_editnormals();   // does face centers too
871                 }
872                 else if ELEM(G.obedit->type, OB_CURVE, OB_SURF) {
873                         extern ListBase editNurb;
874                         Nurb *nu= editNurb.first;
875                         while(nu) {
876                                 test2DNurb(nu);
877                                 testhandlesNurb(nu); /* test for bezier too */
878                                 nu= nu->next;
879                         }
880                 }
881                 else if(G.obedit->type==OB_ARMATURE){
882                         bArmature *arm= G.obedit->data;
883                         EditBone *ebo;
884                         TransVert *tv= transvmain;
885                         int a=0;
886                         
887                         /* Ensure all bone tails are correctly adjusted */
888                         for (ebo=G.edbo.first; ebo; ebo=ebo->next) {
889                                 /* adjust tip if both ends selected */
890                                 if ((ebo->flag & BONE_ROOTSEL) && (ebo->flag & BONE_TIPSEL)) {
891                                         if (tv) {
892                                                 float diffvec[3];
893                                                 
894                                                 VecSubf(diffvec, tv->loc, tv->oldloc);
895                                                 VecAddf(ebo->tail, ebo->tail, diffvec);
896                                                 
897                                                 a++;
898                                                 if (a<tottrans) tv++;
899                                         }
900                                 }
901                         }
902                         
903                         /* Ensure all bones are correctly adjusted */
904                         for (ebo=G.edbo.first; ebo; ebo=ebo->next) {
905                                 if ((ebo->flag & BONE_CONNECTED) && ebo->parent){
906                                         /* If this bone has a parent tip that has been moved */
907                                         if (ebo->parent->flag & BONE_TIPSEL){
908                                                 VECCOPY (ebo->head, ebo->parent->tail);
909                                         }
910                                         /* If this bone has a parent tip that has NOT been moved */
911                                         else{
912                                                 VECCOPY (ebo->parent->tail, ebo->head);
913                                         }
914                                 }
915                         }
916                         if(arm->flag & ARM_MIRROR_EDIT) 
917                                 transform_armature_mirror_update();
918                 }
919                 else if(G.obedit->type==OB_LATTICE) {
920                         if(editLatt->flag & LT_OUTSIDE) outside_lattice(editLatt);
921                 }
922         }
923 }
924
925 /* copied from editobject.c, needs to be replaced with new transform code still */
926 /* mode: 1 = proportional, 2 = all joints (for bones only) */
927 static void make_trans_verts(float *min, float *max, int mode)  
928 {
929         extern ListBase editNurb;
930         EditMesh *em = G.editMesh;
931         Nurb *nu;
932         BezTriple *bezt;
933         BPoint *bp;
934         TransVert *tv=NULL;
935         MetaElem *ml;
936         EditVert *eve;
937         EditBone        *ebo;
938         float total, center[3], centroid[3];
939         int a;
940
941         tottrans= 0; // global!
942         
943         INIT_MINMAX(min, max);
944         centroid[0]=centroid[1]=centroid[2]= 0.0;
945         
946         /* note for transform refactor: dont rely on countall anymore... its ancient */
947         /* I skip it for editmesh now (ton) */
948         if(G.obedit->type!=OB_MESH) {
949                 countall();
950                 if(mode) tottrans= G.totvert;
951                 else tottrans= G.totvertsel;
952
953                 if(G.totvertsel==0) {
954                         tottrans= 0;
955                         return;
956                 }
957                 tv=transvmain= MEM_callocN(tottrans*sizeof(TransVert), "maketransverts");
958         }
959         
960         /* we count again because of hide (old, not for mesh!) */
961         tottrans= 0;
962         
963         if(G.obedit->type==OB_MESH) {
964                 int proptrans= 0;
965                 
966                 // transform now requires awareness for select mode, so we tag the f1 flags in verts
967                 tottrans= 0;
968                 if(G.scene->selectmode & SCE_SELECT_VERTEX) {
969                         for(eve= em->verts.first; eve; eve= eve->next) {
970                                 if(eve->h==0 && (eve->f & SELECT)) {
971                                         eve->f1= SELECT;
972                                         tottrans++;
973                                 }
974                                 else eve->f1= 0;
975                         }
976                 }
977                 else if(G.scene->selectmode & SCE_SELECT_EDGE) {
978                         EditEdge *eed;
979                         for(eve= em->verts.first; eve; eve= eve->next) eve->f1= 0;
980                         for(eed= em->edges.first; eed; eed= eed->next) {
981                                 if(eed->h==0 && (eed->f & SELECT)) eed->v1->f1= eed->v2->f1= SELECT;
982                         }
983                         for(eve= em->verts.first; eve; eve= eve->next) if(eve->f1) tottrans++;
984                 }
985                 else {
986                         EditFace *efa;
987                         for(eve= em->verts.first; eve; eve= eve->next) eve->f1= 0;
988                         for(efa= em->faces.first; efa; efa= efa->next) {
989                                 if(efa->h==0 && (efa->f & SELECT)) {
990                                         efa->v1->f1= efa->v2->f1= efa->v3->f1= SELECT;
991                                         if(efa->v4) efa->v4->f1= SELECT;
992                                 }
993                         }
994                         for(eve= em->verts.first; eve; eve= eve->next) if(eve->f1) tottrans++;
995                 }
996                 
997                 /* proportional edit exception... */
998                 if((mode & 1) && tottrans) {
999                         for(eve= em->verts.first; eve; eve= eve->next) {
1000                                 if(eve->h==0) {
1001                                         eve->f1 |= 2;
1002                                         proptrans++;
1003                                 }
1004                         }
1005                         if(proptrans>tottrans) tottrans= proptrans;
1006                 }
1007                 
1008                 /* and now make transverts */
1009                 if(tottrans) {
1010                         tv=transvmain= MEM_callocN(tottrans*sizeof(TransVert), "maketransverts");
1011
1012                         for(eve= em->verts.first; eve; eve= eve->next) {
1013                                 if(eve->f1) {
1014                                         VECCOPY(tv->oldloc, eve->co);
1015                                         tv->loc= eve->co;
1016                                         if(eve->no[0]!=0.0 || eve->no[1]!=0.0 ||eve->no[2]!=0.0)
1017                                                 tv->nor= eve->no; // note this is a hackish signal (ton)
1018                                         tv->flag= eve->f1 & SELECT;
1019                                         tv++;
1020                                 }
1021                         }
1022                 }
1023         }
1024         else if (G.obedit->type==OB_ARMATURE){
1025                 bArmature *arm= G.obedit->data;
1026                 
1027                 for (ebo=G.edbo.first;ebo;ebo=ebo->next){
1028                         if(ebo->layer & arm->layer) {
1029                                 short tipsel= (ebo->flag & BONE_TIPSEL);
1030                                 short rootsel= (ebo->flag & BONE_ROOTSEL);
1031                                 short rootok= (!(ebo->parent && (ebo->flag & BONE_CONNECTED) && ebo->parent->flag & BONE_TIPSEL));
1032                                 
1033                                 if ((tipsel && rootsel) || (rootsel)) {
1034                                         /* Don't add the tip (unless mode & 2, for getting all joints), 
1035                                          * otherwise we get zero-length bones as tips will snap to the same
1036                                          * location as heads. 
1037                                          */
1038                                         if (rootok) {
1039                                                 VECCOPY (tv->oldloc, ebo->head);
1040                                                 tv->loc= ebo->head;
1041                                                 tv->nor= NULL;
1042                                                 tv->flag= 1;
1043                                                 tv++;
1044                                                 tottrans++;
1045                                         }       
1046                                         
1047                                         if ((mode & 2) && (tipsel)) {
1048                                                 VECCOPY (tv->oldloc, ebo->tail);
1049                                                 tv->loc= ebo->tail;
1050                                                 tv->nor= NULL;
1051                                                 tv->flag= 1;
1052                                                 tv++;
1053                                                 tottrans++;
1054                                         }                                       
1055                                 }
1056                                 else if (tipsel) {
1057                                         VECCOPY (tv->oldloc, ebo->tail);
1058                                         tv->loc= ebo->tail;
1059                                         tv->nor= NULL;
1060                                         tv->flag= 1;
1061                                         tv++;
1062                                         tottrans++;
1063                                 }
1064                         }                       
1065                 }
1066         }
1067         else if ELEM(G.obedit->type, OB_CURVE, OB_SURF) {
1068                 nu= editNurb.first;
1069                 while(nu) {
1070                         if((nu->type & 7)==CU_BEZIER) {
1071                                 a= nu->pntsu;
1072                                 bezt= nu->bezt;
1073                                 while(a--) {
1074                                         if(bezt->hide==0) {
1075                                                 if((mode & 1) || (bezt->f1 & SELECT)) {
1076                                                         VECCOPY(tv->oldloc, bezt->vec[0]);
1077                                                         tv->loc= bezt->vec[0];
1078                                                         tv->flag= bezt->f1 & SELECT;
1079                                                         tv++;
1080                                                         tottrans++;
1081                                                 }
1082                                                 if((mode & 1) || (bezt->f2 & SELECT)) {
1083                                                         VECCOPY(tv->oldloc, bezt->vec[1]);
1084                                                         tv->loc= bezt->vec[1];
1085                                                         tv->val= &(bezt->alfa);
1086                                                         tv->oldval= bezt->alfa;
1087                                                         tv->flag= bezt->f2 & SELECT;
1088                                                         tv++;
1089                                                         tottrans++;
1090                                                 }
1091                                                 if((mode & 1) || (bezt->f3 & SELECT)) {
1092                                                         VECCOPY(tv->oldloc, bezt->vec[2]);
1093                                                         tv->loc= bezt->vec[2];
1094                                                         tv->flag= bezt->f3 & SELECT;
1095                                                         tv++;
1096                                                         tottrans++;
1097                                                 }
1098                                         }
1099                                         bezt++;
1100                                 }
1101                         }
1102                         else {
1103                                 a= nu->pntsu*nu->pntsv;
1104                                 bp= nu->bp;
1105                                 while(a--) {
1106                                         if(bp->hide==0) {
1107                                                 if((mode & 1) || (bp->f1 & SELECT)) {
1108                                                         VECCOPY(tv->oldloc, bp->vec);
1109                                                         tv->loc= bp->vec;
1110                                                         tv->val= &(bp->alfa);
1111                                                         tv->oldval= bp->alfa;
1112                                                         tv->flag= bp->f1 & SELECT;
1113                                                         tv++;
1114                                                         tottrans++;
1115                                                 }
1116                                         }
1117                                         bp++;
1118                                 }
1119                         }
1120                         nu= nu->next;
1121                 }
1122         }
1123         else if(G.obedit->type==OB_MBALL) {
1124                 extern ListBase editelems;  /* go away ! */
1125                 ml= editelems.first;
1126                 while(ml) {
1127                         if(ml->flag & SELECT) {
1128                                 tv->loc= &ml->x;
1129                                 VECCOPY(tv->oldloc, tv->loc);
1130                                 tv->val= &(ml->rad);
1131                                 tv->oldval= ml->rad;
1132                                 tv->flag= 1;
1133                                 tv++;
1134                                 tottrans++;
1135                         }
1136                         ml= ml->next;
1137                 }
1138         }
1139         else if(G.obedit->type==OB_LATTICE) {
1140                 bp= editLatt->def;
1141                 
1142                 a= editLatt->pntsu*editLatt->pntsv*editLatt->pntsw;
1143                 
1144                 while(a--) {
1145                         if((mode & 1) || (bp->f1 & SELECT)) {
1146                                 if(bp->hide==0) {
1147                                         VECCOPY(tv->oldloc, bp->vec);
1148                                         tv->loc= bp->vec;
1149                                         tv->flag= bp->f1 & SELECT;
1150                                         tv++;
1151                                         tottrans++;
1152                                 }
1153                         }
1154                         bp++;
1155                 }
1156         }
1157         
1158         /* cent etc */
1159         tv= transvmain;
1160         total= 0.0;
1161         for(a=0; a<tottrans; a++, tv++) {
1162                 if(tv->flag & SELECT) {
1163                         centroid[0]+= tv->oldloc[0];
1164                         centroid[1]+= tv->oldloc[1];
1165                         centroid[2]+= tv->oldloc[2];
1166                         total+= 1.0;
1167                         DO_MINMAX(tv->oldloc, min, max);
1168                 }
1169         }
1170         if(total!=0.0) {
1171                 centroid[0]/= total;
1172                 centroid[1]/= total;
1173                 centroid[2]/= total;
1174         }
1175
1176         center[0]= (min[0]+max[0])/2.0;
1177         center[1]= (min[1]+max[1])/2.0;
1178         center[2]= (min[2]+max[2])/2.0;
1179         
1180 }
1181
1182 void snap_sel_to_grid()
1183 {
1184         extern float originmat[3][3];   /* object.c */
1185         TransVert *tv;
1186         Base *base;
1187         Object *ob;
1188         float gridf, imat[3][3], bmat[3][3], vec[3];
1189         int a;
1190
1191         gridf= G.vd->gridview;
1192
1193
1194         if(G.obedit) {
1195                 tottrans= 0;
1196                 
1197                 if ELEM6(G.obedit->type, OB_ARMATURE, OB_LATTICE, OB_MESH, OB_SURF, OB_CURVE, OB_MBALL) 
1198                         make_trans_verts(bmat[0], bmat[1], 0);
1199                 if(tottrans==0) return;
1200                 
1201                 Mat3CpyMat4(bmat, G.obedit->obmat);
1202                 Mat3Inv(imat, bmat);
1203                 
1204                 tv= transvmain;
1205                 for(a=0; a<tottrans; a++, tv++) {
1206                         
1207                         VECCOPY(vec, tv->loc);
1208                         Mat3MulVecfl(bmat, vec);
1209                         VecAddf(vec, vec, G.obedit->obmat[3]);
1210                         vec[0]= G.vd->gridview*floor(.5+ vec[0]/gridf);
1211                         vec[1]= G.vd->gridview*floor(.5+ vec[1]/gridf);
1212                         vec[2]= G.vd->gridview*floor(.5+ vec[2]/gridf);
1213                         VecSubf(vec, vec, G.obedit->obmat[3]);
1214                         
1215                         Mat3MulVecfl(imat, vec);
1216                         VECCOPY(tv->loc, vec);
1217                         
1218                 }
1219                 
1220                 special_transvert_update();
1221                 
1222                 MEM_freeN(transvmain);
1223                 transvmain= 0;
1224         
1225                 allqueue(REDRAWVIEW3D, 0);
1226                 return;
1227         }
1228
1229         base= (G.scene->base.first);
1230         while(base) {
1231                 if( ( ((base)->flag & SELECT) && ((base)->lay & G.vd->lay) && ((base)->object->id.lib==0))) {
1232                         ob= base->object;
1233                         if(ob->flag & OB_POSEMODE) {
1234                                 bPoseChannel *pchan;
1235                                 bArmature *arm= ob->data;
1236                                 
1237                                 for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
1238                                         if(pchan->bone->flag & BONE_SELECTED) {
1239                                                 if(pchan->bone->layer & arm->layer) {
1240                                                         if((pchan->bone->flag & BONE_CONNECTED)==0) { 
1241                                                                 float vecN[3], nLoc[3]; 
1242                                                                 
1243                                                                 /* get nearest grid point to snap to */
1244                                                                 VECCOPY(nLoc, pchan->pose_mat[3]);
1245                                                                 vec[0]= gridf * (float)(floor(.5+ nLoc[0]/gridf));
1246                                                                 vec[1]= gridf * (float)(floor(.5+ nLoc[1]/gridf));
1247                                                                 vec[2]= gridf * (float)(floor(.5+ nLoc[2]/gridf));
1248                                                                 
1249                                                                 /* get bone-space location of grid point */
1250                                                                 armature_loc_pose_to_bone(pchan, vec, vecN);
1251                                                                 
1252                                                                 /* adjust location */
1253                                                                 VECCOPY(pchan->loc, vecN);
1254                                                         }
1255                                                         /* if the bone has a parent and is connected to the parent, 
1256                                                          * don't do anything - will break chain unless we do auto-ik. 
1257                                                          */
1258                                                 }
1259                                         }
1260                                 }
1261                                 ob->pose->flag |= (POSE_LOCKED|POSE_DO_UNLOCK);
1262                                 
1263                                 /* auto-keyframing */
1264                                 autokeyframe_pose_cb_func(ob, TFM_TRANSLATION, 0);
1265                                 DAG_object_flush_update(G.scene, ob, OB_RECALC_DATA);
1266                         }
1267                         else {
1268                                 ob->recalc |= OB_RECALC_OB;
1269                                 
1270                                 vec[0]= -ob->obmat[3][0]+G.vd->gridview*floor(.5+ ob->obmat[3][0]/gridf);
1271                                 vec[1]= -ob->obmat[3][1]+G.vd->gridview*floor(.5+ ob->obmat[3][1]/gridf);
1272                                 vec[2]= -ob->obmat[3][2]+G.vd->gridview*floor(.5+ ob->obmat[3][2]/gridf);
1273                                 
1274                                 if(ob->parent) {
1275                                         where_is_object(ob);
1276                                         
1277                                         Mat3Inv(imat, originmat);
1278                                         Mat3MulVecfl(imat, vec);
1279                                         ob->loc[0]+= vec[0];
1280                                         ob->loc[1]+= vec[1];
1281                                         ob->loc[2]+= vec[2];
1282                                 }
1283                                 else {
1284                                         ob->loc[0]+= vec[0];
1285                                         ob->loc[1]+= vec[1];
1286                                         ob->loc[2]+= vec[2];
1287                                 }
1288 #ifdef WITH_VERSE
1289                                 if(ob->vnode) b_verse_send_transformation(ob);
1290 #endif
1291                         
1292                                 /* auto-keyframing */
1293                                 autokeyframe_ob_cb_func(ob, TFM_TRANSLATION);
1294                         }
1295                 }
1296
1297                 base= base->next;
1298         }
1299         DAG_scene_flush_update(G.scene, screen_view3d_layers(), 0);
1300         allqueue(REDRAWVIEW3D, 0);
1301 }
1302
1303 void snap_sel_to_curs()
1304 {
1305         extern float originmat[3][3];   /* object.c */
1306         TransVert *tv;
1307         Base *base;
1308         Object *ob;
1309         float *curs, imat[3][3], bmat[3][3], vec[3];
1310         int a;
1311
1312         curs= give_cursor();
1313
1314         if(G.obedit) {
1315                 tottrans= 0;
1316                 
1317                 if ELEM6(G.obedit->type, OB_ARMATURE, OB_LATTICE, OB_MESH, OB_SURF, OB_CURVE, OB_MBALL) 
1318                         make_trans_verts(bmat[0], bmat[1], 0);
1319                 if(tottrans==0) return;
1320                 
1321                 Mat3CpyMat4(bmat, G.obedit->obmat);
1322                 Mat3Inv(imat, bmat);
1323                 
1324                 tv= transvmain;
1325                 for(a=0; a<tottrans; a++, tv++) {
1326                         vec[0]= curs[0]-G.obedit->obmat[3][0];
1327                         vec[1]= curs[1]-G.obedit->obmat[3][1];
1328                         vec[2]= curs[2]-G.obedit->obmat[3][2];
1329                         
1330                         Mat3MulVecfl(imat, vec);
1331                         VECCOPY(tv->loc, vec);
1332                 }
1333                 
1334                 special_transvert_update();
1335                 
1336                 MEM_freeN(transvmain);
1337                 transvmain= 0;
1338                 
1339                 allqueue(REDRAWVIEW3D, 0);
1340                 return;
1341         }
1342
1343         base= (G.scene->base.first);
1344         while(base) {
1345                 if( ( ((base)->flag & SELECT) && ((base)->lay & G.vd->lay) && ((base)->object->id.lib==0))) {
1346                         ob= base->object;
1347                         if(ob->flag & OB_POSEMODE) {
1348                                 bPoseChannel *pchan;
1349                                 bArmature *arm= ob->data;
1350                                 float cursp[3];
1351                                 
1352                                 Mat4Invert(ob->imat, ob->obmat);
1353                                 VECCOPY(cursp, curs);
1354                                 Mat4MulVecfl(ob->imat, cursp);
1355                                 
1356                                 for (pchan = ob->pose->chanbase.first; pchan; pchan=pchan->next) {
1357                                         if(pchan->bone->flag & BONE_SELECTED) {
1358                                                 if(pchan->bone->layer & arm->layer) {
1359                                                         if((pchan->bone->flag & BONE_CONNECTED)==0) { 
1360                                                                 float curspn[3];
1361                                                                 
1362                                                                 /* get location of cursor in bone-space */
1363                                                                 armature_loc_pose_to_bone(pchan, cursp, curspn);
1364                                                                 
1365                                                                 /* calculate new position */
1366                                                                 VECCOPY(pchan->loc, curspn);
1367                                                         }
1368                                                         /* if the bone has a parent and is connected to the parent, 
1369                                                          * don't do anything - will break chain unless we do auto-ik. 
1370                                                          */
1371                                                 }
1372                                         }
1373                                 }
1374                                 ob->pose->flag |= (POSE_LOCKED|POSE_DO_UNLOCK);
1375                                 
1376                                 /* auto-keyframing */
1377                                 autokeyframe_pose_cb_func(ob, TFM_TRANSLATION, 0);
1378                                 DAG_object_flush_update(G.scene, ob, OB_RECALC_DATA);
1379                         }
1380                         else {
1381                                 ob->recalc |= OB_RECALC_OB;
1382                                 
1383                                 vec[0]= -ob->obmat[3][0] + curs[0];
1384                                 vec[1]= -ob->obmat[3][1] + curs[1];
1385                                 vec[2]= -ob->obmat[3][2] + curs[2];
1386                                 
1387                                 if(ob->parent) {
1388                                         where_is_object(ob);
1389                                         
1390                                         Mat3Inv(imat, originmat);
1391                                         Mat3MulVecfl(imat, vec);
1392                                         ob->loc[0]+= vec[0];
1393                                         ob->loc[1]+= vec[1];
1394                                         ob->loc[2]+= vec[2];
1395                                 }
1396                                 else {
1397                                         ob->loc[0]+= vec[0];
1398                                         ob->loc[1]+= vec[1];
1399                                         ob->loc[2]+= vec[2];
1400                                 }
1401 #ifdef WITH_VERSE
1402                                 if(ob->vnode) b_verse_send_transformation(ob);
1403 #endif
1404                                 
1405                                 /* auto-keyframing */
1406                                 autokeyframe_ob_cb_func(ob, TFM_TRANSLATION);
1407                         }
1408                 }
1409
1410                 base= base->next;
1411         }
1412         DAG_scene_flush_update(G.scene, screen_view3d_layers(), 0);
1413         allqueue(REDRAWVIEW3D, 0);
1414 }
1415
1416 void snap_curs_to_grid()
1417 {
1418         float gridf, *curs;
1419
1420         gridf= G.vd->gridview;
1421         curs= give_cursor();
1422
1423         curs[0]= G.vd->gridview*floor(.5+curs[0]/gridf);
1424         curs[1]= G.vd->gridview*floor(.5+curs[1]/gridf);
1425         curs[2]= G.vd->gridview*floor(.5+curs[2]/gridf);
1426
1427         allqueue(REDRAWVIEW3D, 0);
1428 }
1429
1430 void snap_curs_to_sel()
1431 {
1432         TransVert *tv;
1433         Base *base;
1434         float *curs, bmat[3][3], vec[3], min[3], max[3], centroid[3];
1435         int count, a;
1436
1437         curs= give_cursor();
1438
1439         count= 0;
1440         INIT_MINMAX(min, max);
1441         centroid[0]= centroid[1]= centroid[2]= 0.0;
1442
1443         if(G.obedit) {
1444                 tottrans=0;
1445                 
1446                 if ELEM6(G.obedit->type, OB_ARMATURE, OB_LATTICE, OB_MESH, OB_SURF, OB_CURVE, OB_MBALL) 
1447                         make_trans_verts(bmat[0], bmat[1], 2);
1448                 if(tottrans==0) return;
1449                 
1450                 Mat3CpyMat4(bmat, G.obedit->obmat);
1451                 
1452                 tv= transvmain;
1453                 for(a=0; a<tottrans; a++, tv++) {
1454                         VECCOPY(vec, tv->loc);
1455                         Mat3MulVecfl(bmat, vec);
1456                         VecAddf(vec, vec, G.obedit->obmat[3]);
1457                         VecAddf(centroid, centroid, vec);
1458                         DO_MINMAX(vec, min, max);
1459                 }
1460                 
1461                 if(G.vd->around==V3D_CENTROID) {
1462                         VecMulf(centroid, 1.0/(float)tottrans);
1463                         VECCOPY(curs, centroid);
1464                 }
1465                 else {
1466                         curs[0]= (min[0]+max[0])/2;
1467                         curs[1]= (min[1]+max[1])/2;
1468                         curs[2]= (min[2]+max[2])/2;
1469                 }
1470                 MEM_freeN(transvmain);
1471                 transvmain= 0;
1472         }
1473         else {
1474                 Object *ob= OBACT;
1475                 
1476                 if(ob && (ob->flag & OB_POSEMODE)) {
1477                         bArmature *arm= ob->data;
1478                         bPoseChannel *pchan;
1479                         for (pchan = ob->pose->chanbase.first; pchan; pchan=pchan->next) {
1480                                 if(arm->layer & pchan->bone->layer) {
1481                                         if(pchan->bone->flag & BONE_SELECTED) {
1482                                                 VECCOPY(vec, pchan->pose_head);
1483                                                 Mat4MulVecfl(ob->obmat, vec);
1484                                                 VecAddf(centroid, centroid, vec);
1485                                                 DO_MINMAX(vec, min, max);
1486                                                 count++;
1487                                         }
1488                                 }
1489                         }
1490                 }
1491                 else {
1492                         for(base= G.scene->base.first; base; base= base->next) {
1493                                 if(((base)->flag & SELECT) && ((base)->lay & G.vd->lay) ) {
1494                                         VECCOPY(vec, base->object->obmat[3]);
1495                                         VecAddf(centroid, centroid, vec);
1496                                         DO_MINMAX(vec, min, max);
1497                                         count++;
1498                                 }
1499                         }
1500                 }
1501                 if(count) {
1502                         if(G.vd->around==V3D_CENTROID) {
1503                                 VecMulf(centroid, 1.0/(float)count);
1504                                 VECCOPY(curs, centroid);
1505                         }
1506                         else {
1507                                 curs[0]= (min[0]+max[0])/2;
1508                                 curs[1]= (min[1]+max[1])/2;
1509                                 curs[2]= (min[2]+max[2])/2;
1510                         }
1511                 }
1512         }
1513         allqueue(REDRAWVIEW3D, 0);
1514 }
1515
1516 void snap_curs_to_active()
1517 {
1518         float *curs;
1519         curs = give_cursor();
1520
1521         if (G.obedit)
1522         {
1523                 if (G.obedit->type == OB_MESH)
1524                 {
1525                         /* check active */
1526                         EditSelection ese;
1527                         if (EM_get_actSelection(&ese)) {
1528                                 EM_editselection_center(curs, &ese);
1529                         }
1530                         
1531                         Mat4MulVecfl(G.obedit->obmat, curs);
1532                 }
1533         }
1534         else
1535         {
1536                 if (BASACT)
1537                 {
1538                         VECCOPY(curs, BASACT->object->obmat[3]);
1539                 }
1540         }
1541         allqueue(REDRAWVIEW3D, 0);
1542 }
1543
1544
1545 void snap_to_center()
1546 {
1547         extern float originmat[3][3];
1548         TransVert *tv;
1549         Base *base;
1550         Object *ob;
1551         float snaploc[3], imat[3][3], bmat[3][3], vec[3], min[3], max[3], centroid[3];
1552         int count, a;
1553
1554         /*calculate the snaplocation (centerpoint) */
1555         count= 0;
1556         INIT_MINMAX(min, max);
1557         centroid[0]= centroid[1]= centroid[2]= 0.0f;
1558         snaploc[0]= snaploc[1]= snaploc[2]= 0.0f;
1559
1560         if(G.obedit) {
1561                 tottrans= 0;
1562                 
1563                 if ELEM6(G.obedit->type, OB_ARMATURE, OB_LATTICE, OB_MESH, OB_SURF, OB_CURVE, OB_MBALL) 
1564                         make_trans_verts(bmat[0], bmat[1], 0);
1565                 if(tottrans==0) return;
1566                 
1567                 Mat3CpyMat4(bmat, G.obedit->obmat);
1568                 Mat3Inv(imat, bmat);
1569                 
1570                 tv= transvmain;
1571                 for(a=0; a<tottrans; a++, tv++) {
1572                         VECCOPY(vec, tv->loc);
1573                         Mat3MulVecfl(bmat, vec);
1574                         VecAddf(vec, vec, G.obedit->obmat[3]);
1575                         VecAddf(centroid, centroid, vec);
1576                         DO_MINMAX(vec, min, max);
1577                 }
1578                 
1579                 if(G.vd->around==V3D_CENTROID) {
1580                         VecMulf(centroid, 1.0/(float)tottrans);
1581                         VECCOPY(snaploc, centroid);
1582                 }
1583                 else {
1584                         snaploc[0]= (min[0]+max[0])/2;
1585                         snaploc[1]= (min[1]+max[1])/2;
1586                         snaploc[2]= (min[2]+max[2])/2;
1587                 }
1588                 
1589                 MEM_freeN(transvmain);
1590                 transvmain= 0;
1591         }
1592         else {
1593                 base= (G.scene->base.first);
1594                 while(base) {
1595                         if(((base)->flag & SELECT) && ((base)->lay & G.vd->lay) ) {
1596                                 ob= base->object;
1597                                 if(ob->flag & OB_POSEMODE) {
1598                                         bPoseChannel *pchan;
1599                                         bArmature *arm= ob->data;
1600                                         
1601                                         for (pchan = ob->pose->chanbase.first; pchan; pchan=pchan->next) {
1602                                                 if(pchan->bone->flag & BONE_SELECTED) {
1603                                                         if(pchan->bone->layer & arm->layer) {
1604                                                                 VECCOPY(vec, pchan->pose_mat[3]);
1605                                                                 VecAddf(centroid, centroid, vec);
1606                                                                 DO_MINMAX(vec, min, max);
1607                                                                 count++;
1608                                                         }
1609                                                 }
1610                                         }
1611                                 }
1612                                 else {
1613                                         /* not armature bones (i.e. objects) */
1614                                         VECCOPY(vec, base->object->obmat[3]);
1615                                         VecAddf(centroid, centroid, vec);
1616                                         DO_MINMAX(vec, min, max);
1617                                         count++;
1618                                 }
1619                         }
1620                         base= base->next;
1621                 }
1622                 if(count) {
1623                         if(G.vd->around==V3D_CENTROID) {
1624                                 VecMulf(centroid, 1.0/(float)count);
1625                                 VECCOPY(snaploc, centroid);
1626                         }
1627                         else {
1628                                 snaploc[0]= (min[0]+max[0])/2;
1629                                 snaploc[1]= (min[1]+max[1])/2;
1630                                 snaploc[2]= (min[2]+max[2])/2;
1631                         }
1632                 }
1633         }
1634
1635         /* Snap the selection to the snaplocation (duh!) */
1636         if(G.obedit) {
1637                 tottrans= 0;
1638                 
1639                 if ELEM6(G.obedit->type, OB_ARMATURE, OB_LATTICE, OB_MESH, OB_SURF, OB_CURVE, OB_MBALL) 
1640                         make_trans_verts(bmat[0], bmat[1], 0);
1641                 if(tottrans==0) return;
1642                 
1643                 Mat3CpyMat4(bmat, G.obedit->obmat);
1644                 Mat3Inv(imat, bmat);
1645                 
1646                 tv= transvmain;
1647                 for(a=0; a<tottrans; a++, tv++) {
1648                         vec[0]= snaploc[0]-G.obedit->obmat[3][0];
1649                         vec[1]= snaploc[1]-G.obedit->obmat[3][1];
1650                         vec[2]= snaploc[2]-G.obedit->obmat[3][2];
1651                         
1652                         Mat3MulVecfl(imat, vec);
1653                         VECCOPY(tv->loc, vec);
1654                 }
1655                 
1656                 special_transvert_update();
1657                 
1658                 MEM_freeN(transvmain);
1659                 transvmain= 0;
1660                 
1661                 allqueue(REDRAWVIEW3D, 0);
1662                 return;
1663         }
1664
1665         base= (G.scene->base.first);
1666         while(base) {
1667                 if( ( ((base)->flag & SELECT) && ((base)->lay & G.vd->lay) && ((base)->object->id.lib==0))) {
1668                         ob= base->object;
1669                         if(ob->flag & OB_POSEMODE) {
1670                                 bPoseChannel *pchan;
1671                                 bArmature *arm= ob->data;
1672                                 
1673                                 for (pchan = ob->pose->chanbase.first; pchan; pchan=pchan->next) {
1674                                         if(pchan->bone->flag & BONE_SELECTED) {
1675                                                 if(pchan->bone->layer & arm->layer) {
1676                                                         if((pchan->bone->flag & BONE_CONNECTED)==0) { 
1677                                                                 /* get location of cursor in bone-space */
1678                                                                 armature_loc_pose_to_bone(pchan, snaploc, vec);
1679                                                                 
1680                                                                 /* calculate new position */
1681                                                                 VECCOPY(pchan->loc, vec);
1682                                                         }
1683                                                         /* if the bone has a parent and is connected to the parent, 
1684                                                          * don't do anything - will break chain unless we do auto-ik. 
1685                                                          */
1686                                                 }
1687                                         }
1688                                 }
1689                                 
1690                                 /* auto-keyframing */
1691                                 ob->pose->flag |= POSE_DO_UNLOCK;
1692                                 autokeyframe_pose_cb_func(ob, TFM_TRANSLATION, 0);
1693                                 DAG_object_flush_update(G.scene, ob, OB_RECALC_DATA);
1694                         }
1695                         else {
1696                                 ob->recalc |= OB_RECALC_OB;
1697                                 
1698                                 vec[0]= -ob->obmat[3][0] + snaploc[0];
1699                                 vec[1]= -ob->obmat[3][1] + snaploc[1];
1700                                 vec[2]= -ob->obmat[3][2] + snaploc[2];
1701                                 
1702                                 if(ob->parent) {
1703                                         where_is_object(ob);
1704                                         
1705                                         Mat3Inv(imat, originmat);
1706                                         Mat3MulVecfl(imat, vec);
1707                                         ob->loc[0]+= vec[0];
1708                                         ob->loc[1]+= vec[1];
1709                                         ob->loc[2]+= vec[2];
1710                                 }
1711                                 else {
1712                                         ob->loc[0]+= vec[0];
1713                                         ob->loc[1]+= vec[1];
1714                                         ob->loc[2]+= vec[2];
1715                                 }
1716 #ifdef WITH_VERSE
1717                                 if(ob->vnode) b_verse_send_transformation(ob);
1718 #endif
1719                                 
1720                                 /* auto-keyframing */
1721                                 autokeyframe_ob_cb_func(ob, TFM_TRANSLATION);
1722                         }
1723                 }
1724                 
1725                 base= base->next;
1726         }
1727         DAG_scene_flush_update(G.scene, screen_view3d_layers(), 0);
1728         allqueue(REDRAWVIEW3D, 0);
1729 }
1730
1731
1732 void snapmenu()
1733 {
1734         short event;
1735
1736         event = pupmenu("Snap %t|Selection -> Grid%x1|Selection -> Cursor%x2|Selection -> Center%x3|%l|Cursor -> Selection%x4|Cursor -> Grid%x5|Cursor -> Active%x6");
1737
1738         switch (event) {
1739                 case 1: /*Selection to grid*/
1740                     snap_sel_to_grid();
1741                         BIF_undo_push("Snap selection to grid");
1742                     break;
1743                 case 2: /*Selection to cursor*/
1744                     snap_sel_to_curs();
1745                         BIF_undo_push("Snap selection to cursor");
1746                     break;
1747                 case 3: /*Selection to center of selection*/
1748                     snap_to_center();
1749                         BIF_undo_push("Snap selection to center");
1750                     break;
1751                 case 4: /*Cursor to selection*/
1752                     snap_curs_to_sel();
1753                     break;
1754                 case 5: /*Cursor to grid*/
1755                     snap_curs_to_grid();
1756                     break;
1757                 case 6: /*Cursor to Active*/
1758                     snap_curs_to_active();
1759                         BIF_undo_push("Snap selection to center");
1760                     break;
1761         }
1762 }
1763
1764 void alignmenu()
1765 {
1766         short val;
1767         char *str_menu = BIF_menustringTransformOrientation("Align");
1768         val= pupmenu(str_menu);
1769         MEM_freeN(str_menu);
1770
1771         if (val >= 0)
1772         {
1773                 short old_val = G.vd->twmode; 
1774                 G.vd->twmode = val;
1775                 initTransform(TFM_ALIGN, CTX_NO_PET|CTX_AUTOCONFIRM);
1776                 Transform();
1777                 G.vd->twmode = old_val;
1778         }
1779 }
1780
1781 #define MERGELIMIT 0.001
1782 void mergemenu(void)
1783 {       
1784
1785         short event;
1786         int remCount = 0;
1787         
1788         if(G.scene->selectmode & SCE_SELECT_VERTEX)
1789                 if(G.editMesh->selected.first && G.editMesh->selected.last && 
1790                         ((EditSelection*)G.editMesh->selected.first)->type == EDITVERT && ((EditSelection*)G.editMesh->selected.last)->type == EDITVERT) 
1791                                 event = pupmenu("Merge %t|At First %x6|At Last%x1|At Center%x3|At Cursor%x4|Collapse%x2");
1792                 else if (G.editMesh->selected.first && ((EditSelection*)G.editMesh->selected.first)->type == EDITVERT) 
1793                         event = pupmenu("Merge %t|At First %x6|At Center%x3|At Cursor%x4|Collapse%x2");
1794                 else if (G.editMesh->selected.last && ((EditSelection*)G.editMesh->selected.last)->type == EDITVERT) 
1795                         event = pupmenu("Merge %t|At Last %x1|At Center%x3|At Cursor%x4|Collapse%x2");
1796                 else event = pupmenu("Merge %t|At Center%x3|At Cursor%x4|Collapse%x2");
1797         else event = pupmenu("Merge %t|At Center%x3|At Cursor%x4|Collapse%x2");
1798         switch (event)
1799         {
1800                 case -1:
1801                         return;
1802                 case 3:
1803                         if(G.qual & LR_CTRLKEY) remCount = merge_target(0,1);
1804                         else remCount = merge_target(0,0);
1805                         BIF_undo_push("Merge at center");
1806                         break;
1807                 case 4:
1808                         if(G.qual & LR_CTRLKEY) remCount = merge_target(1,1);
1809                         else remCount = merge_target(1,0);
1810                         BIF_undo_push("Merge at cursor");
1811                         break;
1812                 case 1:
1813                         if(G.qual & LR_CTRLKEY) remCount = merge_firstlast(0,1);
1814                         else remCount = merge_firstlast(0,0);
1815                         BIF_undo_push("Merge at last selected");
1816                         break;
1817                 case 6:
1818                         if(G.qual & LR_CTRLKEY) remCount = merge_firstlast(1,1);
1819                         else remCount = merge_firstlast(1,0);
1820                         BIF_undo_push("Merge at first selected");
1821                         break;
1822                 case 2:
1823                         remCount = collapseEdges();
1824                         BIF_undo_push("Collapse");
1825                         break;
1826         }
1827         notice("Removed %d Vertices", remCount);
1828         allqueue(REDRAWVIEW3D, 0);
1829         countall();
1830 }
1831 #undef MERGELIMIT
1832
1833
1834 void delete_context_selected(void) 
1835 {
1836         if(BIF_fullSketchMode())
1837         {
1838                 BIF_deleteSketch();
1839         }
1840         else if(G.obedit) {
1841                 if(G.obedit->type==OB_MESH) delete_mesh();
1842                 else if ELEM(G.obedit->type, OB_CURVE, OB_SURF) delNurb();
1843                 else if(G.obedit->type==OB_MBALL) delete_mball();
1844                 else if (G.obedit->type==OB_ARMATURE) delete_armature();
1845         }
1846         else if(G.f & G_PARTICLEEDIT){
1847                 PE_delete_particle();
1848         }
1849         else delete_obj(0);
1850 }
1851
1852 void duplicate_context_selected(void) 
1853 {
1854         if(G.obedit) {
1855                 if(G.obedit->type==OB_MESH) adduplicate_mesh();
1856                 else if(G.obedit->type==OB_ARMATURE) adduplicate_armature();
1857                 else if(G.obedit->type==OB_MBALL) adduplicate_mball();
1858                 else if ELEM(G.obedit->type, OB_CURVE, OB_SURF) adduplicate_nurb();
1859         }
1860         else if(G.f & G_PARTICLEEDIT);
1861         else
1862                 adduplicate(0, U.dupflag);
1863 }
1864
1865 void toggle_shading(void) 
1866 {
1867         if(G.qual & LR_SHIFTKEY) {
1868                 if(G.qual & LR_ALTKEY) {
1869                         reshadeall_displist();
1870                         G.vd->drawtype= OB_SHADED;
1871                 }
1872                 else {
1873                         if(G.vd->drawtype== OB_SHADED) G.vd->drawtype= OB_WIRE;
1874                         else G.vd->drawtype= OB_SHADED;
1875                 }
1876         }
1877         else if(G.qual & LR_ALTKEY) {
1878                 if(G.vd->drawtype== OB_TEXTURE) G.vd->drawtype= OB_SOLID;
1879                 else G.vd->drawtype= OB_TEXTURE;
1880         }
1881         else {
1882                 if(G.vd->drawtype==OB_SOLID || G.vd->drawtype==OB_SHADED) G.vd->drawtype= OB_WIRE;
1883                 else G.vd->drawtype= OB_SOLID;
1884         }
1885         
1886         if(G.vd->drawtype != OB_WIRE && G.vd->depths) {
1887                 G.vd->depths->damaged= 1;
1888         }
1889 }
1890
1891 int minmax_verts(float *min, float *max)
1892 {
1893         TransVert *tv;
1894         float centroid[3], vec[3], bmat[3][3];
1895         int a;
1896
1897         tottrans=0;
1898         if ELEM5(G.obedit->type, OB_ARMATURE, OB_LATTICE, OB_MESH, OB_SURF, OB_CURVE) 
1899                 make_trans_verts(bmat[0], bmat[1], 2);
1900         if(tottrans==0) return 0;
1901
1902         Mat3CpyMat4(bmat, G.obedit->obmat);
1903         
1904         tv= transvmain;
1905         for(a=0; a<tottrans; a++, tv++) {               
1906                 VECCOPY(vec, tv->loc);
1907                 Mat3MulVecfl(bmat, vec);
1908                 VecAddf(vec, vec, G.obedit->obmat[3]);
1909                 VecAddf(centroid, centroid, vec);
1910                 DO_MINMAX(vec, min, max);               
1911         }
1912         
1913         MEM_freeN(transvmain);
1914         transvmain= 0;
1915         return 1;
1916 }
1917