4.4. Extended Filter Implementations

In Chapter 3.3 (Simple Worklets) and Chapter 4.3 (Worklet Types) we discuss how to implement an algorithm in the Viskores framework by creating a worklet. For simplicity, worklet algorithms are wrapped in what are called filter objects for general usage. Chapter 2.6 (Running Filters) introduces the concept of filters, and Chapter 2.7 (Provided Filters) documents those that come with the Viskores library. Chapter 3.4 (Basic Filter Implementation) gives a brief introduction on implementing filters. This chapter elaborates on building new filter objects by introducing new filter types. These will be used to wrap filters around the extended worklet examples in Chapter 4.3 (Worklet Types).

Unsurprisingly, the base filter objects are contained in the viskores::filter package. In particular, all filter objects inherit from viskores::filter::Filter, either directly or indirectly. The filter implementation must override the protected pure virtual method viskores::filter::Filter::DoExecute(). The base class will call this method to run the operation of the filter.

The viskores::filter::Filter::DoExecute() method has a single argument that is a viskores::cont::DataSet. The viskores::cont::DataSet contains the data on which the filter will operate. viskores::filter::Filter::DoExecute() must then return a new viskores::cont::DataSet containing the derived data. The viskores::cont::DataSet should be created with one of the viskores::filter::Filter::CreateResult() methods.

A filter implementation may also optionally override the viskores::filter::Filter::DoExecutePartitions(). This method is similar to viskores::filter::Filter::DoExecute() except that it takes and returns a viskores::cont::PartitionedDataSet object. If a filter does not provide a viskores::filter::Filter::DoExecutePartitions() method, then if given a viskores::cont::PartitionedDataSet, the base class will call viskores::filter::Filter::DoExecute() on each of the partitions and build a viskores::cont::PartitionedDataSet with the results.

In addition to (or instead of) operating on the geometric structure of a viskores::cont::DataSet, a filter will commonly take one or more fields from the input viskores::cont::DataSet and write one or more fields to the result. For this reason, viskores::filter::Filter provides convenience methods to select input fields and output field names.

It also provides a method named viskores::filter::Filter::GetFieldFromDataSet() that can be used to get the input fields from the viskores::cont::DataSet passed to viskores::filter::Filter::DoExecute(). When getting a field with viskores::filter::Filter::GetFieldFromDataSet(), you get a viskores::cont::Field object. Before you can operate on the viskores::cont::Field, you have to convert it to a viskores::cont::ArrayHandle. viskores::filter::Filter::CastAndCallScalarField() can be used to do this conversion. It takes the field object as the first argument and attempts to convert it to an viskores::cont::ArrayHandle of different types. When it finds the correct type, it calls the provided functor with the appropriate viskores::cont::ArrayHandle. The similar viskores::filter::Filter::CastAndCallVecField() does the same thing to find an viskores::cont::ArrayHandle with viskores::Vec’s of a selected length, and viskores::filter::Filter::CastAndCallVariableVecField() does the same thing but will find viskores::Vec’s of any length.

The remainder of this chapter will provide some common patterns of filter operation based on the data they use and generate.

4.4.1. Deriving Fields from other Fields

A common type of filter is one that generates a new field that is derived from one or more existing fields or point coordinates on the data set. For example, mass, volume, and density are interrelated, and any one can be derived from the other two. Typically, you would use viskores::filter::Filter::GetFieldFromDataSet() to retrieve the input fields, one of the viskores::filter::Filter::CastAndCall() methods to resolve the array type of the field, and finally use viskores::filter::Filter::CreateResultField() to produce the output.

In this section we provide an example implementation of a field filter that wraps the “magnitude” worklet provided in Example 4.42. By C++ convention, object implementations are split into two files. The first file is a standard header file with a .h extension that contains the declaration of the filter class without the implementation. So we would expect the following code to be in a file named FieldMagnitude.h.

