import_nvm.h
Go to the documentation of this file.
00001 /****************************************************************************
00002 * VCGLib                                                            o o     *
00003 * Visual and Computer Graphics Library                            o     o   *
00004 *                                                                _   O  _   *
00005 * Copyright(C) 2004                                                \/)\/    *
00006 * Visual Computing Lab                                            /\/|      *
00007 * ISTI - Italian National Research Council                           |      *
00008 *                                                                    \      *
00009 * All rights reserved.                                                      *
00010 *                                                                           *
00011 * This program is free software; you can redistribute it and/or modify      *
00012 * it under the terms of the GNU General Public License as published by      *
00013 * the Free Software Foundation; either version 2 of the License, or         *
00014 * (at your option) any later version.                                       *
00015 *                                                                           *
00016 * This program is distributed in the hope that it will be useful,           *
00017 * but WITHOUT ANY WARRANTY; without even the implied warranty of            *
00018 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             *
00019 * GNU General Public License (http://www.gnu.org/licenses/gpl.txt)          *
00020 * for more details.                                                         *
00021 *                                                                           *
00022 ****************************************************************************/
00023 #ifndef __VCGLIB_IMPORTERNVM
00024 #define __VCGLIB_IMPORTERNVM
00025 
00026 #include <stddef.h>
00027 #include <stdio.h>
00028 #include <vcg/complex/complex.h>
00029 //#include <vcg/complex/allocate.h>
00030 #include <vcg/complex/algorithms/update/bounding.h>
00031 #include <wrap/callback.h>
00032 #include <wrap/io_trimesh/io_mask.h>
00033 
00034 #include <QString>
00035 #include <QImageReader>
00036 
00037 namespace vcg {
00038 namespace tri {
00039 namespace io {
00040 
00041   /*struct Correspondence{
00042                 Correspondence(unsigned int id_img_,unsigned int key_,float x_,float y_):id_img(id_img_),key(key_),x(x_),y(y_){}
00043     unsigned int id_img,key;
00044     float x;
00045     float y;
00046   };*/
00047 
00048         typedef std::vector<Correspondence> CorrVec;
00049 
00053 template <class OpenMeshType>
00054 class ImporterNVM
00055 {
00056 public:
00057 
00058 typedef typename OpenMeshType::VertexPointer VertexPointer;
00059   typedef typename OpenMeshType::ScalarType ScalarType;
00060   typedef typename OpenMeshType::CoordType CoordType;
00061 typedef typename OpenMeshType::VertexType VertexType;
00062 typedef typename OpenMeshType::FaceType FaceType;
00063 typedef typename OpenMeshType::VertexIterator VertexIterator;
00064 typedef typename OpenMeshType::FaceIterator FaceIterator;
00065 typedef typename OpenMeshType::EdgeIterator EdgeIterator;
00066 
00067 static void readline(FILE *fp, char *line, int max=1000){
00068     int i=0;
00069     char c;
00070     fscanf(fp, "%c", &c);
00071     while( (c!=10) && (c!=13) && (i<max-1) ){
00072         line[i++] = c;
00073         fscanf(fp, "%c", &c);
00074     }
00075     line[i] = '\0'; //end of string
00076 }
00077 
00078 static bool ReadHeader(FILE *fp,unsigned int &num_cams){
00079     char line[1000];
00080     readline(fp, line);
00081     if( (line[0]=='\0') || (0!=strcmp("NVM_V3 ", line)) ) return false;
00082     readline(fp, line);
00083     readline(fp, line); if(line[0]=='\0') return false;
00084     sscanf(line, "%d", &num_cams);
00085     return true;
00086 }
00087 
00088 static bool ReadHeader(const char * filename,unsigned int &/*num_cams*/, unsigned int &/*num_points*/){
00089     FILE *fp = fopen(filename, "r");
00090     if(!fp) return false;
00091     ReadHeader(fp);
00092     fclose(fp);
00093     return true;
00094 }
00095 
00096 
00097 static int Open( OpenMeshType &m, std::vector<Shot<ScalarType> >  & shots,
00098                  std::vector<std::string > & image_filenames,
00099                  const char * filename, CallBackPos *cb=0)
00100 {
00101   unsigned int   num_cams,num_points;
00102 
00103   FILE *fp = fopen(filename,"r");
00104   if(!fp) return false;
00105   ReadHeader(fp, num_cams);
00106   char line[1000], name[1000];
00107  /* if(cb) cb(0,"Reading images");
00108   ReadImagesFilenames(filename_images, image_filenames);*/
00109 
00110   if(cb) cb(50,"Reading cameras");
00111   //image_filenames.resize(num_cams);
00112   shots.resize(num_cams);
00113   for(uint i = 0; i < num_cams;++i)
00114   {
00115     float f, k1;
00116     vcg::Point4f R;
00117     vcg::Point3f t;
00118 
00119     readline(fp, line); if(line[0]=='\0') return false; sscanf(line, "%s %f %f %f %f %f %f %f %f %f", name, &f, &(R[0]), &(R[1]), &(R[2]), &(R[3]), &(t[0]), &(t[1]), &(t[2]), &k1);
00120 
00121     std::string n(name);
00122     image_filenames.push_back(n);
00123 
00124     /*readline(fp, line); if(line[0]=='\0') return false; sscanf(line, "%f %f %f", &f, &k1, &k2);
00125 
00126     readline(fp, line); if(line[0]=='\0') return false; sscanf(line, "%f %f %f", &(R[0]), &(R[1]), &(R[2]));  R[3] = 0;
00127     readline(fp, line); if(line[0]=='\0') return false; sscanf(line, "%f %f %f", &(R[4]), &(R[5]), &(R[6]));  R[7] = 0;
00128     readline(fp, line); if(line[0]=='\0') return false; sscanf(line, "%f %f %f", &(R[8]), &(R[9]), &(R[10])); R[11] = 0;
00129 
00130     readline(fp, line); if(line[0]=='\0') return false; sscanf(line, "%f %f %f", &(t[0]), &(t[1]), &(t[2]));*/
00131 
00132     //vcg::Matrix44f mat = vcg::Matrix44<vcg::Shotf::ScalarType>::Construct<float>(R);
00133 
00134     vcg::Quaternion<ScalarType> qfrom; qfrom.Import(R);
00135 
00136     vcg::Matrix44<ScalarType> mat; qfrom.ToMatrix(mat);
00137 
00138     /*vcg::Matrix33f Rt = vcg::Matrix33f( vcg::Matrix44f(mat), 3);
00139     Rt.Transpose();
00140 
00141     vcg::Point3f pos = Rt * vcg::Point3f(t[0], t[1], t[2]);*/
00142 
00143     mat[1][0]=-mat[1][0];
00144     mat[1][1]=-mat[1][1];
00145     mat[1][2]=-mat[1][2];
00146 
00147     mat[2][0]=-mat[2][0];
00148     mat[2][1]=-mat[2][1];
00149     mat[2][2]=-mat[2][2];
00150 
00151     shots[i].Extrinsics.SetTra(CoordType(t[0],t[1],t[2]));
00152     shots[i].Extrinsics.SetRot(mat);
00153 
00154     shots[i].Intrinsics.FocalMm    = f/100.0f;
00155     shots[i].Intrinsics.k[0] = 0.0;//k1; To be uncommented when distortion is taken into account reliably
00156     shots[i].Intrinsics.k[1] = 0.0;//k2;
00157     shots[i].Intrinsics.PixelSizeMm = vcg::Point2<ScalarType>(0.01,0.01);
00158     QImageReader sizeImg(QString::fromStdString(image_filenames[i]));
00159     QSize size=sizeImg.size();
00160     shots[i].Intrinsics.ViewportPx = vcg::Point2i(size.width(),size.height());
00161     shots[i].Intrinsics.CenterPx[0] = (int)((double)shots[i].Intrinsics.ViewportPx[0]/2.0f);
00162     shots[i].Intrinsics.CenterPx[1] = (int)((double)shots[i].Intrinsics.ViewportPx[1]/2.0f);
00163   }
00164   readline(fp, line);
00165   readline(fp, line); if(line[0]=='\0') return false;
00166     sscanf(line, "%d", &num_points);
00167 
00168   // load all correspondences
00169   typename OpenMeshType::template PerVertexAttributeHandle<CorrVec> ch = vcg::tri::Allocator<OpenMeshType>::template GetPerVertexAttribute<CorrVec>(m,"correspondences");
00170 
00171   typename OpenMeshType::VertexIterator vi = vcg::tri::Allocator<OpenMeshType>::AddVertices(m,num_points);
00172   for(uint i = 0; i < num_points;++i,++vi){
00173     float x,y,z;
00174     unsigned int r,g,b,i_cam, key_sift,n_corr;
00175     fscanf(fp,"%f %f %f ",&x,&y,&z);
00176     (*vi).P() = vcg::Point3<typename OpenMeshType::ScalarType>(x,y,z);
00177     fscanf(fp,"%d %d %d ",&r,&g,&b);
00178     (*vi).C() = vcg::Color4b(r,g,b,255);
00179 
00180     fscanf(fp,"%d ",&n_corr);
00181     for(uint j = 0; j < n_corr; ++j){
00182       fscanf(fp,"%d %d %f %f ",&i_cam,&key_sift,&x,&y);
00183       Correspondence corr(i_cam,key_sift,x,y);
00184       ch[i].push_back(corr);
00185     }
00186   }
00187   vcg::tri::UpdateBounding<OpenMeshType>::Box(m);
00188   fclose(fp);
00189 
00190   return (shots.size() == 0);
00191 }
00192 
00193 
00194 static bool ReadImagesFilenames(const char *  filename,std::vector<std::string> &image_filenames)
00195 {
00196     FILE * fp = fopen(filename,"r");
00197     if (!fp) return false;
00198     else
00199     {
00200         char line[1000], name[1000];
00201         while(!feof(fp)){
00202             readline(fp, line, 1000);
00203             if(line[0] == '\0') continue; //ignore empty lines (in theory, might happen only at end of file)
00204             sscanf(line, "%s", name);
00205             std::string n(name);
00206             image_filenames.push_back(n);
00207         }
00208     }
00209     fclose(fp);
00210         return true;
00211 }
00212 
00213 }; // end class
00214 
00215 
00216 
00217 } // end namespace tri
00218 } // end namespace io
00219 } // end namespace vcg
00220 
00221 #endif
00222 


shape_reconstruction
Author(s): Roberto Martín-Martín
autogenerated on Sat Jun 8 2019 18:31:57