File_parser.cpp

Go to the documentation of this file.
00001 
00002 #include <File_parser.h>
00003 #define PI 3.14159
00004 #define SIZE_BUFFER 1024
00005 #define DEBUG 1
00006 
00007 File_parser::File_parser()
00008 {
00009 }
00010 
00011 File_parser::~File_parser()
00012 {
00013 }
00014 
00015 
00016 
00017 
00018 
00019 int File_parser::load_collada_skeleton(const char* filename,const char* skeleton_name,Skeleton *skeleton) const
00020 {
00021   skeleton->reccursive_destroy();
00022 
00023 #ifdef DEBUG
00024   printf("\n[Load skeleton %s ...]\n",filename);
00025 #endif
00026 
00027 
00028   TiXmlDocument doc(filename);
00029   if(!doc.LoadFile())
00030     {
00031       printf("ERROR opening %s\n",filename);
00032       cerr << "error #" << doc.ErrorId() << " : " << doc.ErrorDesc() << endl;
00033       exit(-1);
00034     }
00035   
00036   TiXmlHandle visual_scenes(doc.RootElement());
00037   visual_scenes = visual_scenes.FirstChild("library_visual_scenes").FirstChild("visual_scene");
00038   if(visual_scenes.ToNode()==NULL)
00039     {printf("ERROR cannot find visual_scenes for skeleton in file %s\n",filename);exit(-1);}
00040 
00041 
00042 #ifdef DEBUG
00043   printf("[find skeleton visual scene id[%s] named %s]\n",visual_scenes.ToElement()->Attribute("id"),visual_scenes.ToElement()->Attribute("name"));
00044 #endif
00045 
00046 
00047   std::string name;
00048   TiXmlHandle node(visual_scenes);
00049   node = node.FirstChild("node");
00050   TiXmlElement *look_for_skeleton_name=NULL;
00051   look_for_skeleton_name = node.ToElement();
00052   if(look_for_skeleton_name->Attribute("name")!=NULL)
00053     name = look_for_skeleton_name->Attribute("name");
00054 
00055 
00056   while( (look_for_skeleton_name!=NULL) && (name!=skeleton_name) )
00057     {
00058       look_for_skeleton_name = look_for_skeleton_name->NextSiblingElement();
00059       if( (look_for_skeleton_name!=NULL) && look_for_skeleton_name->Attribute("name")!=NULL)
00060         name = look_for_skeleton_name->Attribute("name");
00061     }
00062 
00063 
00064   if(look_for_skeleton_name==NULL)
00065     {printf("Error cannot find %s in file %s\n",skeleton_name,filename);exit(-1);}
00066 #ifdef DEBUG
00067   printf("[Find skeleton %s]\n",skeleton_name);
00068 #endif
00069 
00070   name = look_for_skeleton_name->Attribute("id");
00071 
00072 
00073   Joint *current = skeleton->get_root();
00074   if(current==NULL)
00075     {printf("ERROR root_node is null in loading new skeleton\n"),exit(-1);}
00076 
00077   current->set_name(name);
00078   if(look_for_skeleton_name->Attribute("sid")==NULL)
00079     {current->set_weight_name("_N0_WEIGHT_");}
00080   else
00081     {current->set_weight_name(look_for_skeleton_name->Attribute("sid"));}
00082 
00083 
00084   TiXmlHandle starting_node(look_for_skeleton_name);
00085   reccursive_skeleton_collada_reading_node(starting_node,current);
00086 
00087 
00088   skeleton->read_skeleton();
00089   skeleton->precalculate_matrix();
00090   skeleton->init_time_animation();
00091 
00092 #ifdef DEBUG
00093   printf("[SKELETON %s LOADED]\n\n",filename);
00094 #endif
00095 
00096   return 0;
00097 }
00098 
00099 int File_parser::reccursive_skeleton_collada_reading_node(TiXmlHandle &current_node,Joint *current) const
00100 {
00101 
00102   int k0=0,k1=0,count=0;
00103   std::string X;
00104 
00105   TiXmlHandle child(current_node);
00106   TiXmlElement *e_child=NULL;
00107   
00108 
00109   e_child = child.FirstChild().ToElement();
00110 
00111   if(e_child==NULL)
00112     {printf("ERROR no child to node named %s\n",current->get_name());exit(-1);}
00113 
00114   while(e_child!=NULL)
00115     {
00116       if(e_child->ValueStr()=="rotate")
00117         {
00118           if((e_child->Attribute("sid"))==NULL)
00119             {printf("Error no attribute sid in child rotate in node %s\n",current->get_name());exit(-1);}
00120           std::string rotation;
00121           rotation = e_child->Attribute("sid");
00122           
00123           std::string rotation_data_str;
00124           std::vector <double> rotation_data;
00125           
00126           if(e_child->FirstChild()==NULL)
00127             {printf("ERROR no data in rotation for node %s\n",current->get_name());exit(-1);}
00128           
00129           rotation_data_str = e_child->FirstChild()->ValueStr();
00130           
00131           count=0;k0=0;k1=0;
00132           rotation_data.resize(0);
00133           for(count=0;count<4;count++)
00134             {
00135               k1=rotation_data_str.find(" ",k1+1);
00136               X = rotation_data_str.substr(k0,k1-k0);
00137               rotation_data.push_back(atof(X.data()));
00138               k0=k1+1;
00139             }
00140           
00141           //rotate
00142           current->add_rotation(rotation_data[0],rotation_data[1],rotation_data[2],rotation_data[3]*PI/180);
00143           
00144         }
00145       else if(e_child->ValueStr()=="translate")
00146         {
00147 
00148           std::string translation_data_str;
00149           std::vector <double> translation_data;
00150           
00151           if(e_child->FirstChild()==NULL)
00152             {printf("ERROR no data in translation for node %s\n",current->get_name());exit(-1);}
00153           
00154           translation_data_str = e_child->FirstChild()->ValueStr();
00155           
00156           count=0;k0=0;k1=0;
00157           translation_data.resize(0);
00158           for(count=0;count<3;count++)
00159             {
00160               k1=translation_data_str.find(" ",k1+1);
00161               X = translation_data_str.substr(k0,k1-k0);
00162               translation_data.push_back(atof(X.data()));
00163               k0=k1+1;
00164             }
00165 
00166           //translate
00167           current->add_position(translation_data[0],translation_data[1],translation_data[2]);
00168 
00169         }
00170       else if(e_child->ValueStr()=="scale")
00171         {
00172 
00173           std::string scale_data_str;
00174           std::vector <double> scale_data;
00175           
00176           if(e_child->FirstChild()==NULL)
00177             {printf("ERROR no data in translation for node %s\n",current->get_name());exit(-1);}
00178           
00179           scale_data_str = e_child->FirstChild()->ValueStr();
00180           
00181           count=0;k0=0;k1=0;
00182           scale_data.resize(0);
00183           for(count=0;count<3;count++)
00184             {
00185               k1=scale_data_str.find(" ",k1+1);
00186               X = scale_data_str.substr(k0,k1-k0);
00187               scale_data.push_back(atof(X.data()));
00188               k0=k1+1;
00189             }
00190 
00191           //scale
00192           current->scale(scale_data[0],scale_data[1],scale_data[2]);
00193 
00194 
00195         }
00196       else if(e_child->ValueStr()=="matrix")
00197         {
00198           std::string matrix_data_str;
00199           std::vector <double> matrix_data;
00200           
00201           if(e_child->FirstChild()==NULL)
00202             {printf("ERROR no data in translation for node %s\n",current->get_name());exit(-1);}
00203           
00204           matrix_data_str = e_child->FirstChild()->ValueStr();
00205           
00206           count=0;k0=0;k1=0;
00207           matrix_data.resize(0);
00208           for(count=0;count<16;count++)
00209             {
00210               k1=matrix_data_str.find(" ",k1+1);
00211               X = matrix_data_str.substr(k0,k1-k0);
00212               matrix_data.push_back(atof(X.data()));
00213               k0=k1+1;
00214             }
00215 
00216           //scale
00217           Matrix M;
00218           M.set_value(matrix_data[0],matrix_data[1],matrix_data[2],matrix_data[3],matrix_data[4],matrix_data[5],matrix_data[6],matrix_data[7],matrix_data[8],matrix_data[9],matrix_data[10],matrix_data[11],matrix_data[12],matrix_data[13],matrix_data[14],matrix_data[15]);
00219           current->set_matrix(M);
00220 
00221         }
00222       else if(e_child->ValueStr()=="node")
00223         {
00224           current->add_son();
00225           if(e_child->Attribute("id")==NULL)
00226             {printf("Error no name for child of node %s\n",current->get_name());exit(-1);}
00227           current->get_son(0)->set_name(e_child->Attribute("id"));
00228           if(e_child->Attribute("sid")==NULL)
00229             {current->get_son(0)->set_weight_name("_NO_WEIGHT_");}
00230           else
00231             {current->get_son(0)->set_weight_name(e_child->Attribute("sid"));}
00232 #ifdef DEBUG
00233           //printf("[find skeleton node <%s>]\n",current->get_name());
00234 #endif
00235 
00236           TiXmlHandle new_child(e_child);
00237           reccursive_skeleton_collada_reading_node(new_child,current->get_son(0));
00238         }
00239       
00240       
00241       e_child = e_child->NextSiblingElement();
00242     }
00243   
00244   return 0;
00245 }
00246 
00247 int File_parser::save_skeleton_sk(const char* filename,Skeleton &skeleton) const
00248 {
00249 
00250   //just in case
00251   skeleton.read_skeleton();
00252   skeleton.precalculate_matrix();
00253 
00254 
00255     //save obj format
00256   FILE *fid=NULL;
00257   
00258   char name_skeleton[SIZE_BUFFER];
00259   strcpy(name_skeleton,filename);
00260   strcat(name_skeleton,".sk");
00261   fid = fopen(name_skeleton,"w");
00262 
00263 
00264   if(fid==NULL)
00265     {
00266       cout<<"ERROR opening file "<<name_skeleton<<endl;
00267       return -1;
00268     }
00269   
00270   fprintf(fid,"# skinning model %s\n",name_skeleton);
00271   fprintf(fid,"# skeleton model\n");
00272   fprintf(fid,"# N_joint= %d \n",skeleton.get_N_joint());
00273 
00274   fprintf(fid,"#\n# Visual Hierarchy [level] name (id_number) <father: id_father> [number_of_son s: id_sons ...]\n#\n");
00275 
00276   int count=0;
00277   int k_joint=0;
00278   Joint *current=NULL;
00279   for(k_joint=0;k_joint<skeleton.get_N_joint();k_joint++)
00280     {
00281       current = skeleton.get_joint(k_joint);
00282       fprintf(fid,"# ");
00283       for(count=0;count<current->get_level();count++)
00284         fprintf(fid,"  ");
00285       //fprintf(fid,"<%s> (%d)\n ",current->get_name(),current->get_access_number());
00286       fprintf(fid," [%d] <%s> (%d)  \t\t\t <f:%d> [%d s: ",current->get_level(),current->get_name(),current->get_access_number(),current->get_father()->get_access_number(),current->get_N_son());
00287       for(count=0;count<current->get_N_son();count++)
00288         fprintf(fid,"%d ",current->get_son(current->get_N_son()-count-1)->get_access_number());
00289       fprintf(fid,"]\n");
00290     }
00291   fprintf(fid,"#\n");
00292 
00293   if(fclose(fid)!=0)
00294     {
00295       cout<<"WARNING ERROR closing file"<<name_skeleton<<endl;
00296       exit(-1);
00297     }
00298   return 0;
00299 
00300 }
00301 
00302 
00303 int File_parser::load_collada_animation(const char* filename,Skeleton *skeleton) const 
00304 {
00305 
00306 
00307 
00308 #ifdef DEBUG
00309   printf("\n[Load animation skeleton %s ...]\n",filename);
00310 #endif
00311 
00312   TiXmlDocument doc(filename);
00313   if(!doc.LoadFile())
00314     {
00315       printf("ERROR opening %s\n",filename);
00316       cerr << "error #" << doc.ErrorId() << " : " << doc.ErrorDesc() << endl;
00317       exit(-1);
00318     }
00319   
00320   TiXmlHandle library_animations(doc.RootElement());
00321   library_animations = library_animations.FirstChild("library_animations");
00322   if(library_animations.ToNode()==NULL)
00323     {printf("ERROR cannot find library_animations for skeleton in file %s\n",filename);exit(-1);}
00324   
00325   TiXmlHandle animation(library_animations);
00326   animation = animation.FirstChild("animation");
00327   
00328   TiXmlElement *anim_element=NULL;
00329 
00330   TiXmlElement *anim_input=NULL;
00331   TiXmlElement *anim_output=NULL;
00332   std::string anim_description;
00333   string::size_type location;
00334 
00335   std::string anim_name;
00336 
00337 
00338   std::string anim_body_part;
00339   std::string anim_action;
00340 
00341   
00342   std::string anim_input_str;
00343   std::vector <double> anim_input_data;
00344   std::string anim_output_str;
00345   std::vector <double> anim_output_data;
00346   std::string X;
00347 
00348   int k0=0;
00349   int num_joint=0;
00350 
00351   int k1=0;
00352   int vector_count=0;
00353   int count;
00354 
00355   anim_element = animation.ToElement();
00356   if(anim_element==NULL)
00357     {printf("Error cannot find animation in library_animation\n");exit(-1);}
00358 
00359 
00360   int stride_accessor=0;
00361   std::string accessor_type;
00362 
00363   while(anim_element!=NULL)
00364     {
00365       if(anim_element->Attribute("id")==NULL)
00366         {printf("Error cannot find animation id in animation\n");exit(-1);}
00367       anim_name = anim_element->Attribute("id");
00368       
00369       //separates name.action
00370       k0 = anim_name.rfind(".");
00371       if(k0==-1)//try name_action
00372         k0 = anim_name.rfind("_");
00373       if(k0==-1)
00374         k0 = anim_name.rfind("-");
00375       if(k0==-1)
00376         {printf("ERROR cannot find separator for action type in %s\n",anim_name.data());exit(-1);}
00377       
00378 
00379 
00380       anim_body_part = anim_name.substr(0,k0);
00381       anim_action = anim_name.substr(k0+1,anim_name.length()-k0-1);
00382 
00383 //       //try to find name-node
00384 //       int k3=0;
00385 //       k3 = anim_body_part.rfind("-node");
00386 //       if(k3!=-1)
00387 //      anim_body_part = anim_name.substr(0,k3);
00388 
00389       //get access number of the name
00390       num_joint = skeleton->get_access_number_of_joint_named(anim_body_part);
00391       //if(num_joint>0)
00392         {
00393 
00394 
00395 #ifdef DEBUG
00396       printf("find [%s](%d) with action [%s]\n",anim_body_part.data(),num_joint,anim_action.data());
00397 #endif 
00398 
00399       //find input
00400       //**********************//
00401 
00402       anim_input = anim_element->FirstChild("source")->ToElement();
00403       if(anim_input == NULL)
00404         {printf("Error no source child in animation with id[%s]\n",anim_name.data());exit(-1);}
00405 
00406  
00407       do
00408         {
00409           if(anim_input->Attribute("id")==NULL)
00410             {printf("Error no id in source for animation id[%s]\n",anim_name.data());exit(-1);}
00411           anim_description = anim_input->Attribute("id");
00412           location = anim_description.find("-input");
00413           if(location==string::npos)//not fing input
00414             anim_input = anim_input->NextSiblingElement();
00415         }while(anim_input!=NULL && location==string::npos);
00416         
00417         if(anim_input==NULL)
00418           {printf("ERROR cannot find input array for anim [%s]\n",anim_name.data());exit(-1);}
00419 
00420         if(anim_input->FirstChild("float_array") == NULL)
00421           {printf("Error no float_array in anim %s\n",anim_name.data());exit(-1);}
00422         if(anim_input->FirstChild("float_array")->ToElement()->Attribute("count")==NULL)
00423           {printf("Error no coutn attribute in float_array for anim %s\n",anim_name.data());exit(-1);}
00424         anim_input->FirstChild("float_array")->ToElement()->QueryIntAttribute("count",&vector_count);
00425         if(anim_input->FirstChild("float_array")->FirstChild() == NULL)
00426           {printf("Error no float_array data in anim %s\n",anim_name.data());exit(-1);}
00427         anim_input_str = anim_input->FirstChild("float_array")->FirstChild()->ValueStr();
00428         
00429         //set size
00430         skeleton->set_size_time(num_joint,vector_count);
00431         
00432 
00433         count=0;
00434         k0=0;
00435         k1=0;
00436         anim_input_data.resize(0);
00437         for(count=0;count<vector_count;count++)
00438           {
00439             k1=anim_input_str.find(" ",k1+1);
00440             X = anim_input_str.substr(k0,k1-k0);
00441             anim_input_data.push_back(atof(X.data()));
00442 
00443             //set time
00444             skeleton->set_time(num_joint,count,anim_input_data[count]);
00445             k0=k1+1;
00446           }
00447 
00448 
00449 
00450 
00451         //get output
00452         //**********************//
00453         
00454         anim_output = anim_element->FirstChild("source")->ToElement();
00455         if(anim_output == NULL)
00456           {printf("Error no source child in animation with id[%s]\n",anim_name.data());exit(-1);}
00457 
00458 
00459         do
00460           {
00461             if(anim_output->Attribute("id")==NULL)
00462               {printf("Error no id in source for animation id[%s]\n",anim_name.data());exit(-1);}
00463             anim_description = anim_output->Attribute("id");
00464             location = anim_description.find("-output");
00465             if(location==string::npos)//not fing input
00466               anim_output = anim_output->NextSiblingElement();
00467           }while(anim_output!=NULL && location==string::npos);
00468         
00469         if(anim_output==NULL)
00470           {printf("ERROR cannot find output array for anim [%s]\n",anim_name.data());exit(-1);}
00471         
00472         if(anim_output->FirstChild("float_array") == NULL)
00473           {printf("Error no float_array in anim %s\n",anim_name.data());exit(-1);}
00474         if(anim_output->FirstChild("float_array")->ToElement()->Attribute("count")==NULL)
00475           {printf("Error no coutn attribute in float_array for anim %s\n",anim_name.data());exit(-1);}
00476         anim_output->FirstChild("float_array")->ToElement()->QueryIntAttribute("count",&vector_count);
00477         if(anim_output->FirstChild("float_array")->FirstChild() == NULL)
00478           {printf("Error no float_array data in anim %s\n",anim_name.data());exit(-1);}
00479         anim_output_str = anim_output->FirstChild("float_array")->FirstChild()->ValueStr();
00480         
00481         if(anim_output->FirstChild("technique_common")==NULL)
00482           {printf("Error no technique_common found\n");exit(-1);}
00483         if(anim_output->FirstChild("technique_common")->FirstChild("accessor")==NULL)
00484           {printf("Error no accessor found\n");exit(-1);}
00485         if(anim_output->FirstChild("technique_common")->FirstChild("accessor")->ToElement()->Attribute("stride")==NULL)
00486           {printf("Error no stride found in accessor %s\n",anim_name.data());exit(-1);}
00487         
00488         anim_output->FirstChild("technique_common")->FirstChild("accessor")->ToElement()->QueryIntAttribute("stride",&stride_accessor);
00489 
00490 
00491 
00492         if(stride_accessor==1 && anim_action=="translate")
00493           {
00494             //look for what
00495             if(anim_output->FirstChild("technique_common")->FirstChild("accessor")->FirstChild("param")==NULL)
00496               {printf("ERROR cannot find param in accessor of animation %s\n",anim_name.data());exit(-1);}
00497             if(anim_output->FirstChild("technique_common")->FirstChild("accessor")->FirstChild("param")->ToElement()->Attribute("name")==NULL)
00498               {printf("ERROR cannot find name in param of accessor of animation %s\n",anim_name.data());exit(-1);}
00499             accessor_type = anim_output->FirstChild("technique_common")->FirstChild("accessor")->FirstChild("param")->ToElement()->Attribute("name");
00500 
00501             if(accessor_type=="X")
00502               {
00503                 count=0;k0=0;k1=0;anim_output_data.resize(0);
00504                 for(count=0;count<vector_count;count++)
00505                   {
00506                     k1=anim_output_str.find(" ",k1+1);
00507                     X = anim_output_str.substr(k0,k1-k0);
00508                     anim_output_data.push_back(atof(X.data()));
00509                     anim_output_data.push_back(0.0);
00510                     anim_output_data.push_back(0.0);
00511                     k0=k1+1;
00512                   }
00513               }
00514             else if(accessor_type=="Y")
00515               {
00516                 count=0;k0=0;k1=0;anim_output_data.resize(0);
00517                 for(count=0;count<vector_count;count++)
00518                   {
00519                     k1=anim_output_str.find(" ",k1+1);
00520                     X = anim_output_str.substr(k0,k1-k0);
00521                     anim_output_data.push_back(0.0);
00522                     anim_output_data.push_back(atof(X.data()));
00523                     anim_output_data.push_back(0.0);
00524                     k0=k1+1;
00525                   }
00526               }
00527             else if(accessor_type=="Z")
00528               {
00529                 count=0;k0=0;k1=0;anim_output_data.resize(0);
00530                 for(count=0;count<vector_count;count++)
00531                   {
00532                     k1=anim_output_str.find(" ",k1+1);
00533                     X = anim_output_str.substr(k0,k1-k0);
00534                     anim_output_data.push_back(0.0);
00535                     anim_output_data.push_back(0.0);
00536                     anim_output_data.push_back(atof(X.data()));
00537                     k0=k1+1;
00538                   }
00539               }
00540             else
00541               {printf("ERROR unknown accessor_type [%s] for anim %s\n",accessor_type.data(),anim_name.data());exit(-1);}
00542 
00543 
00544           }
00545         else//stride==3 for translate or rotateX/Y/Z or transform
00546           {
00547             count=0;
00548             k0=0;
00549             k1=0;
00550             anim_output_data.resize(0);
00551             for(count=0;count<vector_count;count++)
00552               {
00553                 k1=anim_output_str.find(" ",k1+1);
00554                 X = anim_output_str.substr(k0,k1-k0);
00555                 anim_output_data.push_back(atof(X.data()));
00556                 k0=k1+1;
00557               }
00558           }
00559 
00560         double c=1;
00561 
00562         if(anim_action=="rotateX")
00563           {
00564             for(count=0;count<vector_count;count++)
00565               skeleton->set_angle(num_joint,count,0,anim_output_data[count]*c);
00566           }
00567         else if(anim_action=="rotateY")
00568           {
00569             for(count=0;count<vector_count;count++)
00570               skeleton->set_angle(num_joint,count,1,anim_output_data[count]*c);
00571           }
00572         else if(anim_action=="rotateZ")
00573           {
00574             for(count=0;count<vector_count;count++)
00575               skeleton->set_angle(num_joint,count,2,anim_output_data[count]*c);
00576           }
00577         else if(anim_action=="translate")
00578           {
00579             if( (vector_count%3) !=0)
00580               {printf("something strange in size vector of translation for joint n.[%d] \n",num_joint);exit(-1);}
00581 
00582             for(count=0;count<vector_count/3;count++)
00583               skeleton->set_translate(num_joint,count,anim_output_data[3*count+0],anim_output_data[3*count+1],anim_output_data[3*count+2]);
00584           }
00585         else if(anim_action=="transform")
00586           {
00587             if( (vector_count%16) !=0)
00588               {printf("something strange in size matrix for joint n.[%d] \n",num_joint);exit(-1);}
00589             for(count=0;count<vector_count/16;count++)
00590               for(int k=0;k<16;k++)
00591                 {
00592                   //printf("%d,%d,%d\n",num_joint,count,k);
00593                   skeleton->set_anim_matrix(num_joint,count,k,anim_output_data[16*count+k]);
00594                 }
00595           }
00596 
00597         }
00598 
00599         anim_element = anim_element->NextSiblingElement();
00600     }
00601 
00602 
00603 #ifdef DEBUG
00604   printf("[find library_animations]\n");
00605 #endif
00606 
00607 
00608 
00609 
00610   printf("[ANIMATION SKELETON %s LOADED]\n\n",filename);
00611 
00612   
00613 
00614   return 0;
00615 }
00616 
00617 int File_parser::load_collada_skining_attribute(const char* filename, Skinned *skinned) const
00618 {
00619 
00620   Skeleton *skeleton = skinned->get_skeleton();
00621   Mesh_object *mesh = skinned->get_mesh();
00622 
00623 
00624 #ifdef DEBUG
00625   printf("\n[Load skinning attribute %s ...]\n",filename);
00626 #endif
00627 
00628   TiXmlDocument doc(filename);
00629   if(!doc.LoadFile())
00630     {
00631       printf("ERROR opening %s\n",filename);
00632       cerr << "error #" << doc.ErrorId() << " : " << doc.ErrorDesc() << endl;
00633       exit(-1);
00634     }
00635   
00636   TiXmlHandle library_animations(doc.RootElement());
00637   library_animations = library_animations.FirstChild("library_controllers");
00638   if(library_animations.ToNode()==NULL)
00639     {printf("ERROR cannot find library_controllers in file %s\n",filename);exit(-1);}
00640   
00641   TiXmlHandle skin(library_animations);
00642   skin = library_animations.FirstChild("controller").FirstChild("skin");
00643   if(skin.ToNode()==NULL)
00644     {printf("ERROR cannot find skin source in file %s\n",filename);exit(-1);}
00645   
00646 
00647   //NAME
00648   //****************************//
00649 
00650 
00651   TiXmlHandle name(skin);
00652   //try first IDREF
00653   name = skin.FirstChild("source").FirstChild("IDREF_array");  
00654   if(name.ToNode()==NULL)//if null try Name_array (new collada)
00655     {
00656       name = skin.FirstChild("source").FirstChild("Name_array");
00657       if(name.ToNode()==NULL)
00658         {printf("Error cannot find Name_array in source in library_controllers for skinning information in file %s\n",filename);exit(-1);}
00659     }
00660   if(name.FirstChild().ToNode()==NULL)
00661     {printf("Error cannot find name list in library_controllers for skinning information in file %s\n",filename);exit(-1);}
00662 
00663   if(name.ToElement()->Attribute("count")==NULL)
00664     {printf("Error cannot access to count attribute in name_list of skinning in file %s\n",filename);exit(-1);}
00665 
00666   int N_name_list=0;
00667   name.ToElement()->QueryIntAttribute("count",&N_name_list);
00668 
00669 #ifdef DEBUG
00670   printf("[Find list of %d names]\n",N_name_list);
00671 #endif
00672 
00673   std::string name_list_str;
00674   std::vector <std::string> name_list;
00675   
00676   name_list_str = name.FirstChild().ToText()->ValueStr();
00677   
00678   std::string X;
00679   int count=0;
00680   int k0=0;
00681   int k1=0;
00682   name_list.resize(0);
00683   for(count=0;count<N_name_list;count++)
00684     {
00685       k1 = name_list_str.find(" ",k1+1);
00686       X  = name_list_str.substr(k0,k1-k0);
00687 
00688 //       //throw away -node
00689 //       if((int)X.rfind("-node")!=-1)
00690 //      X = X.substr(0,X.rfind("-node"));
00691 
00692       name_list.push_back(X);
00693       k0=k1+1;
00694     }
00695 
00696   
00697 
00698   //BIND POSE
00699   //****************************//
00700   TiXmlHandle bind(skin);
00701   int N;
00702   std::string look_for_bind;
00703 
00704   bind = skin.FirstChild("source");
00705   TiXmlElement *look_for_bind_element=NULL;
00706   look_for_bind_element = bind.ToElement();
00707   do
00708     {
00709       if(look_for_bind_element==NULL)
00710         {printf("Error can't find source for bind pose in file %s\n",filename);exit(-1);}
00711 
00712       look_for_bind=look_for_bind_element->Attribute("id");
00713       N = look_for_bind.rfind("-bind_poses");
00714       if(N<0)
00715         look_for_bind_element = look_for_bind_element->NextSiblingElement("source");
00716     }while(look_for_bind_element!=NULL && N<0);
00717 
00718 
00719   
00720   if(look_for_bind_element==NULL)
00721     {printf("Error can't find source for bind pose in file %s\n",filename);exit(-1);}
00722 
00723   TiXmlHandle bind_array(look_for_bind_element);  
00724 
00725   bind_array = bind_array.FirstChild("float_array");
00726   if(bind_array.ToNode()==NULL)
00727     {printf("Error cannot find float_array in source for bind_pose in file %s\n",filename);exit(-1);}
00728 
00729   int count_bind_pose=0;
00730   if(bind_array.ToElement()->Attribute("count")==NULL)
00731     {printf("Error cannot find count Attribute in float_array for bind pose in file %s\n",filename);exit(-1);}
00732   bind_array.ToElement()->QueryIntAttribute("count",&count_bind_pose);
00733   if(count_bind_pose%16!=0)
00734     {printf("Something strange in count_bind_pose in file %s\n",filename);exit(-1);}
00735 
00736   std::string bind_pose_str;
00737   std::vector <double> bind_pose_data;
00738 
00739   if(bind_array.FirstChild().ToNode()==NULL)
00740     {printf("Error no data in bind_array in file %s\n",filename);exit(-1);}
00741   bind_pose_str = bind_array.FirstChild().ToText()->ValueStr();
00742 
00743   
00744   
00745   count=0;k0=0;k1=0;
00746   int joint_number;
00747   Matrix M;
00748   for(count=0;count<count_bind_pose/16;count++)
00749     {
00750       joint_number = skeleton->get_access_number_of_joint_named(name_list[count]);
00751       //if(
00752          for(int count_21=0;count_21<4;count_21++)
00753            for(int count_22=0;count_22<4;count_22++)
00754              {
00755                k1=bind_pose_str.find(" ",k1+1);
00756                X = bind_pose_str.substr(k0,k1-k0);
00757                M.set_value(atof(X.data()),count_22,count_21);
00758                k0=k1+1;
00759              }
00760          skeleton->get_joint(joint_number)->set_bind_pose(M);
00761          // )
00762     }
00763   
00764       
00765   
00766   //SKINNING WEIGHT
00767   //****************************//
00768 
00769   TiXmlHandle weight(skin);
00770   std::string look_for_weight;
00771 
00772   weight = weight.FirstChild("source");
00773   TiXmlElement *look_for_weight_element=NULL;
00774   look_for_weight_element = weight.ToElement();
00775   do
00776     {
00777       if(look_for_weight_element==NULL)
00778         {printf("Error can't find source for skinning weights in file %s\n",filename);exit(-1);}
00779 
00780       look_for_weight = look_for_weight_element->Attribute("id");
00781       N = look_for_weight.rfind("-skin-weights");
00782       if(N<0)
00783         look_for_weight_element = look_for_weight_element->NextSiblingElement("source");
00784     }while(look_for_weight_element!=NULL && N<0);
00785 
00786 
00787 
00788 
00789   if(look_for_weight_element==NULL)
00790     {printf("Error can't find source for weight element in file %s\n",filename);exit(-1);}
00791 
00792   TiXmlHandle weight_array(look_for_weight_element);  
00793 
00794   weight_array = weight_array.FirstChild("float_array");
00795   if(weight_array.ToNode()==NULL)
00796     {printf("Error cannot find float_array in source for weight_array in file %s\n",filename);exit(-1);}
00797 
00798   int count_weight=0;
00799   if(weight_array.ToElement()->Attribute("count")==NULL)
00800     {printf("Error cannot find count Attribute in float_array for weight array in file %s\n",filename);exit(-1);}
00801   weight_array.ToElement()->QueryIntAttribute("count",&count_weight);
00802 
00803   std::string weight_str;
00804 
00805   if(weight_array.FirstChild().ToNode()==NULL)
00806     {printf("Error no data in weight_array in file %s\n",filename);exit(-1);}
00807   weight_str = weight_array.FirstChild().ToText()->ValueStr();
00808 
00809 
00810 
00811 #ifdef DEBUG
00812   printf("[Find %d skinning weights]\n",count_weight);
00813 #endif
00814 
00815 
00816 
00817   count=0;k0=0;k1=0;
00818   for(count=0;count<count_weight;count++)
00819     {
00820       k1=weight_str.find(" ",k1+1);
00821       X = weight_str.substr(k0,k1-k0);
00822       skinned->add_weight(atof(X.data()));
00823       k0=k1+1;
00824     }
00825 
00826 
00827   //Joint and weight list
00828   //****************************//
00829 
00830   TiXmlElement *vertex_weight=NULL;
00831   vertex_weight = skin.FirstChild("vertex_weights").ToElement();
00832   if(vertex_weight==NULL)
00833     {printf("Error cannot find vertex_weights in skin in library_controllers\n");exit(-1);}
00834   
00835   if(vertex_weight->Attribute("count")==NULL)
00836     {printf("Error cannot find attribute count in vertex_weights in library_controllers\n");exit(-1);}
00837   int N_vertex_weight=0;
00838   vertex_weight->QueryIntAttribute("count",&N_vertex_weight);
00839   
00840   if(N_vertex_weight!=skinned->get_mesh()->get_vertex_number())
00841     {printf("Error something strange in size of <vcount> of vertex_weight size[%d] in library_controllers for skinning link\n",N_vertex_weight);exit(-1);}
00842 
00843   TiXmlNode *vcount_size_vertex=NULL;
00844   vcount_size_vertex = vertex_weight->FirstChild("vcount");
00845   if(vcount_size_vertex==NULL)
00846     {printf("Error cannot find vcount in vertex_weights in library_controllers\n");exit(-1);}
00847   if(vcount_size_vertex->FirstChild()==NULL)
00848     {printf("Error no child in vcount for skinning dependencies\n");exit(-1);}
00849 
00850   std::string vcount_str;
00851   std::vector <int> dependencies_number;
00852   dependencies_number.resize(0);
00853 
00854   vcount_str = vcount_size_vertex->FirstChild()->ToText()->ValueStr();
00855   skinned->set_size_index_weight(mesh->get_vertex_number());
00856 
00857   count=0;k0=0;k1=0;
00858   for(count=0;count<N_vertex_weight;count++)
00859     {
00860       k1=vcount_str.find(" ",k1+1);
00861       X = vcount_str.substr(k0,k1-k0);
00862       dependencies_number.push_back(atoi(X.data()));
00863       //      skinned->set_size_index_weight(count,atoi(X.data()));
00864       k0=k1+1;
00865     }
00866 
00867 
00868 
00869   //INDEX
00870   //********************//
00871 
00872 
00873 
00874   TiXmlNode *vector_index_joint=NULL;
00875   vector_index_joint = vertex_weight->FirstChild("v");
00876   if(vector_index_joint==NULL)
00877     {printf("Error cannot find <v> vector of joint and weights index in skinning parameters\n");exit(-1);}
00878 
00879   std::string vector_index_str;
00880   std::vector <int> vector_index_joint_data;
00881   std::vector <int> vector_index_weight_data;
00882 
00883   if(vector_index_joint->FirstChild()==NULL)
00884     {printf("Error vector_index_joint has no child in skinning parameters in file %s\n",filename);exit(-1);}
00885   vector_index_str = vector_index_joint->FirstChild()->ToText()->ValueStr();
00886 
00887 
00888   //set size of index
00889   skinned->set_size_index_weight(mesh->get_vertex_number());
00890   skinned->set_size_index_joint(mesh->get_vertex_number());
00891 
00892 
00893   count=0;k0=0;k1=0;
00894   int k_size=0;
00895   int joint_number_pointed=0;
00896   for(count=0;count<N_vertex_weight;count++)
00897     {
00898       vector_index_joint_data.resize(0);
00899       vector_index_weight_data.resize(0);
00900       for(k_size=0;k_size<dependencies_number[count];k_size++)
00901         {
00902 
00903           //joint
00904           k1=vector_index_str.find(" ",k1+1);
00905           X = vector_index_str.substr(k0,k1-k0);
00906           //get_name of joint
00907           joint_number_pointed = skeleton->get_access_number_of_joint_named(name_list[atoi(X.data())]);
00908 
00909           vector_index_joint_data.push_back(joint_number_pointed);
00910           k0=k1+1;
00911 
00912 
00913           //weight
00914           k1=vector_index_str.find(" ",k1+1);
00915           X = vector_index_str.substr(k0,k1-k0);
00916           vector_index_weight_data.push_back(atoi(X.data()));
00917           k0=k1+1;
00918         }
00919 
00920       skinned->add_index_joint(count,vector_index_joint_data);
00921       skinned->add_index_weight(count,vector_index_weight_data);
00922 
00923     }
00924   
00925 #ifdef DEBUG
00926   printf("[index joint and weight Loaded for %d vertices]\n",count);
00927 #endif
00928 
00929   //reorder weights and index
00930 
00931   std::vector <double> new_weight;
00932   std::vector <int_vector> new_index_weights;
00933   std::vector <int_vector> new_index_bones;
00934 
00935   
00936   new_weight.resize(mesh->get_vertex_number()*skeleton->get_N_joint());
00937   new_index_weights.resize(mesh->get_vertex_number());
00938   new_index_bones.resize(mesh->get_vertex_number());
00939 
00940   int k_vertex=0;
00941   int k_joint=0;
00942   int k_v;
00943 
00944   for(k_vertex=0;k_vertex<mesh->get_vertex_number();k_vertex++)
00945     {
00946 
00947       for(k_v=0;k_v<skinned->get_size_index_weight(k_vertex);k_v++)
00948         new_weight[k_vertex+skinned->get_index_joint(k_vertex,k_v)*mesh->get_vertex_number()] = skinned->get_weight(k_vertex,k_v);
00949       
00950       new_index_weights[k_vertex].resize(skeleton->get_N_joint());
00951       new_index_bones  [k_vertex].resize(skeleton->get_N_joint());
00952       for(k_joint=0;k_joint<skeleton->get_N_joint();k_joint++)
00953         {
00954           new_index_weights[k_vertex][k_joint] = k_vertex+k_joint*mesh->get_vertex_number();
00955           new_index_bones  [k_vertex][k_joint] = k_joint;
00956         }
00957 
00958     }
00959 
00960   skinned->set_weight(new_weight);
00961   skinned->set_index_weight(new_index_weights);
00962   skinned->set_index_joint(new_index_bones);
00963 
00964   
00965 
00966   skinned->fill_vertex_depending_on_bone();
00967 
00968 #ifdef DEBUG
00969   printf("[index joint and weight loaded and sorted by bones in Skinned with success]\n");
00970 #endif
00971   
00972 
00973 
00974   
00975   printf("[SKINNING ATTRIBUTES %s LOADED]\n\n",filename);
00976   
00977   return 0;
00978 }
00979 
00980 int File_parser::load_old_sk_skeleton(const char *filename,Skeleton *skeleton) const
00981 {
00982 
00983   if(skeleton==NULL){
00984     printf("Error skeleton not allowed\n");exit(-1);}
00985 
00986   FILE *fid=NULL;
00987 
00988   char name_skeleton[1024]={'\0'};
00989   strcpy(name_skeleton,filename);
00990   strcat(name_skeleton,".sk");
00991 
00992   fid = fopen(name_skeleton,"r");
00993   if(fid==NULL)
00994     {cout<<"ERROR opening file "<<name_skeleton<<endl;exit(-1);}
00995   
00996 
00997   char buffer[1024];
00998 
00999   int is_first=1;
01000   Joint *current_joint = skeleton->get_root();
01001   Joint *father=NULL;
01002 
01003   int level=0;
01004   int k=0;
01005   float value=0.0;
01006   Matrix M;
01007 
01008 
01009   while(fscanf(fid,"%s",buffer)!=EOF)
01010     {
01011       switch(buffer[0])
01012         {
01013         case '#'://comments
01014           fgets(buffer,1024,fid);
01015           break;
01016         case 'n'://new joint
01017           if(is_first!=1)
01018             {
01019               //fgets(buffer,1024,fid);//read rest of line
01020               current_joint->add_son();
01021               father = current_joint;
01022               current_joint = current_joint->get_son(0);
01023               level++;
01024             }
01025           is_first=0;
01026           break;
01027         case 'M'://matrix
01028           for(k=0;k<16;k++)
01029             {
01030               if(fscanf(fid,"%f",&value)!=1)
01031                 {printf("ERROR reading matrix value in skeleton reading %f\n",value);exit(-1);}
01032               M.set_value(value,k,0);
01033             }
01034           current_joint->set_matrix(M);
01035           break;
01036         case 'B'://Bind Pose
01037           for(k=0;k<16;k++)
01038             {
01039               if(fscanf(fid,"%f",&value)!=1)
01040                 {printf("ERROR reading matrix value in skeleton reading %f\n",value);exit(-1);}
01041               M.set_value(value,k,0);
01042             }
01043           current_joint->set_bind_pose(M);
01044           break;
01045         case '_':
01046           if(buffer[1]=='d' && buffer[2]=='n')
01047             {
01048               level--;
01049               if(current_joint->get_father()!=NULL)
01050                 current_joint = current_joint->get_father();
01051             }
01052           break;
01053         }
01054 
01055 
01056     }  
01057   
01058 
01059   
01060   if(fclose(fid)!=0)
01061     {
01062       cout<<"WARNING ERROR closing file"<<name_skeleton<<endl;
01063       exit(-1);
01064     }
01065 
01066 
01067   //set skeleton
01068   skeleton->read_skeleton();
01069   skeleton->init_time();
01070   skeleton->fix_init_bind_pose();
01071   skeleton->precalculate_matrix();
01072   
01073 
01074   return 0;
01075 }
01076 
01077 
01078 

Generated on Mon Mar 30 16:55:54 2009 by  doxygen 1.5.6