vtk-m/vtkm/filter/field_conversion/testing/UnitTestCellAverageFilter.cxx

131 lines
4.7 KiB
C++
Raw Permalink 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/testing/MakeTestDataSet.h>
2017-05-18 14:51:24 +00:00
#include <vtkm/cont/testing/Testing.h>
2022-01-29 20:12:01 +00:00
#include <vtkm/filter/field_conversion/CellAverage.h>
2016-01-19 14:59:31 +00:00
2017-05-18 14:29:41 +00:00
namespace
{
2016-01-19 14:59:31 +00:00
void TestCellAverageRegular3D()
{
std::cout << "Testing CellAverage Filter on 3D structured data" << std::endl;
2016-01-19 14:59:31 +00:00
vtkm::cont::testing::MakeTestDataSet testDataSet;
vtkm::cont::DataSet dataSet = testDataSet.Make3DUniformDataSet0();
2022-01-29 20:12:01 +00:00
vtkm::filter::field_conversion::CellAverage cellAverage;
cellAverage.SetOutputFieldName("avgvals");
cellAverage.SetActiveField("pointvar");
vtkm::cont::DataSet result = cellAverage.Execute(dataSet);
2016-01-19 14:59:31 +00:00
VTKM_TEST_ASSERT(result.HasCellField("avgvals") == true, "Result field not present.");
2016-01-19 14:59:31 +00:00
vtkm::cont::ArrayHandle<vtkm::Float32> resultArrayHandle;
result.GetCellField("avgvals").GetData().AsArrayHandle(resultArrayHandle);
2016-01-19 14:59:31 +00:00
{
vtkm::Float32 expected[4] = { 60.1875f, 70.2125f, 120.3375f, 130.3625f };
for (vtkm::Id i = 0; i < 4; ++i)
2016-01-19 14:59:31 +00:00
{
VTKM_TEST_ASSERT(test_equal(resultArrayHandle.ReadPortal().Get(i), expected[i]),
2017-05-18 14:29:41 +00:00
"Wrong result for CellAverage worklet on 3D regular data");
2016-01-19 14:59:31 +00:00
}
}
std::cout << "Run again for point coordinates" << std::endl;
cellAverage.SetOutputFieldName("avgpos");
cellAverage.SetUseCoordinateSystemAsField(true);
result = cellAverage.Execute(dataSet);
VTKM_TEST_ASSERT(result.HasCellField("avgpos"), "Result field not present.");
vtkm::cont::ArrayHandle<vtkm::Vec3f> resultPointArray;
vtkm::cont::Field resultPointField = result.GetCellField("avgpos");
resultPointField.GetData().AsArrayHandle(resultPointArray);
{
vtkm::FloatDefault expected[4][3] = {
2017-05-18 14:29:41 +00:00
{ 0.5f, 0.5f, 0.5f }, { 1.5f, 0.5f, 0.5f }, { 0.5f, 0.5f, 1.5f }, { 1.5f, 0.5f, 1.5f }
};
for (vtkm::Id i = 0; i < 4; ++i)
{
vtkm::Vec3f expectedVec(expected[i][0], expected[i][1], expected[i][2]);
vtkm::Vec3f computedVec(resultPointArray.ReadPortal().Get(i));
2017-05-18 14:29:41 +00:00
VTKM_TEST_ASSERT(test_equal(computedVec, expectedVec),
"Wrong result for CellAverage worklet on 3D regular data");
}
}
2016-01-19 14:59:31 +00:00
}
void TestCellAverageRegular2D()
{
std::cout << "Testing CellAverage Filter on 2D structured data" << std::endl;
2016-01-19 14:59:31 +00:00
vtkm::cont::testing::MakeTestDataSet testDataSet;
vtkm::cont::DataSet dataSet = testDataSet.Make2DUniformDataSet0();
2022-01-29 20:12:01 +00:00
vtkm::filter::field_conversion::CellAverage cellAverage;
cellAverage.SetActiveField("pointvar");
2016-01-19 14:59:31 +00:00
vtkm::cont::DataSet result = cellAverage.Execute(dataSet);
2016-01-19 14:59:31 +00:00
// If no name is given, should have the same name as the input.
VTKM_TEST_ASSERT(result.HasPointField("pointvar"), "Field missing.");
vtkm::cont::Field resultField = result.GetCellField("pointvar");
2016-01-19 14:59:31 +00:00
vtkm::cont::ArrayHandle<vtkm::Float32> resultArrayHandle;
resultField.GetData().AsArrayHandle(resultArrayHandle);
vtkm::Float32 expected[2] = { 30.1f, 40.1f };
for (int i = 0; i < 2; ++i)
2016-01-19 14:59:31 +00:00
{
VTKM_TEST_ASSERT(test_equal(resultArrayHandle.ReadPortal().Get(i), expected[i]),
"Wrong result for CellAverage worklet on 2D regular data");
2016-01-19 14:59:31 +00:00
}
}
void TestCellAverageExplicit()
{
std::cout << "Testing CellAverage Filter on Explicit data" << std::endl;
vtkm::cont::testing::MakeTestDataSet testDataSet;
vtkm::cont::DataSet dataSet = testDataSet.Make3DExplicitDataSet0();
2022-01-29 20:12:01 +00:00
vtkm::filter::field_conversion::CellAverage cellAverage;
cellAverage.SetActiveField("pointvar");
2016-01-19 14:59:31 +00:00
vtkm::cont::DataSet result = cellAverage.Execute(dataSet);
2016-01-19 14:59:31 +00:00
// If no name is given, should have the same name as the input.
VTKM_TEST_ASSERT(result.HasCellField("pointvar"), "Field missing.");
2016-01-19 14:59:31 +00:00
vtkm::cont::ArrayHandle<vtkm::Float32> resultArrayHandle;
vtkm::cont::Field resultField = result.GetCellField("pointvar");
resultField.GetData().AsArrayHandle(resultArrayHandle);
vtkm::Float32 expected[2] = { 20.1333f, 35.2f };
for (int i = 0; i < 2; ++i)
2016-01-19 14:59:31 +00:00
{
VTKM_TEST_ASSERT(test_equal(resultArrayHandle.ReadPortal().Get(i), expected[i]),
"Wrong result for CellAverage worklet on 3D regular data");
2016-01-19 14:59:31 +00:00
}
}
void TestCellAverage()
{
TestCellAverageRegular2D();
TestCellAverageRegular3D();
TestCellAverageExplicit();
}
}
int UnitTestCellAverageFilter(int argc, char* argv[])
2016-01-19 14:59:31 +00:00
{
return vtkm::cont::testing::Testing::Run(TestCellAverage, argc, argv);
2016-01-19 14:59:31 +00:00
}