Add legacy VTK file readers

This commit is contained in:
Sujin Philip 2015-10-26 10:07:36 -04:00
parent 557f4dcd0a
commit cf372c7014
13 changed files with 1916 additions and 2 deletions

@ -18,6 +18,11 @@
## this software.
##============================================================================
set(headers
ErrorIO.h
)
vtkm_declare_headers(${headers})
add_subdirectory(reader)
add_subdirectory(writers)

38
vtkm/io/ErrorIO.h Normal file

@ -0,0 +1,38 @@
//============================================================================
// Copyright (c) Kitware, Inc.
// All rights reserved.
// See LICENSE.txt for details.
// This software is distributed WITHOUT ANY WARRANTY; without even
// the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
// PURPOSE. See the above copyright notice for more information.
//
// Copyright 2014 Sandia Corporation.
// Copyright 2014 UT-Battelle, LLC.
// Copyright 2014 Los Alamos National Security.
//
// Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
// the U.S. Government retains certain rights in this software.
//
// Under the terms of Contract DE-AC52-06NA25396 with Los Alamos National
// Laboratory (LANL), the U.S. Government retains certain rights in
// this software.
//============================================================================
#ifndef vtk_m_io_ErrorIO_h
#define vtk_m_io_ErrorIO_h
#include <vtkm/cont/Error.h>
namespace vtkm {
namespace io {
class ErrorIO : public vtkm::cont::Error
{
public:
ErrorIO() { }
ErrorIO(const std::string message) : Error(message) { }
};
}
} // namespace vtkm::io
#endif //vtk_m_io_ErrorIO_h

@ -0,0 +1,33 @@
##============================================================================
## Copyright (c) Kitware, Inc.
## All rights reserved.
## See LICENSE.txt for details.
## This software is distributed WITHOUT ANY WARRANTY; without even
## the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
## PURPOSE. See the above copyright notice for more information.
##
## Copyright 2014 Sandia Corporation.
## Copyright 2014 UT-Battelle, LLC.
## Copyright 2014 Los Alamos National Security.
##
## Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
## the U.S. Government retains certain rights in this software.
##
## Under the terms of Contract DE-AC52-06NA25396 with Los Alamos National
## Laboratory (LANL), the U.S. Government retains certain rights in
## this software.
##============================================================================
set(headers
VTKDataSetReaderBase.h
VTKPolyDataReader.h
VTKStructuredGridReader.h
VTKStructuredPointsReader.h
VTKUnstructuredGridReader.h
VTKDataSetReader.h
)
vtkm_declare_headers(${headers})
add_subdirectory(internal)
add_subdirectory(testing)

@ -0,0 +1,101 @@
//============================================================================
// Copyright (c) Kitware, Inc.
// All rights reserved.
// See LICENSE.txt for details.
// This software is distributed WITHOUT ANY WARRANTY; without even
// the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
// PURPOSE. See the above copyright notice for more information.
//
// Copyright 2015 Sandia Corporation.
// Copyright 2015 UT-Battelle, LLC.
// Copyright 2015 Los Alamos National Security.
//
// Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
// the U.S. Government retains certain rights in this software.
//
// Under the terms of Contract DE-AC52-06NA25396 with Los Alamos National
// Laboratory (LANL), the U.S. Government retains certain rights in
// this software.
//============================================================================
#ifndef vtk_m_io_reader_VTKDataSetReader_h
#define vtk_m_io_reader_VTKDataSetReader_h
#include <vtkm/io/reader/VTKDataSetReaderBase.h>
#include <vtkm/io/reader/VTKStructuredPointsReader.h>
#include <vtkm/io/reader/VTKStructuredGridReader.h>
#include <vtkm/io/reader/VTKPolyDataReader.h>
#include <vtkm/io/reader/VTKUnstructuredGridReader.h>
VTKM_THIRDPARTY_PRE_INCLUDE
#include <boost/smart_ptr/scoped_ptr.hpp>
VTKM_THIRDPARTY_POST_INCLUDE
namespace vtkm {
namespace io {
namespace reader {
class VTKDataSetReader : public VTKDataSetReaderBase
{
public:
VTKDataSetReader(const char *fileName)
: VTKDataSetReaderBase(fileName)
{ }
virtual void PrintSummary(std::ostream &out) const
{
if (this->Reader)
{
this->Reader->PrintSummary(out);
}
else
{
VTKDataSetReaderBase::PrintSummary(out);
}
}
private:
virtual void CloseFile()
{
if (this->Reader)
{
this->Reader->CloseFile();
}
else
{
VTKDataSetReaderBase::CloseFile();
}
}
virtual void Read()
{
switch (this->DataFile->Structure)
{
case internal::DATASET_STRUCTURED_POINTS:
this->Reader.reset(new VTKStructuredPointsReader(""));
break;
case internal::DATASET_STRUCTURED_GRID:
this->Reader.reset(new VTKStructuredGridReader(""));
break;
case internal::DATASET_POLYDATA:
this->Reader.reset(new VTKPolyDataReader(""));
break;
case internal::DATASET_UNSTRUCTURED_GRID:
this->Reader.reset(new VTKUnstructuredGridReader(""));
break;
default:
throw vtkm::io::ErrorIO("Unsupported DataSet type.");
}
this->TransferDataFile(*this->Reader.get());
this->Reader->Read();
this->DataSet = this->Reader->GetDataSet();
}
boost::scoped_ptr<VTKDataSetReaderBase> Reader;
};
}
}
} // vtkm::io::reader
#endif // vtk_m_io_reader_VTKReader_h

