Commit 7babe645 authored by Mathieu Malaterre's avatar Mathieu Malaterre

ENH: Remove ref to sstream, since VTK does not support stringstream, should...

ENH: Remove ref to sstream, since VTK does not support stringstream, should use strstream instead. + minor style issues
parent 91ae34bd
...@@ -14,29 +14,24 @@ ...@@ -14,29 +14,24 @@
=========================================================================*/ =========================================================================*/
#include "vtkParticleReader.h" #include "vtkParticleReader.h"
#include <sstream>
#include <algorithm>
#include <vector>
#include "vtkSmartPointer.h" #include "vtkSmartPointer.h"
#include "vtkStreamingDemandDrivenPipeline.h" #include "vtkStreamingDemandDrivenPipeline.h"
#include "vtkInformation.h" #include "vtkInformation.h"
#include "vtkInformationVector.h" #include "vtkInformationVector.h"
#include "vtkObjectFactory.h" #include "vtkObjectFactory.h"
#include "vtkByteSwap.h" #include "vtkByteSwap.h"
#include "vtkPointData.h" #include "vtkPointData.h"
#include "vtkPoints.h" #include "vtkPoints.h"
#include "vtkPolyData.h" #include "vtkPolyData.h"
#include "vtkCellArray.h" #include "vtkCellArray.h"
#include "vtkDataArray.h" #include "vtkDataArray.h"
#include "vtkDoubleArray.h" #include "vtkDoubleArray.h"
#include "vtkFloatArray.h" #include "vtkFloatArray.h"
vtkCxxRevisionMacro(vtkParticleReader, "1.22"); #include <vtkstd/algorithm>
#include <vtkstd/vector>
vtkCxxRevisionMacro(vtkParticleReader, "1.23");
vtkStandardNewMacro(vtkParticleReader); vtkStandardNewMacro(vtkParticleReader);
namespace { namespace {
...@@ -51,7 +46,7 @@ namespace { ...@@ -51,7 +46,7 @@ namespace {
class ParseLine class ParseLine
{ {
public: public:
ParseLine (): lookForEndString(false) {}; ParseLine (): LookForEndString(false) {};
int operator () (vtkstd::string &s, T val[4]) int operator () (vtkstd::string &s, T val[4])
{ {
// Skip over comment lines. // Skip over comment lines.
...@@ -60,15 +55,17 @@ namespace { ...@@ -60,15 +55,17 @@ namespace {
itr = vtkstd::search(s.begin(),s.end(),tgt.begin(),tgt.end()); itr = vtkstd::search(s.begin(),s.end(),tgt.begin(),tgt.end());
if ( itr != s.end() ) if ( itr != s.end() )
{ {
lookForEndString = true; LookForEndString = true;
//continue; //continue;
} }
if ( lookForEndString ) if ( LookForEndString )
{ {
tgt = "*/"; tgt = "*/";
itr = vtkstd::search(s.begin(),s.end(),tgt.begin(),tgt.end()); itr = vtkstd::search(s.begin(),s.end(),tgt.begin(),tgt.end());
if ( itr != s.end() ) if ( itr != s.end() )
lookForEndString = false; {
LookForEndString = false;
}
//continue; //continue;
return 0; return 0;
} }
...@@ -76,28 +73,34 @@ namespace { ...@@ -76,28 +73,34 @@ namespace {
tgt = "//"; tgt = "//";
itr = vtkstd::search(s.begin(),s.end(),tgt.begin(),tgt.end()); itr = vtkstd::search(s.begin(),s.end(),tgt.begin(),tgt.end());
if ( itr != s.end() ) if ( itr != s.end() )
{
return 0; return 0;
}
tgt = "%"; tgt = "%";
itr = vtkstd::search(s.begin(),s.end(),tgt.begin(),tgt.end()); itr = vtkstd::search(s.begin(),s.end(),tgt.begin(),tgt.end());
if ( itr != s.end() ) if ( itr != s.end() )
{
return 0; return 0;
}
tgt = "#"; tgt = "#";
itr = vtkstd::search(s.begin(),s.end(),tgt.begin(),tgt.end()); itr = vtkstd::search(s.begin(),s.end(),tgt.begin(),tgt.end());
if ( itr != s.end() ) if ( itr != s.end() )
{
return 0; return 0;
}
// If comma delimited, replace with tab // If comma delimited, replace with tab
vtkstd::replace(s.begin(),s.end(),',','\t'); vtkstd::replace(s.begin(),s.end(),',','\t');
// We have data. // We have data.
vtkstd::istringstream is(s);// is.str(s); strstream is;
is << s.c_str() << ends;// no istringstream in VTK
is >> val[0] >> val[1] >> val[2] >> val[3]; is >> val[0] >> val[1] >> val[2] >> val[3];
is.clear();
return 1; return 1;
} }
private: private:
bool lookForEndString; bool LookForEndString;
}; };
...@@ -114,8 +117,8 @@ vtkParticleReader::vtkParticleReader() : ...@@ -114,8 +117,8 @@ vtkParticleReader::vtkParticleReader() :
, HasScalar(1) , HasScalar(1)
, FileType(FILE_TYPE_IS_UNKNOWN) , FileType(FILE_TYPE_IS_UNKNOWN)
, DataType(VTK_FLOAT) , DataType(VTK_FLOAT)
, alliquot(0) , Alliquot(0)
, count(0) , Count(0)
{ {
this->SetNumberOfInputPorts(0); this->SetNumberOfInputPorts(0);
} }
...@@ -278,69 +281,75 @@ int vtkParticleReader::DetermineFileType() ...@@ -278,69 +281,75 @@ int vtkParticleReader::DetermineFileType()
vtkstd::vector <unsigned char> s; vtkstd::vector <unsigned char> s;
for ( size_t i = 0; i < sampleSize; ++i ) for ( size_t i = 0; i < sampleSize; ++i )
{ {
char c; char c;
this->File->read(&c,sizeof(char)); this->File->read(&c,sizeof(char));
s.push_back(c); s.push_back(c);
} }
// If read terminated prematurely then it may have detected // If read terminated prematurely then it may have detected
// a premature EOF character in the data. // a premature EOF character in the data.
// Assume that the file type is undetermined in this case. // Assume that the file type is undetermined in this case.
if ( s.size() != sampleSize ) if ( s.size() != sampleSize )
{
// cout << "Premature termination" << endl;
return FILE_TYPE_IS_UNKNOWN;
}
size_t charsCounted = 0;
size_t zero = 0;
size_t conventionalASCII = 0;
size_t extendedASCII = 0;
size_t controlASCII = 0;
size_t otherASCII = 0;
for ( size_t j = 0; j < s.size(); ++j )
{
if ( s[j] == '\0' )
{ {
// cout << "Premature termination" << endl; zero++;
return FILE_TYPE_IS_UNKNOWN; charsCounted++;
continue;
} }
// Conventional ASCII characters.
size_t charsCounted = 0; if ( s[j] > 0x1f && s[j] < 0x80 )
size_t zero = 0; {
size_t conventionalASCII = 0; conventionalASCII++;
size_t extendedASCII = 0; charsCounted++;
size_t controlASCII = 0; continue;
size_t otherASCII = 0; }
for ( size_t j = 0; j < s.size(); ++j ) // Extended ASCII characters may have been used.
{ if ( s[j] > 0x7f )
if ( s[j] == '\0' ) {
{ extendedASCII++;
zero++;
charsCounted++;
continue;
}
// Conventional ASCII characters.
if ( s[j] > 0x1f && s[j] < 0x80 )
{
conventionalASCII++;
charsCounted++;
continue;
}
// Extended ASCII characters may have been used.
if ( s[j] > 0x7f )
{
extendedASCII++;
charsCounted++;
continue;
}
// Control characters.
if ( s[j] == '\n' || s[j] == '\r' || s[j] == '\t' || s[j] == '\f' )
{
controlASCII++;
charsCounted++;
continue;
}
otherASCII++;
charsCounted++; charsCounted++;
} continue;
}
// Control characters.
if ( s[j] == '\n' || s[j] == '\r' || s[j] == '\t' || s[j] == '\f' )
{
controlASCII++;
charsCounted++;
continue;
}
otherASCII++;
charsCounted++;
}
// cout << "Zero: " << zero << " Conventional ASCII:" << conventionalASCII << // cout << "Zero: " << zero << " Conventional ASCII:" << conventionalASCII <<
// " Extended ASCII: " << extendedASCII << " Control ASCII: " << controlASCII << // " Extended ASCII: " << extendedASCII << " Control ASCII: " << controlASCII <<
// " Other characters: " << otherASCII << // " Other characters: " << otherASCII <<
// " Total counted: " << charsCounted << endl; // " Total counted: " << charsCounted << endl;
// NULL shouldn't ever appear in a text file. // NULL shouldn't ever appear in a text file.
if ( zero != 0 || otherASCII > 0 || conventionalASCII == 0 ) if ( zero != 0 || otherASCII > 0 || conventionalASCII == 0 )
return FILE_TYPE_IS_BINARY; {
if ( (double)extendedASCII / (double) conventionalASCII < hiToLowASCII ) return FILE_TYPE_IS_BINARY;
return FILE_TYPE_IS_TEXT; }
else if ( (double)extendedASCII / (double) conventionalASCII < hiToLowASCII )
return FILE_TYPE_IS_BINARY; {
return FILE_TYPE_IS_TEXT;
}
else
{
return FILE_TYPE_IS_BINARY;
}
} }
//---------------------------------------------------------------------------- //----------------------------------------------------------------------------
...@@ -369,31 +378,33 @@ int vtkParticleReader::ProduceOutputFromTextFileDouble(vtkInformationVector *out ...@@ -369,31 +378,33 @@ int vtkParticleReader::ProduceOutputFromTextFileDouble(vtkInformationVector *out
this->File->seekg(0,ios::beg); this->File->seekg(0,ios::beg);
this->alliquot = fileLength / quantum; this->Alliquot = fileLength / quantum;
this->count = 1; this->Count = 1;
ParseLine<double> pl; ParseLine<double> pl;
while ( vtkstd::getline(*this->File,s,'\n') ) while ( vtkstd::getline(*this->File,s,'\n') )
{ {
if ( s.size() != 0 ) if ( s.size() != 0 )
{ {
bytesRead += s.size(); bytesRead += s.size();
DoProgressUpdate( bytesRead, fileLength ); this->DoProgressUpdate( bytesRead, fileLength );
double val[4]; double val[4];
val[0]=val[1]=val[2]=val[3]=0; val[0]=val[1]=val[2]=val[3]=0;
if ( pl(s,val) ) if ( pl(s,val) )
{ {
points->InsertNextPoint(val[0], val[1], val[2]); points->InsertNextPoint(val[0], val[1], val[2]);
if ( this->HasScalar) if ( this->HasScalar)
{
scalars->InsertNextValue(val[3]); scalars->InsertNextValue(val[3]);
}
} }
}
} }
}
vtkSmartPointer<vtkCellArray> vertices = vtkSmartPointer<vtkCellArray>::New(); vtkSmartPointer<vtkCellArray> vertices = vtkSmartPointer<vtkCellArray>::New();
vertices->Reset(); vertices->Reset();
this->numberOfPoints = points->GetNumberOfPoints(); this->NumberOfPoints = points->GetNumberOfPoints();
for( vtkIdType j = 0; j < (vtkIdType)this->numberOfPoints; ++j ) for( vtkIdType j = 0; j < (vtkIdType)this->NumberOfPoints; ++j )
{ {
vertices->InsertNextCell( 1 ); vertices->InsertNextCell( 1 );
vertices->InsertCellPoint( j ); vertices->InsertCellPoint( j );
...@@ -442,15 +453,15 @@ int vtkParticleReader::ProduceOutputFromTextFileFloat(vtkInformationVector *outp ...@@ -442,15 +453,15 @@ int vtkParticleReader::ProduceOutputFromTextFileFloat(vtkInformationVector *outp
this->File->seekg(0,ios::beg); this->File->seekg(0,ios::beg);
this->alliquot = fileLength / quantum; this->Alliquot = fileLength / quantum;
this->count = 1; this->Count = 1;
ParseLine<float> pl; ParseLine<float> pl;
while ( vtkstd::getline(*this->File,s,'\n') ) while ( vtkstd::getline(*this->File,s,'\n') )
{ {
if ( s.size() != 0 ) if ( s.size() != 0 )
{ {
bytesRead += s.size(); bytesRead += s.size();
DoProgressUpdate( bytesRead, fileLength ); this->DoProgressUpdate( bytesRead, fileLength );
float val[4]; float val[4];
val[0]=val[1]=val[2]=val[3]=0; val[0]=val[1]=val[2]=val[3]=0;
...@@ -458,7 +469,9 @@ int vtkParticleReader::ProduceOutputFromTextFileFloat(vtkInformationVector *outp ...@@ -458,7 +469,9 @@ int vtkParticleReader::ProduceOutputFromTextFileFloat(vtkInformationVector *outp
{ {
points->InsertNextPoint(val[0], val[1], val[2]); points->InsertNextPoint(val[0], val[1], val[2]);
if ( this->HasScalar) if ( this->HasScalar)
{
scalars->InsertNextValue(val[3]); scalars->InsertNextValue(val[3]);
}
} }
} }
} }
...@@ -466,8 +479,8 @@ int vtkParticleReader::ProduceOutputFromTextFileFloat(vtkInformationVector *outp ...@@ -466,8 +479,8 @@ int vtkParticleReader::ProduceOutputFromTextFileFloat(vtkInformationVector *outp
vtkSmartPointer<vtkCellArray> vertices = vtkSmartPointer<vtkCellArray>::New(); vtkSmartPointer<vtkCellArray> vertices = vtkSmartPointer<vtkCellArray>::New();
vertices->Reset(); vertices->Reset();
this->numberOfPoints = points->GetNumberOfPoints(); this->NumberOfPoints = points->GetNumberOfPoints();
for( vtkIdType j = 0; j < (vtkIdType)this->numberOfPoints; ++j ) for( vtkIdType j = 0; j < (vtkIdType)this->NumberOfPoints; ++j )
{ {
vertices->InsertNextCell( 1 ); vertices->InsertNextCell( 1 );
vertices->InsertCellPoint( j ); vertices->InsertCellPoint( j );
...@@ -483,7 +496,9 @@ int vtkParticleReader::ProduceOutputFromTextFileFloat(vtkInformationVector *outp ...@@ -483,7 +496,9 @@ int vtkParticleReader::ProduceOutputFromTextFileFloat(vtkInformationVector *outp
output->SetPoints(points); output->SetPoints(points);
output->SetVerts(vertices); output->SetVerts(vertices);
if ( this->HasScalar ) if ( this->HasScalar )
{
output->GetPointData()->SetScalars(scalars); output->GetPointData()->SetScalars(scalars);
}
output->Modified(); output->Modified();
...@@ -519,9 +534,13 @@ int vtkParticleReader::ProduceOutputFromBinaryFileDouble(vtkInformationVector *o ...@@ -519,9 +534,13 @@ int vtkParticleReader::ProduceOutputFromBinaryFileDouble(vtkInformationVector *o
fileLength = (unsigned long)this->File->tellg(); fileLength = (unsigned long)this->File->tellg();
if ( this->HasScalar ) if ( this->HasScalar )
this->numberOfPoints = fileLength / (4 * sizeof(double)); {
this->NumberOfPoints = fileLength / (4 * sizeof(double));
}
else else
this->numberOfPoints = fileLength / (3 * sizeof(double)); {
this->NumberOfPoints = fileLength / (3 * sizeof(double));
}
// get the info object // get the info object
vtkInformation *outInfo = outputVector->GetInformationObject(0); vtkInformation *outInfo = outputVector->GetInformationObject(0);
...@@ -531,31 +550,38 @@ int vtkParticleReader::ProduceOutputFromBinaryFileDouble(vtkInformationVector *o ...@@ -531,31 +550,38 @@ int vtkParticleReader::ProduceOutputFromBinaryFileDouble(vtkInformationVector *o
numPieces = numPieces =
outInfo->Get(vtkStreamingDemandDrivenPipeline::UPDATE_NUMBER_OF_PIECES()); outInfo->Get(vtkStreamingDemandDrivenPipeline::UPDATE_NUMBER_OF_PIECES());
if ((unsigned long)numPieces > this->numberOfPoints) if ((unsigned long)numPieces > this->NumberOfPoints)
{ {
numPieces = (int)(this->numberOfPoints); numPieces = (int)(this->NumberOfPoints);
} }
if (numPieces <= 0 || piece < 0 || piece >= numPieces) if (numPieces <= 0 || piece < 0 || piece >= numPieces)
{ {
return 0; return 0;
} }
start = piece * this->numberOfPoints / numPieces; start = piece * this->NumberOfPoints / numPieces;
next = (piece+1) * this->numberOfPoints / numPieces; next = (piece+1) * this->NumberOfPoints / numPieces;
length = next - start; length = next - start;
if ( this->HasScalar ) if ( this->HasScalar )
{
data = new double[length * 4]; data = new double[length * 4];
}
else else
{
data = new double[length * 3]; data = new double[length * 3];
}
// Seek to the first point in the file. // Seek to the first point in the file.
if ( this->HasScalar ) if ( this->HasScalar )
{
this->File->seekg(start*4*sizeof(double), ios::beg); this->File->seekg(start*4*sizeof(double), ios::beg);
}
else else
{
this->File->seekg(start*3*sizeof(double), ios::beg); this->File->seekg(start*3*sizeof(double), ios::beg);
}
if (this->File->fail()) if (this->File->fail())
{ {
vtkErrorMacro(<< "File operation failed: Seeking to " << start*4); vtkErrorMacro(<< "File operation failed: Seeking to " << start*4);
...@@ -603,10 +629,13 @@ int vtkParticleReader::ProduceOutputFromBinaryFileDouble(vtkInformationVector *o ...@@ -603,10 +629,13 @@ int vtkParticleReader::ProduceOutputFromBinaryFileDouble(vtkInformationVector *o
if (this->GetSwapBytes()) if (this->GetSwapBytes())
{ {
if ( this->HasScalar ) if ( this->HasScalar )
{
vtkByteSwap::SwapVoidRange(data, length*4, sizeof(double)); vtkByteSwap::SwapVoidRange(data, length*4, sizeof(double));
}
else else
{
vtkByteSwap::SwapVoidRange(data, length*3, sizeof(double)); vtkByteSwap::SwapVoidRange(data, length*3, sizeof(double));
}
} }
this->UpdateProgress(0.5); this->UpdateProgress(0.5);
...@@ -630,7 +659,9 @@ int vtkParticleReader::ProduceOutputFromBinaryFileDouble(vtkInformationVector *o ...@@ -630,7 +659,9 @@ int vtkParticleReader::ProduceOutputFromBinaryFileDouble(vtkInformationVector *o
while (length > 0) while (length > 0)
{ {
if ( count % 10 == 0 ) if ( count % 10 == 0 )
{
this->UpdateProgress(0.5+((count * 1000.0)/len)/2.0); this->UpdateProgress(0.5+((count * 1000.0)/len)/2.0);
}
count++; count++;
cellLength = 1000; cellLength = 1000;
if (cellLength > length) if (cellLength > length)
...@@ -648,7 +679,9 @@ int vtkParticleReader::ProduceOutputFromBinaryFileDouble(vtkInformationVector *o ...@@ -648,7 +679,9 @@ int vtkParticleReader::ProduceOutputFromBinaryFileDouble(vtkInformationVector *o
ptr += 4; ptr += 4;
} }
else else
{
ptr += 3; ptr += 3;
}
verts->InsertCellPoint(ptIdx); verts->InsertCellPoint(ptIdx);
++ptIdx; ++ptIdx;
} }
...@@ -662,7 +695,9 @@ int vtkParticleReader::ProduceOutputFromBinaryFileDouble(vtkInformationVector *o ...@@ -662,7 +695,9 @@ int vtkParticleReader::ProduceOutputFromBinaryFileDouble(vtkInformationVector *o
output->SetPoints(points); output->SetPoints(points);
output->SetVerts(verts); output->SetVerts(verts);
if ( this->HasScalar ) if ( this->HasScalar )
{
output->GetPointData()->SetScalars(array); output->GetPointData()->SetScalars(array);
}
return 1; return 1;
} }
...@@ -694,9 +729,13 @@ int vtkParticleReader::ProduceOutputFromBinaryFileFloat(vtkInformationVector *ou ...@@ -694,9 +729,13 @@ int vtkParticleReader::ProduceOutputFromBinaryFileFloat(vtkInformationVector *ou
fileLength = (unsigned long)this->File->tellg(); fileLength = (unsigned long)this->File->tellg();
if ( this->HasScalar ) if ( this->HasScalar )
this->numberOfPoints = fileLength / (4 * sizeof(float)); {
this->NumberOfPoints = fileLength / (4 * sizeof(float));
}
else else
this->numberOfPoints = fileLength / (3 * sizeof(float)); {
this->NumberOfPoints = fileLength / (3 * sizeof(float));
}
// get the info object // get the info object
...@@ -707,31 +746,39 @@ int vtkParticleReader::ProduceOutputFromBinaryFileFloat(vtkInformationVector *ou ...@@ -707,31 +746,39 @@ int vtkParticleReader::ProduceOutputFromBinaryFileFloat(vtkInformationVector *ou
numPieces = numPieces =
outInfo->Get(vtkStreamingDemandDrivenPipeline::UPDATE_NUMBER_OF_PIECES()); outInfo->Get(vtkStreamingDemandDrivenPipeline::UPDATE_NUMBER_OF_PIECES());
if ((unsigned long)numPieces > this->numberOfPoints) if ((unsigned long)numPieces > this->NumberOfPoints)
{ {
numPieces = (int)(this->numberOfPoints); numPieces = (int)(this->NumberOfPoints);
} }
if (numPieces <= 0 || piece < 0 || piece >= numPieces) if (numPieces <= 0 || piece < 0 || piece >= numPieces)
{ <