2019-07-31 20:22:26 +00:00
|
|
|
//============================================================================
|
|
|
|
// 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>
|
|
|
|
|
2022-12-01 20:07:56 +00:00
|
|
|
#include <vtkm/filter/FilterField.h>
|
2019-07-31 20:22:26 +00:00
|
|
|
|
2021-09-08 14:54:15 +00:00
|
|
|
#include <vtkm/io/VTKDataSetReader.h>
|
2020-04-30 12:12:04 +00:00
|
|
|
#include <vtkm/io/VTKDataSetWriter.h>
|
2019-07-31 20:22:26 +00:00
|
|
|
|
|
|
|
#include <vtkm/cont/Initialize.h>
|
|
|
|
|
|
|
|
#include <vtkm/VectorAnalysis.h>
|
|
|
|
|
2021-09-08 14:54:15 +00:00
|
|
|
#include <cstdlib>
|
|
|
|
#include <iostream>
|
|
|
|
|
2022-09-26 16:23:26 +00:00
|
|
|
namespace hello_worklet_example
|
2019-07-31 20:22:26 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
struct HelloWorklet : public vtkm::worklet::WorkletMapField
|
|
|
|
{
|
|
|
|
using ControlSignature = void(FieldIn inVector, FieldOut outMagnitude);
|
|
|
|
|
2022-09-26 16:23:26 +00:00
|
|
|
template <typename T>
|
|
|
|
VTKM_EXEC void operator()(const vtkm::Vec<T, 3>& inVector, T& outMagnitude) const
|
2019-07-31 20:22:26 +00:00
|
|
|
{
|
|
|
|
outMagnitude = vtkm::Magnitude(inVector);
|
|
|
|
}
|
|
|
|
};
|
2022-09-26 16:23:26 +00:00
|
|
|
|
|
|
|
} // namespace hello_worklet_example
|
2019-07-31 20:22:26 +00:00
|
|
|
|
|
|
|
namespace vtkm
|
|
|
|
{
|
|
|
|
namespace filter
|
|
|
|
{
|
|
|
|
|
2022-12-01 20:07:56 +00:00
|
|
|
class HelloField : public vtkm::filter::FilterField
|
2019-07-31 20:22:26 +00:00
|
|
|
{
|
|
|
|
public:
|
2022-09-26 16:23:26 +00:00
|
|
|
VTKM_CONT vtkm::cont::DataSet DoExecute(const vtkm::cont::DataSet& inDataSet)
|
2019-07-31 20:22:26 +00:00
|
|
|
{
|
2022-09-26 16:23:26 +00:00
|
|
|
// Input field
|
|
|
|
vtkm::cont::Field inField = this->GetFieldFromDataSet(inDataSet);
|
|
|
|
|
|
|
|
// Holder for output
|
|
|
|
vtkm::cont::UnknownArrayHandle outArray;
|
2019-07-31 20:22:26 +00:00
|
|
|
|
2022-09-26 16:23:26 +00:00
|
|
|
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;
|
|
|
|
};
|
2019-07-31 20:22:26 +00:00
|
|
|
|
2022-09-26 16:23:26 +00:00
|
|
|
this->CastAndCallVecField<3>(inField, resolveType);
|
2019-07-31 20:22:26 +00:00
|
|
|
|
2022-09-26 16:23:26 +00:00
|
|
|
std::string outFieldName = this->GetOutputFieldName();
|
|
|
|
if (outFieldName.empty())
|
2019-07-31 20:22:26 +00:00
|
|
|
{
|
2022-09-26 16:23:26 +00:00
|
|
|
outFieldName = inField.GetName() + "_magnitude";
|
2019-07-31 20:22:26 +00:00
|
|
|
}
|
|
|
|
|
2022-09-26 16:23:26 +00:00
|
|
|
return this->CreateResultField(inDataSet, outFieldName, inField.GetAssociation(), outArray);
|
2019-07-31 20:22:26 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
} // vtkm::filter
|
|
|
|
|
|
|
|
|
|
|
|
int main(int argc, char** argv)
|
|
|
|
{
|
2021-09-08 14:54:15 +00:00
|
|
|
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];
|
|
|
|
}
|
2019-07-31 20:22:26 +00:00
|
|
|
|
2021-09-08 14:54:15 +00:00
|
|
|
vtkm::io::VTKDataSetReader reader(infilename);
|
|
|
|
vtkm::cont::DataSet inputData = reader.ReadDataSet();
|
2019-07-31 20:22:26 +00:00
|
|
|
|
|
|
|
vtkm::filter::HelloField helloField;
|
2021-09-08 14:54:15 +00:00
|
|
|
helloField.SetActiveField(infield);
|
2019-07-31 20:22:26 +00:00
|
|
|
vtkm::cont::DataSet outputData = helloField.Execute(inputData);
|
|
|
|
|
2021-09-08 14:54:15 +00:00
|
|
|
vtkm::io::VTKDataSetWriter writer(outfilename);
|
2019-07-31 20:22:26 +00:00
|
|
|
writer.WriteDataSet(outputData);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|