@ -0,0 +1,700 @@
//============================================================================
// Copyright (c) Kitware, Inc.
// All rights reserved.
// See LICENSE.txt for details.
// This software is distributed WITHOUT ANY WARRANTY; without even
// the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
// PURPOSE. See the above copyright notice for more information.
//
// Copyright 2015 Sandia Corporation.
// Copyright 2015 UT-Battelle, LLC.
// Copyright 2015 Los Alamos National Security.
//
// Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
// the U.S. Government retains certain rights in this software.
//
// Under the terms of Contract DE-AC52-06NA25396 with Los Alamos National
// Laboratory (LANL), the U.S. Government retains certain rights in
// this software.
//============================================================================
#ifndef vtk_m_io_reader_VTKDataSetReaderBase_h
#define vtk_m_io_reader_VTKDataSetReaderBase_h
#include <vtkm/io/reader/internal/TypeInfo.h>
#include <vtkm/Types.h>
#include <vtkm/VecTraits.h>
#include <vtkm/cont/ArrayHandle.h>
#include <vtkm/cont/DynamicArrayHandle.h>
#include <vtkm/cont/ArrayPortalToIterators.h>
#include <vtkm/cont/DataSet.h>
#include <vtkm/io/ErrorIO.h>
VTKM_THIRDPARTY_PRE_INCLUDE
#include <boost/smart_ptr/scoped_ptr.hpp>
#include <boost/lexical_cast.hpp>
VTKM_THIRDPARTY_POST_INCLUDE
#include <algorithm>
#include <fstream>
#include <string>
#include <vector>
namespace vtkm {
namespace io {
namespace reader {
namespace internal {
struct VTKDataSetFile
{
std::string FileName;
vtkm::Id2 Version;
std::string Title;
bool IsBinary;
DataSetType Structure;
std::ifstream Stream;
};
inline void PrintVTKDataFileSummary(const VTKDataSetFile &df, std::ostream &out)
{
out << "\tFile: " << df.FileName << std::endl;
out << "\tVersion: " << df.Version[0] << "." << df.Version[0] << std::endl;
out << "\tTitle: " << df.Title << std::endl;
out << "\tFormat: " << (df.IsBinary ? "BINARY" : "ASCII") << std::endl;
out << "\tDataSet type: " << internal::DataSetTypeString(df.Structure) << std::endl;
}
inline bool isLittleEndian()
{
static const vtkm::Int16 i16 = 0x1;
const vtkm::Int8 *i8p = reinterpret_cast<const vtkm::Int8*>(&i16);
return (*i8p == 1);
}
template<typename T>
inline void flipEndianness(std::vector<T> &buffer)
{
for (std::size_t i = 0; i < buffer.size(); ++i)
{
vtkm::UInt8 *bytes = reinterpret_cast<vtkm::UInt8*>(&buffer[i]);
std::reverse(bytes, bytes + sizeof(T));
}
}
inline void parseAssert(bool condition)
{
if (!condition)
{
throw vtkm::io::ErrorIO("Parse Error");
}
}
struct DummyFixed8Type
{
vtkm::UInt8 data;
};
template <typename T>
struct TypeTraits
{
typedef T AsciiReadType;
};
template <>
struct TypeTraits<vtkm::Int8>
{
typedef vtkm::Int16 AsciiReadType;
};
template <>
struct TypeTraits<vtkm::UInt8>
{
typedef vtkm::UInt16 AsciiReadType;
};
template <>
struct TypeTraits<DummyFixed8Type>
{
typedef vtkm::Float32 AsciiReadType;
};
} // namespace internal
class VTKDataSetReaderBase
{
public:
explicit VTKDataSetReaderBase(const char *fileName)
: DataFile(new internal::VTKDataSetFile), DataSet(), Loaded(false)
{
this->DataFile->FileName = fileName;
}
virtual ~VTKDataSetReaderBase()
{ }
const vtkm::cont::DataSet& ReadDataSet()
{
if (!this->Loaded)
{
try
{
this->OpenFile();
this->ReadHeader();
this->Read();
this->CloseFile();
this->Loaded = true;
}
catch (std::ifstream::failure e)
{
std::string message("IO Error: ");
throw vtkm::io::ErrorIO(message + e.what());
}
}
return this->DataSet;
}
const vtkm::cont::DataSet& GetDataSet() const
{
return this->DataSet;
}
virtual void PrintSummary(std::ostream &out) const
{
out << "VTKDataSetReader" << std::endl;
PrintVTKDataFileSummary(*this->DataFile.get(), out);
this->DataSet.PrintSummary(out);
}
protected:
void ReadPoints()
{
std::string tag, dataType;
std::size_t numPoints;
this->DataFile->Stream >> tag >> numPoints >> dataType >> std::ws;
internal::parseAssert(tag == "POINTS");
vtkm::cont::DynamicArrayHandle points;
this->DoReadDynamicArray(dataType, numPoints, 3, points);
this->DataSet.AddCoordinateSystem(
vtkm::cont::CoordinateSystem("coordinates", 1, points));
}
void ReadCells(std::vector<vtkm::Id> &connectivity,
std::vector<vtkm::IdComponent> &numIndices)
{
std::size_t numCells, numInts;
this->DataFile->Stream >> numCells >> numInts >> std::ws;
connectivity.resize(numInts - numCells);
numIndices.resize(numCells);
if (this->DataFile->IsBinary)
{
std::vector<vtkm::Int32> buffer(numInts);
this->ReadArray(buffer);
vtkm::Int32 *buffp = &buffer[0];
vtkm::Id *connp = &connectivity[0];
vtkm::IdComponent *indsp = &numIndices[0];
for (std::size_t i = 0; i < numCells; ++i)
{
*indsp = *buffp++;
for (vtkm::IdComponent j = 0; j < *indsp; ++j)
{
*connp++ = *buffp++;
}
++indsp;
}
}
else
{
vtkm::Id *connp = &connectivity[0];
vtkm::IdComponent *indsp = &numIndices[0];
for (std::size_t i = 0; i < numCells; ++i)
{
this->DataFile->Stream >> *indsp;
for (vtkm::IdComponent j = 0; j < *indsp; ++j)
{
this->DataFile->Stream >> *connp++;
}
++indsp;
}
this->DataFile->Stream >> std::ws;
}
}
void ReadShapes(std::vector<vtkm::UInt8> &shapes)
{
std::string tag;
std::size_t numCells;
this->DataFile->Stream >> tag >> numCells >> std::ws;
internal::parseAssert(tag == "CELL_TYPES");
shapes.resize(numCells);
if (this->DataFile->IsBinary)
{
std::vector<vtkm::Int32> buffer(numCells);
this->ReadArray(buffer);
for (std::size_t i = 0; i < numCells; ++i)
{
shapes[i] = static_cast<vtkm::UInt8>(buffer[i]);
}
}
else
{
this->ReadArray(shapes);
}
}
void ReadAttributes()
{
if (this->DataFile->Stream.eof())
{
return;
}
vtkm::cont::Field::AssociationEnum association = vtkm::cont::Field::ASSOC_ANY;
std::size_t size;
std::string tag;
this->DataFile->Stream >> tag;
while (!this->DataFile->Stream.eof())
{
if (tag == "POINT_DATA")
{
association = vtkm::cont::Field::ASSOC_POINTS;
}
else if (tag == "CELL_DATA")
{
association = vtkm::cont::Field::ASSOC_CELL_SET;
}
else
{
internal::parseAssert(false);
}
this->DataFile->Stream >> size;
while (!this->DataFile->Stream.eof())
{
std::string name;
vtkm::cont::ArrayHandle<vtkm::Float32> empty;
vtkm::cont::DynamicArrayHandle data(empty);
this->DataFile->Stream >> tag;
if (tag == "SCALARS")
{
this->ReadScalars(size, name, data);
}
else if (tag == "COLOR_SCALARS")
{
this->ReadColorScalars(size, name);
}
else if (tag == "LOOKUP_TABLE")
{
this->ReadLookupTable(name);
}
else if (tag == "VECTORS" || tag == "NORMALS")
{
this->ReadVectors(size, name, data);
}
else if (tag == "TEXTURE_COORDINATES")
{
this->ReadTextureCoordinates(size, name, data);
}
else if (tag == "TENSORS")
{
this->ReadTensors(size, name, data);
}
else if (tag == "FIELD")
{
this->ReadFields(name);
}
else
{
break;
}
if (data.GetNumberOfValues() > 0)
{
name = tag + ":" + name;
switch (association)
{
case vtkm::cont::Field::ASSOC_POINTS:
this->DataSet.AddField(vtkm::cont::Field(name, 0, association, data));
break;
case vtkm::cont::Field::ASSOC_CELL_SET:
this->DataSet.AddField(
vtkm::cont::Field(name, 0, association, "cells", data));
break;
default:
break;
}
}
}
}
}
void TransferDataFile(VTKDataSetReaderBase &reader)
{
reader.DataFile.swap(this->DataFile);
this->DataFile.reset(NULL);
}
virtual void CloseFile()
{
this->DataFile->Stream.close();
}
private:
void OpenFile()
{
this->DataFile->Stream.exceptions(std::ifstream::failbit | std::ifstream::badbit);
this->DataFile->Stream.open(this->DataFile->FileName.c_str(),
std::ios_base::in | std::ios_base::binary);
}
void ReadHeader()
{
char vstring[] = "# vtk DataFile Version";
const int vlen = sizeof(vstring);
// Read version line
char vbuf[vlen];
this->DataFile->Stream.read(vbuf, vlen - 1);
vbuf[vlen - 1] = '\0';
if (std::string(vbuf) != "# vtk DataFile Version")
{
throw vtkm::io::ErrorIO("Incorrect file format.");
}
char dot;
this->DataFile->Stream >> this->DataFile->Version[0] >> dot
>> this->DataFile->Version[1] >> std::ws;
// Read title line
std::getline(this->DataFile->Stream, this->DataFile->Title);
// Read format line
this->DataFile->IsBinary = false;
std::string format;
this->DataFile->Stream >> format >> std::ws;
if (format == "BINARY")
{
this->DataFile->IsBinary = true;
}
else if (format != "ASCII")
{
throw vtkm::io::ErrorIO("Unsupported Format.");
}
// Read structure line
std::string tag, structStr;
this->DataFile->Stream >> tag >> structStr >> std::ws;
internal::parseAssert(tag == "DATASET");
this->DataFile->Structure = internal::DataSetTypeId(structStr);
if (this->DataFile->Structure == internal::DATASET_UNKNOWN)
{
throw vtkm::io::ErrorIO("Unsupported DataSet type.");
}
}
virtual void Read() = 0;
void ReadScalars(std::size_t numElements, std::string &dataName,
vtkm::cont::DynamicArrayHandle &data)
{
std::string dataType, lookupTableName;
vtkm::IdComponent numComponents = 1;
this->DataFile->Stream >> dataName >> dataType;
std::string tag;
this->DataFile->Stream >> tag;
if (tag != "LOOKUP_TABLE")
{
try
{
numComponents = boost::lexical_cast<vtkm::IdComponent>(tag);
}
catch (boost::bad_lexical_cast &)
{
internal::parseAssert(false);
}
this->DataFile->Stream >> tag;
}
internal::parseAssert(tag == "LOOKUP_TABLE");
this->DataFile->Stream >> lookupTableName >> std::ws;
this->DoReadDynamicArray(dataType, numElements, numComponents, data);
}
void ReadColorScalars(std::size_t numElements, std::string &dataName)
{
std::cerr << "Support for COLOR_SCALARS is not implemented. Skipping."
<< std::endl;
std::size_t numValues;
this->DataFile->Stream >> dataName >> numValues >> std::ws;
this->SkipArray(numElements * numValues, internal::DummyFixed8Type());
}
void ReadLookupTable(std::string &dataName)
{
std::cerr << "Support for LOOKUP_TABLE is not implemented. Skipping."
<< std::endl;
std::size_t numEntries;
this->DataFile->Stream >> dataName >> numEntries >> std::ws;
this->SkipArray(numEntries, vtkm::Vec<internal::DummyFixed8Type, 4>());
}
void ReadTextureCoordinates(std::size_t numElements, std::string &dataName,
vtkm::cont::DynamicArrayHandle &data)
{
vtkm::IdComponent numComponents;
std::string dataType;
this->DataFile->Stream >> dataName >> numComponents >> dataType >> std::ws;
this->DoReadDynamicArray(dataType, numElements, numComponents, data);
}
void ReadVectors(std::size_t numElements, std::string &dataName,
vtkm::cont::DynamicArrayHandle &data)
{
std::string dataType;
this->DataFile->Stream >> dataName >> dataType >> std::ws;
this->DoReadDynamicArray(dataType, numElements, 3, data);
}
void ReadTensors(std::size_t numElements, std::string &dataName,
vtkm::cont::DynamicArrayHandle &data)
{
std::string dataType;
this->DataFile->Stream >> dataName >> dataType >> std::ws;
this->DoReadDynamicArray(dataType, numElements, 9, data);
}
void ReadFields(std::string &dataName)
{
std::cerr << "Support for FIELD is not implemented. Skipping."
<< std::endl;
vtkm::Id numArrays;
this->DataFile->Stream >> dataName >> numArrays >> std::ws;
for (vtkm::Id i = 0; i < numArrays; ++i)
{
std::size_t numTuples;
vtkm::IdComponent numComponents;
std::string arrayName, dataType;
this->DataFile->Stream >> arrayName >> numComponents >> numTuples
>> dataType >> std::ws;
this->DoSkipDynamicArray(dataType, numTuples, numComponents);
}
}
class SkipDynamicArray
{
public:
SkipDynamicArray(VTKDataSetReaderBase *reader,
std::size_t numElements)
: Reader(reader), NumElements(numElements)
{ }
template <typename T>
void operator()(T) const
{
this->Reader->SkipArray(this->NumElements, T());
}
template <typename T>
void operator()(vtkm::IdComponent numComponents, T) const
{
this->Reader->SkipArray(
this->NumElements * static_cast<std::size_t>(numComponents), T());
}
protected:
VTKDataSetReaderBase *Reader;
std::size_t NumElements;
};
class ReadDynamicArray : public SkipDynamicArray
{
public:
ReadDynamicArray(VTKDataSetReaderBase *reader,
std::size_t numElements,
vtkm::cont::DynamicArrayHandle &data)
: SkipDynamicArray(reader, numElements), Data(&data)
{ }
template <typename T>
void operator()(T) const
{
std::vector<T> buffer(this->NumElements);
this->Reader->ReadArray(buffer);
vtkm::cont::ArrayHandle<T> data;
data.Allocate(static_cast<vtkm::Id>(buffer.size()));
std::copy(buffer.begin(), buffer.end(),
vtkm::cont::ArrayPortalToIteratorBegin(data.GetPortalControl()));
*this->Data = vtkm::cont::DynamicArrayHandle(data);
}
template <typename T>
void operator()(vtkm::IdComponent numComponents, T) const
{
std::cerr << "Support for " << numComponents
<< " components not implemented. Skipping." << std::endl;
SkipDynamicArray::operator()(numComponents, T());
}
private:
vtkm::cont::DynamicArrayHandle *Data;
};
void DoSkipDynamicArray(std::string dataType, std::size_t numElements,
vtkm::IdComponent numComponents)
{
internal::DataType typeId = internal::DataTypeId(dataType);
internal::SelectTypeAndCall(typeId, numComponents,
SkipDynamicArray(this, numElements));
}
void DoReadDynamicArray(std::string dataType, std::size_t numElements,
vtkm::IdComponent numComponents,
vtkm::cont::DynamicArrayHandle &data)
{
internal::DataType typeId = internal::DataTypeId(dataType);
internal::SelectTypeAndCall(typeId, numComponents,
ReadDynamicArray(this, numElements, data));
}
template <typename T>
void ReadArray(std::vector<T> &buffer)
{
std::size_t numElements = buffer.size();
if (this->DataFile->IsBinary)
{
this->DataFile->Stream.read(reinterpret_cast<char*>(&buffer[0]),
static_cast<std::streamsize>(numElements * sizeof(T)));
if(internal::isLittleEndian())
{
internal::flipEndianness(buffer);
}
}
else
{
typedef typename vtkm::VecTraits<T>::ComponentType ComponentType;
const vtkm::IdComponent numComponents = vtkm::VecTraits<T>::NUM_COMPONENTS;
for (std::size_t i = 0; i < numElements; ++i)
{
for (vtkm::IdComponent j = 0; j < numComponents; ++j)
{
typename internal::TypeTraits<ComponentType>::AsciiReadType val;
this->DataFile->Stream >> val;
vtkm::VecTraits<T>::SetComponent(buffer[i], j,
static_cast<ComponentType>(val));
}
}
}
this->DataFile->Stream >> std::ws;
}
template <vtkm::IdComponent NumComponents>
void ReadArray(std::vector<vtkm::Vec<internal::DummyBitType, NumComponents> > &buffer)
{
std::cerr << "Support for data type 'bit' is not implemented. Skipping."
<< std::endl;
this->SkipArray(buffer.size(), vtkm::Vec<internal::DummyBitType, NumComponents>());
buffer.clear();
}
void ReadArray(std::vector<internal::DummyBitType> &buffer)
{
std::cerr << "Support for data type 'bit' is not implemented. Skipping."
<< std::endl;
this->SkipArray(buffer.size(), internal::DummyBitType());
buffer.clear();
}
template <typename T>
void SkipArray(std::size_t numElements, T)
{
if (this->DataFile->IsBinary)
{
this->DataFile->Stream.seekg(
static_cast<std::streamoff>(numElements * sizeof(T)),
std::ios_base::cur);
}
else
{
typedef typename vtkm::VecTraits<T>::ComponentType ComponentType;
const vtkm::IdComponent numComponents = vtkm::VecTraits<T>::NUM_COMPONENTS;
for (std::size_t i = 0; i < numElements; ++i)
{
for (vtkm::IdComponent j = 0; j < numComponents; ++j)
{
typename internal::TypeTraits<ComponentType>::AsciiReadType val;
this->DataFile->Stream >> val;
}
}
}
this->DataFile->Stream >> std::ws;
}
template <vtkm::IdComponent NumComponents>
void SkipArray(std::size_t numElements, vtkm::Vec<internal::DummyBitType, NumComponents>)
{
this->SkipArray(numElements * static_cast<std::size_t>(NumComponents),
internal::DummyBitType());
}
void SkipArray(std::size_t numElements, internal::DummyBitType)
{
if (this->DataFile->IsBinary)
{
numElements = (numElements + 7) / 8;
this->DataFile->Stream.seekg(static_cast<std::streamoff>(numElements), std::ios_base::cur);
}
else
{
for (std::size_t i = 0; i < numElements; ++i)
{
vtkm::UInt16 val;
this->DataFile->Stream >> val;
}
}
this->DataFile->Stream >> std::ws;
}
protected:
boost::scoped_ptr<internal::VTKDataSetFile> DataFile;
vtkm::cont::DataSet DataSet;
private:
bool Loaded;
friend class VTKDataSetReader;
};
}
}
} // vtkm::io::reader
VTKM_BASIC_TYPE_VECTOR(io::reader::internal::DummyFixed8Type)
VTKM_BASIC_TYPE_VECTOR(io::reader::internal::DummyBitType)
#endif // vtk_m_io_reader_VTKDataSetReaderBase_h

