FilterDataSetWithField.hxx 5.66 KB
Newer Older
1 2 3 4 5 6 7 8
//============================================================================
//  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.
//
9
//  Copyright 2014 National Technology & Engineering Solutions of Sandia, LLC (NTESS).
10 11 12
//  Copyright 2014 UT-Battelle, LLC.
//  Copyright 2014 Los Alamos National Security.
//
13
//  Under the terms of Contract DE-NA0003525 with NTESS,
14 15 16 17 18 19 20 21 22
//  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/filter/FieldMetadata.h>
#include <vtkm/filter/FilterTraits.h>
23
#include <vtkm/filter/PolicyDefault.h>
24 25 26 27 28

#include <vtkm/filter/internal/ResolveFieldTypeAndExecute.h>
#include <vtkm/filter/internal/ResolveFieldTypeAndMap.h>

#include <vtkm/cont/Error.h>
29
#include <vtkm/cont/ErrorBadAllocation.h>
30 31 32 33 34
#include <vtkm/cont/ErrorExecution.h>

#include <vtkm/cont/cuda/DeviceAdapterCuda.h>
#include <vtkm/cont/tbb/DeviceAdapterTBB.h>

35 36 37 38
namespace vtkm
{
namespace filter
{
39 40

//----------------------------------------------------------------------------
Utkarsh Ayachit's avatar
Utkarsh Ayachit committed
41
template <typename Derived>
42 43 44 45
inline VTKM_CONT FilterDataSetWithField<Derived>::FilterDataSetWithField()
  : OutputFieldName()
  , CellSetIndex(0)
  , CoordinateSystemIndex(0)
Utkarsh Ayachit's avatar
Utkarsh Ayachit committed
46
  , ActiveFieldName()
47
  , ActiveFieldAssociation(vtkm::cont::Field::Association::ANY)
Utkarsh Ayachit's avatar
Utkarsh Ayachit committed
48
  , UseCoordinateSystemAsField(false)
49 50 51 52
{
}

//----------------------------------------------------------------------------
53
template <typename Derived>
Utkarsh Ayachit's avatar
Utkarsh Ayachit committed
54
inline VTKM_CONT FilterDataSetWithField<Derived>::~FilterDataSetWithField()
55 56 57
{
}

dongliangchu's avatar
dongliangchu committed
58 59 60
//-----------------------------------------------------------------------------
template <typename Derived>
template <typename DerivedPolicy>
61
inline VTKM_CONT vtkm::cont::DataSet FilterDataSetWithField<Derived>::PrepareForExecution(
Utkarsh Ayachit's avatar
Utkarsh Ayachit committed
62
  const vtkm::cont::DataSet& input,
dongliangchu's avatar
dongliangchu committed
63 64
  const vtkm::filter::PolicyBase<DerivedPolicy>& policy)
{
Utkarsh Ayachit's avatar
Utkarsh Ayachit committed
65
  if (this->UseCoordinateSystemAsField)
dongliangchu's avatar
dongliangchu committed
66
  {
Utkarsh Ayachit's avatar
Utkarsh Ayachit committed
67 68 69 70 71 72 73 74 75
    // we need to state that the field is actually a coordinate system, so that
    // the filter uses the proper policy to convert the types.
    return this->PrepareForExecution(
      input, input.GetCoordinateSystem(this->GetActiveCellSetIndex()), policy);
  }
  else
  {
    return this->PrepareForExecution(
      input, input.GetField(this->GetActiveFieldName(), this->GetActiveFieldAssociation()), policy);
dongliangchu's avatar
dongliangchu committed
76
  }
77 78 79
}

//-----------------------------------------------------------------------------
80 81
template <typename Derived>
template <typename DerivedPolicy>
82
inline VTKM_CONT vtkm::cont::DataSet FilterDataSetWithField<Derived>::PrepareForExecution(
83 84
  const vtkm::cont::DataSet& input,
  const vtkm::cont::Field& field,
85
  const vtkm::filter::PolicyBase<DerivedPolicy>& policy)
86
{
87
  vtkm::filter::FieldMetadata metaData(field);
88
  vtkm::cont::DataSet result;
89

90 91
  using FunctorType =
    internal::ResolveFieldTypeAndExecute<Derived, DerivedPolicy, vtkm::cont::DataSet>;
92
  FunctorType functor(static_cast<Derived*>(this), input, metaData, policy, result);
93
  using Traits = vtkm::filter::FilterTraits<Derived>;
94
  vtkm::cont::CastAndCall(
Utkarsh Ayachit's avatar
Utkarsh Ayachit committed
95
    vtkm::filter::ApplyPolicy(field, policy, Traits()), functor, this->GetRuntimeDeviceTracker());
96 97 98 99
  return result;
}

//-----------------------------------------------------------------------------
100 101
template <typename Derived>
template <typename DerivedPolicy>
102
inline VTKM_CONT vtkm::cont::DataSet FilterDataSetWithField<Derived>::PrepareForExecution(
103 104
  const vtkm::cont::DataSet& input,
  const vtkm::cont::CoordinateSystem& field,
105
  const vtkm::filter::PolicyBase<DerivedPolicy>& policy)
106 107 108
{
  //We have a special signature just for CoordinateSystem, so that we can ask
  //the policy for the storage types and value types just for coordinate systems
109
  vtkm::filter::FieldMetadata metaData(field);
110 111

  //determine the field type first
112 113 114
  vtkm::cont::DataSet result;
  using FunctorType =
    internal::ResolveFieldTypeAndExecute<Derived, DerivedPolicy, vtkm::cont::DataSet>;
115
  FunctorType functor(static_cast<Derived*>(this), input, metaData, policy, result);
116

117
  using Traits = vtkm::filter::FilterTraits<Derived>;
Utkarsh Ayachit's avatar
Utkarsh Ayachit committed
118 119 120 121 122
  constexpr bool supportsVec3 = vtkm::ListContains<typename Traits::InputFieldTypeList,
                                                   vtkm::Vec<vtkm::FloatDefault, 3>>::value;
  using supportsCoordinateSystem = std::integral_constant<bool, supportsVec3>;
  vtkm::cont::ConditionalCastAndCall(
    supportsCoordinateSystem(), field, functor, this->GetRuntimeDeviceTracker());
123 124 125 126 127

  return result;
}

//-----------------------------------------------------------------------------
128 129 130
template <typename Derived>
template <typename DerivedPolicy>
inline VTKM_CONT bool FilterDataSetWithField<Derived>::MapFieldOntoOutput(
131
  vtkm::cont::DataSet& result,
132
  const vtkm::cont::Field& field,
133
  const vtkm::filter::PolicyBase<DerivedPolicy>& policy)
134 135 136
{
  bool valid = false;

137 138 139 140 141 142
  vtkm::filter::FieldMetadata metaData(field);
  using FunctorType = internal::ResolveFieldTypeAndMap<Derived, DerivedPolicy>;
  FunctorType functor(static_cast<Derived*>(this), result, metaData, policy, valid);

  vtkm::cont::CastAndCall(
    vtkm::filter::ApplyPolicy(field, policy), functor, this->GetRuntimeDeviceTracker());
143 144 145

  //the bool valid will be modified by the map algorithm to hold if the
  //mapping occurred or not. If the mapping was good a new field has been
146
  //added to the result that was passed in.
147 148 149 150
  return valid;
}
}
}