Merge with trunk: svn merge -r 12151:12179 https://svn.blender.org/svnroot/bf-blender...
[blender.git] / source / blender / src / transform_generics.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL/BL DUAL 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. The Blender
10  * Foundation also sells licenses for use in proprietary software under
11  * the Blender License.  See http://www.blender.org/BL/ for information
12  * about this.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22  *
23  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
24  * All rights reserved.
25  *
26  * The Original Code is: all of this file.
27  *
28  * Contributor(s): none yet.
29  *
30  * ***** END GPL/BL DUAL LICENSE BLOCK *****
31  */
32
33 #include <string.h>
34 #include <math.h>
35
36 #include "MEM_guardedalloc.h"
37
38 #include "DNA_action_types.h"
39 #include "DNA_armature_types.h"
40 #include "DNA_constraint_types.h"
41 #include "DNA_curve_types.h"
42 #include "DNA_lattice_types.h"
43 #include "DNA_mesh_types.h"
44 #include "DNA_modifier_types.h"
45 #include "DNA_object_types.h"
46 #include "DNA_object_force.h"
47 #include "DNA_screen_types.h"
48 #include "DNA_space_types.h"
49 #include "DNA_scene_types.h"
50 #include "DNA_userdef_types.h"
51 #include "DNA_view3d_types.h"
52
53 #include "BIF_screen.h"
54 #include "BIF_resources.h"
55 #include "BIF_mywindow.h"
56 #include "BIF_gl.h"
57 #include "BIF_editaction.h"
58 #include "BIF_editarmature.h"
59 #include "BIF_editmesh.h"
60 #include "BIF_editnla.h"
61 #include "BIF_editsima.h"
62 #include "BIF_meshtools.h"
63 #include "BIF_retopo.h"
64
65 #include "BSE_editipo.h"
66 #include "BSE_editipo_types.h"
67
68 #ifdef WITH_VERSE
69 #include "BIF_verse.h"
70 #endif
71
72 #include "BKE_action.h"
73 #include "BKE_anim.h"
74 #include "BKE_armature.h"
75 #include "BKE_cloth.h"
76 #include "BKE_curve.h"
77 #include "BKE_depsgraph.h"
78 #include "BKE_displist.h"
79 #include "BKE_depsgraph.h"
80 #include "BKE_global.h"
81 #include "BKE_group.h"
82 #include "BKE_ipo.h"
83 #include "BKE_lattice.h"
84 #include "BKE_key.h"
85 #include "BKE_mesh.h"
86 #include "BKE_modifier.h"
87 #include "BKE_object.h"
88 #include "BKE_utildefines.h"
89
90 #ifdef WITH_VERSE
91 #include "BKE_verse.h"
92 #endif
93
94 #include "BSE_view.h"
95 #include "BSE_editaction_types.h"
96 #include "BDR_unwrapper.h"
97
98 #include "BLI_arithb.h"
99 #include "BLI_blenlib.h"
100 #include "BLI_editVert.h"
101 #include "BLI_rand.h"
102
103 #include "blendef.h"
104
105 #include "mydevice.h"
106
107 #include "transform.h"
108
109 extern ListBase editNurb;
110 extern ListBase editelems;
111
112 extern TransInfo Trans; /* From transform.c */
113
114 /* ************************** Functions *************************** */
115
116
117 void getViewVector(float coord[3], float vec[3])
118 {
119         TransInfo *t = BIF_GetTransInfo();
120
121         if (t->persp)
122         {
123                 float p1[4], p2[4];
124
125                 VECCOPY(p1, coord);
126                 p1[3] = 1.0f;
127                 VECCOPY(p2, p1);
128                 p2[3] = 1.0f;
129                 Mat4MulVec4fl(t->viewmat, p2);
130
131                 p2[0] = 2.0f * p2[0];
132                 p2[1] = 2.0f * p2[1];
133                 p2[2] = 2.0f * p2[2];
134
135                 Mat4MulVec4fl(t->viewinv, p2);
136
137                 VecSubf(vec, p1, p2);
138         }
139         else {
140                 VECCOPY(vec, t->viewinv[2]);
141         }
142         Normalize(vec);
143 }
144
145 /* ************************** GENERICS **************************** */
146
147 static void clipMirrorModifier(TransInfo *t, Object *ob)
148 {
149         ModifierData *md= ob->modifiers.first;
150         float tolerance[3] = {0.0f, 0.0f, 0.0f};
151         int axis = 0;
152
153         for (; md; md=md->next) {
154                 if (md->type==eModifierType_Mirror) {
155                         MirrorModifierData *mmd = (MirrorModifierData*) md;     
156                 
157                         if(mmd->flag & MOD_MIR_CLIPPING) {
158                                 if(mmd->flag & MOD_MIR_AXIS_X) {
159                                         axis |= 1;
160                                         tolerance[0] = mmd->tolerance;
161                                 }
162                                 if(mmd->flag & MOD_MIR_AXIS_Y) {
163                                         axis |= 2;
164                                         tolerance[1] = mmd->tolerance;
165                                 }
166                                 if(mmd->flag & MOD_MIR_AXIS_Z) {
167                                         axis |= 4;
168                                         tolerance[2] = mmd->tolerance;
169                                 }
170                         }
171                 }
172         }
173         if (axis) {
174                 TransData *td = t->data;
175                 int i;
176                 
177                 for(i = 0 ; i < t->total; i++, td++) {
178                         if (td->flag & TD_NOACTION)
179                                 break;
180                         if (td->loc==NULL)
181                                 break;
182                         
183                         if(axis & 1) {
184                                 if(fabs(td->iloc[0])<=tolerance[0] || td->loc[0]*td->iloc[0]<0.0f) td->loc[0]= 0.0f;
185                         }
186                         
187                         if(axis & 2) {
188                                 if(fabs(td->iloc[1])<=tolerance[1] || td->loc[1]*td->iloc[1]<0.0f) td->loc[1]= 0.0f;
189                         }
190                         if(axis & 4) {
191                                 if(fabs(td->iloc[2])<=tolerance[2] || td->loc[2]*td->iloc[2]<0.0f) td->loc[2]= 0.0f;
192                         }
193                 }
194         }
195 }
196
197 /* assumes G.obedit set to mesh object */
198 static void editmesh_apply_to_mirror(TransInfo *t)
199 {
200         TransData *td = t->data;
201         EditVert *eve;
202         int i;
203         
204         for(i = 0 ; i < t->total; i++, td++) {
205                 if (td->flag & TD_NOACTION)
206                         break;
207                 if (td->loc==NULL)
208                         break;
209                 
210                 eve= td->tdmir;
211                 if(eve) {
212                         eve->co[0]= -td->loc[0];
213                         eve->co[1]= td->loc[1];
214                         eve->co[2]= td->loc[2];
215                 }               
216         }               
217 }
218
219 /* called for updating while transform acts, once per redraw */
220 void recalcData(TransInfo *t)
221 {
222         Base *base;
223 #ifdef WITH_VERSE
224         struct TransData *td;
225 #endif
226         
227         if (t->spacetype == SPACE_ACTION) {
228                 Object *ob= OBACT;
229                 void *data;
230                 short context;
231                 
232                 /* determine what type of data we are operating on */
233                 data = get_action_context(&context);
234                 if (data == NULL) return;
235                 
236                 if (G.saction->lock) {
237                         if (context == ACTCONT_ACTION) {
238                                 if(ob) {
239                                         ob->ctime= -1234567.0f;
240                                         if(ob->pose || ob_get_key(ob))
241                                                 DAG_object_flush_update(G.scene, ob, OB_RECALC);
242                                         else
243                                                 DAG_object_flush_update(G.scene, ob, OB_RECALC_OB);
244                                 }
245                         }
246                         else if (context == ACTCONT_SHAPEKEY) {
247                                 DAG_object_flush_update(G.scene, OBACT, OB_RECALC_OB|OB_RECALC_DATA);
248                         }
249                 }
250         }       
251         else if (t->spacetype == SPACE_NLA) {
252                 if (G.snla->lock) {
253                         for (base=G.scene->base.first; base; base=base->next) {
254                                 if (base->flag & BA_HAS_RECALC_OB)
255                                         base->object->recalc |= OB_RECALC_OB;
256                                 if (base->flag & BA_HAS_RECALC_DATA)
257                                         base->object->recalc |= OB_RECALC_DATA;
258                                 
259                                 if (base->object->recalc) 
260                                         base->object->ctime= -1234567.0f;       // eveil! 
261                         }
262                         
263                         DAG_scene_flush_update(G.scene, screen_view3d_layers());
264                 }
265         }
266         else if (t->spacetype == SPACE_IPO) {
267                 EditIpo *ei;
268                 int dosort = 0;
269                 int a;
270                 
271                 /* do the flush first */
272                 flushTransIpoData(t);
273                 
274                 /* now test if there is a need to re-sort */
275                 ei= G.sipo->editipo;
276                 for (a=0; a<G.sipo->totipo; a++, ei++) {
277                         if (ISPOIN(ei, flag & IPO_VISIBLE, icu)) {
278                                 
279                                 /* watch it: if the time is wrong: do not correct handles */
280                                 if (test_time_ipocurve(ei->icu) ) dosort++;
281                                 else testhandles_ipocurve(ei->icu);
282                         }
283                 }
284                 
285                 /* do resort and other updates? */
286                 if (dosort) remake_ipo_transdata(t);
287                 if (G.sipo->showkey) update_ipokey_val();
288                 
289                 calc_ipo(G.sipo->ipo, (float)CFRA);
290                 
291                 /* update realtime - not working? */
292                 if (G.sipo->lock) {
293                         if (G.sipo->blocktype==ID_MA || G.sipo->blocktype==ID_TE) {
294                                 do_ipo(G.sipo->ipo);
295                         }
296                         else if(G.sipo->blocktype==ID_CA) {
297                                 do_ipo(G.sipo->ipo);
298                         }
299                         else if(G.sipo->blocktype==ID_KE) {
300                                 Object *ob= OBACT;
301                                 if(ob) {
302                                         ob->shapeflag &= ~OB_SHAPE_TEMPLOCK;
303                                         DAG_object_flush_update(G.scene, ob, OB_RECALC_DATA);
304                                 }
305                         }
306                         else if(G.sipo->blocktype==ID_PO) {
307                                 Object *ob= OBACT;
308                                 if(ob && ob->pose) {
309                                         DAG_object_flush_update(G.scene, ob, OB_RECALC_DATA);
310                                 }
311                         }
312                         else if(G.sipo->blocktype==ID_OB) {
313                                 Base *base= FIRSTBASE;
314                                 
315                                 while(base) {
316                                         if(base->object->ipo==G.sipo->ipo) {
317                                                 do_ob_ipo(base->object);
318                                                 base->object->recalc |= OB_RECALC_OB;
319                                         }
320                                         base= base->next;
321                                 }
322                                 DAG_scene_flush_update(G.scene, screen_view3d_layers());
323                         }
324                 }
325         }
326         else if (G.obedit) {
327                 if (G.obedit->type == OB_MESH) {
328                         if(t->spacetype==SPACE_IMAGE) {
329                                 flushTransUVs(t);
330                                 if (G.sima->flag & SI_LIVE_UNWRAP)
331                                         unwrap_lscm_live_re_solve();
332                         } else {
333                         
334                                 retopo_do_all();
335         
336                                 /* mirror modifier clipping? */
337                                 if(t->state != TRANS_CANCEL)
338                                         clipMirrorModifier(t, G.obedit);
339                                 
340                                 if(G.scene->toolsettings->editbutflag & B_MESH_X_MIRROR)
341                                         editmesh_apply_to_mirror(t);
342                                 
343                                 DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);  /* sets recalc flags */
344                                 
345                                 recalc_editnormals();
346                         }
347                 }
348                 else if ELEM(G.obedit->type, OB_CURVE, OB_SURF) {
349                         Nurb *nu= editNurb.first;
350                         DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);  /* sets recalc flags */
351                         
352                         while(nu) {
353                                 test2DNurb(nu);
354                                 testhandlesNurb(nu); /* test for bezier too */
355                                 nu= nu->next;
356                         }
357
358                         retopo_do_all();
359                 }
360                 else if(G.obedit->type==OB_ARMATURE){   /* no recalc flag, does pose */
361                         bArmature *arm= G.obedit->data;
362                         EditBone *ebo;
363                         
364                         /* Ensure all bones are correctly adjusted */
365                         for (ebo=G.edbo.first; ebo; ebo=ebo->next){
366                                 
367                                 if ((ebo->flag & BONE_CONNECTED) && ebo->parent){
368                                         /* If this bone has a parent tip that has been moved */
369                                         if (ebo->parent->flag & BONE_TIPSEL){
370                                                 VECCOPY (ebo->head, ebo->parent->tail);
371                                                 if(t->mode==TFM_BONE_ENVELOPE) ebo->rad_head= ebo->parent->rad_tail;
372                                         }
373                                         /* If this bone has a parent tip that has NOT been moved */
374                                         else{
375                                                 VECCOPY (ebo->parent->tail, ebo->head);
376                                                 if(t->mode==TFM_BONE_ENVELOPE) ebo->parent->rad_tail= ebo->rad_head;
377                                         }
378                                 }
379                                 
380                                 /* on extrude bones, oldlength==0.0f, so we scale radius of points */
381                                 ebo->length= VecLenf(ebo->head, ebo->tail);
382                                 if(ebo->oldlength==0.0f) {
383                                         ebo->rad_head= 0.25f*ebo->length;
384                                         ebo->rad_tail= 0.10f*ebo->length;
385                                         ebo->dist= 0.25f*ebo->length;
386                                         if(ebo->parent) {
387                                                 if(ebo->rad_head > ebo->parent->rad_tail)
388                                                         ebo->rad_head= ebo->parent->rad_tail;
389                                         }
390                                 }
391                                 else if(t->mode!=TFM_BONE_ENVELOPE) {
392                                         /* if bones change length, lets do that for the deform distance as well */
393                                         ebo->dist*= ebo->length/ebo->oldlength;
394                                         ebo->rad_head*= ebo->length/ebo->oldlength;
395                                         ebo->rad_tail*= ebo->length/ebo->oldlength;
396                                         ebo->oldlength= ebo->length;
397                                 }
398                         }
399                         if(arm->flag & ARM_MIRROR_EDIT) 
400                                 transform_armature_mirror_update();
401                         
402                 }
403                 else if(G.obedit->type==OB_LATTICE) {
404                         DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);  /* sets recalc flags */
405                         
406                         if(editLatt->flag & LT_OUTSIDE) outside_lattice(editLatt);
407                 }
408                 else {
409                         DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);  /* sets recalc flags */
410                 }
411         }
412         else if( (t->flag & T_POSE) && t->poseobj) {
413                 Object *ob= t->poseobj;
414                 bArmature *arm= ob->data;
415                 
416                 /* old optimize trick... this enforces to bypass the depgraph */
417                 if (!(arm->flag & ARM_DELAYDEFORM)) {
418                         DAG_object_flush_update(G.scene, ob, OB_RECALC_DATA);  /* sets recalc flags */
419                         
420                         /* bah, softbody exception... recalcdata doesnt reset */
421                         for(base= FIRSTBASE; base; base= base->next) {
422                                 if(base->object->recalc & OB_RECALC_DATA)
423                                 {                                       
424                                         ClothModifierData *clmd = NULL;
425                                         
426                                         if(modifiers_isSoftbodyEnabled(base->object)) {
427                                                 base->object->softflag |= OB_SB_REDO;
428                                         }
429                                         else if(modifiers_isClothEnabled(ob)) {
430                                                 cloth_free_modifier(modifiers_isClothEnabled(ob));
431                                         }
432                                         
433                                 }
434                         }
435                 }
436                 else
437                         where_is_pose(ob);
438         }
439         else {
440                 for(base= FIRSTBASE; base; base= base->next) {
441                         Object *ob= base->object;
442                         
443                         /* this flag is from depgraph, was stored in nitialize phase, handled in drawview.c */
444                         if(base->flag & BA_HAS_RECALC_OB)
445                                 ob->recalc |= OB_RECALC_OB;
446                         if(base->flag & BA_HAS_RECALC_DATA)
447                                 ob->recalc |= OB_RECALC_DATA;
448                         
449                         /* thanks to ob->ctime usage, ipos are not called in where_is_object,
450                            unless we edit ipokeys */
451                         if(base->flag & BA_DO_IPO) {
452                                 if(ob->ipo) {
453                                         IpoCurve *icu;
454                                         
455                                         ob->ctime= -1234567.0;
456                                         
457                                         icu= ob->ipo->curve.first;
458                                         while(icu) {
459                                                 calchandles_ipocurve(icu);
460                                                 icu= icu->next;
461                                         }
462                                 }                               
463                         }
464                         
465                         /* softbody & cloth exception */
466                         if(ob->recalc & OB_RECALC_DATA)
467                         {
468                                 ClothModifierData *clmd = NULL;
469                                 
470                                 if(modifiers_isSoftbodyEnabled(ob)) {
471                                                 ob->softflag |= OB_SB_REDO;
472                                 }
473                                 else if(modifiers_isClothEnabled(ob)) {
474                                         cloth_free_modifier(modifiers_isClothEnabled(ob));
475                                 }
476                         }
477                         
478                         /* proxy exception */
479                         if(ob->proxy)
480                                 ob->proxy->recalc |= ob->recalc;
481                         if(ob->proxy_group)
482                                 group_tag_recalc(ob->proxy_group->dup_group);
483                 } 
484         }
485
486 #ifdef WITH_VERSE
487         for (td = t->data; td < t->data + t->total; td++) {
488                 if(td->flag & TD_VERSE_VERT) {
489                         if(td->verse)
490                                 send_versevert_pos((VerseVert*)td->verse);
491                 }
492                 else if(td->flag & TD_VERSE_OBJECT)
493                         if(td->verse) b_verse_send_transformation((Object*)td->verse);
494         }
495 #endif
496         
497         /* update shaded drawmode while transform */
498         if(t->spacetype==SPACE_VIEW3D && G.vd->drawtype == OB_SHADED)
499                 reshadeall_displist();
500         
501 }
502
503 void drawLine(float *center, float *dir, char axis, short options)
504 {
505         extern void make_axis_color(char *col, char *col2, char axis);  // drawview.c
506         float v1[3], v2[3], v3[3];
507         char col[3], col2[3];
508         
509         //if(G.obedit) mymultmatrix(G.obedit->obmat);   // sets opengl viewing
510
511         VecCopyf(v3, dir);
512         VecMulf(v3, G.vd->far);
513         
514         VecSubf(v2, center, v3);
515         VecAddf(v1, center, v3);
516
517         if (options & DRAWLIGHT) {
518                 col[0] = col[1] = col[2] = 220;
519         }
520         else {
521                 BIF_GetThemeColor3ubv(TH_GRID, col);
522         }
523         make_axis_color(col, col2, axis);
524         glColor3ubv((GLubyte *)col2);
525
526         setlinestyle(0);
527         glBegin(GL_LINE_STRIP); 
528                 glVertex3fv(v1); 
529                 glVertex3fv(v2); 
530         glEnd();
531         
532         myloadmatrix(G.vd->viewmat);
533 }
534
535 void initTrans (TransInfo *t)
536 {
537         /* moving: is shown in drawobject() (transform color) */
538         if(G.obedit || (t->flag & T_POSE) ) G.moving= G_TRANSFORM_EDIT;
539         else G.moving= G_TRANSFORM_OBJ;
540
541         t->data = NULL;
542         t->ext = NULL;
543
544         t->flag = 0;
545         t->num.flag = 0;
546
547
548         /* setting PET flag */
549         if ((t->context & CTX_NO_PET) == 0 && (G.scene->proportional)) {
550                 t->flag |= T_PROP_EDIT;
551                 if(G.scene->proportional==2) t->flag |= T_PROP_CONNECTED;       // yes i know, has to become define
552         }
553
554         getmouseco_areawin(t->imval);
555         t->con.imval[0] = t->imval[0];
556         t->con.imval[1] = t->imval[1];
557
558         t->transform            = NULL;
559         t->handleEvent          = NULL;
560         t->customData           = NULL;
561
562         t->total                        =
563                 t->num.idx              =
564                 t->num.idx_max  =
565                 t->num.ctrl[0]  = 
566                 t->num.ctrl[1]  = 
567                 t->num.ctrl[2]  = 0;
568
569         t->val = 0.0f;
570
571         t->num.val[0]           = 
572                 t->num.val[1]   = 
573                 t->num.val[2]   = 0.0f;
574
575         t->vec[0]                       =
576                 t->vec[1]               =
577                 t->vec[2]               = 0.0f;
578         
579         t->center[0]            =
580                 t->center[1]    =
581                 t->center[2]    = 0.0f;
582         
583         Mat3One(t->mat);
584         
585         t->spacetype = curarea->spacetype;
586         if(t->spacetype==SPACE_VIEW3D) {
587                 if(G.vd->flag & V3D_ALIGN) t->flag |= T_V3D_ALIGN;
588                 t->around = G.vd->around;
589         } else if(t->spacetype==SPACE_IMAGE) {
590                 t->around = G.v2d->around;
591         }
592         else
593                 t->around = V3D_CENTER;
594
595         setTransformViewMatrices(t);
596 }
597
598 /* Here I would suggest only TransInfo related issues, like free data & reset vars. Not redraws */
599 void postTrans (TransInfo *t) 
600 {
601         TransData *td;
602
603         G.moving = 0; // Set moving flag off (display as usual)
604 #ifdef WITH_VERSE
605
606         for (td = t->data; td < t->data + t->total; td++) {
607                 if(td->flag & TD_VERSE_VERT) {
608                         if(td->verse) send_versevert_pos((VerseVert*)td->verse);
609                 }
610                 else if(td->flag & TD_VERSE_OBJECT) {
611                         if(td->verse) {
612                                 struct VNode *vnode;
613                                 vnode = (VNode*)((Object*)td->verse)->vnode;
614                                 ((VObjectData*)vnode->data)->flag |= POS_SEND_READY;
615                                 ((VObjectData*)vnode->data)->flag |= ROT_SEND_READY;
616                                 ((VObjectData*)vnode->data)->flag |= SCALE_SEND_READY;
617                                 b_verse_send_transformation((Object*)td->verse);
618                         }
619                 }
620         }
621 #endif
622
623         stopConstraint(t);
624         
625         /* postTrans can be called when nothing is selected, so data is NULL already */
626         if (t->data) {
627                 int a;
628
629                 /* since ipokeys are optional on objects, we mallocced them per trans-data */
630                 for(a=0, td= t->data; a<t->total; a++, td++) {
631                         if(td->tdi) MEM_freeN(td->tdi);
632                 }
633                 MEM_freeN(t->data);
634         }
635
636         if (t->ext) MEM_freeN(t->ext);
637         if (t->data2d) {
638                 MEM_freeN(t->data2d);
639                 t->data2d= NULL;
640         }
641         
642         if ((t->flag & T_FREE_CUSTOMDATA) && t->customData != NULL) {
643                 MEM_freeN(t->customData);
644         }
645
646         if(t->spacetype==SPACE_IMAGE) {
647                 if (G.sima->flag & SI_LIVE_UNWRAP)
648                         unwrap_lscm_live_end(t->state == TRANS_CANCEL);
649         }
650 }
651
652 void applyTransObjects(TransInfo *t)
653 {
654         TransData *td;
655         
656         for (td = t->data; td < t->data + t->total; td++) {
657                 VECCOPY(td->iloc, td->loc);
658                 if (td->ext->rot) {
659                         VECCOPY(td->ext->irot, td->ext->rot);
660                 }
661                 if (td->ext->size) {
662                         VECCOPY(td->ext->isize, td->ext->size);
663                 }
664         }       
665         recalcData(t);
666
667
668 /* helper for below */
669 static void restore_ipokey(float *poin, float *old)
670 {
671         if(poin) {
672                 poin[0]= old[0];
673                 poin[-3]= old[3];
674                 poin[3]= old[6];
675         }
676 }
677
678 static void restoreElement(TransData *td) {
679         /* TransData for crease has no loc */
680         if (td->loc) {
681                 VECCOPY(td->loc, td->iloc);
682         }
683         if (td->val) {
684                 *td->val = td->ival;
685         }
686         if (td->ext) {
687                 if (td->ext->rot) {
688                         VECCOPY(td->ext->rot, td->ext->irot);
689                 }
690                 if (td->ext->size) {
691                         VECCOPY(td->ext->size, td->ext->isize);
692                 }
693                 if(td->flag & TD_USEQUAT) {
694                         if (td->ext->quat) {
695                                 QUATCOPY(td->ext->quat, td->ext->iquat);
696                         }
697                 }
698         }
699         if(td->tdi) {
700                 TransDataIpokey *tdi= td->tdi;
701                 
702                 restore_ipokey(tdi->locx, tdi->oldloc);
703                 restore_ipokey(tdi->locy, tdi->oldloc+1);
704                 restore_ipokey(tdi->locz, tdi->oldloc+2);
705
706                 restore_ipokey(tdi->rotx, tdi->oldrot);
707                 restore_ipokey(tdi->roty, tdi->oldrot+1);
708                 restore_ipokey(tdi->rotz, tdi->oldrot+2);
709                 
710                 restore_ipokey(tdi->sizex, tdi->oldsize);
711                 restore_ipokey(tdi->sizey, tdi->oldsize+1);
712                 restore_ipokey(tdi->sizez, tdi->oldsize+2);
713         }
714 }
715
716 void restoreTransObjects(TransInfo *t)
717 {
718         TransData *td;
719         
720         for (td = t->data; td < t->data + t->total; td++) {
721                 restoreElement(td);
722 #ifdef WITH_VERSE
723                 /* position of vertexes and object transformation matrix is sent
724                  * extra, becuase blender uses synchronous sending of vertexes
725                  * position as well object trans. matrix and it isn't possible to
726                  * send it in recalcData sometimes */
727                 if(td->flag & TD_VERSE_VERT) {
728                         if(td->verse) {
729                                 ((VerseVert*)td->verse)->flag |= VERT_POS_OBSOLETE;
730                         }
731                 }
732                 else if(td->flag & TD_VERSE_OBJECT)
733                         if(td->verse) {
734                                 struct VNode *vnode;
735                                 vnode = (VNode*)((Object*)td->verse)->vnode;
736                                 ((VObjectData*)vnode->data)->flag |= POS_SEND_READY;
737                                 ((VObjectData*)vnode->data)->flag |= ROT_SEND_READY;
738                                 ((VObjectData*)vnode->data)->flag |= SCALE_SEND_READY;
739                         }
740 #endif
741         }       
742         recalcData(t);
743 }
744
745 void calculateCenter2D(TransInfo *t)
746 {
747         if (t->flag & (T_EDIT|T_POSE)) {
748                 Object *ob= G.obedit?G.obedit:t->poseobj;
749                 float vec[3];
750                 
751                 VECCOPY(vec, t->center);
752                 Mat4MulVecfl(ob->obmat, vec);
753                 projectIntView(t, vec, t->center2d);
754         }
755         else {
756                 projectIntView(t, t->center, t->center2d);
757         }
758 }
759
760 void calculateCenterCursor(TransInfo *t)
761 {
762         float *cursor;
763
764         cursor = give_cursor();
765         VECCOPY(t->center, cursor);
766
767         /* If edit or pose mode, move cursor in local space */
768         if (t->flag & (T_EDIT|T_POSE)) {
769                 Object *ob = G.obedit?G.obedit:t->poseobj;
770                 float mat[3][3], imat[3][3];
771                 
772                 VecSubf(t->center, t->center, ob->obmat[3]);
773                 Mat3CpyMat4(mat, ob->obmat);
774                 Mat3Inv(imat, mat);
775                 Mat3MulVecfl(imat, t->center);
776         }
777         
778         calculateCenter2D(t);
779 }
780
781 void calculateCenterCursor2D(TransInfo *t)
782 {
783         float aspx=1.0, aspy=1.0;
784         
785         if(t->spacetype==SPACE_IMAGE) /* only space supported right now but may change */
786                 transform_aspect_ratio_tface_uv(&aspx, &aspy);
787         if (G.v2d) {
788                 t->center[0] = G.v2d->cursor[0] * aspx; 
789                 t->center[1] = G.v2d->cursor[1] * aspy; 
790         }
791         calculateCenter2D(t);
792 }
793
794 void calculateCenterMedian(TransInfo *t)
795 {
796         float partial[3] = {0.0f, 0.0f, 0.0f};
797         int i;
798         
799         for(i = 0; i < t->total; i++) {
800                 if (t->data[i].flag & TD_SELECTED) {
801                         VecAddf(partial, partial, t->data[i].center);
802                 }
803                 else {
804                         /* 
805                            All the selected elements are at the head of the array 
806                            which means we can stop when it finds unselected data
807                         */
808                         break;
809                 }
810         }
811         if(i)
812                 VecMulf(partial, 1.0f / i);
813         VECCOPY(t->center, partial);
814
815         calculateCenter2D(t);
816 }
817
818 void calculateCenterBound(TransInfo *t)
819 {
820         float max[3];
821         float min[3];
822         int i;
823         for(i = 0; i < t->total; i++) {
824                 if (i) {
825                         if (t->data[i].flag & TD_SELECTED) {
826                                 MinMax3(min, max, t->data[i].center);
827                         }
828                         else {
829                                 /* 
830                                    All the selected elements are at the head of the array 
831                                    which means we can stop when it finds unselected data
832                                 */
833                                 break;
834                         }
835                 }
836                 else {
837                         VECCOPY(max, t->data[i].center);
838                         VECCOPY(min, t->data[i].center);
839                 }
840         }
841         VecAddf(t->center, min, max);
842         VecMulf(t->center, 0.5);
843
844         calculateCenter2D(t);
845 }
846
847 void calculateCenter(TransInfo *t) 
848 {
849         switch(t->around) {
850         case V3D_CENTER:
851                 calculateCenterBound(t);
852                 break;
853         case V3D_CENTROID:
854                 calculateCenterMedian(t);
855                 break;
856         case V3D_CURSOR:
857                 if(t->spacetype==SPACE_IMAGE)
858                         calculateCenterCursor2D(t);
859                 else
860                         calculateCenterCursor(t);
861                 break;
862         case V3D_LOCAL:
863                 /* Individual element center uses median center for helpline and such */
864                 calculateCenterMedian(t);
865                 break;
866         case V3D_ACTIVE:
867                 /* set median, and if if if... do object center */
868                 
869                 /* EDIT MODE ACTIVE EDITMODE ELEMENT */
870                 if (G.obedit && G.obedit->type == OB_MESH && G.editMesh->selected.last) {
871                         EM_editselection_center(t->center, G.editMesh->selected.last);
872                         calculateCenter2D(t);
873                         break;
874                 } /* END EDIT MODE ACTIVE ELEMENT */
875                 
876                 calculateCenterMedian(t);
877                 if((t->flag & (T_EDIT|T_POSE))==0) {
878                         Object *ob= OBACT;
879                         if(ob) {
880                                 VECCOPY(t->center, ob->obmat[3]);
881                                 projectIntView(t, t->center, t->center2d);
882                         }
883                 }
884         }
885
886         /* setting constraint center */
887         VECCOPY(t->con.center, t->center);
888         if(t->flag & (T_EDIT|T_POSE)) {
889                 Object *ob= G.obedit?G.obedit:t->poseobj;
890                 Mat4MulVecfl(ob->obmat, t->con.center);
891         }
892
893         /* voor panning from cameraview */
894         if(t->flag & T_OBJECT) {
895                 if( G.vd->camera==OBACT && G.vd->persp>1) {
896                         float axis[3];
897                         /* persinv is nasty, use viewinv instead, always right */
898                         VECCOPY(axis, t->viewinv[2]);
899                         Normalize(axis);
900
901                         /* 6.0 = 6 grid units */
902                         axis[0]= t->center[0]- 6.0f*axis[0];
903                         axis[1]= t->center[1]- 6.0f*axis[1];
904                         axis[2]= t->center[2]- 6.0f*axis[2];
905                         
906                         projectIntView(t, axis, t->center2d);
907                         
908                         /* rotate only needs correct 2d center, grab needs initgrabz() value */
909                         if(t->mode==TFM_TRANSLATION) {
910                                 VECCOPY(t->center, axis);
911                                 VECCOPY(t->con.center, t->center);
912                         }
913                 }
914         }       
915
916         if(t->spacetype==SPACE_VIEW3D)
917                 initgrabz(t->center[0], t->center[1], t->center[2]);
918 }
919
920 void calculatePropRatio(TransInfo *t)
921 {
922         TransData *td = t->data;
923         int i;
924         float dist;
925         short connected = t->flag & T_PROP_CONNECTED;
926
927         if (t->flag & T_PROP_EDIT) {
928                 for(i = 0 ; i < t->total; i++, td++) {
929                         if (td->flag & TD_SELECTED) {
930                                 td->factor = 1.0f;
931                         }
932                         else if ((connected && 
933                                                 (td->flag & TD_NOTCONNECTED || td->dist > t->propsize))
934                                 ||
935                                         (connected == 0 &&
936                                                 td->rdist > t->propsize)) {
937                                 /* 
938                                    The elements are sorted according to their dist member in the array,
939                                    that means we can stop when it finds one element outside of the propsize.
940                                 */
941                                 td->flag |= TD_NOACTION;
942                                 td->factor = 0.0f;
943                                 restoreElement(td);
944                         }
945                         else {
946                                 /* Use rdist for falloff calculations, it is the real distance */
947                                 td->flag &= ~TD_NOACTION;
948                                 dist= (t->propsize-td->rdist)/t->propsize;
949                                 
950                                 /*
951                                  * Clamp to positive numbers.
952                                  * Certain corner cases with connectivity and individual centers
953                                  * can give values of rdist larger than propsize.
954                                  */
955                                 if (dist < 0.0f)
956                                         dist = 0.0f;
957                                 
958                                 switch(G.scene->prop_mode) {
959                                 case PROP_SHARP:
960                                         td->factor= dist*dist;
961                                         break;
962                                 case PROP_SMOOTH:
963                                         td->factor= 3.0f*dist*dist - 2.0f*dist*dist*dist;
964                                         break;
965                                 case PROP_ROOT:
966                                         td->factor = (float)sqrt(dist);
967                                         break;
968                                 case PROP_LIN:
969                                         td->factor = dist;
970                                         break;
971                                 case PROP_CONST:
972                                         td->factor = 1.0f;
973                                         break;
974                                 case PROP_SPHERE:
975                                         td->factor = (float)sqrt(2*dist - dist * dist);
976                                         break;
977                                 case PROP_RANDOM:
978                                         BLI_srand( BLI_rand() ); /* random seed */
979                                         td->factor = BLI_frand()*dist;
980                                         break;
981                                 default:
982                                         td->factor = 1;
983                                 }
984                         }
985                 }
986                 switch(G.scene->prop_mode) {
987                 case PROP_SHARP:
988                         strcpy(t->proptext, "(Sharp)");
989                         break;
990                 case PROP_SMOOTH:
991                         strcpy(t->proptext, "(Smooth)");
992                         break;
993                 case PROP_ROOT:
994                         strcpy(t->proptext, "(Root)");
995                         break;
996                 case PROP_LIN:
997                         strcpy(t->proptext, "(Linear)");
998                         break;
999                 case PROP_CONST:
1000                         strcpy(t->proptext, "(Constant)");
1001                         break;
1002                 case PROP_SPHERE:
1003                         strcpy(t->proptext, "(Sphere)");
1004                         break;
1005                 case PROP_RANDOM:
1006                         strcpy(t->proptext, "(Random)");
1007                         break;
1008                 default:
1009                         strcpy(t->proptext, "");
1010                 }
1011         }
1012         else {
1013                 for(i = 0 ; i < t->total; i++, td++) {
1014                         td->factor = 1.0;
1015                 }
1016                 strcpy(t->proptext, "");
1017         }
1018 }
1019
1020 TransInfo * BIF_GetTransInfo() {
1021         return &Trans;
1022 }
1023