Example 4.55 Header declaration for a field filter.
 1namespace viskores
 2{
 3namespace filter
 4{
 5namespace vector_calculus
 6{
 7
 8class VISKORES_FILTER_VECTOR_CALCULUS_EXPORT FieldMagnitude
 9  : public viskores::filter::Filter
10{
11public:
12  VISKORES_CONT FieldMagnitude();
13
14  VISKORES_CONT viskores::cont::DataSet DoExecute(
15    const viskores::cont::DataSet& inDataSet) override;
16};
17
18} // namespace vector_calculus
19} // namespace filter
20} // namespace viskores

You may notice in Example 4.55, line 8 there is a special macro names VISKORES_FILTER_VECTOR_CALCULUS_EXPORT. This macro tells the C++ compiler that the class FieldMagnitude is going to be exported from a library. More specifically, the CMake for Viskores’s build will generate a header file containing this export macro for the associated library. By Viskores’s convention, a filter in the viskores::filter::vector_calculus will be defined in the viskores/filter/vector_calculus directory. When defining the targets for this library, CMake will create a header file named viskores_filter_vector_calculus.h that contains the macro named VISKORES_FILTER_VECTOR_CALCULUS_EXPORT. This macro will provide the correct modifiers for the particular C++ compiler being used to export the class from the library. If this macro is left out, then the library will work on some platforms, but on other platforms will produce a linker error for missing symbols.

Once the filter class is declared in the .h file, the implementation filter is by convention given in a separate .cxx file. So the continuation of our example that follows would be expected in a file named FieldMagnitude.cxx.

Example 4.56 Implementation of a field filter.
 1namespace viskores
 2{
 3namespace filter
 4{
 5namespace vector_calculus
 6{
 7
 8VISKORES_CONT
 9FieldMagnitude::FieldMagnitude()
10{
11  this->SetOutputFieldName("");
12}
13
14VISKORES_CONT viskores::cont::DataSet FieldMagnitude::DoExecute(
15  const viskores::cont::DataSet& inDataSet)
16{
17  viskores::cont::Field inField = this->GetFieldFromDataSet(inDataSet);
18
19  viskores::cont::UnknownArrayHandle outField;
20
21  // Use a C++ lambda expression to provide a callback for CastAndCall. The lambda
22  // will capture references to local variables like outFieldArray (using `[&]`)
23  // that it can read and write.
24  auto resolveType = [&](const auto& inFieldArray)
25  {
26    using InArrayHandleType = std::decay_t<decltype(inFieldArray)>;
27    using ComponentType =
28      typename viskores::VecTraits<typename InArrayHandleType::ValueType>::ComponentType;
29
30    viskores::cont::ArrayHandle<ComponentType> outFieldArray;
31
32    this->Invoke(ComputeMagnitude{}, inFieldArray, outFieldArray);
33    outField = outFieldArray;
34  };
35
36  this->CastAndCallVecField<3>(inField, resolveType);
37
38  std::string outFieldName = this->GetOutputFieldName();
39  if (outFieldName == "")
40  {
41    outFieldName = inField.GetName() + "_magnitude";
42  }
43
44  return this->CreateResultField(
45    inDataSet, outFieldName, inField.GetAssociation(), outField);
46}
47
48} // namespace vector_calculus
49} // namespace filter
50} // namespace viskores

The implementation of viskores::filter::Filter::DoExecute() first pulls the input field from the provided viskores::cont::DataSet using viskores::filter::Filter::GetFieldFromDataSet(). It then uses viskores::filter::Filter::CastAndCallVecField() to determine what type of viskores::cont::ArrayHandle is contained in the input field. That calls a lambda function that invokes a worklet to create the output field.

template<viskores::IdComponent VecSize, typename Functor, typename ...Args>
inline void viskores::filter::Filter::CastAndCallVecField(const viskores::cont::UnknownArrayHandle &fieldArray, Functor &&functor, Args&&... args) const

Convenience method to get the array from a filter’s input vector field.

