1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
|
#include <vtkCellData.h>
#include <vtkCubeSource.h>
#include <vtkDataObjectWriter.h>
#include <vtkDelaunay3D.h>
#include <vtkDirectedGraph.h>
#include <vtkEdgeListIterator.h>
#include <vtkXMLGenericDataObjectReader.h>
#include <vtkXMLDataSetWriter.h>
#include <vtkGraph.h>
#include <vtkImageData.h>
#include <vtkImageNoiseSource.h>
#include <vtkIntArray.h>
#include <vtkMutableDirectedGraph.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 <vtkUndirectedGraph.h>
#include <vtkUniformGrid.h>
#include <vtkUnstructuredGrid.h>
#include <vtkVariant.h>
namespace
{
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(vtkUniformGrid* Data)
{
InitializeData(static_cast<vtkImageData*>(Data));
}
void InitializeData(vtkUnstructuredGrid* Data)
{
vtkCubeSource* const source = vtkCubeSource::New();
vtkDelaunay3D* const delaunay = vtkDelaunay3D::New();
delaunay->AddInputConnection(source->GetOutputPort());
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 TestDataObjectXMLSerialization()
{
DataT* const output_data = DataT::New();
InitializeData(output_data);
const char* const filename = output_data->GetClassName();
vtkXMLDataSetWriter* const writer =
vtkXMLDataSetWriter::New();
writer->SetInputData(output_data);
writer->SetFileName(filename);
writer->Write();
writer->Delete();
vtkXMLGenericDataObjectReader* const reader =
vtkXMLGenericDataObjectReader::New();
reader->SetFileName(filename);
reader->Update();
vtkDataObject *obj = reader->GetOutput();
DataT* input_data = DataT::SafeDownCast(obj);
if(!input_data)
{
reader->Delete();
output_data->Delete();
return false;
}
const bool result = CompareData(output_data, input_data);
reader->Delete();
output_data->Delete();
return result;
}
bool TestUniformGridXMLSerialization()
{
vtkUniformGrid* const output_data = vtkUniformGrid::New();
InitializeData(output_data);
const char* const filename = output_data->GetClassName();
vtkXMLDataSetWriter* const writer =
vtkXMLDataSetWriter::New();
writer->SetInputData(output_data);
writer->SetFileName(filename);
writer->Write();
writer->Delete();
vtkXMLGenericDataObjectReader* const reader =
vtkXMLGenericDataObjectReader::New();
reader->SetFileName(filename);
reader->Update();
vtkDataObject *obj = reader->GetOutput();
vtkImageData* input_data = vtkImageData::SafeDownCast(obj);
if(!input_data)
{
reader->Delete();
output_data->Delete();
return false;
}
const bool result = CompareData(output_data, input_data);
reader->Delete();
output_data->Delete();
return result;
}
}
int TestDataObjectXMLIO(int /*argc*/, char* /*argv*/[])
{
int result = 0;
if(!TestDataObjectXMLSerialization<vtkImageData>())
{
cerr << "Error: failure serializing vtkImageData" << endl;
result = 1;
}
if(!TestUniformGridXMLSerialization())
{
// note that the current output from serializing a vtkUniformGrid
// is a vtkImageData. this is the same as writing out a
// vtkUniformGrid using vtkXMLImageDataWriter.
cerr << "Error: failure serializing vtkUniformGrid" << endl;
result = 1;
}
if(!TestDataObjectXMLSerialization<vtkPolyData>())
{
cerr << "Error: failure serializing vtkPolyData" << endl;
result = 1;
}
if(!TestDataObjectXMLSerialization<vtkRectilinearGrid>())
{
cerr << "Error: failure serializing vtkRectilinearGrid" << endl;
result = 1;
}
// if(!TestDataObjectXMLSerialization<vtkStructuredGrid>())
// {
// cerr << "Error: failure serializing vtkStructuredGrid" << endl;
// result = 1;
// }
if(!TestDataObjectXMLSerialization<vtkUnstructuredGrid>())
{
cerr << "Error: failure serializaing vtkUnstructuredGrid" << endl;
result = 1;
}
return result;
}
|