Commit 07a1c7ab authored by Jan Möbius's avatar Jan Möbius
Browse files

Merged text readers into one function

parent 69ecf014
......@@ -42,14 +42,6 @@
\*===========================================================================*/
//================================================================
//
// CLASS FilePTSPlugin - IMPLEMENTATION
//
//================================================================
//== INCLUDES ====================================================
......@@ -76,7 +68,7 @@ static const int COLORRANGE_0_1 = 0;
template <typename MeshT>
class AdaptorMesh : public AdaptorBase {
public:
AdaptorMesh(MeshT& _mesh) : mesh_(_mesh),currentPoint_(0) {}
AdaptorMesh(MeshT& _mesh, DataType _type) : mesh_(_mesh),currentPoint_(0),type_(_type) {}
void clear() override {
mesh_.clear();
......@@ -122,14 +114,82 @@ public:
mesh_.reserve(_size,0,0);
};
virtual DataType adaptorType() override {
return type_;
};
private :
MeshT& mesh_;
typename MeshT::VertexHandle currentPoint_;
DataType type_;
};
class AdaptorSplat : public AdaptorBase {
public:
AdaptorSplat(SplatCloud& _cloud) : cloud_(_cloud),splatIdx_(-1) {
cloud_.requestPositions();
}
void clear() override {
cloud_.clear();
cloud_.requestPositions();
};
void add_point(ACG::Vec3d _point) override {
++splatIdx_;
cloud_.pushbackSplat();
cloud_.positions( splatIdx_ ) = _point;
};
void setNormal(ACG::Vec3d _normal) override{
cloud_.normals( splatIdx_ ) = _normal;
};
void setColor(ACG::Vec4f _color) override{
cloud_.colors( splatIdx_ ) = OpenMesh::color_cast<ACG::Vec3f>(_color);
};
void setPointSize(double /*_size*/ ) override {
};
void setIndex(int /*_size*/ ) override {
};
void request_vertex_normals() override {
cloud_.requestNormals();
}
void request_vertex_colors() override {
cloud_.requestColors();
}
virtual void request_point_sizes() override {
cloud_.requestPointsizes();
}
virtual void request_indices() override {
cloud_.requestIndices();
}
void reserve(int /*_size*/ ) override {
};
virtual DataType adaptorType() override {
return DATA_SPLATCLOUD;
};
private :
SplatCloud& cloud_;
int splatIdx_;
};
FilePTSPlugin::FilePTSPlugin() :
loadOptions_( 0 ),
saveOptions_( 0 ),
......@@ -485,175 +545,6 @@ bool FilePTSPlugin::readBinaryFile( const char *_filename, SplatCloud &_splatClo
//----------------------------------------------------------------
bool FilePTSPlugin::readTextFile( const char *_filename, SplatCloud &_splatCloud ) /*const*/
{
// clear splatcloud
_splatCloud.clear();
// set default options
bool loadNormals = OpenFlipperSettings().value( "FilePTS/Load/Normals", true ).toBool();
bool loadPointsizes = OpenFlipperSettings().value( "FilePTS/Load/Pointsizes", false ).toBool();
bool loadColors = OpenFlipperSettings().value( "FilePTS/Load/Colors", false ).toBool();
int loadColorRange = OpenFlipperSettings().value( "FilePTS/Load/ColorRange",0 ).toInt();
bool loadIndices = OpenFlipperSettings().value( "FilePTS/Load/Indices", false ).toBool();
// get options
if( OpenFlipper::Options::gui() && loadOptions_ )
{
loadNormals = loadNormals_ ->isChecked();
loadPointsizes = loadPointsizes_->isChecked();
loadColors = loadColors_ ->isChecked();
loadColorRange = loadColorRange_->currentIndex();
loadIndices = loadIndices_ ->isChecked();
}
// request properties
bool success = true;
{ if( !_splatCloud.requestPositions() ) success = false; }
if( loadNormals ) { if( !_splatCloud.requestNormals() ) success = false; }
if( loadPointsizes ) { if( !_splatCloud.requestPointsizes() ) success = false; }
if( loadColors ) { if( !_splatCloud.requestColors() ) success = false; }
if( loadIndices ) { if( !_splatCloud.requestIndices() ) success = false; }
// check success of requests
if( !success )
{
emit log( LOGERR, tr("Out of memory for input file \"%1\".\n").arg( _filename ) );
return false; // return failure
}
// open file
FILE *file = fopen( _filename, "rb" );
if( !file )
{
emit log( LOGERR, tr("Could not open input file \"%1\".\n").arg( _filename ) );
return false;
}
int splatIdx;
for( splatIdx = 0; ; ++splatIdx )
{
// read position
{
float pos[3];
const int read = fscanf( file, "%32f %32f %32f", &pos[0], &pos[1], &pos[2] );
if( read != 3 ) {
std::cerr << pos[0] << " " << pos[1] << " " << pos[2] << std::endl;
emit log( LOGERR, tr("Scanned for 3 coordinates but got %1 at Index %2.\n").arg( read ).arg(splatIdx) );
break;
}
// increase number of splats
_splatCloud.pushbackSplat();
SplatCloud::Position position;
position[0] = pos[0];
position[1] = pos[1];
position[2] = pos[2];
_splatCloud.positions( splatIdx ) = position;
}
// read color
if( loadColors )
{
float col[3];
const int read = fscanf( file, "%32f %32f %32f", &col[0], &col[1], &col[2] );
if( read != 3 ) {
emit log( LOGERR, tr("Scanned for 3 colors but got %1 at Index %2.\n").arg( read ).arg(splatIdx) );
break;
}
SplatCloud::Color color;
if( loadColorRange == COLORRANGE_0_1 )
{
color[0] = (unsigned char) (255.999f * col[0]);
color[1] = (unsigned char) (255.999f * col[1]);
color[2] = (unsigned char) (255.999f * col[2]);
}
else // loadColorRange == COLORRANGE_0_255
{
color[0] = (unsigned char) col[0];
color[1] = (unsigned char) col[1];
color[2] = (unsigned char) col[2];
}
_splatCloud.colors( splatIdx ) = color;
}
// read normal
if( loadNormals )
{
float nrm[3];
fscanf( file, "%32f %32f %32f", &nrm[0], &nrm[1], &nrm[2] );
SplatCloud::Normal normal;
normal[0] = nrm[0];
normal[1] = nrm[1];
normal[2] = nrm[2];
_splatCloud.normals( splatIdx ) = normal;
}
// read pointsize
if( loadPointsizes )
{
float ps = 0.0f;
fscanf( file, "%32f", &ps );
SplatCloud::Pointsize pointsize;
pointsize = ps;
_splatCloud.pointsizes( splatIdx ) = pointsize;
}
// read index
if( loadIndices )
{
int idx = -1;
fscanf( file, "%16i", &idx );
SplatCloud::Index index;
index = idx;
_splatCloud.indices( splatIdx ) = index;
}
// check for errors
if( ferror( file ) )
{
emit log( LOGERR, tr("Could not read input file \"%1\".\n").arg( _filename ) );
fclose( file );
return false; // return failure
}
if( feof( file ) )
{
emit log( LOGERR, tr("Unexpected end in input file \"%1\".\n").arg( _filename ) );
fclose( file );
return false; // return failure
}
}
// check for errors
if( !feof( file ) ) // if end-of-file is *not* reached, something went wrong
{
emit log( LOGERR, tr("Bad file format of input file \"%1\".\n").arg( _filename ) );
fclose( file );
return false; // return failure
}
// close file
fclose( file );
// return success
return true;
}
bool FilePTSPlugin::readTextFile ( const char *_filename, AdaptorBase& _adaptor ) {
// clear Mesh
_adaptor.clear();
......@@ -666,6 +557,8 @@ bool FilePTSPlugin::readTextFile ( const char *_filename, AdaptorBase& _adaptor
int loadColorRange = OpenFlipperSettings().value( "FilePTS/Load/ColorRange",0 ).toInt();
bool loadIndices = OpenFlipperSettings().value( "FilePTS/Load/Indices", false ).toBool();
// get options
if( OpenFlipper::Options::gui() && loadOptions_ )
{
......@@ -680,6 +573,9 @@ bool FilePTSPlugin::readTextFile ( const char *_filename, AdaptorBase& _adaptor
// request properties
if( loadNormals ) { _adaptor.request_vertex_normals(); }
if( loadColors ) { _adaptor.request_vertex_colors(); }
if( loadPointsizes ) { _adaptor.request_point_sizes(); }
if( loadIndices ) { _adaptor.request_indices(); }
QFile f(_filename);
......@@ -776,31 +672,43 @@ bool FilePTSPlugin::readTextFile ( const char *_filename, AdaptorBase& _adaptor
}
// // read pointsize
// if( loadPointsizes )
// {
// float ps = 0.0f;
// fscanf( file, "%32f", &ps );
// read pointsize
if( loadPointsizes )
{
float ps;
in >> ps;
if ( _adaptor.adaptorType() == DATA_SPLATCLOUD ) {
if ( in.status() == QTextStream::Ok ) {
_adaptor.setPointSize(ps);
} else {
emit log( LOGERR, tr("Failed to read point size for point %1\n").arg( currentPoint ) );
break;
}
} else
emit log(LOGERR, "Pointsize not implemented for meshes");
// emit log(LOGERR, "Pointsize not implemented for meshes");
}
// //SplatCloud::Pointsize pointsize;
// //pointsize = ps;
// //_splatCloud.pointsizes( splatIdx ) = pointsize;
// }
// read index
if( loadPointsizes )
{
int idx = -1;
// // read index
// if( loadIndices )
// {
// int idx = -1;
// fscanf( file, "%16i", &idx );
in >> idx;
// emit log(LOGERR, "Index not implemented for meshes");
if ( _adaptor.adaptorType() == DATA_SPLATCLOUD ) {
if ( in.status() == QTextStream::Ok ) {
_adaptor.setIndex(idx);
} else {
emit log( LOGERR, tr("Failed to read index for point %1\n").arg( currentPoint ) );
break;
}
} else
emit log(LOGERR, "Index not implemented for meshes, skipped");
// SplatCloud::Index index;
// index = idx;
// _splatCloud.indices( splatIdx ) = index;
// }
}
++currentPoint;
line = in.readLine();
......@@ -1167,8 +1075,10 @@ int FilePTSPlugin::loadObject( QString _filename )
SplatCloudNode *splatCloudNode = splatCloudObject->splatCloudNode();
if( (splatCloud != 0) && (splatCloudNode != 0) )
{
AdaptorSplat adaptor(*splatCloud);
// read splatcloud from disk
if( loadBinaryFile ? readBinaryFile( _filename.toLatin1(), *splatCloud ) : readTextFile( _filename.toLatin1(), *splatCloud ) )
if( loadBinaryFile ? readBinaryFile( _filename.toLatin1(), *splatCloud ) : readTextFile( _filename.toLatin1(), adaptor ) )
{
// emit signals that the object has to be updated and that a file was opened
emit updatedObject( splatCloudObjectId, UPDATE_ALL );
......@@ -1239,7 +1149,7 @@ int FilePTSPlugin::loadObject( QString _filename )
if ( loadBinaryFile ) {
emit log(LOGERR,"Binary not supported for mesh target!");
} else {
AdaptorMesh<TriMesh> adaptor(*mesh);
AdaptorMesh<TriMesh> adaptor(*mesh,DATA_TRIANGLE_MESH);
readTextFile( _filename.toLatin1(), adaptor ) ;
......@@ -1277,7 +1187,7 @@ int FilePTSPlugin::loadObject( QString _filename )
if ( loadBinaryFile ) {
emit log(LOGERR,"Binary not supported for mesh target!");
} else {
AdaptorMesh<PolyMesh> adaptor(*mesh);
AdaptorMesh<PolyMesh> adaptor(*mesh,DATA_POLY_MESH);
readTextFile( _filename.toLatin1(), adaptor ) ;
......
......@@ -100,6 +100,8 @@ public:
virtual void request_indices() = 0;
virtual void reserve(int _size) = 0;
virtual DataType adaptorType() = 0;
};
......@@ -171,7 +173,6 @@ private:
// read binary/text file from disc to scenegraph node
bool readBinaryFile( const char *_filename, SplatCloud &_splatCloud ) /*const*/;
bool readTextFile ( const char *_filename, SplatCloud &_splatCloud ) /*const*/;
bool readTextFile ( const char *_filename, AdaptorBase& _adaptor );
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment