60 #include "FileBundle.hh"
63 #if QT_VERSION >= 0x050000
74 #include <OpenMesh/Core/IO/IOManager.hh>
82 static const char IMAGELIST_SUFFIX[] =
".txt";
83 static const char IMAGELIST_FALLBACK[] =
"image_list.txt";
89 static std::string readLine( FILE *_file )
92 std::string result =
"";
98 fscanf( _file,
"%c", &c );
101 if( feof( _file ) || c ==
'\0' || c ==
'\n' || c ==
'\r' )
116 static void splitFilename(
const std::string &_str, std::string &_path, std::string &_name, std::string &_ext )
118 size_t i = _str.find_last_of(
"/\\" );
122 if( i == std::string::npos )
129 _path = _str.substr( 0, i+1 );
130 rest = _str.substr( i+1 );
133 size_t j = rest.find_last_of(
'.' );
135 if( j == std::string::npos )
142 _name = rest.substr( 0, j );
143 _ext = rest.substr( j );
151 bool FileBundlePlugin::addEmptyObjects(
unsigned int _num,
const DataType &_dataType, std::vector<int> &_objectIDs )
153 deleteObjects( _objectIDs );
154 _objectIDs.reserve( _num );
156 OpenFlipper::Options::blockSceneGraphUpdates();
159 for( i=0; i<_num; ++i )
162 emit addEmptyObject( _dataType, objectId );
167 _objectIDs.push_back( objectId );
170 OpenFlipper::Options::unblockSceneGraphUpdates();
175 deleteObjects( _objectIDs );
183 void FileBundlePlugin::deleteObjects( std::vector<int> &_objectIDs )
185 unsigned int i, num = _objectIDs.size();
186 for( i=0; i<num; ++i )
187 emit deleteObject( _objectIDs[ i ] );
196 bool FileBundlePlugin::readImagelistFile(
const char *_filename, std::vector<std::string> &_imagePaths )
200 FILE *file = fopen( _filename,
"rt" );
203 emit log(
LOGINFO, tr(
"Could not open imagelist file \"%1\".\n").arg( _filename ) );
212 fscanf( file,
"%4095s", path );
213 fscanf( file,
"%31s", temp );
214 fscanf( file,
"%31s", temp );
219 _imagePaths.push_back( std::string( path ) );
224 emit log(
LOGINFO, tr(
"Using imagelist file \"%1\".\n").arg( _filename ) );
232 void FileBundlePlugin::readCameras( FILE *_file,
const std::vector<int> &_cameraObjectIDs, SplatCloud_Cameras &_cameras )
236 unsigned int cameraIdx = 0;
237 SplatCloud_Cameras::iterator cameraIter;
238 for( cameraIter = _cameras.begin(); cameraIter != _cameras.end(); ++cameraIter, ++cameraIdx )
242 camera.objectId_ = _cameraObjectIDs[ cameraIdx ];
245 fscanf( _file,
"%32s", str ); proj.f_ = atof( str );
246 fscanf( _file,
"%32s", str ); proj.k1_ = atof( str );
247 fscanf( _file,
"%32s", str ); proj.k2_ = atof( str );
248 fscanf( _file,
"%32s", str ); proj.r_[0][0] = atof( str );
249 fscanf( _file,
"%32s", str ); proj.r_[0][1] = atof( str );
250 fscanf( _file,
"%32s", str ); proj.r_[0][2] = atof( str );
251 fscanf( _file,
"%32s", str ); proj.r_[1][0] = atof( str );
252 fscanf( _file,
"%32s", str ); proj.r_[1][1] = atof( str );
253 fscanf( _file,
"%32s", str ); proj.r_[1][2] = atof( str );
254 fscanf( _file,
"%32s", str ); proj.r_[2][0] = atof( str );
255 fscanf( _file,
"%32s", str ); proj.r_[2][1] = atof( str );
256 fscanf( _file,
"%32s", str ); proj.r_[2][2] = atof( str );
257 fscanf( _file,
"%32s", str ); proj.t_[0] = atof( str );
258 fscanf( _file,
"%32s", str ); proj.t_[1] = atof( str );
259 fscanf( _file,
"%32s", str ); proj.t_[2] = atof( str );
261 camera.imagePath_ =
"";
263 camera.imageWidth_ = 0;
264 camera.imageHeight_ = 0;
272 void FileBundlePlugin::readPoints( FILE *_file,
const std::vector<int> &_cameraObjectIDs,
SplatCloud &_splatCloud )
276 int maxCamObjId = _cameraObjectIDs.size() - 1;
278 unsigned int splatIdx;
279 for( splatIdx = 0; splatIdx < _splatCloud.
numSplats(); ++splatIdx )
283 fscanf( _file,
"%32s", str ); pos[0] = atof( str );
284 fscanf( _file,
"%32s", str ); pos[1] = atof( str );
285 fscanf( _file,
"%32s", str ); pos[2] = atof( str );
290 unsigned int r=0, g=0, b=0;
291 fscanf( _file,
"%16u", &r ); col[0] = r;
292 fscanf( _file,
"%16u", &g ); col[1] = g;
293 fscanf( _file,
"%16u", &b ); col[2] = b;
297 SplatCloud::Viewlist &viewlist = _splatCloud.
viewlists( splatIdx );
299 unsigned int numEntries = 0;
300 fscanf( _file,
"%16u", &numEntries );
302 viewlist.resize( numEntries );
304 SplatCloud::Viewlist::iterator viewIter;
305 for( viewIter = viewlist.begin(); viewIter != viewlist.end(); ++viewIter )
309 fscanf( _file,
"%16i", &i ); viewIter->cameraObjectId_ = ((i >= 0) && (i <= maxCamObjId)) ? _cameraObjectIDs[ i ] : -1;
310 fscanf( _file,
"%16i", &j ); viewIter->featureIdx_ = j;
311 fscanf( _file,
"%32s", str ); viewIter->x_ = atof( str );
312 fscanf( _file,
"%32s", str ); viewIter->y_ = atof( str );
322 bool FileBundlePlugin::readBundleFile(
const char *_filename,
SplatCloud &_splatCloud )
328 FILE *file = fopen( _filename,
"rt" );
331 emit log(
LOGERR, tr(
"Could not open input file \"%1\".\n").arg( _filename ) );
336 std::string magicAndVersion = readLine( file );
337 if( magicAndVersion.compare(
"# Bundle file v0.3" ) != 0 )
339 emit log(
LOGERR, tr(
"Bad magic/version \"%1\" in input file \"%2\".\n").arg( magicAndVersion.c_str(), _filename ) );
345 unsigned int numCameras = 0;
346 unsigned int numPoints = 0;
347 fscanf( file,
"%16u", &numCameras );
348 fscanf( file,
"%16u", &numPoints );
351 std::vector<int> cameraObjectIDs;
352 if( !addEmptyObjects( numCameras,
DATA_CAMERA, cameraObjectIDs ) )
354 emit log(
LOGERR, tr(
"Unable to add %1 cameras for input file \"%2\".\n").arg( QString::number( numCameras ), _filename ) );
360 if( numCameras != 0 )
364 cameraManager.cameras_.resize( numCameras );
365 readCameras( file, cameraObjectIDs, cameraManager.cameras_ );
369 std::vector<std::string> imagePaths;
371 std::string path,
name, ext;
372 splitFilename( _filename, path, name, ext );
374 if( !readImagelistFile( (path + name + IMAGELIST_SUFFIX).c_str(), imagePaths ) )
375 readImagelistFile( (path + IMAGELIST_FALLBACK ).c_str(), imagePaths );
377 bool hasImg = (cameraManager.cameras_.size() <= imagePaths.size());
381 unsigned int cameraIdx = 0;
382 SplatCloud_Cameras::iterator cameraIter;
383 for( cameraIter = cameraManager.cameras_.begin(); cameraIter != cameraManager.cameras_.end(); ++cameraIter, ++cameraIdx )
384 cameraIter->imagePath_ = imagePaths[ cameraIdx ];
387 _splatCloud.
requestCloudProperty( SPLATCLOUD_GENERALMANAGER_HANDLE )->data().flags_.set( SPLATCLOUD_CAMERA_HAS_IMAGEPATH_FLAG, hasImg );
398 readPoints( file, cameraObjectIDs, _splatCloud );
400 _splatCloud.
requestCloudProperty( SPLATCLOUD_GENERALMANAGER_HANDLE )->data().flags_.set( SPLATCLOUD_SPLAT_VIEWLIST_HAS_FEATURE_INDICES_FLAG,
true );
401 _splatCloud.
requestCloudProperty( SPLATCLOUD_GENERALMANAGER_HANDLE )->data().flags_.set( SPLATCLOUD_SPLAT_VIEWLIST_COORDS_NORMALIZED_FLAG,
false );
407 emit log(
LOGERR, tr(
"Unexpected end in input file \"%1\".\n" ).arg( _filename ) );
423 bool FileBundlePlugin::writeBundleFile(
const char *_filename,
const SplatCloud &_splatCloud )
432 int FileBundlePlugin::loadObject( QString _filename )
435 int splatCloudObjectId = -1;
437 if( splatCloudObjectId != -1 )
450 if( (splatCloud != 0) && (splatCloudNode != 0) )
453 if( readBundleFile( _filename.toLatin1(), *splatCloud ) )
456 emit updatedObject( splatCloudObjectId,
UPDATE_ALL );
457 emit openedFile( splatCloudObjectId );
469 emit log(
LOGERR, tr(
"Shader DrawModes for SplatCloud not existent!") );
481 drawmode |= pointsDrawMode;
487 return splatCloudObjectId;
501 bool FileBundlePlugin::saveObject(
int _objectId, QString _filename )
513 if( splatCloud != 0 )
516 if( writeBundleFile( _filename.toLatin1(), *splatCloud ) )
548 #if QT_VERSION < 0x050000
bool requestPositions()
Request the predefined property.
SplatCloud * splatCloud()
Get SplatCloud.
bool containsAtomicDrawMode(DrawMode _atomicDrawMode) const
Check whether an Atomic DrawMode is active in this draw Mode.
SplatCloudObject * splatCloudObject(BaseObjectData *_object)
Cast an SplatCloudObject to a SplatCloudObject if possible.
void setName(QString _name)
Set the name of the Object.
void setFromFileName(const QString &_filename)
void setDrawMode(const ACG::SceneGraph::DrawModes::DrawMode &_mode, int _viewer)
Set the draw Mode of a Viewer. .
bool getObject(int _identifier, BSplineCurveObject *&_object)
bool requestColors()
Request the predefined property.
QWidget * saveOptionsWidget(QString)
void resizeSplats(unsigned int _num)
Resize the data vector of all splat-properties.
CloudPropertyT< T > * requestCloudProperty(const PropertyHandleT< T > &_handle)
Request a new property.
bool requestViewlists()
Request the predefined property.
void clear()
Remove all properties and reset the number of splats.
QString filename() const
return the filename of the object
QString name()
Return a name for the plugin.
Viewlist & viewlists(int _idx)
Get a reference of the predefined property's value.
const DrawMode & getDrawMode(const std::string &_name)
Get a custom DrawMode.
unsigned int numSplats() const
Get the number of splats.
const UpdateType UPDATE_ALL(UpdateTypeSet(1))
Identifier for all updates.
Position & positions(int _idx)
Get a reference of the predefined property's value.
SplatCloudNode * splatCloudNode()
Get SplatCloud's scenegraph Node.
DrawMode NONE
not a valid draw mode
QWidget * loadOptionsWidget(QString)
Color & colors(int _idx)
Get a reference of the predefined property's value.
ACG::SceneGraph::DrawModes::DrawMode drawMode(int _viewer)
Get the current draw Mode of a Viewer.
SplatCloudNode * splatCloudNode(BaseObjectData *_object)
Get a SplatCloudNode from an object.
SplatCloud * splatCloud(BaseObjectData *_object)
Get a SplatCloud from an object.