#include "stdio.h"
#include <stdlib.h> 
#include<string.h> 
#define TIMERATIO 10 
#define CARMOVETIME 20000 
 const float secondToMssecond= 1000.000; 
 struct vectorStruct  
 { 
         float x; 
         float y; 
         float z; 
 };
 struct TCB
 {
         float t;
         float c;
         float b;
 };
 struct shiftInfo
 {
         float time;
     struct vectorStruct vertex;
     struct TCB value;
 };
 struct rollRange
 {
         int axis;
         float angle;
 };
 struct rollInfo
 {
         float time;
     struct rollRange temp;
     struct TCB value;
 };
 int
 getRollTimeValue(float time,rollInfo*rollArr,int &rollTime0,int& rollTime1,int& rollTime2,int initialRollNum)
 {
         time = time/secondToMssecond;
         for (int i=1;i<initialRollNum-1;i++)
         {
                  
                 if ((time>rollArr[i].time)&&(time<rollArr[i+1].time)) 
                 { 
                         rollTime0 = i-1; 
                         rollTime1 = i; 
                         rollTime2 = i+1; 
                         break; 
  
                 } 
                 if (time==rollArr[i].time) 
                 { 
                         rollTime0 = i; 
                         rollTime1 = i; 
                         rollTime2 = i; 
                         break; 
                 } 
                 if (time==rollArr[i+1].time) 
                 { 
                         rollTime0 = i+1; 
                         rollTime1 = i+1; 
                         rollTime2 = i+1; 
                         break; 
                 } 
         } 
         return 0; 
 } 
  
 int  
 getShiftTimeValue(float time,shiftInfo*shiftArr,int &shiftTime0,int& shiftTime1,int& shiftTime2,int initialShiftNum) 
 { 
         time = time/secondToMssecond; 
         for (int i=1;i<initialShiftNum-1;i++) 
         { 
                 if ((time>shiftArr[i].time)&&(time<shiftArr[i+1].time)) 
                 { 
                         shiftTime0 = i-1; 
                         shiftTime1 = i; 
                         shiftTime2 = i+1; 
                         break; 
                 } 
                 if (time==shiftArr[i].time) 
                 { 
                         shiftTime0 = i; 
                         shiftTime1 = i; 
                         shiftTime2 = i; 
                         break; 
                 } 
                 if (time==shiftArr[i+1].time) 
                 { 
                         shiftTime0 = i+1; 
                         shiftTime1 = i+1; 
                         shiftTime2 = i+1; 
                         break; 
                 } 
         } 
         return 0; 
 } 
  
 rollRange  
 getRollInterpolationValue(float time,rollInfo*rollArr,int rollTime0,int rollTime1,int rollTime2) 
 { 
     time = time/secondToMssecond; 
         rollRange p;  
         if (rollTime0==rollTime1) 
         { 
                 p.angle = rollArr[rollTime0].temp.angle; 
                 p.axis = rollArr[rollTime0].temp.axis; 
                 return p; 
         } 
         rollInfo p0,p1,p2; 
         p0 = *(rollArr+rollTime0); 
         p1 = *(rollArr+rollTime1); 
         p2 = *(rollArr+rollTime2); 
  
         float t1 = p1.value.t; 
         float b1 = p1.value.b; 
         float c1 = p1.value.c; 
  
         float t2 = p2.value.t; 
         float b2 = p2.value.b; 
         float c2 = p2.value.c; 
  
         rollRange TSi; 
         TSi.angle= (((1-t1)*(1-c1)*(1+b1))/2.0)*(p0.temp.angle-p1.temp.angle)+(((1-t1)*(1-c1)*(1+b1))/2.0)*(p2.temp.angle-p1.temp.angle); 
  
         rollRange TDi; 
         TDi.angle= (((1-t2)*(1-c2)*(1+b2))/2.0)*(p0.temp.angle-p1.temp.angle)+(((1-t2)*(1-c2)*(1+b2))/2.0)*(p2.temp.angle-p1.temp.angle); 
  
         float s = (rollArr[rollTime1].time-time)/(rollArr[rollTime1].time-rollArr[rollTime2].time); 
         float h1 = 2*s*s*s-3*s*s+1; 
         float h2 = -2*s*s*s+3*s*s; 
         float h3 = s*s*s-2*s*s+s; 
         float h4 = s*s*s-s*s; 
  
         p.angle = h1*p1.temp.angle+h2*p2.temp.angle+h3*TSi.angle+h4*TDi.angle; 
         p.axis = p1.temp.axis; 
         return p; 
 } 
 vectorStruct 
 getShiftInterpolationValue(float time,shiftInfo*shiftArr,int shiftTime0,int shiftTime1,int shiftTime2) 
 { 
  
     time = time/secondToMssecond; 
         vectorStruct p; 
     if (shiftTime0==shiftTime1) 
     { 
                 p = shiftArr[shiftTime0].vertex; 
                 return p; 
     } 
         shiftInfo p0,p1,p2; 
         p0 = *(shiftArr+shiftTime0); 
         p1 = *(shiftArr+shiftTime1); 
         p2 = *(shiftArr+shiftTime2); 
  
         float t1 = p1.value.t; 
         float b1 = p1.value.b; 
         float c1 = p1.value.c; 
  
         float t2 = p2.value.t; 
         float b2 = p2.value.b; 
         float c2 = p2.value.c; 
  
         vectorStruct TSi; 
         TSi.x= (((1-t1)*(1-c1)*(1+b1))/2.0)*(p0.vertex.x-p1.vertex.x)+(((1-t1)*(1-c1)*(1+b1))/2.0)*(p2.vertex.x-p1.vertex.x); 
         TSi.y= (((1-t1)*(1-c1)*(1+b1))/2.0)*(p0.vertex.y-p1.vertex.y)+(((1-t1)*(1-c1)*(1+b1))/2.0)*(p2.vertex.y-p1.vertex.y); 
         TSi.z= (((1-t1)*(1-c1)*(1+b1))/2.0)*(p0.vertex.z-p1.vertex.z)+(((1-t1)*(1-c1)*(1+b1))/2.0)*(p2.vertex.z-p1.vertex.z); 
         vectorStruct TDi; 
         TDi.x= (((1-t2)*(1-c2)*(1+b2))/2.0)*(p0.vertex.x-p1.vertex.x)+(((1-t2)*(1-c2)*(1+b2))/2.0)*(p2.vertex.x-p1.vertex.x); 
         TDi.y= (((1-t2)*(1-c2)*(1+b2))/2.0)*(p0.vertex.y-p1.vertex.y)+(((1-t2)*(1-c2)*(1+b2))/2.0)*(p2.vertex.y-p1.vertex.y); 
         TDi.z= (((1-t2)*(1-c2)*(1+b2))/2.0)*(p0.vertex.z-p1.vertex.z)+(((1-t2)*(1-c2)*(1+b2))/2.0)*(p2.vertex.z-p1.vertex.z); 
  
         float s = (shiftArr[shiftTime1].time-time)/(shiftArr[shiftTime1].time-shiftArr[shiftTime2].time); 
         float h1 = 2*s*s*s-3*s*s+1; 
         float h2 = 3*s*s-2*s*s*s; 
         float h3 = s*s*s-2*s*s+s; 
         float h4 = s*s*s-s*s; 
  
          
         p.x= h1*p1.vertex.x+h2*p2.vertex.x+h3*TSi.x+h4*TDi.x; 
         p.y= h1*p1.vertex.y+h2*p2.vertex.y+h3*TSi.y+h4*TDi.y; 
         p.z= h1*p1.vertex.z+h2*p2.vertex.z+h3*TSi.z+h4*TDi.z; 
         return p; 
 } 
  
 int  
 getOrbitCar(int interpolatTimeInterval,rollInfo*rollArr,shiftInfo*shiftArr, 
                         rollRange* rollOffsetArr,vectorStruct*shiftOffsetArr,int initialRollNum,int initialShiftNum) 
 { 
         for(int t=0;t<interpolatTimeInterval;t++) 
         { 
                 int  shiftTime0,shiftTime1,shiftTime2; 
                 int  rollTime0,rollTime1,rollTime2; 
  
                 getRollTimeValue((float)t*TIMERATIO,rollArr,rollTime0,rollTime1,rollTime2,initialRollNum); 
  
                 getShiftTimeValue((float)t*TIMERATIO,shiftArr,shiftTime0,shiftTime1,shiftTime2,initialShiftNum); 
  
                 *(rollOffsetArr+t)= getRollInterpolationValue((float)t*TIMERATIO,rollArr,rollTime0,rollTime1,rollTime2); 
  
                 *(shiftOffsetArr+t)= getShiftInterpolationValue((float)t*TIMERATIO,shiftArr,shiftTime0,shiftTime1,shiftTime2); 
  
         } 
         return 0; 
 } 
 int 
 main() 
 { 
  
         int initialRollNum = 0; 
         int initialShiftNum = 0; 
         FILE *fp; 
         char path[100]=".\\\\"; 
         strcat(path,"car roll.txt"); 
         if(((fp = fopen(path,"r"))==NULL)) 
         { 
                 printf("can not open car roll.txt\n"); 
         } 
         fscanf(fp,"%d",&initialRollNum); 
         rollInfo * rollArr = (rollInfo*)malloc(sizeof(rollInfo)*initialRollNum); 
         for (int i=0;i<initialRollNum;i++) 
         { 
                 int x=0,y=0,z=0; 
                 fscanf(fp,"%f%d%d%d%f%f%f%f",&(rollArr[i].time),&x,&y,&z,&(rollArr[i].temp.angle), 
                         &(rollArr[i].value.t),&(rollArr[i].value.b),&(rollArr[i].value.c)); 
                 rollArr[i].temp.axis = x+y*2+z*4;  
         } 
         fclose(fp); 
         memset(path,NULL,100); 
         strcat(path,".\\\\"); 
     strcat(path,"car shift.txt"); 
         if ((fp=fopen(path,"r"))==NULL) 
         { 
                 printf("can not open car shift.txt","r"); 
         } 
         fscanf(fp,"%d",&initialShiftNum); 
         shiftInfo * shiftArr = (shiftInfo*)malloc(sizeof(shiftInfo)*initialShiftNum); 
         for (int j=0;j<initialShiftNum;j++) 
         { 
                 fscanf(fp,"%f%f%f%f%f%f%f",&(shiftArr[j].time),&(shiftArr[j].vertex.x),&(shiftArr[j].vertex.y), 
                         &(shiftArr[j].vertex.z),&(shiftArr[j].value.t),&(shiftArr[j].value.b),&(shiftArr[j].value.c)); 
         } 
         fclose(fp); 
  
         int interpolatTimeInterval = CARMOVETIME/TIMERATIO; 
         rollRange *rollOffsetArr = (rollRange*)malloc(sizeof(rollRange)*interpolatTimeInterval); 
         vectorStruct *shiftOffsetArr = (vectorStruct*)malloc(sizeof(vectorStruct)*interpolatTimeInterval); 
         getOrbitCar(interpolatTimeInterval,rollArr,shiftArr,rollOffsetArr,shiftOffsetArr,initialRollNum,initialShiftNum); 
  
         free(rollArr); 
         rollArr = NULL; 
         free(shiftArr); 
         free(rollOffsetArr); 
         rollOffsetArr = NULL; 
         free(shiftOffsetArr); 
         shiftOffsetArr = NULL; 
         return 0; 
 } 

说明: 主要是“运动场景中的TCB样条插值研究”。

      要求为:运动场景中使用插值法来描述动画数据。在一段连续的时间段之内,非关键帧的场景几何数据,是通过非线性插值所获得的。以TCB插值方式为主,研究在时间序列中,通过关键帧数据获得插值数据的方法。要求编制相应的程序,进行计算和验证。