vtk-m2/vtkm/filter/NewFilterField.h

207 lines
6.6 KiB
C
Raw Normal View History

2021-12-13 23:20:39 +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.
//============================================================================
#ifndef vtk_m_filter_NewFilterField_h
#define vtk_m_filter_NewFilterField_h
#include <vtkm/filter/NewFilter.h>
namespace vtkm
{
namespace filter
{
2022-01-05 20:20:19 +00:00
class VTKM_FILTER_CORE_EXPORT NewFilterField : public vtkm::filter::NewFilter
2021-12-13 23:20:39 +00:00
{
public:
NewFilterField() { this->SetActiveCoordinateSystem(0); }
2021-12-13 23:20:39 +00:00
VTKM_CONT
void SetOutputFieldName(const std::string& name) { this->OutputFieldName = name; }
VTKM_CONT
const std::string& GetOutputFieldName() const { return this->OutputFieldName; }
//@{
/// Choose the field to operate on. Note, if
/// `this->UseCoordinateSystemAsField` is true, then the active field is not used.
VTKM_CONT
void SetActiveField(
const std::string& name,
vtkm::cont::Field::Association association = vtkm::cont::Field::Association::ANY)
{
2021-12-14 20:43:42 +00:00
this->SetActiveField(0, name, association);
2021-12-13 23:20:39 +00:00
}
2021-12-14 20:43:42 +00:00
void SetActiveField(
vtkm::IdComponent index,
const std::string& name,
vtkm::cont::Field::Association association = vtkm::cont::Field::Association::ANY)
{
auto index_st = static_cast<std::size_t>(index);
ResizeIfNeeded(index_st);
this->ActiveFieldNames[index_st] = name;
this->ActiveFieldAssociation[index_st] = association;
}
VTKM_CONT const std::string& GetActiveFieldName(vtkm::IdComponent index = 0) const
{
VTKM_ASSERT((index >= 0) &&
(index < static_cast<vtkm::IdComponent>(this->ActiveFieldNames.size())));
return this->ActiveFieldNames[index];
}
VTKM_CONT vtkm::cont::Field::Association GetActiveFieldAssociation(
vtkm::IdComponent index = 0) const
2021-12-13 23:20:39 +00:00
{
2021-12-14 20:43:42 +00:00
return this->ActiveFieldAssociation[index];
2021-12-13 23:20:39 +00:00
}
//@}
//@{
/// Select the coordinate system coord_idx to make active to use when processing the input
/// DataSet. This is used primarily by the Filter to select the coordinate system
/// to use as a field when \c UseCoordinateSystemAsField is true.
VTKM_CONT
void SetActiveCoordinateSystem(vtkm::Id coord_idx)
{
this->SetActiveCoordinateSystem(0, coord_idx);
}
VTKM_CONT
void SetActiveCoordinateSystem(vtkm::IdComponent index, vtkm::Id coord_idx)
{
auto index_st = static_cast<std::size_t>(index);
ResizeIfNeeded(index_st);
this->ActiveCoordinateSystemIndices[index_st] = coord_idx;
}
VTKM_CONT
vtkm::Id GetActiveCoordinateSystemIndex() const
{
return this->GetActiveCoordinateSystemIndex(0);
}
VTKM_CONT
vtkm::Id GetActiveCoordinateSystemIndex(vtkm::IdComponent index) const
{
auto index_st = static_cast<std::size_t>(index);
return this->ActiveCoordinateSystemIndices[index_st];
}
//@}
2021-12-13 23:20:39 +00:00
//@{
/// To simply use the active coordinate system as the field to operate on, set
/// UseCoordinateSystemAsField to true.
VTKM_CONT
2021-12-14 20:43:42 +00:00
void SetUseCoordinateSystemAsField(bool val) { SetUseCoordinateSystemAsField(0, val); }
2021-12-13 23:20:39 +00:00
VTKM_CONT
2021-12-14 20:43:42 +00:00
void SetUseCoordinateSystemAsField(vtkm::IdComponent index, bool val)
{
auto index_st = static_cast<std::size_t>(index);
2021-12-17 16:49:04 +00:00
this->ResizeIfNeeded(index_st);
2021-12-14 20:43:42 +00:00
this->UseCoordinateSystemAsField[index] = val;
}
2021-12-13 23:20:39 +00:00
2021-12-14 20:43:42 +00:00
VTKM_CONT
bool GetUseCoordinateSystemAsField(vtkm::IdComponent index = 0) const
{
VTKM_ASSERT((index >= 0) &&
(index < static_cast<vtkm::IdComponent>(this->ActiveFieldNames.size())));
return this->UseCoordinateSystemAsField[index];
}
//@}
2021-12-15 23:29:38 +00:00
protected:
2021-12-13 23:20:39 +00:00
VTKM_CONT
const vtkm::cont::Field& GetFieldFromDataSet(const vtkm::cont::DataSet& input) const
{
2021-12-17 16:49:04 +00:00
return this->GetFieldFromDataSet(0, input);
2021-12-14 20:43:42 +00:00
}
VTKM_CONT
const vtkm::cont::Field& GetFieldFromDataSet(vtkm::IdComponent index,
const vtkm::cont::DataSet& input) const
{
if (this->UseCoordinateSystemAsField[index])
2021-12-13 23:20:39 +00:00
{
return input.GetCoordinateSystem(this->GetActiveCoordinateSystemIndex(index));
2021-12-13 23:20:39 +00:00
}
else
{
2021-12-14 20:43:42 +00:00
return input.GetField(this->GetActiveFieldName(index),
this->GetActiveFieldAssociation(index));
2021-12-13 23:20:39 +00:00
}
}
template <typename Functor, typename... Args>
VTKM_CONT void CastAndCallScalarField(const vtkm::cont::UnknownArrayHandle& fieldArray,
Functor&& functor,
Args&&... args) const
{
fieldArray
.CastAndCallForTypesWithFloatFallback<vtkm::TypeListFieldScalar, VTKM_DEFAULT_STORAGE_LIST>(
std::forward<Functor>(functor), std::forward<Args>(args)...);
}
template <typename Functor, typename... Args>
VTKM_CONT void CastAndCallScalarField(const vtkm::cont::Field& field,
Functor&& functor,
Args&&... args) const
{
this->CastAndCallScalarField(
field.GetData(), std::forward<Functor>(functor), std::forward<Args>(args)...);
}
private:
template <vtkm::IdComponent VecSize>
struct ScalarToVec
{
template <typename T>
using type = vtkm::Vec<T, VecSize>;
};
protected:
template <vtkm::IdComponent VecSize, typename Functor, typename... Args>
VTKM_CONT void CastAndCallVecField(const vtkm::cont::UnknownArrayHandle& fieldArray,
Functor&& functor,
Args&&... args) const
{
using VecList =
vtkm::ListTransform<vtkm::TypeListFieldScalar, ScalarToVec<VecSize>::template type>;
fieldArray.CastAndCallForTypesWithFloatFallback<VecList, VTKM_DEFAULT_STORAGE_LIST>(
std::forward<Functor>(functor), std::forward<Args>(args)...);
}
template <vtkm::IdComponent VecSize, typename Functor, typename... Args>
VTKM_CONT void CastAndCallVecField(const vtkm::cont::Field& field,
Functor&& functor,
Args&&... args) const
{
this->CastAndCallVecField<VecSize>(
field.GetData(), std::forward<Functor>(functor), std::forward<Args>(args)...);
}
2021-12-13 23:20:39 +00:00
private:
void ResizeIfNeeded(size_t index_st);
2021-12-14 20:43:42 +00:00
2021-12-13 23:20:39 +00:00
std::string OutputFieldName;
2021-12-14 20:43:42 +00:00
std::vector<std::string> ActiveFieldNames;
std::vector<vtkm::cont::Field::Association> ActiveFieldAssociation;
std::vector<bool> UseCoordinateSystemAsField;
std::vector<vtkm::Id> ActiveCoordinateSystemIndices;
2021-12-13 23:20:39 +00:00
};
} // namespace filter
} // namespace vtkm
#endif // vtk_m_filter_NewFilterField_h