@ -0,0 +1,177 @@
//============================================================================
// Copyright (c) Kitware, Inc.
// All rights reserved.
// See LICENSE.txt for details.
// This software is distributed WITHOUT ANY WARRANTY; without even
// the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
// PURPOSE. See the above copyright notice for more information.
//
// Copyright 2015 Sandia Corporation.
// Copyright 2015 UT-Battelle, LLC.
// Copyright 2015 Los Alamos National Security.
//
// Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
// the U.S. Government retains certain rights in this software.
//
// Under the terms of Contract DE-AC52-06NA25396 with Los Alamos National
// Laboratory (LANL), the U.S. Government retains certain rights in
// this software.
//============================================================================
#ifndef vtk_m_io_reader_VTKPolyDataReader_h
#define vtk_m_io_reader_VTKPolyDataReader_h
#include <vtkm/io/reader/VTKDataSetReaderBase.h>
namespace vtkm {
namespace io {
namespace reader {
class VTKPolyDataReader : public VTKDataSetReaderBase
{
public:
explicit VTKPolyDataReader(const char *fileName)
: VTKDataSetReaderBase(fileName)
{ }
private:
virtual void Read()
{
if (this->DataFile->Structure != internal::DATASET_POLYDATA)
{
throw vtkm::io::ErrorIO("Incorrect DataSet type");
}
std::string tag;
// Read the points
this->ReadPoints();
// Read the cellset
std::vector<vtkm::Id> connectivity;
std::vector<vtkm::IdComponent> numIndices;
std::vector<vtkm::UInt8> shapes;
bool sameShape = true;
while (!this->DataFile->Stream.eof())
{
vtkm::CellShapeIdEnum shape = vtkm::CELL_SHAPE_EMPTY;
this->DataFile->Stream >> tag;
if (tag == "VERTICES")
{
shape = vtkm::CELL_SHAPE_VERTEX;
}
else if (tag == "LINES")
{
shape = vtkm::CELL_SHAPE_LINE;
}
else if (tag == "POLYGONS")
{
shape = vtkm::CELL_SHAPE_POLYGON;
}
else if (tag == "TRIANGLE_STRIPS")
{
std::cerr << "Triangle strips are not supported. Skipping.";
}
else
{
this->DataFile->Stream.seekg(-static_cast<std::streamoff>(tag.length()),
std::ios_base::cur);
break;
}
std::size_t prevConnLength = connectivity.size();
std::size_t prevNumIndicesLength = numIndices.size();
sameShape = (prevNumIndicesLength == 0);
this->ReadCells(connectivity, numIndices);
std::size_t numNewCells = prevNumIndicesLength - numIndices.size();
std::size_t newConnSize = prevConnLength - connectivity.size();
switch (shape)
{
case vtkm::CELL_SHAPE_VERTEX:
if (newConnSize != numNewCells)
{
throw vtkm::io::ErrorIO("POLY_VERTEX not supported");
}
shapes.insert(shapes.end(), numNewCells, vtkm::CELL_SHAPE_VERTEX);
break;
case vtkm::CELL_SHAPE_LINE:
if (newConnSize != (numNewCells * 2))
{
throw vtkm::io::ErrorIO("POLY_LINE not supported");
}
shapes.insert(shapes.end(), numNewCells, vtkm::CELL_SHAPE_LINE);
break;
case vtkm::CELL_SHAPE_POLYGON:
for (std::size_t i = prevNumIndicesLength; i < numIndices.size() - 1; ++i)
{
switch (numIndices[i])
{
case 3:
shapes.push_back(vtkm::CELL_SHAPE_TRIANGLE);
break;
case 4:
shapes.push_back(vtkm::CELL_SHAPE_QUAD);
break;
default:
sameShape = false;
shapes.push_back(vtkm::CELL_SHAPE_POLYGON);
break;
}
if (i > prevNumIndicesLength && sameShape && shapes[i - 1] != shapes[i])
{
sameShape = false;
}
}
break;
case vtkm::CELL_SHAPE_EMPTY: // TRIANGLE_STRIPS
continue;
default:
assert(false);
}
}
if (sameShape)
{
vtkm::cont::CellSetSingleType<> cs;
switch(shapes[0])
{
case vtkm::CELL_SHAPE_VERTEX:
cs = vtkm::cont::CellSetSingleType<>(vtkm::CellShapeTagVertex(),
"cells");
break;
case vtkm::CELL_SHAPE_LINE:
cs = vtkm::cont::CellSetSingleType<>(vtkm::CellShapeTagLine(),
"cells");
break;
case vtkm::CELL_SHAPE_TRIANGLE:
cs = vtkm::cont::CellSetSingleType<>(vtkm::CellShapeTagTriangle(),
"cells");
break;
case vtkm::CELL_SHAPE_QUAD:
cs = vtkm::cont::CellSetSingleType<>(vtkm::CellShapeTagQuad(),
"cells");
break;
default:
assert(false);
}
cs.FillViaCopy(connectivity);
this->DataSet.AddCellSet(cs);
}
else
{
vtkm::cont::CellSetExplicit<> cs(0, "cells");
cs.FillViaCopy(shapes, numIndices, connectivity);
this->DataSet.AddCellSet(cs);
}
// Read points and cell attributes
this->ReadAttributes();
}
};
}
}
} // namespace vtkm::io:reader
#endif // vtk_m_io_reader_VTKPolyDataReader_h

