Commit 8622a242 authored by Li-Ta Lo's avatar Li-Ta Lo Committed by Kitware Robot

Merge topic 'connected_component'

bdb9c37e update based on issues pointed out by Robert
a713a0d8 Generalize and documentation for DeviceAdapterAlgorithm::Transform
29232c49 Revert un-intended change to examples
7ef956a9 Merge branch 'master' into connected_component
a9ed1ecf add CMakeLists.txt for header files
ba3cba64 update copyright statements
aa96874e Merge branch 'connected_component' of gitlab.kitware.com:ollielo/vtk-m into connected_component
2f07119e Merge branch 'master' into connected_component
...
Acked-by: Kitware Robot's avatarKitware Robot <kwrobot@kitware.com>
Acked-by: default avatarRobert Maynard <robert.maynard@kitware.com>
Merge-request: !1044
parents 03b84a47 bdb9c37e
......@@ -245,7 +245,7 @@ int main(int argc, char* argv[])
vtkm::filter::MarchingCubes filter;
filter.SetGenerateNormals(true);
filter.SetMergeDuplicatePoints(false);
filter.SetIsoValue(0, 0.5);
filter.SetIsoValue(0, 0.1);
vtkm::filter::Result result = filter.Execute(dataSet, dataSet.GetField("nodevar"));
filter.MapFieldOntoOutput(result, dataSet.GetField("nodevar"));
......
......@@ -439,6 +439,26 @@ struct DeviceAdapterAlgorithm
///
VTKM_CONT static void Synchronize();
/// \brief Apply a given binary operation function element-wise to input arrays.
///
/// Apply the give binary operation to pairs of elements from the two input array
/// \c input1 and \c input2. The number of elements in the input arrays do not
/// have to be the same, in this case, only the smaller of the two numbers of elements
/// will be applied.
/// Outputs of the binary operation is stored in \c output.
///
template <typename T,
typename U,
typename V,
typename StorageT,
typename StorageU,
typename StorageV,
typename BinaryFunctor>
VTKM_CONT static void Transform(const vtkm::cont::ArrayHandle<T, StorageT>& input1,
const vtkm::cont::ArrayHandle<U, StorageU>& input2,
vtkm::cont::ArrayHandle<V, StorageV>& output,
BinaryFunctor binaryFunctor);
/// \brief Reduce an array to only the unique values it contains
///
/// Removes all duplicate values in \c values that are adjacent to each
......
......@@ -763,6 +763,37 @@ public:
DerivedAlgorithm::Sort(zipHandle, internal::KeyCompare<T, U, BinaryCompare>(binary_compare));
}
template <typename T,
typename U,
typename V,
typename StorageT,
typename StorageU,
typename StorageV,
typename BinaryFunctor>
VTKM_CONT static void Transform(const vtkm::cont::ArrayHandle<T, StorageT>& input1,
const vtkm::cont::ArrayHandle<U, StorageU>& input2,
vtkm::cont::ArrayHandle<V, StorageV>& output,
BinaryFunctor binaryFunctor)
{
vtkm::Id numValues = vtkm::Min(input1.GetNumberOfValues(), input2.GetNumberOfValues());
if (numValues <= 0)
{
return;
}
auto input1Portal = input1.PrepareForInput(DeviceAdapterTag());
auto input2Portal = input2.PrepareForInput(DeviceAdapterTag());
auto outputPortal = output.PrepareForOutput(numValues, DeviceAdapterTag());
BinaryTransformKernel<decltype(input1Portal),
decltype(input2Portal),
decltype(outputPortal),
BinaryFunctor>
binaryKernel(input1Portal, input2Portal, outputPortal, binaryFunctor);
DerivedAlgorithm::Schedule(binaryKernel, numValues);
}
//};
//--------------------------------------------------------------------------
// Unique
template <typename T, class Storage>
......
......@@ -881,6 +881,38 @@ struct ScanKernel : vtkm::exec::FunctorBase
}
}
};
template <typename InPortalType1,
typename InPortalType2,
typename OutPortalType,
typename BinaryFunctor>
struct BinaryTransformKernel : vtkm::exec::FunctorBase
{
InPortalType1 InPortal1;
InPortalType2 InPortal2;
OutPortalType OutPortal;
BinaryFunctor BinaryOperator;
VTKM_CONT
BinaryTransformKernel(const InPortalType1& inPortal1,
const InPortalType2& inPortal2,
const OutPortalType& outPortal,
BinaryFunctor binaryOperator)
: InPortal1(inPortal1)
, InPortal2(inPortal2)
, OutPortal(outPortal)
, BinaryOperator(binaryOperator)
{
}
VTKM_SUPPRESS_EXEC_WARNINGS
VTKM_EXEC
void operator()(vtkm::Id index) const
{
this->OutPortal.Set(
index, this->BinaryOperator(this->InPortal1.Get(index), this->InPortal2.Get(index)));
}
};
}
}
} // namespace vtkm::cont::internal
......
##============================================================================
## 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 2018 National Technology & Engineering Solutions of Sandia, LLC (NTESS).
## Copyright 2018 UT-Battelle, LLC.
## Copyright 2018 Los Alamos National Security.
##
## Under the terms of Contract DE-NA0003525 with NTESS,
## 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.
##============================================================================
set(headers
CellSetConnectivity.h
CellSetDualGraph.h
GraphConnectivity.h
InnerJoin.h
)
#-----------------------------------------------------------------------------
vtkm_declare_headers(${headers})
//=============================================================================
//
// 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 2018 National Technology & Engineering Solutions of Sandia, LLC (NTESS).
// Copyright 2018 UT-Battelle, LLC.
// Copyright 2018 Los Alamos National Security.
//
// Under the terms of Contract DE-NA0003525 with NTESS,
// 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.
//
//=============================================================================
#ifndef vtk_m_worklet_connectivity_CellSetConnectivity_h
#define vtk_m_worklet_connectivity_CellSetConnectivity_h
#include <vtkm/worklet/connectivities/CellSetDualGraph.h>
#include <vtkm/worklet/connectivities/GraphConnectivity.h>
class CellSetConnectivity
{
public:
template <typename CellSetType, typename DeviceAdapter>
void Run(const CellSetType& cellSet,
vtkm::cont::ArrayHandle<vtkm::Id>& componentArray,
DeviceAdapter) const
{
vtkm::cont::ArrayHandle<vtkm::Id> numIndicesArray;
vtkm::cont::ArrayHandle<vtkm::Id> indexOffsetArray;
vtkm::cont::ArrayHandle<vtkm::Id> connectivityArray;
// create cell to cell connectivity graph (dual graph)
CellSetDualGraph<DeviceAdapter>().Run(
cellSet, numIndicesArray, indexOffsetArray, connectivityArray);
// find the connected component of the dual graph
GraphConnectivity<DeviceAdapter>().Run(
numIndicesArray, indexOffsetArray, connectivityArray, componentArray);
}
};
#endif // vtk_m_worklet_connectivity_CellSetConnectivity_h
//=============================================================================
//
// 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 2018 National Technology & Engineering Solutions of Sandia, LLC (NTESS).
// Copyright 2018 UT-Battelle, LLC.
// Copyright 2018 Los Alamos National Security.
//
// Under the terms of Contract DE-NA0003525 with NTESS,
// 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.
//
//=============================================================================
#ifndef vtk_m_worklet_CellSetDualGraph_h
#define vtk_m_worklet_CellSetDualGraph_h
#include <vtkm/cont/CellSetSingleType.h>
#include <vtkm/exec/CellEdge.h>
#include <vtkm/worklet/DispatcherMapTopology.h>
#include <vtkm/worklet/ScatterCounting.h>
#include <vtkm/worklet/WorkletMapTopology.h>
struct EdgeCount : public vtkm::worklet::WorkletMapPointToCell
{
typedef void ControlSignature(CellSetIn, FieldOutCell<> numEdgesInCell);
typedef _2 ExecutionSignature(CellShape, PointCount);
using InputDomain = _1;
template <typename CellShapeTag>
VTKM_EXEC vtkm::IdComponent operator()(CellShapeTag cellShape, vtkm::IdComponent pointCount) const
{
return vtkm::exec::CellEdgeNumberOfEdges(pointCount, cellShape, *this);
}
};
struct EdgeExtract : public vtkm::worklet::WorkletMapPointToCell
{
typedef void ControlSignature(CellSetIn, FieldOutCell<> cellIndices, FieldOutCell<> edgeIndices);
typedef void ExecutionSignature(CellShape, InputIndex, PointIndices, VisitIndex, _2, _3);
using InputDomain = _1;
using ScatterType = vtkm::worklet::ScatterCounting;
VTKM_CONT ScatterType GetScatter() const { return this->Scatter; }
VTKM_CONT EdgeExtract(const ScatterType& scatter)
: Scatter(scatter)
{
}
template <typename CellShapeTag,
typename CellIndexType,
typename PointIndexVecType,
typename EdgeIndexVecType>
VTKM_EXEC void operator()(CellShapeTag cellShape,
CellIndexType cellIndex,
const PointIndexVecType& pointIndices,
vtkm::IdComponent visitIndex,
CellIndexType& cellIndexOut,
EdgeIndexVecType& edgeIndices) const
{
cellIndexOut = cellIndex;
edgeIndices = vtkm::exec::CellEdgeCanonicalId(
pointIndices.GetNumberOfComponents(), visitIndex, cellShape, pointIndices, *this);
};
private:
ScatterType Scatter;
};
struct CellToCellConnectivity : public vtkm::worklet::WorkletMapField
{
typedef void ControlSignature(FieldIn<> index,
WholeArrayIn<> cells,
WholeArrayOut<> from,
WholeArrayOut<> to);
typedef void ExecutionSignature(_1, InputIndex, _2, _3, _4);
using InputDomain = _1;
template <typename ConnectivityPortalType, typename CellIdPortalType>
VTKM_EXEC void operator()(vtkm::Id offset,
vtkm::Id index,
const CellIdPortalType& cells,
ConnectivityPortalType& from,
ConnectivityPortalType& to) const
{
from.Set(index * 2, cells.Get(offset));
to.Set(index * 2, cells.Get(offset + 1));
from.Set(index * 2 + 1, cells.Get(offset + 1));
to.Set(index * 2 + 1, cells.Get(offset));
}
};
template <typename DeviceAdapter>
class CellSetDualGraph
{
public:
using Algorithm = vtkm::cont::DeviceAdapterAlgorithm<DeviceAdapter>;
struct degree2
{
VTKM_EXEC
bool operator()(vtkm::Id degree) const { return degree == 2; }
};
template <typename T>
void EdgeToCellConnectivity(const vtkm::cont::CellSetSingleType<T>& cellSet,
vtkm::cont::ArrayHandle<vtkm::Id>& cellIds,
vtkm::cont::ArrayHandle<vtkm::Id2>& cellEdges) const
{
// Get number of edges for each cell and use it as scatter count.
vtkm::cont::ArrayHandle<vtkm::IdComponent> numEdgesPerCell;
vtkm::worklet::DispatcherMapTopology<EdgeCount, DeviceAdapter> edgesPerCellDisp;
edgesPerCellDisp.Invoke(cellSet, numEdgesPerCell);
// Get uncompress Cell to Edge mapping
vtkm::worklet::ScatterCounting scatter{ numEdgesPerCell, DeviceAdapter() };
vtkm::worklet::DispatcherMapTopology<EdgeExtract, DeviceAdapter> edgeExtractDisp{ scatter };
edgeExtractDisp.Invoke(cellSet, cellIds, cellEdges);
}
template <typename T>
void Run(const vtkm::cont::CellSetSingleType<T>& cellSet,
vtkm::cont::ArrayHandle<vtkm::Id>& numIndicesArray,
vtkm::cont::ArrayHandle<vtkm::Id>& indexOffsetArray,
vtkm::cont::ArrayHandle<vtkm::Id>& connectivityArray) const
{
// calculate the uncompressed Edge to Cell connectivity from Point to Cell connectivity
// in the CellSet
vtkm::cont::ArrayHandle<vtkm::Id> cellIds;
vtkm::cont::ArrayHandle<vtkm::Id2> cellEdges;
EdgeToCellConnectivity(cellSet, cellIds, cellEdges);
// sort cell ids by cell edges, this groups cells by cell edges
Algorithm::SortByKey(cellEdges, cellIds);
// count how many times an edge is shared by cells.
vtkm::cont::ArrayHandle<vtkm::Id2> uniqueEdges;
vtkm::cont::ArrayHandle<vtkm::Id> uniqueEdgeDegree;
Algorithm::ReduceByKey(
cellEdges,
vtkm::cont::ArrayHandleConstant<vtkm::Id>(1, cellEdges.GetNumberOfValues()),
uniqueEdges,
uniqueEdgeDegree,
vtkm::Add());
// Extract edges shared by two cells
vtkm::cont::ArrayHandle<vtkm::Id2> sharedEdges;
Algorithm::CopyIf(uniqueEdges, uniqueEdgeDegree, sharedEdges, degree2());
// find shared edges within all the edges.
vtkm::cont::ArrayHandle<vtkm::Id> lb;
Algorithm::LowerBounds(cellEdges, sharedEdges, lb);
// take each shared edge and the cells to create 2 edges of the dual graph
vtkm::cont::ArrayHandle<vtkm::Id> connFrom;
vtkm::cont::ArrayHandle<vtkm::Id> connTo;
connFrom.Allocate(sharedEdges.GetNumberOfValues() * 2);
connTo.Allocate(sharedEdges.GetNumberOfValues() * 2);
vtkm::worklet::DispatcherMapField<CellToCellConnectivity, DeviceAdapter> c2cDisp;
c2cDisp.Invoke(lb, cellIds, connFrom, connTo);
// Turn dual graph into Compressed Sparse Row format
Algorithm::SortByKey(connFrom, connTo);
Algorithm::Copy(connTo, connectivityArray);
vtkm::cont::ArrayHandle<vtkm::Id> dualGraphVertices;
Algorithm::ReduceByKey(
connFrom,
vtkm::cont::ArrayHandleConstant<vtkm::Id>(1, connFrom.GetNumberOfValues()),
dualGraphVertices,
numIndicesArray,
vtkm::Add());
Algorithm::ScanExclusive(numIndicesArray, indexOffsetArray);
}
};
#endif //vtk_m_worklet_CellSetDualGraph_h
//=============================================================================
//
// 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 2018 National Technology & Engineering Solutions of Sandia, LLC (NTESS).
// Copyright 2018 UT-Battelle, LLC.
// Copyright 2018 Los Alamos National Security.
//
// Under the terms of Contract DE-NA0003525 with NTESS,
// 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.
//
//=============================================================================
#ifndef vtk_m_worklet_connectivity_graph_connectivity_h
#define vtk_m_worklet_connectivity_graph_connectivity_h
#include <vtkm/worklet/connectivities/CellSetDualGraph.h>
#include <vtkm/worklet/connectivities/InnerJoin.h>
class Graft : public vtkm::worklet::WorkletMapField
{
public:
typedef void ControlSignature(FieldIn<IdType> index,
FieldIn<IdType> start,
FieldIn<IdType> degree,
WholeArrayIn<IdType> ids,
WholeArrayInOut<IdType> comp);
typedef void ExecutionSignature(_1, _2, _3, _4, _5);
typedef _1 InputDomain;
// TODO: Use Scatter?
template <typename InPortalType, typename InOutPortalType>
VTKM_EXEC void operator()(vtkm::Id index,
vtkm::Id start,
vtkm::Id degree,
const InPortalType& conn,
InOutPortalType& comp) const
{
for (vtkm::Id offset = start; offset < start + degree; offset++)
{
vtkm::Id neighbor = conn.Get(offset);
if ((comp.Get(index) == comp.Get(comp.Get(index))) && (comp.Get(neighbor) < comp.Get(index)))
{
comp.Set(comp.Get(index), comp.Get(neighbor));
}
}
}
};
class PointerJumping : public vtkm::worklet::WorkletMapField
{
public:
typedef void ControlSignature(FieldIn<IdType> index, WholeArrayInOut<IdType> comp);
typedef void ExecutionSignature(_1, _2);
typedef _1 InputDomain;
template <typename InOutPortalType>
VTKM_EXEC void operator()(vtkm::Id index, InOutPortalType& comp) const
{
// keep updating component id until we reach the root of the tree.
for (auto parent = comp.Get(index); comp.Get(parent) != parent; parent = comp.Get(index))
{
comp.Set(index, comp.Get(parent));
}
};
};
class IsStar : public vtkm::worklet::WorkletMapField
{
public:
typedef void ControlSignature(FieldIn<IdType> index, WholeArrayIn<IdType> comp, FieldOut<>);
typedef _3 ExecutionSignature(_1, _2);
typedef _1 InputDomain;
template <typename InOutPortalType>
VTKM_EXEC bool operator()(vtkm::Id index, InOutPortalType& comp) const
{
return comp.Get(index) == comp.Get(comp.Get(index));
}
};
template <typename DeviceAdapter>
class GraphConnectivity
{
public:
using Algorithm = vtkm::cont::DeviceAdapterAlgorithm<DeviceAdapter>;
template <typename InputPortalType, typename OutputPortalType>
void Run(const InputPortalType& numIndexArray,
const InputPortalType& indexOffsetArray,
const InputPortalType& connectivityArray,
OutputPortalType& componentsOut) const
{
using Algorithm = vtkm::cont::DeviceAdapterAlgorithm<DeviceAdapter>;
bool allStar = false;
vtkm::cont::ArrayHandle<vtkm::Id> components;
vtkm::cont::ArrayHandle<bool> isStar;
vtkm::cont::ArrayHandle<vtkm::Id> cellIds;
Algorithm::Copy(
vtkm::cont::ArrayHandleCounting<vtkm::Id>(0, 1, numIndexArray.GetNumberOfValues()), cellIds);
Algorithm::Copy(cellIds, components);
do
{
vtkm::worklet::DispatcherMapField<Graft, DeviceAdapter> graftDispatcher;
graftDispatcher.Invoke(
cellIds, indexOffsetArray, numIndexArray, connectivityArray, components);
// Detection of allStar has to come before pointer jumping. Don't try to rearrange it.
vtkm::worklet::DispatcherMapField<IsStar, DeviceAdapter> isStarDisp;
isStarDisp.Invoke(cellIds, components, isStar);
allStar = Algorithm::Reduce(isStar, true, vtkm::LogicalAnd());
vtkm::worklet::DispatcherMapField<PointerJumping, DeviceAdapter> pointJumpingDispatcher;
pointJumpingDispatcher.Invoke(cellIds, components);
} while (!allStar);
// renumber connected component to the range of [0, number of components).
vtkm::cont::ArrayHandle<vtkm::Id> uniqueComponents;
Algorithm::Copy(components, uniqueComponents);
Algorithm::Sort(uniqueComponents);
Algorithm::Unique(uniqueComponents);
vtkm::cont::ArrayHandle<vtkm::Id> uniqueColor;
Algorithm::Copy(
vtkm::cont::ArrayHandleCounting<vtkm::Id>(0, 1, uniqueComponents.GetNumberOfValues()),
uniqueColor);
vtkm::cont::ArrayHandle<vtkm::Id> cellColors;
vtkm::cont::ArrayHandle<vtkm::Id> cellIdsOut;
InnerJoin<DeviceAdapter>().Run(
components, cellIds, uniqueComponents, uniqueColor, cellColors, cellIdsOut, componentsOut);
Algorithm::SortByKey(cellIdsOut, componentsOut);
}
};
#endif //vtk_m_worklet_connectivity_graph_connectivity_h
//=============================================================================
//
// 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 2018 National Technology & Engineering Solutions of Sandia, LLC (NTESS).
// Copyright 2018 UT-Battelle, LLC.
// Copyright 2018 Los Alamos National Security.
//
// Under the terms of Contract DE-NA0003525 with NTESS,
// 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.
//
//=============================================================================
#ifndef vtk_m_worklet_connectivity_InnerJoin_h
#define vtk_m_worklet_connectivity_InnerJoin_h
#include <vtkm/cont/ArrayHandleCounting.h>
#include <vtkm/cont/DeviceAdapter.h>
#include <vtkm/worklet/DispatcherMapField.h>
#include <vtkm/worklet/ScatterCounting.h>
#include <vtkm/worklet/WorkletMapField.h>
template <typename DeviceAdapter>
class InnerJoin
{
public:
struct Merge : vtkm::worklet::WorkletMapField
{
typedef void ControlSignature(FieldIn<>,
FieldIn<>,
FieldIn<>,
WholeArrayIn<>,
FieldOut<>,
FieldOut<>,
FieldOut<>);
typedef void ExecutionSignature(_1, _2, _3, VisitIndex, _4, _5, _6, _7);
using InputDomain = _1;
using ScatterType = vtkm::worklet::ScatterCounting;
VTKM_CONT
ScatterType GetScatter() const { return this->Scatter; }
VTKM_CONT
Merge(const ScatterType& scatter)
: Scatter(scatter)
{
}
// TODO: type trait for array portal?
template <typename KeyType, typename ValueType1, typename InPortalType, typename ValueType2>
VTKM_EXEC void operator()(KeyType key,