| 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_IMPORTERBUNDLER |
| 24 |
#define __VCGLIB_IMPORTERBUNDLER |
| 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 |
#include <QImageReader> |
| 34 |
#include <QFileInfo> |
| 35 |
#include "exif.h" |
| 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 |
|
| 52 |
|
| 53 |
template <class OpenMeshType> |
| 54 |
class ImporterOUT |
| 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=100){ |
| 68 |
fgets ( line, max, fp); |
| 69 |
} |
| 70 |
|
| 71 |
static bool ReadHeader(FILE *fp,unsigned int &num_cams, unsigned int &num_points){ |
| 72 |
char line[100]; |
| 73 |
readline(fp, line); |
| 74 |
if( line[0]=='\0' ) return false; |
| 75 |
line[18]='\0'; |
| 76 |
if(0!=strcmp("# Bundle file v0.3", line)) return false; |
| 77 |
readline(fp, line); |
| 78 |
if(line[0]=='\0') return false; |
| 79 |
if (sscanf(line, "%d %d", &num_cams, &num_points) != 2) return false; |
| 80 |
return true; |
| 81 |
} |
| 82 |
|
| 83 |
static int Open( |
| 84 |
OpenMeshType &m, |
| 85 |
std::vector<Shot<ScalarType> > & shots, |
| 86 |
std::vector<std::string > & image_filenames, |
| 87 |
const char * filename, |
| 88 |
const char * filename_images, |
| 89 |
CallBackPos *cb=0) |
| 90 |
{ |
| 91 |
unsigned int num_cams,num_points; |
| 92 |
typedef typename vcg::Matrix44<ScalarType> Matrix44x; |
| 93 |
typedef typename vcg::Matrix33<ScalarType> Matrix33x; |
| 94 |
FILE *fp = fopen(filename,"r"); |
| 95 |
if(!fp) return false; |
| 96 |
ReadHeader(fp, num_cams, num_points); |
| 97 |
char line[100]; |
| 98 |
if(cb) cb(0,"Reading images"); |
| 99 |
ReadImagesFilenames(filename_images, image_filenames); |
| 100 |
const QString path_im = QFileInfo(filename_images).absolutePath()+QString("/"); |
| 101 |
|
| 102 |
if(cb) cb(50,"Reading cameras"); |
| 103 |
shots.resize(num_cams); |
| 104 |
for(uint i = 0; i < num_cams;++i) |
| 105 |
{ |
| 106 |
float f, k1, k2; |
| 107 |
float R[16]={0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,1}; |
| 108 |
vcg::Point3f t; |
| 109 |
|
| 110 |
readline(fp, line); if(line[0]=='\0') return false; if (sscanf(line, "%f %f %f", &f, &k1, &k2) != 3) return false; |
| 111 |
|
| 112 |
readline(fp, line); if(line[0]=='\0') return false; if (sscanf(line, "%f %f %f", &(R[0]), &(R[1]), &(R[2])) != 3) return false; R[3] = 0; |
| 113 |
readline(fp, line); if(line[0]=='\0') return false; if (sscanf(line, "%f %f %f", &(R[4]), &(R[5]), &(R[6])) != 3) return false; R[7] = 0; |
| 114 |
readline(fp, line); if(line[0]=='\0') return false; if (sscanf(line, "%f %f %f", &(R[8]), &(R[9]), &(R[10])) != 3) return false; R[11] = 0; |
| 115 |
readline(fp, line); if(line[0]=='\0') return false; if (sscanf(line, "%f %f %f", &(t[0]), &(t[1]), &(t[2])) != 3) return false; |
| 116 |
|
| 117 |
Matrix44x mat = Matrix44x::Construct(Matrix44f(R)); |
| 118 |
|
| 119 |
Matrix33x Rt = Matrix33x( Matrix44x(mat), 3); |
| 120 |
Rt.Transpose(); |
| 121 |
|
| 122 |
CoordType pos = Rt * CoordType(t[0], t[1], t[2]); |
| 123 |
|
| 124 |
shots[i].Extrinsics.SetTra(CoordType(-pos[0],-pos[1],-pos[2])); |
| 125 |
shots[i].Extrinsics.SetRot(mat); |
| 126 |
|
| 127 |
shots[i].Intrinsics.FocalMm = f; |
| 128 |
shots[i].Intrinsics.k[0] = 0.0; |
| 129 |
shots[i].Intrinsics.k[1] = 0.0; |
| 130 |
shots[i].Intrinsics.PixelSizeMm = vcg::Point2<ScalarType>(1,1); |
| 131 |
QSize size; |
| 132 |
QImageReader sizeImg(QString::fromStdString(image_filenames[i])); |
| 133 |
if(sizeImg.size()==QSize(-1,-1)) |
| 134 |
{ |
| 135 |
QImageReader sizeImg(QString::fromStdString(qPrintable(path_im)+image_filenames[i])); |
| 136 |
size=sizeImg.size(); |
| 137 |
} |
| 138 |
else |
| 139 |
size=sizeImg.size(); |
| 140 |
shots[i].Intrinsics.ViewportPx = vcg::Point2i(size.width(),size.height()); |
| 141 |
shots[i].Intrinsics.CenterPx[0] = (int)((double)shots[i].Intrinsics.ViewportPx[0]/2.0f); |
| 142 |
shots[i].Intrinsics.CenterPx[1] = (int)((double)shots[i].Intrinsics.ViewportPx[1]/2.0f); |
| 143 |
|
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
typename OpenMeshType::template PerVertexAttributeHandle<CorrVec> ch = vcg::tri::Allocator<OpenMeshType>::template GetPerVertexAttribute<CorrVec>(m,"correspondences"); |
| 148 |
|
| 149 |
typename OpenMeshType::VertexIterator vi = vcg::tri::Allocator<OpenMeshType>::AddVertices(m,num_points); |
| 150 |
for(uint i = 0; i < num_points;++i,++vi){ |
| 151 |
double x,y,z; |
| 152 |
unsigned int r,g,b,i_cam, key_sift,n_corr; |
| 153 |
if (fscanf(fp,"%lf %lf %lf ",&x,&y,&z) != 3) return false; |
| 154 |
(*vi).P() = vcg::Point3<typename OpenMeshType::ScalarType>(x,y,z); |
| 155 |
if (fscanf(fp,"%d %d %d ",&r,&g,&b) != 3) return false; |
| 156 |
(*vi).C() = vcg::Color4b(r,g,b,255); |
| 157 |
|
| 158 |
if (fscanf(fp,"%d ",&n_corr) != 1) return false; |
| 159 |
for(uint j = 0; j < n_corr; ++j){ |
| 160 |
if (fscanf(fp,"%d %d %lf %lf ",&i_cam,&key_sift,&x,&y) != 4) return false; |
| 161 |
Correspondence corr(i_cam,key_sift,x,y); |
| 162 |
ch[i].push_back(corr); |
| 163 |
} |
| 164 |
} |
| 165 |
vcg::tri::UpdateBounding<OpenMeshType>::Box(m); |
| 166 |
fclose(fp); |
| 167 |
|
| 168 |
return (shots.size() == 0); |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
static bool ReadImagesFilenames(const char * filename,std::vector<std::string> &image_filenames) |
| 173 |
{ |
| 174 |
FILE * fp = fopen(filename,"r"); |
| 175 |
if (!fp) return false; |
| 176 |
else |
| 177 |
{ |
| 178 |
char line[1000], name[1000]; |
| 179 |
while(!feof(fp)){ |
| 180 |
readline(fp, line, 1000); |
| 181 |
if(line[0] == '\0') continue; |
| 182 |
if (sscanf(line, "%s", name) != 1) return false; |
| 183 |
std::string n(name); |
| 184 |
image_filenames.push_back(n); |
| 185 |
} |
| 186 |
} |
| 187 |
fclose(fp); |
| 188 |
return true; |
| 189 |
} |
| 190 |
|
| 191 |
static bool AddIntrinsics(vcg::Shotf &shot, const char * image_file) |
| 192 |
{ |
| 193 |
|
| 194 |
FILE *fp = fopen(qUtf8Printable(image_file), "rb"); |
| 195 |
if (!fp) { |
| 196 |
std::cerr << "Exif Parsing: Unable to open file:\n\"%1\"\n\nError details: file %1 is not readable."; |
| 197 |
return false; |
| 198 |
} |
| 199 |
fseek(fp, 0, SEEK_END); |
| 200 |
unsigned long fsize = ftell(fp); |
| 201 |
rewind(fp); |
| 202 |
unsigned char *buf = new unsigned char[fsize]; |
| 203 |
if (fread(buf, 1, fsize, fp) != fsize) { |
| 204 |
std::cerr << "Exif Parsing: Unable to read the content of the opened file:\n\"%1\"\n\nError details: file %1 is not readable."; |
| 205 |
delete[] buf; |
| 206 |
fclose(fp); |
| 207 |
return false; |
| 208 |
} |
| 209 |
fclose(fp); |
| 210 |
|
| 211 |
|
| 212 |
easyexif::EXIFInfo ImageInfo; |
| 213 |
int ret = ImageInfo.parseFrom(buf, fsize); |
| 214 |
delete[] buf; |
| 215 |
if (ret == 0) { |
| 216 |
std::cerr << "Warning unable to parse exif for file %s" << qPrintable(image_file); |
| 217 |
return false; |
| 218 |
} |
| 219 |
|
| 220 |
shot.Intrinsics.ViewportPx = vcg::Point2i(ImageInfo.ImageWidth, ImageInfo.ImageHeight); |
| 221 |
shot.Intrinsics.CenterPx = vcg::Point2f(float(ImageInfo.ImageWidth/2.0), float(ImageInfo.ImageHeight/2.0)); |
| 222 |
|
| 223 |
return true; |
| 224 |
} |
| 225 |
}; |
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
} |
| 230 |
} |
| 231 |
} |
| 232 |
|
| 233 |
#endif |
| 234 |
|