mirror of
https://gitlab.kitware.com/vtk/vtk-m
synced 2024-09-16 17:22:55 +00:00
Merge topic 'reader_oversegmented'
b82051746 Test that no warnings are generated and tests pass. 20c6e6933 Deprecate writer/ directory. Acked-by: Kitware Robot <kwrobot@kitware.com> Acked-by: Kenneth Moreland <kmorel@sandia.gov> Merge-request: !2065
This commit is contained in:
commit
e87e0a4dae
@ -22,7 +22,6 @@
|
||||
#include <vtkm/cont/DataSetFieldAdd.h>
|
||||
#include <vtkm/cont/DeviceAdapter.h>
|
||||
#include <vtkm/cont/ErrorFilterExecution.h>
|
||||
#include <vtkm/io/writer/VTKDataSetWriter.h>
|
||||
#include <vtkm/worklet/ParticleAdvection.h>
|
||||
#include <vtkm/worklet/WorkletMapField.h>
|
||||
#include <vtkm/worklet/particleadvection/GridEvaluators.h>
|
||||
|
@ -17,9 +17,6 @@
|
||||
|
||||
#include <vtkm/filter/GhostCellRemove.h>
|
||||
|
||||
#include <vtkm/io/writer/VTKDataSetWriter.h>
|
||||
|
||||
|
||||
namespace
|
||||
{
|
||||
|
||||
|
@ -28,7 +28,6 @@
|
||||
#include <vtkm/cont/testing/Testing.h>
|
||||
#include <vtkm/filter/MeshQuality.h>
|
||||
#include <vtkm/io/reader/VTKDataSetReader.h>
|
||||
#include <vtkm/io/writer/VTKDataSetWriter.h>
|
||||
|
||||
//Adapted from vtkm/cont/testing/MakeTestDataSet.h
|
||||
//Modified the content of the MakeExplicitDataSetZoo() function
|
||||
|
@ -12,6 +12,7 @@ set(headers
|
||||
ErrorIO.h
|
||||
DecodePNG.h
|
||||
EncodePNG.h
|
||||
VTKDataSetWriter.h
|
||||
)
|
||||
|
||||
set(sources
|
||||
@ -31,3 +32,4 @@ target_link_libraries(vtkm_io PUBLIC vtkm_cont PRIVATE vtkm_lodepng)
|
||||
add_subdirectory(internal)
|
||||
add_subdirectory(reader)
|
||||
add_subdirectory(writer)
|
||||
add_subdirectory(testing)
|
||||
|
415
vtkm/io/VTKDataSetWriter.h
Normal file
415
vtkm/io/VTKDataSetWriter.h
Normal file
@ -0,0 +1,415 @@
|
||||
//============================================================================
|
||||
// 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.
|
||||
//============================================================================
|
||||
#ifndef vtk_m_io_DataSetWriter_h
|
||||
#define vtk_m_io_DataSetWriter_h
|
||||
|
||||
#include <cctype>
|
||||
#include <vtkm/CellShape.h>
|
||||
|
||||
#include <vtkm/cont/CellSetExplicit.h>
|
||||
#include <vtkm/cont/CellSetSingleType.h>
|
||||
#include <vtkm/cont/CellSetStructured.h>
|
||||
#include <vtkm/cont/DataSet.h>
|
||||
#include <vtkm/cont/ErrorBadType.h>
|
||||
#include <vtkm/cont/ErrorBadValue.h>
|
||||
#include <vtkm/cont/Field.h>
|
||||
|
||||
#include <vtkm/io/ErrorIO.h>
|
||||
|
||||
#include <vtkm/io/internal/VTKDataSetTypes.h>
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstring>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace vtkm
|
||||
{
|
||||
namespace io
|
||||
{
|
||||
namespace detail
|
||||
{
|
||||
|
||||
struct OutputPointsFunctor
|
||||
{
|
||||
private:
|
||||
std::ostream& out;
|
||||
|
||||
template <typename PortalType>
|
||||
VTKM_CONT void Output(const PortalType& portal) const
|
||||
{
|
||||
for (vtkm::Id index = 0; index < portal.GetNumberOfValues(); index++)
|
||||
{
|
||||
const int VTKDims = 3; // VTK files always require 3 dims for points
|
||||
|
||||
using ValueType = typename PortalType::ValueType;
|
||||
using VecType = typename vtkm::VecTraits<ValueType>;
|
||||
|
||||
const ValueType& value = portal.Get(index);
|
||||
|
||||
vtkm::IdComponent numComponents = VecType::GetNumberOfComponents(value);
|
||||
for (vtkm::IdComponent c = 0; c < numComponents && c < VTKDims; c++)
|
||||
{
|
||||
out << (c == 0 ? "" : " ") << VecType::GetComponent(value, c);
|
||||
}
|
||||
for (vtkm::IdComponent c = numComponents; c < VTKDims; c++)
|
||||
{
|
||||
out << " 0";
|
||||
}
|
||||
out << '\n';
|
||||
}
|
||||
}
|
||||
|
||||
public:
|
||||
VTKM_CONT
|
||||
OutputPointsFunctor(std::ostream& o)
|
||||
: out(o)
|
||||
{
|
||||
}
|
||||
|
||||
template <typename T, typename Storage>
|
||||
VTKM_CONT void operator()(const vtkm::cont::ArrayHandle<T, Storage>& array) const
|
||||
{
|
||||
this->Output(array.ReadPortal());
|
||||
}
|
||||
};
|
||||
|
||||
struct OutputFieldFunctor
|
||||
{
|
||||
private:
|
||||
std::ostream& out;
|
||||
|
||||
template <typename PortalType>
|
||||
VTKM_CONT void Output(const PortalType& portal) const
|
||||
{
|
||||
for (vtkm::Id index = 0; index < portal.GetNumberOfValues(); index++)
|
||||
{
|
||||
using ValueType = typename PortalType::ValueType;
|
||||
using VecType = typename vtkm::VecTraits<ValueType>;
|
||||
|
||||
const ValueType& value = portal.Get(index);
|
||||
|
||||
vtkm::IdComponent numComponents = VecType::GetNumberOfComponents(value);
|
||||
for (vtkm::IdComponent c = 0; c < numComponents; c++)
|
||||
{
|
||||
out << (c == 0 ? "" : " ") << VecType::GetComponent(value, c);
|
||||
}
|
||||
out << '\n';
|
||||
}
|
||||
}
|
||||
|
||||
public:
|
||||
VTKM_CONT
|
||||
OutputFieldFunctor(std::ostream& o)
|
||||
: out(o)
|
||||
{
|
||||
}
|
||||
|
||||
template <typename T, typename Storage>
|
||||
VTKM_CONT void operator()(const vtkm::cont::ArrayHandle<T, Storage>& array) const
|
||||
{
|
||||
this->Output(array.ReadPortal());
|
||||
}
|
||||
};
|
||||
|
||||
class GetDataTypeName
|
||||
{
|
||||
public:
|
||||
GetDataTypeName(std::string& name)
|
||||
: Name(&name)
|
||||
{
|
||||
}
|
||||
|
||||
template <typename ArrayHandleType>
|
||||
void operator()(const ArrayHandleType&) const
|
||||
{
|
||||
using DataType = typename vtkm::VecTraits<typename ArrayHandleType::ValueType>::ComponentType;
|
||||
*this->Name = vtkm::io::internal::DataTypeName<DataType>::Name();
|
||||
}
|
||||
|
||||
private:
|
||||
std::string* Name;
|
||||
};
|
||||
|
||||
} // namespace detail
|
||||
|
||||
struct VTKDataSetWriter
|
||||
{
|
||||
private:
|
||||
static void WritePoints(std::ostream& out, const vtkm::cont::DataSet& dataSet)
|
||||
{
|
||||
///\todo: support other coordinate systems
|
||||
int cindex = 0;
|
||||
auto cdata = dataSet.GetCoordinateSystem(cindex).GetData();
|
||||
|
||||
vtkm::Id npoints = cdata.GetNumberOfValues();
|
||||
out << "POINTS " << npoints << " "
|
||||
<< vtkm::io::internal::DataTypeName<vtkm::FloatDefault>::Name() << " " << '\n';
|
||||
|
||||
detail::OutputPointsFunctor{ out }(cdata);
|
||||
}
|
||||
|
||||
template <class CellSetType>
|
||||
static void WriteExplicitCells(std::ostream& out, const CellSetType& cellSet)
|
||||
{
|
||||
vtkm::Id nCells = cellSet.GetNumberOfCells();
|
||||
|
||||
vtkm::Id conn_length = 0;
|
||||
for (vtkm::Id i = 0; i < nCells; ++i)
|
||||
{
|
||||
conn_length += 1 + cellSet.GetNumberOfPointsInCell(i);
|
||||
}
|
||||
|
||||
out << "CELLS " << nCells << " " << conn_length << '\n';
|
||||
|
||||
for (vtkm::Id i = 0; i < nCells; ++i)
|
||||
{
|
||||
vtkm::cont::ArrayHandle<vtkm::Id> ids;
|
||||
vtkm::Id nids = cellSet.GetNumberOfPointsInCell(i);
|
||||
cellSet.GetIndices(i, ids);
|
||||
out << nids;
|
||||
auto IdPortal = ids.ReadPortal();
|
||||
for (int j = 0; j < nids; ++j)
|
||||
out << " " << IdPortal.Get(j);
|
||||
out << '\n';
|
||||
}
|
||||
|
||||
out << "CELL_TYPES " << nCells << '\n';
|
||||
for (vtkm::Id i = 0; i < nCells; ++i)
|
||||
{
|
||||
vtkm::Id shape = cellSet.GetCellShape(i);
|
||||
out << shape << '\n';
|
||||
}
|
||||
}
|
||||
|
||||
static void WriteVertexCells(std::ostream& out, const vtkm::cont::DataSet& dataSet)
|
||||
{
|
||||
vtkm::Id nCells = dataSet.GetCoordinateSystem(0).GetNumberOfPoints();
|
||||
|
||||
out << "CELLS " << nCells << " " << nCells * 2 << '\n';
|
||||
for (int i = 0; i < nCells; i++)
|
||||
{
|
||||
out << "1 " << i << '\n';
|
||||
}
|
||||
out << "CELL_TYPES " << nCells << '\n';
|
||||
for (int i = 0; i < nCells; i++)
|
||||
{
|
||||
out << vtkm::CELL_SHAPE_VERTEX << '\n';
|
||||
}
|
||||
}
|
||||
|
||||
static void WritePointFields(std::ostream& out, const vtkm::cont::DataSet& dataSet)
|
||||
{
|
||||
bool wrote_header = false;
|
||||
for (vtkm::Id f = 0; f < dataSet.GetNumberOfFields(); f++)
|
||||
{
|
||||
const vtkm::cont::Field field = dataSet.GetField(f);
|
||||
|
||||
if (field.GetAssociation() != vtkm::cont::Field::Association::POINTS)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
vtkm::Id npoints = field.GetNumberOfValues();
|
||||
int ncomps = field.GetData().GetNumberOfComponents();
|
||||
if (ncomps > 4)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!wrote_header)
|
||||
{
|
||||
out << "POINT_DATA " << npoints << '\n';
|
||||
wrote_header = true;
|
||||
}
|
||||
|
||||
std::string typeName;
|
||||
vtkm::cont::CastAndCall(field.GetData().ResetTypes(TypeListAll{}),
|
||||
detail::GetDataTypeName(typeName));
|
||||
std::string name = field.GetName();
|
||||
for (auto& c : name)
|
||||
{
|
||||
if (std::isspace(c))
|
||||
{
|
||||
c = '_';
|
||||
}
|
||||
}
|
||||
out << "SCALARS " << name << " " << typeName << " " << ncomps << '\n';
|
||||
out << "LOOKUP_TABLE default" << '\n';
|
||||
|
||||
vtkm::cont::CastAndCall(field.GetData().ResetTypes(TypeListAll{}),
|
||||
detail::OutputFieldFunctor(out));
|
||||
}
|
||||
}
|
||||
|
||||
static void WriteCellFields(std::ostream& out, const vtkm::cont::DataSet& dataSet)
|
||||
{
|
||||
bool wrote_header = false;
|
||||
for (vtkm::Id f = 0; f < dataSet.GetNumberOfFields(); f++)
|
||||
{
|
||||
const vtkm::cont::Field field = dataSet.GetField(f);
|
||||
if (!field.IsFieldCell())
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
vtkm::Id ncells = field.GetNumberOfValues();
|
||||
int ncomps = field.GetData().GetNumberOfComponents();
|
||||
if (ncomps > 4)
|
||||
continue;
|
||||
|
||||
if (!wrote_header)
|
||||
{
|
||||
out << "CELL_DATA " << ncells << '\n';
|
||||
wrote_header = true;
|
||||
}
|
||||
|
||||
std::string typeName;
|
||||
vtkm::cont::CastAndCall(field.GetData().ResetTypes(TypeListAll{}),
|
||||
detail::GetDataTypeName(typeName));
|
||||
|
||||
std::string name = field.GetName();
|
||||
for (auto& c : name)
|
||||
{
|
||||
if (std::isspace(c))
|
||||
{
|
||||
c = '_';
|
||||
}
|
||||
}
|
||||
|
||||
out << "SCALARS " << name << " " << typeName << " " << ncomps << '\n';
|
||||
out << "LOOKUP_TABLE default" << '\n';
|
||||
|
||||
vtkm::cont::CastAndCall(field.GetData().ResetTypes(TypeListAll{}),
|
||||
detail::OutputFieldFunctor(out));
|
||||
}
|
||||
}
|
||||
|
||||
static void WriteDataSetAsPoints(std::ostream& out, const vtkm::cont::DataSet& dataSet)
|
||||
{
|
||||
out << "DATASET UNSTRUCTURED_GRID" << '\n';
|
||||
WritePoints(out, dataSet);
|
||||
WriteVertexCells(out, dataSet);
|
||||
}
|
||||
|
||||
template <class CellSetType>
|
||||
static void WriteDataSetAsUnstructured(std::ostream& out,
|
||||
const vtkm::cont::DataSet& dataSet,
|
||||
const CellSetType& cellSet)
|
||||
{
|
||||
out << "DATASET UNSTRUCTURED_GRID" << '\n';
|
||||
WritePoints(out, dataSet);
|
||||
WriteExplicitCells(out, cellSet);
|
||||
}
|
||||
|
||||
template <vtkm::IdComponent DIM>
|
||||
static void WriteDataSetAsStructured(std::ostream& out,
|
||||
const vtkm::cont::DataSet& dataSet,
|
||||
const vtkm::cont::CellSetStructured<DIM>& cellSet)
|
||||
{
|
||||
///\todo: support uniform/rectilinear
|
||||
out << "DATASET STRUCTURED_GRID" << '\n';
|
||||
|
||||
auto pointDimensions = cellSet.GetPointDimensions();
|
||||
using VTraits = vtkm::VecTraits<decltype(pointDimensions)>;
|
||||
|
||||
out << "DIMENSIONS ";
|
||||
out << VTraits::GetComponent(pointDimensions, 0) << " ";
|
||||
out << (DIM > 1 ? VTraits::GetComponent(pointDimensions, 1) : 1) << " ";
|
||||
out << (DIM > 2 ? VTraits::GetComponent(pointDimensions, 2) : 1) << " ";
|
||||
|
||||
WritePoints(out, dataSet);
|
||||
}
|
||||
|
||||
static void Write(std::ostream& out, const vtkm::cont::DataSet& dataSet, bool just_points = false)
|
||||
{
|
||||
// The Paraview parser cannot handle scientific notation:
|
||||
out << std::fixed;
|
||||
out << "# vtk DataFile Version 3.0" << '\n';
|
||||
out << "vtk output" << '\n';
|
||||
out << "ASCII" << '\n';
|
||||
|
||||
if (just_points)
|
||||
{
|
||||
WriteDataSetAsPoints(out, dataSet);
|
||||
WritePointFields(out, dataSet);
|
||||
}
|
||||
else
|
||||
{
|
||||
vtkm::cont::DynamicCellSet cellSet = dataSet.GetCellSet();
|
||||
if (cellSet.IsType<vtkm::cont::CellSetExplicit<>>())
|
||||
{
|
||||
WriteDataSetAsUnstructured(out, dataSet, cellSet.Cast<vtkm::cont::CellSetExplicit<>>());
|
||||
}
|
||||
else if (cellSet.IsType<vtkm::cont::CellSetStructured<1>>())
|
||||
{
|
||||
WriteDataSetAsStructured(out, dataSet, cellSet.Cast<vtkm::cont::CellSetStructured<1>>());
|
||||
}
|
||||
else if (cellSet.IsType<vtkm::cont::CellSetStructured<2>>())
|
||||
{
|
||||
WriteDataSetAsStructured(out, dataSet, cellSet.Cast<vtkm::cont::CellSetStructured<2>>());
|
||||
}
|
||||
else if (cellSet.IsType<vtkm::cont::CellSetStructured<3>>())
|
||||
{
|
||||
WriteDataSetAsStructured(out, dataSet, cellSet.Cast<vtkm::cont::CellSetStructured<3>>());
|
||||
}
|
||||
else if (cellSet.IsType<vtkm::cont::CellSetSingleType<>>())
|
||||
{
|
||||
// these function just like explicit cell sets
|
||||
WriteDataSetAsUnstructured(out, dataSet, cellSet.Cast<vtkm::cont::CellSetSingleType<>>());
|
||||
}
|
||||
else
|
||||
{
|
||||
throw vtkm::cont::ErrorBadType("Could not determine type to write out.");
|
||||
}
|
||||
|
||||
WritePointFields(out, dataSet);
|
||||
WriteCellFields(out, dataSet);
|
||||
}
|
||||
}
|
||||
|
||||
public:
|
||||
VTKM_CONT
|
||||
explicit VTKDataSetWriter(const std::string& filename)
|
||||
: FileName(filename)
|
||||
{
|
||||
}
|
||||
|
||||
VTKM_CONT
|
||||
void WriteDataSet(const vtkm::cont::DataSet& dataSet, bool just_points = false) const
|
||||
{
|
||||
if (dataSet.GetNumberOfCoordinateSystems() < 1)
|
||||
{
|
||||
throw vtkm::cont::ErrorBadValue(
|
||||
"DataSet has no coordinate system, which is not supported by VTK file format.");
|
||||
}
|
||||
try
|
||||
{
|
||||
std::ofstream fileStream(this->FileName.c_str(), std::fstream::trunc);
|
||||
this->Write(fileStream, dataSet, just_points);
|
||||
fileStream.close();
|
||||
}
|
||||
catch (std::ofstream::failure& error)
|
||||
{
|
||||
throw vtkm::io::ErrorIO(error.what());
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
std::string FileName;
|
||||
|
||||
}; //struct VTKDataSetWriter
|
||||
}
|
||||
} //namespace vtkm::io
|
||||
|
||||
#endif //vtk_m_io_DataSetWriter_h
|
12
vtkm/io/writer/testing/UnitTestVTKDataSetWriter.cxx → vtkm/io/testing/UnitTestVTKDataSetWriter.cxx
12
vtkm/io/writer/testing/UnitTestVTKDataSetWriter.cxx → vtkm/io/testing/UnitTestVTKDataSetWriter.cxx
@ -11,7 +11,7 @@
|
||||
#include <complex>
|
||||
#include <cstdio>
|
||||
#include <vector>
|
||||
#include <vtkm/io/writer/VTKDataSetWriter.h>
|
||||
#include <vtkm/io/VTKDataSetWriter.h>
|
||||
|
||||
#include <vtkm/cont/testing/MakeTestDataSet.h>
|
||||
#include <vtkm/cont/testing/Testing.h>
|
||||
@ -25,7 +25,7 @@ namespace
|
||||
void TestVTKWriteTestData(const std::string& methodName, const vtkm::cont::DataSet& data)
|
||||
{
|
||||
std::cout << "Writing " << methodName << std::endl;
|
||||
vtkm::io::writer::VTKDataSetWriter writer(methodName + ".vtk");
|
||||
vtkm::io::VTKDataSetWriter writer(methodName + ".vtk");
|
||||
writer.WriteDataSet(data);
|
||||
}
|
||||
|
||||
@ -51,7 +51,7 @@ void TestVTKExplicitWrite()
|
||||
WRITE_FILE(Make3DExplicitDataSetCowNose);
|
||||
|
||||
std::cout << "Force writer to output an explicit grid as points" << std::endl;
|
||||
vtkm::io::writer::VTKDataSetWriter writer("Make3DExplicitDataSet0-no-grid.vtk");
|
||||
vtkm::io::VTKDataSetWriter writer("Make3DExplicitDataSet0-no-grid.vtk");
|
||||
writer.WriteDataSet(tds.Make3DExplicitDataSet0(), true);
|
||||
}
|
||||
|
||||
@ -76,7 +76,7 @@ void TestVTKUniformWrite()
|
||||
WRITE_FILE(Make3DRegularDataSet1);
|
||||
|
||||
std::cout << "Force writer to output a uniform grid as points" << std::endl;
|
||||
vtkm::io::writer::VTKDataSetWriter writer("Make3DUniformDataSet0-no-grid.vtk");
|
||||
vtkm::io::VTKDataSetWriter writer("Make3DUniformDataSet0-no-grid.vtk");
|
||||
writer.WriteDataSet(tds.Make3DUniformDataSet0(), true);
|
||||
}
|
||||
|
||||
@ -89,7 +89,7 @@ void TestVTKRectilinearWrite()
|
||||
WRITE_FILE(Make3DRectilinearDataSet0);
|
||||
|
||||
std::cout << "Force writer to output a rectilinear grid as points" << std::endl;
|
||||
vtkm::io::writer::VTKDataSetWriter writer("Make3DRectilinearDataSet0-no-grid.vtk");
|
||||
vtkm::io::VTKDataSetWriter writer("Make3DRectilinearDataSet0-no-grid.vtk");
|
||||
writer.WriteDataSet(tds.Make3DRectilinearDataSet0(), true);
|
||||
}
|
||||
|
||||
@ -126,7 +126,7 @@ void TestVTKCompoundWrite()
|
||||
}
|
||||
|
||||
dsf.AddPointField(dataSet, "z", points.data(), static_cast<vtkm::Id>(points.size()));
|
||||
vtkm::io::writer::VTKDataSetWriter writer("chirp.vtk");
|
||||
vtkm::io::VTKDataSetWriter writer("chirp.vtk");
|
||||
writer.WriteDataSet(dataSet);
|
||||
std::remove("chirp.vtk");
|
||||
}
|
@ -13,6 +13,3 @@ set(headers
|
||||
)
|
||||
|
||||
vtkm_declare_headers(${headers})
|
||||
|
||||
#-----------------------------------------------------------------------------
|
||||
add_subdirectory(testing)
|
||||
|
@ -10,27 +10,8 @@
|
||||
#ifndef vtk_m_io_writer_DataSetWriter_h
|
||||
#define vtk_m_io_writer_DataSetWriter_h
|
||||
|
||||
#include <cctype>
|
||||
#include <vtkm/CellShape.h>
|
||||
|
||||
#include <vtkm/cont/CellSetExplicit.h>
|
||||
#include <vtkm/cont/CellSetSingleType.h>
|
||||
#include <vtkm/cont/CellSetStructured.h>
|
||||
#include <vtkm/cont/DataSet.h>
|
||||
#include <vtkm/cont/ErrorBadType.h>
|
||||
#include <vtkm/cont/ErrorBadValue.h>
|
||||
#include <vtkm/cont/Field.h>
|
||||
|
||||
#include <vtkm/io/ErrorIO.h>
|
||||
|
||||
#include <vtkm/io/internal/VTKDataSetTypes.h>
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstring>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <vtkm/Deprecated.h>
|
||||
#include <vtkm/io/VTKDataSetWriter.h>
|
||||
|
||||
namespace vtkm
|
||||
{
|
||||
@ -39,379 +20,15 @@ namespace io
|
||||
namespace writer
|
||||
{
|
||||
|
||||
namespace detail
|
||||
struct VTKM_DEPRECATED(1.6, "Please use vtkm::io::VTKDataSetWriter") VTKDataSetWriter
|
||||
: vtkm::io::VTKDataSetWriter
|
||||
{
|
||||
|
||||
struct OutputPointsFunctor
|
||||
{
|
||||
private:
|
||||
std::ostream& out;
|
||||
|
||||
template <typename PortalType>
|
||||
VTKM_CONT void Output(const PortalType& portal) const
|
||||
{
|
||||
for (vtkm::Id index = 0; index < portal.GetNumberOfValues(); index++)
|
||||
{
|
||||
const int VTKDims = 3; // VTK files always require 3 dims for points
|
||||
|
||||
using ValueType = typename PortalType::ValueType;
|
||||
using VecType = typename vtkm::VecTraits<ValueType>;
|
||||
|
||||
const ValueType& value = portal.Get(index);
|
||||
|
||||
vtkm::IdComponent numComponents = VecType::GetNumberOfComponents(value);
|
||||
for (vtkm::IdComponent c = 0; c < numComponents && c < VTKDims; c++)
|
||||
{
|
||||
out << (c == 0 ? "" : " ") << VecType::GetComponent(value, c);
|
||||
}
|
||||
for (vtkm::IdComponent c = numComponents; c < VTKDims; c++)
|
||||
{
|
||||
out << " 0";
|
||||
}
|
||||
out << '\n';
|
||||
}
|
||||
}
|
||||
|
||||
public:
|
||||
VTKM_CONT
|
||||
OutputPointsFunctor(std::ostream& o)
|
||||
: out(o)
|
||||
VTKDataSetWriter(const std::string& filename)
|
||||
: vtkm::io::VTKDataSetWriter(filename)
|
||||
{
|
||||
}
|
||||
|
||||
template <typename T, typename Storage>
|
||||
VTKM_CONT void operator()(const vtkm::cont::ArrayHandle<T, Storage>& array) const
|
||||
{
|
||||
this->Output(array.ReadPortal());
|
||||
}
|
||||
};
|
||||
|
||||
struct OutputFieldFunctor
|
||||
{
|
||||
private:
|
||||
std::ostream& out;
|
||||
|
||||
template <typename PortalType>
|
||||
VTKM_CONT void Output(const PortalType& portal) const
|
||||
{
|
||||
for (vtkm::Id index = 0; index < portal.GetNumberOfValues(); index++)
|
||||
{
|
||||
using ValueType = typename PortalType::ValueType;
|
||||
using VecType = typename vtkm::VecTraits<ValueType>;
|
||||
|
||||
const ValueType& value = portal.Get(index);
|
||||
|
||||
vtkm::IdComponent numComponents = VecType::GetNumberOfComponents(value);
|
||||
for (vtkm::IdComponent c = 0; c < numComponents; c++)
|
||||
{
|
||||
out << (c == 0 ? "" : " ") << VecType::GetComponent(value, c);
|
||||
}
|
||||
out << '\n';
|
||||
}
|
||||
}
|
||||
|
||||
public:
|
||||
VTKM_CONT
|
||||
OutputFieldFunctor(std::ostream& o)
|
||||
: out(o)
|
||||
{
|
||||
}
|
||||
|
||||
template <typename T, typename Storage>
|
||||
VTKM_CONT void operator()(const vtkm::cont::ArrayHandle<T, Storage>& array) const
|
||||
{
|
||||
this->Output(array.ReadPortal());
|
||||
}
|
||||
};
|
||||
|
||||
class GetDataTypeName
|
||||
{
|
||||
public:
|
||||
GetDataTypeName(std::string& name)
|
||||
: Name(&name)
|
||||
{
|
||||
}
|
||||
|
||||
template <typename ArrayHandleType>
|
||||
void operator()(const ArrayHandleType&) const
|
||||
{
|
||||
using DataType = typename vtkm::VecTraits<typename ArrayHandleType::ValueType>::ComponentType;
|
||||
*this->Name = vtkm::io::internal::DataTypeName<DataType>::Name();
|
||||
}
|
||||
|
||||
private:
|
||||
std::string* Name;
|
||||
};
|
||||
|
||||
} // namespace detail
|
||||
|
||||
struct VTKDataSetWriter
|
||||
{
|
||||
private:
|
||||
static void WritePoints(std::ostream& out, const vtkm::cont::DataSet& dataSet)
|
||||
{
|
||||
///\todo: support other coordinate systems
|
||||
int cindex = 0;
|
||||
auto cdata = dataSet.GetCoordinateSystem(cindex).GetData();
|
||||
|
||||
vtkm::Id npoints = cdata.GetNumberOfValues();
|
||||
out << "POINTS " << npoints << " "
|
||||
<< vtkm::io::internal::DataTypeName<vtkm::FloatDefault>::Name() << " " << '\n';
|
||||
|
||||
detail::OutputPointsFunctor{ out }(cdata);
|
||||
}
|
||||
|
||||
template <class CellSetType>
|
||||
static void WriteExplicitCells(std::ostream& out, const CellSetType& cellSet)
|
||||
{
|
||||
vtkm::Id nCells = cellSet.GetNumberOfCells();
|
||||
|
||||
vtkm::Id conn_length = 0;
|
||||
for (vtkm::Id i = 0; i < nCells; ++i)
|
||||
{
|
||||
conn_length += 1 + cellSet.GetNumberOfPointsInCell(i);
|
||||
}
|
||||
|
||||
out << "CELLS " << nCells << " " << conn_length << '\n';
|
||||
|
||||
for (vtkm::Id i = 0; i < nCells; ++i)
|
||||
{
|
||||
vtkm::cont::ArrayHandle<vtkm::Id> ids;
|
||||
vtkm::Id nids = cellSet.GetNumberOfPointsInCell(i);
|
||||
cellSet.GetIndices(i, ids);
|
||||
out << nids;
|
||||
auto IdPortal = ids.ReadPortal();
|
||||
for (int j = 0; j < nids; ++j)
|
||||
out << " " << IdPortal.Get(j);
|
||||
out << '\n';
|
||||
}
|
||||
|
||||
out << "CELL_TYPES " << nCells << '\n';
|
||||
for (vtkm::Id i = 0; i < nCells; ++i)
|
||||
{
|
||||
vtkm::Id shape = cellSet.GetCellShape(i);
|
||||
out << shape << '\n';
|
||||
}
|
||||
}
|
||||
|
||||
static void WriteVertexCells(std::ostream& out, const vtkm::cont::DataSet& dataSet)
|
||||
{
|
||||
vtkm::Id nCells = dataSet.GetCoordinateSystem(0).GetNumberOfPoints();
|
||||
|
||||
out << "CELLS " << nCells << " " << nCells * 2 << '\n';
|
||||
for (int i = 0; i < nCells; i++)
|
||||
{
|
||||
out << "1 " << i << '\n';
|
||||
}
|
||||
out << "CELL_TYPES " << nCells << '\n';
|
||||
for (int i = 0; i < nCells; i++)
|
||||
{
|
||||
out << vtkm::CELL_SHAPE_VERTEX << '\n';
|
||||
}
|
||||
}
|
||||
|
||||
static void WritePointFields(std::ostream& out, const vtkm::cont::DataSet& dataSet)
|
||||
{
|
||||
bool wrote_header = false;
|
||||
for (vtkm::Id f = 0; f < dataSet.GetNumberOfFields(); f++)
|
||||
{
|
||||
const vtkm::cont::Field field = dataSet.GetField(f);
|
||||
|
||||
if (field.GetAssociation() != vtkm::cont::Field::Association::POINTS)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
vtkm::Id npoints = field.GetNumberOfValues();
|
||||
int ncomps = field.GetData().GetNumberOfComponents();
|
||||
if (ncomps > 4)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!wrote_header)
|
||||
{
|
||||
out << "POINT_DATA " << npoints << '\n';
|
||||
wrote_header = true;
|
||||
}
|
||||
|
||||
std::string typeName;
|
||||
vtkm::cont::CastAndCall(field.GetData().ResetTypes(TypeListAll{}),
|
||||
detail::GetDataTypeName(typeName));
|
||||
std::string name = field.GetName();
|
||||
for (auto& c : name)
|
||||
{
|
||||
if (std::isspace(c))
|
||||
{
|
||||
c = '_';
|
||||
}
|
||||
}
|
||||
out << "SCALARS " << name << " " << typeName << " " << ncomps << '\n';
|
||||
out << "LOOKUP_TABLE default" << '\n';
|
||||
|
||||
vtkm::cont::CastAndCall(field.GetData().ResetTypes(TypeListAll{}),
|
||||
detail::OutputFieldFunctor(out));
|
||||
}
|
||||
}
|
||||
|
||||
static void WriteCellFields(std::ostream& out, const vtkm::cont::DataSet& dataSet)
|
||||
{
|
||||
bool wrote_header = false;
|
||||
for (vtkm::Id f = 0; f < dataSet.GetNumberOfFields(); f++)
|
||||
{
|
||||
const vtkm::cont::Field field = dataSet.GetField(f);
|
||||
if (!field.IsFieldCell())
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
vtkm::Id ncells = field.GetNumberOfValues();
|
||||
int ncomps = field.GetData().GetNumberOfComponents();
|
||||
if (ncomps > 4)
|
||||
continue;
|
||||
|
||||
if (!wrote_header)
|
||||
{
|
||||
out << "CELL_DATA " << ncells << '\n';
|
||||
wrote_header = true;
|
||||
}
|
||||
|
||||
std::string typeName;
|
||||
vtkm::cont::CastAndCall(field.GetData().ResetTypes(TypeListAll{}),
|
||||
detail::GetDataTypeName(typeName));
|
||||
|
||||
std::string name = field.GetName();
|
||||
for (auto& c : name)
|
||||
{
|
||||
if (std::isspace(c))
|
||||
{
|
||||
c = '_';
|
||||
}
|
||||
}
|
||||
|
||||
out << "SCALARS " << name << " " << typeName << " " << ncomps << '\n';
|
||||
out << "LOOKUP_TABLE default" << '\n';
|
||||
|
||||
vtkm::cont::CastAndCall(field.GetData().ResetTypes(TypeListAll{}),
|
||||
detail::OutputFieldFunctor(out));
|
||||
}
|
||||
}
|
||||
|
||||
static void WriteDataSetAsPoints(std::ostream& out, const vtkm::cont::DataSet& dataSet)
|
||||
{
|
||||
out << "DATASET UNSTRUCTURED_GRID" << '\n';
|
||||
WritePoints(out, dataSet);
|
||||
WriteVertexCells(out, dataSet);
|
||||
}
|
||||
|
||||
template <class CellSetType>
|
||||
static void WriteDataSetAsUnstructured(std::ostream& out,
|
||||
const vtkm::cont::DataSet& dataSet,
|
||||
const CellSetType& cellSet)
|
||||
{
|
||||
out << "DATASET UNSTRUCTURED_GRID" << '\n';
|
||||
WritePoints(out, dataSet);
|
||||
WriteExplicitCells(out, cellSet);
|
||||
}
|
||||
|
||||
template <vtkm::IdComponent DIM>
|
||||
static void WriteDataSetAsStructured(std::ostream& out,
|
||||
const vtkm::cont::DataSet& dataSet,
|
||||
const vtkm::cont::CellSetStructured<DIM>& cellSet)
|
||||
{
|
||||
///\todo: support uniform/rectilinear
|
||||
out << "DATASET STRUCTURED_GRID" << '\n';
|
||||
|
||||
auto pointDimensions = cellSet.GetPointDimensions();
|
||||
using VTraits = vtkm::VecTraits<decltype(pointDimensions)>;
|
||||
|
||||
out << "DIMENSIONS ";
|
||||
out << VTraits::GetComponent(pointDimensions, 0) << " ";
|
||||
out << (DIM > 1 ? VTraits::GetComponent(pointDimensions, 1) : 1) << " ";
|
||||
out << (DIM > 2 ? VTraits::GetComponent(pointDimensions, 2) : 1) << " ";
|
||||
|
||||
WritePoints(out, dataSet);
|
||||
}
|
||||
|
||||
static void Write(std::ostream& out, const vtkm::cont::DataSet& dataSet, bool just_points = false)
|
||||
{
|
||||
// The Paraview parser cannot handle scientific notation:
|
||||
out << std::fixed;
|
||||
out << "# vtk DataFile Version 3.0" << '\n';
|
||||
out << "vtk output" << '\n';
|
||||
out << "ASCII" << '\n';
|
||||
|
||||
if (just_points)
|
||||
{
|
||||
WriteDataSetAsPoints(out, dataSet);
|
||||
WritePointFields(out, dataSet);
|
||||
}
|
||||
else
|
||||
{
|
||||
vtkm::cont::DynamicCellSet cellSet = dataSet.GetCellSet();
|
||||
if (cellSet.IsType<vtkm::cont::CellSetExplicit<>>())
|
||||
{
|
||||
WriteDataSetAsUnstructured(out, dataSet, cellSet.Cast<vtkm::cont::CellSetExplicit<>>());
|
||||
}
|
||||
else if (cellSet.IsType<vtkm::cont::CellSetStructured<1>>())
|
||||
{
|
||||
WriteDataSetAsStructured(out, dataSet, cellSet.Cast<vtkm::cont::CellSetStructured<1>>());
|
||||
}
|
||||
else if (cellSet.IsType<vtkm::cont::CellSetStructured<2>>())
|
||||
{
|
||||
WriteDataSetAsStructured(out, dataSet, cellSet.Cast<vtkm::cont::CellSetStructured<2>>());
|
||||
}
|
||||
else if (cellSet.IsType<vtkm::cont::CellSetStructured<3>>())
|
||||
{
|
||||
WriteDataSetAsStructured(out, dataSet, cellSet.Cast<vtkm::cont::CellSetStructured<3>>());
|
||||
}
|
||||
else if (cellSet.IsType<vtkm::cont::CellSetSingleType<>>())
|
||||
{
|
||||
// these function just like explicit cell sets
|
||||
WriteDataSetAsUnstructured(out, dataSet, cellSet.Cast<vtkm::cont::CellSetSingleType<>>());
|
||||
}
|
||||
else
|
||||
{
|
||||
throw vtkm::cont::ErrorBadType("Could not determine type to write out.");
|
||||
}
|
||||
|
||||
WritePointFields(out, dataSet);
|
||||
WriteCellFields(out, dataSet);
|
||||
}
|
||||
}
|
||||
|
||||
public:
|
||||
VTKM_CONT
|
||||
explicit VTKDataSetWriter(const std::string& filename)
|
||||
: FileName(filename)
|
||||
{
|
||||
}
|
||||
|
||||
VTKM_CONT
|
||||
void WriteDataSet(const vtkm::cont::DataSet& dataSet, bool just_points = false) const
|
||||
{
|
||||
if (dataSet.GetNumberOfCoordinateSystems() < 1)
|
||||
{
|
||||
throw vtkm::cont::ErrorBadValue(
|
||||
"DataSet has no coordinate system, which is not supported by VTK file format.");
|
||||
}
|
||||
try
|
||||
{
|
||||
std::ofstream fileStream(this->FileName.c_str(), std::fstream::trunc);
|
||||
this->Write(fileStream, dataSet, just_points);
|
||||
fileStream.close();
|
||||
}
|
||||
catch (std::ofstream::failure& error)
|
||||
{
|
||||
throw vtkm::io::ErrorIO(error.what());
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
std::string FileName;
|
||||
|
||||
}; //struct VTKDataSetWriter
|
||||
}
|
||||
}
|
||||
} //namespace vtkm::io::writer
|
||||
|
@ -23,7 +23,7 @@ vtkm::UInt32 DecodePNG(std::vector<unsigned char>& out_image,
|
||||
unsigned long& image_width,
|
||||
unsigned long& image_height,
|
||||
const unsigned char* in_png,
|
||||
std::size_t in_size) VTKM_DEPRECATED("Please use vtkm::io::DecodePNG")
|
||||
std::size_t in_size) VTKM_DEPRECATED(1.6, "Please use vtkm::io::DecodePNG")
|
||||
{
|
||||
return vtkm::io::DecodePNG(out_image, image_width, image_height, in_png, in_size);
|
||||
}
|
||||
|
@ -23,7 +23,7 @@ vtkm::UInt32 EncodePNG(std::vector<unsigned char> const& image,
|
||||
unsigned long width,
|
||||
unsigned long height,
|
||||
unsigned char* out_png,
|
||||
std::size_t out_size) VTKM_DEPRECATED("Please use vtkm::io::EncodePNG.")
|
||||
std::size_t out_size) VTKM_DEPRECATED(1.6, "Please use vtkm::io::EncodePNG.")
|
||||
{
|
||||
return io::EncodePNG(image, width, height, out_png, out_size);
|
||||
}
|
||||
|
@ -8,7 +8,7 @@
|
||||
// PURPOSE. See the above copyright notice for more information.
|
||||
//============================================================================
|
||||
|
||||
#include <vtkm/io/writer/VTKDataSetWriter.h>
|
||||
#include <vtkm/io/VTKDataSetWriter.h>
|
||||
#include <vtkm/rendering/ScalarRenderer.h>
|
||||
#include <vtkm/rendering/testing/RenderTest.h>
|
||||
|
||||
@ -31,7 +31,7 @@ void RenderTests()
|
||||
vtkm::rendering::ScalarRenderer::Result res = renderer.Render(camera);
|
||||
|
||||
vtkm::cont::DataSet result = res.ToDataSet();
|
||||
vtkm::io::writer::VTKDataSetWriter writer("scalar.vtk");
|
||||
vtkm::io::VTKDataSetWriter writer("scalar.vtk");
|
||||
writer.WriteDataSet(result);
|
||||
}
|
||||
|
||||
|
@ -14,7 +14,6 @@
|
||||
|
||||
#include <vtkm/cont/testing/MakeTestDataSet.h>
|
||||
#include <vtkm/cont/testing/Testing.h>
|
||||
#include <vtkm/io/writer/VTKDataSetWriter.h>
|
||||
|
||||
#include <vtkm/worklet/ExternalFaces.h>
|
||||
|
||||
@ -115,9 +114,6 @@ void TestExternalFaces2()
|
||||
vtkm::cont::testing::MakeTestDataSet dataSetMaker;
|
||||
vtkm::cont::DataSet inDataSet = dataSetMaker.Make3DExplicitDataSet5();
|
||||
|
||||
// vtkm::io::writer::VTKDataSetWriter writer("vtkm_explicit_data_5.vtk");
|
||||
// writer.WriteDataSet(inDataSet);
|
||||
|
||||
// Expected faces
|
||||
const vtkm::IdComponent MAX_POINTS_PER_FACE = 4;
|
||||
const vtkm::Id NUM_FACES = 12;
|
||||
|
@ -10,7 +10,7 @@
|
||||
|
||||
#include <vtkm/cont/DataSetBuilderExplicit.h>
|
||||
#include <vtkm/cont/testing/Testing.h>
|
||||
#include <vtkm/io/writer/VTKDataSetWriter.h>
|
||||
#include <vtkm/io/VTKDataSetWriter.h>
|
||||
#include <vtkm/worklet/DispatcherMapField.h>
|
||||
#include <vtkm/worklet/StreamSurface.h>
|
||||
|
||||
@ -62,13 +62,6 @@ void TestSameNumPolylines()
|
||||
"Wrong number of points in StreamSurface worklet");
|
||||
VTKM_TEST_ASSERT(newCells.GetNumberOfCells() == 12,
|
||||
"Wrong number of cells in StreamSurface worklet");
|
||||
/*
|
||||
vtkm::cont::DataSet ds2;
|
||||
ds2.AddCoordinateSystem(vtkm::cont::CoordinateSystem("coords", newPoints));
|
||||
ds2.SetCellSet(newCells);
|
||||
vtkm::io::writer::VTKDataSetWriter writer("srf.vtk");
|
||||
writer.WriteDataSet(ds2);
|
||||
*/
|
||||
}
|
||||
|
||||
void TestUnequalNumPolylines(int unequalType)
|
||||
@ -129,14 +122,6 @@ void TestUnequalNumPolylines(int unequalType)
|
||||
"Wrong number of points in StreamSurface worklet");
|
||||
VTKM_TEST_ASSERT(newCells.GetNumberOfCells() == numRequiredCells,
|
||||
"Wrong number of cells in StreamSurface worklet");
|
||||
|
||||
/*
|
||||
vtkm::cont::DataSet ds2;
|
||||
ds2.AddCoordinateSystem(vtkm::cont::CoordinateSystem("coords", newPoints));
|
||||
ds2.SetCellSet(newCells);
|
||||
vtkm::io::writer::VTKDataSetWriter writer("srf.vtk");
|
||||
writer.WriteDataSet(ds2);
|
||||
*/
|
||||
}
|
||||
|
||||
void TestStreamSurface()
|
||||
|
@ -21,8 +21,6 @@
|
||||
#include <vtkm/worklet/particleadvection/Particles.h>
|
||||
#include <vtkm/worklet/particleadvection/TemporalGridEvaluators.h>
|
||||
|
||||
#include <vtkm/io/writer/VTKDataSetWriter.h>
|
||||
|
||||
template <typename ScalarType>
|
||||
vtkm::cont::DataSet CreateUniformDataSet(const vtkm::Bounds& bounds, const vtkm::Id3& dims)
|
||||
{
|
||||
|
@ -10,7 +10,6 @@
|
||||
|
||||
#include <vtkm/cont/DataSetBuilderExplicit.h>
|
||||
#include <vtkm/cont/testing/Testing.h>
|
||||
#include <vtkm/io/writer/VTKDataSetWriter.h>
|
||||
#include <vtkm/worklet/DispatcherMapField.h>
|
||||
#include <vtkm/worklet/Tube.h>
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user