@ -0,0 +1,68 @@
//============================================================================
// Copyright (c) Kitware, Inc.
// All rights reserved.
// See LICENSE.txt for details.
// This software is distributed WITHOUT ANY WARRANTY; without even
// the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
// PURPOSE. See the above copyright notice for more information.
//
// Copyright 2015 Sandia Corporation.
// Copyright 2015 UT-Battelle, LLC.
// Copyright 2015 Los Alamos National Security.
//
// Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
// the U.S. Government retains certain rights in this software.
//
// Under the terms of Contract DE-AC52-06NA25396 with Los Alamos National
// Laboratory (LANL), the U.S. Government retains certain rights in
// this software.
//============================================================================
#ifndef vtk_m_io_reader_VTKStructuredGridReader_h
#define vtk_m_io_reader_VTKStructuredGridReader_h
#include <vtkm/io/reader/VTKDataSetReaderBase.h>
namespace vtkm {
namespace io {
namespace reader {
class VTKStructuredGridReader : public VTKDataSetReaderBase
{
public:
explicit VTKStructuredGridReader(const char *fileName)
: VTKDataSetReaderBase(fileName)
{ }
private:
virtual void Read()
{
if (this->DataFile->Structure != internal::DATASET_STRUCTURED_GRID)
{
throw vtkm::io::ErrorIO("Incorrect DataSet type");
}
std::string tag;
// Read structured grid specific meta-data
vtkm::Id3 dim;
vtkm::Vec<vtkm::Float32, 3> origin, spacing;
this->DataFile->Stream >> tag >> dim[0] >> dim[1] >> dim[2] >> std::ws;
internal::parseAssert(tag == "DIMENSIONS");
vtkm::cont::CellSetStructured<3> cs("cells");
cs.SetPointDimensions(vtkm::make_Vec(dim[0], dim[1], dim[2]));
this->DataSet.AddCellSet(cs);
// Read the points
this->ReadPoints();
// Read points and cell attributes
this->ReadAttributes();
}
};
}
}
} // namespace vtkm::io:reader
#endif // vtk_m_io_reader_VTKStructuredGridReader_h

