PLYFile.cpp
Go to the documentation of this file.
00001 
00024 #include "PLYFile.h"
00025 #include <string>
00026 #include <vector>
00027 #include <fstream>
00028 #include <sstream>
00029 
00030 using namespace std;
00031 using namespace DVision::PMVS;
00032 
00033 // ---------------------------------------------------------------------------
00034 
00035 void PLYFile::readFile(const std::string &filename, 
00036   std::vector<PLYPoint>& points)
00037 {
00038   // Format:
00039   // ply
00040   // ... headers (ignored) ...
00041   // end_header
00042   // Points
00043   // 
00044   // Each point:
00045   // x y z nx ny nz r g b
00046   
00047   points.clear();
00048   points.reserve(5000);
00049   
00050   fstream f(filename.c_str(), ios::in);
00051   if(!f.is_open()) throw string("PLYFile: cannot read file ") + filename;
00052   
00053   string line;
00054   bool header = true;
00055   while(!f.eof())
00056   {
00057     getline(f, line);
00058     if(!f.eof() && !line.empty())
00059     {
00060       if(header){
00061         header = (line.compare("end_header") != 0);
00062       }else{
00063         // point
00064         points.push_back(PLYPoint());
00065         PLYPoint &p = points.back();
00066         
00067         stringstream ss(line);
00068         ss >> p.x >> p.y >> p.z >> p.nx >> p.ny >> p.nz >> p.r >> p.g >> p.b;
00069       }
00070     }
00071   }
00072   
00073   f.close();
00074   
00075 }
00076 
00077 // ---------------------------------------------------------------------------
00078 
00079 void PLYFile::saveFile(const std::string &filename, 
00080   const std::vector<PLYPoint>& points)
00081 {
00082   fstream f(filename.c_str(), ios::out);
00083   if(!f.is_open()) throw string("PLYFile: cannot open file ") + filename;
00084   
00085   // standard header
00086   f << "ply" << endl;
00087   f << "format ascii 1.0" << endl;
00088   f << "element vertex " << points.size() << endl;
00089   f << "property float x" << endl;
00090   f << "property float y" << endl;
00091   f << "property float z" << endl;
00092   f << "property float nx" << endl;
00093   f << "property float ny" << endl;
00094   f << "property float nz" << endl;
00095   f << "property uchar diffuse_red" << endl;
00096   f << "property uchar diffuse_green" << endl;
00097   f << "property uchar diffuse_blue" << endl;
00098   f << "end_header" << endl;
00099   
00100   f.precision(6);
00101   
00102   vector<PLYPoint>::const_iterator pit;
00103   for(pit = points.begin(); pit != points.end(); ++pit)
00104   {
00105     f << pit->x << " "
00106       << pit->y << " "
00107       << pit->z << " "
00108       << pit->nx << " "
00109       << pit->ny << " "
00110       << pit->nz << " "
00111       << pit->r << " "
00112       << pit->g << " "
00113       << pit->b << endl;
00114   }
00115   
00116   f.close();
00117 }
00118 
00119 // ---------------------------------------------------------------------------
00120 
00121 int PLYFile::getNumberOfPoints(const std::string &filename)
00122 {
00123   fstream f(filename.c_str(), ios::in);
00124   if(!f.is_open()) throw string("PLYFile: cannot open file ") + filename;
00125   
00126   // standard header
00127   string line;
00128   getline(f, line); // ply
00129   getline(f, line); // format ascii 1.0
00130   getline(f, line); // element vertex N
00131   
00132   int N;
00133   if(0 == sscanf(line.c_str(), "element vertex %d", &N))
00134     throw string("PLYFile: format not supported in ") + filename;
00135   
00136   return N;
00137 }
00138 
00139 // ---------------------------------------------------------------------------
00140 


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