A field filter typically gets its input fields using the internal GetFieldFromDataSet. To use this field in a worklet, it eventually needs to be converted to an viskores::cont::ArrayHandle. If the input field is limited to be a vector field with vectors of a specific size, then this method provides a convenient way to determine the correct array type. Like other CastAndCall methods, it takes as input a viskores::cont::Field (or viskores::cont::UnknownArrayHandle) and a function/functor to call with the appropriate viskores::cont::ArrayHandle type. You also have to provide the vector size as the first template argument. For example CastAndCallVecField<3>(field, functor);.

template<viskores::IdComponent VecSize, typename Functor, typename ...Args>
inline void viskores::filter::Filter::CastAndCallVecField(const viskores::cont::Field &field, Functor &&functor, Args&&... args) const

Convenience method to get the array from a filter’s input vector field.

A field filter typically gets its input fields using the internal GetFieldFromDataSet. To use this field in a worklet, it eventually needs to be converted to an viskores::cont::ArrayHandle. If the input field is limited to be a vector field with vectors of a specific size, then this method provides a convenient way to determine the correct array type. Like other CastAndCall methods, it takes as input a viskores::cont::Field (or viskores::cont::UnknownArrayHandle) and a function/functor to call with the appropriate viskores::cont::ArrayHandle type. You also have to provide the vector size as the first template argument. For example CastAndCallVecField<3>(field, functor);.

Did You Know?

The filter implemented in Example 4.56 is limited to only find the magnitude of viskores::Vec’s with 3 components. It may be the case you wish to implement a filter that operates on viskores::Vec’s of multiple sizes (or perhaps even any size). Chapter 3.5 (Unknown Array Handles) discusses how you can use the viskores::cont::UnknownArrayHandle contained in the viskores::cont::Field to more expressively decide what types to check for.

template<typename Functor, typename ...Args>
inline void viskores::filter::Filter::CastAndCallVariableVecField(const viskores::cont::UnknownArrayHandle &fieldArray, Functor &&functor, Args&&... args) const

This method is like CastAndCallVecField except that it can be used for a field of unknown vector size (or scalars).

This method will call the given functor with an viskores::cont::ArrayHandleRecombineVec.

Note that there are limitations with using viskores::cont::ArrayHandleRecombineVec within a worklet. Because the size of the vectors are not known at compile time, you cannot just create an intermediate viskores::Vec of the correct size. Typically, you must allocate the output array (for example, with viskores::cont::ArrayHandleRuntimeVec), and the worklet must iterate over the components and store them in the prealocated output.

template<typename Functor, typename ...Args>
inline void viskores::filter::Filter::CastAndCallVariableVecField(const viskores::cont::Field &field, Functor &&functor, Args&&... args) const

This method is like CastAndCallVecField except that it can be used for a field of unknown vector size (or scalars).

This method will call the given functor with an viskores::cont::ArrayHandleRecombineVec.

Note that there are limitations with using viskores::cont::ArrayHandleRecombineVec within a worklet. Because the size of the vectors are not known at compile time, you cannot just create an intermediate viskores::Vec of the correct size. Typically, you must allocate the output array (for example, with viskores::cont::ArrayHandleRuntimeVec), and the worklet must iterate over the components and store them in the prealocated output.

Finally, viskores::filter::Filter::CreateResultField() generates the output of the filter. Note that all fields need a unique name, which is the reason for the second argument to viskores::filter::Filter::CreateResult(). The viskores::filter::Filter base class contains a pair of methods named viskores::filter::Filter::SetOutputFieldName() and viskores::filter::Filter::GetOutputFieldName() to allow users to specify the name of output fields. The viskores::filter::Filter::DoExecute() method should respect the given output field name. However, it is also good practice for the filter to have a default name if none is given. This might be simply specifying a name in the constructor, but it is worthwhile for many filters to derive a name based on the name of the input field.

4.4.2. Deriving Fields from Topology

