Animation_transformation.cpp
Go to the documentation of this file.00001
00002 #include <Animation_transformation.h>
00003
00004 #define PI 3.14159
00005
00006 Animation_transformation::Animation_transformation()
00007 {
00008 angle_x.resize(2);
00009 angle_y.resize(2);
00010 angle_z.resize(2);
00011 time.resize(2);
00012 translation.resize(2*3);
00013 anim_matrix.resize(2*16);
00014
00015 time[0]=0;
00016 angle_x[0]=0;angle_y[0]=0;angle_z[0]=0;
00017 for(int count=0;count<3;count++)
00018 translation[count]=0.0;
00019 for(int count_1=0;count_1<4;count_1++)
00020 for(int count_2=0;count_2<4;count_2++)
00021 {
00022 if(count_1!=count_2)
00023 anim_matrix[4*count_1+count_2]=0.0;
00024 else
00025 anim_matrix[4*count_1+count_2]=1.0;
00026 }
00027
00028 time[1]=200;
00029 angle_x[1]=0;angle_y[1]=0;angle_z[1]=0;
00030 for(int count=0;count<3;count++)
00031 translation[count+3]=0.0;
00032 for(int count_1=0;count_1<4;count_1++)
00033 for(int count_2=0;count_2<4;count_2++)
00034 {
00035 if(count_1!=count_2)
00036 anim_matrix[4*count_1+count_2+16]=0.0;
00037 else
00038 anim_matrix[4*count_1+count_2+16]=1.0;
00039 }
00040
00041
00042 bone_number=-1;
00043 }
00044
00045 Animation_transformation::~Animation_transformation()
00046 {
00047 }
00048
00049
00050 int Animation_transformation::set_size_time(int size)
00051 {
00052 int ok=0;
00053 time.resize(size);
00054 for(int k=0;k<size;k++)
00055 time[k]=0;
00056 return ok;
00057 }
00058
00059 int Animation_transformation::set_size_angle(int size)
00060 {
00061 int ok=0;
00062 angle_x.resize(size);
00063 angle_y.resize(size);
00064 angle_z.resize(size);
00065 translation.resize(3*size);
00066 anim_matrix.resize(16*size);
00067 for(int k=0;k<size;k++)
00068 {
00069 angle_x[k]=0;
00070 angle_y[k]=0;
00071 angle_z[k]=0;
00072 }
00073 for(int k=0;k<(int)translation.size();k++)
00074 translation[k]=0;
00075 for(int k=0;k<(int)anim_matrix.size()/16;k++)
00076 for(int count_1=0;count_1<4;count_1++)
00077 for(int count_2=0;count_2<4;count_2++)
00078 {
00079 if(count_1!=count_2)
00080 anim_matrix[4*count_1+count_2+16*k]=0.0;
00081 else
00082 anim_matrix[4*count_1+count_2+16*k]=1.0;
00083 }
00084
00085
00086
00087 return ok;
00088 }
00089
00090
00091
00092 int Animation_transformation::set_time(int k_time,double val)
00093 {
00094 int ok=0;
00095 if((unsigned int)k_time>=time.size())
00096 {
00097 cout<<"ERROR time excced the capacity"<<endl;
00098 exit(-1);
00099 }
00100 time[k_time]=val;
00101 return ok;
00102 }
00103 int Animation_transformation::set_angle(int k_time,int k_axis,double val)
00104 {
00105 int ok=0;
00106 if((unsigned int)k_time>=time.size())
00107 {
00108 cout<<"ERROR time exceed the capacity"<<endl;
00109 exit(-1);
00110 }
00111 if(k_axis==0)
00112 angle_x[k_time]=val/180.0*PI;
00113 if(k_axis==1)
00114 angle_y[k_time]=val/180.0*PI;
00115 if(k_axis==2)
00116 angle_z[k_time]=val/180.0*PI;
00117
00118 return ok;
00119 }
00120
00121 int Animation_transformation::set_translation(int k_time,double tx,double ty,double tz)
00122 {
00123 int ok=0;
00124 if((unsigned int)k_time>=time.size() || (unsigned int)(3*k_time+2)>=translation.size())
00125 {
00126 cout<<"ERROR time exceed the capacity in set_translation"<<endl;
00127 exit(-1);
00128 }
00129 translation[3*k_time+0] = tx;
00130 translation[3*k_time+1] = ty;
00131 translation[3*k_time+2] = tz;
00132
00133 return ok;
00134 }
00135
00136 int Animation_transformation::set_anim_matrix(int k_time,int k_dim,double value)
00137 {
00138
00139
00140 int ok=0;
00141 if((unsigned int)k_time>=time.size() || (unsigned int)(16*k_time+15)>=anim_matrix.size())
00142 {
00143 cout<<"ERROR time exceed the capacity in set_anim_matrix"<<endl;
00144 exit(-1);
00145 }
00146 anim_matrix[16*k_time+k_dim] = value;
00147
00148
00149
00150 return ok;
00151 }
00152
00153
00154 double Animation_transformation::get_angle(int k_time,int k_axis)
00155 {
00156 if((unsigned int)k_time>=time.size())
00157 {
00158 cout<<"ERROR time exceed the capacity in get_angle"<<endl;
00159 exit(-1);
00160 }
00161 if(k_axis==0)
00162 return angle_x[k_time]*180.0/PI;
00163 if(k_axis==1)
00164 return angle_y[k_time]*180.0/PI;
00165 if(k_axis==2)
00166 return angle_z[k_time]*180.0/PI;
00167
00168 else
00169 cout<<"ERROR k axis"<<endl;
00170 exit(-1);
00171
00172 }
00173
00174 Matrix Animation_transformation::get_rotation(double t)
00175 {
00176
00177
00178 Matrix R;
00179 Matrix temp_rot;
00180
00181 if(time.size()>20000)
00182 {printf("Something weird in time size for animation time_size is big ... init ?\n");exit(-1);}
00183
00184 int k=0;
00185 if(t>=time[time.size()-1])
00186 {
00187 printf("error time t exceed time dimension in Animation_transformation t=%f, tmax=%f (t[%d])\n",t,time[time.size()-1],time.size()-1);
00188 exit(-1);
00189 }
00190
00191 if(t<=0)
00192 k=0;
00193 else
00194 {
00195 if(t>=time[time.size()-1])
00196 k=time.size();
00197 else
00198 {
00199 while(!(time[k]<=t && time[k+1]>t))
00200 k++;
00201 }
00202 }
00203
00204
00205
00206
00207 double u = (t-time[k])/(time[k+1]-time[k]);
00208
00209
00210
00211 double ut1=1-u;
00212 double ut2=1-ut1;
00213
00214
00215 temp_rot.set_vector_rotation(1,0,0,angle_x[k]*ut1+angle_x[k+1]*ut2);
00216 R = R*temp_rot;
00217 temp_rot.set_vector_rotation(0,1,0,angle_y[k]*ut1+angle_y[k+1]*ut2);
00218 R = R*temp_rot;
00219 temp_rot.set_vector_rotation(0,0,1,angle_z[k]*ut1+angle_z[k+1]*ut2);
00220 R = R*temp_rot;
00221
00222
00223
00224 return R;
00225 }
00226
00227
00228 Matrix Animation_transformation::get_matrix(double t)
00229 {
00230
00231
00232 Matrix M;
00233
00234
00235
00236 if(time.size()>2000)
00237 {printf("Something weird in time size for animation ... init ?\n");exit(-1);}
00238
00239 int k=0;
00240 if(t>=time[time.size()-1])
00241 {
00242 printf("error time t exceed time dimension in Animation_transformation t=%f, tmax=%f (t[%d])\n",t,time[time.size()-1],time.size()-1);
00243 exit(-1);
00244 }
00245
00246 if(t<=time[0])
00247 k=0;
00248 else
00249 {
00250 if(t>=time[time.size()-1])
00251 k=time.size();
00252 else
00253 {
00254 while(!(time[k]<=t && time[k+1]>t))
00255 {
00256 k++;
00257 if(k>=(int)time.size()-1)
00258 {printf("something wrong in animation, k=size_time ... for time %f\n",t);exit(-1);}
00259 }
00260 }
00261 }
00262
00263
00264
00265 double u = (t-time[k])/(time[k+1]-time[k]);
00266
00267
00268
00269 double ut1=1-u;
00270 double ut2=1-ut1;
00271
00272
00273 Matrix M1;
00274 Matrix M2;
00275
00276
00277
00278 int k1=0,k2=0;
00279 for(k1=0;k1<4;k1++)
00280 for(k2=0;k2<4;k2++)
00281 {
00282 M1.set_value(anim_matrix[16*k+4*k1+k2],k2,k1);
00283 M2.set_value(anim_matrix[16*(k+1)+4*k1+k2],k2,k1);
00284 }
00285
00286 M = M1*ut1+M2*ut2;
00287
00288
00289
00290
00291 return M;
00292 }
00293
00294
00295 Matrix Animation_transformation::get_translation(double t)
00296 {
00297
00298
00299 Matrix T;
00300
00301
00302 int k=0;
00303 if(t>=time[time.size()-1])
00304 {
00305 printf("error time t exceed time dimension in Animation_transformation t=%f, tmax=%f (t[%d])\n",t,time[time.size()-1],time.size()-1);
00306 exit(-1);
00307 }
00308
00309 if(t<=time[0])
00310 k=0;
00311 else
00312 {
00313 if(t>=time[time.size()-1])
00314 k=time.size();
00315 else
00316 {
00317 while(!(time[k]<=t && time[k+1]>t))
00318 {
00319 k++;
00320 if(k>=(int)time.size()-1)
00321 {printf("something wrong in animation, k=size_time ... for time %f\n",t);exit(-1);}
00322 }
00323 }
00324 }
00325
00326 double u = (t-time[k])/(time[k+1]-time[k]);
00327
00328
00329 if(3*(k+1)+2>(int)translation.size())
00330 {printf("Error index too large for translation vector in Animation_transformation [%d] / [%d]\n",3*(k+1)+2,translation.size());exit(-1);}
00331
00332
00333 double ut1=1-u;
00334 double ut2=1-ut1;
00335
00336
00337 T.set_translation(translation[3*k+0]*ut1+translation[3*(k+1)+0]*ut2,
00338 translation[3*k+1]*ut1+translation[3*(k+1)+1]*ut2,
00339 translation[3*k+2]*ut1+translation[3*(k+1)+2]*ut2);
00340
00341 return T;
00342 }
00343
00344 int Animation_transformation::get_size()
00345 {
00346 return time.size();
00347 }
00348
00349
00350 Animation_transformation& Animation_transformation::operator=(const Animation_transformation& anim)
00351 {
00352 bone_number = anim.bone_number;
00353
00354 if(bone_number!=-1)
00355 {
00356 angle_x.resize(bone_number);
00357 angle_y.resize(bone_number);
00358 angle_z.resize(bone_number);
00359 translation.resize(3*bone_number);
00360 time.resize(bone_number);
00361 anim_matrix.resize(16*bone_number);
00362
00363 int k=0;
00364 for(k=0;k<bone_number;k++)
00365 {
00366 angle_x[k] = anim.angle_x[k];
00367 angle_y[k] = anim.angle_y[k];
00368 angle_z[k] = anim.angle_z[k];
00369 time[k] = anim.time[k];
00370 translation[k] = anim.translation[k];
00371 anim_matrix[k] = anim.anim_matrix[k];
00372 }
00373 }
00374
00375 return *this;
00376 }
00377
00378
00379 double Animation_transformation::get_time(int k_index)
00380 {
00381 if(k_index<0 || k_index>=(int)time.size())
00382 {printf("Error time size is too large\n");exit(-1);}
00383 return time[k_index];
00384 }