vtk-m/vtkm/worklet/Triangulate.h
Kenneth Moreland 4239d13b3e Make triangulate implementation more general
The triangulate and tetrahedra worklet implementations were for some
reason grabbing raw arrays out of `CellSetExplicit` and using those for
lookup tables. Rather than do that, simply use a
`WorkletVisitCellsWithPoints` automatically provide the values of these
cells. The advantage of this approach is that it simplifies the code and
also generalize it to cell sets that do not provide this explicit
representation. For example, the new implementation also works with
`CellSetExtrude`.
2021-09-08 12:49:11 -06:00

104 lines
3.3 KiB
C++

//============================================================================
// 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 vtkm_m_worklet_Triangulate_h
#define vtkm_m_worklet_Triangulate_h
#include <vtkm/worklet/triangulate/TriangulateExplicit.h>
#include <vtkm/worklet/triangulate/TriangulateStructured.h>
namespace vtkm
{
namespace worklet
{
class Triangulate
{
public:
//
// Distribute multiple copies of cell data depending on cells create from original
//
struct DistributeCellData : public vtkm::worklet::WorkletMapField
{
using ControlSignature = void(FieldIn inIndices, FieldOut outIndices);
using ScatterType = vtkm::worklet::ScatterCounting;
template <typename CountArrayType>
VTKM_CONT static ScatterType MakeScatter(const CountArrayType& countArray)
{
return ScatterType(countArray);
}
template <typename T>
VTKM_EXEC void operator()(T inputIndex, T& outputIndex) const
{
outputIndex = inputIndex;
}
};
Triangulate()
: OutCellScatter(vtkm::cont::ArrayHandle<vtkm::IdComponent>{})
{
}
// Triangulate explicit data set, save number of triangulated cells per input
template <typename CellSetType>
vtkm::cont::CellSetSingleType<> Run(const CellSetType& cellSet)
{
TriangulateExplicit worklet;
vtkm::cont::ArrayHandle<vtkm::IdComponent> outCellsPerCell;
vtkm::cont::CellSetSingleType<> result = worklet.Run(cellSet, outCellsPerCell);
this->OutCellScatter = DistributeCellData::MakeScatter(outCellsPerCell);
return result;
}
// Triangulate structured data set, save number of triangulated cells per input
vtkm::cont::CellSetSingleType<> Run(const vtkm::cont::CellSetStructured<2>& cellSet)
{
TriangulateStructured worklet;
vtkm::cont::ArrayHandle<vtkm::IdComponent> outCellsPerCell;
vtkm::cont::CellSetSingleType<> result = worklet.Run(cellSet, outCellsPerCell);
this->OutCellScatter = DistributeCellData::MakeScatter(outCellsPerCell);
return result;
}
vtkm::cont::CellSetSingleType<> Run(const vtkm::cont::CellSetStructured<3>&)
{
throw vtkm::cont::ErrorBadType("CellSetStructured<3> can't be triangulated");
}
vtkm::cont::CellSetSingleType<> Run(const vtkm::cont::CellSetStructured<1>&)
{
throw vtkm::cont::ErrorBadType("CellSetStructured<1> can't be tetrahedralized");
}
// Using the saved input to output cells, expand cell data
template <typename ValueType, typename StorageType>
vtkm::cont::ArrayHandle<ValueType> ProcessCellField(
const vtkm::cont::ArrayHandle<ValueType, StorageType>& input) const
{
vtkm::cont::ArrayHandle<ValueType> output;
vtkm::worklet::DispatcherMapField<DistributeCellData> dispatcher(this->OutCellScatter);
dispatcher.Invoke(input, output);
return output;
}
DistributeCellData::ScatterType GetOutCellScatter() const { return this->OutCellScatter; }
private:
DistributeCellData::ScatterType OutCellScatter;
};
}
} // namespace vtkm::worklet
#endif // vtkm_m_worklet_Triangulate_h