Commit 9e466f8c authored by Sujin Philip's avatar Sujin Philip Committed by Kitware Robot
Browse files

Merge topic 'add-probe'

34bae569 Add Probe worklet and filter
4cd306ea Fix a bug in CellLocator
cf1957ca

 Add make_VecFromPortalPermute

Acked-by: Kitware Robot's avatarKitware Robot <kwrobot@kitware.com>
Acked-by: default avatarRobert Maynard <robert.maynard@kitware.com>
Merge-request: !1064
parents e006744a 34bae569
......@@ -126,6 +126,14 @@ struct VecTraits<vtkm::VecFromPortalPermute<IndexVecType, PortalType>>
}
};
template <typename IndexVecType, typename PortalType>
inline VTKM_EXEC VecFromPortalPermute<IndexVecType, PortalType> make_VecFromPortalPermute(
const IndexVecType* index,
const PortalType& portal)
{
return VecFromPortalPermute<IndexVecType, PortalType>(index, portal);
}
} // namespace vtkm
#endif //vtk_m_VecFromPortalPermute_h
......@@ -97,7 +97,7 @@ public:
"expecting ArrayHandleUniformPointCoordinates for coordinates");
auto cellId3 = static_cast<vtkm::Id3>((point - coords.GetOrigin()) / coords.GetSpacing());
auto cellDim = coords.GetDimensions() - vtkm::Id3(1);
auto cellDim = vtkm::Max(vtkm::Id3(1), coords.GetDimensions() - vtkm::Id3(1));
if (cellId3[0] < 0 || cellId3[0] >= cellDim[0] || cellId3[1] < 0 ||
cellId3[1] >= cellDim[1] || cellId3[2] < 0 || cellId3[2] >= cellDim[2])
{
......
......@@ -46,6 +46,7 @@ set(headers
PointElevation.h
PolicyBase.h
PolicyDefault.h
Probe.h
Result.h
Streamline.h
SurfaceNormals.h
......@@ -81,6 +82,7 @@ set(header_template_sources
NDHistogram.hxx
PointAverage.hxx
PointElevation.hxx
Probe.hxx
Streamline.hxx
SurfaceNormals.hxx
Tetrahedralize.hxx
......
//============================================================================
// 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 2014 National Technology & Engineering Solutions of Sandia, LLC (NTESS).
// Copyright 2014 UT-Battelle, LLC.
// Copyright 2014 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_filter_Probe_h
#define vtk_m_filter_Probe_h
#include <vtkm/filter/FilterDataSet.h>
#include <vtkm/worklet/Probe.h>
namespace vtkm
{
namespace filter
{
class Probe : public vtkm::filter::FilterDataSet<Probe>
{
public:
VTKM_CONT
void SetGeometry(const vtkm::cont::DataSet& geometry);
template <typename DerivedPolicy, typename DeviceAdapter>
VTKM_CONT vtkm::filter::Result DoExecute(const vtkm::cont::DataSet& input,
const vtkm::filter::PolicyBase<DerivedPolicy>& policy,
const DeviceAdapter& device);
//Map a new field onto the resulting dataset after running the filter
//this call is only valid
template <typename T, typename StorageType, typename DerivedPolicy, typename DeviceAdapter>
VTKM_CONT bool DoMapField(vtkm::filter::Result& result,
const vtkm::cont::ArrayHandle<T, StorageType>& input,
const vtkm::filter::FieldMetadata& fieldMeta,
const vtkm::filter::PolicyBase<DerivedPolicy>& policy,
const DeviceAdapter& device);
private:
vtkm::cont::DataSet Geometry;
vtkm::worklet::Probe Worklet;
};
}
} // vtkm::filter
#include <vtkm/filter/Probe.hxx>
#endif // vtk_m_filter_Probe_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 2014 National Technology & Engineering Solutions of Sandia, LLC (NTESS).
// Copyright 2014 UT-Battelle, LLC.
// Copyright 2014 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_filter_Probe_hxx
#define vtk_m_filter_Probe_hxx
namespace vtkm
{
namespace filter
{
VTKM_CONT
inline void Probe::SetGeometry(const vtkm::cont::DataSet& geometry)
{
this->Geometry = vtkm::cont::DataSet();
this->Geometry.AddCellSet(geometry.GetCellSet());
this->Geometry.AddCoordinateSystem(geometry.GetCoordinateSystem());
}
template <typename DerivedPolicy, typename DeviceAdapter>
VTKM_CONT inline vtkm::filter::Result Probe::DoExecute(
const vtkm::cont::DataSet& input,
const vtkm::filter::PolicyBase<DerivedPolicy>& policy,
const DeviceAdapter& device)
{
this->Worklet.Run(
vtkm::filter::ApplyPolicy(input.GetCellSet(this->GetActiveCellSetIndex()), policy),
input.GetCoordinateSystem(this->GetActiveCoordinateSystemIndex()),
this->Geometry.GetCoordinateSystem().GetData(),
device);
auto output = this->Geometry;
auto hpf = this->Worklet.GetHiddenPointsField(device);
auto hcf = this->Worklet.GetHiddenCellsField(
vtkm::filter::ApplyPolicy(output.GetCellSet(), policy), device);
output.AddField(vtkm::cont::Field("HIDDEN", vtkm::cont::Field::ASSOC_POINTS, hpf));
output.AddField(vtkm::cont::Field(
"HIDDEN", vtkm::cont::Field::ASSOC_CELL_SET, output.GetCellSet().GetName(), hcf));
return vtkm::filter::Result(output);
}
template <typename T, typename StorageType, typename DerivedPolicy, typename DeviceAdapter>
VTKM_CONT inline bool Probe::DoMapField(vtkm::filter::Result& result,
const vtkm::cont::ArrayHandle<T, StorageType>& input,
const vtkm::filter::FieldMetadata& fieldMeta,
const vtkm::filter::PolicyBase<DerivedPolicy>&,
const DeviceAdapter& device)
{
if (fieldMeta.IsPointField())
{
auto fieldArray =
this->Worklet.ProcessPointField(input, device, typename DerivedPolicy::AllCellSetList());
result.GetDataSet().AddField(fieldMeta.AsField(fieldArray));
return true;
}
else if (fieldMeta.IsCellField())
{
auto fieldArray = this->Worklet.ProcessCellField(input, device);
result.GetDataSet().AddField(fieldMeta.AsField(fieldArray));
return true;
}
return false;
}
}
} // vtkm::filter
#endif // vtk_m_filter_Probe_hxx
......@@ -40,6 +40,7 @@ set(unit_tests
UnitTestNDHistogramFilter.cxx
UnitTestPointAverageFilter.cxx
UnitTestPointElevationFilter.cxx
UnitTestProbe.cxx
UnitTestStreamlineFilter.cxx
UnitTestSurfaceNormalsFilter.cxx
UnitTestTetrahedralizeFilter.cxx
......
//============================================================================
// 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 2014 National Technology & Engineering Solutions of Sandia, LLC (NTESS).
// Copyright 2014 UT-Battelle, LLC.
// Copyright 2014 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.
//============================================================================
#include <vtkm/filter/Probe.h>
#include <vtkm/cont/DataSetBuilderUniform.h>
#include <vtkm/cont/DataSetFieldAdd.h>
#include <vtkm/cont/testing/Testing.h>
#include <vtkm/worklet/CellDeepCopy.h>
namespace
{
vtkm::cont::DataSet MakeInputDataSet()
{
std::vector<vtkm::Float32> pvec(16), cvec(9);
for (std::size_t i = 0; i < 16; ++i)
{
pvec[i] = static_cast<vtkm::Float32>(i) * 0.3f;
}
for (std::size_t i = 0; i < 9; ++i)
{
cvec[i] = static_cast<vtkm::Float32>(i) * 0.7f;
}
auto input = vtkm::cont::DataSetBuilderUniform::Create(
vtkm::Id2(4, 4), vtkm::make_Vec(0.0f, 0.0f), vtkm::make_Vec(1.0f, 1.0f));
vtkm::cont::DataSetFieldAdd::AddPointField(input, "pointdata", pvec);
vtkm::cont::DataSetFieldAdd::AddCellField(input, "celldata", cvec);
return input;
}
vtkm::cont::DataSet MakeGeometryDataSet()
{
auto geometry = vtkm::cont::DataSetBuilderUniform::Create(
vtkm::Id2(9, 9), vtkm::make_Vec(0.7f, 0.7f), vtkm::make_Vec(0.35f, 0.35f));
return geometry;
}
struct ConvertImpl
{
template <typename DeviceAdapter>
bool operator()(DeviceAdapter device,
const vtkm::cont::DataSet& uds,
vtkm::cont::DataSet& eds) const
{
vtkm::cont::CellSetExplicit<> cs(uds.GetCellSet().GetName());
vtkm::worklet::CellDeepCopy::Run(uds.GetCellSet(), cs, device);
eds.AddCellSet(cs);
vtkm::cont::ArrayHandle<vtkm::Vec<vtkm::FloatDefault, 3>> points;
vtkm::cont::DeviceAdapterAlgorithm<DeviceAdapter>::Copy(uds.GetCoordinateSystem().GetData(),
points);
eds.AddCoordinateSystem(
vtkm::cont::CoordinateSystem(uds.GetCoordinateSystem().GetName(), points));
for (vtkm::IdComponent i = 0; i < uds.GetNumberOfFields(); ++i)
{
eds.AddField(uds.GetField(i));
}
return true;
}
};
vtkm::cont::DataSet ConvertDataSetUniformToExplicit(const vtkm::cont::DataSet& uds)
{
vtkm::cont::DataSet eds;
vtkm::cont::TryExecute(ConvertImpl(), uds, eds);
return eds;
}
const std::vector<vtkm::Float32>& GetExpectedPointData()
{
static std::vector<vtkm::Float32> expected = {
1.05f, 1.155f, 1.26f, 1.365f, 1.47f, 1.575f, 1.68f, 0.0f, 0.0f, 1.47f, 1.575f, 1.68f,
1.785f, 1.89f, 1.995f, 2.1f, 0.0f, 0.0f, 1.89f, 1.995f, 2.1f, 2.205f, 2.31f, 2.415f,
2.52f, 0.0f, 0.0f, 2.31f, 2.415f, 2.52f, 2.625f, 2.73f, 2.835f, 2.94f, 0.0f, 0.0f,
2.73f, 2.835f, 2.94f, 3.045f, 3.15f, 3.255f, 3.36f, 0.0f, 0.0f, 3.15f, 3.255f, 3.36f,
3.465f, 3.57f, 3.675f, 3.78f, 0.0f, 0.0f, 3.57f, 3.675f, 3.78f, 3.885f, 3.99f, 4.095f,
4.2f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f
};
return expected;
}
const std::vector<vtkm::Float32>& GetExpectedCellData()
{
static std::vector<vtkm::Float32> expected = {
0.0f, 0.7f, 0.7f, 0.7f, 1.4f, 1.4f, 1.4f, 0.0f, 0.0f, 2.1f, 2.8f, 2.8f, 2.8f, 3.5f,
3.5f, 3.5f, 0.0f, 0.0f, 2.1f, 2.8f, 2.8f, 2.8f, 3.5f, 3.5f, 3.5f, 0.0f, 0.0f, 2.1f,
2.8f, 2.8f, 2.8f, 3.5f, 3.5f, 3.5f, 0.0f, 0.0f, 4.2f, 4.9f, 4.9f, 4.9f, 5.6f, 5.6f,
5.6f, 0.0f, 0.0f, 4.2f, 4.9f, 4.9f, 4.9f, 5.6f, 5.6f, 5.6f, 0.0f, 0.0f, 4.2f, 4.9f,
4.9f, 4.9f, 5.6f, 5.6f, 5.6f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f
};
return expected;
}
const std::vector<vtkm::UInt8>& GetExpectedHiddenPoints()
{
static std::vector<vtkm::UInt8> expected = { 0, 0, 0, 0, 0, 0, 0, 2, 2, 0, 0, 0, 0, 0, 0, 0, 2,
2, 0, 0, 0, 0, 0, 0, 0, 2, 2, 0, 0, 0, 0, 0, 0, 0,
2, 2, 0, 0, 0, 0, 0, 0, 0, 2, 2, 0, 0, 0, 0, 0, 0,
0, 2, 2, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 };
return expected;
}
const std::vector<vtkm::UInt8>& GetExpectedHiddenCells()
{
static std::vector<vtkm::UInt8> expected = { 0, 0, 0, 0, 0, 0, 2, 2, 0, 0, 0, 0, 0, 0, 2, 2,
0, 0, 0, 0, 0, 0, 2, 2, 0, 0, 0, 0, 0, 0, 2, 2,
0, 0, 0, 0, 0, 0, 2, 2, 0, 0, 0, 0, 0, 0, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 };
return expected;
}
template <typename T>
void TestResultArray(const vtkm::cont::ArrayHandle<T>& result, const std::vector<T>& expected)
{
VTKM_TEST_ASSERT(result.GetNumberOfValues() == static_cast<vtkm::Id>(expected.size()),
"Incorrect field size");
auto portal = result.GetPortalConstControl();
vtkm::Id size = portal.GetNumberOfValues();
for (vtkm::Id i = 0; i < size; ++i)
{
VTKM_TEST_ASSERT(test_equal(portal.Get(i), expected[static_cast<std::size_t>(i)]),
"Incorrect field value");
}
}
class TestProbe
{
private:
using FieldArrayType = vtkm::cont::ArrayHandle<vtkm::Float32>;
using HiddenArrayType = vtkm::cont::ArrayHandle<vtkm::UInt8>;
static void ExplicitToUnifrom()
{
std::cout << "Testing Probe Explicit to Uniform:\n";
auto input = ConvertDataSetUniformToExplicit(MakeInputDataSet());
auto geometry = MakeGeometryDataSet();
vtkm::filter::Probe probe;
probe.SetGeometry(geometry);
auto result = probe.Execute(input);
probe.MapFieldOntoOutput(result, input.GetField("pointdata"));
probe.MapFieldOntoOutput(result, input.GetField("celldata"));
auto output = result.GetDataSet();
TestResultArray(output.GetField("pointdata").GetData().template Cast<FieldArrayType>(),
GetExpectedPointData());
TestResultArray(output.GetField("celldata").GetData().template Cast<FieldArrayType>(),
GetExpectedCellData());
TestResultArray(output.GetPointField("HIDDEN").GetData().template Cast<HiddenArrayType>(),
GetExpectedHiddenPoints());
TestResultArray(output.GetCellField("HIDDEN").GetData().template Cast<HiddenArrayType>(),
GetExpectedHiddenCells());
}
static void UniformToExplict()
{
std::cout << "Testing Probe Uniform to Explicit:\n";
auto input = MakeInputDataSet();
auto geometry = ConvertDataSetUniformToExplicit(MakeGeometryDataSet());
vtkm::filter::Probe probe;
probe.SetGeometry(geometry);
auto result = probe.Execute(input);
probe.MapFieldOntoOutput(result, input.GetField("pointdata"));
probe.MapFieldOntoOutput(result, input.GetField("celldata"));
auto output = result.GetDataSet();
TestResultArray(output.GetField("pointdata").GetData().template Cast<FieldArrayType>(),
GetExpectedPointData());
TestResultArray(output.GetField("celldata").GetData().template Cast<FieldArrayType>(),
GetExpectedCellData());
TestResultArray(output.GetPointField("HIDDEN").GetData().template Cast<HiddenArrayType>(),
GetExpectedHiddenPoints());
TestResultArray(output.GetCellField("HIDDEN").GetData().template Cast<HiddenArrayType>(),
GetExpectedHiddenCells());
}
static void ExplicitToExplict()
{
std::cout << "Testing Probe Explicit to Explicit:\n";
auto input = ConvertDataSetUniformToExplicit(MakeInputDataSet());
auto geometry = ConvertDataSetUniformToExplicit(MakeGeometryDataSet());
vtkm::filter::Probe probe;
probe.SetGeometry(geometry);
auto result = probe.Execute(input);
probe.MapFieldOntoOutput(result, input.GetField("pointdata"));
probe.MapFieldOntoOutput(result, input.GetField("celldata"));
auto output = result.GetDataSet();
TestResultArray(output.GetField("pointdata").GetData().template Cast<FieldArrayType>(),
GetExpectedPointData());
TestResultArray(output.GetField("celldata").GetData().template Cast<FieldArrayType>(),
GetExpectedCellData());
TestResultArray(output.GetPointField("HIDDEN").GetData().template Cast<HiddenArrayType>(),
GetExpectedHiddenPoints());
TestResultArray(output.GetCellField("HIDDEN").GetData().template Cast<HiddenArrayType>(),
GetExpectedHiddenCells());
}
public:
static void Run()
{
ExplicitToUnifrom();
UniformToExplict();
ExplicitToExplict();
}
};
} // anonymous namespace
int UnitTestProbe(int, char* [])
{
return vtkm::cont::testing::Testing::Run(TestProbe::Run);
}
......@@ -54,6 +54,7 @@ set(headers
ParticleAdvection.h
PointAverage.h
PointElevation.h
Probe.h
RemoveUnusedPoints.h
ScalarsToColors.h
ScatterCounting.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 2014 National Technology & Engineering Solutions of Sandia, LLC (NTESS).
// Copyright 2014 UT-Battelle, LLC.
// Copyright 2014 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_Probe_h
#define vtk_m_worklet_Probe_h
#include <vtkm/cont/ArrayHandle.h>
#include <vtkm/cont/CellLocator.h>
#include <vtkm/worklet/DispatcherMapField.h>
#include <vtkm/worklet/DispatcherMapTopology.h>
#include <vtkm/worklet/WorkletMapField.h>
#include <vtkm/worklet/WorkletMapTopology.h>
#include <vtkm/VecFromPortalPermute.h>
namespace vtkm
{
namespace worklet
{
class Probe
{
//============================================================================
private:
template <typename CellSetType>
static vtkm::ListTagBase<CellSetType> GetCellSetListTag(const CellSetType&)
{
return {};
}
template <typename CellSetList>
static CellSetList GetCellSetListTag(const vtkm::cont::DynamicCellSetBase<CellSetList>&)
{
return {};
}
template <typename CellSetType, typename PointsType, typename PointsStorage, typename Device>
void RunImpl(const CellSetType& cells,
const vtkm::cont::CoordinateSystem& coords,
const vtkm::cont::ArrayHandle<PointsType, PointsStorage>& points,
Device device)
{
this->InputCellSet = vtkm::cont::DynamicCellSet(cells);
vtkm::cont::CellLocator locator;
locator.SetCellSet(this->InputCellSet);
locator.SetCoordinates(coords);
locator.Build(device);
locator.FindCells(
points, this->CellIds, this->ParametricCoordinates, device, GetCellSetListTag(cells));
}
//============================================================================
public:
class ProbeUniformPoints : public vtkm::worklet::WorkletMapPointToCell
{
public:
typedef void ControlSignature(CellSetIn cellset,
FieldInPoint<Vec3> coords,
WholeArrayIn<Vec3> points,
WholeArrayOut<IdType> cellIds,
WholeArrayOut<Vec3> parametricCoords);
typedef void ExecutionSignature(InputIndex, CellShape, _2, _3, _4, _5);
using InputDomain = _1;
template <typename CellShapeTag,
typename CoordsVecType,
typename UniformPoints,
typename CellIdsType,
typename ParametricCoordsType>
VTKM_EXEC void operator()(vtkm::Id cellId,
CellShapeTag cellShape,
const CoordsVecType& cellPoints,
const UniformPoints& points,
CellIdsType& cellIds,
ParametricCoordsType& pcoords) const
{
// Compute cell bounds
using CoordsType = typename vtkm::VecTraits<CoordsVecType>::ComponentType;
auto numPoints = vtkm::VecTraits<CoordsVecType>::GetNumberOfComponents(cellPoints);
CoordsType cbmin = cellPoints[0], cbmax = cellPoints[0];
for (vtkm::IdComponent i = 1; i < numPoints; ++i)
{
cbmin = vtkm::Min(cbmin, cellPoints[i]);
cbmax = vtkm::Max(cbmax, cellPoints[i]);
}
// Compute points inside cell bounds
auto portal = points.GetPortal();
auto minp = static_cast<vtkm::Id3>((cbmin - portal.GetOrigin()) / portal.GetSpacing());
auto maxp = static_cast<vtkm::Id3>((cbmax - portal.GetOrigin()) / portal.GetSpacing());
// clamp
minp = vtkm::Max(minp, vtkm::Id3(0));
maxp = vtkm::Min(maxp, portal.GetDimensions() - vtkm::Id3(1));
for (vtkm::Id k = minp[2]; k <= maxp[2]; ++k)
{
for (vtkm::Id j = minp[1]; j <= maxp[1]; ++j)
{
for (vtkm::Id i = minp[0]; i <= maxp[0]; ++i)
{
auto pt = portal.Get(vtkm::Id3(i, j, k));
bool success = false;
auto pc = vtkm::exec::WorldCoordinatesToParametricCoordinates(
cellPoints, pt, cellShape, success, *this);
if (success && vtkm::exec::CellInside(pc, cellShape))
{
auto pointId = i + portal.GetDimensions()[0] * (j + portal.GetDimensions()[1] * k);
cellIds.Set(pointId, cellId);
pcoords.Set(pointId, pc);
}
}
}
}
}
};
private:
template <typename CellSetType, typename Device>
void RunImpl(const CellSetType& cells,
const vtkm::cont::Coord