2016-11-06 22:15:22 +00:00
|
|
|
//============================================================================
|
|
|
|
// Copyright (c) Kitware, Inc.
|
|
|
|
// All rights reserved.
|
|
|
|
// See LICENSE.txt for details.
|
2019-04-15 23:24:21 +00:00
|
|
|
//
|
2016-11-06 22:15:22 +00:00
|
|
|
// 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.
|
|
|
|
//============================================================================
|
|
|
|
|
2019-01-28 14:24:48 +00:00
|
|
|
#include <vtkm/filter/CleanGrid.h>
|
|
|
|
|
2016-11-06 22:15:22 +00:00
|
|
|
#include <vtkm/worklet/CellDeepCopy.h>
|
|
|
|
#include <vtkm/worklet/RemoveUnusedPoints.h>
|
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
|
2017-05-18 14:29:41 +00:00
|
|
|
namespace vtkm
|
|
|
|
{
|
|
|
|
namespace filter
|
|
|
|
{
|
2016-11-06 22:15:22 +00:00
|
|
|
|
2017-05-18 14:29:41 +00:00
|
|
|
inline VTKM_CONT CleanGrid::CleanGrid()
|
2016-11-06 22:15:22 +00:00
|
|
|
: CompactPointFields(true)
|
2019-01-28 14:24:48 +00:00
|
|
|
, MergePoints(true)
|
|
|
|
, Tolerance(1.0e-6)
|
|
|
|
, ToleranceIsAbsolute(false)
|
2019-02-15 16:40:07 +00:00
|
|
|
, RemoveDegenerateCells(true)
|
2019-01-28 14:24:48 +00:00
|
|
|
, FastMerge(true)
|
2017-05-18 14:29:41 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-08-30 15:53:18 +00:00
|
|
|
template <typename Policy>
|
2018-04-02 21:58:21 +00:00
|
|
|
inline VTKM_CONT vtkm::cont::DataSet CleanGrid::DoExecute(const vtkm::cont::DataSet& inData,
|
2018-08-30 15:53:18 +00:00
|
|
|
vtkm::filter::PolicyBase<Policy> policy)
|
2016-11-06 22:15:22 +00:00
|
|
|
{
|
|
|
|
using CellSetType = vtkm::cont::CellSetExplicit<>;
|
2019-08-13 17:08:54 +00:00
|
|
|
using VecId = std::size_t;
|
2016-11-06 22:15:22 +00:00
|
|
|
|
2019-02-15 16:40:07 +00:00
|
|
|
VecId activeCoordIndex = static_cast<VecId>(this->GetActiveCoordinateSystemIndex());
|
|
|
|
|
2019-08-13 17:08:54 +00:00
|
|
|
CellSetType outputCellSet;
|
2016-11-06 22:15:22 +00:00
|
|
|
// Do a deep copy of the cells to new CellSetExplicit structures
|
2019-08-13 17:08:54 +00:00
|
|
|
const vtkm::cont::DynamicCellSet& inCellSet = inData.GetCellSet();
|
|
|
|
if (inCellSet.IsType<CellSetType>())
|
2016-11-06 22:15:22 +00:00
|
|
|
{
|
2019-08-13 17:08:54 +00:00
|
|
|
// Is expected type, do a shallow copy
|
|
|
|
outputCellSet = inCellSet.Cast<CellSetType>();
|
2016-11-06 22:15:22 +00:00
|
|
|
}
|
2019-08-13 17:08:54 +00:00
|
|
|
else
|
|
|
|
{ // Clean the grid
|
2019-09-09 22:08:11 +00:00
|
|
|
auto deducedCellSet = vtkm::filter::ApplyPolicyCellSet(inCellSet, policy);
|
2019-08-13 17:08:54 +00:00
|
|
|
vtkm::cont::ArrayHandle<vtkm::IdComponent> numIndices;
|
|
|
|
|
|
|
|
this->Invoke(worklet::CellDeepCopy::CountCellPoints{}, deducedCellSet, numIndices);
|
|
|
|
|
|
|
|
vtkm::cont::ArrayHandle<vtkm::UInt8> shapes;
|
|
|
|
vtkm::cont::ArrayHandle<vtkm::Id> offsets;
|
|
|
|
vtkm::Id connectivitySize;
|
|
|
|
vtkm::cont::ConvertNumComponentsToOffsets(numIndices, offsets, connectivitySize);
|
|
|
|
numIndices.ReleaseResourcesExecution();
|
|
|
|
|
|
|
|
vtkm::cont::ArrayHandle<vtkm::Id> connectivity;
|
|
|
|
connectivity.Allocate(connectivitySize);
|
|
|
|
|
|
|
|
this->Invoke(worklet::CellDeepCopy::PassCellStructure{},
|
|
|
|
deducedCellSet,
|
|
|
|
shapes,
|
|
|
|
vtkm::cont::make_ArrayHandleGroupVecVariable(connectivity, offsets));
|
|
|
|
shapes.ReleaseResourcesExecution();
|
|
|
|
offsets.ReleaseResourcesExecution();
|
|
|
|
connectivity.ReleaseResourcesExecution();
|
|
|
|
|
|
|
|
outputCellSet.Fill(
|
|
|
|
deducedCellSet.GetNumberOfPoints(), shapes, numIndices, connectivity, offsets);
|
|
|
|
|
|
|
|
//Release the input grid from the execution space
|
|
|
|
deducedCellSet.ReleaseResourcesExecution();
|
|
|
|
}
|
|
|
|
|
2016-11-06 22:15:22 +00:00
|
|
|
|
2019-01-28 14:24:48 +00:00
|
|
|
VecId numCoordSystems = static_cast<VecId>(inData.GetNumberOfCoordinateSystems());
|
|
|
|
std::vector<vtkm::cont::CoordinateSystem> outputCoordinateSystems(numCoordSystems);
|
|
|
|
|
|
|
|
// Start with a shallow copy of the coordinate systems
|
|
|
|
for (VecId coordSystemIndex = 0; coordSystemIndex < numCoordSystems; ++coordSystemIndex)
|
|
|
|
{
|
|
|
|
outputCoordinateSystems[coordSystemIndex] =
|
|
|
|
inData.GetCoordinateSystem(static_cast<vtkm::IdComponent>(coordSystemIndex));
|
|
|
|
}
|
|
|
|
|
2016-11-06 22:15:22 +00:00
|
|
|
// Optionally adjust the cell set indices to remove all unused points
|
|
|
|
if (this->GetCompactPointFields())
|
|
|
|
{
|
2018-08-30 15:53:18 +00:00
|
|
|
this->PointCompactor.FindPointsStart();
|
2019-08-13 17:08:54 +00:00
|
|
|
this->PointCompactor.FindPoints(outputCellSet);
|
2018-08-30 15:53:18 +00:00
|
|
|
this->PointCompactor.FindPointsEnd();
|
2016-11-06 22:15:22 +00:00
|
|
|
|
2019-08-13 17:08:54 +00:00
|
|
|
outputCellSet = this->PointCompactor.MapCellSet(outputCellSet);
|
2019-01-28 14:24:48 +00:00
|
|
|
|
|
|
|
for (VecId coordSystemIndex = 0; coordSystemIndex < numCoordSystems; ++coordSystemIndex)
|
|
|
|
{
|
|
|
|
outputCoordinateSystems[coordSystemIndex] =
|
|
|
|
vtkm::cont::CoordinateSystem(outputCoordinateSystems[coordSystemIndex].GetName(),
|
|
|
|
this->PointCompactor.MapPointFieldDeep(
|
|
|
|
outputCoordinateSystems[coordSystemIndex].GetData()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Optionally find and merge coincident points
|
|
|
|
if (this->GetMergePoints())
|
|
|
|
{
|
2019-02-15 16:40:07 +00:00
|
|
|
vtkm::cont::CoordinateSystem activeCoordSystem = outputCoordinateSystems[activeCoordIndex];
|
2019-01-28 14:24:48 +00:00
|
|
|
vtkm::Bounds bounds = activeCoordSystem.GetBounds();
|
|
|
|
|
|
|
|
vtkm::Float64 delta = this->GetTolerance();
|
|
|
|
if (!this->GetToleranceIsAbsolute())
|
|
|
|
{
|
|
|
|
delta *=
|
|
|
|
vtkm::Magnitude(vtkm::make_Vec(bounds.X.Length(), bounds.Y.Length(), bounds.Z.Length()));
|
|
|
|
}
|
|
|
|
|
|
|
|
auto coordArray = activeCoordSystem.GetData();
|
|
|
|
this->PointMerger.Run(delta, this->GetFastMerge(), bounds, coordArray);
|
|
|
|
activeCoordSystem = vtkm::cont::CoordinateSystem(activeCoordSystem.GetName(), coordArray);
|
|
|
|
|
|
|
|
for (VecId coordSystemIndex = 0; coordSystemIndex < numCoordSystems; ++coordSystemIndex)
|
|
|
|
{
|
2019-02-15 16:40:07 +00:00
|
|
|
if (coordSystemIndex == activeCoordIndex)
|
2019-01-28 14:24:48 +00:00
|
|
|
{
|
|
|
|
outputCoordinateSystems[coordSystemIndex] = activeCoordSystem;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
outputCoordinateSystems[coordSystemIndex] = vtkm::cont::CoordinateSystem(
|
|
|
|
outputCoordinateSystems[coordSystemIndex].GetName(),
|
|
|
|
this->PointMerger.MapPointField(outputCoordinateSystems[coordSystemIndex].GetData()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-13 17:08:54 +00:00
|
|
|
outputCellSet = this->PointMerger.MapCellSet(outputCellSet);
|
2016-11-06 22:15:22 +00:00
|
|
|
}
|
|
|
|
|
2019-02-15 16:40:07 +00:00
|
|
|
// Optionally remove degenerate cells
|
|
|
|
if (this->GetRemoveDegenerateCells())
|
|
|
|
{
|
2019-08-13 17:08:54 +00:00
|
|
|
outputCellSet = this->CellCompactor.Run(outputCellSet);
|
2019-02-15 16:40:07 +00:00
|
|
|
}
|
|
|
|
|
2016-11-06 22:15:22 +00:00
|
|
|
// Construct resulting data set with new cell sets
|
|
|
|
vtkm::cont::DataSet outData;
|
2019-08-13 17:08:54 +00:00
|
|
|
outData.SetCellSet(outputCellSet);
|
2016-11-06 22:15:22 +00:00
|
|
|
|
|
|
|
// Pass the coordinate systems
|
2019-01-28 14:24:48 +00:00
|
|
|
for (VecId coordSystemIndex = 0; coordSystemIndex < numCoordSystems; ++coordSystemIndex)
|
2016-11-06 22:15:22 +00:00
|
|
|
{
|
2019-01-28 14:24:48 +00:00
|
|
|
outData.AddCoordinateSystem(outputCoordinateSystems[coordSystemIndex]);
|
2016-11-06 22:15:22 +00:00
|
|
|
}
|
|
|
|
|
2018-04-02 21:58:21 +00:00
|
|
|
return outData;
|
2016-11-06 22:15:22 +00:00
|
|
|
}
|
|
|
|
|
2018-08-30 15:53:18 +00:00
|
|
|
template <typename ValueType, typename Storage, typename Policy>
|
2017-05-18 14:29:41 +00:00
|
|
|
inline VTKM_CONT bool CleanGrid::DoMapField(
|
2018-04-02 21:58:21 +00:00
|
|
|
vtkm::cont::DataSet& result,
|
2017-05-26 17:53:28 +00:00
|
|
|
const vtkm::cont::ArrayHandle<ValueType, Storage>& input,
|
|
|
|
const vtkm::filter::FieldMetadata& fieldMeta,
|
2018-08-30 15:53:18 +00:00
|
|
|
vtkm::filter::PolicyBase<Policy>)
|
2016-11-06 22:15:22 +00:00
|
|
|
{
|
2019-01-28 14:24:48 +00:00
|
|
|
if (fieldMeta.IsPointField() && (this->GetCompactPointFields() || this->GetMergePoints()))
|
2016-11-06 22:15:22 +00:00
|
|
|
{
|
2019-01-28 14:24:48 +00:00
|
|
|
vtkm::cont::ArrayHandle<ValueType> compactedArray;
|
|
|
|
if (this->GetCompactPointFields())
|
|
|
|
{
|
|
|
|
compactedArray = this->PointCompactor.MapPointFieldDeep(input);
|
|
|
|
if (this->GetMergePoints())
|
|
|
|
{
|
|
|
|
compactedArray = this->PointMerger.MapPointField(compactedArray);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (this->GetMergePoints())
|
|
|
|
{
|
|
|
|
compactedArray = this->PointMerger.MapPointField(input);
|
|
|
|
}
|
2018-04-02 21:58:21 +00:00
|
|
|
result.AddField(fieldMeta.AsField(compactedArray));
|
2016-11-06 22:15:22 +00:00
|
|
|
}
|
2019-02-15 16:40:07 +00:00
|
|
|
else if (fieldMeta.IsCellField() && this->GetRemoveDegenerateCells())
|
|
|
|
{
|
|
|
|
result.AddField(fieldMeta.AsField(this->CellCompactor.ProcessCellField(input)));
|
|
|
|
}
|
2016-11-06 22:15:22 +00:00
|
|
|
else
|
|
|
|
{
|
2018-04-02 21:58:21 +00:00
|
|
|
result.AddField(fieldMeta.AsField(input));
|
2016-11-06 22:15:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|