2019-04-15 23:24:21 +00:00
|
|
|
//============================================================================
|
2015-12-30 17:34:04 +00:00
|
|
|
// 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.
|
2019-04-15 23:24:21 +00:00
|
|
|
//============================================================================
|
2015-12-30 17:34:04 +00:00
|
|
|
|
|
|
|
#include <vtkm/cont/CellSetStructured.h>
|
2016-04-20 21:41:14 +00:00
|
|
|
#include <vtkm/cont/DataSetBuilderRectilinear.h>
|
|
|
|
#include <vtkm/cont/DynamicCellSet.h>
|
2017-02-06 21:24:36 +00:00
|
|
|
#include <vtkm/cont/testing/MakeTestDataSet.h>
|
2015-12-30 17:34:04 +00:00
|
|
|
#include <vtkm/cont/testing/Testing.h>
|
|
|
|
|
2016-09-02 18:44:30 +00:00
|
|
|
#include <ctime>
|
2016-09-28 14:54:04 +00:00
|
|
|
#include <random>
|
2015-12-30 17:34:04 +00:00
|
|
|
#include <vector>
|
|
|
|
|
2017-05-18 14:29:41 +00:00
|
|
|
namespace DataSetBuilderRectilinearNamespace
|
|
|
|
{
|
2015-12-30 17:34:04 +00:00
|
|
|
|
2016-09-28 14:54:04 +00:00
|
|
|
std::mt19937 g_RandomGenerator;
|
2016-01-14 22:15:19 +00:00
|
|
|
|
2017-05-26 17:53:28 +00:00
|
|
|
void ValidateDataSet(const vtkm::cont::DataSet& ds,
|
|
|
|
int dim,
|
|
|
|
vtkm::Id numPoints,
|
|
|
|
vtkm::Id numCells,
|
2017-05-18 14:29:41 +00:00
|
|
|
const vtkm::Bounds& bounds)
|
2015-12-30 17:34:04 +00:00
|
|
|
{
|
2017-02-07 16:16:07 +00:00
|
|
|
//Verify basics..
|
2019-08-13 17:08:54 +00:00
|
|
|
|
2017-05-18 14:29:41 +00:00
|
|
|
VTKM_TEST_ASSERT(ds.GetNumberOfFields() == 2, "Wrong number of fields.");
|
|
|
|
VTKM_TEST_ASSERT(ds.GetNumberOfCoordinateSystems() == 1, "Wrong number of coordinate systems.");
|
2019-08-13 17:08:54 +00:00
|
|
|
VTKM_TEST_ASSERT(ds.GetNumberOfPoints() == numPoints, "Wrong number of coordinates.");
|
|
|
|
VTKM_TEST_ASSERT(ds.GetNumberOfCells() == numCells, "Wrong number of cells.");
|
2017-02-07 16:16:07 +00:00
|
|
|
|
|
|
|
// test various field-getting methods and associations
|
|
|
|
try
|
|
|
|
{
|
2019-08-13 14:10:52 +00:00
|
|
|
ds.GetCellField("cellvar");
|
2017-02-07 16:16:07 +00:00
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
2018-05-18 20:10:15 +00:00
|
|
|
VTKM_TEST_FAIL("Failed to get field 'cellvar' with Association::CELL_SET.");
|
2017-02-07 16:16:07 +00:00
|
|
|
}
|
2017-02-06 21:24:36 +00:00
|
|
|
|
2017-02-07 16:16:07 +00:00
|
|
|
try
|
|
|
|
{
|
2019-08-13 14:10:52 +00:00
|
|
|
ds.GetPointField("pointvar");
|
2017-02-07 16:16:07 +00:00
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
VTKM_TEST_FAIL("Failed to get field 'pointvar' with ASSOC_POINT_SET.");
|
|
|
|
}
|
|
|
|
|
|
|
|
//Make sure the bounds are correct.
|
2017-02-24 21:39:37 +00:00
|
|
|
vtkm::Bounds res = ds.GetCoordinateSystem().GetBounds();
|
2017-05-18 14:29:41 +00:00
|
|
|
VTKM_TEST_ASSERT(test_equal(bounds, res), "Bounds of coordinates do not match");
|
2017-05-24 18:25:57 +00:00
|
|
|
if (dim == 1)
|
|
|
|
{
|
|
|
|
vtkm::cont::CellSetStructured<1> cellSet;
|
2019-08-13 17:08:54 +00:00
|
|
|
ds.GetCellSet().CopyTo(cellSet);
|
2017-05-24 18:25:57 +00:00
|
|
|
vtkm::IdComponent shape = cellSet.GetCellShape();
|
|
|
|
VTKM_TEST_ASSERT(shape == vtkm::CELL_SHAPE_LINE, "Wrong element type");
|
|
|
|
}
|
|
|
|
else if (dim == 2)
|
2017-02-07 16:16:07 +00:00
|
|
|
{
|
|
|
|
vtkm::cont::CellSetStructured<2> cellSet;
|
2019-08-13 17:08:54 +00:00
|
|
|
ds.GetCellSet().CopyTo(cellSet);
|
2017-02-07 16:16:07 +00:00
|
|
|
vtkm::IdComponent shape = cellSet.GetCellShape();
|
|
|
|
VTKM_TEST_ASSERT(shape == vtkm::CELL_SHAPE_QUAD, "Wrong element type");
|
|
|
|
}
|
|
|
|
else if (dim == 3)
|
|
|
|
{
|
|
|
|
vtkm::cont::CellSetStructured<3> cellSet;
|
2019-08-13 17:08:54 +00:00
|
|
|
ds.GetCellSet().CopyTo(cellSet);
|
2017-02-07 16:16:07 +00:00
|
|
|
vtkm::IdComponent shape = cellSet.GetCellShape();
|
|
|
|
VTKM_TEST_ASSERT(shape == vtkm::CELL_SHAPE_HEXAHEDRON, "Wrong element type");
|
|
|
|
}
|
2015-12-30 17:34:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
2017-05-18 14:29:41 +00:00
|
|
|
void FillArray(std::vector<T>& arr, vtkm::Id size, vtkm::IdComponent fillMethod)
|
2015-12-30 17:34:04 +00:00
|
|
|
{
|
2016-01-14 22:15:19 +00:00
|
|
|
arr.resize(static_cast<std::size_t>(size));
|
2017-05-24 18:25:57 +00:00
|
|
|
arr[0] = T(0);
|
|
|
|
for (size_t i = 1; i < static_cast<std::size_t>(size); i++)
|
2016-01-14 22:15:19 +00:00
|
|
|
{
|
2016-01-18 20:04:18 +00:00
|
|
|
T xi = static_cast<T>(i);
|
2016-01-14 22:15:19 +00:00
|
|
|
|
|
|
|
switch (fillMethod)
|
2015-12-30 17:34:04 +00:00
|
|
|
{
|
2017-05-18 14:29:41 +00:00
|
|
|
case 0:
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
xi /= static_cast<vtkm::Float32>(size - 1);
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
xi *= 2;
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
xi *= 0.1f;
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
xi *= xi;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
VTKM_TEST_FAIL("Bad internal test state: invalid fill method.");
|
2015-12-30 17:34:04 +00:00
|
|
|
}
|
2016-01-14 22:15:19 +00:00
|
|
|
arr[i] = xi;
|
|
|
|
}
|
2015-12-30 17:34:04 +00:00
|
|
|
}
|
|
|
|
|
2016-01-07 15:23:52 +00:00
|
|
|
template <typename T>
|
2017-05-18 14:29:41 +00:00
|
|
|
void RectilinearTests()
|
2015-12-30 17:34:04 +00:00
|
|
|
{
|
2016-01-14 22:15:19 +00:00
|
|
|
const vtkm::Id NUM_TRIALS = 10;
|
|
|
|
const vtkm::Id MAX_DIM_SIZE = 20;
|
|
|
|
const vtkm::Id NUM_FILL_METHODS = 5;
|
|
|
|
|
|
|
|
vtkm::cont::DataSetBuilderRectilinear dataSetBuilder;
|
2016-01-14 21:01:28 +00:00
|
|
|
|
2017-05-24 18:25:57 +00:00
|
|
|
std::uniform_int_distribution<vtkm::Id> randomDim(1, MAX_DIM_SIZE);
|
2017-05-18 14:29:41 +00:00
|
|
|
std::uniform_int_distribution<vtkm::IdComponent> randomFill(0, NUM_FILL_METHODS - 1);
|
2016-01-14 21:01:28 +00:00
|
|
|
|
2016-01-14 22:15:19 +00:00
|
|
|
for (vtkm::Id trial = 0; trial < NUM_TRIALS; trial++)
|
2016-01-14 21:01:28 +00:00
|
|
|
{
|
2016-01-14 22:15:19 +00:00
|
|
|
std::cout << "Trial " << trial << std::endl;
|
|
|
|
|
2020-07-16 16:32:32 +00:00
|
|
|
vtkm::cont::DataSet dataSet;
|
|
|
|
|
2017-05-26 17:53:28 +00:00
|
|
|
vtkm::Id3 dimensions(
|
|
|
|
randomDim(g_RandomGenerator), randomDim(g_RandomGenerator), randomDim(g_RandomGenerator));
|
2016-01-14 22:15:19 +00:00
|
|
|
std::cout << "Dimensions: " << dimensions << std::endl;
|
|
|
|
|
|
|
|
vtkm::IdComponent fillMethodX = randomFill(g_RandomGenerator);
|
|
|
|
vtkm::IdComponent fillMethodY = randomFill(g_RandomGenerator);
|
|
|
|
vtkm::IdComponent fillMethodZ = randomFill(g_RandomGenerator);
|
2017-05-18 14:29:41 +00:00
|
|
|
std::cout << "Fill methods: [" << fillMethodX << "," << fillMethodY << "," << fillMethodZ << "]"
|
|
|
|
<< std::endl;
|
2016-01-14 22:15:19 +00:00
|
|
|
|
|
|
|
std::vector<T> xCoordinates;
|
|
|
|
std::vector<T> yCoordinates;
|
|
|
|
std::vector<T> zCoordinates;
|
|
|
|
FillArray(xCoordinates, dimensions[0], fillMethodX);
|
|
|
|
FillArray(yCoordinates, dimensions[1], fillMethodY);
|
|
|
|
FillArray(zCoordinates, dimensions[2], fillMethodZ);
|
|
|
|
|
2017-05-24 18:25:57 +00:00
|
|
|
vtkm::Id numPoints = 1, numCells = 1;
|
2017-05-18 14:29:41 +00:00
|
|
|
vtkm::Bounds bounds(0, 0, 0, 0, 0, 0);
|
2017-05-24 18:25:57 +00:00
|
|
|
int ndims = 0;
|
2016-09-23 13:00:40 +00:00
|
|
|
|
2017-05-24 18:25:57 +00:00
|
|
|
std::cout << "1D parameters" << std::endl;
|
|
|
|
bounds.X = vtkm::Range(xCoordinates.front(), xCoordinates.back());
|
|
|
|
numPoints *= dimensions[0];
|
|
|
|
if (dimensions[0] > 1)
|
2017-02-07 16:16:07 +00:00
|
|
|
{
|
2017-05-24 18:25:57 +00:00
|
|
|
numCells = dimensions[0] - 1;
|
|
|
|
ndims += 1;
|
2017-02-07 16:16:07 +00:00
|
|
|
}
|
2017-05-24 18:25:57 +00:00
|
|
|
if (ndims)
|
2017-02-07 16:16:07 +00:00
|
|
|
{
|
2017-05-24 18:25:57 +00:00
|
|
|
std::vector<T> varP1D(static_cast<unsigned long>(numPoints));
|
|
|
|
for (unsigned long i = 0; i < static_cast<unsigned long>(numPoints); i++)
|
|
|
|
{
|
2018-01-08 21:53:33 +00:00
|
|
|
float fi = static_cast<float>(i);
|
|
|
|
varP1D[i] = static_cast<T>(fi * 1.1f);
|
2017-05-24 18:25:57 +00:00
|
|
|
}
|
|
|
|
std::vector<T> varC1D(static_cast<unsigned long>(numCells));
|
|
|
|
for (unsigned long i = 0; i < static_cast<unsigned long>(numCells); i++)
|
|
|
|
{
|
2018-01-08 21:53:33 +00:00
|
|
|
float fi = static_cast<float>(i);
|
|
|
|
varC1D[i] = static_cast<T>(fi * 1.1f);
|
2017-05-24 18:25:57 +00:00
|
|
|
}
|
|
|
|
std::cout << " Create with std::vector" << std::endl;
|
|
|
|
dataSet = dataSetBuilder.Create(xCoordinates);
|
2020-05-27 19:27:47 +00:00
|
|
|
dataSet.AddPointField("pointvar", varP1D);
|
|
|
|
dataSet.AddCellField("cellvar", varC1D);
|
2017-05-24 18:25:57 +00:00
|
|
|
ValidateDataSet(dataSet, ndims, numPoints, numCells, bounds);
|
2017-02-07 16:16:07 +00:00
|
|
|
}
|
|
|
|
|
2017-05-24 18:25:57 +00:00
|
|
|
std::cout << "2D parameters" << std::endl;
|
|
|
|
bounds.Y = vtkm::Range(yCoordinates.front(), yCoordinates.back());
|
|
|
|
numPoints *= dimensions[1];
|
|
|
|
if (dimensions[1] > 1)
|
2017-02-07 16:16:07 +00:00
|
|
|
{
|
2017-05-24 18:25:57 +00:00
|
|
|
numCells *= dimensions[1] - 1;
|
|
|
|
ndims += 1;
|
2017-02-07 16:16:07 +00:00
|
|
|
}
|
2017-05-24 18:25:57 +00:00
|
|
|
if (ndims)
|
2017-02-07 16:16:07 +00:00
|
|
|
{
|
2017-05-24 18:25:57 +00:00
|
|
|
std::vector<T> varP2D(static_cast<unsigned long>(numPoints));
|
|
|
|
for (unsigned long i = 0; i < static_cast<unsigned long>(numPoints); i++)
|
|
|
|
{
|
2018-01-08 21:53:33 +00:00
|
|
|
float fi = static_cast<float>(i);
|
|
|
|
varP2D[i] = static_cast<T>(fi * 1.1f);
|
2017-05-24 18:25:57 +00:00
|
|
|
}
|
|
|
|
std::vector<T> varC2D(static_cast<unsigned long>(numCells));
|
|
|
|
for (unsigned long i = 0; i < static_cast<unsigned long>(numCells); i++)
|
|
|
|
{
|
2018-01-08 21:53:33 +00:00
|
|
|
float fi = static_cast<float>(i);
|
|
|
|
varC2D[i] = static_cast<T>(fi * 1.1f);
|
2017-05-24 18:25:57 +00:00
|
|
|
}
|
|
|
|
std::cout << " Create with std::vector" << std::endl;
|
|
|
|
dataSet = dataSetBuilder.Create(xCoordinates, yCoordinates);
|
2020-05-27 19:27:47 +00:00
|
|
|
dataSet.AddPointField("pointvar", varP2D);
|
|
|
|
dataSet.AddCellField("cellvar", varC2D);
|
2017-05-24 18:25:57 +00:00
|
|
|
ValidateDataSet(dataSet, ndims, numPoints, numCells, bounds);
|
2016-01-14 22:15:19 +00:00
|
|
|
|
2017-05-24 18:25:57 +00:00
|
|
|
std::cout << " Create with C array" << std::endl;
|
2017-05-26 17:53:28 +00:00
|
|
|
dataSet = dataSetBuilder.Create(
|
2020-07-20 16:41:20 +00:00
|
|
|
dimensions[0], dimensions[1], xCoordinates.data(), yCoordinates.data());
|
|
|
|
dataSet.AddPointField("pointvar", varP2D.data(), numPoints);
|
|
|
|
dataSet.AddCellField("cellvar", varC2D.data(), numCells);
|
2017-05-24 18:25:57 +00:00
|
|
|
ValidateDataSet(dataSet, ndims, numPoints, numCells, bounds);
|
2016-01-14 22:15:19 +00:00
|
|
|
|
2017-05-24 18:25:57 +00:00
|
|
|
std::cout << " Create with ArrayHandle" << std::endl;
|
2020-07-16 16:32:32 +00:00
|
|
|
dataSet =
|
|
|
|
dataSetBuilder.Create(vtkm::cont::make_ArrayHandle(xCoordinates, vtkm::CopyFlag::Off),
|
|
|
|
vtkm::cont::make_ArrayHandle(yCoordinates, vtkm::CopyFlag::Off));
|
|
|
|
dataSet.AddPointField("pointvar", vtkm::cont::make_ArrayHandle(varP2D, vtkm::CopyFlag::Off));
|
|
|
|
dataSet.AddCellField("cellvar", vtkm::cont::make_ArrayHandle(varC2D, vtkm::CopyFlag::Off));
|
2017-05-24 18:25:57 +00:00
|
|
|
ValidateDataSet(dataSet, ndims, numPoints, numCells, bounds);
|
|
|
|
}
|
2016-01-14 22:15:19 +00:00
|
|
|
|
2017-05-24 18:25:57 +00:00
|
|
|
std::cout << "3D parameters" << std::endl;
|
|
|
|
bounds.Z = vtkm::Range(zCoordinates.front(), zCoordinates.back());
|
2016-01-14 22:15:19 +00:00
|
|
|
numPoints *= dimensions[2];
|
2017-05-24 18:25:57 +00:00
|
|
|
if (dimensions[2] > 1)
|
2017-02-07 16:16:07 +00:00
|
|
|
{
|
2017-05-24 18:25:57 +00:00
|
|
|
numCells *= dimensions[2] - 1;
|
|
|
|
ndims += 1;
|
2017-02-07 16:16:07 +00:00
|
|
|
}
|
2017-05-24 18:25:57 +00:00
|
|
|
if (ndims)
|
2017-02-07 16:16:07 +00:00
|
|
|
{
|
2017-05-24 18:25:57 +00:00
|
|
|
std::vector<T> varP3D(static_cast<unsigned long>(numPoints));
|
|
|
|
for (unsigned long i = 0; i < static_cast<unsigned long>(numPoints); i++)
|
|
|
|
{
|
2018-01-08 21:53:33 +00:00
|
|
|
float fi = static_cast<float>(i);
|
|
|
|
varP3D[i] = static_cast<T>(fi * 1.1f);
|
2017-05-24 18:25:57 +00:00
|
|
|
}
|
|
|
|
std::vector<T> varC3D(static_cast<unsigned long>(numCells));
|
|
|
|
for (unsigned long i = 0; i < static_cast<unsigned long>(numCells); i++)
|
|
|
|
{
|
2018-01-08 21:53:33 +00:00
|
|
|
float fi = static_cast<float>(i);
|
|
|
|
varC3D[i] = static_cast<T>(fi * 1.1f);
|
2017-05-24 18:25:57 +00:00
|
|
|
}
|
2016-01-14 22:15:19 +00:00
|
|
|
|
2017-05-24 18:25:57 +00:00
|
|
|
std::cout << " Create with std::vector" << std::endl;
|
|
|
|
dataSet = dataSetBuilder.Create(xCoordinates, yCoordinates, zCoordinates);
|
2020-05-27 19:27:47 +00:00
|
|
|
dataSet.AddPointField("pointvar", varP3D);
|
|
|
|
dataSet.AddCellField("cellvar", varC3D);
|
2017-05-24 18:25:57 +00:00
|
|
|
ValidateDataSet(dataSet, ndims, numPoints, numCells, bounds);
|
2016-01-14 22:15:19 +00:00
|
|
|
|
2017-05-24 18:25:57 +00:00
|
|
|
std::cout << " Create with C array" << std::endl;
|
2017-05-26 17:53:28 +00:00
|
|
|
dataSet = dataSetBuilder.Create(dimensions[0],
|
|
|
|
dimensions[1],
|
|
|
|
dimensions[2],
|
2020-07-20 16:41:20 +00:00
|
|
|
xCoordinates.data(),
|
|
|
|
yCoordinates.data(),
|
|
|
|
zCoordinates.data());
|
2020-07-16 16:32:32 +00:00
|
|
|
dataSet.AddPointField("pointvar", vtkm::cont::make_ArrayHandle(varP3D, vtkm::CopyFlag::Off));
|
|
|
|
dataSet.AddCellField("cellvar", vtkm::cont::make_ArrayHandle(varC3D, vtkm::CopyFlag::Off));
|
2017-05-24 18:25:57 +00:00
|
|
|
ValidateDataSet(dataSet, ndims, numPoints, numCells, bounds);
|
2016-01-14 22:15:19 +00:00
|
|
|
|
2017-05-24 18:25:57 +00:00
|
|
|
std::cout << " Create with ArrayHandle" << std::endl;
|
2020-07-16 16:32:32 +00:00
|
|
|
dataSet =
|
|
|
|
dataSetBuilder.Create(vtkm::cont::make_ArrayHandle(xCoordinates, vtkm::CopyFlag::Off),
|
|
|
|
vtkm::cont::make_ArrayHandle(yCoordinates, vtkm::CopyFlag::Off),
|
|
|
|
vtkm::cont::make_ArrayHandle(zCoordinates, vtkm::CopyFlag::Off));
|
|
|
|
dataSet.AddPointField("pointvar", vtkm::cont::make_ArrayHandle(varP3D, vtkm::CopyFlag::Off));
|
|
|
|
dataSet.AddCellField("cellvar", vtkm::cont::make_ArrayHandle(varC3D, vtkm::CopyFlag::Off));
|
2017-05-24 18:25:57 +00:00
|
|
|
ValidateDataSet(dataSet, ndims, numPoints, numCells, bounds);
|
|
|
|
}
|
2016-01-14 21:01:28 +00:00
|
|
|
}
|
2015-12-30 17:34:04 +00:00
|
|
|
}
|
|
|
|
|
2017-05-18 14:29:41 +00:00
|
|
|
void TestDataSetBuilderRectilinear()
|
2016-01-07 15:23:52 +00:00
|
|
|
{
|
2016-09-02 18:44:30 +00:00
|
|
|
vtkm::UInt32 seed = static_cast<vtkm::UInt32>(std::time(nullptr));
|
2016-01-14 22:15:19 +00:00
|
|
|
std::cout << "Seed: " << seed << std::endl;
|
|
|
|
g_RandomGenerator.seed(seed);
|
|
|
|
|
|
|
|
std::cout << "======== Float32 ==========================" << std::endl;
|
|
|
|
RectilinearTests<vtkm::Float32>();
|
|
|
|
std::cout << "======== Float64 ==========================" << std::endl;
|
|
|
|
RectilinearTests<vtkm::Float64>();
|
2016-01-07 15:23:52 +00:00
|
|
|
}
|
|
|
|
|
2015-12-30 17:34:04 +00:00
|
|
|
} // namespace DataSetBuilderRectilinearNamespace
|
|
|
|
|
2019-01-01 22:19:02 +00:00
|
|
|
int UnitTestDataSetBuilderRectilinear(int argc, char* argv[])
|
2015-12-30 17:34:04 +00:00
|
|
|
{
|
2017-05-18 14:29:41 +00:00
|
|
|
using namespace DataSetBuilderRectilinearNamespace;
|
2019-01-01 22:19:02 +00:00
|
|
|
return vtkm::cont::testing::Testing::Run(TestDataSetBuilderRectilinear, argc, argv);
|
2015-12-30 17:34:04 +00:00
|
|
|
}
|