removeModelFace.cpp
Go to the documentation of this file.
00001 
00034 // Usage: removeModelFace <model_dir> <face_idx>
00035 
00036 #include <iostream>
00037 #include <fstream>
00038 #include <iomanip>
00039 #include <vector>
00040 #include <string>
00041 #include <opencv/cv.h>
00042 #include <opencv/highgui.h>
00043 
00044 #include "../ObjectModel.h"
00045 #include "../MetaFile.h"
00046 
00047 #include "ros/ros.h"
00048 #include "DUtils.h"
00049 #include "DUtilsCV.h"
00050 
00051 #include <stdio.h>
00052 
00053 using namespace std;
00054 
00055 // ---------------------------------------------------------------------------
00056 
00057 int main(int argc, char *argv[])
00058 {
00059   ros::init(argc, argv, "removeModelFace");
00060   
00061   if(argc < 2)
00062   {
00063     cout << "Usage: " << argv[0] << " <model dir> <face idx>" << endl;
00064     return 0;
00065   }
00066 
00067   string model_dir = argv[1];
00068   int face_idx = atoi(argv[2]);
00069   
00070   // check model
00071   if(!ObjectModel::checkDirectory(model_dir))
00072   {
00073     ROS_ERROR("Directory %s does not seem to contain a valid object model",
00074       model_dir.c_str());
00075     return 1;
00076   }
00077   
00078   // check index
00079   ObjectModel model(model_dir, false);
00080   
00081   if(model.Faces.empty())
00082   {
00083     ROS_WARN("Object %s does not contain any face", model.Name.c_str());
00084     return 0;
00085   }
00086   
00087   if(face_idx < 0 || face_idx >= (int)model.Faces.size())
00088   {
00089     ROS_WARN("Face index out of bounds. Object %s has %d faces (0 - %d)",
00090       model.Name.c_str(), model.Faces.size(), model.Faces.size() - 1);
00091     return 2;
00092   }
00093   
00094   vector<string> extensions;
00095   extensions.push_back(".key.gz");
00096   extensions.push_back(".ply");
00097   extensions.push_back(".png");
00098   extensions.push_back(".txt");
00099   
00100   for(int idx = face_idx; idx < (int)model.Faces.size()-1; ++idx)
00101   {
00102     // mv face_%03d .key.gz / .ply / .png / .txt one index backwards
00103     stringstream source, target;
00104     source << model_dir << "/face_" << setw(3) << setfill('0') << idx+1;
00105     target << model_dir << "/face_" << setw(3) << setfill('0') << idx;
00106     
00107     for(unsigned int j = 0; j < extensions.size(); ++j)
00108     {
00109       rename((source.str() + extensions[j]).c_str(), 
00110         (target.str() + extensions[j]).c_str() );
00111     }
00112   }
00113   
00114   // delete face_%03d with index |model.Faces|-1
00115   stringstream source;
00116   source << model_dir << "/face_" << setw(3) << setfill('0') 
00117     << model.Faces.size() - 1;
00118 
00119   for(unsigned int j = 0; j < extensions.size(); ++j)
00120   {
00121     remove((source.str() + extensions[j]).c_str());
00122   }
00123   
00124   // update meta
00125   ROS_INFO("Updating meta data...");
00126   
00127   MetaFile::MetaData data;
00128   string meta_file = model_dir + "/meta.xml";
00129   MetaFile::readFile(meta_file, data);
00130   
00131   data.NFaces--;
00132   if(data.Type == "planar")
00133   {
00134     data.Dimensions.Planar.Faces.erase(
00135       data.Dimensions.Planar.Faces.begin() + face_idx);
00136   }
00137   
00138   MetaFile::saveFile(meta_file, data);
00139 
00140   ROS_INFO("Done. Face %d of object %s has been deleted", face_idx, 
00141     model.Name.c_str());
00142 }
00143 
00144 // ---------------------------------------------------------------------------
00145 
00146 


re_vision
Author(s): Dorian Galvez-Lopez
autogenerated on Sun Jan 5 2014 11:32:21