Commit 359107f2 authored by Rusty Blue's avatar Rusty Blue

ENH: Added reading and writing of FieldData (in vtkDataObject) to XML readers and writers.

parent 8da0b05a
......@@ -25,7 +25,7 @@
#include "vtkInformation.h"
#include "vtkExecutive.h"
vtkCxxRevisionMacro(vtkXMLDataReader, "1.14");
vtkCxxRevisionMacro(vtkXMLDataReader, "1.15");
//----------------------------------------------------------------------------
vtkXMLDataReader::vtkXMLDataReader()
......@@ -384,6 +384,44 @@ int vtkXMLDataReader::ReadPieceData()
return 1;
}
//----------------------------------------------------------------------------
void vtkXMLDataReader::ReadXMLData()
{
// Let superclasses read data. This also allocates output data.
this->Superclass::ReadXMLData();
if (this->FieldDataElement) // read the field data information
{
int i, numTuples;
vtkFieldData *fieldData = this->GetOutputDataObject(0)->GetFieldData();
for(i=0; i < this->FieldDataElement->GetNumberOfNestedElements() &&
!this->AbortExecute; i++)
{
vtkXMLDataElement* eNested = this->FieldDataElement->GetNestedElement(i);
vtkDataArray* array = this->CreateDataArray(eNested);
if (array)
{
if(eNested->GetScalarAttribute("NumberOfTuples", numTuples))
{
array->SetNumberOfTuples(numTuples);
}
else
{
numTuples = 0;
}
fieldData->AddArray(array);
array->Delete();
if (!this->ReadData(eNested, array->GetVoidPointer(0),
array->GetDataType(), 0, numTuples*array->GetNumberOfComponents()))
{
this->DataError = 1;
}
}
}
}
}
//----------------------------------------------------------------------------
int vtkXMLDataReader::ReadArrayForPoints(vtkXMLDataElement* da,
vtkDataArray* outArray)
......
......@@ -68,6 +68,8 @@ protected:
int ReadPieceData(int piece);
virtual int ReadPieceData();
virtual void ReadXMLData();
// Read a data array whose tuples coorrespond to points or cells.
virtual int ReadArrayForPoints(vtkXMLDataElement* da,
vtkDataArray* outArray);
......
......@@ -26,7 +26,7 @@
#include "vtkInformation.h"
#include "vtkStreamingDemandDrivenPipeline.h"
vtkCxxRevisionMacro(vtkXMLPDataReader, "1.15");
vtkCxxRevisionMacro(vtkXMLPDataReader, "1.16");
//----------------------------------------------------------------------------
vtkXMLPDataReader::vtkXMLPDataReader()
......@@ -354,6 +354,16 @@ int vtkXMLPDataReader::ReadPieceData()
vtkDataSet* input = this->GetPieceInputAsDataSet(this->Piece);
vtkDataSet* output = this->GetOutputAsDataSet(0);
// copy any field data
if (input->GetFieldData())
{
int i;
for (i = 0; i < input->GetFieldData()->GetNumberOfArrays(); i++)
{
output->GetFieldData()->AddArray( input->GetFieldData()->GetArray(i) );
}
}
// Copy point data and cell data for this piece.
int i;
for(i=0;i < output->GetPointData()->GetNumberOfArrays();++i)
......
......@@ -32,7 +32,7 @@
#include <sys/stat.h>
vtkCxxRevisionMacro(vtkXMLReader, "1.24");
vtkCxxRevisionMacro(vtkXMLReader, "1.25");
//----------------------------------------------------------------------------
vtkXMLReader::vtkXMLReader()
......@@ -41,6 +41,7 @@ vtkXMLReader::vtkXMLReader()
this->Stream = 0;
this->FileStream = 0;
this->XMLParser = 0;
this->FieldDataElement = 0;
this->PointDataArraySelection = vtkDataArraySelection::New();
this->CellDataArraySelection = vtkDataArraySelection::New();
this->InformationError = 0;
......@@ -362,7 +363,7 @@ int vtkXMLReader::RequestData(vtkInformation *request,
// We are just starting to execute. No errors have yet occurred.
this->XMLParser->SetAbort(0);
this->DataError = 0;
// Let the subclasses read the data they want.
this->ReadXMLData();
......@@ -390,7 +391,7 @@ int vtkXMLReader::RequestData(vtkInformation *request,
//----------------------------------------------------------------------------
void vtkXMLReader::ReadXMLData()
{
// Allocate the output's data.
// Initialize the output's data.
this->SetupOutputData();
}
......@@ -428,9 +429,22 @@ int vtkXMLReader::ReadVTKFile(vtkXMLDataElement* eVTKFile)
}
//----------------------------------------------------------------------------
int vtkXMLReader::ReadPrimaryElement(vtkXMLDataElement*)
int vtkXMLReader::ReadPrimaryElement(vtkXMLDataElement *ePrimary)
{
// We don't need any information from the primary element here.
// See if there is a FieldData element
int numNested = ePrimary->GetNumberOfNestedElements();
int i;
for(i=0; i < numNested; ++i)
{
vtkXMLDataElement* eNested = ePrimary->GetNestedElement(i);
if(strcmp(eNested->GetName(), "FieldData") == 0)
{
this->FieldDataElement = eNested;
return 1;
}
}
this->FieldDataElement = 0;
return 1;
}
......
......@@ -160,7 +160,10 @@ protected:
// The vtkXMLDataParser instance used to hide XML reading details.
vtkXMLDataParser* XMLParser;
// The FieldData element representation.
vtkXMLDataElement* FieldDataElement;
// The input file's name.
char* FileName;
......
......@@ -26,7 +26,7 @@
#include "vtkPointData.h"
#include "vtkStreamingDemandDrivenPipeline.h"
vtkCxxRevisionMacro(vtkXMLStructuredDataWriter, "1.10");
vtkCxxRevisionMacro(vtkXMLStructuredDataWriter, "1.11");
vtkCxxSetObjectMacro(vtkXMLStructuredDataWriter, ExtentTranslator,
vtkExtentTranslator);
......@@ -137,6 +137,18 @@ int vtkXMLStructuredDataWriter::ProcessRequest(
{
return 0;
}
if( this->DataMode == vtkXMLWriter::Appended && this->FieldDataOffsets)
{
// Write the field data arrays.
this->WriteFieldDataAppendedData(this->GetInput()->GetFieldData(),
this->FieldDataOffsets);
if (this->ErrorCode == vtkErrorCode::OutOfDiskSpaceError)
{
this->DeletePositionArrays();
return 0;
}
}
}
result = this->WriteAPiece();
......@@ -188,6 +200,11 @@ void vtkXMLStructuredDataWriter::DeletePositionArrays()
{
delete [] this->PointDataOffsets;
delete [] this->CellDataOffsets;
if (this->FieldDataOffsets)
{
delete [] this->FieldDataOffsets;
this->FieldDataOffsets = NULL;
}
}
//----------------------------------------------------------------------------
......@@ -208,6 +225,8 @@ int vtkXMLStructuredDataWriter::WriteHeader()
os << ">\n";
this->WriteFieldData(indent.GetNextIndent());
if(this->DataMode == vtkXMLWriter::Appended)
{
vtkIndent nextIndent = indent.GetNextIndent();
......
......@@ -30,7 +30,7 @@
#include "vtkStreamingDemandDrivenPipeline.h"
#include "vtkUnsignedCharArray.h"
vtkCxxRevisionMacro(vtkXMLUnstructuredDataWriter, "1.11");
vtkCxxRevisionMacro(vtkXMLUnstructuredDataWriter, "1.12");
//----------------------------------------------------------------------------
vtkXMLUnstructuredDataWriter::vtkXMLUnstructuredDataWriter()
......@@ -155,6 +155,18 @@ int vtkXMLUnstructuredDataWriter::ProcessRequest(
this->NumberOfPieces = numPieces;
return 0;
}
if( this->DataMode == vtkXMLWriter::Appended && this->FieldDataOffsets)
{
// Write the field data arrays.
this->WriteFieldDataAppendedData(this->GetInput()->GetFieldData(),
this->FieldDataOffsets);
if (this->ErrorCode == vtkErrorCode::OutOfDiskSpaceError)
{
this->DeletePositionArrays();
return 0;
}
}
}
result = this->WriteAPiece();
......@@ -213,8 +225,14 @@ void vtkXMLUnstructuredDataWriter::DeletePositionArrays()
delete [] this->PointsPositions;
delete [] this->PointDataPositions;
delete [] this->CellDataPositions;
if (this->FieldDataOffsets)
{
delete [] this->FieldDataOffsets;
this->FieldDataOffsets = NULL;
}
}
//----------------------------------------------------------------------------
int vtkXMLUnstructuredDataWriter::WriteHeader()
{
......@@ -225,6 +243,8 @@ int vtkXMLUnstructuredDataWriter::WriteHeader()
// Open the primary element.
os << indent << "<" << this->GetDataSetName() << ">\n";
this->WriteFieldData(indent.GetNextIndent());
if(this->DataMode == vtkXMLWriter::Appended)
{
vtkIndent nextIndent = indent.GetNextIndent();
......
......@@ -37,7 +37,7 @@
# include <io.h> /* unlink */
#endif
vtkCxxRevisionMacro(vtkXMLWriter, "1.42");
vtkCxxRevisionMacro(vtkXMLWriter, "1.43");
vtkCxxSetObjectMacro(vtkXMLWriter, Compressor, vtkDataCompressor);
//----------------------------------------------------------------------------
......@@ -80,6 +80,7 @@ vtkXMLWriter::vtkXMLWriter()
this->SetNumberOfInputPorts(1);
this->OutFile = 0;
this->FieldDataOffsets = NULL;
}
//----------------------------------------------------------------------------
......@@ -1403,7 +1404,8 @@ int vtkXMLWriter::WriteAsciiData(void* data, int numWords, int wordType,
//----------------------------------------------------------------------------
unsigned long vtkXMLWriter::WriteDataArrayAppended(vtkDataArray* a,
vtkIndent indent,
const char* alternateName)
const char* alternateName,
int writeNumTuples)
{
ostream& os = *(this->Stream);
os << indent << "<DataArray";
......@@ -1425,6 +1427,11 @@ unsigned long vtkXMLWriter::WriteDataArrayAppended(vtkDataArray* a,
this->WriteScalarAttribute("NumberOfComponents",
a->GetNumberOfComponents());
}
if(writeNumTuples)
{
this->WriteScalarAttribute("NumberOfTuples",
a->GetNumberOfTuples());
}
this->WriteDataModeAttribute("format");
unsigned long pos = this->ReserveAttributeSpace("offset");
os << "/>\n";
......@@ -1452,7 +1459,8 @@ void vtkXMLWriter::WriteDataArrayAppendedData(vtkDataArray* a,
//----------------------------------------------------------------------------
void vtkXMLWriter::WriteDataArrayInline(vtkDataArray* a, vtkIndent indent,
const char* alternateName)
const char* alternateName,
int writeNumTuples)
{
ostream& os = *(this->Stream);
os << indent << "<DataArray";
......@@ -1474,6 +1482,11 @@ void vtkXMLWriter::WriteDataArrayInline(vtkDataArray* a, vtkIndent indent,
this->WriteScalarAttribute("NumberOfComponents",
a->GetNumberOfComponents());
}
if(writeNumTuples)
{
this->WriteScalarAttribute("NumberOfTuples",
a->GetNumberOfTuples());
}
this->WriteDataModeAttribute("format");
os << ">\n";
this->WriteInlineData(a->GetVoidPointer(0),
......@@ -1505,6 +1518,62 @@ void vtkXMLWriter::WriteInlineData(void* data, int numWords, int wordType,
}
}
//----------------------------------------------------------------------------
void vtkXMLWriter::WriteFieldData(vtkIndent indent)
{
vtkFieldData *fieldData = this->GetInput()->GetFieldData();
if (!fieldData || !fieldData->GetNumberOfArrays())
{
return;
}
if(this->DataMode == vtkXMLWriter::Appended)
{
this->FieldDataOffsets = this->WriteFieldDataAppended(fieldData, indent);
}
else
{
// Write the point data arrays.
this->WriteFieldDataInline(fieldData, indent);
}
return;
}
//----------------------------------------------------------------------------
void vtkXMLWriter::WriteFieldDataInline(vtkFieldData* fd, vtkIndent indent)
{
ostream& os = *(this->Stream);
char** names = this->CreateStringArray(fd->GetNumberOfArrays());
os << indent << "<FieldData>\n";
float progressRange[2] = {0,0};
this->GetProgressRange(progressRange);
int i;
for(i=0; i < fd->GetNumberOfArrays(); ++i)
{
this->SetProgressRange(progressRange, i, fd->GetNumberOfArrays());
this->WriteDataArrayInline(fd->GetArray(i), indent.GetNextIndent(),
names[i], 1);
if (this->ErrorCode != vtkErrorCode::NoError)
{
this->DestroyStringArray(fd->GetNumberOfArrays(), names);
return;
}
}
os << indent << "</FieldData>\n";
os.flush();
if (os.fail())
{
this->SetErrorCode(vtkErrorCode::GetLastSystemError());
this->DestroyStringArray(fd->GetNumberOfArrays(), names);
return;
}
this->DestroyStringArray(fd->GetNumberOfArrays(), names);
}
//----------------------------------------------------------------------------
void vtkXMLWriter::WritePointDataInline(vtkPointData* pd, vtkIndent indent)
{
......@@ -1595,6 +1664,66 @@ void vtkXMLWriter::WriteCellDataInline(vtkCellData* cd, vtkIndent indent)
this->DestroyStringArray(cd->GetNumberOfArrays(), names);
}
unsigned long* vtkXMLWriter::WriteFieldDataAppended(vtkFieldData* fd,
vtkIndent indent)
{
ostream& os = *(this->Stream);
unsigned long* fdPositions = new unsigned long [fd->GetNumberOfArrays()];
char** names = this->CreateStringArray(fd->GetNumberOfArrays());
os << indent << "<FieldData>\n";
int i;
for(i=0; i < fd->GetNumberOfArrays(); ++i)
{
fdPositions[i] = this->WriteDataArrayAppended(fd->GetArray(i),
indent.GetNextIndent(),
names[i], 1);
if (this->ErrorCode != vtkErrorCode::NoError)
{
delete [] fdPositions;
this->DestroyStringArray(fd->GetNumberOfArrays(), names);
return NULL;
}
}
os << indent << "</FieldData>\n";
os.flush();
if (os.fail())
{
this->SetErrorCode(vtkErrorCode::GetLastSystemError());
delete [] fdPositions;
this->DestroyStringArray(fd->GetNumberOfArrays(), names);
return NULL;
}
this->DestroyStringArray(fd->GetNumberOfArrays(), names);
return fdPositions;
}
//----------------------------------------------------------------------------
void vtkXMLWriter::WriteFieldDataAppendedData(vtkFieldData* fd,
unsigned long* fdPositions)
{
float progressRange[2] = {0,0};
this->GetProgressRange(progressRange);
int i;
for(i=0; i < fd->GetNumberOfArrays(); ++i)
{
this->SetProgressRange(progressRange, i, fd->GetNumberOfArrays());
this->WriteDataArrayAppendedData(fd->GetArray(i), fdPositions[i]);
if (this->ErrorCode != vtkErrorCode::NoError)
{
return;
}
}
}
//----------------------------------------------------------------------------
unsigned long* vtkXMLWriter::WritePointDataAppended(vtkPointData* pd,
vtkIndent indent)
......
......@@ -32,6 +32,7 @@ class vtkDataSetAttributes;
class vtkOutputStream;
class vtkPointData;
class vtkPoints;
class vtkFieldData;
class VTK_IO_EXPORT vtkXMLWriter : public vtkAlgorithm
{
......@@ -168,6 +169,9 @@ protected:
// The stream position at which appended data starts.
unsigned long AppendedDataPosition;
// appended data offsets for field data
unsigned long* FieldDataOffsets;
//BTX
// We need a 32 bit unsigned integer type for platform-independent
// binary headers. Note that this is duplicated in
......@@ -302,16 +306,20 @@ protected:
int WriteStringAttribute(const char* name, const char* value);
unsigned long WriteDataArrayAppended(vtkDataArray* a, vtkIndent indent,
const char* alternateName=0);
const char* alternateName=0, int writeNumTuples=0);
void WriteDataArrayAppendedData(vtkDataArray* a, unsigned long pos);
void WriteDataArrayInline(vtkDataArray* a, vtkIndent indent,
const char* alternateName=0);
const char* alternateName=0, int writeNumTuples=0);
void WriteInlineData(void* data, int numWords, int wordType,
vtkIndent indent);
// Methods for writing points, point data, and cell data.
void WriteFieldData(vtkIndent indent);
void WriteFieldDataInline(vtkFieldData* fd, vtkIndent indent);
void WritePointDataInline(vtkPointData* pd, vtkIndent indent);
void WriteCellDataInline(vtkCellData* cd, vtkIndent indent);
unsigned long* WriteFieldDataAppended(vtkFieldData* fd, vtkIndent indent);
void WriteFieldDataAppendedData(vtkFieldData* pd, unsigned long* fdPositions);
unsigned long* WritePointDataAppended(vtkPointData* pd, vtkIndent indent);
void WritePointDataAppendedData(vtkPointData* pd, unsigned long* pdPositions);
unsigned long* WriteCellDataAppended(vtkCellData* cd, vtkIndent indent);
......
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