Commit 34bf0964 authored by Jan's avatar Jan
Browse files

Switched parser for Triangle meshes to textstream

parent 41bcc8a0
......@@ -73,12 +73,12 @@ static const int COLORRANGE_0_1 = 0;
//== IMPLEMENTATION ==============================================
FilePTSPlugin::FilePTSPlugin() :
loadOptions_( 0 ),
saveOptions_( 0 ),
loadType_(0),
loadBinaryFile_( 0 ),
pointCount_ ( 0 ),
loadNormals_ ( 0 ),
loadPointsizes_( 0 ),
loadColors_ ( 0 ),
......@@ -602,6 +602,7 @@ bool FilePTSPlugin::readTextFile ( const char *_filename, TriMesh &_mesh ) {
_mesh.clear();
// set default options
bool pointCount = OpenFlipperSettings().value( "FilePTS/Load/PointCount", true ).toBool();
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();
......@@ -611,6 +612,7 @@ bool FilePTSPlugin::readTextFile ( const char *_filename, TriMesh &_mesh ) {
// get options
if( OpenFlipper::Options::gui() && loadOptions_ )
{
pointCount = pointCount_ ->isChecked();
loadNormals = loadNormals_ ->isChecked();
loadPointsizes = loadPointsizes_->isChecked();
loadColors = loadColors_ ->isChecked();
......@@ -622,126 +624,142 @@ bool FilePTSPlugin::readTextFile ( const char *_filename, TriMesh &_mesh ) {
if( loadNormals ) { _mesh.request_vertex_normals(); }
if( loadColors ) { _mesh.request_vertex_colors(); }
// open file
FILE *file = fopen( _filename, "rb" );
if( !file )
{
emit log( LOGERR, tr("Could not open input file \"%1\".\n").arg( _filename ) );
return false;
QFile f(_filename);
if (!f.open(QIODevice::ReadOnly | QIODevice::Text)) {
emit log( LOGERR, tr("Could not open input file \"%1\".\n").arg( _filename ) );
return false;
}
QTextStream in(&f);
int splatIdx;
for( splatIdx = 0; ; ++splatIdx )
{
// read position
{
float pos[3];
QString line = in.readLine();
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;
// We have a point count header. So parse it and allocate memory in advance
if (pointCount) {
bool ok;
const int size = line.toInt(&ok);
if ( ok ) {
emit log( LOGINFO, tr("Point count in header: \"%1\"").arg( size ) );
_mesh.reserve(size,0,0);
} else {
emit log( LOGERR, tr("Failed to read point count header. Got \"%1\"").arg( line ) );
return false;
}
_mesh.add_vertex( TriMesh::Point(pos) );
}
// 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;
}
int currentPoint = 0;
// Process all lines in file.
while (!in.atEnd()) {
TriMesh::Color color(0.0,0.0,0.0,1.0);
if ( (currentPoint % 10000) == 0 )
emit log( LOGINFO, tr("Reading point %1\n").arg( currentPoint ) );
if( loadColorRange == COLORRANGE_0_1 )
// Read a position, which should be always here:
{
color[0] = (unsigned char) (255.999f * col[0]);
color[1] = (unsigned char) (255.999f * col[1]);
color[2] = (unsigned char) (255.999f * col[2]);
double pos[3];
in >> pos[0] >> pos[1] >> pos[2];
if ( in.status() == QTextStream::Ok ) {
_mesh.add_vertex( TriMesh::Point(pos) );
} else {
emit log( LOGERR, tr("Failed to read postion for point %1\n").arg( currentPoint ) );
break;
}
}
else // loadColorRange == COLORRANGE_0_255
// Read color, if it has been selected
if( loadColors )
{
color[0] = (unsigned char) col[0];
color[1] = (unsigned char) col[1];
color[2] = (unsigned char) col[2];
}
_mesh.set_color( _mesh.vertex_handle(_mesh.n_vertices()-1),color );
float col[3];
}
in >> col[0] >> col[1] >> col[2];
// read normal
if( loadNormals )
{
float nrm[3];
fscanf( file, "%32f %32f %32f", &nrm[0], &nrm[1], &nrm[2] );
if ( in.status() == QTextStream::Ok ) {
TriMesh::Normal normal;
normal[0] = nrm[0];
normal[1] = nrm[1];
normal[2] = nrm[2];
TriMesh::Color color(0.0,0.0,0.0,1.0);
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];
}
_mesh.set_normal( _mesh.vertex_handle(_mesh.n_vertices()-1),normal);
}
_mesh.set_color( _mesh.vertex_handle(_mesh.n_vertices()-1),color );
// // read pointsize
// if( loadPointsizes )
// {
// float ps = 0.0f;
// fscanf( file, "%32f", &ps );
} else {
emit log( LOGERR, tr("Failed to read color for point %1\n").arg( currentPoint ) );
break;
}
}
// SplatCloud::Pointsize pointsize;
// pointsize = ps;
// read normal
if( loadNormals )
{
double nrm[3];
in >> nrm[0] >> nrm[1] >> nrm[2];
if ( in.status() == QTextStream::Ok ) {
TriMesh::Normal normal;
normal[0] = nrm[0];
normal[1] = nrm[1];
normal[2] = nrm[2];
_mesh.set_normal( _mesh.vertex_handle(_mesh.n_vertices()-1),normal);
} else {
emit log( LOGERR, tr("Failed to read normal for point %1\n").arg( currentPoint ) );
break;
}
// _splatCloud.pointsizes( splatIdx ) = pointsize;
// }
}
// // read index
// if( loadIndices )
// {
// int idx = -1;
// fscanf( file, "%16i", &idx );
// // read pointsize
// if( loadPointsizes )
// {
// float ps = 0.0f;
// fscanf( file, "%32f", &ps );
// SplatCloud::Index index;
// index = idx;
// emit log(LOGERR, "Pointsize not implemented for meshes");
// _splatCloud.indices( splatIdx ) = index;
// }
// //SplatCloud::Pointsize pointsize;
// //pointsize = ps;
// //_splatCloud.pointsizes( splatIdx ) = pointsize;
// }
// 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
}
// // read index
// if( loadIndices )
// {
// int idx = -1;
// fscanf( file, "%16i", &idx );
// emit log(LOGERR, "Index not implemented for meshes");
// SplatCloud::Index index;
// index = idx;
// _splatCloud.indices( splatIdx ) = index;
// }
++currentPoint;
line = in.readLine();
//std::cerr << line.toStdString() << std::endl;
}
// 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
if ( in.status() != QTextStream::Ok ) {
emit log( LOGERR, tr("Input File Stream Status not ok! Points read: %1\n").arg( currentPoint ) );
}
// close file
fclose( file );
// Sanity Check:
if ( !in.atEnd() ) {
emit log( LOGERR, tr("Not at end of file! Points read: %1\n").arg( currentPoint ) );
}
f.close();
// return success
return true;
......@@ -1167,6 +1185,10 @@ int FilePTSPlugin::loadObject( QString _filename )
emit updatedObject( triangleMeshID, UPDATE_ALL );
emit openedFile( triangleMeshID );
ACG::SceneGraph::DrawModes::DrawMode pointsDrawMode = ACG::SceneGraph::DrawModes::getDrawMode( "Points" );
triObject->meshNode()->drawMode(pointsDrawMode);
return triangleMeshID;
}
}
}
......@@ -1233,6 +1255,7 @@ QWidget *FilePTSPlugin::loadOptionsWidget( QString /*_currentFilter*/ )
loadBinaryFile_ = new QCheckBox( tr("Load as Binary File") );
pointCount_ = new QCheckBox( tr("First Line contains number of points") );
loadNormals_ = new QCheckBox( tr("Contains Normals") );
loadPointsizes_ = new QCheckBox( tr("Contains Pointsizes") );
loadColors_ = new QCheckBox( tr("Contains Colors") );
......@@ -1251,6 +1274,7 @@ QWidget *FilePTSPlugin::loadOptionsWidget( QString /*_currentFilter*/ )
QVBoxLayout *loadStructureLayout = new QVBoxLayout();
loadStructureLayout->setSpacing( 6 );
loadStructureLayout->addWidget( pointCount_ );
loadStructureLayout->addWidget( loadNormals_ );
loadStructureLayout->addWidget( loadPointsizes_ );
loadStructureLayout->addItem ( loadColorsLayout );
......@@ -1280,6 +1304,7 @@ QWidget *FilePTSPlugin::loadOptionsWidget( QString /*_currentFilter*/ )
// get Load Options from OpenFlipper (from disc)
loadType_->setCurrentIndex( OpenFlipperSettings().value( "FilePTS/Load/LoadType", 0 ).toInt() );
loadBinaryFile_-> setChecked ( OpenFlipperSettings().value( "FilePTS/Load/BinaryFile", true ).toBool() );
pointCount_-> setChecked ( OpenFlipperSettings().value( "FilePTS/Load/PointCount", true ).toBool() );
loadNormals_-> setChecked ( OpenFlipperSettings().value( "FilePTS/Load/Normals", true ).toBool() );
loadPointsizes_-> setChecked ( OpenFlipperSettings().value( "FilePTS/Load/Pointsizes", true ).toBool() );
loadColors_-> setChecked ( OpenFlipperSettings().value( "FilePTS/Load/Colors", true ).toBool() );
......@@ -1383,6 +1408,7 @@ void FilePTSPlugin::slotLoadMakeDefaultButtonClicked()
{
// pass our Load Options to OpenFlipper (to disc)
OpenFlipperSettings().setValue( "FilePTS/Load/LoadType", loadType_->currentIndex() );
OpenFlipperSettings().setValue( "FilePTS/Load/PointCount", pointCount_->isChecked() );
OpenFlipperSettings().setValue( "FilePTS/Load/BinaryFile", loadBinaryFile_->isChecked() );
OpenFlipperSettings().setValue( "FilePTS/Load/Normals", loadNormals_-> isChecked() );
OpenFlipperSettings().setValue( "FilePTS/Load/Pointsizes", loadPointsizes_->isChecked() );
......@@ -1390,6 +1416,7 @@ void FilePTSPlugin::slotLoadMakeDefaultButtonClicked()
OpenFlipperSettings().setValue( "FilePTS/Load/ColorRange", loadColorRange_->currentIndex() );
OpenFlipperSettings().setValue( "FilePTS/Load/Indices", loadIndices_-> isChecked() );
// OpenFlipperSettings().setValue( "Core/File/UseLoadDefaults", true );
}
......
......@@ -171,6 +171,7 @@ private:
// options in the loading menu
QComboBox *loadType_;
QCheckBox *loadBinaryFile_;
QCheckBox *pointCount_;
QCheckBox *loadNormals_;
QCheckBox *loadPointsizes_;
QCheckBox *loadColors_;
......
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