vtk-m/vtkm/cont/DataSetBuilderRectilinear.h

248 lines
9.2 KiB
C
Raw Normal View History

//============================================================================
// Copyright (c) Kitware, Inc.
// All rights reserved.
// See LICENSE.txt for details.
2019-04-15 23:24:21 +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.
//============================================================================
#ifndef vtk_m_cont_DataSetBuilderRectilinear_h
#define vtk_m_cont_DataSetBuilderRectilinear_h
#include <vtkm/cont/ArrayCopy.h>
#include <vtkm/cont/ArrayHandleCartesianProduct.h>
#include <vtkm/cont/ArrayPortalToIterators.h>
#include <vtkm/cont/CoordinateSystem.h>
#include <vtkm/cont/DataSet.h>
#include <vtkm/cont/serial/DeviceAdapterSerial.h>
2017-05-18 14:29:41 +00:00
namespace vtkm
{
namespace cont
{
class VTKM_CONT_EXPORT DataSetBuilderRectilinear
{
2017-05-18 14:29:41 +00:00
template <typename T, typename U>
VTKM_CONT static void CopyInto(const std::vector<T>& input, vtkm::cont::ArrayHandle<U>& output)
{
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
DataSetBuilderRectilinear::CopyInto(vtkm::cont::make_ArrayHandle(input, vtkm::CopyFlag::Off),
output);
}
2017-05-18 14:29:41 +00:00
template <typename T, typename U>
VTKM_CONT static void CopyInto(const vtkm::cont::ArrayHandle<T>& input,
vtkm::cont::ArrayHandle<U>& output)
{
vtkm::cont::ArrayCopy(input, output);
}
2017-05-18 14:29:41 +00:00
template <typename T, typename U>
VTKM_CONT static void CopyInto(const T* input, vtkm::Id len, vtkm::cont::ArrayHandle<U>& output)
{
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
DataSetBuilderRectilinear::CopyInto(
vtkm::cont::make_ArrayHandle(input, len, vtkm::CopyFlag::Off), output);
}
2017-05-18 14:29:41 +00:00
public:
VTKM_CONT
DataSetBuilderRectilinear();
//1D grids.
2017-05-18 14:29:41 +00:00
template <typename T>
VTKM_CONT static vtkm::cont::DataSet Create(const std::vector<T>& xvals,
const std::string& coordNm = "coords")
{
2017-05-18 14:29:41 +00:00
std::vector<T> yvals(1, 0), zvals(1, 0);
return DataSetBuilderRectilinear::BuildDataSet(xvals, yvals, zvals, coordNm);
}
2017-05-18 14:29:41 +00:00
template <typename T>
VTKM_CONT static vtkm::cont::DataSet Create(vtkm::Id nx,
T* xvals,
const std::string& coordNm = "coords")
{
T yvals = 0, zvals = 0;
return DataSetBuilderRectilinear::BuildDataSet(nx, 1, 1, xvals, &yvals, &zvals, coordNm);
}
2017-05-18 14:29:41 +00:00
template <typename T>
VTKM_CONT static vtkm::cont::DataSet Create(const vtkm::cont::ArrayHandle<T>& xvals,
const std::string& coordNm = "coords")
{
vtkm::cont::ArrayHandle<T> yvals, zvals;
yvals.Allocate(1);
yvals.WritePortal().Set(0, 0.0);
zvals.Allocate(1);
zvals.WritePortal().Set(0, 0.0);
return DataSetBuilderRectilinear::BuildDataSet(xvals, yvals, zvals, coordNm);
}
//2D grids.
2017-05-18 14:29:41 +00:00
template <typename T>
VTKM_CONT static vtkm::cont::DataSet Create(const std::vector<T>& xvals,
const std::vector<T>& yvals,
const std::string& coordNm = "coords")
{
2017-05-18 14:29:41 +00:00
std::vector<T> zvals(1, 0);
return DataSetBuilderRectilinear::BuildDataSet(xvals, yvals, zvals, coordNm);
}
2017-05-18 14:29:41 +00:00
template <typename T>
VTKM_CONT static vtkm::cont::DataSet Create(vtkm::Id nx,
vtkm::Id ny,
T* xvals,
T* yvals,
const std::string& coordNm = "coords")
{
T zvals = 0;
return DataSetBuilderRectilinear::BuildDataSet(nx, ny, 1, xvals, yvals, &zvals, coordNm);
}
2017-05-18 14:29:41 +00:00
template <typename T>
VTKM_CONT static vtkm::cont::DataSet Create(const vtkm::cont::ArrayHandle<T>& xvals,
const vtkm::cont::ArrayHandle<T>& yvals,
const std::string& coordNm = "coords")
{
vtkm::cont::ArrayHandle<T> zvals;
zvals.Allocate(1);
zvals.WritePortal().Set(0, 0.0);
return DataSetBuilderRectilinear::BuildDataSet(xvals, yvals, zvals, coordNm);
}
//3D grids.
2017-05-18 14:29:41 +00:00
template <typename T>
VTKM_CONT static vtkm::cont::DataSet Create(vtkm::Id nx,
vtkm::Id ny,
vtkm::Id nz,
T* xvals,
T* yvals,
T* zvals,
const std::string& coordNm = "coords")
{
return DataSetBuilderRectilinear::BuildDataSet(nx, ny, nz, xvals, yvals, zvals, coordNm);
}
2017-05-18 14:29:41 +00:00
template <typename T>
VTKM_CONT static vtkm::cont::DataSet Create(const std::vector<T>& xvals,
const std::vector<T>& yvals,
const std::vector<T>& zvals,
const std::string& coordNm = "coords")
{
return DataSetBuilderRectilinear::BuildDataSet(xvals, yvals, zvals, coordNm);
}
2017-05-18 14:29:41 +00:00
template <typename T>
VTKM_CONT static vtkm::cont::DataSet Create(const vtkm::cont::ArrayHandle<T>& xvals,
const vtkm::cont::ArrayHandle<T>& yvals,
const vtkm::cont::ArrayHandle<T>& zvals,
const std::string& coordNm = "coords")
{
return DataSetBuilderRectilinear::BuildDataSet(xvals, yvals, zvals, coordNm);
}
private:
2017-05-18 14:29:41 +00:00
template <typename T>
VTKM_CONT static vtkm::cont::DataSet BuildDataSet(const std::vector<T>& xvals,
2017-05-18 14:29:41 +00:00
const std::vector<T>& yvals,
const std::vector<T>& zvals,
const std::string& coordNm)
{
vtkm::cont::ArrayHandle<vtkm::FloatDefault> Xc, Yc, Zc;
DataSetBuilderRectilinear::CopyInto(xvals, Xc);
DataSetBuilderRectilinear::CopyInto(yvals, Yc);
DataSetBuilderRectilinear::CopyInto(zvals, Zc);
return DataSetBuilderRectilinear::BuildDataSet(Xc, Yc, Zc, coordNm);
}
2017-05-18 14:29:41 +00:00
template <typename T>
VTKM_CONT static vtkm::cont::DataSet BuildDataSet(vtkm::Id nx,
vtkm::Id ny,
vtkm::Id nz,
const T* xvals,
const T* yvals,
const T* zvals,
const std::string& coordNm)
{
vtkm::cont::ArrayHandle<vtkm::FloatDefault> Xc, Yc, Zc;
DataSetBuilderRectilinear::CopyInto(xvals, nx, Xc);
DataSetBuilderRectilinear::CopyInto(yvals, ny, Yc);
DataSetBuilderRectilinear::CopyInto(zvals, nz, Zc);
return DataSetBuilderRectilinear::BuildDataSet(Xc, Yc, Zc, coordNm);
2017-05-18 14:29:41 +00:00
}
2017-05-18 14:29:41 +00:00
template <typename T>
VTKM_CONT static vtkm::cont::DataSet BuildDataSet(const vtkm::cont::ArrayHandle<T>& X,
2017-05-18 14:29:41 +00:00
const vtkm::cont::ArrayHandle<T>& Y,
const vtkm::cont::ArrayHandle<T>& Z,
const std::string& coordNm)
{
vtkm::cont::DataSet dataSet;
//Convert all coordinates to floatDefault.
2017-05-18 14:29:41 +00:00
vtkm::cont::ArrayHandleCartesianProduct<vtkm::cont::ArrayHandle<vtkm::FloatDefault>,
vtkm::cont::ArrayHandle<vtkm::FloatDefault>,
vtkm::cont::ArrayHandle<vtkm::FloatDefault>>
coords;
vtkm::cont::ArrayHandle<vtkm::FloatDefault> Xc, Yc, Zc;
DataSetBuilderRectilinear::CopyInto(X, Xc);
DataSetBuilderRectilinear::CopyInto(Y, Yc);
DataSetBuilderRectilinear::CopyInto(Z, Zc);
2017-05-18 14:29:41 +00:00
coords = vtkm::cont::make_ArrayHandleCartesianProduct(Xc, Yc, Zc);
vtkm::cont::CoordinateSystem cs(coordNm, coords);
dataSet.AddCoordinateSystem(cs);
// compute the dimensions of the cellset by counting the number of axes
// with >1 dimension
int ndims = 0;
vtkm::Id dims[3];
if (Xc.GetNumberOfValues() > 1)
{
dims[ndims++] = Xc.GetNumberOfValues();
}
if (Yc.GetNumberOfValues() > 1)
{
dims[ndims++] = Yc.GetNumberOfValues();
}
if (Zc.GetNumberOfValues() > 1)
{
dims[ndims++] = Zc.GetNumberOfValues();
}
if (ndims == 1)
{
2019-08-13 21:28:37 +00:00
vtkm::cont::CellSetStructured<1> cellSet;
cellSet.SetPointDimensions(dims[0]);
dataSet.SetCellSet(cellSet);
}
else if (ndims == 2)
{
2019-08-13 21:28:37 +00:00
vtkm::cont::CellSetStructured<2> cellSet;
cellSet.SetPointDimensions(vtkm::make_Vec(dims[0], dims[1]));
dataSet.SetCellSet(cellSet);
}
else if (ndims == 3)
{
2019-08-13 21:28:37 +00:00
vtkm::cont::CellSetStructured<3> cellSet;
cellSet.SetPointDimensions(vtkm::make_Vec(dims[0], dims[1], dims[2]));
dataSet.SetCellSet(cellSet);
}
else
{
2017-05-18 14:29:41 +00:00
throw vtkm::cont::ErrorBadValue("Invalid cell set dimension");
}
return dataSet;
}
};
} // namespace cont
} // namespace vtkm
#endif //vtk_m_cont_DataSetBuilderRectilinear_h