vtk-m/tutorial/point_to_cell.cxx

106 lines
3.3 KiB
C++
Raw Normal View History

//============================================================================
// 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.
//============================================================================
2022-01-05 20:30:40 +00:00
#include <vtkm/cont/Initialize.h>
#include <vtkm/io/VTKDataSetReader.h>
#include <vtkm/io/VTKDataSetWriter.h>
2022-01-05 20:30:40 +00:00
#include <vtkm/worklet/WorkletMapTopology.h>
namespace vtkm
{
namespace worklet
{
struct ConvertPointFieldToCells : vtkm::worklet::WorkletVisitCellsWithPoints
{
using ControlSignature = void(CellSetIn topology,
FieldInPoint inPointField,
FieldOutCell outCellField);
using ExecutionSignature = void(_2 inPointField, _3 outCellField);
using InputDomain = _1;
template <typename InPointFieldVecType, typename OutCellFieldType>
2022-02-04 16:43:00 +00:00
VTKM_EXEC void operator()(const InPointFieldVecType& inPointFieldVec,
OutCellFieldType& outCellField) const
2022-01-05 20:30:40 +00:00
{
vtkm::IdComponent numPoints = inPointFieldVec.GetNumberOfComponents();
outCellField = OutCellFieldType(0);
for (vtkm::IdComponent pointIndex = 0; pointIndex < numPoints; ++pointIndex)
{
outCellField = outCellField + inPointFieldVec[pointIndex];
}
2022-02-16 20:16:07 +00:00
outCellField =
static_cast<OutCellFieldType>(outCellField / static_cast<vtkm::FloatDefault>(numPoints));
2022-01-05 20:30:40 +00:00
}
};
} // namespace worklet
} // namespace vtkm
#include <vtkm/filter/FilterField.h>
2022-01-05 20:30:40 +00:00
namespace vtkm
{
namespace filter
{
struct ConvertPointFieldToCells : vtkm::filter::FilterField
2022-01-05 20:30:40 +00:00
{
VTKM_CONT vtkm::cont::DataSet DoExecute(const vtkm::cont::DataSet& inDataSet) override;
2022-01-05 20:30:40 +00:00
};
VTKM_CONT cont::DataSet ConvertPointFieldToCells::DoExecute(const vtkm::cont::DataSet& inDataSet)
2022-01-05 20:30:40 +00:00
{
const auto& inField = this->GetFieldFromDataSet(inDataSet);
2022-01-05 20:30:40 +00:00
vtkm::cont::UnknownArrayHandle outArray;
auto resolveType = [&](const auto& inConcrete) {
using ValueType = typename std::decay_t<decltype(inConcrete)>::ValueType;
2022-01-05 20:30:40 +00:00
vtkm::cont::ArrayHandle<ValueType> outConcrete;
this->Invoke(
vtkm::worklet::ConvertPointFieldToCells{}, inDataSet.GetCellSet(), inConcrete, outConcrete);
outArray = outConcrete;
};
this->CastAndCallScalarField(inField, resolveType);
2022-01-05 20:30:40 +00:00
std::string outFieldName = this->GetOutputFieldName();
if (outFieldName == "")
{
outFieldName = inField.GetName();
2022-01-05 20:30:40 +00:00
}
return this->CreateResultFieldCell(inDataSet, outFieldName, outArray);
2022-01-05 20:30:40 +00:00
}
} // namespace filter
} // namespace vtkm
int main(int argc, char** argv)
{
auto opts = vtkm::cont::InitializeOptions::DefaultAnyDevice;
vtkm::cont::InitializeResult config = vtkm::cont::Initialize(argc, argv, opts);
const char* input = "data/kitchen.vtk";
vtkm::io::VTKDataSetReader reader(input);
vtkm::cont::DataSet ds_from_file = reader.ReadDataSet();
vtkm::filter::ConvertPointFieldToCells pointToCell;
pointToCell.SetActiveField("c1");
vtkm::cont::DataSet ds_from_convert = pointToCell.Execute(ds_from_file);
vtkm::io::VTKDataSetWriter writer("out_point_to_cell.vtk");
writer.WriteDataSet(ds_from_convert);
return 0;
}