00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
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
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
00042
00043
00044
00045
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';
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 &, unsigned int &){
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
00108
00109
00110 if(cb) cb(50,"Reading cameras");
00111
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
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134 vcg::Quaternion<ScalarType> qfrom; qfrom.Import(R);
00135
00136 vcg::Matrix44<ScalarType> mat; qfrom.ToMatrix(mat);
00137
00138
00139
00140
00141
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;
00156 shots[i].Intrinsics.k[1] = 0.0;
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
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;
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 };
00214
00215
00216
00217 }
00218 }
00219 }
00220
00221 #endif
00222