VTK-m  2.0
worklet/ImageConnectivity.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 
11 #ifndef vtk_m_worklet_connectivity_ImageConnectivity_h
12 #define vtk_m_worklet_connectivity_ImageConnectivity_h
13 
14 #include <vtkm/cont/Algorithm.h>
15 #include <vtkm/cont/Invoker.h>
20 
23 
24 
25 namespace vtkm
26 {
27 namespace worklet
28 {
29 namespace connectivity
30 {
31 namespace detail
32 {
33 
34 class ImageGraft : public vtkm::worklet::WorkletPointNeighborhood
35 {
36 public:
37  using ControlSignature = void(CellSetIn,
38  FieldInNeighborhood neighborComp,
39  FieldInNeighborhood neighborColor,
40  AtomicArrayInOut compOut);
41 
42  using ExecutionSignature = void(Boundary, _2, _3, _4);
43 
44 
45  // compOut is a "linear" alias of neighborComp such that we can update component labels
46  template <typename Boundary,
47  typename NeighborComp,
48  typename NeighborColor,
49  typename AtomicCompOut>
50  VTKM_EXEC void operator()(Boundary boundary,
51  const NeighborComp& neighborComp,
52  const NeighborColor& neighborColor,
53  AtomicCompOut& compOut) const
54  {
55  auto thisColor = neighborColor.Get(0, 0, 0);
56 
57  auto minIndices = boundary.MinNeighborIndices(1);
58  auto maxIndices = boundary.MaxNeighborIndices(1);
59 
60  for (int k = minIndices[2]; k <= maxIndices[2]; k++)
61  {
62  for (int j = minIndices[1]; j <= maxIndices[1]; j++)
63  {
64  for (int i = minIndices[0]; i <= maxIndices[0]; i++)
65  {
66  if (thisColor == neighborColor.Get(i, j, k))
67  {
68  // We need to reload thisComp and thatComp every iteration since
69  // they might have been changed by Unite(), both as a result of
70  // attaching one tree to the other or as a result of path compaction
71  // in findRoot().
72  auto thisComp = neighborComp.Get(0, 0, 0);
73  auto thatComp = neighborComp.Get(i, j, k);
74 
75  // Merge the two components one way or the other, the order will
76  // be resolved by Unite().
77  UnionFind::Unite(compOut, thisComp, thatComp);
78  }
79  }
80  }
81  }
82  }
83 };
84 } // namespace detail
85 
86 // Single pass connected component algorithm from
87 // Jaiganesh, Jayadharini, and Martin Burtscher.
88 // "A high-performance connected components implementation for GPUs."
89 // Proceedings of the 27th International Symposium on High-Performance
90 // Parallel and Distributed Computing. 2018.
92 {
93  class RunImpl
94  {
95  public:
96  template <int Dimension, typename T, typename StorageT, typename OutputPortalType>
99  OutputPortalType& componentsOut) const
100  {
101  using Algorithm = vtkm::cont::Algorithm;
102 
103  // Initialize the parent pointer to point to the pixel itself. There are other
104  // ways to initialize the parent pointers, for example, a smaller or the minimal
105  // neighbor.
106  Algorithm::Copy(vtkm::cont::ArrayHandleIndex(pixels.GetNumberOfValues()), componentsOut);
107 
108  vtkm::cont::Invoker invoke;
109  invoke(detail::ImageGraft{}, input, componentsOut, pixels, componentsOut);
110  invoke(PointerJumping{}, componentsOut);
111 
112  // renumber connected component to the range of [0, number of components).
113  Renumber::Run(componentsOut);
114  }
115  };
116 
117 public:
118  template <typename T, typename S, typename OutputPortalType>
119  void Run(const vtkm::cont::UnknownCellSet& input,
120  const vtkm::cont::ArrayHandle<T, S>& pixels,
121  OutputPortalType& componentsOut) const
122  {
123  input.template CastAndCallForTypes<vtkm::cont::CellSetListStructured>(
124  RunImpl(), pixels, componentsOut);
125  }
126 };
127 } // namespace connectivity
128 } // namespace worklet
129 } // namespace vtkm
130 
131 #endif // vtk_m_worklet_connectivity_ImageConnectivity_h
vtkm::worklet::connectivity::Renumber::Run
static void Run(vtkm::cont::ArrayHandle< vtkm::Id > &componentsInOut)
Definition: InnerJoin.h:88
vtkm::cont::ArrayHandle::GetNumberOfValues
VTKM_CONT vtkm::Id GetNumberOfValues() const
Returns the number of entries in the array.
Definition: ArrayHandle.h:448
vtkm::cont::ArrayHandle
Manages an array-worth of data.
Definition: ArrayHandle.h:283
UnionFind.h
VTKM_EXEC
#define VTKM_EXEC
Definition: ExportMacros.h:51
vtkm
Groups connected points that have the same field value.
Definition: Atomic.h:19
WorkletMapField.h
vtkm::cont::CellSetStructured
Definition: CastAndCall.h:32
WorkletPointNeighborhood.h
vtkm::worklet::connectivity::ImageConnectivity::Run
void Run(const vtkm::cont::UnknownCellSet &input, const vtkm::cont::ArrayHandle< T, S > &pixels, OutputPortalType &componentsOut) const
Definition: worklet/ImageConnectivity.h:119
vtkm::worklet::connectivity::UnionFind::Unite
static VTKM_EXEC void Unite(Parents &parents, vtkm::Id u, vtkm::Id v)
Definition: UnionFind.h:45
vtkm::worklet::connectivity::ImageConnectivity
Definition: worklet/ImageConnectivity.h:91
Invoker.h
vtkm::cont::UnknownCellSet
A CellSet of an unknown type.
Definition: UnknownCellSet.h:48
UncertainCellSet.h
UncertainArrayHandle.h
Algorithm.h
vtkm::worklet::connectivity::ImageConnectivity::RunImpl
Definition: worklet/ImageConnectivity.h:93
vtkm::cont::Invoker
Allows launching any worklet without a dispatcher.
Definition: Invoker.h:41
InnerJoin.h
vtkm::cont::Algorithm
Definition: Algorithm.h:385
vtkm::worklet::connectivity::PointerJumping
Definition: UnionFind.h:157
vtkm::worklet::connectivity::ImageConnectivity::RunImpl::operator()
void operator()(const vtkm::cont::CellSetStructured< Dimension > &input, const vtkm::cont::ArrayHandle< T, StorageT > &pixels, OutputPortalType &componentsOut) const
Definition: worklet/ImageConnectivity.h:97
vtkm::worklet::WorkletPointNeighborhood
Definition: WorkletPointNeighborhood.h:27
vtkm::cont::ArrayHandleIndex
An implicit array handle containing the its own indices.
Definition: ArrayHandleIndex.h:54