mirror of
https://gitlab.kitware.com/vtk/vtk-m
synced 2024-09-19 10:35:42 +00:00
Change interface of VTKDataSetWriter
Make it match VTKDataSetReader better. It's also a bit easier to use because you just have to give it a filename rather than open your own file stream.
This commit is contained in:
parent
177b31f330
commit
e863ee991a
@ -62,9 +62,10 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
VTKM_CONT_EXPORT
|
VTKM_CONT_EXPORT
|
||||||
const vtkm::cont::Field &GetField(const std::string &name,
|
vtkm::Id GetFieldIndex(
|
||||||
|
const std::string &name,
|
||||||
vtkm::cont::Field::AssociationEnum assoc = vtkm::cont::Field::ASSOC_ANY)
|
vtkm::cont::Field::AssociationEnum assoc = vtkm::cont::Field::ASSOC_ANY)
|
||||||
const
|
const
|
||||||
{
|
{
|
||||||
for (std::size_t i=0; i < this->Fields.size(); ++i)
|
for (std::size_t i=0; i < this->Fields.size(); ++i)
|
||||||
{
|
{
|
||||||
@ -72,12 +73,20 @@ public:
|
|||||||
assoc == this->Fields[i].GetAssociation()) &&
|
assoc == this->Fields[i].GetAssociation()) &&
|
||||||
this->Fields[i].GetName() == name)
|
this->Fields[i].GetName() == name)
|
||||||
{
|
{
|
||||||
return this->Fields[i];
|
return static_cast<vtkm::Id>(i);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
throw vtkm::cont::ErrorControlBadValue("No field with requested name: "+name);
|
throw vtkm::cont::ErrorControlBadValue("No field with requested name: "+name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
VTKM_CONT_EXPORT
|
||||||
|
const vtkm::cont::Field &GetField(const std::string &name,
|
||||||
|
vtkm::cont::Field::AssociationEnum assoc = vtkm::cont::Field::ASSOC_ANY)
|
||||||
|
const
|
||||||
|
{
|
||||||
|
return this->GetField(this->GetFieldIndex(name, assoc));
|
||||||
|
}
|
||||||
|
|
||||||
VTKM_CONT_EXPORT
|
VTKM_CONT_EXPORT
|
||||||
void AddCoordinateSystem(vtkm::cont::CoordinateSystem cs)
|
void AddCoordinateSystem(vtkm::cont::CoordinateSystem cs)
|
||||||
{
|
{
|
||||||
@ -94,20 +103,26 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
VTKM_CONT_EXPORT
|
VTKM_CONT_EXPORT
|
||||||
const vtkm::cont::CoordinateSystem &
|
vtkm::Id GetCoordinateSystemIndex(const std::string &name) const
|
||||||
GetCoordinateSystem(const std::string &name) const
|
|
||||||
{
|
{
|
||||||
for (std::size_t i=0; i < this->CoordSystems.size(); ++i)
|
for (std::size_t i=0; i < this->CoordSystems.size(); ++i)
|
||||||
{
|
{
|
||||||
if (this->CoordSystems[i].GetName() == name)
|
if (this->CoordSystems[i].GetName() == name)
|
||||||
{
|
{
|
||||||
return this->CoordSystems[i];
|
return static_cast<vtkm::Id>(i);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
throw vtkm::cont::ErrorControlBadValue(
|
throw vtkm::cont::ErrorControlBadValue(
|
||||||
"No coordinate system with requested name");
|
"No coordinate system with requested name");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
VTKM_CONT_EXPORT
|
||||||
|
const vtkm::cont::CoordinateSystem &
|
||||||
|
GetCoordinateSystem(const std::string &name) const
|
||||||
|
{
|
||||||
|
return this->GetCoordinateSystem(this->GetCoordinateSystemIndex(name));
|
||||||
|
}
|
||||||
|
|
||||||
VTKM_CONT_EXPORT
|
VTKM_CONT_EXPORT
|
||||||
void AddCellSet(vtkm::cont::DynamicCellSet cellSet)
|
void AddCellSet(vtkm::cont::DynamicCellSet cellSet)
|
||||||
{
|
{
|
||||||
@ -131,19 +146,24 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
VTKM_CONT_EXPORT
|
VTKM_CONT_EXPORT
|
||||||
vtkm::cont::DynamicCellSet GetCellSet(const std::string &name)
|
vtkm::Id GetCellSetIndex(const std::string &name) const
|
||||||
const
|
|
||||||
{
|
{
|
||||||
for (std::size_t i=0; i < static_cast<size_t>(this->GetNumberOfCellSets()); ++i)
|
for (std::size_t i=0; i < static_cast<size_t>(this->GetNumberOfCellSets()); ++i)
|
||||||
{
|
{
|
||||||
if (this->CellSets[i].GetCellSet().GetName() == name)
|
if (this->CellSets[i].GetCellSet().GetName() == name)
|
||||||
{
|
{
|
||||||
return this->CellSets[i];
|
return static_cast<vtkm::Id>(i);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
throw vtkm::cont::ErrorControlBadValue("No cell set with requested name");
|
throw vtkm::cont::ErrorControlBadValue("No cell set with requested name");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
VTKM_CONT_EXPORT
|
||||||
|
vtkm::cont::DynamicCellSet GetCellSet(const std::string &name) const
|
||||||
|
{
|
||||||
|
return this->GetCellSet(this->GetCellSetIndex(name));
|
||||||
|
}
|
||||||
|
|
||||||
VTKM_CONT_EXPORT
|
VTKM_CONT_EXPORT
|
||||||
vtkm::IdComponent GetNumberOfCellSets() const
|
vtkm::IdComponent GetNumberOfCellSets() const
|
||||||
{
|
{
|
||||||
|
@ -37,7 +37,7 @@ namespace reader {
|
|||||||
class VTKDataSetReader : public VTKDataSetReaderBase
|
class VTKDataSetReader : public VTKDataSetReaderBase
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
VTKDataSetReader(const char *fileName)
|
explicit VTKDataSetReader(const char *fileName)
|
||||||
: VTKDataSetReaderBase(fileName)
|
: VTKDataSetReaderBase(fileName)
|
||||||
{ }
|
{ }
|
||||||
|
|
||||||
|
@ -20,6 +20,19 @@
|
|||||||
#ifndef vtk_m_io_writer_DataSetWriter_h
|
#ifndef vtk_m_io_writer_DataSetWriter_h
|
||||||
#define vtk_m_io_writer_DataSetWriter_h
|
#define vtk_m_io_writer_DataSetWriter_h
|
||||||
|
|
||||||
|
#include <vtkm/CellShape.h>
|
||||||
|
|
||||||
|
#include <vtkm/cont/CellSetExplicit.h>
|
||||||
|
#include <vtkm/cont/CellSetStructured.h>
|
||||||
|
#include <vtkm/cont/CellSetSingleType.h>
|
||||||
|
#include <vtkm/cont/DataSet.h>
|
||||||
|
#include <vtkm/cont/ErrorControlBadValue.h>
|
||||||
|
#include <vtkm/cont/Field.h>
|
||||||
|
|
||||||
|
#include <vtkm/io/ErrorIO.h>
|
||||||
|
|
||||||
|
#include <vtkm/io/internal/VTKDataSetTypes.h>
|
||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
@ -27,52 +40,11 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
#include <vtkm/CellShape.h>
|
namespace vtkm {
|
||||||
#include <vtkm/cont/CellSetExplicit.h>
|
namespace io {
|
||||||
#include <vtkm/cont/CellSetStructured.h>
|
namespace writer {
|
||||||
#include <vtkm/cont/CellSetSingleType.h>
|
|
||||||
#include <vtkm/cont/DataSet.h>
|
|
||||||
#include <vtkm/cont/Field.h>
|
|
||||||
#include <vtkm/io/internal/VTKDataSetTypes.h>
|
|
||||||
|
|
||||||
namespace {
|
namespace detail {
|
||||||
#define VTK_EMPTY_CELL 0
|
|
||||||
#define VTK_VERTEX 1
|
|
||||||
#define VTK_POLY_VERTEX 2
|
|
||||||
#define VTK_LINE 3
|
|
||||||
#define VTK_POLY_LINE 4
|
|
||||||
#define VTK_TRIANGLE 5
|
|
||||||
#define VTK_TRIANGLE_STRIP 6
|
|
||||||
#define VTK_POLYGON 7
|
|
||||||
#define VTK_PIXEL 8
|
|
||||||
#define VTK_QUAD 9
|
|
||||||
#define VTK_TETRA 10
|
|
||||||
#define VTK_VOXEL 11
|
|
||||||
#define VTK_HEXAHEDRON 12
|
|
||||||
#define VTK_WEDGE 13
|
|
||||||
#define VTK_PYRAMID 14
|
|
||||||
#define VTK_PENTAGONAL_PRISM 15
|
|
||||||
#define VTK_HEXAGONAL_PRISM 16
|
|
||||||
|
|
||||||
int CellShapeToVTK(vtkm::Id type)
|
|
||||||
{
|
|
||||||
switch(type)
|
|
||||||
{
|
|
||||||
case vtkm::CELL_SHAPE_VERTEX: return 1;
|
|
||||||
case vtkm::CELL_SHAPE_LINE: return 3;
|
|
||||||
case vtkm::CELL_SHAPE_TRIANGLE: return 5;
|
|
||||||
case vtkm::CELL_SHAPE_QUAD: return 9;
|
|
||||||
//case vtkm::CELL_SHAPE_PIXEL: return 8;
|
|
||||||
case vtkm::CELL_SHAPE_TETRA: return 10;
|
|
||||||
case vtkm::CELL_SHAPE_PYRAMID: return 14;
|
|
||||||
case vtkm::CELL_SHAPE_WEDGE: return 13;
|
|
||||||
case vtkm::CELL_SHAPE_HEXAHEDRON: return 12;
|
|
||||||
//case vtkm::CELL_SHAPE_VOXEL: return 11;
|
|
||||||
case vtkm::CELL_SHAPE_POLYGON: return 7;
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
struct OutputPointsFunctor
|
struct OutputPointsFunctor
|
||||||
{
|
{
|
||||||
@ -175,53 +147,48 @@ private:
|
|||||||
std::string *Name;
|
std::string *Name;
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
} // namespace detail
|
||||||
|
|
||||||
namespace vtkm
|
|
||||||
{
|
|
||||||
namespace io
|
|
||||||
{
|
|
||||||
namespace writer
|
|
||||||
{
|
|
||||||
|
|
||||||
struct VTKDataSetWriter
|
struct VTKDataSetWriter
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
static void WritePoints(std::ostream &out,
|
static void WritePoints(std::ostream &out,
|
||||||
vtkm::cont::DataSet ds)
|
vtkm::cont::DataSet dataSet)
|
||||||
{
|
{
|
||||||
///\todo: support other coordinate systems
|
///\todo: support other coordinate systems
|
||||||
int cindex = 0;
|
int cindex = 0;
|
||||||
|
|
||||||
vtkm::cont::CoordinateSystem coords = ds.GetCoordinateSystem(cindex);
|
vtkm::cont::CoordinateSystem coords = dataSet.GetCoordinateSystem(cindex);
|
||||||
vtkm::cont::DynamicArrayHandleCoordinateSystem cdata = coords.GetData();
|
vtkm::cont::DynamicArrayHandleCoordinateSystem cdata = coords.GetData();
|
||||||
|
|
||||||
vtkm::Id npoints = cdata.GetNumberOfValues();
|
vtkm::Id npoints = cdata.GetNumberOfValues();
|
||||||
|
|
||||||
std::string typeName;
|
std::string typeName;
|
||||||
cdata.CastAndCall(GetDataTypeName(typeName));
|
cdata.CastAndCall(detail::GetDataTypeName(typeName));
|
||||||
|
|
||||||
out << "POINTS " << npoints << " " << typeName << " " << std::endl;
|
out << "POINTS " << npoints << " " << typeName << " " << std::endl;
|
||||||
cdata.CastAndCall(OutputPointsFunctor(out));
|
cdata.CastAndCall(detail::OutputPointsFunctor(out));
|
||||||
}
|
}
|
||||||
|
|
||||||
template <class CellSetType>
|
template <class CellSetType>
|
||||||
static void WriteExplicitCells(std::ostream &out,
|
static void WriteExplicitCells(std::ostream &out,
|
||||||
CellSetType cs)
|
CellSetType cellSet)
|
||||||
{
|
{
|
||||||
vtkm::Id nCells = cs.GetNumberOfCells();
|
vtkm::Id nCells = cellSet.GetNumberOfCells();
|
||||||
|
|
||||||
vtkm::Id conn_length = 0;
|
vtkm::Id conn_length = 0;
|
||||||
for (vtkm::Id i=0; i<nCells; ++i)
|
for (vtkm::Id i=0; i<nCells; ++i)
|
||||||
conn_length += 1 + cs.GetNumberOfPointsInCell(i);
|
{
|
||||||
|
conn_length += 1 + cellSet.GetNumberOfPointsInCell(i);
|
||||||
|
}
|
||||||
|
|
||||||
out << "CELLS " << nCells << " " << conn_length << std::endl;
|
out << "CELLS " << nCells << " " << conn_length << std::endl;
|
||||||
|
|
||||||
vtkm::Vec<vtkm::Id,8> ids;
|
vtkm::Vec<vtkm::Id,8> ids;
|
||||||
for (vtkm::Id i=0; i<nCells; ++i)
|
for (vtkm::Id i=0; i<nCells; ++i)
|
||||||
{
|
{
|
||||||
vtkm::Id nids = cs.GetNumberOfPointsInCell(i);
|
vtkm::Id nids = cellSet.GetNumberOfPointsInCell(i);
|
||||||
cs.GetIndices(i, ids);
|
cellSet.GetIndices(i, ids);
|
||||||
out << nids;
|
out << nids;
|
||||||
for (int j=0; j<nids; ++j)
|
for (int j=0; j<nids; ++j)
|
||||||
out << " " << ids[j];
|
out << " " << ids[j];
|
||||||
@ -231,71 +198,75 @@ private:
|
|||||||
out << "CELL_TYPES " << nCells << std::endl;
|
out << "CELL_TYPES " << nCells << std::endl;
|
||||||
for (vtkm::Id i=0; i<nCells; ++i)
|
for (vtkm::Id i=0; i<nCells; ++i)
|
||||||
{
|
{
|
||||||
vtkm::Id shape = cs.GetCellShape(i);
|
vtkm::Id shape = cellSet.GetCellShape(i);
|
||||||
out << CellShapeToVTK(shape) << std::endl;
|
out << shape << std::endl;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void WriteVertexCells(std::ostream &out,
|
static void WriteVertexCells(std::ostream &out,
|
||||||
vtkm::cont::DataSet ds)
|
vtkm::cont::DataSet dataSet)
|
||||||
{
|
{
|
||||||
vtkm::Id n = ds.GetCoordinateSystem(0).GetData().GetNumberOfValues();
|
vtkm::Id nCells = dataSet.GetCoordinateSystem(0).GetData().GetNumberOfValues();
|
||||||
|
|
||||||
out << "CELLS " << n << " " << n*2 << std::endl;
|
out << "CELLS " << nCells << " " << nCells*2 << std::endl;
|
||||||
for (int i = 0; i < n; i++)
|
for (int i = 0; i < nCells; i++)
|
||||||
{
|
{
|
||||||
out << "1 " << i << std::endl;
|
out << "1 " << i << std::endl;
|
||||||
}
|
}
|
||||||
out << "CELL_TYPES " << n << std::endl;
|
out << "CELL_TYPES " << nCells << std::endl;
|
||||||
for (int i = 0; i < n; i++)
|
for (int i = 0; i < nCells; i++)
|
||||||
{
|
{
|
||||||
out << CellShapeToVTK(CELL_SHAPE_VERTEX) << std::endl;
|
out << vtkm::CELL_SHAPE_VERTEX << std::endl;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void WritePointFields(std::ostream &out,
|
static void WritePointFields(std::ostream &out,
|
||||||
vtkm::cont::DataSet ds)
|
vtkm::cont::DataSet dataSet)
|
||||||
{
|
{
|
||||||
bool wrote_header = false;
|
bool wrote_header = false;
|
||||||
for (vtkm::Id f = 0; f < ds.GetNumberOfFields(); f++)
|
for (vtkm::Id f = 0; f < dataSet.GetNumberOfFields(); f++)
|
||||||
{
|
{
|
||||||
const vtkm::cont::Field field = ds.GetField(f);
|
const vtkm::cont::Field field = dataSet.GetField(f);
|
||||||
|
|
||||||
if (field.GetAssociation() != vtkm::cont::Field::ASSOC_POINTS)
|
if (field.GetAssociation() != vtkm::cont::Field::ASSOC_POINTS) {continue;}
|
||||||
continue;
|
|
||||||
|
|
||||||
vtkm::Id npoints = field.GetData().GetNumberOfValues();
|
vtkm::Id npoints = field.GetData().GetNumberOfValues();
|
||||||
int ncomps = field.GetData().GetNumberOfComponents();
|
int ncomps = field.GetData().GetNumberOfComponents();
|
||||||
if (ncomps > 4)
|
if (ncomps > 4) { continue; }
|
||||||
continue;
|
|
||||||
|
|
||||||
if (!wrote_header)
|
if (!wrote_header)
|
||||||
|
{
|
||||||
out << "POINT_DATA " << npoints << std::endl;
|
out << "POINT_DATA " << npoints << std::endl;
|
||||||
wrote_header = true;
|
wrote_header = true;
|
||||||
|
}
|
||||||
|
|
||||||
std::string typeName;
|
std::string typeName;
|
||||||
field.GetData().CastAndCall(GetDataTypeName(typeName));
|
field.GetData().CastAndCall(detail::GetDataTypeName(typeName));
|
||||||
|
|
||||||
out << "SCALARS " << field.GetName() << " " << typeName << " " << ncomps << std::endl;
|
out << "SCALARS " << field.GetName() << " "
|
||||||
|
<< typeName << " " << ncomps << std::endl;
|
||||||
out << "LOOKUP_TABLE default" << std::endl;
|
out << "LOOKUP_TABLE default" << std::endl;
|
||||||
|
|
||||||
field.GetData().CastAndCall(OutputFieldFunctor(out));
|
field.GetData().CastAndCall(detail::OutputFieldFunctor(out));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void WriteCellFields(std::ostream &out,
|
static void WriteCellFields(std::ostream &out,
|
||||||
vtkm::cont::DataSet ds,
|
vtkm::cont::DataSet dataSet,
|
||||||
vtkm::cont::DynamicCellSet cs)
|
vtkm::cont::DynamicCellSet cellSet)
|
||||||
{
|
{
|
||||||
bool wrote_header = false;
|
bool wrote_header = false;
|
||||||
for (vtkm::Id f = 0; f < ds.GetNumberOfFields(); f++)
|
for (vtkm::Id f = 0; f < dataSet.GetNumberOfFields(); f++)
|
||||||
{
|
{
|
||||||
const vtkm::cont::Field field = ds.GetField(f);
|
const vtkm::cont::Field field = dataSet.GetField(f);
|
||||||
|
|
||||||
if (field.GetAssociation() != vtkm::cont::Field::ASSOC_CELL_SET)
|
if (field.GetAssociation() != vtkm::cont::Field::ASSOC_CELL_SET)
|
||||||
|
{
|
||||||
continue;
|
continue;
|
||||||
if (field.GetAssocCellSet() != cs.GetCellSet().GetName())
|
}
|
||||||
|
if (field.GetAssocCellSet() != cellSet.GetCellSet().GetName()) {
|
||||||
continue;
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
vtkm::Id ncells = field.GetData().GetNumberOfValues();
|
vtkm::Id ncells = field.GetData().GetNumberOfValues();
|
||||||
int ncomps = field.GetData().GetNumberOfComponents();
|
int ncomps = field.GetData().GetNumberOfComponents();
|
||||||
@ -303,57 +274,62 @@ private:
|
|||||||
continue;
|
continue;
|
||||||
|
|
||||||
if (!wrote_header)
|
if (!wrote_header)
|
||||||
|
{
|
||||||
out << "CELL_DATA " << ncells << std::endl;
|
out << "CELL_DATA " << ncells << std::endl;
|
||||||
wrote_header = true;
|
wrote_header = true;
|
||||||
|
}
|
||||||
|
|
||||||
std::string typeName;
|
std::string typeName;
|
||||||
field.GetData().CastAndCall(GetDataTypeName(typeName));
|
field.GetData().CastAndCall(detail::GetDataTypeName(typeName));
|
||||||
|
|
||||||
out << "SCALARS " << field.GetName() << " " << typeName << " " << ncomps << std::endl;
|
out << "SCALARS " << field.GetName() << " "
|
||||||
|
<< typeName << " " << ncomps << std::endl;
|
||||||
out << "LOOKUP_TABLE default" << std::endl;
|
out << "LOOKUP_TABLE default" << std::endl;
|
||||||
|
|
||||||
field.GetData().CastAndCall(OutputFieldFunctor(out));
|
field.GetData().CastAndCall(detail::OutputFieldFunctor(out));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void WriteDataSetAsPoints(std::ostream &out,
|
static void WriteDataSetAsPoints(std::ostream &out,
|
||||||
vtkm::cont::DataSet ds)
|
vtkm::cont::DataSet dataSet)
|
||||||
{
|
{
|
||||||
out << "DATASET UNSTRUCTURED_GRID" << std::endl;
|
out << "DATASET UNSTRUCTURED_GRID" << std::endl;
|
||||||
WritePoints(out, ds);
|
WritePoints(out, dataSet);
|
||||||
WriteVertexCells(out, ds);
|
WriteVertexCells(out, dataSet);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <class CellSetType>
|
template <class CellSetType>
|
||||||
static void WriteDataSetAsUnstructured(std::ostream &out,
|
static void WriteDataSetAsUnstructured(std::ostream &out,
|
||||||
vtkm::cont::DataSet ds,
|
vtkm::cont::DataSet dataSet,
|
||||||
CellSetType cs)
|
CellSetType cellSet)
|
||||||
{
|
{
|
||||||
out << "DATASET UNSTRUCTURED_GRID" << std::endl;
|
out << "DATASET UNSTRUCTURED_GRID" << std::endl;
|
||||||
WritePoints(out, ds);
|
WritePoints(out, dataSet);
|
||||||
WriteExplicitCells(out, cs);
|
WriteExplicitCells(out, cellSet);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <vtkm::IdComponent DIM>
|
template <vtkm::IdComponent DIM>
|
||||||
static void WriteDataSetAsStructured(std::ostream &out,
|
static void WriteDataSetAsStructured(
|
||||||
vtkm::cont::DataSet ds,
|
std::ostream &out,
|
||||||
vtkm::cont::CellSetStructured<DIM> cs)
|
vtkm::cont::DataSet dataSet,
|
||||||
|
vtkm::cont::CellSetStructured<DIM> cellSet)
|
||||||
{
|
{
|
||||||
///\todo: support uniform/rectilinear
|
///\todo: support uniform/rectilinear
|
||||||
out << "DATASET STRUCTURED_GRID" << std::endl;
|
out << "DATASET STRUCTURED_GRID" << std::endl;
|
||||||
|
|
||||||
out << "DIMENSIONS ";
|
out << "DIMENSIONS ";
|
||||||
out << cs.GetPointDimensions()[0] << " ";
|
out << cellSet.GetPointDimensions()[0] << " ";
|
||||||
out << (DIM>1 ? cs.GetPointDimensions()[1] : 1) << " ";
|
out << (DIM>1 ? cellSet.GetPointDimensions()[1] : 1) << " ";
|
||||||
out << (DIM>2 ? cs.GetPointDimensions()[2] : 1) << std::endl;
|
out << (DIM>2 ? cellSet.GetPointDimensions()[2] : 1) << std::endl;
|
||||||
|
|
||||||
WritePoints(out, ds);
|
WritePoints(out, dataSet);
|
||||||
}
|
}
|
||||||
|
|
||||||
public:
|
static void Write(std::ostream &out,
|
||||||
static void Write(std::ostream &out, vtkm::cont::DataSet ds, int csindex=0)
|
vtkm::cont::DataSet dataSet,
|
||||||
|
vtkm::Id csindex=0)
|
||||||
{
|
{
|
||||||
VTKM_ASSERT_CONT(csindex < ds.GetNumberOfCellSets());
|
VTKM_ASSERT_CONT(csindex < dataSet.GetNumberOfCellSets());
|
||||||
|
|
||||||
out << "# vtk DataFile Version 3.0" << std::endl;
|
out << "# vtk DataFile Version 3.0" << std::endl;
|
||||||
out << "vtk output" << std::endl;
|
out << "vtk output" << std::endl;
|
||||||
@ -361,49 +337,103 @@ public:
|
|||||||
|
|
||||||
if (csindex < 0)
|
if (csindex < 0)
|
||||||
{
|
{
|
||||||
WriteDataSetAsPoints(out, ds);
|
WriteDataSetAsPoints(out, dataSet);
|
||||||
WritePointFields(out, ds);
|
WritePointFields(out, dataSet);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
vtkm::cont::DynamicCellSet cs = ds.GetCellSet(csindex);
|
vtkm::cont::DynamicCellSet cellSet = dataSet.GetCellSet(csindex);
|
||||||
if (cs.IsType<vtkm::cont::CellSetExplicit<> >())
|
if (cellSet.IsType<vtkm::cont::CellSetExplicit<> >())
|
||||||
{
|
{
|
||||||
WriteDataSetAsUnstructured(out,
|
WriteDataSetAsUnstructured(out,
|
||||||
ds,
|
dataSet,
|
||||||
cs.Cast<vtkm::cont::CellSetExplicit<> >());
|
cellSet.Cast<vtkm::cont::CellSetExplicit<> >());
|
||||||
}
|
}
|
||||||
else if (cs.IsType<vtkm::cont::CellSetStructured<2> >())
|
else if (cellSet.IsType<vtkm::cont::CellSetStructured<2> >())
|
||||||
{
|
{
|
||||||
WriteDataSetAsStructured(out,
|
WriteDataSetAsStructured(out,
|
||||||
ds,
|
dataSet,
|
||||||
cs.Cast<vtkm::cont::CellSetStructured<2> >());
|
cellSet.Cast<vtkm::cont::CellSetStructured<2> >());
|
||||||
}
|
}
|
||||||
else if (cs.IsType<vtkm::cont::CellSetStructured<3> >())
|
else if (cellSet.IsType<vtkm::cont::CellSetStructured<3> >())
|
||||||
{
|
{
|
||||||
WriteDataSetAsStructured(out,
|
WriteDataSetAsStructured(out,
|
||||||
ds,
|
dataSet,
|
||||||
cs.Cast<vtkm::cont::CellSetStructured<3> >());
|
cellSet.Cast<vtkm::cont::CellSetStructured<3> >());
|
||||||
}
|
}
|
||||||
else if (cs.IsType<vtkm::cont::CellSetSingleType<> >())
|
else if (cellSet.IsType<vtkm::cont::CellSetSingleType<> >())
|
||||||
{
|
{
|
||||||
// these function just like explicit cell sets
|
// these function just like explicit cell sets
|
||||||
WriteDataSetAsUnstructured(out,
|
WriteDataSetAsUnstructured(out,
|
||||||
ds,
|
dataSet,
|
||||||
cs.Cast<vtkm::cont::CellSetSingleType<> >());
|
cellSet.Cast<vtkm::cont::CellSetSingleType<> >());
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
VTKM_ASSERT_CONT(false);
|
VTKM_ASSERT_CONT(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
WritePointFields(out, ds);
|
WritePointFields(out, dataSet);
|
||||||
WriteCellFields(out, ds, cs);
|
WriteCellFields(out, dataSet, cellSet);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public:
|
||||||
|
VTKM_CONT_EXPORT
|
||||||
|
explicit VTKDataSetWriter(const std::string &filename)
|
||||||
|
: FileName(filename) { }
|
||||||
|
|
||||||
|
VTKM_CONT_EXPORT
|
||||||
|
void WriteDataSet(vtkm::cont::DataSet dataSet,
|
||||||
|
vtkm::Id cellSetIndex = 0) const
|
||||||
|
{
|
||||||
|
if (cellSetIndex >= dataSet.GetNumberOfCellSets())
|
||||||
|
{
|
||||||
|
if (cellSetIndex == 0)
|
||||||
|
{
|
||||||
|
// Special case where there are no cell sets. In this case, write out
|
||||||
|
// the data as points.
|
||||||
|
cellSetIndex = -1;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
throw vtkm::cont::ErrorControlBadValue(
|
||||||
|
"Selected invalid cell set index.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (dataSet.GetNumberOfCoordinateSystems() < 1)
|
||||||
|
{
|
||||||
|
throw vtkm::cont::ErrorControlBadValue(
|
||||||
|
"DataSet has no coordinate system, which is not supported by VTK file format.");
|
||||||
|
}
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
std::ofstream fileStream(this->FileName, std::fstream::trunc);
|
||||||
|
this->Write(fileStream, dataSet, cellSetIndex);
|
||||||
|
fileStream.close();
|
||||||
|
}
|
||||||
|
catch (std::ofstream::failure error)
|
||||||
|
{
|
||||||
|
throw vtkm::io::ErrorIO(error.what());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
VTKM_CONT_EXPORT
|
||||||
|
void WriteDataSet(vtkm::cont::DataSet dataSet,
|
||||||
|
const std::string &cellSetName)
|
||||||
|
{
|
||||||
|
this->WriteDataSet(dataSet, dataSet.GetCellSetIndex(cellSetName));
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::string FileName;
|
||||||
|
|
||||||
}; //struct VTKDataSetWriter
|
}; //struct VTKDataSetWriter
|
||||||
|
|
||||||
}}} //namespace vtkm::io::writer
|
}
|
||||||
|
}
|
||||||
|
} //namespace vtkm::io::writer
|
||||||
|
|
||||||
#endif //vtk_m_io_writer_DataSetWriter_h
|
#endif //vtk_m_io_writer_DataSetWriter_h
|
||||||
|
@ -18,66 +18,44 @@
|
|||||||
// this software.
|
// this software.
|
||||||
//============================================================================
|
//============================================================================
|
||||||
|
|
||||||
#include <algorithm>
|
#include <vtkm/io/writer/VTKDataSetWriter.h>
|
||||||
#include <fstream>
|
|
||||||
#include <iostream>
|
|
||||||
#include <iterator>
|
|
||||||
#include <vector>
|
|
||||||
|
|
||||||
#include <vtkm/cont/ArrayPortalToIterators.h>
|
|
||||||
#include <vtkm/cont/DeviceAdapter.h>
|
|
||||||
#include <vtkm/cont/testing/Testing.h>
|
#include <vtkm/cont/testing/Testing.h>
|
||||||
#include <vtkm/cont/testing/MakeTestDataSet.h>
|
#include <vtkm/cont/testing/MakeTestDataSet.h>
|
||||||
|
|
||||||
#include <vtkm/io/writer/VTKDataSetWriter.h>
|
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
void TestVTKExplicitWrite()
|
void TestVTKExplicitWrite()
|
||||||
{
|
{
|
||||||
vtkm::cont::testing::MakeTestDataSet tds;
|
vtkm::cont::testing::MakeTestDataSet tds;
|
||||||
|
|
||||||
std::ofstream out1("fileA1.vtk");
|
vtkm::io::writer::VTKDataSetWriter writer1("fileA1.vtk");
|
||||||
vtkm::io::writer::VTKDataSetWriter::Write(out1,
|
writer1.WriteDataSet(tds.Make3DExplicitDataSet0());
|
||||||
tds.Make3DExplicitDataSet0());
|
|
||||||
out1.close();
|
|
||||||
|
|
||||||
// force it to output an explicit grid as points
|
// force it to output an explicit grid as points
|
||||||
std::ofstream out2("fileA2.vtk");
|
vtkm::io::writer::VTKDataSetWriter writer2("fileA2.vtk");
|
||||||
vtkm::io::writer::VTKDataSetWriter::Write(out2,
|
writer2.WriteDataSet(tds.Make3DExplicitDataSet0(), -1);
|
||||||
tds.Make3DExplicitDataSet0(), -1);
|
|
||||||
out2.close();
|
|
||||||
|
|
||||||
std::ofstream out3("fileA3.vtk");
|
vtkm::io::writer::VTKDataSetWriter writer3("fileA3.vtk");
|
||||||
vtkm::io::writer::VTKDataSetWriter::Write(out3,
|
writer3.WriteDataSet(tds.Make3DExplicitDataSet0());
|
||||||
tds.Make3DExplicitDataSet0());
|
|
||||||
out3.close();
|
|
||||||
|
|
||||||
std::ofstream out4("fileA4.vtk");
|
vtkm::io::writer::VTKDataSetWriter writer4("fileA4.vtk");
|
||||||
vtkm::io::writer::VTKDataSetWriter::Write(out4,
|
writer4.WriteDataSet(tds.Make3DExplicitDataSetCowNose());
|
||||||
tds.Make3DExplicitDataSetCowNose());
|
|
||||||
out4.close();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void TestVTKUniformWrite()
|
void TestVTKUniformWrite()
|
||||||
{
|
{
|
||||||
vtkm::cont::testing::MakeTestDataSet tds;
|
vtkm::cont::testing::MakeTestDataSet tds;
|
||||||
|
|
||||||
std::ofstream out1("fileB1.vtk");
|
vtkm::io::writer::VTKDataSetWriter writer1("fileB1.vtk");
|
||||||
vtkm::io::writer::VTKDataSetWriter::Write(out1,
|
writer1.WriteDataSet(tds.Make2DUniformDataSet0());
|
||||||
tds.Make2DUniformDataSet0());
|
|
||||||
out1.close();
|
|
||||||
|
|
||||||
std::ofstream out2("fileB2.vtk");
|
vtkm::io::writer::VTKDataSetWriter writer2("fileB2.vtk");
|
||||||
vtkm::io::writer::VTKDataSetWriter::Write(out2,
|
writer2.WriteDataSet(tds.Make3DUniformDataSet0());
|
||||||
tds.Make3DUniformDataSet0());
|
|
||||||
out2.close();
|
|
||||||
|
|
||||||
// force it to output an explicit grid as points
|
// force it to output an explicit grid as points
|
||||||
std::ofstream out3("fileB3.vtk");
|
vtkm::io::writer::VTKDataSetWriter writer3("fileB3.vtk");
|
||||||
vtkm::io::writer::VTKDataSetWriter::Write(out3,
|
writer3.WriteDataSet(tds.Make3DUniformDataSet0(), -1);
|
||||||
tds.Make3DUniformDataSet0(), -1);
|
|
||||||
out3.close();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void TestVTKWrite()
|
void TestVTKWrite()
|
||||||
|
Loading…
Reference in New Issue
Block a user