VTK-m  2.0
worklet/SurfaceNormals.h
Go to the documentation of this file.
1 //============================================================================
2 // Copyright (c) Kitware, Inc.
3 // All rights reserved.
4 // See LICENSE.txt for details.
5 //
6 // This software is distributed WITHOUT ANY WARRANTY; without even
7 // the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
8 // PURPOSE. See the above copyright notice for more information.
9 //============================================================================
10 #ifndef vtk_m_worklet_SurfaceNormals_h
11 #define vtk_m_worklet_SurfaceNormals_h
12 
13 
16 
17 #include <vtkm/CellTraits.h>
18 #include <vtkm/TypeTraits.h>
19 #include <vtkm/VectorAnalysis.h>
21 
22 namespace vtkm
23 {
24 namespace worklet
25 {
26 
27 namespace detail
28 {
29 struct PassThrough
30 {
31  template <typename T>
32  VTKM_EXEC vtkm::Vec<T, 3> operator()(const vtkm::Vec<T, 3>& in) const
33  {
34  return in;
35  }
36 };
37 
38 struct Normal
39 {
40  template <typename T>
41  VTKM_EXEC vtkm::Vec<T, 3> operator()(const vtkm::Vec<T, 3>& in) const
42  {
43  return vtkm::Normal(in);
44  }
45 };
46 } // detail
47 
49 {
50 public:
51  template <typename NormalFnctr = detail::Normal>
53  {
54  public:
55  using ControlSignature = void(CellSetIn cellset, FieldInPoint points, FieldOutCell normals);
56  using ExecutionSignature = void(CellShape, _2, _3);
57 
58  using InputDomain = _1;
59 
60  template <typename CellShapeTag, typename PointsVecType, typename T>
61  VTKM_EXEC void operator()(CellShapeTag,
62  const PointsVecType& points,
63  vtkm::Vec<T, 3>& normal) const
64  {
65  using CTraits = vtkm::CellTraits<CellShapeTag>;
66  const auto tag = typename CTraits::TopologicalDimensionsTag();
67  this->Compute(tag, points, normal);
68  }
69 
70  template <vtkm::IdComponent Dim, typename PointsVecType, typename T>
72  const PointsVecType&,
73  vtkm::Vec<T, 3>& normal) const
74  {
75  normal = vtkm::TypeTraits<vtkm::Vec<T, 3>>::ZeroInitialization();
76  }
77 
78  template <typename PointsVecType, typename T>
80  const PointsVecType& points,
81  vtkm::Vec<T, 3>& normal) const
82  {
83  normal = this->Normal(vtkm::Cross(points[2] - points[1], points[0] - points[1]));
84  }
85 
86 
87 
88  template <typename PointsVecType, typename T>
90  const PointsVecType& points,
91  vtkm::Vec<T, 3>& normal) const
92  {
93  switch (shape.Id)
94  {
95  vtkmGenericCellShapeMacro(this->operator()(CellShapeTag(), points, normal));
96  default:
97  this->RaiseError("unknown cell type");
98  break;
99  }
100  }
101 
102  private:
103  NormalFnctr Normal;
104  };
105 
107  : Normalize(true)
108  {
109  }
110 
112  void SetNormalize(bool value) { this->Normalize = value; }
113  bool GetNormalize() const { return this->Normalize; }
114 
115  template <typename CellSetType, typename PointsType, typename NormalCompType>
116  void Run(const CellSetType& cellset,
117  const PointsType& points,
119  {
120  if (this->Normalize)
121  {
122  vtkm::worklet::DispatcherMapTopology<Worklet<>>().Invoke(cellset, points, normals);
123  }
124  else
125  {
127  cellset, points, normals);
128  }
129  }
130 
131 private:
132  bool Normalize;
133 };
134 
136 {
137 public:
139  {
140  public:
141  using ControlSignature = void(CellSetIn cellset,
142  FieldInCell faceNormals,
143  FieldOutPoint pointNormals);
144  using ExecutionSignature = void(CellCount, _2, _3);
145 
146  using InputDomain = _1;
147 
148  template <typename FaceNormalsVecType, typename T>
150  const FaceNormalsVecType& faceNormals,
151  vtkm::Vec<T, 3>& pointNormal) const
152  {
153  if (numCells == 0)
154  {
155  pointNormal = vtkm::TypeTraits<vtkm::Vec<T, 3>>::ZeroInitialization();
156  }
157  else
158  {
159  auto result = faceNormals[0];
160  for (vtkm::IdComponent i = 1; i < numCells; ++i)
161  {
162  result += faceNormals[i];
163  }
164  pointNormal = vtkm::Normal(result);
165  }
166  }
167  };
168 
169  template <typename CellSetType, typename NormalCompType, typename FaceNormalStorageType>
170  void Run(
171  const CellSetType& cellset,
172  const vtkm::cont::ArrayHandle<vtkm::Vec<NormalCompType, 3>, FaceNormalStorageType>& faceNormals,
174  {
175  vtkm::worklet::DispatcherMapTopology<Worklet>().Invoke(cellset, faceNormals, pointNormals);
176  }
177 };
178 }
179 } // vtkm::worklet
180 
181 #endif // vtk_m_worklet_SurfaceNormals_h
vtkm::worklet::FacetedSurfaceNormals::Worklet::Normal
NormalFnctr Normal
Definition: worklet/SurfaceNormals.h:103
vtkm::cont::ArrayHandle
Manages an array-worth of data.
Definition: ArrayHandle.h:283
vtkm::worklet::FacetedSurfaceNormals::Worklet
Definition: worklet/SurfaceNormals.h:52
VTKM_EXEC
#define VTKM_EXEC
Definition: ExportMacros.h:51
vtkm
Groups connected points that have the same field value.
Definition: Atomic.h:19
vtkm::TypeTraits
The TypeTraits class provides helpful compile-time information about the basic types used in VTKm (an...
Definition: TypeTraits.h:61
vtkm::worklet::SmoothSurfaceNormals
Definition: worklet/SurfaceNormals.h:135
vtkm::worklet::WorkletVisitPointsWithCells::FieldInCell
FieldInIncident FieldInCell
Definition: WorkletMapTopology.h:278
vtkm::worklet::FacetedSurfaceNormals::Normalize
bool Normalize
Definition: worklet/SurfaceNormals.h:132
vtkm::IdComponent
vtkm::Int32 IdComponent
Represents a component ID (index of component in a vector).
Definition: Types.h:168
vtkm::worklet::FacetedSurfaceNormals::Worklet::ExecutionSignature
void(CellShape, _2, _3) ExecutionSignature
Definition: worklet/SurfaceNormals.h:56
vtkm::worklet::FacetedSurfaceNormals::GetNormalize
bool GetNormalize() const
Definition: worklet/SurfaceNormals.h:113
vtkm::worklet::SmoothSurfaceNormals::Worklet
Definition: worklet/SurfaceNormals.h:138
vtkm::worklet::SmoothSurfaceNormals::Worklet::InputDomain
_1 InputDomain
Definition: worklet/SurfaceNormals.h:146
vtkm::worklet::SmoothSurfaceNormals::Worklet::ExecutionSignature
void(CellCount, _2, _3) ExecutionSignature
Definition: worklet/SurfaceNormals.h:144
vtkm::worklet::FacetedSurfaceNormals::Worklet::Compute
VTKM_EXEC void Compute(vtkm::CellTopologicalDimensionsTag< Dim >, const PointsVecType &, vtkm::Vec< T, 3 > &normal) const
Definition: worklet/SurfaceNormals.h:71
vtkm::Normal
VTKM_EXEC_CONT T Normal(const T &x)
Returns a normalized version of the given vector.
Definition: VectorAnalysis.h:157
VectorAnalysis.h
vtkmGenericCellShapeMacro
#define vtkmGenericCellShapeMacro(call)
A macro used in a switch statement to determine cell shape.
Definition: CellShape.h:230
TypeTraits.h
vtkm::worklet::SmoothSurfaceNormals::Worklet::ControlSignature
void(CellSetIn cellset, FieldInCell faceNormals, FieldOutPoint pointNormals) ControlSignature
Definition: worklet/SurfaceNormals.h:143
UncertainArrayHandle.h
vtkm::worklet::FacetedSurfaceNormals::SetNormalize
void SetNormalize(bool value)
Set/Get if the results should be normalized.
Definition: worklet/SurfaceNormals.h:112
vtkm::worklet::WorkletVisitPointsWithCells::CellCount
IncidentElementCount CellCount
Definition: WorkletMapTopology.h:286
vtkm::worklet::SmoothSurfaceNormals::Run
void Run(const CellSetType &cellset, const vtkm::cont::ArrayHandle< vtkm::Vec< NormalCompType, 3 >, FaceNormalStorageType > &faceNormals, vtkm::cont::ArrayHandle< vtkm::Vec< NormalCompType, 3 >> &pointNormals)
Definition: worklet/SurfaceNormals.h:170
vtkm::worklet::WorkletVisitPointsWithCells
Base class for worklets that map from Cells to Points.
Definition: WorkletMapTopology.h:274
vtkm::CellTopologicalDimensionsTag
vtkm::CellTraits::TopologyDimensionType is typedef to this with the template parameter set to TOPOLOG...
Definition: CellTraits.h:23
vtkm::worklet::DispatcherMapTopology
Dispatcher for worklets that inherit from WorkletMapTopology.
Definition: DispatcherMapTopology.h:31
vtkm::Cross
VTKM_EXEC_CONT vtkm::Vec< typename detail::FloatingPointReturnType< T >::Type, 3 > Cross(const vtkm::Vec< T, 3 > &x, const vtkm::Vec< T, 3 > &y)
Find the cross product of two vectors.
Definition: VectorAnalysis.h:177
vtkm::worklet::WorkletVisitCellsWithPoints
Base class for worklets that map from Points to Cells.
Definition: WorkletMapTopology.h:255
vtkm::worklet::FacetedSurfaceNormals::Worklet::operator()
VTKM_EXEC void operator()(CellShapeTag, const PointsVecType &points, vtkm::Vec< T, 3 > &normal) const
Definition: worklet/SurfaceNormals.h:61
vtkm::worklet::WorkletVisitCellsWithPoints::FieldInPoint
FieldInIncident FieldInPoint
Definition: WorkletMapTopology.h:259
vtkm::worklet::FacetedSurfaceNormals::Worklet::ControlSignature
void(CellSetIn cellset, FieldInPoint points, FieldOutCell normals) ControlSignature
Definition: worklet/SurfaceNormals.h:55
vtkm::worklet::FacetedSurfaceNormals::Worklet::Compute
VTKM_EXEC void Compute(vtkm::CellTopologicalDimensionsTag< 2 >, const PointsVecType &points, vtkm::Vec< T, 3 > &normal) const
Definition: worklet/SurfaceNormals.h:79
vtkm::worklet::FacetedSurfaceNormals::Worklet::InputDomain
_1 InputDomain
Definition: worklet/SurfaceNormals.h:58
vtkm::Vec< T, 3 >
Definition: Types.h:975
vtkm::Vec
A short fixed-length array.
Definition: Types.h:767
vtkm::CellShapeTagGeneric::Id
vtkm::UInt8 Id
Definition: CellShape.h:160
vtkm::worklet::FacetedSurfaceNormals::Run
void Run(const CellSetType &cellset, const PointsType &points, vtkm::cont::ArrayHandle< vtkm::Vec< NormalCompType, 3 >> &normals)
Definition: worklet/SurfaceNormals.h:116
vtkm::worklet::WorkletVisitPointsWithCells::FieldOutPoint
FieldOut FieldOutPoint
Definition: WorkletMapTopology.h:282
vtkm::worklet::FacetedSurfaceNormals
Definition: worklet/SurfaceNormals.h:48
vtkm::worklet::Normalize
Definition: Normalize.h:34
vtkm::worklet::FacetedSurfaceNormals::FacetedSurfaceNormals
FacetedSurfaceNormals()
Definition: worklet/SurfaceNormals.h:106
CellTraits.h
vtkm::worklet::FacetedSurfaceNormals::Worklet::operator()
VTKM_EXEC void operator()(vtkm::CellShapeTagGeneric shape, const PointsVecType &points, vtkm::Vec< T, 3 > &normal) const
Definition: worklet/SurfaceNormals.h:89
vtkm::CellShapeTagGeneric
A special cell shape tag that holds a cell shape that is not known at compile time.
Definition: CellShape.h:152
DispatcherMapTopology.h
WorkletMapTopology.h
vtkm::CellTraits
Information about a cell based on its tag.
Definition: CellTraits.h:46
vtkm::exec::FunctorBase::RaiseError
VTKM_EXEC void RaiseError(const char *message) const
Definition: FunctorBase.h:40
vtkm::worklet::WorkletVisitCellsWithPoints::FieldOutCell
FieldOut FieldOutCell
Definition: WorkletMapTopology.h:263
vtkm::worklet::SmoothSurfaceNormals::Worklet::operator()
VTKM_EXEC void operator()(vtkm::IdComponent numCells, const FaceNormalsVecType &faceNormals, vtkm::Vec< T, 3 > &pointNormal) const
Definition: worklet/SurfaceNormals.h:149