vtk-m/vtkm/filter/Threshold.hxx
Kenneth Moreland bd742fc6d4 Allow Threshold::Run to work on dynamic cell sets
Added an overloaded version of vtkm::worklet::Threshold::Run that takes
a dynamic cell set instead of a concrete cell set type. This is
implemented by basically moving the CastAndCall from the Threshold
filter to the worklet class. This allows other filters (that might have
similar operations with different predicates) to use the same code. (It
also happens to grately simplify an example for the User's Guide I am
working on.)

Once cell sets use virtual methods, we should be able to collapse code
like this quite a bit anyway.
2017-09-13 15:58:25 -06:00

120 lines
4.0 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.
//
// Copyright 2014 Sandia Corporation.
// Copyright 2014 UT-Battelle, LLC.
// Copyright 2014 Los Alamos National Security.
//
// Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
// the U.S. Government retains certain rights in this software.
//
// Under the terms of Contract DE-AC52-06NA25396 with Los Alamos National
// Laboratory (LANL), the U.S. Government retains certain rights in
// this software.
//============================================================================
#include <vtkm/cont/ArrayHandleCounting.h>
#include <vtkm/cont/ArrayHandlePermutation.h>
#include <vtkm/cont/CellSetPermutation.h>
#include <vtkm/cont/DynamicCellSet.h>
#include <vtkm/worklet/DispatcherMapTopology.h>
namespace
{
class ThresholdRange
{
public:
VTKM_CONT
ThresholdRange(const vtkm::Float64& lower, const vtkm::Float64& upper)
: Lower(lower)
, Upper(upper)
{
}
template <typename T>
VTKM_EXEC bool operator()(const T& value) const
{
return value >= static_cast<T>(this->Lower) && value <= static_cast<T>(this->Upper);
}
private:
vtkm::Float64 Lower;
vtkm::Float64 Upper;
};
} // end anon namespace
namespace vtkm
{
namespace filter
{
//-----------------------------------------------------------------------------
inline VTKM_CONT Threshold::Threshold()
: vtkm::filter::FilterDataSetWithField<Threshold>()
, LowerValue(0)
, UpperValue(0)
{
}
//-----------------------------------------------------------------------------
template <typename T, typename StorageType, typename DerivedPolicy, typename DeviceAdapter>
inline VTKM_CONT vtkm::filter::Result Threshold::DoExecute(
const vtkm::cont::DataSet& input,
const vtkm::cont::ArrayHandle<T, StorageType>& field,
const vtkm::filter::FieldMetadata& fieldMeta,
const vtkm::filter::PolicyBase<DerivedPolicy>& policy,
DeviceAdapter)
{
//get the cells and coordinates of the dataset
const vtkm::cont::DynamicCellSet& cells = input.GetCellSet(this->GetActiveCellSetIndex());
ThresholdRange predicate(this->GetLowerThreshold(), this->GetUpperThreshold());
vtkm::cont::DynamicCellSet cellOut = this->Worklet.Run(vtkm::filter::ApplyPolicy(cells, policy),
field,
fieldMeta.GetAssociation(),
predicate,
DeviceAdapter());
vtkm::cont::DataSet output;
output.AddCellSet(cellOut);
output.AddCoordinateSystem(input.GetCoordinateSystem(this->GetActiveCoordinateSystemIndex()));
return output;
}
//-----------------------------------------------------------------------------
template <typename T, typename StorageType, typename DerivedPolicy, typename DeviceAdapter>
inline VTKM_CONT bool Threshold::DoMapField(vtkm::filter::Result& result,
const vtkm::cont::ArrayHandle<T, StorageType>& input,
const vtkm::filter::FieldMetadata& fieldMeta,
const vtkm::filter::PolicyBase<DerivedPolicy>&,
DeviceAdapter device)
{
if (fieldMeta.IsPointField())
{
//we copy the input handle to the result dataset, reusing the metadata
result.GetDataSet().AddField(fieldMeta.AsField(input));
return true;
}
else if (fieldMeta.IsCellField())
{
vtkm::cont::ArrayHandle<T> out = this->Worklet.ProcessCellField(input, device);
result.GetDataSet().AddField(fieldMeta.AsField(out));
return true;
}
else
{
return false;
}
}
}
}