From d626f7317d28cbb9b0ef2813bd09961ad997a33e Mon Sep 17 00:00:00 2001 From: Kenneth Moreland Date: Thu, 21 Jan 2016 10:45:33 -0700 Subject: [PATCH 1/3] Make DataSetBuilder methods static This makes is slightly easier to use as you do not actually have to construct the DataSetBuilder object but just call its static method. The DataSetBuilderExplicitIterative methods are not static because they use state of the builder object to create the data. --- vtkm/cont/DataSetBuilderExplicit.h | 91 +++++++++++++++++---------- vtkm/cont/DataSetBuilderRectilinear.h | 61 ++++++++++++------ vtkm/cont/DataSetBuilderUniform.h | 23 ++++--- vtkm/cont/DataSetFieldAdd.h | 19 ++++-- 4 files changed, 128 insertions(+), 66 deletions(-) diff --git a/vtkm/cont/DataSetBuilderExplicit.h b/vtkm/cont/DataSetBuilderExplicit.h index 3ed9a7cad..72a21eab5 100644 --- a/vtkm/cont/DataSetBuilderExplicit.h +++ b/vtkm/cont/DataSetBuilderExplicit.h @@ -35,6 +35,8 @@ namespace cont { class DataSetBuilderExplicit { template + VTKM_CONT_EXPORT + static void CopyInto(const std::vector& input, vtkm::cont::ArrayHandle& output ) { @@ -52,6 +54,7 @@ public: //Zoo explicit cell template VTKM_CONT_EXPORT + static vtkm::cont::DataSet Create(const std::vector &xVals, const std::vector &yVals, @@ -63,13 +66,14 @@ public: const std::string &cellNm="cells") { std::vector zVals(xVals.size(),0); - return Create(xVals,yVals,zVals, - shapes,numIndices,connectivity, - dimensionality, coordsNm,cellNm); + return DataSetBuilderExplicit::Create(xVals,yVals,zVals, + shapes,numIndices,connectivity, + dimensionality, coordsNm,cellNm); } template VTKM_CONT_EXPORT + static vtkm::cont::DataSet Create(const std::vector &xVals, const std::vector &yVals, @@ -83,6 +87,7 @@ public: template VTKM_CONT_EXPORT + static vtkm::cont::DataSet Create(const vtkm::cont::ArrayHandle &xVals, const vtkm::cont::ArrayHandle &yVals, @@ -94,14 +99,16 @@ public: const std::string &coordsNm="coords", const std::string &cellNm="cells") { - return BuildDataSet(xVals,yVals,zVals, - shapes,numIndices,connectivity, - dimensionality, coordsNm,cellNm); + return DataSetBuilderExplicit::BuildDataSet( + xVals,yVals,zVals, + shapes,numIndices,connectivity, + dimensionality, coordsNm,cellNm); } template VTKM_CONT_EXPORT + static vtkm::cont::DataSet Create(const std::vector > &coords, const std::vector &shapes, @@ -113,6 +120,7 @@ public: template VTKM_CONT_EXPORT + static vtkm::cont::DataSet Create(const vtkm::cont::ArrayHandle > &coords, const vtkm::cont::ArrayHandle &shapes, @@ -122,12 +130,18 @@ public: const std::string &coordsNm="coords", const std::string &cellNm="cells") { - return BuildDataSet(coords, shapes, numIndices, connectivity, - dimensionality, coordsNm, cellNm); + return DataSetBuilderExplicit::BuildDataSet(coords, + shapes, + numIndices, + connectivity, + dimensionality, + coordsNm, + cellNm); } template VTKM_CONT_EXPORT + static vtkm::cont::DataSet Create(const std::vector > &coords, CellShapeTag tag, @@ -137,6 +151,7 @@ public: template VTKM_CONT_EXPORT + static vtkm::cont::DataSet Create(const vtkm::cont::ArrayHandle > &coords, CellShapeTag tag, @@ -144,11 +159,16 @@ public: const std::string &coordsNm="coords", const std::string &cellNm="cells") { - return BuildDataSet(coords, tag, connectivity, coordsNm, cellNm); + return DataSetBuilderExplicit::BuildDataSet(coords, + tag, + connectivity, + coordsNm, + cellNm); } private: template + static vtkm::cont::DataSet BuildDataSet(const vtkm::cont::ArrayHandle &X, const vtkm::cont::ArrayHandle &Y, @@ -162,6 +182,7 @@ private: template VTKM_CONT_EXPORT + static vtkm::cont::DataSet BuildDataSet(const vtkm::cont::ArrayHandle > &coords, const vtkm::cont::ArrayHandle &shapes, @@ -173,6 +194,7 @@ private: template VTKM_CONT_EXPORT + static vtkm::cont::DataSet BuildDataSet(const vtkm::cont::ArrayHandle > &coords, CellShapeTag tag, @@ -198,31 +220,33 @@ DataSetBuilderExplicit::Create(const std::vector &xVals, xVals.size() > 0); vtkm::cont::ArrayHandle Xc, Yc, Zc; - this->CopyInto(xVals, Xc); - this->CopyInto(yVals, Yc); - this->CopyInto(zVals, Zc); + DataSetBuilderExplicit::CopyInto(xVals, Xc); + DataSetBuilderExplicit::CopyInto(yVals, Yc); + DataSetBuilderExplicit::CopyInto(zVals, Zc); vtkm::cont::ArrayHandle Sc; vtkm::cont::ArrayHandle Nc; vtkm::cont::ArrayHandle Cc; - this->CopyInto(shapes, Sc); - this->CopyInto(numIndices, Nc); - this->CopyInto(connectivity, Cc); + DataSetBuilderExplicit::CopyInto(shapes, Sc); + DataSetBuilderExplicit::CopyInto(numIndices, Nc); + DataSetBuilderExplicit::CopyInto(connectivity, Cc); - return BuildDataSet(Xc,Yc,Zc, Sc,Nc,Cc, dimensionality, coordsNm, cellNm); + return DataSetBuilderExplicit::BuildDataSet( + Xc,Yc,Zc, Sc,Nc,Cc, dimensionality, coordsNm, cellNm); } template vtkm::cont::DataSet -DataSetBuilderExplicit::BuildDataSet(const vtkm::cont::ArrayHandle &X, - const vtkm::cont::ArrayHandle &Y, - const vtkm::cont::ArrayHandle &Z, - const vtkm::cont::ArrayHandle &shapes, - const vtkm::cont::ArrayHandle &numIndices, - const vtkm::cont::ArrayHandle &connectivity, - int dimensionality, - const std::string &coordsNm, - const std::string &cellNm) +DataSetBuilderExplicit::BuildDataSet( + const vtkm::cont::ArrayHandle &X, + const vtkm::cont::ArrayHandle &Y, + const vtkm::cont::ArrayHandle &Z, + const vtkm::cont::ArrayHandle &shapes, + const vtkm::cont::ArrayHandle &numIndices, + const vtkm::cont::ArrayHandle &connectivity, + int dimensionality, + const std::string &coordsNm, + const std::string &cellNm) { VTKM_ASSERT_CONT(X.GetNumberOfValues() == Y.GetNumberOfValues() && Y.GetNumberOfValues() == Z.GetNumberOfValues() && @@ -253,16 +277,17 @@ DataSetBuilderExplicit::Create(const std::vector > &coords, const std::string &cellNm) { vtkm::cont::ArrayHandle > coordsArray; - CopyInto(coords, coordsArray); + DataSetBuilderExplicit::CopyInto(coords, coordsArray); vtkm::cont::ArrayHandle Sc; vtkm::cont::ArrayHandle Nc; vtkm::cont::ArrayHandle Cc; - this->CopyInto(shapes, Sc); - this->CopyInto(numIndices, Nc); - this->CopyInto(connectivity, Cc); + DataSetBuilderExplicit::CopyInto(shapes, Sc); + DataSetBuilderExplicit::CopyInto(numIndices, Nc); + DataSetBuilderExplicit::CopyInto(connectivity, Cc); - return Create(coordsArray, Sc, Nc, Cc, dimensionality, coordsNm, cellNm); + return DataSetBuilderExplicit::Create( + coordsArray, Sc, Nc, Cc, dimensionality, coordsNm, cellNm); } template @@ -298,12 +323,12 @@ DataSetBuilderExplicit::Create(const std::vector > &coords, const std::string &cellNm) { vtkm::cont::ArrayHandle > coordsArray; - this->CopyInto(coords, coordsArray); + DataSetBuilderExplicit::CopyInto(coords, coordsArray); vtkm::cont::ArrayHandle Cc; - this->CopyInto(connectivity, Cc); + DataSetBuilderExplicit::CopyInto(connectivity, Cc); - return Create(coordsArray, tag, Cc, coordsNm, cellNm); + return DataSetBuilderExplicit::Create(coordsArray, tag, Cc, coordsNm, cellNm); } template diff --git a/vtkm/cont/DataSetBuilderRectilinear.h b/vtkm/cont/DataSetBuilderRectilinear.h index 8348e5eee..a12a75a56 100644 --- a/vtkm/cont/DataSetBuilderRectilinear.h +++ b/vtkm/cont/DataSetBuilderRectilinear.h @@ -32,6 +32,8 @@ namespace cont { class DataSetBuilderRectilinear { template + VTKM_CONT_EXPORT + static void CopyInto(const std::vector& input, vtkm::cont::ArrayHandle& output ) { @@ -41,6 +43,8 @@ class DataSetBuilderRectilinear } template + VTKM_CONT_EXPORT + static void CopyInto(const vtkm::cont::ArrayHandle& input, vtkm::cont::ArrayHandle& output ) { @@ -51,6 +55,8 @@ class DataSetBuilderRectilinear } template + VTKM_CONT_EXPORT + static void CopyInto(const T* input, vtkm::Id len, vtkm::cont::ArrayHandle& output ) { @@ -65,45 +71,52 @@ public: //2D grids. template VTKM_CONT_EXPORT + static vtkm::cont::DataSet Create(vtkm::Id nx, vtkm::Id ny, T *xvals, T *yvals, std::string coordNm="coords", std::string cellNm="cells") { T zvals = 0; - return Create(2, nx,ny, 1, xvals, yvals, &zvals, coordNm, cellNm); + return DataSetBuilderRectilinear::Create( + 2, nx,ny, 1, xvals, yvals, &zvals, coordNm, cellNm); } template VTKM_CONT_EXPORT + static vtkm::cont::DataSet Create(int dim, vtkm::Id nx, vtkm::Id ny, vtkm::Id nz, T *xvals, T *yvals, T *zvals, std::string coordNm, std::string cellNm) { VTKM_ASSERT_CONT(nx>1 && ny>1 && - ((dim==2 && nz==1)||(dim==3 && nz>=1))); + ((dim==2 && nz==1)||(dim==3 && nz>=1))); vtkm::cont::ArrayHandle Xc, Yc, Zc; - CopyInto(xvals,nx,Xc); - CopyInto(yvals,ny,Yc); - CopyInto(zvals,nz,Zc); + DataSetBuilderRectilinear::CopyInto(xvals,nx,Xc); + DataSetBuilderRectilinear::CopyInto(yvals,ny,Yc); + DataSetBuilderRectilinear::CopyInto(zvals,nz,Zc); - return BuildDataSet(dim, Xc,Yc,Zc, coordNm, cellNm); + return DataSetBuilderRectilinear::BuildDataSet( + dim, Xc,Yc,Zc, coordNm, cellNm); } template VTKM_CONT_EXPORT + static vtkm::cont::DataSet Create(const std::vector &xvals, const std::vector &yvals, std::string coordNm="coords", std::string cellNm="cells") { std::vector zvals(1,0); - return BuildDataSet(2, xvals,yvals,zvals, coordNm,cellNm); + return DataSetBuilderRectilinear::BuildDataSet( + 2, xvals,yvals,zvals, coordNm,cellNm); } template VTKM_CONT_EXPORT + static vtkm::cont::DataSet Create(const vtkm::cont::ArrayHandle &xvals, const vtkm::cont::ArrayHandle &yvals, @@ -114,33 +127,39 @@ public: vtkm::cont::ArrayHandle zvals; zvals.Allocate(1); zvals.GetPortalControl().Set(0,0.0); - return BuildDataSet(2, xvals,yvals,zvals, coordNm, cellNm); + return DataSetBuilderRectilinear::BuildDataSet( + 2, xvals,yvals,zvals, coordNm, cellNm); } //3D grids. template VTKM_CONT_EXPORT + static vtkm::cont::DataSet Create(vtkm::Id nx, vtkm::Id ny, vtkm::Id nz, T *xvals, T *yvals, T *zvals, std::string coordNm="coords", std::string cellNm="cells") { - return Create(3, nx,ny,nz, xvals, yvals, zvals, coordNm, cellNm); + return DataSetBuilderRectilinear::Create( + 3, nx,ny,nz, xvals, yvals, zvals, coordNm, cellNm); } template VTKM_CONT_EXPORT + static vtkm::cont::DataSet Create(const std::vector &xvals, const std::vector &yvals, const std::vector &zvals, std::string coordNm="coords", std::string cellNm="cells") { - return BuildDataSet(3, xvals, yvals, zvals, coordNm, cellNm); + return DataSetBuilderRectilinear::BuildDataSet( + 3, xvals, yvals, zvals, coordNm, cellNm); } template VTKM_CONT_EXPORT + static vtkm::cont::DataSet Create(const vtkm::cont::ArrayHandle &xvals, const vtkm::cont::ArrayHandle &yvals, @@ -150,12 +169,14 @@ public: VTKM_ASSERT_CONT(xvals.GetNumberOfValues()>1 && yvals.GetNumberOfValues()>1 && zvals.GetNumberOfValues()>1); - return BuildDataSet(3, xvals,yvals,zvals, coordNm, cellNm); + return DataSetBuilderRectilinear::BuildDataSet( + 3, xvals,yvals,zvals, coordNm, cellNm); } private: template VTKM_CONT_EXPORT + static vtkm::cont::DataSet BuildDataSet(int dim, const std::vector &xvals, @@ -164,18 +185,20 @@ private: std::string coordNm, std::string cellNm) { VTKM_ASSERT_CONT(xvals.size()>1 && yvals.size()>1 && - ((dim==2 && zvals.size()==1)||(dim==3 && zvals.size()>=1))); + ((dim==2 && zvals.size()==1)||(dim==3 && zvals.size()>=1))); vtkm::cont::ArrayHandle Xc, Yc, Zc; - this->CopyInto(xvals, Xc); - this->CopyInto(yvals, Yc); - this->CopyInto(zvals, Zc); + DataSetBuilderRectilinear::CopyInto(xvals, Xc); + DataSetBuilderRectilinear::CopyInto(yvals, Yc); + DataSetBuilderRectilinear::CopyInto(zvals, Zc); - return BuildDataSet(dim, Xc,Yc,Zc, coordNm, cellNm); + return DataSetBuilderRectilinear::BuildDataSet( + dim, Xc,Yc,Zc, coordNm, cellNm); } template VTKM_CONT_EXPORT + static vtkm::cont::DataSet BuildDataSet(int dim, const vtkm::cont::ArrayHandle &X, @@ -192,9 +215,9 @@ private: vtkm::cont::ArrayHandle > coords; vtkm::cont::ArrayHandle Xc, Yc, Zc; - this->CopyInto(X, Xc); - this->CopyInto(Y, Yc); - this->CopyInto(Z, Zc); + DataSetBuilderRectilinear::CopyInto(X, Xc); + DataSetBuilderRectilinear::CopyInto(Y, Yc); + DataSetBuilderRectilinear::CopyInto(Z, Zc); coords = vtkm::cont::make_ArrayHandleCartesianProduct(Xc,Yc,Zc); vtkm::cont::CoordinateSystem cs(coordNm, 1, coords); diff --git a/vtkm/cont/DataSetBuilderUniform.h b/vtkm/cont/DataSetBuilderUniform.h index 5b063e6fc..77aaf8be4 100644 --- a/vtkm/cont/DataSetBuilderUniform.h +++ b/vtkm/cont/DataSetBuilderUniform.h @@ -35,36 +35,41 @@ public: //2D uniform grids. VTKM_CONT_EXPORT + static vtkm::cont::DataSet Create(const vtkm::Id2 &dimensions, const vtkm::Vec &origin = vtkm::Vec(0.0f), const vtkm::Vec &spacing = vtkm::Vec(1.0f), std::string coordNm="coords", std::string cellNm="cells") { - return CreateDS(2, - dimensions[0],dimensions[1],1, origin[0],origin[1],0.0f, - spacing[0],spacing[1],1.0f, - coordNm, cellNm); + return DataSetBuilderUniform::CreateDS(2, + dimensions[0],dimensions[1],1, + origin[0],origin[1],0.0f, + spacing[0],spacing[1],1.0f, + coordNm, cellNm); } //3D uniform grids. VTKM_CONT_EXPORT + static vtkm::cont::DataSet Create(const vtkm::Id3 &dimensions, const vtkm::Vec &origin = vtkm::Vec(0.0f), const vtkm::Vec &spacing = vtkm::Vec(1.0f), std::string coordNm="coords", std::string cellNm="cells") { - return CreateDS(3, - dimensions[0],dimensions[1],dimensions[2], - origin[0],origin[1],origin[2], - spacing[0],spacing[1],spacing[2], - coordNm, cellNm); + return DataSetBuilderUniform::CreateDS( + 3, + dimensions[0],dimensions[1],dimensions[2], + origin[0],origin[1],origin[2], + spacing[0],spacing[1],spacing[2], + coordNm, cellNm); } private: template VTKM_CONT_EXPORT + static vtkm::cont::DataSet CreateDS(int dim, vtkm::Id nx, vtkm::Id ny, vtkm::Id nz, T originX, T originY, T originZ, diff --git a/vtkm/cont/DataSetFieldAdd.h b/vtkm/cont/DataSetFieldAdd.h index 6b6d29258..706062b7a 100644 --- a/vtkm/cont/DataSetFieldAdd.h +++ b/vtkm/cont/DataSetFieldAdd.h @@ -36,6 +36,7 @@ public: //Point centered fields. template VTKM_CONT_EXPORT + static void AddPointField(vtkm::cont::DataSet &dataSet, const std::string &fieldName, vtkm::cont::ArrayHandle &field) @@ -46,6 +47,7 @@ public: template VTKM_CONT_EXPORT + static void AddPointField(vtkm::cont::DataSet &dataSet, const std::string &fieldName, const std::vector &field) @@ -53,9 +55,10 @@ public: dataSet.AddField(Field(fieldName, 1, vtkm::cont::Field::ASSOC_POINTS, field)); } - + template VTKM_CONT_EXPORT + static void AddPointField(vtkm::cont::DataSet &dataSet, const std::string &fieldName, const T *field, const vtkm::Id &n) @@ -67,6 +70,7 @@ public: //Cell centered field template VTKM_CONT_EXPORT + static void AddCellField(vtkm::cont::DataSet &dataSet, const std::string &fieldName, vtkm::cont::ArrayHandle &field, @@ -78,6 +82,7 @@ public: template VTKM_CONT_EXPORT + static void AddCellField(vtkm::cont::DataSet &dataSet, const std::string &fieldName, const std::vector &field, @@ -86,9 +91,10 @@ public: dataSet.AddField(Field(fieldName, 1, vtkm::cont::Field::ASSOC_CELL_SET, cellSetName, field)); } - + template VTKM_CONT_EXPORT + static void AddCellField(vtkm::cont::DataSet &dataSet, const std::string &fieldName, const T *field, const vtkm::Id &n, @@ -100,6 +106,7 @@ public: template VTKM_CONT_EXPORT + static void AddCellField(vtkm::cont::DataSet &dataSet, const std::string &fieldName, vtkm::cont::ArrayHandle &field, @@ -107,10 +114,11 @@ public: { std::string cellSetName = dataSet.GetCellSet(cellSetIndex).GetCellSet().GetName(); - this->AddCellField(dataSet, fieldName, field, cellSetName); + DataSetFieldAdd::AddCellField(dataSet, fieldName, field, cellSetName); } template VTKM_CONT_EXPORT + static void AddCellField(vtkm::cont::DataSet &dataSet, const std::string &fieldName, const std::vector &field, @@ -118,11 +126,12 @@ public: { std::string cellSetName = dataSet.GetCellSet(cellSetIndex).GetCellSet().GetName(); - this->AddCellField(dataSet, fieldName, field, cellSetName); + DataSetFieldAdd::AddCellField(dataSet, fieldName, field, cellSetName); } template VTKM_CONT_EXPORT + static void AddCellField(vtkm::cont::DataSet &dataSet, const std::string &fieldName, const T *field, const vtkm::Id &n, @@ -130,7 +139,7 @@ public: { std::string cellSetName = dataSet.GetCellSet(cellSetIndex).GetCellSet().GetName(); - this->AddCellField(dataSet, fieldName, field, n, cellSetName); + DataSetFieldAdd::AddCellField(dataSet, fieldName, field, n, cellSetName); } From e06621409009ed7614508598bf6e2d1a9344a9b1 Mon Sep 17 00:00:00 2001 From: Kenneth Moreland Date: Thu, 21 Jan 2016 13:00:41 -0700 Subject: [PATCH 2/3] Add methods to DataSetFieldAdd that accept a DynamicArrayHandle --- vtkm/cont/DataSetFieldAdd.h | 38 +++++++++++++++++-- .../testing/UnitTestDataSetPermutation.cxx | 3 +- 2 files changed, 36 insertions(+), 5 deletions(-) diff --git a/vtkm/cont/DataSetFieldAdd.h b/vtkm/cont/DataSetFieldAdd.h index 706062b7a..7ab8316d9 100644 --- a/vtkm/cont/DataSetFieldAdd.h +++ b/vtkm/cont/DataSetFieldAdd.h @@ -34,12 +34,22 @@ public: DataSetFieldAdd() {} //Point centered fields. + VTKM_CONT_EXPORT + static + void AddPointField(vtkm::cont::DataSet &dataSet, + const std::string &fieldName, + const vtkm::cont::DynamicArrayHandle &field) + { + dataSet.AddField(Field(fieldName, 1, vtkm::cont::Field::ASSOC_POINTS, + field)); + } + template VTKM_CONT_EXPORT static void AddPointField(vtkm::cont::DataSet &dataSet, const std::string &fieldName, - vtkm::cont::ArrayHandle &field) + const vtkm::cont::ArrayHandle &field) { dataSet.AddField(Field(fieldName, 1, vtkm::cont::Field::ASSOC_POINTS, field)); @@ -68,12 +78,23 @@ public: } //Cell centered field + VTKM_CONT_EXPORT + static + void AddCellField(vtkm::cont::DataSet &dataSet, + const std::string &fieldName, + const vtkm::cont::DynamicArrayHandle &field, + const std::string &cellSetName) + { + dataSet.AddField(Field(fieldName, 1, vtkm::cont::Field::ASSOC_CELL_SET, + cellSetName, field)); + } + template VTKM_CONT_EXPORT static void AddCellField(vtkm::cont::DataSet &dataSet, const std::string &fieldName, - vtkm::cont::ArrayHandle &field, + const vtkm::cont::ArrayHandle &field, const std::string &cellSetName) { dataSet.AddField(Field(fieldName, 1, vtkm::cont::Field::ASSOC_CELL_SET, @@ -104,12 +125,23 @@ public: cellSetName, field, n)); } + VTKM_CONT_EXPORT + static + void AddCellField(vtkm::cont::DataSet &dataSet, + const std::string &fieldName, + const vtkm::cont::DynamicArrayHandle &field, + vtkm::Id cellSetIndex = 0) + { + std::string cellSetName = + dataSet.GetCellSet(cellSetIndex).GetCellSet().GetName(); + DataSetFieldAdd::AddCellField(dataSet, fieldName, field, cellSetName); + } template VTKM_CONT_EXPORT static void AddCellField(vtkm::cont::DataSet &dataSet, const std::string &fieldName, - vtkm::cont::ArrayHandle &field, + const vtkm::cont::ArrayHandle &field, vtkm::Id cellSetIndex = 0) { std::string cellSetName = diff --git a/vtkm/cont/testing/UnitTestDataSetPermutation.cxx b/vtkm/cont/testing/UnitTestDataSetPermutation.cxx index ba4471645..1ee2a1696 100644 --- a/vtkm/cont/testing/UnitTestDataSetPermutation.cxx +++ b/vtkm/cont/testing/UnitTestDataSetPermutation.cxx @@ -84,8 +84,7 @@ inline vtkm::cont::DataSet make_SingleTypeDataSet() const int nVerts = 5; vtkm::Float32 vars[nVerts] = {10.1f, 20.1f, 30.2f, 40.2f, 50.3f}; - vtkm::cont::DataSetFieldAdd fieldAdder; - fieldAdder.AddPointField(ds, "pointvar", vars, nVerts); + vtkm::cont::DataSetFieldAdd::AddPointField(ds, "pointvar", vars, nVerts); return ds; } From f9750e83f7778cd727ae452415913e36bca97b16 Mon Sep 17 00:00:00 2001 From: Kenneth Moreland Date: Thu, 21 Jan 2016 13:51:36 -0700 Subject: [PATCH 3/3] Fix issues with Field constructor overloads I ran into a few minor issues with the constructors to the Field class. The big change I made was that I removed the Field constructors that take an example type and create an empty field of that type. The problem was that the example type was easily confused with some other type that was supposed to describe an array. This lead to some odd behavior in the compiler and resulted in errors in unexpected places. The use case for this constructor is dubious. There were several tests in the code that would create an empty field, add it to a data set, then get it back out to pass to the worklet. The code is much simpler if you just make an ArrayHandle of the right type and use that in the worklet invoke directly. It is also faster to compile with smaller code because the type is known statically (whereas it is lost the other way). The other change was to declare references to ArrayHandle and DynamicArrayHandle as const. There is nothing in the behavior that invalidates the const, and it accepts arrays constructed in the parameter. --- vtkm/cont/Field.h | 78 ++++--------------- vtkm/worklet/testing/UnitTestCellAverage.cxx | 60 +++++--------- .../testing/UnitTestPointElevation.cxx | 14 ++-- .../UnitTestWorkletMapTopologyExplicit.cxx | 73 +++-------------- .../UnitTestWorkletMapTopologyUniform.cxx | 71 +++-------------- 5 files changed, 65 insertions(+), 231 deletions(-) diff --git a/vtkm/cont/Field.h b/vtkm/cont/Field.h index 0249d3faa..9e879a493 100644 --- a/vtkm/cont/Field.h +++ b/vtkm/cont/Field.h @@ -158,10 +158,10 @@ private: // Special implementation for regular point coordinates, which are easy // to determine. - void operator()(vtkm::cont::ArrayHandle< + void operator()(const vtkm::cont::ArrayHandle< vtkm::Vec, vtkm::cont::ArrayHandleUniformPointCoordinates::StorageTag> - array) + &array) { vtkm::internal::ArrayPortalUniformPointCoordinates portal = array.GetPortalConstControl(); @@ -327,32 +327,13 @@ public: this->CopyData(data, nvals); } - template - VTKM_CONT_EXPORT - Field(std::string name, - vtkm::IdComponent order, - AssociationEnum association, - T) - : Name(name), - Order(order), - Association(association), - AssocCellSetName(), - AssocLogicalDim(-1), - Data(vtkm::cont::ArrayHandle()), - Bounds(), - ModifiedFlag(true) - { - VTKM_ASSERT_CONT((this->Association == ASSOC_WHOLE_MESH) || - (this->Association == ASSOC_POINTS)); - } - /// constructors for cell set associations VTKM_CONT_EXPORT Field(std::string name, vtkm::IdComponent order, AssociationEnum association, const std::string& cellSetName, - vtkm::cont::DynamicArrayHandle &data) + const vtkm::cont::DynamicArrayHandle &data) : Name(name), Order(order), Association(association), @@ -371,7 +352,7 @@ public: vtkm::IdComponent order, AssociationEnum association, const std::string& cellSetName, - vtkm::cont::ArrayHandle &data) + const vtkm::cont::ArrayHandle &data) : Name(name), Order(order), Association(association), @@ -423,32 +404,13 @@ public: this->CopyData(data, nvals); } - template - VTKM_CONT_EXPORT - Field(std::string name, - vtkm::IdComponent order, - AssociationEnum association, - const std::string& cellSetName, - T) - : Name(name), - Order(order), - Association(association), - AssocCellSetName(cellSetName), - AssocLogicalDim(-1), - Data(vtkm::cont::ArrayHandle()), - Bounds(), - ModifiedFlag(true) - { - VTKM_ASSERT_CONT(this->Association == ASSOC_CELL_SET); - } - /// constructors for logical dimension associations VTKM_CONT_EXPORT Field(std::string name, vtkm::IdComponent order, AssociationEnum association, vtkm::IdComponent logicalDim, - vtkm::cont::DynamicArrayHandle &data) + const vtkm::cont::DynamicArrayHandle &data) : Name(name), Order(order), Association(association), @@ -467,7 +429,7 @@ public: vtkm::IdComponent order, AssociationEnum association, vtkm::IdComponent logicalDim, - vtkm::cont::ArrayHandle &data) + const vtkm::cont::ArrayHandle &data) : Name(name), Order(order), Association(association), @@ -515,25 +477,6 @@ public: CopyData(data, nvals); } - template - VTKM_CONT_EXPORT - Field(std::string name, - vtkm::IdComponent order, - AssociationEnum association, - vtkm::IdComponent logicalDim, - T) - : Name(name), - Order(order), - Association(association), - AssocCellSetName(), - AssocLogicalDim(logicalDim), - Data(vtkm::cont::ArrayHandle()), - Bounds(), - ModifiedFlag(true) - { - VTKM_ASSERT_CONT(this->Association == ASSOC_LOGICAL_DIM); - } - VTKM_CONT_EXPORT Field() : Name(), @@ -659,7 +602,14 @@ public: template VTKM_CONT_EXPORT - void SetData(vtkm::cont::ArrayHandle &newdata) + void SetData(const vtkm::cont::ArrayHandle &newdata) + { + this->Data = newdata; + this->ModifiedFlag = true; + } + + VTKM_CONT_EXPORT + void SetData(const vtkm::cont::DynamicArrayHandle &newdata) { this->Data = newdata; this->ModifiedFlag = true; diff --git a/vtkm/worklet/testing/UnitTestCellAverage.cxx b/vtkm/worklet/testing/UnitTestCellAverage.cxx index 79baa6210..c15ba03f5 100644 --- a/vtkm/worklet/testing/UnitTestCellAverage.cxx +++ b/vtkm/worklet/testing/UnitTestCellAverage.cxx @@ -37,26 +37,20 @@ void TestCellAverageUniform3D() vtkm::cont::testing::MakeTestDataSet testDataSet; vtkm::cont::DataSet dataSet = testDataSet.Make3DUniformDataSet0(); - vtkm::cont::Field result("avgvals", - 1, - vtkm::cont::Field::ASSOC_CELL_SET, - std::string("cells"), - vtkm::Float32()); + vtkm::cont::ArrayHandle result; vtkm::worklet::DispatcherMapTopology dispatcher; dispatcher.Invoke(dataSet.GetField("pointvar").GetData(), dataSet.GetCellSet(), - result.GetData()); - - vtkm::cont::ArrayHandle resultArrayHandle; - result.GetData().CopyTo(resultArrayHandle); + result); vtkm::Float32 expected[4] = { 60.1875f, 70.2125f, 120.3375f, 130.3625f }; for (int i = 0; i < 4; ++i) - { - VTKM_TEST_ASSERT(test_equal(resultArrayHandle.GetPortalConstControl().Get(i), - expected[i]), "Wrong result for CellAverage worklet on 3D uniform data"); - } + { + VTKM_TEST_ASSERT( + test_equal(result.GetPortalConstControl().Get(i), expected[i]), + "Wrong result for CellAverage worklet on 3D uniform data"); + } } void TestCellAverageUniform2D() @@ -66,26 +60,20 @@ void TestCellAverageUniform2D() vtkm::cont::testing::MakeTestDataSet testDataSet; vtkm::cont::DataSet dataSet = testDataSet.Make2DUniformDataSet0(); - vtkm::cont::Field result("avgvals", - 1, - vtkm::cont::Field::ASSOC_CELL_SET, - std::string("cells"), - vtkm::Float32()); + vtkm::cont::ArrayHandle result; vtkm::worklet::DispatcherMapTopology dispatcher; dispatcher.Invoke(dataSet.GetField("pointvar").GetData(), dataSet.GetCellSet(), - result.GetData()); - - vtkm::cont::ArrayHandle resultArrayHandle; - result.GetData().CopyTo(resultArrayHandle); + result); vtkm::Float32 expected[2] = { 30.1f, 40.1f }; for (int i = 0; i < 2; ++i) - { - VTKM_TEST_ASSERT(test_equal(resultArrayHandle.GetPortalConstControl().Get(i), - expected[i]), "Wrong result for CellAverage worklet on 2D uniform data"); - } + { + VTKM_TEST_ASSERT( + test_equal(result.GetPortalConstControl().Get(i), expected[i]), + "Wrong result for CellAverage worklet on 2D uniform data"); + } } void TestCellAverageExplicit() @@ -95,26 +83,20 @@ void TestCellAverageExplicit() vtkm::cont::testing::MakeTestDataSet testDataSet; vtkm::cont::DataSet dataSet = testDataSet.Make3DExplicitDataSet0(); - vtkm::cont::Field result("avgvals", - 1, - vtkm::cont::Field::ASSOC_CELL_SET, - std::string("cells"), - vtkm::Float32()); + vtkm::cont::ArrayHandle result; vtkm::worklet::DispatcherMapTopology dispatcher; dispatcher.Invoke(dataSet.GetField("pointvar").GetData(), dataSet.GetCellSet(), - result.GetData()); - - vtkm::cont::ArrayHandle resultArrayHandle; - result.GetData().CopyTo(resultArrayHandle); + result); vtkm::Float32 expected[2] = { 20.1333f, 35.2f }; for (int i = 0; i < 2; ++i) - { - VTKM_TEST_ASSERT(test_equal(resultArrayHandle.GetPortalConstControl().Get(i), - expected[i]), "Wrong result for CellAverage worklet on 3D explicit data"); - } + { + VTKM_TEST_ASSERT( + test_equal(result.GetPortalConstControl().Get(i), expected[i]), + "Wrong result for CellAverage worklet on 3D explicit data"); + } } diff --git a/vtkm/worklet/testing/UnitTestPointElevation.cxx b/vtkm/worklet/testing/UnitTestPointElevation.cxx index 5d6942408..c85ee3c7f 100644 --- a/vtkm/worklet/testing/UnitTestPointElevation.cxx +++ b/vtkm/worklet/testing/UnitTestPointElevation.cxx @@ -81,8 +81,7 @@ void TestPointElevation() vtkm::cont::DataSet dataSet = MakePointElevationTestDataSet(); - dataSet.AddField(vtkm::cont::Field("elevation", 1, vtkm::cont::Field::ASSOC_POINTS, - vtkm::Float32())); + vtkm::cont::ArrayHandle result; vtkm::worklet::PointElevation pointElevationWorklet; pointElevationWorklet.SetLowPoint(vtkm::make_Vec(0.0, 0.0, 0.0)); @@ -92,18 +91,17 @@ void TestPointElevation() vtkm::worklet::DispatcherMapField dispatcher(pointElevationWorklet); dispatcher.Invoke(dataSet.GetCoordinateSystem().GetData(), - dataSet.GetField("elevation").GetData()); + result); vtkm::cont::ArrayHandle > coordinates; dataSet.GetCoordinateSystem().GetData().CopyTo(coordinates); - vtkm::cont::ArrayHandle result; - dataSet.GetField("elevation").GetData().CopyTo(result); for (vtkm::Id i = 0; i < result.GetNumberOfValues(); ++i) { - VTKM_TEST_ASSERT(test_equal(coordinates.GetPortalConstControl().Get(i)[1] * 2.0, - result.GetPortalConstControl().Get(i)), - "Wrong result for PointElevation worklet"); + VTKM_TEST_ASSERT( + test_equal(coordinates.GetPortalConstControl().Get(i)[1] * 2.0, + result.GetPortalConstControl().Get(i)), + "Wrong result for PointElevation worklet"); } } diff --git a/vtkm/worklet/testing/UnitTestWorkletMapTopologyExplicit.cxx b/vtkm/worklet/testing/UnitTestWorkletMapTopologyExplicit.cxx index 7ef6795da..76ad2f76a 100644 --- a/vtkm/worklet/testing/UnitTestWorkletMapTopologyExplicit.cxx +++ b/vtkm/worklet/testing/UnitTestWorkletMapTopologyExplicit.cxx @@ -150,35 +150,19 @@ TestMaxPointOrCell() vtkm::cont::testing::MakeTestDataSet testDataSet; vtkm::cont::DataSet dataSet = testDataSet.Make3DExplicitDataSet0(); - //Run a worklet to populate a cell centered field. - //Here, we're filling it with test values. - vtkm::cont::Field f("outcellvar", - 0, - vtkm::cont::Field::ASSOC_CELL_SET, - std::string("cells"), - vtkm::Float32()); - - dataSet.AddField(f); - - VTKM_TEST_ASSERT(dataSet.GetNumberOfCellSets() == 1, - "Incorrect number of cell sets"); - - VTKM_TEST_ASSERT(dataSet.GetNumberOfFields() == 3, - "Incorrect number of fields"); + vtkm::cont::ArrayHandle result; vtkm::worklet::DispatcherMapTopology< ::test_explicit::MaxPointOrCellValue > dispatcher; dispatcher.Invoke(dataSet.GetField("cellvar").GetData(), dataSet.GetField("pointvar").GetData(), dataSet.GetCellSet(0), - dataSet.GetField("outcellvar").GetData()); + result); //Make sure we got the right answer. - vtkm::cont::ArrayHandle res; - dataSet.GetField("outcellvar").GetData().CopyTo(res); - VTKM_TEST_ASSERT(test_equal(res.GetPortalConstControl().Get(0), 100.1f), + VTKM_TEST_ASSERT(test_equal(result.GetPortalConstControl().Get(0), 100.1f), "Wrong result for PointToCellMax worklet"); - VTKM_TEST_ASSERT(test_equal(res.GetPortalConstControl().Get(1), 100.2f), + VTKM_TEST_ASSERT(test_equal(result.GetPortalConstControl().Get(1), 100.2f), "Wrong result for PointToCellMax worklet"); } @@ -190,34 +174,17 @@ TestAvgPointToCell() vtkm::cont::testing::MakeTestDataSet testDataSet; vtkm::cont::DataSet dataSet = testDataSet.Make3DExplicitDataSet0(); - //Run a worklet to populate a cell centered field. - //Here, we're filling it with test values. - vtkm::cont::Field f("outcellvar", - 0, - vtkm::cont::Field::ASSOC_CELL_SET, - std::string("cells"), - vtkm::Float32()); - - dataSet.AddField(f); - - VTKM_TEST_ASSERT(dataSet.GetNumberOfCellSets() == 1, - "Incorrect number of cell sets"); - - VTKM_TEST_ASSERT(dataSet.GetNumberOfFields() == 3, - "Incorrect number of fields"); + vtkm::cont::ArrayHandle result; vtkm::worklet::DispatcherMapTopology< ::test_explicit::AveragePointToCellValue > dispatcher; dispatcher.Invoke(dataSet.GetField("pointvar").GetData(), dataSet.GetCellSet(), - dataSet.GetField("outcellvar").GetData()); + result); //make sure we got the right answer. - vtkm::cont::ArrayHandle res; - dataSet.GetField("outcellvar").GetData().CopyTo(res); - - VTKM_TEST_ASSERT(test_equal(res.GetPortalConstControl().Get(0), 20.1333f), + VTKM_TEST_ASSERT(test_equal(result.GetPortalConstControl().Get(0), 20.1333f), "Wrong result for PointToCellAverage worklet"); - VTKM_TEST_ASSERT(test_equal(res.GetPortalConstControl().Get(1), 35.2f), + VTKM_TEST_ASSERT(test_equal(result.GetPortalConstControl().Get(1), 35.2f), "Wrong result for PointToCellAverage worklet"); } @@ -229,34 +196,18 @@ TestAvgCellToPoint() vtkm::cont::testing::MakeTestDataSet testDataSet; vtkm::cont::DataSet dataSet = testDataSet.Make3DExplicitDataSet1(); - //Run a worklet to populate a point centered field. - //Here, we're filling it with test values. - vtkm::cont::Field f("outpointvar", - 1, - vtkm::cont::Field::ASSOC_POINTS, - vtkm::Float32()); - - dataSet.AddField(f); - - VTKM_TEST_ASSERT(dataSet.GetNumberOfCellSets() == 1, - "Incorrect number of cell sets"); - - VTKM_TEST_ASSERT(dataSet.GetNumberOfFields() == 3, - "Incorrect number of fields"); + vtkm::cont::ArrayHandle result; vtkm::worklet::DispatcherMapTopology< ::test_explicit::AverageCellToPointValue > dispatcher; dispatcher.Invoke(dataSet.GetField("cellvar").GetData(), dataSet.GetCellSet(), - dataSet.GetField("outpointvar").GetData()); + result); //make sure we got the right answer. - vtkm::cont::ArrayHandle res; - dataSet.GetField("outpointvar").GetData().CopyTo(res); - - VTKM_TEST_ASSERT(test_equal(res.GetPortalConstControl().Get(0), 100.1f), + VTKM_TEST_ASSERT(test_equal(result.GetPortalConstControl().Get(0), 100.1f), "Wrong result for CellToPointAverage worklet"); - VTKM_TEST_ASSERT(test_equal(res.GetPortalConstControl().Get(1), 100.15f), + VTKM_TEST_ASSERT(test_equal(result.GetPortalConstControl().Get(1), 100.15f), "Wrong result for CellToPointAverage worklet"); } diff --git a/vtkm/worklet/testing/UnitTestWorkletMapTopologyUniform.cxx b/vtkm/worklet/testing/UnitTestWorkletMapTopologyUniform.cxx index a1d28ca6a..f3e8761f3 100644 --- a/vtkm/worklet/testing/UnitTestWorkletMapTopologyUniform.cxx +++ b/vtkm/worklet/testing/UnitTestWorkletMapTopologyUniform.cxx @@ -178,21 +178,8 @@ TestMaxPointOrCell() vtkm::cont::testing::MakeTestDataSet testDataSet; vtkm::cont::DataSet dataSet = testDataSet.Make2DUniformDataSet0(); - //Run a worklet to populate a cell centered field. - //Here, we're filling it with test values. - vtkm::cont::Field f("outcellvar", - 1, - vtkm::cont::Field::ASSOC_CELL_SET, - std::string("cells"), - vtkm::Float32()); + vtkm::cont::ArrayHandle result; - dataSet.AddField(f); - - VTKM_TEST_ASSERT(test_equal(dataSet.GetNumberOfCellSets(), 1), - "Incorrect number of cell sets"); - - VTKM_TEST_ASSERT(test_equal(dataSet.GetNumberOfFields(), 3), - "Incorrect number of fields"); vtkm::worklet::DispatcherMapTopology< ::test_uniform::MaxPointOrCellValue > dispatcher; dispatcher.Invoke(dataSet.GetField("cellvar").GetData(), dataSet.GetField("pointvar").GetData(), @@ -202,15 +189,13 @@ TestMaxPointOrCell() // part more flexible. dataSet.GetCellSet(0).ResetCellSetList( vtkm::cont::CellSetListTagStructured2D()), - dataSet.GetField("outcellvar").GetData()); + result); //make sure we got the right answer. - vtkm::cont::ArrayHandle res; - dataSet.GetField("outcellvar").GetData().CopyTo(res); - VTKM_TEST_ASSERT(test_equal(res.GetPortalConstControl().Get(0), 100.1f), + VTKM_TEST_ASSERT(test_equal(result.GetPortalConstControl().Get(0), 100.1f), "Wrong result for MaxPointOrCell worklet"); - VTKM_TEST_ASSERT(test_equal(res.GetPortalConstControl().Get(1), 200.1f), + VTKM_TEST_ASSERT(test_equal(result.GetPortalConstControl().Get(1), 200.1f), "Wrong result for MaxPointOrCell worklet"); } @@ -221,21 +206,8 @@ TestAvgPointToCell() vtkm::cont::testing::MakeTestDataSet testDataSet; vtkm::cont::DataSet dataSet = testDataSet.Make2DUniformDataSet0(); - //Run a worklet to populate a cell centered field. - //Here, we're filling it with test values. - vtkm::cont::Field f("outcellvar", - 0, - vtkm::cont::Field::ASSOC_CELL_SET, - std::string("cells"), - vtkm::Float32()); + vtkm::cont::ArrayHandle result; - dataSet.AddField(f); - - VTKM_TEST_ASSERT(test_equal(dataSet.GetNumberOfCellSets(), 1), - "Incorrect number of cell sets"); - - VTKM_TEST_ASSERT(test_equal(dataSet.GetNumberOfFields(), 3), - "Incorrect number of fields"); vtkm::worklet::DispatcherMapTopology< ::test_uniform::AveragePointToCellValue > dispatcher; dispatcher.Invoke(dataSet.GetField("pointvar").GetData(), // We know that the cell set is a structured 2D grid and @@ -244,15 +216,12 @@ TestAvgPointToCell() // part more flexible. dataSet.GetCellSet(0).ResetCellSetList( vtkm::cont::CellSetListTagStructured2D()), - dataSet.GetField("outcellvar").GetData()); + result); //make sure we got the right answer. - vtkm::cont::ArrayHandle res; - dataSet.GetField("outcellvar").GetData().CopyTo(res); - - VTKM_TEST_ASSERT(test_equal(res.GetPortalConstControl().Get(0), 30.1f), + VTKM_TEST_ASSERT(test_equal(result.GetPortalConstControl().Get(0), 30.1f), "Wrong result for PointToCellAverage worklet"); - VTKM_TEST_ASSERT(test_equal(res.GetPortalConstControl().Get(1), 40.1f), + VTKM_TEST_ASSERT(test_equal(result.GetPortalConstControl().Get(1), 40.1f), "Wrong result for PointToCellAverage worklet"); } @@ -264,20 +233,7 @@ TestAvgCellToPoint() vtkm::cont::testing::MakeTestDataSet testDataSet; vtkm::cont::DataSet dataSet = testDataSet.Make2DUniformDataSet0(); - //Run a worklet to populate a point centered field. - //Here, we're filling it with test values. - vtkm::cont::Field f("outpointvar", - 1, - vtkm::cont::Field::ASSOC_POINTS, - vtkm::Float32()); - - dataSet.AddField(f); - - VTKM_TEST_ASSERT(test_equal(dataSet.GetNumberOfCellSets(), 1), - "Incorrect number of cell sets"); - - VTKM_TEST_ASSERT(test_equal(dataSet.GetNumberOfFields(), 3), - "Incorrect number of fields"); + vtkm::cont::ArrayHandle result; vtkm::worklet::DispatcherMapTopology< ::test_uniform::AverageCellToPointValue > dispatcher; dispatcher.Invoke(dataSet.GetField("cellvar").GetData(), @@ -287,15 +243,12 @@ TestAvgCellToPoint() // part more flexible. dataSet.GetCellSet(0).ResetCellSetList( vtkm::cont::CellSetListTagStructured2D()), - dataSet.GetField("outpointvar").GetData()); + result); //make sure we got the right answer. - vtkm::cont::ArrayHandle res; - dataSet.GetField("outpointvar").GetData().CopyTo(res); - - VTKM_TEST_ASSERT(test_equal(res.GetPortalConstControl().Get(0), 100.1f), + VTKM_TEST_ASSERT(test_equal(result.GetPortalConstControl().Get(0), 100.1f), "Wrong result for CellToPointAverage worklet"); - VTKM_TEST_ASSERT(test_equal(res.GetPortalConstControl().Get(1), 150.1f), + VTKM_TEST_ASSERT(test_equal(result.GetPortalConstControl().Get(1), 150.1f), "Wrong result for CellToPointAverage worklet"); }