vtk-m/examples/hello_worklet/HelloWorklet.cxx
Kenneth Moreland a17ebdf52a Deprecated vtkm::filter::FilterField
The original design of the filter base class required several specialized
base classes to control what information was pulled from the input
`DataSet` and provided to the derived class. Since the filter base class was
redesigned, the derived classes all get a `DataSet` and pull their own
information from it. Thus, most specialized filter base classes became
unnecessary and removed.

The one substantial exception was the `FilterField`. This filter base class
managed input and output arrays. This was kept separate from the base
`Filter` because not all filters need the ability to select this
information.

That said, this separation has not been particularly helpful. There are
several other features of `Filter` that does not apply to all subclasses.
Furthermore, there are several derived filters that are using `FilterField`
merely to pick a single part, like selecting a coordinate system, and
ignoring the rest of the abilities.

Thus, it makes more sense to deprecate `FilterField` and have these classes
inherit directly from `Filter`.
2024-02-12 08:13:19 +09:00

115 lines
3.1 KiB
C++

//============================================================================
// 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.
//============================================================================
#include <vtkm/worklet/WorkletMapField.h>
#include <vtkm/filter/Filter.h>
#include <vtkm/io/VTKDataSetReader.h>
#include <vtkm/io/VTKDataSetWriter.h>
#include <vtkm/cont/Initialize.h>
#include <vtkm/VectorAnalysis.h>
#include <cstdlib>
#include <iostream>
namespace hello_worklet_example
{
struct HelloWorklet : public vtkm::worklet::WorkletMapField
{
using ControlSignature = void(FieldIn inVector, FieldOut outMagnitude);
template <typename T>
VTKM_EXEC void operator()(const vtkm::Vec<T, 3>& inVector, T& outMagnitude) const
{
outMagnitude = vtkm::Magnitude(inVector);
}
};
} // namespace hello_worklet_example
namespace vtkm
{
namespace filter
{
class HelloField : public vtkm::filter::Filter
{
public:
VTKM_CONT vtkm::cont::DataSet DoExecute(const vtkm::cont::DataSet& inDataSet)
{
// Input field
vtkm::cont::Field inField = this->GetFieldFromDataSet(inDataSet);
// Holder for output
vtkm::cont::UnknownArrayHandle outArray;
hello_worklet_example::HelloWorklet mag;
auto resolveType = [&](const auto& inputArray) {
// use std::decay to remove const ref from the decltype of concrete.
using T = typename std::decay_t<decltype(inputArray)>::ValueType::ComponentType;
vtkm::cont::ArrayHandle<T> result;
this->Invoke(mag, inputArray, result);
outArray = result;
};
this->CastAndCallVecField<3>(inField, resolveType);
std::string outFieldName = this->GetOutputFieldName();
if (outFieldName.empty())
{
outFieldName = inField.GetName() + "_magnitude";
}
return this->CreateResultField(inDataSet, outFieldName, inField.GetAssociation(), outArray);
}
};
}
} // vtkm::filter
int main(int argc, char** argv)
{
vtkm::cont::Initialize(argc, argv);
if ((argc < 3) || (argc > 4))
{
std::cerr << "Usage: " << argv[0] << " in_data.vtk field_name [out_data.vtk]\n\n";
std::cerr << "For example, you could use the simple_unstructured_bin.vtk that comes with the "
"VTK-m source:\n\n";
std::cerr
<< " " << argv[0]
<< " <path-to-vtkm-source>/data/data/unstructured/simple_unstructured_bin.vtk vectors\n";
return 1;
}
std::string infilename = argv[1];
std::string infield = argv[2];
std::string outfilename = "out_data.vtk";
if (argc == 4)
{
outfilename = argv[3];
}
vtkm::io::VTKDataSetReader reader(infilename);
vtkm::cont::DataSet inputData = reader.ReadDataSet();
vtkm::filter::HelloField helloField;
helloField.SetActiveField(infield);
vtkm::cont::DataSet outputData = helloField.Execute(inputData);
vtkm::io::VTKDataSetWriter writer(outfilename);
writer.WriteDataSet(outputData);
return 0;
}