@ -0,0 +1,72 @@
//============================================================================
// Copyright (c) Kitware, Inc.
// All rights reserved.
// See LICENSE.txt for details.
// This software is distributed WITHOUT ANY WARRANTY; without even
// the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
// PURPOSE. See the above copyright notice for more information.
//
// Copyright 2015 Sandia Corporation.
// Copyright 2015 UT-Battelle, LLC.
// Copyright 2015 Los Alamos National Security.
//
// Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
// the U.S. Government retains certain rights in this software.
//
// Under the terms of Contract DE-AC52-06NA25396 with Los Alamos National
// Laboratory (LANL), the U.S. Government retains certain rights in
// this software.
//============================================================================
#ifndef vtk_m_io_reader_VTKStructuredPointsReader_h
#define vtk_m_io_reader_VTKStructuredPointsReader_h
#include <vtkm/io/reader/VTKDataSetReaderBase.h>
namespace vtkm {
namespace io {
namespace reader {
class VTKStructuredPointsReader : public VTKDataSetReaderBase
{
public:
explicit VTKStructuredPointsReader(const char *fileName)
: VTKDataSetReaderBase(fileName)
{ }
private:
virtual void Read()
{
if (this->DataFile->Structure != internal::DATASET_STRUCTURED_POINTS)
{
throw vtkm::io::ErrorIO("Incorrect DataSet type");
}
std::string tag;
// Read structured points specific meta-data
vtkm::Id3 dim;
vtkm::Vec<vtkm::Float32, 3> origin, spacing;
this->DataFile->Stream >> tag >> dim[0] >> dim[1] >> dim[2] >> std::ws;
internal::parseAssert(this->DataFile->Stream.good() && tag == "DIMENSIONS");
this->DataFile->Stream >> tag >> spacing[0] >> spacing[1] >> spacing[2] >> std::ws;
internal::parseAssert(this->DataFile->Stream.good() && tag == "SPACING");
this->DataFile->Stream >> tag >> origin[0] >> origin[1] >> origin[2] >> std::ws;
internal::parseAssert(this->DataFile->Stream.good() && tag == "ORIGIN");
vtkm::cont::CellSetStructured<3> cs("cells");
cs.SetPointDimensions(vtkm::make_Vec(dim[0], dim[1], dim[2]));
this->DataSet.AddCellSet(cs);
this->DataSet.AddCoordinateSystem(vtkm::cont::CoordinateSystem("coordinates",
1, dim, origin, spacing));
// Read points and cell attributes
this->ReadAttributes();
}
};
}
}
} // namespace vtkm::io:reader
#endif // vtk_m_io_reader_VTKStructuredPointsReader_h

@ -0,0 +1,121 @@
//============================================================================
// Copyright (c) Kitware, Inc.
// All rights reserved.
// See LICENSE.txt for details.
// This software is distributed WITHOUT ANY WARRANTY; without even
// the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
// PURPOSE. See the above copyright notice for more information.
//
// Copyright 2015 Sandia Corporation.
// Copyright 2015 UT-Battelle, LLC.
// Copyright 2015 Los Alamos National Security.
//
// Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
// the U.S. Government retains certain rights in this software.
//
// Under the terms of Contract DE-AC52-06NA25396 with Los Alamos National
// Laboratory (LANL), the U.S. Government retains certain rights in
// this software.
//============================================================================
#ifndef vtk_m_io_reader_VTKUnstructuredGridReader_h
#define vtk_m_io_reader_VTKUnstructuredGridReader_h
#include <vtkm/io/reader/VTKDataSetReaderBase.h>
namespace vtkm {
namespace io {
namespace reader {
class VTKUnstructuredGridReader : public VTKDataSetReaderBase
{
public:
explicit VTKUnstructuredGridReader(const char *fileName)
: VTKDataSetReaderBase(fileName)
{ }
private:
virtual void Read()
{
if (this->DataFile->Structure != internal::DATASET_UNSTRUCTURED_GRID)
{
throw vtkm::io::ErrorIO("Incorrect DataSet type");
}
std::string tag;
// Read the points
this->ReadPoints();
// Read the cellset
std::vector<vtkm::Id> connectivity;
std::vector<vtkm::IdComponent> numIndices;
std::vector<vtkm::UInt8> shapes;
this->DataFile->Stream >> tag;
internal::parseAssert(tag == "CELLS");
this->ReadCells(connectivity, numIndices);
this->ReadShapes(shapes);
bool sameShape = true;
for (std::size_t i = 1; i < shapes.size(); ++i)
{
if (shapes[i] != shapes[i - 1])
{
sameShape = false;
break;
}
}
if (sameShape)
{
vtkm::cont::CellSetSingleType<> cs;
switch(shapes[0])
{
case vtkm::CELL_SHAPE_VERTEX:
cs = vtkm::cont::CellSetSingleType<>(vtkm::CellShapeTagVertex(), "cells");
break;
case vtkm::CELL_SHAPE_LINE:
cs = vtkm::cont::CellSetSingleType<>(vtkm::CellShapeTagLine(), "cells");
break;
case vtkm::CELL_SHAPE_TRIANGLE:
cs = vtkm::cont::CellSetSingleType<>(vtkm::CellShapeTagTriangle(), "cells");
break;
case vtkm::CELL_SHAPE_QUAD:
cs = vtkm::cont::CellSetSingleType<>(vtkm::CellShapeTagQuad(), "cells");
break;
case vtkm::CELL_SHAPE_TETRA:
cs = vtkm::cont::CellSetSingleType<>(vtkm::CellShapeTagTetra(), "cells");
break;
case vtkm::CELL_SHAPE_HEXAHEDRON:
cs = vtkm::cont::CellSetSingleType<>(vtkm::CellShapeTagHexahedron(), "cells");
break;
case vtkm::CELL_SHAPE_WEDGE:
cs = vtkm::cont::CellSetSingleType<>(vtkm::CellShapeTagWedge(), "cells");
break;
case vtkm::CELL_SHAPE_PYRAMID:
cs = vtkm::cont::CellSetSingleType<>(vtkm::CellShapeTagPyramid(), "cells");
break;
default:
assert(false);
}
cs.FillViaCopy(connectivity);
this->DataSet.AddCellSet(cs);
}
else
{
vtkm::cont::CellSetExplicit<> cs(0, "cells");
cs.FillViaCopy(shapes, numIndices, connectivity);
this->DataSet.AddCellSet(cs);
}
// Read points and cell attributes
this->ReadAttributes();
}
};
}
}
} // namespace vtkm::io:reader
#endif // vtk_m_io_reader_VTKUnstructuredGridReader_h

@ -0,0 +1,25 @@
##============================================================================
## Copyright (c) Kitware, Inc.
## All rights reserved.
## See LICENSE.txt for details.
## This software is distributed WITHOUT ANY WARRANTY; without even
## the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
## PURPOSE. See the above copyright notice for more information.
##
## Copyright 2014 Sandia Corporation.
## Copyright 2014 UT-Battelle, LLC.
## Copyright 2014 Los Alamos National Security.
##
## Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
## the U.S. Government retains certain rights in this software.
##
## Under the terms of Contract DE-AC52-06NA25396 with Los Alamos National
## Laboratory (LANL), the U.S. Government retains certain rights in
## this software.
##============================================================================
set(headers
TypeInfo.h
)
vtkm_declare_headers(${headers})

