vtk-m2/vtkm/cont/testing/UnitTestDataSetBuilderExplicit.cxx

271 lines
9.0 KiB
C++
Raw Normal View History

2019-04-15 23:24:21 +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
//============================================================================
#include <vtkm/cont/Algorithm.h>
#include <vtkm/cont/DataSetBuilderExplicit.h>
#include <vtkm/cont/testing/ExplicitTestData.h>
2017-05-18 14:51:24 +00:00
#include <vtkm/cont/testing/MakeTestDataSet.h>
#include <vtkm/cont/testing/Testing.h>
#include <vector>
2017-05-18 14:29:41 +00:00
namespace DataSetBuilderExplicitNamespace
{
using DFA = vtkm::cont::Algorithm;
template <typename T>
2017-05-18 14:29:41 +00:00
vtkm::Bounds ComputeBounds(std::size_t numPoints, const T* coords)
{
vtkm::Bounds bounds;
for (std::size_t i = 0; i < numPoints; i++)
{
2017-05-18 14:29:41 +00:00
bounds.Include(vtkm::Vec<T, 3>(coords[i * 3 + 0], coords[i * 3 + 1], coords[i * 3 + 2]));
}
return bounds;
}
void ValidateDataSet(const vtkm::cont::DataSet& ds,
vtkm::Id numPoints,
vtkm::Id numCells,
2017-05-18 14:29:41 +00:00
const vtkm::Bounds& bounds)
{
//Verify basics..
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.");
VTKM_TEST_ASSERT(ds.GetNumberOfPoints() == numPoints, "Wrong number of coordinates.");
VTKM_TEST_ASSERT(ds.GetNumberOfCells() == numCells, "Wrong number of cells.");
// test various field-getting methods and associations
try
{
ds.GetCellField("cellvar");
}
catch (...)
{
VTKM_TEST_FAIL("Failed to get field 'cellvar' with Association::CELL_SET.");
}
2017-05-18 14:29:41 +00:00
try
{
ds.GetPointField("pointvar");
}
catch (...)
{
VTKM_TEST_FAIL("Failed to get field 'pointvar' with ASSOC_POINT_SET.");
}
//Make sure bounds are correct.
vtkm::Bounds computedBounds = ds.GetCoordinateSystem().GetBounds();
2017-05-18 14:29:41 +00:00
VTKM_TEST_ASSERT(test_equal(bounds, computedBounds), "Bounds of coordinates do not match");
}
template <typename T>
2017-05-18 14:29:41 +00:00
std::vector<T> createVec(std::size_t n, const T* data)
{
std::vector<T> vec(n);
for (std::size_t i = 0; i < n; i++)
{
vec[i] = data[i];
}
return vec;
}
template <typename T>
2017-05-18 14:29:41 +00:00
vtkm::cont::ArrayHandle<T> createAH(std::size_t n, const T* data)
{
Improvements to moving data into ArrayHandle We have made several improvements to adding data into an `ArrayHandle`. ## Moving data from an `std::vector` For numerous reasons, it is convenient to define data in a `std::vector` and then wrap that into an `ArrayHandle`. It is often the case that an `std::vector` is filled and then becomes unused once it is converted to an `ArrayHandle`. In this case, what we really want is to pass the data off to the `ArrayHandle` so that the `ArrayHandle` is now managing the data and not the `std::vector`. C++11 has a mechanism to do this: move semantics. You can now pass variables to functions as an "rvalue" (right-hand value). When something is passed as an rvalue, it can pull state out of that variable and move it somewhere else. `std::vector` implements this movement so that an rvalue can be moved to another `std::vector` without actually copying the data. `make_ArrayHandle` now also takes advantage of this feature to move rvalue `std::vector`s. There is a special form of `make_ArrayHandle` named `make_ArrayHandleMove` that takes an rvalue. There is also a special overload of `make_ArrayHandle` itself that handles an rvalue `vector`. (However, using the explicit move version is better if you want to make sure the data is actually moved.) ## Make `ArrayHandle` from initalizer list A common use case for using `std::vector` (particularly in our unit tests) is to quickly add an initalizer list into an `ArrayHandle`. Now you can by simply passing an initializer list to `make_ArrayHandle`. ## Deprecated `make_ArrayHandle` with default shallow copy For historical reasons, passing an `std::vector` or a pointer to `make_ArrayHandle` does a shallow copy (i.e. `CopyFlag` defaults to `Off`). Although more efficient, this mode is inherintly unsafe, and making it the default is asking for trouble. To combat this, calling `make_ArrayHandle` without a copy flag is deprecated. In this way, if you wish to do the faster but more unsafe creation of an `ArrayHandle` you should explicitly express that. This requried quite a few changes through the VTK-m source (particularly in the tests). ## Similar changes to `Field` `vtkm::cont::Field` has a `make_Field` helper function that is similar to `make_ArrayHandle`. It also features the ability to create fields from `std::vector`s and C arrays. It also likewise had the same unsafe behavior by default of not copying from the source of the arrays. That behavior has similarly been depreciated. You now have to specify a copy flag. The ability to construct a `Field` from an initializer list of values has also been added.
2020-07-16 16:32:32 +00:00
return vtkm::cont::make_ArrayHandle(data, static_cast<vtkm::Id>(n), vtkm::CopyFlag::On);
}
template <typename T>
vtkm::cont::DataSet CreateDataSetArr(bool useSeparatedCoords,
std::size_t numPoints,
const T* coords,
std::size_t numCells,
std::size_t numConn,
const vtkm::Id* conn,
const vtkm::IdComponent* indices,
2017-05-18 14:29:41 +00:00
const vtkm::UInt8* shape)
{
vtkm::cont::DataSet dataSet;
vtkm::cont::DataSetBuilderExplicit dsb;
float f = 0.0f;
if (useSeparatedCoords)
{
std::vector<T> xvals(numPoints), yvals(numPoints), zvals(numPoints);
2017-05-18 14:29:41 +00:00
std::vector<T> varP(numPoints), varC(numCells);
std::vector<vtkm::UInt8> shapevals(numCells);
std::vector<vtkm::IdComponent> indicesvals(numCells);
std::vector<vtkm::Id> connvals(numConn);
for (std::size_t i = 0; i < numPoints; i++, f++)
{
2017-05-18 14:29:41 +00:00
xvals[i] = coords[i * 3 + 0];
yvals[i] = coords[i * 3 + 1];
zvals[i] = coords[i * 3 + 2];
varP[i] = static_cast<T>(f * 1.1f);
}
f = 0.0f;
for (std::size_t i = 0; i < numCells; i++, f++)
{
varC[i] = static_cast<T>(f * 1.1f);
shapevals[i] = shape[i];
indicesvals[i] = indices[i];
}
for (std::size_t i = 0; i < numConn; i++)
{
connvals[i] = conn[i];
}
dataSet = dsb.Create(xvals, yvals, zvals, shapevals, indicesvals, connvals);
Improvements to moving data into ArrayHandle We have made several improvements to adding data into an `ArrayHandle`. ## Moving data from an `std::vector` For numerous reasons, it is convenient to define data in a `std::vector` and then wrap that into an `ArrayHandle`. It is often the case that an `std::vector` is filled and then becomes unused once it is converted to an `ArrayHandle`. In this case, what we really want is to pass the data off to the `ArrayHandle` so that the `ArrayHandle` is now managing the data and not the `std::vector`. C++11 has a mechanism to do this: move semantics. You can now pass variables to functions as an "rvalue" (right-hand value). When something is passed as an rvalue, it can pull state out of that variable and move it somewhere else. `std::vector` implements this movement so that an rvalue can be moved to another `std::vector` without actually copying the data. `make_ArrayHandle` now also takes advantage of this feature to move rvalue `std::vector`s. There is a special form of `make_ArrayHandle` named `make_ArrayHandleMove` that takes an rvalue. There is also a special overload of `make_ArrayHandle` itself that handles an rvalue `vector`. (However, using the explicit move version is better if you want to make sure the data is actually moved.) ## Make `ArrayHandle` from initalizer list A common use case for using `std::vector` (particularly in our unit tests) is to quickly add an initalizer list into an `ArrayHandle`. Now you can by simply passing an initializer list to `make_ArrayHandle`. ## Deprecated `make_ArrayHandle` with default shallow copy For historical reasons, passing an `std::vector` or a pointer to `make_ArrayHandle` does a shallow copy (i.e. `CopyFlag` defaults to `Off`). Although more efficient, this mode is inherintly unsafe, and making it the default is asking for trouble. To combat this, calling `make_ArrayHandle` without a copy flag is deprecated. In this way, if you wish to do the faster but more unsafe creation of an `ArrayHandle` you should explicitly express that. This requried quite a few changes through the VTK-m source (particularly in the tests). ## Similar changes to `Field` `vtkm::cont::Field` has a `make_Field` helper function that is similar to `make_ArrayHandle`. It also features the ability to create fields from `std::vector`s and C arrays. It also likewise had the same unsafe behavior by default of not copying from the source of the arrays. That behavior has similarly been depreciated. You now have to specify a copy flag. The ability to construct a `Field` from an initializer list of values has also been added.
2020-07-16 16:32:32 +00:00
vtkm::cont::ArrayHandle<T> P = vtkm::cont::make_ArrayHandle(varP, vtkm::CopyFlag::On);
vtkm::cont::ArrayHandle<T> C = vtkm::cont::make_ArrayHandle(varC, vtkm::CopyFlag::On);
2020-05-27 19:27:47 +00:00
dataSet.AddPointField("pointvar", P);
dataSet.AddCellField("cellvar", C);
return dataSet;
}
else
{
2017-05-18 14:29:41 +00:00
std::vector<vtkm::Vec<T, 3>> tmp(numPoints);
std::vector<vtkm::Vec<T, 1>> varP(numPoints), varC(numCells);
for (std::size_t i = 0; i < numPoints; i++, f++)
{
2017-05-18 14:29:41 +00:00
tmp[i][0] = coords[i * 3 + 0];
tmp[i][1] = coords[i * 3 + 1];
tmp[i][2] = coords[i * 3 + 2];
varP[i][0] = static_cast<T>(f * 1.1f);
}
f = 0.0f;
for (std::size_t i = 0; i < numCells; i++, f++)
{
varC[i][0] = static_cast<T>(f * 1.1f);
}
Improvements to moving data into ArrayHandle We have made several improvements to adding data into an `ArrayHandle`. ## Moving data from an `std::vector` For numerous reasons, it is convenient to define data in a `std::vector` and then wrap that into an `ArrayHandle`. It is often the case that an `std::vector` is filled and then becomes unused once it is converted to an `ArrayHandle`. In this case, what we really want is to pass the data off to the `ArrayHandle` so that the `ArrayHandle` is now managing the data and not the `std::vector`. C++11 has a mechanism to do this: move semantics. You can now pass variables to functions as an "rvalue" (right-hand value). When something is passed as an rvalue, it can pull state out of that variable and move it somewhere else. `std::vector` implements this movement so that an rvalue can be moved to another `std::vector` without actually copying the data. `make_ArrayHandle` now also takes advantage of this feature to move rvalue `std::vector`s. There is a special form of `make_ArrayHandle` named `make_ArrayHandleMove` that takes an rvalue. There is also a special overload of `make_ArrayHandle` itself that handles an rvalue `vector`. (However, using the explicit move version is better if you want to make sure the data is actually moved.) ## Make `ArrayHandle` from initalizer list A common use case for using `std::vector` (particularly in our unit tests) is to quickly add an initalizer list into an `ArrayHandle`. Now you can by simply passing an initializer list to `make_ArrayHandle`. ## Deprecated `make_ArrayHandle` with default shallow copy For historical reasons, passing an `std::vector` or a pointer to `make_ArrayHandle` does a shallow copy (i.e. `CopyFlag` defaults to `Off`). Although more efficient, this mode is inherintly unsafe, and making it the default is asking for trouble. To combat this, calling `make_ArrayHandle` without a copy flag is deprecated. In this way, if you wish to do the faster but more unsafe creation of an `ArrayHandle` you should explicitly express that. This requried quite a few changes through the VTK-m source (particularly in the tests). ## Similar changes to `Field` `vtkm::cont::Field` has a `make_Field` helper function that is similar to `make_ArrayHandle`. It also features the ability to create fields from `std::vector`s and C arrays. It also likewise had the same unsafe behavior by default of not copying from the source of the arrays. That behavior has similarly been depreciated. You now have to specify a copy flag. The ability to construct a `Field` from an initializer list of values has also been added.
2020-07-16 16:32:32 +00:00
vtkm::cont::ArrayHandle<vtkm::Vec<T, 3>> pts =
vtkm::cont::make_ArrayHandle(tmp, vtkm::CopyFlag::On);
dataSet = dsb.Create(
pts, createAH(numCells, shape), createAH(numCells, indices), createAH(numConn, conn));
2020-05-27 19:27:47 +00:00
dataSet.AddPointField("pointvar", varP);
dataSet.AddCellField("cellvar", varC);
return dataSet;
}
}
template <typename T>
vtkm::cont::DataSet CreateDataSetVec(bool useSeparatedCoords,
std::size_t numPoints,
const T* coords,
std::size_t numCells,
std::size_t numConn,
const vtkm::Id* conn,
const vtkm::IdComponent* indices,
2017-05-18 14:29:41 +00:00
const vtkm::UInt8* shape)
{
vtkm::cont::DataSet dataSet;
vtkm::cont::DataSetBuilderExplicit dsb;
float f = 0.0f;
if (useSeparatedCoords)
{
std::vector<T> X(numPoints), Y(numPoints), Z(numPoints), varP(numPoints), varC(numCells);
for (std::size_t i = 0; i < numPoints; i++, f++)
{
2017-05-18 14:29:41 +00:00
X[i] = coords[i * 3 + 0];
Y[i] = coords[i * 3 + 1];
Z[i] = coords[i * 3 + 2];
varP[i] = static_cast<T>(f * 1.1f);
}
f = 0.0f;
for (std::size_t i = 0; i < numCells; i++, f++)
{
varC[i] = static_cast<T>(f * 1.1f);
}
dataSet = dsb.Create(
X, Y, Z, createVec(numCells, shape), createVec(numCells, indices), createVec(numConn, conn));
2020-05-27 19:27:47 +00:00
dataSet.AddPointField("pointvar", varP);
dataSet.AddCellField("cellvar", varC);
return dataSet;
}
else
{
2017-05-18 14:29:41 +00:00
std::vector<vtkm::Vec<T, 3>> pts(numPoints);
std::vector<vtkm::Vec<T, 1>> varP(numPoints), varC(numCells);
for (std::size_t i = 0; i < numPoints; i++, f++)
{
2017-05-18 14:29:41 +00:00
pts[i][0] = coords[i * 3 + 0];
pts[i][1] = coords[i * 3 + 1];
pts[i][2] = coords[i * 3 + 2];
varP[i][0] = static_cast<T>(f * 1.1f);
}
f = 0.0f;
for (std::size_t i = 0; i < numCells; i++, f++)
{
varC[i][0] = static_cast<T>(f * 1.1f);
}
dataSet = dsb.Create(
pts, createVec(numCells, shape), createVec(numCells, indices), createVec(numConn, conn));
2020-05-27 19:27:47 +00:00
dataSet.AddPointField("pointvar", varP);
dataSet.AddCellField("cellvar", varC);
return dataSet;
}
}
2017-05-18 14:29:41 +00:00
#define TEST_DATA(num) \
vtkm::cont::testing::ExplicitData##num::numPoints, \
vtkm::cont::testing::ExplicitData##num::coords, \
vtkm::cont::testing::ExplicitData##num::numCells, \
vtkm::cont::testing::ExplicitData##num::numConn, vtkm::cont::testing::ExplicitData##num::conn, \
vtkm::cont::testing::ExplicitData##num::numIndices, \
vtkm::cont::testing::ExplicitData##num::shapes
#define TEST_NUMS(num) \
vtkm::cont::testing::ExplicitData##num::numPoints, \
vtkm::cont::testing::ExplicitData##num::numCells
#define TEST_BOUNDS(num) \
2017-05-18 14:29:41 +00:00
vtkm::cont::testing::ExplicitData##num::numPoints, vtkm::cont::testing::ExplicitData##num::coords
2017-05-18 14:29:41 +00:00
void TestDataSetBuilderExplicit()
{
vtkm::cont::DataSet ds;
vtkm::Bounds bounds;
//Iterate over organization of coordinates.
for (int i = 0; i < 2; i++)
{
//Test ExplicitData0
bounds = ComputeBounds(TEST_BOUNDS(0));
2017-05-18 14:29:41 +00:00
ds = CreateDataSetArr(i == 0, TEST_DATA(0));
ValidateDataSet(ds, TEST_NUMS(0), bounds);
2017-05-18 14:29:41 +00:00
ds = CreateDataSetVec(i == 0, TEST_DATA(0));
ValidateDataSet(ds, TEST_NUMS(0), bounds);
//Test ExplicitData1
bounds = ComputeBounds(TEST_BOUNDS(1));
2017-05-18 14:29:41 +00:00
ds = CreateDataSetArr(i == 0, TEST_DATA(1));
ValidateDataSet(ds, TEST_NUMS(1), bounds);
2017-05-18 14:29:41 +00:00
ds = CreateDataSetVec(i == 0, TEST_DATA(1));
ValidateDataSet(ds, TEST_NUMS(1), bounds);
//Test ExplicitData2
bounds = ComputeBounds(TEST_BOUNDS(2));
2017-05-18 14:29:41 +00:00
ds = CreateDataSetArr(i == 0, TEST_DATA(2));
ValidateDataSet(ds, TEST_NUMS(2), bounds);
2017-05-18 14:29:41 +00:00
ds = CreateDataSetVec(i == 0, TEST_DATA(2));
ValidateDataSet(ds, TEST_NUMS(2), bounds);
}
}
} // namespace DataSetBuilderExplicitNamespace
int UnitTestDataSetBuilderExplicit(int argc, char* argv[])
{
using namespace DataSetBuilderExplicitNamespace;
return vtkm::cont::testing::Testing::Run(TestDataSetBuilderExplicit, argc, argv);
}