| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 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 |
|
| 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 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
typedef std::vector<Correspondence> CorrVec; |
| 49 |
|
| 50 |
|
| 51 |
|
| 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'; |
| 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 |
|
| 101 |
|
| 102 |
|
| 103 |
if(cb) cb(50,"Reading cameras"); |
| 104 |
|
| 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 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
vcg::Quaternion<ScalarType> qfrom; qfrom.Import(R); |
| 128 |
|
| 129 |
vcg::Matrix44<ScalarType> mat; qfrom.ToMatrix(mat); |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 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; |
| 149 |
shots[i].Intrinsics.k[1] = 0.0; |
| 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 |
|
| 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; |
| 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 |
}; |
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
} |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
#endif |
| 219 |
|