Commit aa869a5c authored by Timothy M. Shead's avatar Timothy M. Shead
Browse files

ENH: Created vtkGraphReader, vtkGraphWriter, vtkTableReader, vtkTableWriter,...

ENH: Created vtkGraphReader, vtkGraphWriter, vtkTableReader, vtkTableWriter, vtkTreeReader, and vtkTreeWriter classes for serializing infovis data structures
ENH: Created vtkGenericDataObjectReader and vtkGenericDataObjectWriter classes which can serialize any vtkDataObject derivative (the entire object, not just the fields)
parent 3c24316b
......@@ -73,6 +73,9 @@
#define VTK_GENERIC_DATA_SET 16
#define VTK_HYPER_OCTREE 17
#define VTK_TEMPORAL_DATA_SET 18
#define VTK_TABLE 19
#define VTK_GRAPH 20
#define VTK_TREE 21
/*--------------------------------------------------------------------------*/
/* Define a casting macro for use by the constants below. */
......
......@@ -44,6 +44,10 @@ public:
vtkTypeRevisionMacro(vtkGraph,vtkAbstractGraph);
void PrintSelf(ostream& os, vtkIndent indent);
// Description:
// Return what type of dataset this is.
int GetDataObjectType() {return VTK_GRAPH;}
// Description:
// The number of nodes in the graph.
virtual vtkIdType GetNumberOfNodes();
......
......@@ -28,7 +28,7 @@
// Standard functions
//
vtkCxxRevisionMacro(vtkTable, "1.4");
vtkCxxRevisionMacro(vtkTable, "1.5");
vtkStandardNewMacro(vtkTable);
//----------------------------------------------------------------------------
......@@ -433,3 +433,13 @@ vtkTable* vtkTable::GetData(vtkInformationVector* v, int i)
{
return vtkTable::GetData(v->GetInformationObject(i));
}
void vtkTable::ShallowCopy(vtkDataObject* src)
{
if(vtkTable* const table = vtkTable::SafeDownCast(src))
{
this->Rows = table->Rows;
}
Superclass::ShallowCopy(src);
}
......@@ -49,6 +49,10 @@ public:
vtkTypeRevisionMacro(vtkTable, vtkDataObject);
void PrintSelf(ostream &os, vtkIndent indent);
// Description:
// Return what type of dataset this is.
int GetDataObjectType() {return VTK_TABLE;}
// Description:
// Sets the field data for the table.
virtual void SetFieldData(vtkFieldData* data);
......@@ -141,6 +145,8 @@ public:
static vtkTable* GetData(vtkInformation* info);
static vtkTable* GetData(vtkInformationVector* v, int i=0);
virtual void ShallowCopy(vtkDataObject* src);
protected:
vtkTable();
~vtkTable() {}
......
......@@ -36,6 +36,10 @@ public:
vtkTypeRevisionMacro(vtkTree,vtkAbstractGraph);
void PrintSelf(ostream& os, vtkIndent indent);
// Description:
// Return what type of dataset this is.
int GetDataObjectType() {return VTK_TREE;}
// Description:
// The number of nodes in the graph.
virtual vtkIdType GetNumberOfNodes();
......
......@@ -36,12 +36,16 @@ vtkEnSightMasterServerReader.cxx
vtkEnSightReader.cxx
vtkFLUENTReader.cxx
vtkFacetWriter.cxx
vtkGenericDataObjectReader.cxx
vtkGenericDataObjectWriter.cxx
vtkGESignaReader.cxx
vtkGAMBITReader.cxx
vtkGaussianCubeReader.cxx
vtkGenericEnSightReader.cxx
vtkGenericMovieWriter.cxx
vtkGlobFileNames.cxx
vtkGraphReader.cxx
vtkGraphWriter.cxx
vtkIVWriter.cxx
vtkImageReader.cxx
vtkImageReader2.cxx
......@@ -94,8 +98,12 @@ vtkStructuredGridReader.cxx
vtkStructuredGridWriter.cxx
vtkStructuredPointsReader.cxx
vtkStructuredPointsWriter.cxx
vtkTableReader.cxx
vtkTableWriter.cxx
vtkTIFFReader.cxx
vtkTIFFWriter.cxx
vtkTreeReader.cxx
vtkTreeWriter.cxx
vtkUGFacetReader.cxx
vtkUnstructuredGridReader.cxx
vtkUnstructuredGridWriter.cxx
......
......@@ -18,11 +18,12 @@ ENDIF(VTK_USE_DISPLAY AND VTK_USE_RENDERING)
CREATE_TEST_SOURCELIST(Tests ${KIT}CxxTests.cxx
TestXML.cxx
TestCompress.cxx
TestDataObjectIO.cxx
${ConditionalTests}
EXTRA_INCLUDE vtkTestDriver.h
)
ADD_EXECUTABLE(${KIT}CxxTests ${Tests})
TARGET_LINK_LIBRARIES(${KIT}CxxTests vtkIO vtkImaging vtksys)
TARGET_LINK_LIBRARIES(${KIT}CxxTests vtkIO vtkImaging vtkInfovis vtksys)
IF (VTK_USE_DISPLAY AND VTK_USE_RENDERING)
TARGET_LINK_LIBRARIES(${KIT}CxxTests vtkRendering)
......@@ -51,3 +52,6 @@ ENDIF(DEFINED vtkMPEG2Encode_INCLUDE_PATH)
IF(VTK_USE_FFMPEG_ENCODER)
ADD_TEST(TestFFMPEGWriter ${CXX_TEST_PATH}/${KIT}CxxTests TestFFMPEGWriter)
ENDIF(VTK_USE_FFMPEG_ENCODER)
ADD_TEST(TestDataObjectIO ${CXX_TEST_PATH}/${KIT}CxxTests TestDataObjectIO)
#include <iostream>
#include <vtkCellData.h>
#include <vtkCubeSource.h>
#include <vtkDataObjectWriter.h>
#include <vtkDelaunay3D.h>
#include <vtkGenericDataObjectReader.h>
#include <vtkGenericDataObjectWriter.h>
#include <vtkGraph.h>
#include <vtkImageData.h>
#include <vtkImageNoiseSource.h>
#include <vtkIntArray.h>
#include <vtkPointData.h>
#include <vtkPolyData.h>
#include <vtkRandomGraphSource.h>
#include <vtkRectilinearGrid.h>
#include <vtkSmartPointer.h>
#include <vtkStructuredGrid.h>
#include <vtkTable.h>
#include <vtkTree.h>
#include <vtkUnstructuredGrid.h>
#include <vtkVariant.h>
void InitializeData(vtkGraph* Data)
{
vtkRandomGraphSource* const source = vtkRandomGraphSource::New();
source->SetNumberOfNodes(5);
source->SetNumberOfArcs(10);
source->IncludeArcWeightsOn();
source->DirectedOn();
source->UseArcProbabilityOff();
source->StartWithTreeOff();
source->AllowSelfLoopsOff();
source->Update();
Data->ShallowCopy(source->GetOutput());
source->Delete();
}
bool CompareData(vtkGraph* Output, vtkGraph* Input)
{
if(Input->GetDirected() != Output->GetDirected())
return false;
if(Input->GetNumberOfNodes() != Output->GetNumberOfNodes())
return false;
if(Input->GetNumberOfArcs() != Output->GetNumberOfArcs())
return false;
if(Input->GetPointData()->GetNumberOfArrays() != Output->GetPointData()->GetNumberOfArrays())
return false;
if(Input->GetCellData()->GetNumberOfArrays() != Output->GetCellData()->GetNumberOfArrays())
return false;
for(int arc = 0; arc != Input->GetNumberOfArcs(); ++arc)
{
if(Input->GetSourceNode(arc) != Output->GetSourceNode(arc))
return false;
if(Input->GetTargetNode(arc) != Output->GetTargetNode(arc))
return false;
}
return true;
}
void InitializeData(vtkImageData* Data)
{
vtkImageNoiseSource* const source = vtkImageNoiseSource::New();
source->SetWholeExtent(0, 15, 0, 15, 0, 0);
source->Update();
Data->ShallowCopy(source->GetOutput());
source->Delete();
}
bool CompareData(vtkImageData* Output, vtkImageData* Input)
{
if(memcmp(Input->GetDimensions(), Output->GetDimensions(), 3 * sizeof(int)))
return false;
const int point_count = Input->GetDimensions()[0] * Input->GetDimensions()[1] * Input->GetDimensions()[2];
for(int point = 0; point != point_count; ++point)
{
if(memcmp(Input->GetPoint(point), Output->GetPoint(point), 3 * sizeof(double)))
return false;
}
return true;
}
void InitializeData(vtkPolyData* Data)
{
vtkCubeSource* const source = vtkCubeSource::New();
source->Update();
Data->ShallowCopy(source->GetOutput());
source->Delete();
}
bool CompareData(vtkPolyData* Output, vtkPolyData* Input)
{
if(Input->GetNumberOfPoints() != Output->GetNumberOfPoints())
return false;
if(Input->GetNumberOfPolys() != Output->GetNumberOfPolys())
return false;
return true;
}
void InitializeData(vtkRectilinearGrid* Data)
{
Data->SetDimensions(2, 3, 4);
}
bool CompareData(vtkRectilinearGrid* Output, vtkRectilinearGrid* Input)
{
if(memcmp(Input->GetDimensions(), Output->GetDimensions(), 3 * sizeof(int)))
return false;
return true;
}
void InitializeData(vtkStructuredGrid* Data)
{
Data->SetDimensions(2, 3, 4);
}
bool CompareData(vtkStructuredGrid* Output, vtkStructuredGrid* Input)
{
if(memcmp(Input->GetDimensions(), Output->GetDimensions(), 3 * sizeof(int)))
return false;
return true;
}
void InitializeData(vtkTable* Data)
{
vtkIntArray* const column1 = vtkIntArray::New();
Data->AddColumn(column1);
column1->Delete();
column1->SetName("column1");
vtkIntArray* const column2 = vtkIntArray::New();
Data->AddColumn(column2);
column2->Delete();
column2->SetName("column2");
Data->InsertNextBlankRow();
Data->InsertNextBlankRow();
Data->InsertNextBlankRow();
Data->SetValue(0, 0, 1);
Data->SetValue(0, 1, 2);
Data->SetValue(1, 0, 3);
Data->SetValue(1, 1, 4);
Data->SetValue(2, 0, 5);
Data->SetValue(2, 1, 6);
}
bool CompareData(vtkTable* Output, vtkTable* Input)
{
if(Input->GetNumberOfColumns() != Output->GetNumberOfColumns())
return false;
if(Input->GetNumberOfRows() != Output->GetNumberOfRows())
return false;
for(int column = 0; column != Input->GetNumberOfColumns(); ++column)
{
for(int row = 0; row != Input->GetNumberOfRows(); ++row)
{
if(Input->GetValue(row, column).ToDouble() != Output->GetValue(row, column).ToDouble())
{
return false;
}
}
}
return true;
}
void InitializeData(vtkTree* Data)
{
const vtkIdType node0 = Data->AddRoot();
Data->AddChild(node0);
const vtkIdType node2 = Data->AddChild(node0);
Data->AddChild(node0);
Data->AddChild(node0);
Data->SetRoot(node2);
}
bool CompareData(vtkTree* Output, vtkTree* Input)
{
if(Input->GetNumberOfNodes() != Output->GetNumberOfNodes())
return false;
if(Input->GetNumberOfArcs() != Output->GetNumberOfArcs())
return false;
if(Input->GetPointData()->GetNumberOfArrays() != Output->GetPointData()->GetNumberOfArrays())
return false;
if(Input->GetCellData()->GetNumberOfArrays() != Output->GetCellData()->GetNumberOfArrays())
return false;
if(Input->GetRoot() != Output->GetRoot())
return false;
for(vtkIdType child = 0; child != Input->GetNumberOfNodes(); ++child)
{
if(Input->GetParent(child) != Output->GetParent(child))
return false;
}
return true;
}
void InitializeData(vtkUnstructuredGrid* Data)
{
vtkCubeSource* const source = vtkCubeSource::New();
vtkDelaunay3D* const delaunay = vtkDelaunay3D::New();
delaunay->AddInput(source->GetOutput());
delaunay->Update();
Data->ShallowCopy(delaunay->GetOutput());
delaunay->Delete();
source->Delete();
}
bool CompareData(vtkUnstructuredGrid* Output, vtkUnstructuredGrid* Input)
{
if(Input->GetNumberOfPoints() != Output->GetNumberOfPoints())
return false;
if(Input->GetNumberOfCells() != Output->GetNumberOfCells())
return false;
return true;
}
template<typename DataT>
bool TestDataObjectSerialization()
{
DataT* const output_data = DataT::New();
InitializeData(output_data);
const char* const filename = output_data->GetClassName();
vtkGenericDataObjectWriter* const writer = vtkGenericDataObjectWriter::New();
writer->SetInput(output_data);
writer->SetFileName(filename);
writer->Write();
writer->Delete();
vtkGenericDataObjectReader* const reader = vtkGenericDataObjectReader::New();
reader->SetFileName(filename);
reader->Update();
DataT* const input_data = DataT::SafeDownCast(reader->GetOutput());
if(!input_data)
return false;
const bool result = CompareData(output_data, input_data);
reader->Delete();
output_data->Delete();
return result;
}
int TestDataObjectIO(int /*argc*/, char* /*argv*/[])
{
int result = 0;
if(!TestDataObjectSerialization<vtkGraph>())
{
cerr << "Error: failure serializing vtkGraph" << endl;
result = 1;
}
if(!TestDataObjectSerialization<vtkImageData>())
{
cerr << "Error: failure serializing vtkImageData" << endl;
result = 1;
}
if(!TestDataObjectSerialization<vtkPolyData>())
{
cerr << "Error: failure serializing vtkPolyData" << endl;
result = 1;
}
if(!TestDataObjectSerialization<vtkRectilinearGrid>())
{
cerr << "Error: failure serializing vtkRectilinearGrid" << endl;
result = 1;
}
if(!TestDataObjectSerialization<vtkStructuredGrid>())
{
cerr << "Error: failure serializing vtkStructuredGrid" << endl;
result = 1;
}
if(!TestDataObjectSerialization<vtkTable>())
{
cerr << "Error: failure serializing vtkTable" << endl;
result = 1;
}
if(!TestDataObjectSerialization<vtkTree>())
{
cerr << "Error: failure serializing vtkTree" << endl;
result = 1;
}
if(!TestDataObjectSerialization<vtkUnstructuredGrid>())
{
cerr << "Error: failure serializaing vtkUnstructuredGrid" << endl;
result = 1;
}
return result;
}
......@@ -37,7 +37,7 @@
#include "vtkUnsignedLongArray.h"
#include "vtkUnsignedShortArray.h"
vtkCxxRevisionMacro(vtkDataWriter, "1.115");
vtkCxxRevisionMacro(vtkDataWriter, "1.116");
vtkStandardNewMacro(vtkDataWriter);
// this undef is required on the hp. vtkMutexLock ends up including
......@@ -239,16 +239,41 @@ int vtkDataWriter::WriteCellData(ostream *fp, vtkDataSet *ds)
vtkDebugMacro(<<"Writing cell data...");
numCells = ds->GetNumberOfCells();
if(numCells <= 0)
{
vtkDebugMacro(<<"No cell data to write!");
return 1;
}
scalars = cd->GetScalars();
if(scalars && scalars->GetNumberOfTuples() <= 0)
scalars = 0;
vectors = cd->GetVectors();
if(vectors && vectors->GetNumberOfTuples() <= 0)
vectors = 0;
normals = cd->GetNormals();
if(normals && normals->GetNumberOfTuples() <= 0)
normals = 0;
tcoords = cd->GetTCoords();
if(tcoords && tcoords->GetNumberOfTuples() <= 0)
tcoords = 0;
tensors = cd->GetTensors();
if(tensors && tensors->GetNumberOfTuples() <= 0)
tensors = 0;
globalIds = cd->GetGlobalIds();
if(globalIds && globalIds->GetNumberOfTuples() <= 0)
globalIds = 0;
field = cd;
if(field && field->GetNumberOfTuples() <= 0)
field = 0;
if ( numCells <= 0 || !(scalars || vectors || normals || tcoords ||
tensors || field))
if(!(scalars || vectors || normals || tcoords || tensors || globalIds || field))
{
vtkDebugMacro(<<"No cell data to write!");
return 1;
......@@ -258,7 +283,7 @@ int vtkDataWriter::WriteCellData(ostream *fp, vtkDataSet *ds)
//
// Write scalar data
//
if ( scalars && scalars->GetNumberOfTuples() > 0 )
if( scalars )
{
if ( ! this->WriteScalarData(fp, scalars, numCells) )
{
......@@ -268,7 +293,7 @@ int vtkDataWriter::WriteCellData(ostream *fp, vtkDataSet *ds)
//
// Write vector data
//
if ( vectors && vectors->GetNumberOfTuples() > 0 )
if( vectors )
{
if ( ! this->WriteVectorData(fp, vectors, numCells) )
{
......@@ -278,7 +303,7 @@ int vtkDataWriter::WriteCellData(ostream *fp, vtkDataSet *ds)
//
// Write normals
//
if ( normals && normals->GetNumberOfTuples() > 0 )
if ( normals )
{
if ( ! this->WriteNormalData(fp, normals, numCells) )
{
......@@ -288,7 +313,7 @@ int vtkDataWriter::WriteCellData(ostream *fp, vtkDataSet *ds)
//
// Write texture coords
//
if ( tcoords && tcoords->GetNumberOfTuples() > 0 )
if ( tcoords )
{
if ( ! this->WriteTCoordData(fp, tcoords, numCells) )
{
......@@ -298,7 +323,7 @@ int vtkDataWriter::WriteCellData(ostream *fp, vtkDataSet *ds)
//
// Write tensors
//
if ( tensors && tensors->GetNumberOfTuples() > 0 )
if ( tensors )
{
if ( ! this->WriteTensorData(fp, tensors, numCells) )
{
......@@ -308,7 +333,7 @@ int vtkDataWriter::WriteCellData(ostream *fp, vtkDataSet *ds)
//
// Write global ids
//
if ( globalIds && globalIds->GetNumberOfTuples() > 0 )
if ( globalIds )
{
if ( ! this->WriteGlobalIdData(fp, globalIds, numCells) )
{
......@@ -318,7 +343,7 @@ int vtkDataWriter::WriteCellData(ostream *fp, vtkDataSet *ds)
//
// Write field
//
if ( field && field->GetNumberOfTuples() > 0 )
if ( field )
{
if ( ! this->WriteFieldData(fp, field) )
{
......@@ -346,16 +371,41 @@ int vtkDataWriter::WritePointData(ostream *fp, vtkDataSet *ds)
vtkDebugMacro(<<"Writing point data...");
numPts = ds->GetNumberOfPoints();
if(numPts <= 0)
{
vtkDebugMacro(<<"No point data to write!");
return 1;
}
scalars = pd->GetScalars();
if(scalars && scalars->GetNumberOfTuples() <= 0)
scalars = 0;
vectors = pd->GetVectors();
if(vectors && vectors->GetNumberOfTuples() <= 0)
vectors = 0;
normals = pd->GetNormals();
if(normals && normals->GetNumberOfTuples() <= 0)
normals = 0;
tcoords = pd->GetTCoords();
if(tcoords && tcoords->GetNumberOfTuples() <= 0)
tcoords = 0;
tensors = pd->GetTensors();
if(tensors && tensors->GetNumberOfTuples() <= 0)
tensors = 0;
globalIds = pd->GetGlobalIds();
if(globalIds && globalIds->GetNumberOfTuples() <= 0)
globalIds = 0;
field = pd;
if(field && field->GetNumberOfTuples() <= 0)
field = 0;
if ( numPts <= 0 || !(scalars || vectors || normals || tcoords ||
tensors || field))
if(!(scalars || vectors || normals || tcoords || tensors || globalIds || field))
{
vtkDebugMacro(<<"No point data to write!");
return 1;
......@@ -365,7 +415,7 @@ int vtkDataWriter::WritePointData(ostream *fp, vtkDataSet *ds)
//
// Write scalar data
//
if ( scalars && scalars->GetNumberOfTuples() > 0 )
if ( scalars )
{
if ( ! this->WriteScalarData(fp, scalars, numPts) )
{
......@@ -375,7 +425,7 @@ int vtkDataWriter::WritePointData(ostream *fp, vtkDataSet *ds)
//
// Write vector data
//