vtk-m/vtkm/worklet/testing/UnitTestThresholdPoints.cxx

195 lines
5.7 KiB
C++
Raw Normal View History

2017-03-07 21:03:22 +00:00
//============================================================================
// Copyright (c) Kitware, Inc.
// All rights reserved.
// See LICENSE.txt for details.
2019-04-15 23:24:21 +00:00
//
2017-03-07 21:03: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.
//============================================================================
#include <vtkm/worklet/ThresholdPoints.h>
#include <vtkm/cont/testing/MakeTestDataSet.h>
#include <vtkm/cont/testing/Testing.h>
#include <vtkm/cont/ArrayPortalToIterators.h>
#include <vtkm/cont/CellSet.h>
#include <algorithm>
#include <iostream>
#include <vector>
2017-05-18 14:29:41 +00:00
namespace
{
2017-03-07 21:03:22 +00:00
// Predicate for values less than minimum
class ValuesBelow
{
public:
VTKM_CONT
2017-05-18 14:29:41 +00:00
ValuesBelow(const vtkm::FloatDefault& value)
: Value(value)
{
}
2017-03-07 21:03:22 +00:00
2017-05-18 14:29:41 +00:00
template <typename ScalarType>
VTKM_EXEC bool operator()(const ScalarType& value) const
2017-03-07 21:03:22 +00:00
{
return static_cast<vtkm::FloatDefault>(value) <= this->Value;
2017-03-07 21:03:22 +00:00
}
private:
vtkm::FloatDefault Value;
2017-03-07 21:03:22 +00:00
};
// Predicate for values greater than maximum
class ValuesAbove
{
public:
VTKM_CONT
2017-05-18 14:29:41 +00:00
ValuesAbove(const vtkm::FloatDefault& value)
: Value(value)
{
}
2017-03-07 21:03:22 +00:00
2017-05-18 14:29:41 +00:00
template <typename ScalarType>
VTKM_EXEC bool operator()(const ScalarType& value) const
2017-03-07 21:03:22 +00:00
{
return static_cast<vtkm::FloatDefault>(value) >= this->Value;
2017-03-07 21:03:22 +00:00
}
private:
vtkm::FloatDefault Value;
2017-03-07 21:03:22 +00:00
};
// Predicate for values between minimum and maximum
class ValuesBetween
{
public:
VTKM_CONT
2017-05-18 14:29:41 +00:00
ValuesBetween(const vtkm::FloatDefault& lower, const vtkm::FloatDefault& upper)
: Lower(lower)
, Upper(upper)
{
}
template <typename ScalarType>
VTKM_EXEC bool operator()(const ScalarType& value) const
2017-03-07 21:03:22 +00:00
{
return static_cast<vtkm::FloatDefault>(value) >= this->Lower &&
2017-05-18 14:29:41 +00:00
static_cast<vtkm::FloatDefault>(value) <= this->Upper;
2017-03-07 21:03:22 +00:00
}
private:
2017-03-28 22:07:56 +00:00
vtkm::FloatDefault Lower;
vtkm::FloatDefault Upper;
2017-03-07 21:03:22 +00:00
};
using vtkm::cont::testing::MakeTestDataSet;
class TestingThresholdPoints
{
public:
void TestUniform2D() const
{
std::cout << "Testing threshold on 2D uniform dataset" << std::endl;
2018-02-22 13:29:13 +00:00
using OutCellSetType = vtkm::cont::CellSetSingleType<>;
2017-03-07 21:03:22 +00:00
vtkm::cont::DataSet dataset = MakeTestDataSet().Make2DUniformDataSet1();
// Output dataset contains input coordinate system and point data
vtkm::cont::DataSet outDataSet;
outDataSet.AddCoordinateSystem(dataset.GetCoordinateSystem(0));
outDataSet.AddField(dataset.GetField("pointvar"));
2017-03-07 21:03:22 +00:00
// Output dataset gets new cell set of points that meet threshold predicate
vtkm::worklet::ThresholdPoints threshold;
OutCellSetType outCellSet;
outCellSet =
threshold.Run(dataset.GetCellSet(),
dataset.GetField("pointvar")
.GetData()
.ResetTypes(vtkm::TypeListFieldScalar{}, VTKM_DEFAULT_STORAGE_LIST{}),
ValuesBetween(40.0f, 71.0f));
outDataSet.SetCellSet(outCellSet);
2017-03-07 21:03:22 +00:00
2017-05-18 14:29:41 +00:00
VTKM_TEST_ASSERT(test_equal(outCellSet.GetNumberOfCells(), 11),
"Wrong result for ThresholdPoints");
vtkm::cont::Field pointField = outDataSet.GetField("pointvar");
vtkm::cont::ArrayHandle<vtkm::Float32> pointFieldArray;
pointField.GetData().AsArrayHandle(pointFieldArray);
VTKM_TEST_ASSERT(pointFieldArray.ReadPortal().Get(12) == 50.0f, "Wrong point field data");
2017-03-07 21:03:22 +00:00
}
void TestUniform3D() const
{
std::cout << "Testing threshold on 3D uniform dataset" << std::endl;
2018-02-22 13:29:13 +00:00
using OutCellSetType = vtkm::cont::CellSetSingleType<>;
2017-03-07 21:03:22 +00:00
vtkm::cont::DataSet dataset = MakeTestDataSet().Make3DUniformDataSet1();
// Output dataset contains input coordinate system and point data
vtkm::cont::DataSet outDataSet;
outDataSet.AddCoordinateSystem(dataset.GetCoordinateSystem(0));
outDataSet.AddField(dataset.GetField("pointvar"));
2017-03-07 21:03:22 +00:00
// Output dataset gets new cell set of points that meet threshold predicate
vtkm::worklet::ThresholdPoints threshold;
OutCellSetType outCellSet;
outCellSet =
threshold.Run(dataset.GetCellSet(),
dataset.GetField("pointvar")
.GetData()
.ResetTypes(vtkm::TypeListFieldScalar{}, VTKM_DEFAULT_STORAGE_LIST{}),
ValuesAbove(1.0f));
outDataSet.SetCellSet(outCellSet);
2017-03-07 21:03:22 +00:00
2017-05-18 14:29:41 +00:00
VTKM_TEST_ASSERT(test_equal(outCellSet.GetNumberOfCells(), 27),
"Wrong result for ThresholdPoints");
2017-03-07 21:03:22 +00:00
}
void TestExplicit3D() const
{
std::cout << "Testing threshold on 3D explicit dataset" << std::endl;
2018-02-22 13:29:13 +00:00
using OutCellSetType = vtkm::cont::CellSetSingleType<>;
2017-03-07 21:03:22 +00:00
vtkm::cont::DataSet dataset = MakeTestDataSet().Make3DExplicitDataSet5();
// Output dataset contains input coordinate system and point data
vtkm::cont::DataSet outDataSet;
outDataSet.AddCoordinateSystem(dataset.GetCoordinateSystem(0));
// Output dataset gets new cell set of points that meet threshold predicate
vtkm::worklet::ThresholdPoints threshold;
OutCellSetType outCellSet;
outCellSet =
threshold.Run(dataset.GetCellSet(),
dataset.GetField("pointvar")
.GetData()
.ResetTypes(vtkm::TypeListFieldScalar{}, VTKM_DEFAULT_STORAGE_LIST{}),
ValuesBelow(50.0f));
outDataSet.SetCellSet(outCellSet);
2017-03-07 21:03:22 +00:00
2017-05-18 14:29:41 +00:00
VTKM_TEST_ASSERT(test_equal(outCellSet.GetNumberOfCells(), 6),
"Wrong result for ThresholdPoints");
2017-03-07 21:03:22 +00:00
}
void operator()() const
{
this->TestUniform2D();
this->TestUniform3D();
this->TestExplicit3D();
}
};
}
int UnitTestThresholdPoints(int argc, char* argv[])
2017-03-07 21:03:22 +00:00
{
return vtkm::cont::testing::Testing::Run(TestingThresholdPoints(), argc, argv);
2017-03-07 21:03:22 +00:00
}