The previous example performed a simple operation on each element of a field independently. However, it is also common for a “field” filter to take into account the topology of a data set. In this case, the implementation involves pulling a viskores::cont::CellSet from the input viskores::cont::DataSet and performing operations on fields associated with different topological elements. The steps involve calling viskores::cont::DataSet::GetCellSet() to get access to the viskores::cont::CellSet object and then using topology-based worklets, described in Section 4.3.2 (Topology Map), to operate on them.

In this section we provide an example implementation of a field filter on cells that wraps the “cell center” worklet provided in Example 4.44.

Example 4.57 Header declaration for a field filter using cell topology.
 1namespace viskores
 2{
 3namespace filter
 4{
 5namespace field_conversion
 6{
 7
 8class VISKORES_FILTER_FIELD_CONVERSION_EXPORT CellCenters
 9  : public viskores::filter::Filter
10{
11public:
12  VISKORES_CONT CellCenters();
13
14  VISKORES_CONT viskores::cont::DataSet DoExecute(
15    const viskores::cont::DataSet& inDataSet) override;
16};
17
18} // namespace field_conversion
19} // namespace filter
20} // namespace viskores

As with any subclass of viskores::filter::Filter, the filter implements viskores::filter::Filter::DoExecute(), which in this case invokes a worklet to compute a new field array and then return a newly constructed viskores::cont::DataSet object.

Example 4.58 Implementation of a field filter using cell topology.
 1namespace viskores
 2{
 3namespace filter
 4{
 5namespace field_conversion
 6{
 7
 8VISKORES_CONT
 9CellCenters::CellCenters()
10{
11  this->SetOutputFieldName("");
12}
13
14VISKORES_CONT cont::DataSet CellCenters::DoExecute(
15  const viskores::cont::DataSet& inDataSet)
16{
17  viskores::cont::Field inField = this->GetFieldFromDataSet(inDataSet);
18
19  if (!inField.IsPointField())
20  {
21    throw viskores::cont::ErrorBadType("Cell Centers filter operates on point data.");
22  }
23
24  viskores::cont::UnknownArrayHandle outUnknownArray;
25
26  auto resolveType = [&](const auto& inArray)
27  {
28    using InArrayHandleType = std::decay_t<decltype(inArray)>;
29    using ValueType = typename InArrayHandleType::ValueType;
30    viskores::cont::ArrayHandle<ValueType> outArray;
31
32    this->Invoke(
33      viskores::worklet::CellCenter{}, inDataSet.GetCellSet(), inArray, outArray);
34
35    outUnknownArray = outArray;
36  };
37
38  viskores::cont::UnknownArrayHandle inUnknownArray = inField.GetData();
39  inUnknownArray.CastAndCallForTypesWithFloatFallback<VISKORES_DEFAULT_TYPE_LIST,
40                                                      VISKORES_DEFAULT_STORAGE_LIST>(
41    resolveType);
42
43  std::string outFieldName = this->GetOutputFieldName();
44  if (outFieldName == "")
45  {
46    outFieldName = inField.GetName() + "_center";
47  }
48
49  return this->CreateResultFieldCell(inDataSet, outFieldName, outUnknownArray);
50}
51
52} // namespace field_conversion
53} // namespace filter
54} // namespace viskores

4.4.3. Data Set Filters

Sometimes, a filter will generate a data set with a new cell set based off the cells of an input data set. For example, a data set can be significantly altered by adding, removing, or replacing cells.

As with any filter, data set filters can be implemented in classes that derive the viskores::filter::Filter base class and implement its viskores::filter::Filter::DoExecute() method.

In this section we provide an example implementation of a data set filter that wraps the functionality of extracting the edges from a data set as line elements. Many variations of implementing this functionality are given in Chapter~ref{chap:GeneratingCellSets}. Suffice it to say that a pair of worklets will be used to create a new viskores::cont::CellSet, and this viskores::cont::CellSet will be used to create the result viskores::cont::DataSet. Details on how the worklets work are given in Section ref{sec:GeneratingCellSets:SingleType}.

Because the operation of this edge extraction depends only on viskores::cont::CellSet in a provided viskores::cont::DataSet, the filter class is a simple subclass of viskores::filter::Filter.

