54 #include "FileBundle.hh" 64 #include <OpenMesh/Core/IO/IOManager.hh> 72 static const char IMAGELIST_SUFFIX[] =
".txt";
73 static const char IMAGELIST_FALLBACK[] =
"image_list.txt";
79 static std::string readLine( FILE *_file )
82 std::string result =
"";
88 fscanf( _file,
"%c", &c );
91 if( feof( _file ) || c ==
'\0' || c ==
'\n' || c ==
'\r' )
106 static void splitFilename(
const std::string &_str, std::string &_path, std::string &_name, std::string &_ext )
108 size_t i = _str.find_last_of(
"/\\" );
112 if( i == std::string::npos )
119 _path = _str.substr( 0, i+1 );
120 rest = _str.substr( i+1 );
123 size_t j = rest.find_last_of(
'.' );
125 if( j == std::string::npos )
132 _name = rest.substr( 0, j );
133 _ext = rest.substr( j );
141 bool FileBundlePlugin::addEmptyObjects(
unsigned int _num,
const DataType &_dataType, std::vector<int> &_objectIDs )
143 deleteObjects( _objectIDs );
144 _objectIDs.reserve( _num );
146 OpenFlipper::Options::blockSceneGraphUpdates();
149 for( i=0; i<_num; ++i )
152 emit addEmptyObject( _dataType, objectId );
157 _objectIDs.push_back( objectId );
160 OpenFlipper::Options::unblockSceneGraphUpdates();
165 deleteObjects( _objectIDs );
173 void FileBundlePlugin::deleteObjects( std::vector<int> &_objectIDs )
175 unsigned int i, num = _objectIDs.size();
176 for( i=0; i<num; ++i )
177 emit deleteObject( _objectIDs[ i ] );
186 bool FileBundlePlugin::readImagelistFile(
const char *_filename, std::vector<std::string> &_imagePaths )
190 FILE *file = fopen( _filename,
"rt" );
193 emit log(
LOGINFO, tr(
"Could not open imagelist file \"%1\".\n").arg( _filename ) );
202 fscanf( file,
"%4095s", path );
203 fscanf( file,
"%31s", temp );
204 fscanf( file,
"%31s", temp );
209 _imagePaths.push_back( std::string( path ) );
214 emit log(
LOGINFO, tr(
"Using imagelist file \"%1\".\n").arg( _filename ) );
222 void FileBundlePlugin::readCameras( FILE *_file,
const std::vector<int> &_cameraObjectIDs, SplatCloud_Cameras &_cameras )
226 unsigned int cameraIdx = 0;
227 SplatCloud_Cameras::iterator cameraIter;
228 for( cameraIter = _cameras.begin(); cameraIter != _cameras.end(); ++cameraIter, ++cameraIdx )
232 camera.objectId_ = _cameraObjectIDs[ cameraIdx ];
235 fscanf( _file,
"%32s", str ); proj.f_ = atof( str );
236 fscanf( _file,
"%32s", str ); proj.k1_ = atof( str );
237 fscanf( _file,
"%32s", str ); proj.k2_ = atof( str );
238 fscanf( _file,
"%32s", str ); proj.r_[0][0] = atof( str );
239 fscanf( _file,
"%32s", str ); proj.r_[0][1] = atof( str );
240 fscanf( _file,
"%32s", str ); proj.r_[0][2] = atof( str );
241 fscanf( _file,
"%32s", str ); proj.r_[1][0] = atof( str );
242 fscanf( _file,
"%32s", str ); proj.r_[1][1] = atof( str );
243 fscanf( _file,
"%32s", str ); proj.r_[1][2] = atof( str );
244 fscanf( _file,
"%32s", str ); proj.r_[2][0] = atof( str );
245 fscanf( _file,
"%32s", str ); proj.r_[2][1] = atof( str );
246 fscanf( _file,
"%32s", str ); proj.r_[2][2] = atof( str );
247 fscanf( _file,
"%32s", str ); proj.t_[0] = atof( str );
248 fscanf( _file,
"%32s", str ); proj.t_[1] = atof( str );
249 fscanf( _file,
"%32s", str ); proj.t_[2] = atof( str );
251 camera.imagePath_ =
"";
253 camera.imageWidth_ = 0;
254 camera.imageHeight_ = 0;
262 void FileBundlePlugin::readPoints( FILE *_file,
const std::vector<int> &_cameraObjectIDs,
SplatCloud &_splatCloud )
266 int maxCamObjId = _cameraObjectIDs.size() - 1;
268 unsigned int splatIdx;
269 for( splatIdx = 0; splatIdx < _splatCloud.
numSplats(); ++splatIdx )
273 fscanf( _file,
"%32s", str ); pos[0] = atof( str );
274 fscanf( _file,
"%32s", str ); pos[1] = atof( str );
275 fscanf( _file,
"%32s", str ); pos[2] = atof( str );
280 unsigned int r=0, g=0, b=0;
281 fscanf( _file,
"%16u", &r ); col[0] = r;
282 fscanf( _file,
"%16u", &g ); col[1] = g;
283 fscanf( _file,
"%16u", &b ); col[2] = b;
287 SplatCloud::Viewlist &viewlist = _splatCloud.
viewlists( splatIdx );
289 unsigned int numEntries = 0;
290 fscanf( _file,
"%16u", &numEntries );
292 viewlist.resize( numEntries );
294 SplatCloud::Viewlist::iterator viewIter;
295 for( viewIter = viewlist.begin(); viewIter != viewlist.end(); ++viewIter )
299 fscanf( _file,
"%16i", &i ); viewIter->cameraObjectId_ = ((i >= 0) && (i <= maxCamObjId)) ? _cameraObjectIDs[ i ] : -1;
300 fscanf( _file,
"%16i", &j ); viewIter->featureIdx_ = j;
301 fscanf( _file,
"%32s", str ); viewIter->x_ = atof( str );
302 fscanf( _file,
"%32s", str ); viewIter->y_ = atof( str );
312 bool FileBundlePlugin::readBundleFile(
const char *_filename,
SplatCloud &_splatCloud )
318 FILE *file = fopen( _filename,
"rt" );
321 emit log(
LOGERR, tr(
"Could not open input file \"%1\".\n").arg( _filename ) );
326 std::string magicAndVersion = readLine( file );
327 if( magicAndVersion.compare(
"# Bundle file v0.3" ) != 0 )
329 emit log(
LOGERR, tr(
"Bad magic/version \"%1\" in input file \"%2\".\n").arg( magicAndVersion.c_str(), _filename ) );
335 unsigned int numCameras = 0;
336 unsigned int numPoints = 0;
337 fscanf( file,
"%16u", &numCameras );
338 fscanf( file,
"%16u", &numPoints );
341 std::vector<int> cameraObjectIDs;
342 if( !addEmptyObjects( numCameras,
DATA_CAMERA, cameraObjectIDs ) )
344 emit log(
LOGERR, tr(
"Unable to add %1 cameras for input file \"%2\".\n").arg( QString::number( numCameras ), _filename ) );
350 if( numCameras != 0 )
354 cameraManager.cameras_.resize( numCameras );
355 readCameras( file, cameraObjectIDs, cameraManager.cameras_ );
359 std::vector<std::string> imagePaths;
361 std::string path,
name, ext;
362 splitFilename( _filename, path, name, ext );
364 if( !readImagelistFile( (path + name + IMAGELIST_SUFFIX).c_str(), imagePaths ) )
365 readImagelistFile( (path + IMAGELIST_FALLBACK ).c_str(), imagePaths );
367 bool hasImg = (cameraManager.cameras_.size() <= imagePaths.size());
371 unsigned int cameraIdx = 0;
372 SplatCloud_Cameras::iterator cameraIter;
373 for( cameraIter = cameraManager.cameras_.begin(); cameraIter != cameraManager.cameras_.end(); ++cameraIter, ++cameraIdx )
374 cameraIter->imagePath_ = imagePaths[ cameraIdx ];
377 _splatCloud.
requestCloudProperty( SPLATCLOUD_GENERALMANAGER_HANDLE )->data().flags_.set( SPLATCLOUD_CAMERA_HAS_IMAGEPATH_FLAG, hasImg );
388 readPoints( file, cameraObjectIDs, _splatCloud );
390 _splatCloud.
requestCloudProperty( SPLATCLOUD_GENERALMANAGER_HANDLE )->data().flags_.set( SPLATCLOUD_SPLAT_VIEWLIST_HAS_FEATURE_INDICES_FLAG,
true );
391 _splatCloud.
requestCloudProperty( SPLATCLOUD_GENERALMANAGER_HANDLE )->data().flags_.set( SPLATCLOUD_SPLAT_VIEWLIST_COORDS_NORMALIZED_FLAG,
false );
397 emit log(
LOGERR, tr(
"Unexpected end in input file \"%1\".\n" ).arg( _filename ) );
413 bool FileBundlePlugin::writeBundleFile(
const char *_filename,
const SplatCloud &_splatCloud )
422 int FileBundlePlugin::loadObject( QString _filename )
425 int splatCloudObjectId = -1;
427 if( splatCloudObjectId != -1 )
440 if( (splatCloud != 0) && (splatCloudNode != 0) )
443 if( readBundleFile( _filename.toLatin1(), *splatCloud ) )
446 emit updatedObject( splatCloudObjectId,
UPDATE_ALL );
447 emit openedFile( splatCloudObjectId );
459 emit log(
LOGERR, tr(
"Shader DrawModes for SplatCloud not existent!") );
471 drawmode |= pointsDrawMode;
477 return splatCloudObjectId;
491 bool FileBundlePlugin::saveObject(
int _objectId, QString _filename )
503 if( splatCloud != 0 )
506 if( writeBundleFile( _filename.toLatin1(), *splatCloud ) )
const UpdateType UPDATE_ALL(UpdateTypeSet(1))
Identifier for all updates.
bool requestViewlists()
Request the predefined property.
QWidget * loadOptionsWidget(QString)
CloudPropertyT< T > * requestCloudProperty(const PropertyHandleT< T > &_handle)
Request a new property.
void setName(QString _name)
Set the name of the Object.
const DrawMode & getDrawMode(const std::string &_name)
Get a custom DrawMode.
Color & colors(int _idx)
Get a reference of the predefined property's value.
void setFromFileName(const QString &_filename)
QWidget * saveOptionsWidget(QString)
bool requestPositions()
Request the predefined property.
void setDrawMode(const ACG::SceneGraph::DrawModes::DrawMode &_mode, int _viewer)
Set the draw Mode of a Viewer. .
Position & positions(int _idx)
Get a reference of the predefined property's value.
bool getObject(const int _identifier, BaseObject *&_object)
Get the object which has the given identifier.
QString filename() const
return the filename of the object
unsigned int numSplats() const
Get the number of splats.
DrawMode NONE
not a valid draw mode
ACG::SceneGraph::DrawModes::DrawMode drawMode(int _viewer)
Get the current draw Mode of a Viewer.
QString name()
Return a name for the plugin.
SplatCloud * splatCloud()
Get SplatCloud.
void clear()
Remove all properties and reset the number of splats.
SplatCloud * splatCloud(BaseObjectData *_object)
Get a SplatCloud from an object.
bool requestColors()
Request the predefined property.
void resizeSplats(unsigned int _num)
Resize the data vector of all splat-properties.
SplatCloudObject * splatCloudObject(BaseObjectData *_object)
Cast an SplatCloudObject to a SplatCloudObject if possible.
SplatCloudNode * splatCloudNode(BaseObjectData *_object)
Get a SplatCloudNode from an object.
bool containsAtomicDrawMode(const DrawMode &_atomicDrawMode) const
Check whether an Atomic DrawMode is active in this draw Mode.
SplatCloudNode * splatCloudNode()
Get SplatCloud's scenegraph Node.
Viewlist & viewlists(int _idx)
Get a reference of the predefined property's value.