vtk-m/vtkm/filter/Contour.hxx

235 lines
7.5 KiB
C++
Raw Normal View History

2016-01-19 14:59:31 +00:00
//============================================================================
// Copyright (c) Kitware, Inc.
// All rights reserved.
// See LICENSE.txt for details.
2019-04-15 23:24:21 +00:00
//
2016-01-19 14:59:31 +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.
//============================================================================
#include <vtkm/cont/ArrayHandleIndex.h>
#include <vtkm/cont/CellSetSingleType.h>
#include <vtkm/cont/CellSetStructured.h>
2017-05-18 14:51:24 +00:00
#include <vtkm/cont/DynamicCellSet.h>
#include <vtkm/cont/ErrorFilterExecution.h>
2016-01-19 14:59:31 +00:00
#include <vtkm/worklet/SurfaceNormals.h>
2016-01-19 14:59:31 +00:00
2017-05-18 14:29:41 +00:00
namespace vtkm
{
namespace filter
{
2016-01-19 14:59:31 +00:00
namespace
{
template <typename CellSetList>
bool IsCellSetStructured(const vtkm::cont::DynamicCellSetBase<CellSetList>& cellset)
{
if (cellset.template IsType<vtkm::cont::CellSetStructured<1>>() ||
cellset.template IsType<vtkm::cont::CellSetStructured<2>>() ||
cellset.template IsType<vtkm::cont::CellSetStructured<3>>())
{
return true;
}
return false;
}
} // anonymous namespace
2016-01-19 14:59:31 +00:00
//-----------------------------------------------------------------------------
2019-08-18 01:10:57 +00:00
inline VTKM_CONT Contour::Contour()
: vtkm::filter::FilterDataSetWithField<Contour>()
2017-05-18 14:29:41 +00:00
, IsoValues()
, GenerateNormals(false)
, AddInterpolationEdgeIds(false)
, ComputeFastNormalsForStructured(false)
, ComputeFastNormalsForUnstructured(true)
2017-05-18 14:29:41 +00:00
, NormalArrayName("normals")
, InterpolationEdgeIdsArrayName("edgeIds")
2017-05-18 14:29:41 +00:00
, Worklet()
2016-01-19 14:59:31 +00:00
{
// todo: keep an instance of marching cubes worklet as a member variable
2016-01-19 14:59:31 +00:00
}
//-----------------------------------------------------------------------------
2019-08-18 01:10:57 +00:00
inline void Contour::SetNumberOfIsoValues(vtkm::Id num)
{
2017-05-18 14:29:41 +00:00
if (num >= 0)
{
this->IsoValues.resize(static_cast<std::size_t>(num));
}
}
//-----------------------------------------------------------------------------
2019-08-18 01:10:57 +00:00
inline vtkm::Id Contour::GetNumberOfIsoValues() const
{
return static_cast<vtkm::Id>(this->IsoValues.size());
}
//-----------------------------------------------------------------------------
2019-08-18 01:10:57 +00:00
inline void Contour::SetIsoValue(vtkm::Id index, vtkm::Float64 v)
{
std::size_t i = static_cast<std::size_t>(index);
2017-05-18 14:29:41 +00:00
if (i >= this->IsoValues.size())
{
2017-05-18 14:29:41 +00:00
this->IsoValues.resize(i + 1);
}
this->IsoValues[i] = v;
}
//-----------------------------------------------------------------------------
2019-08-18 01:10:57 +00:00
inline void Contour::SetIsoValues(const std::vector<vtkm::Float64>& values)
{
this->IsoValues = values;
}
//-----------------------------------------------------------------------------
2019-08-18 01:10:57 +00:00
inline vtkm::Float64 Contour::GetIsoValue(vtkm::Id index) const
{
return this->IsoValues[static_cast<std::size_t>(index)];
}
2016-01-19 14:59:31 +00:00
//-----------------------------------------------------------------------------
template <typename T, typename StorageType, typename DerivedPolicy>
2019-08-18 01:10:57 +00:00
inline VTKM_CONT vtkm::cont::DataSet Contour::DoExecute(
const vtkm::cont::DataSet& input,
const vtkm::cont::ArrayHandle<T, StorageType>& field,
2017-05-18 14:29:41 +00:00
const vtkm::filter::FieldMetadata& fieldMeta,
const vtkm::filter::PolicyBase<DerivedPolicy>& policy)
2016-01-19 14:59:31 +00:00
{
2017-05-18 14:29:41 +00:00
if (fieldMeta.IsPointField() == false)
2016-01-19 14:59:31 +00:00
{
throw vtkm::cont::ErrorFilterExecution("Point field expected.");
2016-01-19 14:59:31 +00:00
}
2017-05-18 14:29:41 +00:00
if (this->IsoValues.size() == 0)
{
throw vtkm::cont::ErrorFilterExecution("No iso-values provided.");
}
// Check the fields of the dataset to see what kinds of fields are present so
// we can free the mapping arrays that won't be needed. A point field must
// exist for this algorithm, so just check cells.
const vtkm::Id numFields = input.GetNumberOfFields();
bool hasCellFields = false;
for (vtkm::Id fieldIdx = 0; fieldIdx < numFields && !hasCellFields; ++fieldIdx)
{
auto f = input.GetField(fieldIdx);
hasCellFields = f.IsFieldCell();
}
2016-01-19 14:59:31 +00:00
//get the cells and coordinates of the dataset
const vtkm::cont::DynamicCellSet& cells = input.GetCellSet();
2016-01-19 14:59:31 +00:00
const vtkm::cont::CoordinateSystem& coords =
2017-05-18 14:29:41 +00:00
input.GetCoordinateSystem(this->GetActiveCoordinateSystemIndex());
2016-01-19 14:59:31 +00:00
using Vec3HandleType = vtkm::cont::ArrayHandle<vtkm::Vec3f>;
Vec3HandleType vertices;
2016-01-19 14:59:31 +00:00
Vec3HandleType normals;
vtkm::cont::DataSet output;
2017-05-18 14:29:41 +00:00
vtkm::cont::CellSetSingleType<> outputCells;
2016-01-19 14:59:31 +00:00
std::vector<T> ivalues(this->IsoValues.size());
2017-05-18 14:29:41 +00:00
for (std::size_t i = 0; i < ivalues.size(); ++i)
{
ivalues[i] = static_cast<T>(this->IsoValues[i]);
}
//not sold on this as we have to generate more signatures for the
//worklet with the design
//But I think we should get this to compile before we tinker with
//a more efficient api
bool generateHighQualityNormals = IsCellSetStructured(cells)
? !this->ComputeFastNormalsForStructured
: !this->ComputeFastNormalsForUnstructured;
if (this->GenerateNormals && generateHighQualityNormals)
2016-01-19 14:59:31 +00:00
{
outputCells = this->Worklet.Run(&ivalues[0],
static_cast<vtkm::Id>(ivalues.size()),
vtkm::filter::ApplyPolicy(cells, policy),
coords.GetData(),
field,
vertices,
normals);
2016-01-19 14:59:31 +00:00
}
else
{
outputCells = this->Worklet.Run(&ivalues[0],
static_cast<vtkm::Id>(ivalues.size()),
vtkm::filter::ApplyPolicy(cells, policy),
coords.GetData(),
field,
vertices);
2016-01-19 14:59:31 +00:00
}
2017-05-18 14:29:41 +00:00
if (this->GenerateNormals)
2016-01-19 14:59:31 +00:00
{
if (!generateHighQualityNormals)
{
Vec3HandleType faceNormals;
vtkm::worklet::FacetedSurfaceNormals faceted;
faceted.Run(outputCells, vertices, faceNormals);
vtkm::worklet::SmoothSurfaceNormals smooth;
smooth.Run(outputCells, faceNormals, normals);
}
output.AddField(vtkm::cont::make_FieldPoint(this->NormalArrayName, normals));
2016-01-19 14:59:31 +00:00
}
if (this->AddInterpolationEdgeIds)
{
vtkm::cont::Field interpolationEdgeIdsField(InterpolationEdgeIdsArrayName,
vtkm::cont::Field::Association::POINTS,
this->Worklet.GetInterpolationEdgeIds());
output.AddField(interpolationEdgeIdsField);
2016-01-19 14:59:31 +00:00
}
//assign the connectivity to the cell set
output.SetCellSet(outputCells);
2016-01-19 14:59:31 +00:00
//add the coordinates to the output dataset
vtkm::cont::CoordinateSystem outputCoords("coordinates", vertices);
2017-05-18 14:29:41 +00:00
output.AddCoordinateSystem(outputCoords);
2016-01-19 14:59:31 +00:00
if (!hasCellFields)
{
this->Worklet.ReleaseCellMapArrays();
}
return output;
2016-01-19 14:59:31 +00:00
}
//-----------------------------------------------------------------------------
template <typename T, typename StorageType, typename DerivedPolicy>
2019-08-18 01:10:57 +00:00
inline VTKM_CONT bool Contour::DoMapField(vtkm::cont::DataSet& result,
const vtkm::cont::ArrayHandle<T, StorageType>& input,
const vtkm::filter::FieldMetadata& fieldMeta,
const vtkm::filter::PolicyBase<DerivedPolicy>&)
2016-01-19 14:59:31 +00:00
{
vtkm::cont::ArrayHandle<T> fieldArray;
if (fieldMeta.IsPointField())
{
fieldArray = this->Worklet.ProcessPointField(input);
}
else if (fieldMeta.IsCellField())
{
fieldArray = this->Worklet.ProcessCellField(input);
}
else
2016-01-19 14:59:31 +00:00
{
return false;
}
//use the same meta data as the input so we get the same field name, etc.
result.AddField(fieldMeta.AsField(fieldArray));
2016-01-19 14:59:31 +00:00
return true;
}
}
} // namespace vtkm::filter