Example 4.59 Header declaration for a data set filter.
 1namespace viskores
 2{
 3namespace filter
 4{
 5namespace entity_extraction
 6{
 7
 8class VISKORES_FILTER_ENTITY_EXTRACTION_EXPORT ExtractEdges
 9  : public viskores::filter::Filter
10{
11public:
12  VISKORES_CONT viskores::cont::DataSet DoExecute(
13    const viskores::cont::DataSet& inData) override;
14};
15
16} // namespace entity_extraction
17} // namespace filter
18} // namespace viskores

The implementation of viskores::filter::Filter::DoExecute() first gets the viskores::cont::CellSet and calls the worklet methods to generate a new viskores::cont::CellSet class. It then uses a form of viskores::filter::Filter::CreateResult() to generate the resulting viskores::cont::DataSet.

Example 4.60 Implementation of the viskores::filter::Filter::DoExecute() method of a data set filter.
 1inline VISKORES_CONT viskores::cont::DataSet ExtractEdges::DoExecute(
 2  const viskores::cont::DataSet& inData)
 3{
 4  auto inCellSet = inData.GetCellSet();
 5
 6  // Count number of edges in each cell.
 7  viskores::cont::ArrayHandle<viskores::IdComponent> edgeCounts;
 8  this->Invoke(viskores::worklet::CountEdgesWorklet{}, inCellSet, edgeCounts);
 9
10  // Build the scatter object (for non 1-to-1 mapping of input to output)
11  viskores::worklet::ScatterCounting scatter(edgeCounts);
12  auto outputToInputCellMap = scatter.GetOutputToInputMap(inCellSet.GetNumberOfCells());
13
14  viskores::cont::ArrayHandle<viskores::Id> connectivityArray;
15  this->Invoke(viskores::worklet::EdgeIndicesWorklet{},
16               scatter,
17               inCellSet,
18               viskores::cont::make_ArrayHandleGroupVec<2>(connectivityArray));
19
20  viskores::cont::CellSetSingleType<> outCellSet;
21  outCellSet.Fill(
22    inCellSet.GetNumberOfPoints(), viskores::CELL_SHAPE_LINE, 2, connectivityArray);
23
24  // This lambda function maps an input field to the output data set. It is
25  // used with the CreateResult method.
26  auto fieldMapper =
27    [&](viskores::cont::DataSet& outData, const viskores::cont::Field& inputField)
28  {
29    if (inputField.IsCellField())
30    {
31      viskores::filter::MapFieldPermutation(inputField, outputToInputCellMap, outData);
32    }
33    else
34    {
35      outData.AddField(inputField); // pass through
36    }
37  };
38
39  return this->CreateResult(inData, outCellSet, fieldMapper);
40}

The form of viskores::filter::Filter::CreateResult() used (Example 4.60, line 39) takes as input a viskores::cont::CellSet to use in the generated data. In forms of viskores::filter::Filter::CreateResult() used in previous examples of this chapter, the cell structure of the output was created from the cell structure of the input. Because these cell structures were the same, coordinate systems and fields needed to be changed. However, because we are providing a new viskores::cont::CellSet, we need to also specify how the coordinate systems and fields change.

The last two arguments to viskores::filter::Filter::CreateResult() are providing this information. The second-to-last argument is a std::vector of the viskores::cont::CoordinateSystem’s to use. Because this filter does not actually change the points in the data set, the viskores::cont::CoordinateSystem’s can just be copied over. The last argument provides a functor that maps a field from the input to the output. The functor takes two arguments: the output viskores::cont::DataSet to modify and the input viskores::cont::Field to map. In this example, the functor is defined as a lambda function (Example 4.60, line 26).

Did You Know?

The field mapper in Example 4.59 uses a helper function named viskores::filter::MapFieldPermutation(). In the case of this example, every cell in the output comes from one cell in the input. For this common case, the values in the field arrays just need to be permuted so that each input value gets to the right output value. viskores::filter::MapFieldPermutation() will do this shuffling for you.

