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 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420
|
#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 <vtkFieldData.h>
#include <vtkFloatArray.h>
#include <vtkGraph.h>
#include <vtkImageData.h>
#include <vtkImageNoiseSource.h>
#include <vtkInformation.h>
#include <vtkIntArray.h>
#include <vtkMutableDirectedGraph.h>
#include <vtkNew.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>
#include <string>
// Serializable keys to test:
#include "vtkInformationDoubleKey.h"
#include "vtkInformationDoubleVectorKey.h"
#include "vtkInformationIdTypeKey.h"
#include "vtkInformationIntegerKey.h"
#include "vtkInformationIntegerVectorKey.h"
#include "vtkInformationStringKey.h"
#include "vtkInformationStringVectorKey.h"
#include "vtkInformationUnsignedLongKey.h"
namespace
{
static vtkInformationDoubleKey *TestDoubleKey =
vtkInformationDoubleKey::MakeKey("Double", "XMLTestKey");
// Test RequiredLength keys. DoubleVector must have Length() == 3
static vtkInformationDoubleVectorKey *TestDoubleVectorKey =
vtkInformationDoubleVectorKey::MakeKey("DoubleVector", "XMLTestKey", 3);
static vtkInformationIdTypeKey *TestIdTypeKey =
vtkInformationIdTypeKey::MakeKey("IdType", "XMLTestKey");
static vtkInformationIntegerKey *TestIntegerKey =
vtkInformationIntegerKey::MakeKey("Integer", "XMLTestKey");
static vtkInformationIntegerVectorKey *TestIntegerVectorKey =
vtkInformationIntegerVectorKey::MakeKey("IntegerVector", "XMLTestKey");
static vtkInformationStringKey *TestStringKey =
vtkInformationStringKey::MakeKey("String", "XMLTestKey");
static vtkInformationStringVectorKey *TestStringVectorKey =
vtkInformationStringVectorKey::MakeKey("StringVector", "XMLTestKey");
static vtkInformationUnsignedLongKey *TestUnsignedLongKey =
vtkInformationUnsignedLongKey::MakeKey("UnsignedLong", "XMLTestKey");
bool stringEqual(const std::string &expect, const std::string &actual)
{
if (expect != actual)
{
std::cerr << "Strings do not match! Expected: '" << expect << "', got: '"
<< actual << "'.\n";
return false;
}
return true;
}
bool stringEqual(const std::string &expect, const char *actual)
{
return stringEqual(expect, std::string(actual ? actual : ""));
}
template <typename T>
bool compareValues(const std::string &desc, T expect, T actual)
{
if (expect != actual)
{
std::cerr << "Failed comparison for '" << desc << "'. Expected '" << expect
<< "', got '" << actual << "'.\n";
return false;
}
return true;
}
void InitializeDataCommon(vtkDataObject *data)
{
vtkFieldData *fd = data->GetFieldData();
if (!fd)
{
fd = vtkFieldData::New();
data->SetFieldData(fd);
fd->FastDelete();
}
// Add a dummy array to test component name and information key serialization.
vtkNew<vtkFloatArray> array;
array->SetName("Test Array");
fd->AddArray(array.GetPointer());
array->SetNumberOfComponents(3);
array->SetComponentName(0, "Component 0 name");
array->SetComponentName(1, "Component 1 name");
array->SetComponentName(2, "Component 2 name");
// Test information keys that can be serialized
vtkInformation *info = array->GetInformation();
info->Set(TestDoubleKey, 1.0);
// Setting from an array, since keys with RequiredLength cannot use Append.
double doubleVecData[3] = {1., 90., 260.};
info->Set(TestDoubleVectorKey, doubleVecData, 3);
info->Set(TestIdTypeKey, 5);
info->Set(TestIntegerKey, 408);
info->Append(TestIntegerVectorKey, 1);
info->Append(TestIntegerVectorKey, 5);
info->Append(TestIntegerVectorKey, 45);
info->Set(TestStringKey, "Test String!\nLine2");
info->Append(TestStringVectorKey, "First");
info->Append(TestStringVectorKey, "Second (with whitespace!)");
info->Append(TestStringVectorKey, "Third (with\nnewline!)");
info->Set(TestUnsignedLongKey, 9);
}
bool CompareDataCommon(vtkDataObject *data)
{
vtkFieldData *fd = data->GetFieldData();
if (!fd)
{
std::cerr << "Field data object missing.\n";
return false;
}
vtkDataArray *array = fd->GetArray("Test Array");
if (!array)
{
std::cerr << "Missing testing array from field data.\n";
return false;
}
if (array->GetNumberOfComponents() != 3)
{
std::cerr << "Test array expected to have 3 components, has "
<< array->GetNumberOfComponents() << std::endl;
return false;
}
if (!array->GetComponentName(0) ||
(strcmp("Component 0 name", array->GetComponentName(0)) != 0) ||
!array->GetComponentName(1) ||
(strcmp("Component 1 name", array->GetComponentName(1)) != 0) ||
!array->GetComponentName(2) ||
(strcmp("Component 2 name", array->GetComponentName(2)) != 0))
{
std::cerr << "Incorrect component names on test array.\n";
return false;
}
vtkInformation *info = array->GetInformation();
if (!info)
{
std::cerr << "Missing array information.\n";
return false;
}
if (!compareValues("double key", 1., info->Get(TestDoubleKey)) ||
!compareValues("double vector key length", 3, info->Length(TestDoubleVectorKey)) ||
!compareValues("double vector key @0", 1., info->Get(TestDoubleVectorKey, 0)) ||
!compareValues("double vector key @1", 90., info->Get(TestDoubleVectorKey, 1)) ||
!compareValues("double vector key @2", 260., info->Get(TestDoubleVectorKey, 2)) ||
!compareValues<vtkIdType>("idtype key", 5, info->Get(TestIdTypeKey)) ||
!compareValues("integer key", 408, info->Get(TestIntegerKey)) ||
!compareValues("integer vector key length", 3, info->Length(TestIntegerVectorKey)) ||
!compareValues("integer vector key @0", 1, info->Get(TestIntegerVectorKey, 0)) ||
!compareValues("integer vector key @1", 5, info->Get(TestIntegerVectorKey, 1)) ||
!compareValues("integer vector key @2", 45, info->Get(TestIntegerVectorKey, 2)) ||
!stringEqual("Test String!\nLine2", info->Get(TestStringKey)) ||
!compareValues("string vector key length", 3, info->Length(TestStringVectorKey)) ||
!stringEqual("First", info->Get(TestStringVectorKey, 0)) ||
!stringEqual("Second (with whitespace!)", info->Get(TestStringVectorKey, 1)) ||
!stringEqual("Third (with\nnewline!)", info->Get(TestStringVectorKey, 2)) ||
!compareValues("unsigned long key", 9ul, info->Get(TestUnsignedLongKey)))
{
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();
InitializeDataCommon(Data);
}
bool CompareData(vtkImageData* Output, vtkImageData* Input)
{
// Compare both input and output as a sanity check.
if (!CompareDataCommon(Input) || !CompareDataCommon(Output))
{
return false;
}
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();
InitializeDataCommon(Data);
}
bool CompareData(vtkPolyData* Output, vtkPolyData* Input)
{
// Compare both input and output as a sanity check.
if (!CompareDataCommon(Input) || !CompareDataCommon(Output))
{
return false;
}
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);
InitializeDataCommon(Data);
}
bool CompareData(vtkRectilinearGrid* Output, vtkRectilinearGrid* Input)
{
// Compare both input and output as a sanity check.
if (!CompareDataCommon(Input) || !CompareDataCommon(Output))
{
return false;
}
if(memcmp(Input->GetDimensions(), Output->GetDimensions(), 3 * sizeof(int)))
return false;
return true;
}
void InitializeData(vtkUniformGrid* Data)
{
InitializeData(static_cast<vtkImageData*>(Data));
InitializeDataCommon(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();
InitializeDataCommon(Data);
}
bool CompareData(vtkUnstructuredGrid* Output, vtkUnstructuredGrid* Input)
{
// Compare both input and output as a sanity check.
if (!CompareDataCommon(Input) || !CompareDataCommon(Output))
{
return false;
}
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 serializing vtkUnstructuredGrid" << endl;
result = 1;
}
return result;
}
|