2011-12-28 14:47:08 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* VCGLib o o *
|
|
|
|
* Visual and Computer Graphics Library o o *
|
|
|
|
* _ O _ *
|
|
|
|
* Copyright(C) 2004 \/)\/ *
|
|
|
|
* Visual Computing Lab /\/| *
|
|
|
|
* ISTI - Italian National Research Council | *
|
|
|
|
* \ *
|
|
|
|
* All rights reserved. *
|
|
|
|
* *
|
2012-01-15 20:38:58 +01:00
|
|
|
* This program is free software; you can redistribute it and/or modify *
|
2011-12-28 14:47:08 +01:00
|
|
|
* it under the terms of the GNU General Public License as published by *
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or *
|
|
|
|
* (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
* This program is distributed in the hope that it will be useful, *
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
* GNU General Public License (http://www.gnu.org/licenses/gpl.txt) *
|
|
|
|
* for more details. *
|
|
|
|
* *
|
|
|
|
****************************************************************************/
|
|
|
|
#ifndef __VCGLIB_IMPORTERBUNDLER
|
|
|
|
#define __VCGLIB_IMPORTERBUNDLER
|
|
|
|
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdio.h>
|
2013-11-28 16:06:28 +01:00
|
|
|
#include <vcg/complex/complex.h>
|
|
|
|
//#include <vcg/complex/allocate.h>
|
2011-12-28 15:14:17 +01:00
|
|
|
#include <vcg/complex/algorithms/update/bounding.h>
|
2011-12-28 14:47:08 +01:00
|
|
|
#include <wrap/callback.h>
|
|
|
|
#include <wrap/io_trimesh/io_mask.h>
|
2013-01-03 10:35:09 +01:00
|
|
|
#include <QImageReader>
|
2011-12-28 14:47:08 +01:00
|
|
|
|
2012-04-05 11:01:44 +02:00
|
|
|
extern "C"
|
|
|
|
{
|
|
|
|
#include <jhead.h>
|
2012-04-24 17:00:49 +02:00
|
|
|
int ReadJpegSections (FILE * infile, ReadMode_t ReadMode);
|
|
|
|
void ResetJpgfile(void);
|
2012-04-05 11:01:44 +02:00
|
|
|
}
|
2011-12-28 14:47:08 +01:00
|
|
|
|
|
|
|
namespace vcg {
|
|
|
|
namespace tri {
|
|
|
|
namespace io {
|
|
|
|
|
2012-01-15 20:38:58 +01:00
|
|
|
struct Correspondence{
|
2011-12-28 15:14:17 +01:00
|
|
|
Correspondence(unsigned int id_img_,unsigned int key_,float x_,float y_):id_img(id_img_),key(key_),x(x_),y(y_){}
|
2012-01-15 20:38:58 +01:00
|
|
|
unsigned int id_img,key;
|
|
|
|
float x;
|
|
|
|
float y;
|
|
|
|
};
|
2011-12-28 14:47:08 +01:00
|
|
|
|
2011-12-28 15:14:17 +01:00
|
|
|
typedef std::vector<Correspondence> CorrVec;
|
2011-12-28 14:47:08 +01:00
|
|
|
|
2012-01-15 20:38:58 +01:00
|
|
|
/**
|
2011-12-28 14:47:08 +01:00
|
|
|
This class encapsulate a filter for opening bundler file
|
|
|
|
*/
|
|
|
|
template <class OpenMeshType>
|
|
|
|
class ImporterOUT
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
typedef typename OpenMeshType::VertexPointer VertexPointer;
|
|
|
|
typedef typename OpenMeshType::ScalarType ScalarType;
|
2014-06-27 10:51:31 +02:00
|
|
|
typedef typename OpenMeshType::CoordType CoordType;
|
2011-12-28 14:47:08 +01:00
|
|
|
typedef typename OpenMeshType::VertexType VertexType;
|
|
|
|
typedef typename OpenMeshType::FaceType FaceType;
|
|
|
|
typedef typename OpenMeshType::VertexIterator VertexIterator;
|
|
|
|
typedef typename OpenMeshType::FaceIterator FaceIterator;
|
|
|
|
typedef typename OpenMeshType::EdgeIterator EdgeIterator;
|
|
|
|
|
2012-01-15 20:38:58 +01:00
|
|
|
static void readline(FILE *fp, char *line, int max=100){
|
2014-04-28 17:37:36 +02:00
|
|
|
fgets ( line, max, fp);
|
2011-12-28 14:47:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool ReadHeader(FILE *fp,unsigned int &num_cams, unsigned int &num_points){
|
2012-01-15 20:38:58 +01:00
|
|
|
char line[100];
|
2014-04-28 17:37:36 +02:00
|
|
|
readline(fp, line);
|
2014-07-01 11:46:46 +02:00
|
|
|
if( line[0]=='\0' ) return false;
|
2014-04-28 17:37:36 +02:00
|
|
|
line[18]='\0';
|
|
|
|
if(0!=strcmp("# Bundle file v0.3", line)) return false;
|
|
|
|
readline(fp, line);
|
|
|
|
if(line[0]=='\0') return false;
|
2011-12-28 14:47:08 +01:00
|
|
|
sscanf(line, "%d %d", &num_cams, &num_points);
|
2012-01-15 20:38:58 +01:00
|
|
|
return true;
|
2011-12-28 14:47:08 +01:00
|
|
|
}
|
|
|
|
|
2012-05-05 16:52:57 +02:00
|
|
|
static bool ReadHeader(const char * filename,unsigned int &/*num_cams*/, unsigned int &/*num_points*/){
|
2014-06-27 10:51:31 +02:00
|
|
|
FILE *fp = fopen(filename, "r");
|
|
|
|
if(!fp) return false;
|
|
|
|
ReadHeader(fp);
|
|
|
|
fclose(fp);
|
|
|
|
return true;
|
2011-12-28 14:47:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-01-15 20:38:58 +01:00
|
|
|
static int Open( OpenMeshType &m, std::vector<Shot<ScalarType> > & shots,
|
2012-05-05 16:52:57 +02:00
|
|
|
std::vector<std::string > & image_filenames,
|
2012-12-02 15:53:37 +01:00
|
|
|
const char * filename,const char * filename_images, CallBackPos *cb=0)
|
2011-12-28 14:47:08 +01:00
|
|
|
{
|
2012-05-05 16:52:57 +02:00
|
|
|
unsigned int num_cams,num_points;
|
2014-06-27 10:51:31 +02:00
|
|
|
typedef typename vcg::Matrix44<ScalarType> Matrix44x;
|
|
|
|
typedef typename vcg::Matrix33<ScalarType> Matrix33x;
|
2012-05-05 16:52:57 +02:00
|
|
|
FILE *fp = fopen(filename,"r");
|
|
|
|
if(!fp) return false;
|
|
|
|
ReadHeader(fp, num_cams, num_points);
|
|
|
|
char line[100];
|
|
|
|
if(cb) cb(0,"Reading images");
|
|
|
|
ReadImagesFilenames(filename_images, image_filenames);
|
2013-11-04 19:09:34 +01:00
|
|
|
const QString path_im = QFileInfo(filename_images).absolutePath()+QString("/");
|
2011-12-28 14:47:08 +01:00
|
|
|
|
2012-05-05 16:52:57 +02:00
|
|
|
if(cb) cb(50,"Reading cameras");
|
|
|
|
shots.resize(num_cams);
|
|
|
|
for(uint i = 0; i < num_cams;++i)
|
|
|
|
{
|
|
|
|
float f, k1, k2;
|
|
|
|
float R[16]={0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,1};
|
|
|
|
vcg::Point3f t;
|
2011-12-28 14:47:08 +01:00
|
|
|
|
2012-05-05 16:52:57 +02:00
|
|
|
readline(fp, line); if(line[0]=='\0') return false; sscanf(line, "%f %f %f", &f, &k1, &k2);
|
2011-12-28 14:47:08 +01:00
|
|
|
|
2012-05-05 16:52:57 +02:00
|
|
|
readline(fp, line); if(line[0]=='\0') return false; sscanf(line, "%f %f %f", &(R[0]), &(R[1]), &(R[2])); R[3] = 0;
|
|
|
|
readline(fp, line); if(line[0]=='\0') return false; sscanf(line, "%f %f %f", &(R[4]), &(R[5]), &(R[6])); R[7] = 0;
|
|
|
|
readline(fp, line); if(line[0]=='\0') return false; sscanf(line, "%f %f %f", &(R[8]), &(R[9]), &(R[10])); R[11] = 0;
|
2012-01-15 20:38:58 +01:00
|
|
|
|
2012-05-05 16:52:57 +02:00
|
|
|
readline(fp, line); if(line[0]=='\0') return false; sscanf(line, "%f %f %f", &(t[0]), &(t[1]), &(t[2]));
|
2011-12-28 14:47:08 +01:00
|
|
|
|
2014-06-27 10:51:31 +02:00
|
|
|
Matrix44x mat = Matrix44x::Construct(Matrix44f(R));
|
2011-12-28 14:47:08 +01:00
|
|
|
|
2014-06-27 10:51:31 +02:00
|
|
|
Matrix33x Rt = Matrix33x( Matrix44x(mat), 3);
|
2012-05-05 16:52:57 +02:00
|
|
|
Rt.Transpose();
|
2011-12-28 14:47:08 +01:00
|
|
|
|
2014-06-27 10:51:31 +02:00
|
|
|
CoordType pos = Rt * CoordType(t[0], t[1], t[2]);
|
2011-12-28 14:47:08 +01:00
|
|
|
|
2014-06-27 10:51:31 +02:00
|
|
|
shots[i].Extrinsics.SetTra(CoordType(-pos[0],-pos[1],-pos[2]));
|
2012-05-05 16:52:57 +02:00
|
|
|
shots[i].Extrinsics.SetRot(mat);
|
2011-12-28 14:47:08 +01:00
|
|
|
|
2012-05-05 16:52:57 +02:00
|
|
|
shots[i].Intrinsics.FocalMm = f;
|
|
|
|
shots[i].Intrinsics.k[0] = 0.0;//k1; To be uncommented when distortion is taken into account reliably
|
|
|
|
shots[i].Intrinsics.k[1] = 0.0;//k2;
|
2014-06-27 10:51:31 +02:00
|
|
|
shots[i].Intrinsics.PixelSizeMm = vcg::Point2<ScalarType>(1,1);
|
|
|
|
QSize size;
|
|
|
|
QImageReader sizeImg(QString::fromStdString(image_filenames[i]));
|
|
|
|
if(sizeImg.size()==QSize(-1,-1))
|
|
|
|
{
|
|
|
|
QImageReader sizeImg(QString::fromStdString(qPrintable(path_im)+image_filenames[i]));
|
|
|
|
size=sizeImg.size();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
size=sizeImg.size();
|
|
|
|
shots[i].Intrinsics.ViewportPx = vcg::Point2i(size.width(),size.height());
|
|
|
|
shots[i].Intrinsics.CenterPx[0] = (int)((double)shots[i].Intrinsics.ViewportPx[0]/2.0f);
|
|
|
|
shots[i].Intrinsics.CenterPx[1] = (int)((double)shots[i].Intrinsics.ViewportPx[1]/2.0f);
|
2012-11-13 18:41:46 +01:00
|
|
|
//AddIntrinsics(shots[i], std::string(filename_images_path).append(image_filenames[i]).c_str());
|
2012-05-05 16:52:57 +02:00
|
|
|
}
|
2011-12-28 14:47:08 +01:00
|
|
|
|
2012-01-15 20:38:58 +01:00
|
|
|
// load all correspondences
|
2012-05-05 16:52:57 +02:00
|
|
|
typename OpenMeshType::template PerVertexAttributeHandle<CorrVec> ch = vcg::tri::Allocator<OpenMeshType>::template GetPerVertexAttribute<CorrVec>(m,"correspondences");
|
|
|
|
|
|
|
|
typename OpenMeshType::VertexIterator vi = vcg::tri::Allocator<OpenMeshType>::AddVertices(m,num_points);
|
|
|
|
for(uint i = 0; i < num_points;++i,++vi){
|
2014-06-27 10:51:31 +02:00
|
|
|
double x,y,z;
|
2012-05-05 16:52:57 +02:00
|
|
|
unsigned int r,g,b,i_cam, key_sift,n_corr;
|
2014-06-27 10:51:31 +02:00
|
|
|
fscanf(fp,"%lf %lf %lf ",&x,&y,&z);
|
2012-05-05 16:52:57 +02:00
|
|
|
(*vi).P() = vcg::Point3<typename OpenMeshType::ScalarType>(x,y,z);
|
|
|
|
fscanf(fp,"%d %d %d ",&r,&g,&b);
|
|
|
|
(*vi).C() = vcg::Color4b(r,g,b,255);
|
|
|
|
|
|
|
|
fscanf(fp,"%d ",&n_corr);
|
|
|
|
for(uint j = 0; j < n_corr; ++j){
|
2014-07-01 11:46:46 +02:00
|
|
|
fscanf(fp,"%d %d %lf %lf ",&i_cam,&key_sift,&x,&y);
|
2012-05-05 16:52:57 +02:00
|
|
|
Correspondence corr(i_cam,key_sift,x,y);
|
|
|
|
ch[i].push_back(corr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
vcg::tri::UpdateBounding<OpenMeshType>::Box(m);
|
|
|
|
fclose(fp);
|
2011-12-28 14:47:08 +01:00
|
|
|
|
2012-05-05 16:52:57 +02:00
|
|
|
return (shots.size() == 0);
|
2011-12-28 14:47:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static bool ReadImagesFilenames(const char * filename,std::vector<std::string> &image_filenames)
|
|
|
|
{
|
2014-06-27 10:51:31 +02:00
|
|
|
FILE * fp = fopen(filename,"r");
|
|
|
|
if (!fp) return false;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
char line[1000], name[1000];
|
|
|
|
while(!feof(fp)){
|
|
|
|
readline(fp, line, 1000);
|
|
|
|
if(line[0] == '\0') continue; //ignore empty lines (in theory, might happen only at end of file)
|
|
|
|
sscanf(line, "%s", name);
|
|
|
|
std::string n(name);
|
|
|
|
image_filenames.push_back(n);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fclose(fp);
|
|
|
|
return true;
|
2011-12-28 14:47:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool AddIntrinsics(vcg::Shotf &shot, const char * image_file)
|
|
|
|
{
|
2014-06-27 10:51:31 +02:00
|
|
|
::ResetJpgfile();
|
|
|
|
FILE * pFile = fopen(image_file, "rb");
|
|
|
|
int ret = ::ReadJpegSections (pFile, READ_METADATA);
|
|
|
|
fclose(pFile);
|
|
|
|
if(ret==0) return false;
|
|
|
|
shot.Intrinsics.ViewportPx = vcg::Point2i(ImageInfo.Width, ImageInfo.Height);
|
|
|
|
shot.Intrinsics.CenterPx = vcg::Point2f(float(ImageInfo.Width/2.0), float(ImageInfo.Height/2.0));
|
|
|
|
|
|
|
|
return true;
|
2011-12-28 14:47:08 +01:00
|
|
|
}
|
|
|
|
}; // end class
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} // end namespace tri
|
|
|
|
} // end namespace io
|
|
|
|
} // end namespace vcg
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|