Debian Codegraph @

src/vcglib/wrap/io_trimesh/import_nvm.h

raw symbols SCIP
1 /****************************************************************************
2 * VCGLib o o *
3 * Visual and Computer Graphics Library o o *
4 * _ O _ *
5 * Copyright(C) 2004-2016 \/)\/ *
6 * Visual Computing Lab /\/| *
7 * ISTI - Italian National Research Council | *
8 * \ *
9 * All rights reserved. *
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 * This program is distributed in the hope that it will be useful, *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
19 * GNU General Public License (http://www.gnu.org/licenses/gpl.txt) *
20 * for more details. *
21 * *
22 ****************************************************************************/
23 #ifndef __VCGLIB_IMPORTERNVM
24 #define __VCGLIB_IMPORTERNVM
25
26 #include <stddef.h>
27 #include <stdio.h>
28 #include <vcg/complex/complex.h>
29 //#include <vcg/complex/allocate.h>
30 #include <vcg/complex/algorithms/update/bounding.h>
31 #include <wrap/callback.h>
32 #include <wrap/io_trimesh/io_mask.h>
33
34 #include <QString>
35 #include <QImageReader>
36
37 namespace vcg {
38 namespace tri {
39 namespace io {
40
41 /*struct Correspondence{
42 Correspondence(unsigned int id_img_,unsigned int key_,float x_,float y_):id_img(id_img_),key(key_),x(x_),y(y_){}
43 unsigned int id_img,key;
44 float x;
45 float y;
46 };*/
47
48 typedef std::vector<Correspondence> CorrVec;
49
50 /**
51 This class encapsulate a filter for opening bundler file
52 */
53 template <class OpenMeshType>
54 class ImporterNVM
55 {
56 public:
57
58 typedef typename OpenMeshType::VertexPointer VertexPointer;
59 typedef typename OpenMeshType::ScalarType ScalarType;
60 typedef typename OpenMeshType::CoordType CoordType;
61 typedef typename OpenMeshType::VertexType VertexType;
62 typedef typename OpenMeshType::FaceType FaceType;
63 typedef typename OpenMeshType::VertexIterator VertexIterator;
64 typedef typename OpenMeshType::FaceIterator FaceIterator;
65 typedef typename OpenMeshType::EdgeIterator EdgeIterator;
66
67 static void readline(FILE *fp, char *line, int max=1000){
68 int i=0;
69 char c;
70 unsigned int rc = 0;
71 rc = fscanf(fp, "%c", &c);
72 while( (c!=10) && (c!=13) && (i<max-1) ){
73 if (rc == 1)
74 line[i++] = c;
75 rc = fscanf(fp, "%c", &c);
76 }
77 line[i] = '\0'; //end of string
78 }
79
80 static bool ReadHeader(FILE *fp,unsigned int &num_cams){
81 char line[1000];
82 readline(fp, line);
83 if( (line[0]=='\0') || (0!=strcmp("NVM_V3 ", line)) ) return false;
84 readline(fp, line);
85 readline(fp, line); if(line[0]=='\0') return false;
86 sscanf(line, "%d", &num_cams);
87 return true;
88 }
89
90 static int Open( OpenMeshType &m, std::vector<Shot<ScalarType> > & shots,
91 std::vector<std::string > & image_filenames,
92 const char * filename, CallBackPos *cb=0)
93 {
94 unsigned int num_cams,num_points;
95
96 FILE *fp = fopen(filename,"r");
97 if(!fp) return false;
98 ReadHeader(fp, num_cams);
99 char line[1000], name[1000];
100 /* if(cb) cb(0,"Reading images");
101 ReadImagesFilenames(filename_images, image_filenames);*/
102
103 if(cb) cb(50,"Reading cameras");
104 //image_filenames.resize(num_cams);
105 shots.resize(num_cams);
106 for(uint i = 0; i < num_cams;++i)
107 {
108 float f, k1;
109 vcg::Point4f R;
110 vcg::Point3f t;
111
112 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);
113
114 std::string n(name);
115 image_filenames.push_back(n);
116
117 /*readline(fp, line); if(line[0]=='\0') return false; sscanf(line, "%f %f %f", &f, &k1, &k2);
118
119 readline(fp, line); if(line[0]=='\0') return false; sscanf(line, "%f %f %f", &(R[0]), &(R[1]), &(R[2])); R[3] = 0;
120 readline(fp, line); if(line[0]=='\0') return false; sscanf(line, "%f %f %f", &(R[4]), &(R[5]), &(R[6])); R[7] = 0;
121 readline(fp, line); if(line[0]=='\0') return false; sscanf(line, "%f %f %f", &(R[8]), &(R[9]), &(R[10])); R[11] = 0;
122
123 readline(fp, line); if(line[0]=='\0') return false; sscanf(line, "%f %f %f", &(t[0]), &(t[1]), &(t[2]));*/
124
125 //vcg::Matrix44f mat = vcg::Matrix44<vcg::Shotf::ScalarType>::Construct<float>(R);
126
127 vcg::Quaternion<ScalarType> qfrom; qfrom.Import(R);
128
129 vcg::Matrix44<ScalarType> mat; qfrom.ToMatrix(mat);
130
131 /*vcg::Matrix33f Rt = vcg::Matrix33f( vcg::Matrix44f(mat), 3);
132 Rt.Transpose();
133
134 vcg::Point3f pos = Rt * vcg::Point3f(t[0], t[1], t[2]);*/
135
136 mat[1][0]=-mat[1][0];
137 mat[1][1]=-mat[1][1];
138 mat[1][2]=-mat[1][2];
139
140 mat[2][0]=-mat[2][0];
141 mat[2][1]=-mat[2][1];
142 mat[2][2]=-mat[2][2];
143
144 shots[i].Extrinsics.SetTra(CoordType(t[0],t[1],t[2]));
145 shots[i].Extrinsics.SetRot(mat);
146
147 shots[i].Intrinsics.FocalMm = f/100.0f;
148 shots[i].Intrinsics.k[0] = 0.0;//k1; To be uncommented when distortion is taken into account reliably
149 shots[i].Intrinsics.k[1] = 0.0;//k2;
150 shots[i].Intrinsics.PixelSizeMm = vcg::Point2<ScalarType>(ScalarType(0.01), ScalarType(0.01));
151 QImageReader sizeImg(QString::fromStdString(image_filenames[i]));
152 QSize size=sizeImg.size();
153 shots[i].Intrinsics.ViewportPx = vcg::Point2i(size.width(),size.height());
154 shots[i].Intrinsics.CenterPx[0] = (int)((double)shots[i].Intrinsics.ViewportPx[0]/2.0f);
155 shots[i].Intrinsics.CenterPx[1] = (int)((double)shots[i].Intrinsics.ViewportPx[1]/2.0f);
156 }
157 readline(fp, line);
158 readline(fp, line); if(line[0]=='\0') return false;
159 sscanf(line, "%d", &num_points);
160
161 // load all correspondences
162 typename OpenMeshType::template PerVertexAttributeHandle<CorrVec> ch = vcg::tri::Allocator<OpenMeshType>::template GetPerVertexAttribute<CorrVec>(m,"correspondences");
163
164 typename OpenMeshType::VertexIterator vi = vcg::tri::Allocator<OpenMeshType>::AddVertices(m,num_points);
165 for(uint i = 0; i < num_points;++i,++vi){
166 float x,y,z;
167 unsigned int r,g,b,i_cam, key_sift,n_corr;
168 uint readValues = fscanf(fp,"%f %f %f ",&x,&y,&z);
169 if (readValues < 3) std::cerr << "Point " << i << ": only " << readValues << " coordinates read!";
170 (*vi).P() = vcg::Point3<typename OpenMeshType::ScalarType>(x,y,z);
171 readValues = fscanf(fp,"%d %d %d ",&r,&g,&b);
172 if (readValues < 3) std::cerr << "Point " << i << ": only " << readValues << " color values read!";
173 (*vi).C() = vcg::Color4b(r,g,b,255);
174
175 readValues = fscanf(fp,"%d ",&n_corr);
176 if (readValues < 1) std::cerr << "Point " << i << ": no n correspondences read!";
177 for(uint j = 0; j < n_corr; ++j){
178 readValues = fscanf(fp,"%d %d %f %f ",&i_cam,&key_sift,&x,&y);
179 if (readValues != 3) std::cerr << "Point " << i << "; Corresp: " << j << ": only " << readValues << " values read!";
180 Correspondence corr(i_cam,key_sift,x,y);
181 ch[i].push_back(corr);
182 }
183 }
184 vcg::tri::UpdateBounding<OpenMeshType>::Box(m);
185 fclose(fp);
186
187 return (shots.size() == 0);
188 }
189
190
191 static bool ReadImagesFilenames(const char * filename,std::vector<std::string> &image_filenames)
192 {
193 FILE * fp = fopen(filename,"r");
194 if (!fp) return false;
195 else
196 {
197 char line[1000], name[1000];
198 while(!feof(fp)){
199 readline(fp, line, 1000);
200 if(line[0] == '\0') continue; //ignore empty lines (in theory, might happen only at end of file)
201 sscanf(line, "%s", name);
202 std::string n(name);
203 image_filenames.push_back(n);
204 }
205 }
206 fclose(fp);
207 return true;
208 }
209
210 }; // end class
211
212
213
214 } // end namespace tri
215 } // end namespace io
216 } // end namespace vcg
217
218 #endif
219