Viskores also comes with a similar helper function viskores::filter::MapFieldMergeAverage() that can be used when each output cell (or point) was constructed from multiple inputs. In this case, viskores::filter::MapFieldMergeAverage() can do a simple average for each output value of all input values that contributed.

bool viskores::filter::MapFieldPermutation(const viskores::cont::Field &inputField, const viskores::cont::ArrayHandle<viskores::Id> &permutation, viskores::cont::Field &outputField, viskores::Float64 invalidValue = viskores::Nan<viskores::Float64>())

Maps a field by permuting it by a given index array.

This method will create a new field containing the data from the provided inputField but reorded by the given permutation index array. The value in the resulting field for index i will be be a value from inputField, but comes from the index that comes from permutation at position i. The result is placed in outputField.

The intention of this method is to implement the mapping of fields from the input to the output in filters (many of which require this permutation of a field), but can be used in other places as well.

outputField is set to have the same metadata as the input. If the metadata needs to change (such as the name or the association) that should be done after the function returns.

This function returns whether the field was successfully permuted. If the returned result is true, then the results in outputField are valid. If it is false, then outputField should not be used.

If an invalid index is given in the permutation array (i.e. less than 0 or greater than the size of the array), then the resulting outputField will be given invalidValue (converted as best as possible to the correct data type).

bool viskores::filter::MapFieldPermutation(const viskores::cont::Field &inputField, const viskores::cont::ArrayHandle<viskores::Id> &permutation, viskores::cont::DataSet &outputData, viskores::Float64 invalidValue = viskores::Nan<viskores::Float64>())

Maps a field by permuting it by a given index array.

This method will create a new field containing the data from the provided inputField but reorded by the given permutation index array. The value in the resulting field for index i will be be a value from inputField, but comes from the index that comes from permutation at position i.

The intention of this method is to implement the MapFieldOntoOutput methods in filters (many of which require this permutation of a field), but can be used in other places as well. The resulting field is put in the given DataSet.

The returned Field has the same metadata as the input. If the metadata needs to change (such as the name or the association), then a different form of MapFieldPermutation should be used.

This function returns whether the field was successfully permuted. If the returned result is true, then outputData has the permuted field. If it is false, then the field is not placed in outputData.

If an invalid index is given in the permutation array (i.e. less than 0 or greater than the size of the array), then the resulting outputField will be given invalidValue (converted as best as possible to the correct data type).

bool viskores::filter::MapFieldMergeAverage(const viskores::cont::Field &inputField, const viskores::worklet::internal::KeysBase &keys, viskores::cont::Field &outputField)

Maps a field by merging entries based on a keys object.

This method will create a new field containing the data from the provided inputField but but with groups of entities merged together. The input keys object encapsulates which elements should be merged together. A group of elements merged together will be averaged. The result is placed in outputField.

The intention of this method is to implement the MapFieldOntoOutput methods in filters (many of which require this merge of a field), but can be used in other places as well.

outputField is set to have the same metadata as the input. If the metadata needs to change (such as the name or the association) that should be done after the function returns.

This function returns whether the field was successfully merged. If the returned result is true, then the results in outputField are valid. If it is false, then outputField should not be used.

bool viskores::filter::MapFieldMergeAverage(const viskores::cont::Field &inputField, const viskores::worklet::internal::KeysBase &keys, viskores::cont::DataSet &outputData)

Maps a field by merging entries based on a keys object.

This method will create a new field containing the data from the provided inputField but but with groups of entities merged together. The input keys object encapsulates which elements should be merged together. A group of elements merged together will be averaged.

The intention of this method is to implement the MapFieldOntoOutput methods in filters (many of which require this merge of a field), but can be used in other places as well. The resulting field is put in the given DataSet.

The returned Field has the same metadata as the input. If the metadata needs to change (such as the name or the association), then a different form of MapFieldMergeAverage should be used.

This function returns whether the field was successfully merged. If the returned result is true, then outputData has the merged field. If it is false, then the field is not placed in outputData.

