Initial revision
[blender.git] / source / blender / blenkernel / intern / key.c
1
2 /*  key.c      MIXED MODEL
3  * 
4  *  mei 95
5  *  
6  * 
7  * $Id$
8  *
9  * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version. The Blender
15  * Foundation also sells licenses for use in proprietary software under
16  * the Blender License.  See http://www.blender.org/BL/ for information
17  * about this.
18  *
19  * This program is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  * GNU General Public License for more details.
23  *
24  * You should have received a copy of the GNU General Public License
25  * along with this program; if not, write to the Free Software Foundation,
26  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
27  *
28  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
29  * All rights reserved.
30  *
31  * The Original Code is: all of this file.
32  *
33  * Contributor(s): none yet.
34  *
35  * ***** END GPL/BL DUAL LICENSE BLOCK *****
36  */
37
38 #include <math.h>
39 #include <string.h>
40
41 #include "MEM_guardedalloc.h"
42
43 #include "DNA_key_types.h"
44 #include "DNA_ipo_types.h"
45 #include "DNA_mesh_types.h"
46 #include "DNA_object_types.h"
47 #include "DNA_curve_types.h"
48 #include "DNA_lattice_types.h"
49
50 #include "BLI_blenlib.h"
51 #include "BKE_utildefines.h"
52 #include "BKE_bad_level_calls.h"
53 #include "BKE_global.h"
54 #include "BKE_main.h"
55 #include "BKE_library.h"
56 #include "BKE_blender.h"
57 #include "BKE_curve.h"
58 #include "BKE_object.h"
59 #include "BKE_mesh.h"
60 #include "BKE_key.h"
61 #include "BKE_ipo.h"
62 #include "BKE_lattice.h"
63
64
65 #define KEY_BPOINT              1
66 #define KEY_BEZTRIPLE   2
67
68 int slurph_opt= 1;
69
70
71 void free_key(Key *key)
72 {
73         KeyBlock *kb;
74         
75         if(key->ipo) key->ipo->id.us--;
76         
77         
78         while( (kb= key->block.first) ) {
79                 
80                 if(kb->data) MEM_freeN(kb->data);
81                 
82                 BLI_remlink(&key->block, kb);
83                 MEM_freeN(kb);
84         }
85         
86 }
87
88 /* GS reads the memory pointed at in a specific ordering. There are,
89  * however two definitions for it. I have jotted them down here, both,
90  * but I think the first one is actually used. The thing is that
91  * big-endian systems might read this the wrong way round. OTOH, we
92  * constructed the IDs that are read out with this macro explicitly as
93  * well. I expect we'll sort it out soon... */
94
95 /* from blendef: */
96 #define GS(a)   (*((short *)(a)))
97
98 /* from misc_util: flip the bytes from x  */
99 /*  #define GS(x) (((unsigned char *)(x))[0] << 8 | ((unsigned char *)(x))[1]) */
100
101 Key *add_key(ID *id)    /* algemeen */
102 {
103         Key *key;
104         char *el;
105         
106         key= alloc_libblock(&G.main->key, ID_KE, "Key");
107         
108         key->type= KEY_NORMAL;
109         key->from= id;
110         
111         if( GS(id->name)==ID_ME) {
112                 el= key->elemstr;
113                 
114                 el[0]= 3;
115                 el[1]= IPO_FLOAT;
116                 el[2]= 0;
117                 
118                 key->elemsize= 12;
119         }
120         else if( GS(id->name)==ID_LT) {
121                 el= key->elemstr;
122                 
123                 el[0]= 3;
124                 el[1]= IPO_FLOAT;
125                 el[2]= 0;
126                 
127                 key->elemsize= 12;
128         }
129         else if( GS(id->name)==ID_CU) {
130                 el= key->elemstr;
131                 
132                 el[0]= 4;
133                 el[1]= IPO_BPOINT;
134                 el[2]= 0;
135                 
136                 key->elemsize= 16;
137         }
138         
139         return key;
140 }
141
142 Key *copy_key(Key *key)
143 {
144         Key *keyn;
145         KeyBlock *kbn, *kb;
146         
147         if(key==0) return 0;
148         
149         keyn= copy_libblock(key);
150         
151         keyn->ipo= copy_ipo(key->ipo);
152
153         duplicatelist(&keyn->block, &key->block);
154         
155         kb= key->block.first;
156         kbn= keyn->block.first;
157         while(kbn) {
158                 
159                 if(kbn->data) kbn->data= MEM_dupallocN(kbn->data);
160                 if( kb==key->refkey ) keyn->refkey= kbn;
161                 
162                 kbn= kbn->next;
163                 kb= kb->next;
164         }
165         
166         return keyn;
167 }
168
169 void make_local_key(Key *key)
170 {
171         
172         /* - zijn er alleen lib users: niet doen
173          * - zijn er alleen locale users: flag zetten
174          * - mixed: copy
175          */
176         if(key==0) return;
177         
178         key->id.lib= 0;
179         new_id(0, (ID *)key, 0);
180         make_local_ipo(key->ipo);
181 }
182
183
184 void sort_keys(Key *key)
185 {
186         KeyBlock *kb;
187         int doit=1;
188         
189         while(doit) {
190                 doit= 0;
191                 
192                 kb= key->block.first;
193                 while(kb) {
194                         if(kb->next) {
195                                 if(kb->pos > kb->next->pos) {
196                                         BLI_remlink(&key->block, kb);
197                                         
198                                         /* insertlink(lb, prevlink, newlink): newlink komt na prevlink */
199                                         BLI_insertlink(&key->block, kb->next, kb);
200                                         
201                                         doit= 1;
202                                         break;
203                                 }
204                         }
205                         kb= kb->next;
206                 }       
207         }
208         
209 }
210
211 /**************** do the key ****************/
212
213
214 void set_four_ipo(float d, float *data, int type)
215 {
216         float d2, d3, fc;
217         
218         if(type==KEY_LINEAR) {
219                 data[0]= 0.0f;
220                 data[1]= 1.0f-d;
221                 data[2]= d;
222                 data[3]= 0.0f;
223         }
224         else {
225                 d2= d*d;
226                 d3= d2*d;
227                 
228                 if(type==KEY_CARDINAL) {
229
230                         fc= 0.71f;
231                         
232                         data[0]= -fc*d3         +2.0f*fc*d2             -fc*d;
233                         data[1]= (2.0f-fc)*d3   +(fc-3.0f)*d2                           +1.0f;
234                         data[2]= (fc-2.0f)*d3   +(3.0f-2.0f*fc)*d2 +fc*d;
235                         data[3]= fc*d3                  -fc*d2;
236                 }
237                 else if(type==KEY_BSPLINE) {
238
239                         data[0]= -0.1666f*d3    +0.5f*d2        -0.5f*d +0.16666f;
240                         data[1]= 0.5f*d3                -d2                             +0.6666f;
241                         data[2]= -0.5f*d3               +0.5f*d2        +0.5f*d +0.1666f;
242                         data[3]= 0.1666f*d3                     ;
243                 }
244         }
245 }
246
247 void set_afgeleide_four_ipo(float d, float *data, int type)
248 {
249         float d2, fc;
250         
251         if(type==KEY_LINEAR) {
252
253         }
254         else {
255                 d2= d*d;
256                 
257                 if(type==KEY_CARDINAL) {
258
259                         fc= 0.71f;
260                         
261                         data[0]= -3.0f*fc*d2            +4.0f*fc*d              -fc;
262                         data[1]= 3.0f*(2.0f-fc)*d2      +2.0f*(fc-3.0f)*d;
263                         data[2]= 3.0f*(fc-2.0f)*d2      +2.0f*(3.0f-2.0f*fc)*d +fc;
264                         data[3]= 3.0f*fc*d2                     -2.0f*fc*d;
265                 }
266                 else if(type==KEY_BSPLINE) {
267
268                         data[0]= -0.1666f*3.0f*d2       +d      -0.5f;
269                         data[1]= 1.5f*d2                -2.0f*d;
270                         data[2]= -1.5f*d2               +d      +0.5f;
271                         data[3]= 0.1666f*3.0f*d2                        ;
272                 }
273         }
274 }
275
276 int setkeys(float fac, ListBase *lb, KeyBlock *k[], float *t, int cycl)
277 {
278         /* return 1 betekent k[2] is de positie, 0 is interpoleren */
279         KeyBlock *k1, *firstkey;
280         float d, dpos, ofs=0, lastpos, temp, fval[4];
281         short bsplinetype;
282
283         firstkey= lb->first;
284         k1= lb->last;
285         lastpos= k1->pos;
286         dpos= lastpos - firstkey->pos;
287
288         if(fac < firstkey->pos) fac= firstkey->pos;
289         else if(fac > k1->pos) fac= k1->pos;
290
291         k1=k[0]=k[1]=k[2]=k[3]= firstkey;
292         t[0]=t[1]=t[2]=t[3]= k1->pos;
293
294         /* if(fac<0.0 || fac>1.0) return 1; */
295
296         if(k1->next==0) return 1;
297
298         if(cycl) {      /* voorsorteren */
299                 k[2]= k1->next;
300                 k[3]= k[2]->next;
301                 if(k[3]==0) k[3]=k1;
302                 while(k1) {
303                         if(k1->next==0) k[0]=k1;
304                         k1=k1->next;
305                 }
306                 k1= k[1];
307                 t[0]= k[0]->pos;
308                 t[1]+= dpos;
309                 t[2]= k[2]->pos + dpos;
310                 t[3]= k[3]->pos + dpos;
311                 fac+= dpos;
312                 ofs= dpos;
313                 if(k[3]==k[1]) { 
314                         t[3]+= dpos; 
315                         ofs= 2.0f*dpos;
316                 }
317                 if(fac<t[1]) fac+= dpos;
318                 k1= k[3];
319         }
320         else {          /* voorsorteren */
321                 /* waarom dit voorsorteren niet eerder gedaan? voor juist interpoleren in begin noodz. */
322                 k[2]= k1->next;
323                 t[2]= k[2]->pos;
324                 k[3]= k[2]->next;
325                 if(k[3]==0) k[3]= k[2];
326                 t[3]= k[3]->pos;
327                 k1= k[3];
328         }
329         
330         while( t[2]<fac ) {     /* goede plek vinden */
331                 if(k1->next==0) {
332                         if(cycl) {
333                                 k1= firstkey;
334                                 ofs+= dpos;
335                         }
336                         else if(t[2]==t[3]) break;
337                 }
338                 else k1= k1->next;
339
340                 t[0]= t[1]; 
341                 k[0]= k[1];
342                 t[1]= t[2]; 
343                 k[1]= k[2];
344                 t[2]= t[3]; 
345                 k[2]= k[3];
346                 t[3]= k1->pos+ofs; 
347                 k[3]= k1;
348
349                 if(ofs>2.1+lastpos) break;
350         }
351         
352         bsplinetype= 0;
353         if(k[1]->type==KEY_BSPLINE || k[2]->type==KEY_BSPLINE) bsplinetype= 1;
354
355
356         if(cycl==0) {
357                 if(bsplinetype==0) {    /* B spline gaat niet door de punten */
358                         if(fac<=t[1]) {         /* fac voor 1e key */
359                                 t[2]= t[1];
360                                 k[2]= k[1];
361                                 return 1;
362                         }
363                         if(fac>=t[2] ) {        /* fac na 2e key */
364                                 return 1;
365                         }
366                 }
367                 else if(fac>t[2]) {     /* laatste key */
368                         fac= t[2];
369                         k[3]= k[2];
370                         t[3]= t[2];
371                 }
372         }
373
374         d= t[2]-t[1];
375         if(d==0.0) {
376                 if(bsplinetype==0) {
377                         return 1;       /* beide keys gelijk */
378                 }
379         }
380         else d= (fac-t[1])/d;
381
382         /* interpolatie */
383         
384         set_four_ipo(d, t, k[1]->type);
385
386         if(k[1]->type != k[2]->type) {
387                 set_four_ipo(d, fval, k[2]->type);
388                 
389                 temp= 1.0f-d;
390                 t[0]= temp*t[0]+ d*fval[0];
391                 t[1]= temp*t[1]+ d*fval[1];
392                 t[2]= temp*t[2]+ d*fval[2];
393                 t[3]= temp*t[3]+ d*fval[3];
394         }
395
396         return 0;
397
398 }
399
400 void flerp(int aantal, float *in, float *f0, float *f1, float *f2, float *f3, float *t) 
401 {
402         int a;
403
404         for(a=0; a<aantal; a++) {
405                 in[a]= t[0]*f0[a]+t[1]*f1[a]+t[2]*f2[a]+t[3]*f3[a];
406         }
407 }
408
409 void cp_key(int start, int end, int tot, char *poin, Key *key, KeyBlock *k, int mode)
410 {
411         float ktot = 0.0, kd = 0.0;
412         int elemsize, poinsize = 0, a, *ofsp, ofs[32], flagflo=0;
413         char *k1;
414         char *cp, elemstr[8];
415
416         if(key->from==0) return;
417
418         if( GS(key->from->name)==ID_ME ) {
419                 ofs[0]= sizeof(MVert);
420                 ofs[1]= 0;
421                 poinsize= ofs[0];
422         }
423         else if( GS(key->from->name)==ID_LT ) {
424                 ofs[0]= sizeof(BPoint);
425                 ofs[1]= 0;
426                 poinsize= ofs[0];
427         }
428         else if( GS(key->from->name)==ID_CU ) {
429                 if(mode==KEY_BPOINT) ofs[0]= sizeof(BPoint);
430                 else ofs[0]= sizeof(BezTriple);
431                 
432                 ofs[1]= 0;
433                 poinsize= ofs[0];
434         }
435
436
437         if(end>tot) end= tot;
438         
439         k1= k->data;
440
441         if(tot != k->totelem) {
442                 ktot= 0.0;
443                 flagflo= 1;
444                 if(k->totelem) {
445                         kd= k->totelem/(float)tot;
446                 }
447                 else return;
448         }
449
450         /* deze uitzondering is om slurphing mogelijk te maken */
451         if(start!=0) {
452                 
453                 poin+= poinsize*start;
454                 
455                 if(flagflo) {
456                         ktot+= start*kd;
457                         a= (int)floor(ktot);
458                         if(a) {
459                                 ktot-= a;
460                                 k1+= a*key->elemsize;
461                         }
462                 }
463                 else k1+= start*key->elemsize;
464         }       
465         
466
467         if(mode==KEY_BEZTRIPLE) {
468                 elemstr[0]= 1;
469                 elemstr[1]= IPO_BEZTRIPLE;
470                 elemstr[2]= 0;
471         }
472         
473         /* alleen in dit stuk, hierboven niet! */
474         elemsize= key->elemsize;
475         if(mode==KEY_BEZTRIPLE) elemsize*= 3;
476
477         for(a=start; a<end; a++) {
478                 cp= key->elemstr;
479                 if(mode==KEY_BEZTRIPLE) cp= elemstr;
480
481                 ofsp= ofs;
482                 
483                 while( cp[0] ) {
484                         
485                         switch(cp[1]) {
486                         case IPO_FLOAT:
487                                 
488                                 memcpy(poin, k1, 4*cp[0]);
489                                 poin+= ofsp[0];
490
491                                 break;
492                         case IPO_BPOINT:
493                                 memcpy(poin, k1, 3*4);
494                                 memcpy(poin+16, k1+12, 4);
495                                 
496                                 poin+= ofsp[0];                         
497
498                                 break;
499                         case IPO_BEZTRIPLE:
500                                 memcpy(poin, k1, 4*12);
501                                 poin+= ofsp[0]; 
502
503                                 break;
504                         }
505                         
506                         cp+= 2; ofsp++;
507                 }
508                 
509                 /* gaan we moeilijk doen */
510                 if(flagflo) {
511                         ktot+= kd;
512                         while(ktot>=1.0) {
513                                 ktot-= 1.0;
514                                 k1+= elemsize;
515                         }
516                 }
517                 else k1+= elemsize;
518                 
519                 if(mode==KEY_BEZTRIPLE) a+=2;
520         }
521 }
522
523 void cp_cu_key(Curve *cu, KeyBlock *kb, int start, int end)
524 {
525         Nurb *nu;
526         int a, step = 0, tot, a1, a2;
527         char *poin;
528
529         tot= count_curveverts(&cu->nurb);
530         nu= cu->nurb.first;
531         a= 0;
532         while(nu) {
533                 if(nu->bp) {
534                         
535                         step= nu->pntsu*nu->pntsv;
536                         
537                         /* uitzondering omdat keys graag met volledige blokken werken */
538                         poin= (char *)nu->bp->vec;
539                         poin -= a*sizeof(BPoint);
540                         
541                         a1= MAX2(a, start);
542                         a2= MIN2(a+step, end);
543                         
544                         if(a1<a2) cp_key(a1, a2, tot, poin, cu->key, kb, KEY_BPOINT);
545                 }
546                 else if(nu->bezt) {
547                         
548                         step= 3*nu->pntsu;
549                         
550                         poin= (char *)nu->bezt->vec;
551                         poin -= a*sizeof(BezTriple);
552
553                         a1= MAX2(a, start);
554                         a2= MIN2(a+step, end);
555
556                         if(a1<a2) cp_key(a1, a2, tot, poin, cu->key, kb, KEY_BEZTRIPLE);
557                         
558                 }
559                 a+= step;
560                 nu=nu->next;
561         }
562 }
563
564
565 void rel_flerp(int aantal, float *in, float *ref, float *out, float fac)
566 {
567         int a;
568
569         for(a=0; a<aantal; a++) {
570                 in[a]-= fac*(ref[a]-out[a]);
571         }
572 }
573
574
575
576 void do_rel_key(int start, int end, int tot, char *basispoin, Key *key, float ctime, int mode)
577 {
578         KeyBlock *kb;
579         IpoCurve *icu;
580         float fac[32], fval;
581         int *ofsp, ofs[3], elemsize, a, b;
582         char *cp, *poin, *reffrom, *from, elemstr[8];
583         
584         if(key->from==0) return;
585         if(key->ipo==0) return;
586         
587         if( GS(key->from->name)==ID_ME ) {
588                 ofs[0]= sizeof(MVert);
589                 ofs[1]= 0;
590         }
591         else if( GS(key->from->name)==ID_LT ) {
592                 ofs[0]= sizeof(BPoint);
593                 ofs[1]= 0;
594         }
595         else if( GS(key->from->name)==ID_CU ) {
596                 if(mode==KEY_BPOINT) ofs[0]= sizeof(BPoint);
597                 else ofs[0]= sizeof(BezTriple);
598                 
599                 ofs[1]= 0;
600         }
601         
602         if(end>tot) end= tot;
603         
604         /* in geval beztriple */
605         elemstr[0]= 1;                          /* aantal ipofloats */
606         elemstr[1]= IPO_BEZTRIPLE;
607         elemstr[2]= 0;
608
609         /* alleen in dit stuk, hierboven niet! */
610         elemsize= key->elemsize;
611         if(mode==KEY_BEZTRIPLE) elemsize*= 3;
612
613         /* step one: fetch ipo values */
614         icu= key->ipo->curve.first;
615         for(a=0; a<KEY_TOTIPO; a++) fac[a]= 0.0;
616         while(icu) {
617                 fac[icu->adrcode]= icu->curval;
618                 icu= icu->next;
619         }
620         
621         /* step 2 init */
622         cp_key(start, end, tot, basispoin, key, key->refkey, mode);
623         
624         /* step 3: do it */
625         
626         a= 1;
627         kb= key->block.first;
628         while(kb) {
629                 
630                 if(kb!=key->refkey) {
631                         fval= fac[a];
632                         a++;
633                         if(a==32) break;
634
635                         /* no difference allowed */
636                         if(kb->totelem==tot) {
637                                 
638                                 poin= basispoin;
639                                 reffrom= key->refkey->data;
640                                 from= kb->data;
641                                 
642                                 poin+= start*ofs[0];
643                                 reffrom+= elemsize*start;
644                                 from+= elemsize*start;
645                                 
646                                 for(b=start; b<end; b++) {
647                                 
648                                         cp= key->elemstr;       
649                                         if(mode==KEY_BEZTRIPLE) cp= elemstr;
650                                         
651                                         ofsp= ofs;
652                                         
653                                         while( cp[0] ) {        /* cp[0]==aantal */
654                                                 
655                                                 switch(cp[1]) {
656                                                 case IPO_FLOAT:
657                                                         rel_flerp(cp[0], (float *)poin, (float *)reffrom, (float *)from, fval);
658                                                         
659                                                         break;
660                                                 case IPO_BPOINT:
661                                                         rel_flerp(3, (float *)poin, (float *)reffrom, (float *)from, fval);
662                                                         rel_flerp(1, (float *)(poin+16), (float *)(reffrom+16), (float *)(from+16), fval);
663                         
664                                                         break;
665                                                 case IPO_BEZTRIPLE:
666                                                         rel_flerp(9, (float *)poin, (float *)reffrom, (float *)from, fval);
667                         
668                                                         break;
669                                                 }
670                                                 
671                                                 poin+= ofsp[0];                         
672                                                 
673                                                 cp+= 2;
674                                                 ofsp++;
675                                         }
676                                         
677                                         reffrom+= elemsize;
678                                         from+= elemsize;
679                                         
680                                 }
681                         }
682                 }
683                 kb= kb->next;
684         }
685 }
686
687
688 void do_key(int start, int end, int tot, char *poin, Key *key, KeyBlock **k, float *t, int mode)
689 {
690         float k1tot = 0.0, k2tot = 0.0, k3tot = 0.0, k4tot = 0.0;
691         float k1d = 0.0, k2d = 0.0, k3d = 0.0, k4d = 0.0;
692         int a, ofs[32], *ofsp;
693         int flagdo= 15, flagflo=0, elemsize, poinsize=0;
694         char *k1, *k2, *k3, *k4;
695         char *cp, elemstr[8];;
696
697         if(key->from==0) return;
698
699         if( GS(key->from->name)==ID_ME ) {
700                 ofs[0]= sizeof(MVert);
701                 ofs[1]= 0;
702                 poinsize= ofs[0];
703         }
704         else if( GS(key->from->name)==ID_LT ) {
705                 ofs[0]= sizeof(BPoint);
706                 ofs[1]= 0;
707                 poinsize= ofs[0];
708         }
709         else if( GS(key->from->name)==ID_CU ) {
710                 if(mode==KEY_BPOINT) ofs[0]= sizeof(BPoint);
711                 else ofs[0]= sizeof(BezTriple);
712                 
713                 ofs[1]= 0;
714                 poinsize= ofs[0];
715         }
716         
717         if(end>tot) end= tot;
718
719         k1= k[0]->data;
720         k2= k[1]->data;
721         k3= k[2]->data;
722         k4= k[3]->data;
723
724         /* testen op meer of minder punten (per key!) */
725         if(tot != k[0]->totelem) {
726                 k1tot= 0.0;
727                 flagflo |= 1;
728                 if(k[0]->totelem) {
729                         k1d= k[0]->totelem/(float)tot;
730                 }
731                 else flagdo -= 1;
732         }
733         if(tot != k[1]->totelem) {
734                 k2tot= 0.0;
735                 flagflo |= 2;
736                 if(k[0]->totelem) {
737                         k2d= k[1]->totelem/(float)tot;
738                 }
739                 else flagdo -= 2;
740         }
741         if(tot != k[2]->totelem) {
742                 k3tot= 0.0;
743                 flagflo |= 4;
744                 if(k[0]->totelem) {
745                         k3d= k[2]->totelem/(float)tot;
746                 }
747                 else flagdo -= 4;
748         }
749         if(tot != k[3]->totelem) {
750                 k4tot= 0.0;
751                 flagflo |= 8;
752                 if(k[0]->totelem) {
753                         k4d= k[3]->totelem/(float)tot;
754                 }
755                 else flagdo -= 8;
756         }
757
758                 /* deze uitzondering is om slurphing mogelijk te maken */
759         if(start!=0) {
760
761                 poin+= poinsize*start;
762                 
763                 if(flagdo & 1) {
764                         if(flagflo & 1) {
765                                 k1tot+= start*k1d;
766                                 a= (int)floor(k1tot);
767                                 if(a) {
768                                         k1tot-= a;
769                                         k1+= a*key->elemsize;
770                                 }
771                         }
772                         else k1+= start*key->elemsize;
773                 }
774                 if(flagdo & 2) {
775                         if(flagflo & 2) {
776                                 k2tot+= start*k2d;
777                                 a= (int)floor(k2tot);
778                                 if(a) {
779                                         k2tot-= a;
780                                         k2+= a*key->elemsize;
781                                 }
782                         }
783                         else k2+= start*key->elemsize;
784                 }
785                 if(flagdo & 4) {
786                         if(flagflo & 4) {
787                                 k3tot+= start*k3d;
788                                 a= (int)floor(k3tot);
789                                 if(a) {
790                                         k3tot-= a;
791                                         k3+= a*key->elemsize;
792                                 }
793                         }
794                         else k3+= start*key->elemsize;
795                 }
796                 if(flagdo & 8) {
797                         if(flagflo & 8) {
798                                 k4tot+= start*k4d;
799                                 a= (int)floor(k4tot);
800                                 if(a) {
801                                         k4tot-= a;
802                                         k4+= a*key->elemsize;
803                                 }
804                         }
805                         else k4+= start*key->elemsize;
806                 }
807
808         }
809
810         /* in geval beztriple */
811         elemstr[0]= 1;                          /* aantal ipofloats */
812         elemstr[1]= IPO_BEZTRIPLE;
813         elemstr[2]= 0;
814
815         /* alleen in dit stuk, hierboven niet! */
816         elemsize= key->elemsize;
817         if(mode==KEY_BEZTRIPLE) elemsize*= 3;
818
819         for(a=start; a<end; a++) {
820         
821                 cp= key->elemstr;       
822                 if(mode==KEY_BEZTRIPLE) cp= elemstr;
823                 
824                 ofsp= ofs;
825                 
826                 while( cp[0] ) {        /* cp[0]==aantal */
827                         
828                         switch(cp[1]) {
829                         case IPO_FLOAT:
830                                 flerp(cp[0], (float *)poin, (float *)k1, (float *)k2, (float *)k3, (float *)k4, t);
831                                 poin+= ofsp[0];                         
832
833                                 break;
834                         case IPO_BPOINT:
835                                 flerp(3, (float *)poin, (float *)k1, (float *)k2, (float *)k3, (float *)k4, t);
836                                 flerp(1, (float *)(poin+16), (float *)(k1+12), (float *)(k2+12), (float *)(k3+12), (float *)(k4+12), t);
837                                 
838                                 poin+= ofsp[0];                         
839
840                                 break;
841                         case IPO_BEZTRIPLE:
842                                 flerp(9, (void *)poin, (void *)k1, (void *)k2, (void *)k3, (void *)k4, t);
843                                 poin+= ofsp[0];                         
844
845                                 break;
846                         }
847                         
848
849                         cp+= 2;
850                         ofsp++;
851                 }
852                 /* gaan we moeilijk doen: als keys van lengte verschillen */
853                 if(flagdo & 1) {
854                         if(flagflo & 1) {
855                                 k1tot+= k1d;
856                                 while(k1tot>=1.0) {
857                                         k1tot-= 1.0;
858                                         k1+= elemsize;
859                                 }
860                         }
861                         else k1+= elemsize;
862                 }
863                 if(flagdo & 2) {
864                         if(flagflo & 2) {
865                                 k2tot+= k2d;
866                                 while(k2tot>=1.0) {
867                                         k2tot-= 1.0;
868                                         k2+= elemsize;
869                                 }
870                         }
871                         else k2+= elemsize;
872                 }
873                 if(flagdo & 4) {
874                         if(flagflo & 4) {
875                                 k3tot+= k3d;
876                                 while(k3tot>=1.0) {
877                                         k3tot-= 1.0;
878                                         k3+= elemsize;
879                                 }
880                         }
881                         else k3+= elemsize;
882                 }
883                 if(flagdo & 8) {
884                         if(flagflo & 8) {
885                                 k4tot+= k4d;
886                                 while(k4tot>=1.0) {
887                                         k4tot-= 1.0;
888                                         k4+= elemsize;
889                                 }
890                         }
891                         else k4+= elemsize;
892                 }
893                 
894                 if(mode==KEY_BEZTRIPLE) a+= 2;
895         }
896 }
897
898
899
900 void do_mesh_key(Mesh *me)
901 {
902         KeyBlock *k[4];
903         float cfra, ctime, t[4], delta, loc[3], size[3];
904         int a, flag = 0, step;
905         
906         if(me->totvert==0) return;
907         if(me->key==0) return;
908         if(me->key->block.first==0) return;
909         
910         if(me->key->slurph && me->key->type!=KEY_RELATIVE ) {
911                 delta= me->key->slurph;
912                 delta/= me->totvert;
913                 
914                 step= 1;
915                 if(me->totvert>100 && slurph_opt) {
916                         step= me->totvert/50;
917                         delta*= step;
918                         /* in do_key en cp_key wordt a>tot afgevangen */
919                 }
920                 
921                 cfra= G.scene->r.cfra;
922                 
923                 for(a=0; a<me->totvert; a+=step, cfra+= delta) {
924                         
925                         ctime= bsystem_time(0, 0, cfra, 0.0);
926                         if(calc_ipo_spec(me->key->ipo, KEY_SPEED, &ctime)==0) {
927                                 ctime /= 100.0;
928                                 CLAMP(ctime, 0.0, 1.0);
929                         }
930                 
931                         flag= setkeys(ctime, &me->key->block, k, t, 0);
932                         if(flag==0) {
933                                 
934                                 do_key(a, a+step, me->totvert, (char *)me->mvert->co, me->key, k, t, 0);
935                         }
936                         else {
937                                 cp_key(a, a+step, me->totvert, (char *)me->mvert->co, me->key, k[2], 0);
938                         }
939                 }
940                 
941                 if(flag && k[2]==me->key->refkey) tex_space_mesh(me);
942                 else boundbox_mesh(me, loc, size);
943                 
944         }
945         else {
946                 
947                 ctime= bsystem_time(0, 0, (float)G.scene->r.cfra, 0.0);
948                 if(calc_ipo_spec(me->key->ipo, KEY_SPEED, &ctime)==0) {
949                         ctime /= 100.0;
950                         CLAMP(ctime, 0.0, 1.0);
951                 }
952                 
953                 if(me->key->type==KEY_RELATIVE) {
954                         do_rel_key(0, me->totvert, me->totvert, (char *)me->mvert->co, me->key, ctime, 0);
955                 }
956                 else {
957                         flag= setkeys(ctime, &me->key->block, k, t, 0);
958                         if(flag==0) {
959                                 
960                                 do_key(0, me->totvert, me->totvert, (char *)me->mvert->co, me->key, k, t, 0);
961                         }
962                         else {
963                                 cp_key(0, me->totvert, me->totvert, (char *)me->mvert->co, me->key, k[2], 0);
964         
965                         }
966                         
967                         if(flag && k[2]==me->key->refkey) tex_space_mesh(me);
968                         else boundbox_mesh(me, loc, size);
969                 }
970         }
971 }
972
973 void do_cu_key(Curve *cu, KeyBlock **k, float *t)
974 {
975         Nurb *nu;
976         int a, step = 0, tot;
977         char *poin;
978         
979         tot= count_curveverts(&cu->nurb);
980         nu= cu->nurb.first;
981         a= 0;
982         while(nu) {
983                 if(nu->bp) {
984                         
985                         step= nu->pntsu*nu->pntsv;
986                         
987                         /* uitzondering omdat keys graag met volledige blokken werken */
988                         poin= (char *)nu->bp->vec;
989                         poin -= a*sizeof(BPoint);
990                         
991                         do_key(a, a+step, tot, poin, cu->key, k, t, KEY_BPOINT);
992                 }
993                 else if(nu->bezt) {
994                         
995                         step= 3*nu->pntsu;
996                         
997                         poin= (char *)nu->bezt->vec;
998                         poin -= a*sizeof(BezTriple);
999
1000                         do_key(a, a+step, tot, poin, cu->key, k, t, KEY_BEZTRIPLE);
1001                         
1002                 }
1003                 a+= step;
1004                 nu=nu->next;
1005         }
1006 }
1007
1008 void do_rel_cu_key(Curve *cu, float ctime)
1009 {
1010         Nurb *nu;
1011         int a, step = 0, tot;
1012         char *poin;
1013         
1014         tot= count_curveverts(&cu->nurb);
1015         nu= cu->nurb.first;
1016         a= 0;
1017         while(nu) {
1018                 if(nu->bp) {
1019                         
1020                         step= nu->pntsu*nu->pntsv;
1021                         
1022                         /* uitzondering omdat keys graag met volledige blokken werken */
1023                         poin= (char *)nu->bp->vec;
1024                         poin -= a*sizeof(BPoint);
1025                         
1026                         do_rel_key(a, a+step, tot, poin, cu->key, ctime, KEY_BPOINT);
1027                 }
1028                 else if(nu->bezt) {
1029                         
1030                         step= 3*nu->pntsu;
1031                         
1032                         poin= (char *)nu->bezt->vec;
1033                         poin -= a*sizeof(BezTriple);
1034
1035                         do_rel_key(a, a+step, tot, poin, cu->key, ctime, KEY_BEZTRIPLE);
1036                         
1037                 }
1038                 a+= step;
1039                 nu=nu->next;
1040         }
1041 }
1042
1043 void do_curve_key(Curve *cu)
1044 {
1045         KeyBlock *k[4];
1046         float cfra, ctime, t[4], delta;
1047         int a, flag = 0, step = 0, tot;
1048         
1049         tot= count_curveverts(&cu->nurb);
1050         
1051         if(tot==0) return;
1052         if(cu->key==0) return;
1053         if(cu->key->block.first==0) return;
1054         
1055         if(cu->key->slurph) {
1056                 delta= cu->key->slurph;
1057                 delta/= tot;
1058                 
1059                 step= 1;
1060                 if(tot>100 && slurph_opt) {
1061                         step= tot/50;
1062                         delta*= step;
1063                         /* in do_key en cp_key wordt a>tot afgevangen */
1064                 }
1065                 
1066                 cfra= G.scene->r.cfra;
1067                 
1068                 for(a=0; a<tot; a+=step, cfra+= delta) {
1069                         
1070                         ctime= bsystem_time(0, 0, cfra, 0.0);
1071                         if(calc_ipo_spec(cu->key->ipo, KEY_SPEED, &ctime)==0) {
1072                                 ctime /= 100.0;
1073                                 CLAMP(ctime, 0.0, 1.0);
1074                         }
1075                 
1076                         flag= setkeys(ctime, &cu->key->block, k, t, 0);
1077                         if(flag==0) {
1078                                 
1079                                 /* do_key(a, a+step, tot, (char *)cu->mvert->co, cu->key, k, t, 0); */
1080                         }
1081                         else {
1082                                 /* cp_key(a, a+step, tot, (char *)cu->mvert->co, cu->key, k[2],0); */
1083                         }
1084                 }
1085
1086                 if(flag && k[2]==cu->key->refkey) tex_space_curve(cu);
1087                 
1088                 
1089         }
1090         else {
1091                 
1092                 ctime= bsystem_time(0, 0, (float)G.scene->r.cfra, 0.0);
1093                 if(calc_ipo_spec(cu->key->ipo, KEY_SPEED, &ctime)==0) {
1094                         ctime /= 100.0;
1095                         CLAMP(ctime, 0.0, 1.0);
1096                 }
1097                 
1098                 if(cu->key->type==KEY_RELATIVE) {
1099                         do_rel_cu_key(cu, ctime);
1100                 }
1101                 else {
1102                         flag= setkeys(ctime, &cu->key->block, k, t, 0);
1103                         
1104                         if(flag==0) do_cu_key(cu, k, t);
1105                         else cp_cu_key(cu, k[2], 0, tot);
1106                                         
1107                         if(flag && k[2]==cu->key->refkey) tex_space_curve(cu);
1108                 }
1109         }
1110 }
1111
1112 void do_latt_key(Lattice *lt)
1113 {
1114         KeyBlock *k[4];
1115         float delta, cfra, ctime, t[4];
1116         int a, tot, flag;
1117         
1118         if(lt->key==0) return;
1119         if(lt->key->block.first==0) return;
1120         
1121         tot= lt->pntsu*lt->pntsv*lt->pntsw;
1122
1123         if(lt->key->slurph) {
1124                 delta= lt->key->slurph;
1125                 delta/= (float)tot;
1126                 
1127                 cfra= G.scene->r.cfra;
1128                 
1129                 for(a=0; a<tot; a++, cfra+= delta) {
1130                         
1131                         ctime= bsystem_time(0, 0, cfra, 0.0);
1132                         if(calc_ipo_spec(lt->key->ipo, KEY_SPEED, &ctime)==0) {
1133                                 ctime /= 100.0;
1134                                 CLAMP(ctime, 0.0, 1.0);
1135                         }
1136                 
1137                         flag= setkeys(ctime, &lt->key->block, k, t, 0);
1138                         if(flag==0) {
1139                                 
1140                                 do_key(a, a+1, tot, (char *)lt->def->vec, lt->key, k, t, 0);
1141                         }
1142                         else {
1143                                 cp_key(a, a+1, tot, (char *)lt->def->vec, lt->key, k[2], 0);
1144                         }
1145                 }               
1146         }
1147         else {
1148                 ctime= bsystem_time(0, 0, (float)G.scene->r.cfra, 0.0);
1149                 if(calc_ipo_spec(lt->key->ipo, KEY_SPEED, &ctime)==0) {
1150                         ctime /= 100.0;
1151                         CLAMP(ctime, 0.0, 1.0);
1152                 }
1153         
1154                 if(lt->key->type==KEY_RELATIVE) {
1155                         do_rel_key(0, tot, tot, (char *)lt->def->vec, lt->key, ctime, 0);
1156                 }
1157                 else {
1158
1159                         flag= setkeys(ctime, &lt->key->block, k, t, 0);
1160                         if(flag==0) {
1161                                 do_key(0, tot, tot, (char *)lt->def->vec, lt->key, k, t, 0);
1162                         }
1163                         else {
1164                                 cp_key(0, tot, tot, (char *)lt->def->vec, lt->key, k[2], 0);
1165                         }
1166                 }
1167         }
1168         
1169         if(lt->flag & LT_OUTSIDE) outside_lattice(lt);
1170 }
1171
1172
1173
1174 void do_all_keys()
1175 {
1176         Key *key;
1177         int idcode;
1178         
1179         key= G.main->key.first;
1180         while(key) {
1181                 if(key->from) {
1182                         idcode= GS(key->from->name);
1183                 
1184                         if(idcode==ID_ME) do_mesh_key( (Mesh *)key->from);
1185                         else if(idcode==ID_CU) do_curve_key( (Curve *)key->from);
1186                         else if(idcode==ID_LT) do_latt_key( (Lattice *)key->from);
1187                 }
1188                 key= key->id.next;
1189         }
1190 }
1191
1192 void do_ob_key(Object *ob)
1193 {
1194         if(ob->type==OB_MESH) do_mesh_key( ob->data);
1195         else if(ob->type==OB_CURVE) do_curve_key( ob->data);
1196         else if(ob->type==OB_SURF) do_curve_key( ob->data);
1197         else if(ob->type==OB_LATTICE) do_latt_key( ob->data);
1198 }
1199
1200 void do_spec_key(Key *key)
1201 {
1202         int idcode;
1203         
1204         if(key==0) return;
1205         
1206         idcode= GS(key->from->name);
1207         
1208         if(idcode==ID_ME) do_mesh_key( (Mesh *)key->from);
1209         else if(idcode==ID_CU) do_curve_key( (Curve *)key->from);
1210         else if(idcode==ID_LT) do_latt_key( (Lattice *)key->from);
1211         
1212 }