@ -0,0 +1,205 @@
//============================================================================
// Copyright (c) Kitware, Inc.
// All rights reserved.
// See LICENSE.txt for details.
// This software is distributed WITHOUT ANY WARRANTY; without even
// the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
// PURPOSE. See the above copyright notice for more information.
//
// Copyright 2015 Sandia Corporation.
// Copyright 2015 UT-Battelle, LLC.
// Copyright 2015 Los Alamos National Security.
//
// Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
// the U.S. Government retains certain rights in this software.
//
// Under the terms of Contract DE-AC52-06NA25396 with Los Alamos National
// Laboratory (LANL), the U.S. Government retains certain rights in
// this software.
//============================================================================
#ifndef vtk_m_io_readers_internal_TypeInfo_h
#define vtk_m_io_readers_internal_TypeInfo_h
#include <vtkm/Types.h>
#include <cassert>
#include <string>
namespace vtkm {
namespace io {
namespace reader {
namespace internal {
enum DataSetType
{
DATASET_UNKNOWN = 0,
DATASET_STRUCTURED_POINTS,
DATASET_STRUCTURED_GRID,
DATASET_UNSTRUCTURED_GRID,
DATASET_POLYDATA,
DATASET_RECTILINEAR_GRID,
DATASET_FIELD
};
enum DataType
{
DTYPE_UNKNOWN = 0,
DTYPE_BIT,
DTYPE_UNSIGNED_CHAR,
DTYPE_CHAR,
DTYPE_UNSIGNED_SHORT,
DTYPE_SHORT,
DTYPE_UNSIGNED_INT,
DTYPE_INT,
DTYPE_UNSIGNED_LONG,
DTYPE_LONG,
DTYPE_FLOAT,
DTYPE_DOUBLE
};
inline const char* DataSetTypeString(int id)
{
static const char *strings[] = {
"",
"STRUCTURED_POINTS",
"STRUCTURED_GRID",
"UNSTRUCTURED_GRID",
"POLYDATA",
"RECTILINEAR_GRID",
"FIELD"
};
return strings[id];
}
inline DataSetType DataSetTypeId(const std::string &str)
{
DataSetType type = DATASET_UNKNOWN;
for (int id = 1; id < 7; ++id)
{
if (str == DataSetTypeString(id))
{
type = static_cast<DataSetType>(id);
}
}
return type;
}
inline const char* DataTypeString(int id)
{
static const char *strings[] = {
"",
"bit",
"unsigned_char",
"char",
"unsigned_short",
"short",
"unsigned_int",
"int",
"unsigned_long",
"long",
"float",
"double"
};
return strings[id];
}
inline DataType DataTypeId(const std::string &str)
{
DataType type = DTYPE_UNKNOWN;
for (int id = 1; id < 12; ++id)
{
if (str == DataTypeString(id))
{
type = static_cast<DataType>(id);
}
}
return type;
}
struct DummyBitType
{
// Needs to work with streams' << operator
operator bool() const
{
return false;
}
};
template <typename T, typename Functor>
inline void SelectVecTypeAndCall(T, vtkm::IdComponent numComponents, const Functor &functor)
{
switch (numComponents)
{
case 1:
functor(T());
break;
case 2:
functor(vtkm::Vec<T, 2>());
break;
case 3:
functor(vtkm::Vec<T, 3>());
break;
case 4:
functor(vtkm::Vec<T, 4>());
break;
case 9:
functor(vtkm::Vec<T, 9>());
break;
default:
functor(numComponents, T());
break;
}
}
template <typename Functor>
inline void SelectTypeAndCall(DataType dtype, vtkm::IdComponent numComponents,
const Functor &functor)
{
switch (dtype)
{
case DTYPE_BIT:
SelectVecTypeAndCall(DummyBitType(), numComponents, functor);
break;
case DTYPE_UNSIGNED_CHAR:
SelectVecTypeAndCall(vtkm::Int8(), numComponents, functor);
break;
case DTYPE_CHAR:
SelectVecTypeAndCall(vtkm::UInt8(), numComponents, functor);
break;
case DTYPE_UNSIGNED_SHORT:
SelectVecTypeAndCall(vtkm::Int16(), numComponents, functor);
break;
case DTYPE_SHORT:
SelectVecTypeAndCall(vtkm::UInt16(), numComponents, functor);
break;
case DTYPE_UNSIGNED_INT:
SelectVecTypeAndCall(vtkm::Int32(), numComponents, functor);
break;
case DTYPE_INT:
SelectVecTypeAndCall(vtkm::UInt32(), numComponents, functor);
break;
case DTYPE_UNSIGNED_LONG:
SelectVecTypeAndCall(vtkm::Int64(), numComponents, functor);
break;
case DTYPE_LONG:
SelectVecTypeAndCall(vtkm::UInt64(), numComponents, functor);
break;
case DTYPE_FLOAT:
SelectVecTypeAndCall(vtkm::Float32(), numComponents, functor);
break;
case DTYPE_DOUBLE:
SelectVecTypeAndCall(vtkm::Float64(), numComponents, functor);
break;
default:
assert(false);
}
}
}
}
}
} // namespace vtkm::io:reader::internal
#endif // vtk_m_io_readers_internal_TypeInfo_h

@ -0,0 +1,25 @@
##============================================================================
## Copyright (c) Kitware, Inc.
## All rights reserved.
## See LICENSE.txt for details.
## This software is distributed WITHOUT ANY WARRANTY; without even
## the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
## PURPOSE. See the above copyright notice for more information.
##
## Copyright 2014 Sandia Corporation.
## Copyright 2014 UT-Battelle, LLC.
## Copyright 2014 Los Alamos National Security.
##
## Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
## the U.S. Government retains certain rights in this software.
##
## Under the terms of Contract DE-AC52-06NA25396 with Los Alamos National
## Laboratory (LANL), the U.S. Government retains certain rights in
## this software.
##============================================================================
set(unit_tests
UnitTestVTKDataSetReader.cxx
)
vtkm_unit_tests(SOURCES ${unit_tests})

