PatchFile.cpp
Go to the documentation of this file.
00001 
00024 #include "PatchFile.h"
00025 #include <fstream>
00026 #include <iostream>
00027 #include <vector>
00028 #include <string>
00029 #include <sstream>
00030 
00031 using namespace std;
00032 using namespace DVision::PMVS;
00033 
00034 // ----------------------------------------------------------------------------
00035 
00036 void PatchFile::readFile(const std::string &filename, 
00037   std::vector<Patch> &patches)
00038 {
00039   fstream f(filename.c_str(), ios::in);
00040   if(!f.is_open()) throw string("PatchFile: cannot read file ") + filename;
00041   
00042   string line;
00043   getline(f, line); // PATCHES keyword
00044   getline(f, line); // number of patches
00045   const int N = PatchFile::parseInt(line);
00046 
00047   patches.resize(N);
00048   
00049   for(int i = 0; i < N; ++i)
00050   {
00051     Patch& patch = patches[i];
00052     
00053     getline(f, line); // PATCHS keyword
00054     getline(f, line); // 3d coords
00055         
00056     sscanf(line.c_str(), "%lf %lf %lf %lf", 
00057       &patch.x, &patch.y, &patch.z, &patch.s);
00058         
00059     getline(f, line); // normal vector
00060     sscanf(line.c_str(), "%lf %lf %lf %lf", 
00061       &patch.nx, &patch.ny, &patch.nz, &patch.ns);
00062             
00063     getline(f, line); // photometric consistency
00064     sscanf(line.c_str(), "%lf %lf %lf", 
00065       &patch.consistency, &patch.dbg1, &patch.dbg2);
00066           
00067     getline(f, line); // strong list length
00068     int n = PatchFile::parseInt(line);
00069     patch.strong_visibility_list.resize(n);
00070     getline(f, line); // strong list
00071     stringstream ss(line);
00072     for(int j = 0; j < n; ++j)
00073     {
00074       ss >> patch.strong_visibility_list[j];
00075     }
00076     
00077     getline(f, line); // weak list length
00078     n = PatchFile::parseInt(line);
00079     patch.weak_visibility_list.resize(n);
00080     getline(f, line); // weak list
00081     stringstream ss2(line);
00082     for(int j = 0; j < n; ++j)
00083     {
00084       ss2 >> patch.weak_visibility_list[j];
00085     }
00086     
00087     getline(f, line); // blank
00088   }
00089 
00090   f.close();
00091 }
00092 
00093 // ----------------------------------------------------------------------------
00094 
00095 void PatchFile::saveFile(const std::string &filename,
00096     const std::vector<Patch> &patches)
00097 {
00098   fstream f(filename.c_str(), ios::out);
00099   if(!f.is_open()) throw string("PatchFile: cannot write in ") + filename;
00100   
00101   f.precision(6);
00102   
00103   f << "PATCHES" << endl;
00104   f << patches.size() << endl;
00105 
00106   for(unsigned int i = 0; i < patches.size(); ++i)
00107   {
00108     const Patch& patch = patches[i];
00109     
00110     f << "PATCHS" << endl;
00111     f << patch.x << " " << patch.y << " " << patch.z << " " << patch.s << endl;
00112     f << patch.nx << " " << patch.ny << " " << patch.nz << " " << patch.ns << endl;
00113     f << patch.consistency << " " << patch.dbg1 << " " << patch.dbg2 << endl;
00114     
00115     f << patch.strong_visibility_list.size() << endl;
00116     for(unsigned int j = 0; j < patch.strong_visibility_list.size(); ++j)
00117     {
00118       f << patch.strong_visibility_list[j] << " ";
00119     }
00120     f << endl;
00121     
00122     f << patch.weak_visibility_list.size() << endl;
00123     for(unsigned int j = 0; j < patch.weak_visibility_list.size(); ++j)
00124     {
00125       f << patch.weak_visibility_list[j] << " ";
00126     }
00127     f << endl;
00128     
00129     f << endl; // blank line 
00130   }
00131   
00132   f.close();
00133 }
00134 
00135 // ----------------------------------------------------------------------------
00136 
00137 void PatchFile::readFile(const std::string &filename, 
00138     std::vector<std::vector<int> > &visibility, bool use_weak_list)
00139 {
00140   visibility.resize(0);
00141   
00142   fstream f(filename.c_str(), ios::in);
00143   if(!f.is_open()) throw string("PatchFile: cannot read file ") + filename;
00144   
00145   string line;
00146   
00147   getline(f, line); // header "PATCHES"
00148   getline(f, line); // number of patches
00149   
00150   int N;
00151   sscanf(line.c_str(), "%d", &N);
00152   
00153   visibility.reserve(N);
00154   
00155   for(int pt_idx = 0; pt_idx < N; ++pt_idx)
00156   {
00157     getline(f, line); // header "PATCHS"
00158     getline(f, line); // point in homogeneous coordinates
00159     getline(f, line); // surface normal
00160     getline(f, line); // photometric consistency info
00161 
00162     // number of images with visibility and consistency
00163     PatchFile::readVisibilityIndices(f, visibility, pt_idx);
00164 
00165     if(use_weak_list)
00166     {
00167       // use also the visibility w/o consistency list
00168       PatchFile::readVisibilityIndices(f, visibility, pt_idx);
00169     }else{
00170       // ignore those indices
00171       getline(f, line); // number of images with visibility but w/o consistency
00172       getline(f, line); // list of indices
00173     }
00174 
00175     getline(f, line); // blank
00176   }
00177     
00178   f.close();
00179 }
00180 
00181 // ----------------------------------------------------------------------------
00182 
00183 void PatchFile::readVisibilityIndices(std::fstream &f, 
00184     std::vector<std::vector<int> > &visibility, int pt_idx)
00185 {
00186   std::string line;
00187   getline(f, line); // number of indices
00188   
00189   int n;
00190   sscanf(line.c_str(), "%d", &n);
00191     
00192   getline(f, line); // list of indices
00193   stringstream ss(line);
00194   
00195   for(int j = 0; j < n; ++j)
00196   {
00197     int img_idx;
00198     ss >> img_idx;
00199     
00200     if((int)visibility.size() <= img_idx)
00201     {
00202       visibility.resize(img_idx+1);
00203     }
00204     
00205     visibility[img_idx].push_back(pt_idx);
00206   }
00207 }
00208 
00209 // ----------------------------------------------------------------------------
00210 
00211 int PatchFile::parseInt(const std::string &s)
00212 {
00213   stringstream ss(s);
00214   int n;
00215   ss >> n;
00216   return n;
00217 }
00218 
00219 // ----------------------------------------------------------------------------
00220 
00221 int PatchFile::getNumberOfPoints(const std::string &filename)
00222 {
00223   fstream f(filename.c_str(), ios::in);
00224   if(!f.is_open()) throw string("PatchFile: cannot read file ") + filename;
00225   
00226   string line;
00227   
00228   getline(f, line); // header "PATCHES"
00229   getline(f, line); // number of patches
00230   
00231   int N;
00232   sscanf(line.c_str(), "%d", &N);
00233   
00234   f.close();
00235   
00236   return N;
00237 }
00238 
00239 // ----------------------------------------------------------------------------
00240 


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