Did You Know?

Although not the case in this example, sometimes a filter creating a new cell set changes the points of the cells. As long as the field mapper you provide to viskores::filter::Filter::CreateResult() properly converts points from the input to the output, all fields and coordinate systems will be automatically filled in the output. Sometimes when creating this new cell set you also create new point coordinates for it. This might be because the point coordinates are necessary for the computation or might be due to a faster way of computing the point coordinates. In either case, if the filter already has point coordinates computed, it can use viskores::filter::Filter::CreateResultCoordinateSystem() to use the precomputed point coordinates.

4.4.4. Data Set with Field Filters

Sometimes, a filter will generate a data set with a new cell set based off the cells of an input data set along with the data in at least one field. For example, a field might determine how each cell is culled, clipped, or sliced.

In this section we provide an example implementation of a data set with field filter that blanks the cells in a data set based on a field that acts as a mask (or stencil). Any cell associated with a mask value of zero will be removed. For simplicity of this example, we will use the viskores::filter::entity_extraction::Threshold filter internally for the implementation.

Example 4.61 Header declaration for a data set with field filter.
 1namespace viskores
 2{
 3namespace filter
 4{
 5namespace entity_extraction
 6{
 7
 8class VISKORES_FILTER_ENTITY_EXTRACTION_EXPORT BlankCells
 9  : public viskores::filter::Filter
10{
11public:
12  VISKORES_CONT viskores::cont::DataSet DoExecute(
13    const viskores::cont::DataSet& inDataSet) override;
14};
15
16
17} // namespace entity_extraction
18} // namespace filter
19} // namespace viskores

The implementation of viskores::filter::Filter::DoExecute() first derives an array that contains a flag whether the input array value is zero or non-zero. This is simply to guarantee the range for the threshold filter. After that a threshold filter is set up and run to generate the result.

Example 4.62 Implementation of the viskores::filter::Filter::DoExecute() method of a data set with field filter.
 1VISKORES_CONT viskores::cont::DataSet BlankCells::DoExecute(
 2  const viskores::cont::DataSet& inData)
 3{
 4  viskores::cont::Field inField = this->GetFieldFromDataSet(inData);
 5  if (!inField.IsCellField())
 6  {
 7    throw viskores::cont::ErrorBadValue("Blanking field must be a cell field.");
 8  }
 9
10  // Set up this array to have a 0 for any cell to be removed and
11  // a 1 for any cell to keep.
12  viskores::cont::ArrayHandle<viskores::FloatDefault> blankingArray;
13
14  auto resolveType = [&](const auto& inFieldArray)
15  {
16    auto transformArray = viskores::cont::make_ArrayHandleTransform(
17      inFieldArray, viskores::NotZeroInitialized{});
18    viskores::cont::ArrayCopyDevice(transformArray, blankingArray);
19  };
20
21  this->CastAndCallScalarField(inField, resolveType);
22
23  // Make a temporary DataSet (shallow copy of the input) to pass blankingArray
24  // to threshold.
25  viskores::cont::DataSet tempData = inData;
26  tempData.AddCellField("viskores-blanking-array", blankingArray);
27
28  // Just use the Threshold filter to implement the actual cell removal.
29  viskores::filter::entity_extraction::Threshold thresholdFilter;
30  thresholdFilter.SetLowerThreshold(0.5);
31  thresholdFilter.SetUpperThreshold(2.0);
32  thresholdFilter.SetActiveField("viskores-blanking-array",
33                                 viskores::cont::Field::Association::Cells);
34
35  // Make sure threshold filter passes all the fields requested, but not the
36  // blanking array.
37  thresholdFilter.SetFieldsToPass(this->GetFieldsToPass());
38  thresholdFilter.SetFieldsToPass("viskores-blanking-array",
39                                  viskores::cont::Field::Association::Cells,
40                                  viskores::filter::FieldSelection::Mode::Exclude);
41
42  // Use the threshold filter to generate the actual output.
43  return thresholdFilter.Execute(tempData);
44}