@ -0,0 +1,344 @@
//============================================================================
// Copyright (c) Kitware, Inc.
// All rights reserved.
// See LICENSE.txt for details.
// This software is distributed WITHOUT ANY WARRANTY; without even
// the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
// PURPOSE. See the above copyright notice for more information.
//
// Copyright 2014 Sandia Corporation.
// Copyright 2014 UT-Battelle, LLC.
// Copyright 2014 Los Alamos National Security.
//
// Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
// the U.S. Government retains certain rights in this software.
//
// Under the terms of Contract DE-AC52-06NA25396 with Los Alamos National
// Laboratory (LANL), the U.S. Government retains certain rights in
// this software.
//============================================================================
#include <vtkm/io/reader/VTKDataSetReader.h>
#include <vtkm/cont/testing/Testing.h>
#include <string>
namespace {
const char polydataAscii[] =
"# vtk DataFile Version 3.0\n"
"Cube example\n"
"ASCII\n"
"DATASET POLYDATA\n"
"POINTS 8 float\n"
"0.0 0.0 0.0 1.0 0.0 0.0 1.0 1.0 0.0 0.0 1.0 0.0 0.0 0.0 1.0\n"
"1.0 0.0 1.0 1.0 1.0 1.0 0.0 1.0 1.0\n"
"POLYGONS 6 30\n"
"4 0 1 2 3 4 4 5 6 7 4 0 1 5 4 4 2 3 7 6 4 0 4 7 3 4 1 2 6 5\n"
"CELL_DATA 6\n"
"SCALARS cell_scalars int 1\n"
"LOOKUP_TABLE default\n"
"0 1 2 3 4 5\n"
"NORMALS cell_normals float\n"
"0 0 -1 0 0 1 0 -1 0 0 1 0 -1 0 0 1 0 0\n"
"FIELD FieldData 2\n"
"cellIds 1 6 int\n"
"0 1 2 3 4 5\n"
"faceAttributes 2 6 float\n"
"0.0 1.0 1.0 2.0 2.0 3.0 3.0 4.0 4.0 5.0 5.0 6.0\n"
"POINT_DATA 8\n"
"SCALARS sample_scalars float 1\n"
"LOOKUP_TABLE my_table\n"
"0.0 1.0 2.0 3.0 4.0 5.0 6.0 7.0\n"
"LOOKUP_TABLE my_table 8\n"
"0.0 0.0 0.0 1.0 1.0 0.0 0.0 1.0 0.0 1.0 0.0 1.0 1.0 1.0 0.0 1.0\n"
"0.0 0.0 1.0 1.0 1.0 0.0 1.0 1.0 0.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0\n";
const char polydataBin[] =
"# vtk DataFile Version 4.0\n"
"Cube example\n"
"BINARY\n"
"DATASET POLYDATA\n"
"POINTS 8 float\n"
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x3f\x80\x00\x00\x00\x00\x00\x00"
"\x00\x00\x00\x00\x3f\x80\x00\x00\x3f\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
"\x3f\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x3f\x80\x00\x00"
"\x3f\x80\x00\x00\x00\x00\x00\x00\x3f\x80\x00\x00\x3f\x80\x00\x00\x3f\x80\x00\x00"
"\x3f\x80\x00\x00\x00\x00\x00\x00\x3f\x80\x00\x00\x3f\x80\x00\x00\n"
"POLYGONS 6 30\n"
"\x00\x00\x00\x04\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x02\x00\x00\x00\x03"
"\x00\x00\x00\x04\x00\x00\x00\x04\x00\x00\x00\x05\x00\x00\x00\x06\x00\x00\x00\x07"
"\x00\x00\x00\x04\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x05\x00\x00\x00\x04"
"\x00\x00\x00\x04\x00\x00\x00\x02\x00\x00\x00\x03\x00\x00\x00\x07\x00\x00\x00\x06"
"\x00\x00\x00\x04\x00\x00\x00\x00\x00\x00\x00\x04\x00\x00\x00\x07\x00\x00\x00\x03"
"\x00\x00\x00\x04\x00\x00\x00\x01\x00\x00\x00\x02\x00\x00\x00\x06\x00\x00\x00\x05"
"\n"
"CELL_DATA 6\n"
"SCALARS cell_scalars int\n"
"LOOKUP_TABLE default\n"
"\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x02\x00\x00\x00\x03\x00\x00\x00\x04"
"\x00\x00\x00\x05\n"
"NORMALS cell_normals float\n"
"\x00\x00\x00\x00\x00\x00\x00\x00\xbf\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
"\x3f\x80\x00\x00\x00\x00\x00\x00\xbf\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
"\x3f\x80\x00\x00\x00\x00\x00\x00\xbf\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
"\x3f\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\n"
"FIELD FieldData 2\n"
"cellIds 1 6 int\n"
"\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x02\x00\x00\x00\x03\x00\x00\x00\x04"
"\x00\x00\x00\x05\n"
"faceAttributes 2 6 float\n"
"\x00\x00\x00\x00\x3f\x80\x00\x00\x3f\x80\x00\x00\x40\x00\x00\x00\x40\x00\x00\x00"
"\x40\x40\x00\x00\x40\x40\x00\x00\x40\x80\x00\x00\x40\x80\x00\x00\x40\xa0\x00\x00"
"\x40\xa0\x00\x00\x40\xc0\x00\x00\n"
"POINT_DATA 8\n"
"SCALARS sample_scalars float\n"
"LOOKUP_TABLE lookup_table\n"
"\x00\x00\x00\x00\x3f\x80\x00\x00\x40\x00\x00\x00\x40\x40\x00\x00\x40\x80\x00\x00"
"\x40\xa0\x00\x00\x40\xc0\x00\x00\x40\xe0\x00\x00\n"
"LOOKUP_TABLE lookup_table 8\n"
"\x00\x00\x00\xff\xff\x00\x00\xff\x00\xff\x00\xff\xff\xff\x00\xff\x00\x00\xff\xff"
"\xff\x00\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\n";
const char structuredPointsAscii[] =
"# vtk DataFile Version 3.0\n"
"Volume example\n"
"ASCII\n"
"DATASET STRUCTURED_POINTS\n"
"DIMENSIONS 3 4 6\n"
"SPACING 1 1 1\n"
"ORIGIN 0 0 0\n"
"POINT_DATA 72\n"
"SCALARS volume_scalars char 1\n"
"LOOKUP_TABLE default\n"
" 0 0 0 0 0 0 0 0 0 0 0 0\n"
" 0 5 10 15 20 25 25 20 15 10 5 0\n"
" 0 10 20 30 40 50 50 40 30 20 10 0\n"
" 0 10 20 30 40 50 50 40 30 20 10 0\n"
" 0 5 10 15 20 25 25 20 15 10 5 0\n"
" 0 0 0 0 0 0 0 0 0 0 0 0\n";
const char structuredPointsBin[] =
"# vtk DataFile Version 4.0\n"
"Volume example\n"
"BINARY\n"
"DATASET STRUCTURED_POINTS\n"
"DIMENSIONS 3 4 6\n"
"SPACING 1 1 1\n"
"ORIGIN 0 0 0\n"
"POINT_DATA 72\n"
"SCALARS volume_scalars char\n"
"LOOKUP_TABLE default\n"
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x05\x0a\x0f\x14\x19\x19\x14"
"\x0f\x0a\x05\x00\x00\x0a\x14\x1e\x28\x32\x32\x28\x1e\x14\x0a\x00\x00\x0a\x14\x1e"
"\x28\x32\x32\x28\x1e\x14\x0a\x00\x00\x05\x0a\x0f\x14\x19\x19\x14\x0f\x0a\x05\x00"
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\n";
const char unsturctureGridAscii[] =
"# vtk DataFile Version 3.0\n"
"Unstructured Grid Example\n"
"ASCII\n"
"DATASET UNSTRUCTURED_GRID\n"
"POINTS 27 float\n"
"0 0 0 1 0 0 2 0 0 0 1 0 1 1 0 2 1 0 0 0 1 1 0 1 2 0 1 0 1 1\n"
"1 1 1 2 1 1 0 1 2 1 1 2 2 1 2 0 1 3 1 1 3 2 1 3 0 1 4 1 1 4\n"
"2 1 4 0 1 5 1 1 5 2 1 5 0 1 6 1 1 6 2 1 6\n"
"CELLS 11 60\n"
"8 0 1 4 3 6 7 10 9 8 1 2 5 4 7 8 11 10 4 6 10 9 12 4 5 11 10 14\n"
"6 15 16 17 14 13 12 6 18 15 19 16 20 17 4 22 23 20 19 3 21 22 18\n"
"3 22 19 18 2 26 25 1 24\n"
"CELL_TYPES 11\n"
"12 12 10 10 7 6 9 5 5 3 1\n"
"POINT_DATA 27\n"
"SCALARS scalars float 1\n"
"LOOKUP_TABLE default\n"
"0.0 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0 11.0\n"
"12.0 13.0 14.0 15.0 16.0 17.0 18.0 19.0 20.0 21.0 22.0 23.0\n"
"24.0 25.0 26.0\n"
"VECTORS vectors float\n"
"1 0 0 1 1 0 0 2 0 1 0 0 1 1 0 0 2 0 1 0 0 1 1 0 0 2 0 1 0 0\n"
"1 1 0 0 2 0 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1\n"
"0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1\n";
const char unsturctureGridBin[] =
"# vtk DataFile Version 4.0\n"
"Unstructured Grid Example\n"
"BINARY\n"
"DATASET UNSTRUCTURED_GRID\n"
"POINTS 27 float\n"
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x3f\x80\x00\x00\x00\x00\x00\x00"
"\x00\x00\x00\x00\x40\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
"\x3f\x80\x00\x00\x00\x00\x00\x00\x3f\x80\x00\x00\x3f\x80\x00\x00\x00\x00\x00\x00"
"\x40\x00\x00\x00\x3f\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
"\x3f\x80\x00\x00\x3f\x80\x00\x00\x00\x00\x00\x00\x3f\x80\x00\x00\x40\x00\x00\x00"
"\x00\x00\x00\x00\x3f\x80\x00\x00\x00\x00\x00\x00\x3f\x80\x00\x00\x3f\x80\x00\x00"
"\x3f\x80\x00\x00\x3f\x80\x00\x00\x3f\x80\x00\x00\x40\x00\x00\x00\x3f\x80\x00\x00"
"\x3f\x80\x00\x00\x00\x00\x00\x00\x3f\x80\x00\x00\x40\x00\x00\x00\x3f\x80\x00\x00"
"\x3f\x80\x00\x00\x40\x00\x00\x00\x40\x00\x00\x00\x3f\x80\x00\x00\x40\x00\x00\x00"
"\x00\x00\x00\x00\x3f\x80\x00\x00\x40\x40\x00\x00\x3f\x80\x00\x00\x3f\x80\x00\x00"
"\x40\x40\x00\x00\x40\x00\x00\x00\x3f\x80\x00\x00\x40\x40\x00\x00\x00\x00\x00\x00"
"\x3f\x80\x00\x00\x40\x80\x00\x00\x3f\x80\x00\x00\x3f\x80\x00\x00\x40\x80\x00\x00"
"\x40\x00\x00\x00\x3f\x80\x00\x00\x40\x80\x00\x00\x00\x00\x00\x00\x3f\x80\x00\x00"
"\x40\xa0\x00\x00\x3f\x80\x00\x00\x3f\x80\x00\x00\x40\xa0\x00\x00\x40\x00\x00\x00"
"\x3f\x80\x00\x00\x40\xa0\x00\x00\x00\x00\x00\x00\x3f\x80\x00\x00\x40\xc0\x00\x00"
"\x3f\x80\x00\x00\x3f\x80\x00\x00\x40\xc0\x00\x00\x40\x00\x00\x00\x3f\x80\x00\x00"
"\x40\xc0\x00\x00\n"
"CELLS 11 60\n"
"\x00\x00\x00\x08\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x04\x00\x00\x00\x03"
"\x00\x00\x00\x06\x00\x00\x00\x07\x00\x00\x00\x0a\x00\x00\x00\x09\x00\x00\x00\x08"
"\x00\x00\x00\x01\x00\x00\x00\x02\x00\x00\x00\x05\x00\x00\x00\x04\x00\x00\x00\x07"
"\x00\x00\x00\x08\x00\x00\x00\x0b\x00\x00\x00\x0a\x00\x00\x00\x04\x00\x00\x00\x06"
"\x00\x00\x00\x0a\x00\x00\x00\x09\x00\x00\x00\x0c\x00\x00\x00\x04\x00\x00\x00\x05"
"\x00\x00\x00\x0b\x00\x00\x00\x0a\x00\x00\x00\x0e\x00\x00\x00\x06\x00\x00\x00\x0f"
"\x00\x00\x00\x10\x00\x00\x00\x11\x00\x00\x00\x0e\x00\x00\x00\x0d\x00\x00\x00\x0c"
"\x00\x00\x00\x06\x00\x00\x00\x12\x00\x00\x00\x0f\x00\x00\x00\x13\x00\x00\x00\x10"
"\x00\x00\x00\x14\x00\x00\x00\x11\x00\x00\x00\x04\x00\x00\x00\x16\x00\x00\x00\x17"
"\x00\x00\x00\x14\x00\x00\x00\x13\x00\x00\x00\x03\x00\x00\x00\x15\x00\x00\x00\x16"
"\x00\x00\x00\x12\x00\x00\x00\x03\x00\x00\x00\x16\x00\x00\x00\x13\x00\x00\x00\x12"
"\x00\x00\x00\x02\x00\x00\x00\x1a\x00\x00\x00\x19\x00\x00\x00\x01\x00\x00\x00\x18"
"\n"
"CELL_TYPES 11\n"
"\x00\x00\x00\x0c\x00\x00\x00\x0c\x00\x00\x00\x0a\x00\x00\x00\x0a\x00\x00\x00\x07"
"\x00\x00\x00\x06\x00\x00\x00\x09\x00\x00\x00\x05\x00\x00\x00\x05\x00\x00\x00\x03"
"\x00\x00\x00\x01\n"
"POINT_DATA 27\n"
"SCALARS scalars float\n"
"LOOKUP_TABLE default\n"
"\x00\x00\x00\x00\x3f\x80\x00\x00\x40\x00\x00\x00\x40\x40\x00\x00\x40\x80\x00\x00"
"\x40\xa0\x00\x00\x40\xc0\x00\x00\x40\xe0\x00\x00\x41\x00\x00\x00\x41\x10\x00\x00"
"\x41\x20\x00\x00\x41\x30\x00\x00\x41\x40\x00\x00\x41\x50\x00\x00\x41\x60\x00\x00"
"\x41\x70\x00\x00\x41\x80\x00\x00\x41\x88\x00\x00\x41\x90\x00\x00\x41\x98\x00\x00"
"\x41\xa0\x00\x00\x41\xa8\x00\x00\x41\xb0\x00\x00\x41\xb8\x00\x00\x41\xc0\x00\x00"
"\x41\xc8\x00\x00\x41\xd0\x00\x00\n"
"VECTORS vectors float\n"
"\x3f\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x3f\x80\x00\x00\x3f\x80\x00\x00"
"\x00\x00\x00\x00\x00\x00\x00\x00\x40\x00\x00\x00\x00\x00\x00\x00\x3f\x80\x00\x00"
"\x00\x00\x00\x00\x00\x00\x00\x00\x3f\x80\x00\x00\x3f\x80\x00\x00\x00\x00\x00\x00"
"\x00\x00\x00\x00\x40\x00\x00\x00\x00\x00\x00\x00\x3f\x80\x00\x00\x00\x00\x00\x00"
"\x00\x00\x00\x00\x3f\x80\x00\x00\x3f\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
"\x40\x00\x00\x00\x00\x00\x00\x00\x3f\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
"\x3f\x80\x00\x00\x3f\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x40\x00\x00\x00"
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x3f\x80\x00\x00\x00\x00\x00\x00"
"\x00\x00\x00\x00\x3f\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x3f\x80\x00\x00"
"\x00\x00\x00\x00\x00\x00\x00\x00\x3f\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
"\x3f\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x3f\x80\x00\x00\x00\x00\x00\x00"
"\x00\x00\x00\x00\x3f\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x3f\x80\x00\x00"
"\x00\x00\x00\x00\x00\x00\x00\x00\x3f\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
"\x3f\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x3f\x80\x00\x00\x00\x00\x00\x00"
"\x00\x00\x00\x00\x3f\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x3f\x80\x00\x00"
"\x00\x00\x00\x00\x00\x00\x00\x00\x3f\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
"\x3f\x80\x00\x00\n";
inline void createFile(const char *buffer, std::size_t size, const char *fname)
{
std::ofstream fstr(fname, std::ios_base::out | std::ios_base::binary);
fstr.write(buffer, static_cast<std::streamsize>(size - 1));
fstr.close();
}
inline vtkm::cont::DataSet readVTKDataSet(const char *fname)
{
vtkm::cont::DataSet ds;
vtkm::io::reader::VTKDataSetReader reader(fname);
try
{
ds = reader.ReadDataSet();
}
catch (vtkm::io::ErrorIO e)
{
std::string message("Error reading: ");
message += fname;
message += ", ";
message += e.GetMessage();
VTKM_TEST_FAIL(message.c_str());
}
return ds;
}
const char *testFileName = "vtkm-io-reader-test.vtk";
enum Format
{
FORMAT_ASCII,
FORMAT_BINARY
};
} // anonymous namespace
void TestReadingPolyData(Format format)
{
(format == FORMAT_ASCII) ?
createFile(polydataAscii, sizeof(polydataAscii), testFileName) :
createFile(polydataBin, sizeof(polydataBin), testFileName);
vtkm::cont::DataSet ds = readVTKDataSet(testFileName);
VTKM_TEST_ASSERT(ds.GetNumberOfFields() == 3,
"Incorrect number of fields");
VTKM_TEST_ASSERT(ds.GetCoordinateSystem().GetData().GetNumberOfValues() == 8,
"Incorrect number of points");
VTKM_TEST_ASSERT(ds.GetCellSet().GetCellSet().GetNumberOfCells() == 6,
"Incorrect number of cells");
VTKM_TEST_ASSERT(ds.GetCellSet().IsType(vtkm::cont::CellSetSingleType<>()),
"Incorrect cellset type");
}
void TestReadingStructuredPoints(Format format)
{
(format == FORMAT_ASCII) ?
createFile(structuredPointsAscii, sizeof(structuredPointsAscii), testFileName) :
createFile(structuredPointsBin, sizeof(structuredPointsBin), testFileName);
vtkm::cont::DataSet ds = readVTKDataSet(testFileName);
VTKM_TEST_ASSERT(ds.GetNumberOfFields() == 1,
"Incorrect number of fields");
VTKM_TEST_ASSERT(ds.GetCoordinateSystem().GetData().GetNumberOfValues() == 72,
"Incorrect number of points");
VTKM_TEST_ASSERT(ds.GetCellSet().GetCellSet().GetNumberOfCells() == 30,
"Incorrect number of cells");
VTKM_TEST_ASSERT(ds.GetCellSet().IsType(vtkm::cont::CellSetStructured<3>()),
"Incorrect cellset type");
}
void TestReadingUnstructuredGrid(Format format)
{
(format == FORMAT_ASCII) ?
createFile(unsturctureGridAscii, sizeof(unsturctureGridAscii), testFileName) :
createFile(unsturctureGridBin, sizeof(unsturctureGridBin), testFileName);
vtkm::cont::DataSet ds = readVTKDataSet(testFileName);
VTKM_TEST_ASSERT(ds.GetNumberOfFields() == 2,
"Incorrect number of fields");
VTKM_TEST_ASSERT(ds.GetCoordinateSystem().GetData().GetNumberOfValues() == 27,
"Incorrect number of points");
VTKM_TEST_ASSERT(ds.GetCellSet().GetCellSet().GetNumberOfCells() == 11,
"Incorrect number of cells");
VTKM_TEST_ASSERT(ds.GetCellSet().IsType(vtkm::cont::CellSetExplicit<>()),
"Incorrect cellset type");
}
void TestReadingVTKDataSet()
{
std::cout << "Test reading VTK Polydata file in ASCII" << std::endl;
TestReadingPolyData(FORMAT_ASCII);
std::cout << "Test reading VTK Polydata file in BINARY" << std::endl;
TestReadingPolyData(FORMAT_BINARY);
std::cout << "Test reading VTK StructuredPoints file in ASCII" << std::endl;
TestReadingStructuredPoints(FORMAT_ASCII);
std::cout << "Test reading VTK StructuredPoints file in BINARY" << std::endl;
TestReadingStructuredPoints(FORMAT_BINARY);
std::cout << "Test reading VTK UnstructuredGrid file in ASCII" << std::endl;
TestReadingUnstructuredGrid(FORMAT_ASCII);
std::cout << "Test reading VTK UnstructuredGrid file in BINARY" << std::endl;
TestReadingUnstructuredGrid(FORMAT_BINARY);
}
int UnitTestVTKDataSetReader(int, char *[])
{
return vtkm::cont::testing::Testing::Run(TestReadingVTKDataSet);
}