vtk-m/vtkm/filter/Threshold.hxx

183 lines
6.4 KiB
C++
Raw Normal View History

2016-01-19 14:59:31 +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.
//
// 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>
2016-01-19 14:59:31 +00:00
#include <vtkm/cont/ArrayHandlePermutation.h>
#include <vtkm/cont/CellSetPermutation.h>
2017-05-18 14:51:24 +00:00
#include <vtkm/cont/DynamicCellSet.h>
2016-01-19 14:59:31 +00:00
#include <vtkm/worklet/DispatcherMapTopology.h>
namespace
{
class ThresholdRange
2016-01-19 14:59:31 +00:00
{
public:
VTKM_CONT
2017-05-18 14:29:41 +00:00
ThresholdRange(const vtkm::Float64& lower, const vtkm::Float64& upper)
: Lower(lower)
, Upper(upper)
2016-01-19 14:59:31 +00:00
{
2017-05-18 14:29:41 +00:00
}
template <typename T>
VTKM_EXEC bool operator()(const T& value) const
{
return value >= static_cast<T>(this->Lower) && value <= static_cast<T>(this->Upper);
2016-01-19 14:59:31 +00:00
}
private:
vtkm::Float64 Lower;
vtkm::Float64 Upper;
2016-01-19 14:59:31 +00:00
};
class AddPermutationCellSet
{
vtkm::cont::DataSet* Output;
vtkm::cont::ArrayHandle<vtkm::Id>* ValidIds;
2017-05-18 14:29:41 +00:00
2016-01-19 14:59:31 +00:00
public:
2017-05-18 14:29:41 +00:00
AddPermutationCellSet(vtkm::cont::DataSet& data, vtkm::cont::ArrayHandle<vtkm::Id>& validIds)
: Output(&data)
, ValidIds(&validIds)
{
}
template <typename CellSetType>
void operator()(const CellSetType& cellset) const
2016-01-19 14:59:31 +00:00
{
typedef vtkm::cont::CellSetPermutation<CellSetType> PermutationCellSetType;
2016-01-19 14:59:31 +00:00
2017-05-18 14:29:41 +00:00
PermutationCellSetType permCellSet(*this->ValidIds, cellset, cellset.GetName());
2016-01-19 14:59:31 +00:00
this->Output->AddCellSet(permCellSet);
}
};
}
2017-05-18 14:29:41 +00:00
namespace vtkm
{
namespace filter
{
2016-01-19 14:59:31 +00:00
//-----------------------------------------------------------------------------
2017-05-18 14:29:41 +00:00
inline VTKM_CONT Threshold::Threshold()
: vtkm::filter::FilterDataSetWithField<Threshold>()
, LowerValue(0)
, UpperValue(0)
, ValidCellIds()
2016-01-19 14:59:31 +00:00
{
}
//-----------------------------------------------------------------------------
2017-05-18 14:29:41 +00:00
template <typename T, typename StorageType, typename DerivedPolicy, typename DeviceAdapter>
inline VTKM_CONT vtkm::filter::ResultDataSet 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, const DeviceAdapter&)
2016-01-19 14:59:31 +00:00
{
//get the cells and coordinates of the dataset
2017-05-18 14:29:41 +00:00
const vtkm::cont::DynamicCellSet& cells = input.GetCellSet(this->GetActiveCellSetIndex());
2016-01-19 14:59:31 +00:00
2017-05-18 14:29:41 +00:00
ThresholdRange predicate(this->GetLowerThreshold(), this->GetUpperThreshold());
2016-01-19 14:59:31 +00:00
vtkm::cont::ArrayHandle<bool> passFlags;
2017-05-18 14:29:41 +00:00
if (fieldMeta.IsPointField())
2016-01-19 14:59:31 +00:00
{
typedef vtkm::worklet::Threshold Worklets;
2017-05-18 14:29:41 +00:00
typedef Worklets::ThresholdByPointField<ThresholdRange> ThresholdWorklet;
2016-01-19 14:59:31 +00:00
ThresholdWorklet worklet(predicate);
vtkm::worklet::DispatcherMapTopology<ThresholdWorklet, DeviceAdapter> dispatcher(worklet);
dispatcher.Invoke(vtkm::filter::ApplyPolicy(cells, policy), field, passFlags);
2016-01-19 14:59:31 +00:00
}
2017-05-18 14:29:41 +00:00
else if (fieldMeta.IsCellField())
2016-01-19 14:59:31 +00:00
{
typedef vtkm::worklet::Threshold Worklets;
2017-05-18 14:29:41 +00:00
typedef Worklets::ThresholdByCellField<ThresholdRange> ThresholdWorklet;
2016-01-19 14:59:31 +00:00
ThresholdWorklet worklet(predicate);
vtkm::worklet::DispatcherMapTopology<ThresholdWorklet, DeviceAdapter> dispatcher(worklet);
dispatcher.Invoke(vtkm::filter::ApplyPolicy(cells, policy), field, passFlags);
2016-01-19 14:59:31 +00:00
}
else
{
//todo: we need to mark this as a failure of input, not a failure
//of the algorithm
return vtkm::filter::ResultDataSet();
2016-01-19 14:59:31 +00:00
}
typedef vtkm::cont::DeviceAdapterAlgorithm<DeviceAdapter> Algorithm;
vtkm::cont::ArrayHandleCounting<vtkm::Id> indices =
2017-05-18 14:29:41 +00:00
vtkm::cont::make_ArrayHandleCounting(vtkm::Id(0), vtkm::Id(1), passFlags.GetNumberOfValues());
Algorithm::CopyIf(indices, passFlags, this->ValidCellIds);
2016-01-19 14:59:31 +00:00
vtkm::cont::DataSet output;
2017-05-18 14:29:41 +00:00
output.AddCoordinateSystem(input.GetCoordinateSystem(this->GetActiveCoordinateSystemIndex()));
2016-01-19 14:59:31 +00:00
//now generate the output cellset. We are going to need to do a cast
//and call to generate the correct form of output.
//todo: see if we can make this into a helper class that other filters
//can use to reduce code duplication, and make it easier to write filters
//that return complex dataset types.
AddPermutationCellSet addCellSet(output, this->ValidCellIds);
2017-05-18 14:29:41 +00:00
vtkm::cont::CastAndCall(vtkm::filter::ApplyPolicy(cells, policy), addCellSet);
2016-01-19 14:59:31 +00:00
//todo: We need to generate a new output policy that replaces
//the original storage tag with a new storage tag where everything is
//wrapped in CellSetPermutation.
return output;
}
//-----------------------------------------------------------------------------
2017-05-18 14:29:41 +00:00
template <typename T, typename StorageType, typename DerivedPolicy, typename DeviceAdapter>
inline VTKM_CONT bool Threshold::DoMapField(vtkm::filter::ResultDataSet& result,
const vtkm::cont::ArrayHandle<T, StorageType>& input,
const vtkm::filter::FieldMetadata& fieldMeta,
const vtkm::filter::PolicyBase<DerivedPolicy>&,
const DeviceAdapter&)
2016-01-19 14:59:31 +00:00
{
2017-05-18 14:29:41 +00:00
if (fieldMeta.IsPointField())
2016-01-19 14:59:31 +00:00
{
//we copy the input handle to the result dataset, reusing the metadata
2017-05-18 14:29:41 +00:00
result.GetDataSet().AddField(fieldMeta.AsField(input));
2016-01-19 14:59:31 +00:00
return true;
}
2017-05-18 14:29:41 +00:00
if (fieldMeta.IsCellField())
2016-01-19 14:59:31 +00:00
{
//todo: We need to generate a new output policy that replaces
//the original storage tag with a new storage tag where everything is
//wrapped in ArrayHandlePermutation.
2017-05-18 14:29:41 +00:00
typedef vtkm::cont::ArrayHandlePermutation<vtkm::cont::ArrayHandle<vtkm::Id>,
vtkm::cont::ArrayHandle<T, StorageType>>
PermutationType;
2016-01-19 14:59:31 +00:00
PermutationType permutation =
2017-05-18 14:29:41 +00:00
vtkm::cont::make_ArrayHandlePermutation(this->ValidCellIds, input);
2016-01-19 14:59:31 +00:00
2017-05-18 14:29:41 +00:00
result.GetDataSet().AddField(fieldMeta.AsField(permutation));
2016-01-19 14:59:31 +00:00
return true;
}
return false;
}
}
}