From 69226803c228ed66c181742d1c04babfca2789de Mon Sep 17 00:00:00 2001 From: Kenneth Moreland Date: Thu, 22 Aug 2019 14:46:52 -0600 Subject: [PATCH] Make PointTransform actually transform the points The primary (likely only) use of PointTransform is to perform affine transform on the position of the mesh. However, PointTransform did not actually move the mesh. Rather, it just created a new field with the transformed points. Now, the output has its coordinate system replaced with the transformed one generated (in addition to be added as a field). This can be turned off (but defaults to on). Also changed the constructor to turn on UseCoordinateSystemAsField so that by default the filter operates on the existing coordinate system and replaces it with the new coordinate system. Also removed the template parameter on the filter. That added an unnecessary complication to using it. --- vtkm/cont/DataSet.cxx | 22 +++++ vtkm/cont/DataSet.h | 8 ++ vtkm/filter/PointTransform.h | 42 ++++++---- vtkm/filter/PointTransform.hxx | 83 ++++++++++++------- .../filter/testing/UnitTestPointTransform.cxx | 28 ++++--- vtkm/worklet/PointTransform.h | 53 ++++-------- 6 files changed, 141 insertions(+), 95 deletions(-) diff --git a/vtkm/cont/DataSet.cxx b/vtkm/cont/DataSet.cxx index bcde71f16..582c80bad 100644 --- a/vtkm/cont/DataSet.cxx +++ b/vtkm/cont/DataSet.cxx @@ -53,6 +53,12 @@ const vtkm::cont::CoordinateSystem& DataSet::GetCoordinateSystem(vtkm::Id index) return this->CoordSystems[static_cast(index)]; } +vtkm::cont::CoordinateSystem& DataSet::GetCoordinateSystem(vtkm::Id index) +{ + VTKM_ASSERT((index >= 0) && (index < this->GetNumberOfCoordinateSystems())); + return this->CoordSystems[static_cast(index)]; +} + vtkm::Id DataSet::GetCoordinateSystemIndex(const std::string& name) const { vtkm::Id index = -1; @@ -83,6 +89,22 @@ const vtkm::cont::CoordinateSystem& DataSet::GetCoordinateSystem(const std::stri return this->GetCoordinateSystem(index); } +vtkm::cont::CoordinateSystem& DataSet::GetCoordinateSystem(const std::string& name) +{ + vtkm::Id index = this->GetCoordinateSystemIndex(name); + if (index < 0) + { + std::string error_message("No coordinate system with the name " + name + + " valid names are: \n"); + for (const auto& cs : this->CoordSystems) + { + error_message += cs.GetName() + "\n"; + } + throw vtkm::cont::ErrorBadValue(error_message); + } + return this->GetCoordinateSystem(index); +} + const vtkm::cont::DynamicCellSet& DataSet::GetCellSet(vtkm::Id index) const { VTKM_ASSERT((index >= 0) && (index < this->GetNumberOfCellSets())); diff --git a/vtkm/cont/DataSet.h b/vtkm/cont/DataSet.h index 763dd0e43..d7a32128f 100644 --- a/vtkm/cont/DataSet.h +++ b/vtkm/cont/DataSet.h @@ -109,6 +109,9 @@ public: VTKM_CONT const vtkm::cont::CoordinateSystem& GetCoordinateSystem(vtkm::Id index = 0) const; + VTKM_CONT + vtkm::cont::CoordinateSystem& GetCoordinateSystem(vtkm::Id index = 0); + /// Returns the index for the first CoordinateSystem whose /// name matches the provided string. /// Will return -1 if no match is found @@ -117,9 +120,14 @@ public: /// Returns the first CoordinateSystem that matches the provided name. /// Will throw an exception if no match is found + //@{ VTKM_CONT const vtkm::cont::CoordinateSystem& GetCoordinateSystem(const std::string& name) const; + VTKM_CONT + vtkm::cont::CoordinateSystem& GetCoordinateSystem(const std::string& name); + //@} + VTKM_CONT void AddCellSet(const vtkm::cont::DynamicCellSet& cellSet) { this->CellSets.push_back(cellSet); } diff --git a/vtkm/filter/PointTransform.h b/vtkm/filter/PointTransform.h index cce1a608b..1080560ef 100644 --- a/vtkm/filter/PointTransform.h +++ b/vtkm/filter/PointTransform.h @@ -21,8 +21,7 @@ namespace filter /// \brief /// /// Generate scalar field from a dataset. -template -class PointTransform : public vtkm::filter::FilterField> +class PointTransform : public vtkm::filter::FilterField { public: using SupportedTypes = vtkm::TypeListTagFieldVec3; @@ -30,27 +29,39 @@ public: VTKM_CONT PointTransform(); - void SetTranslation(const S& tx, const S& ty, const S& tz); + void SetTranslation(const vtkm::FloatDefault& tx, + const vtkm::FloatDefault& ty, + const vtkm::FloatDefault& tz); - void SetTranslation(const vtkm::Vec& v); + void SetTranslation(const vtkm::Vec3f& v); - void SetRotation(const S& angleDegrees, const vtkm::Vec& axis); + void SetRotation(const vtkm::FloatDefault& angleDegrees, const vtkm::Vec3f& axis); - void SetRotation(const S& angleDegrees, const S& rx, const S& ry, const S& rz); + void SetRotation(const vtkm::FloatDefault& angleDegrees, + const vtkm::FloatDefault& rx, + const vtkm::FloatDefault& ry, + const vtkm::FloatDefault& rz); - void SetRotationX(const S& angleDegrees); + void SetRotationX(const vtkm::FloatDefault& angleDegrees); - void SetRotationY(const S& angleDegrees); + void SetRotationY(const vtkm::FloatDefault& angleDegrees); - void SetRotationZ(const S& angleDegrees); + void SetRotationZ(const vtkm::FloatDefault& angleDegrees); - void SetScale(const S& s); + void SetScale(const vtkm::FloatDefault& s); - void SetScale(const S& sx, const S& sy, const S& sz); + void SetScale(const vtkm::FloatDefault& sx, + const vtkm::FloatDefault& sy, + const vtkm::FloatDefault& sz); + + void SetScale(const vtkm::Vec3f& v); + + void SetTransform(const vtkm::Matrix& mtx); + + void SetChangeCoordinateSystem(bool flag); + bool GetChangeCoordinateSystem() const; - void SetScale(const vtkm::Vec& v); - void SetTransform(const vtkm::Matrix& mtx); template VTKM_CONT vtkm::cont::DataSet DoExecute(const vtkm::cont::DataSet& input, @@ -59,11 +70,14 @@ public: vtkm::filter::PolicyBase policy); private: - vtkm::worklet::PointTransform Worklet; + vtkm::worklet::PointTransform Worklet; + bool ChangeCoordinateSystem; }; } } // namespace vtkm::filter +#ifndef vtk_m_filter_PointTransform_hxx #include +#endif #endif // vtk_m_filter_PointTransform_h diff --git a/vtkm/filter/PointTransform.hxx b/vtkm/filter/PointTransform.hxx index 4b28e2a76..cf36d2611 100644 --- a/vtkm/filter/PointTransform.hxx +++ b/vtkm/filter/PointTransform.hxx @@ -8,105 +8,114 @@ // PURPOSE. See the above copyright notice for more information. //============================================================================ +#ifndef vtk_m_filter_PointTransform_hxx +#define vtk_m_filter_PointTransform_hxx +#include + namespace vtkm { namespace filter { //----------------------------------------------------------------------------- -template -inline VTKM_CONT PointTransform::PointTransform() +inline VTKM_CONT PointTransform::PointTransform() : Worklet() + , ChangeCoordinateSystem(true) { this->SetOutputFieldName("transform"); + this->SetUseCoordinateSystemAsField(true); } //----------------------------------------------------------------------------- -template -inline VTKM_CONT void PointTransform::SetTranslation(const S& tx, const S& ty, const S& tz) +inline VTKM_CONT void PointTransform::SetTranslation(const vtkm::FloatDefault& tx, + const vtkm::FloatDefault& ty, + const vtkm::FloatDefault& tz) { this->Worklet.SetTranslation(tx, ty, tz); } //----------------------------------------------------------------------------- -template -inline VTKM_CONT void PointTransform::SetTranslation(const vtkm::Vec& v) +inline VTKM_CONT void PointTransform::SetTranslation(const vtkm::Vec3f& v) { this->Worklet.SetTranslation(v); } //----------------------------------------------------------------------------- -template -inline VTKM_CONT void PointTransform::SetRotation(const S& angleDegrees, - const vtkm::Vec& axis) +inline VTKM_CONT void PointTransform::SetRotation(const vtkm::FloatDefault& angleDegrees, + const vtkm::Vec3f& axis) { this->Worklet.SetRotation(angleDegrees, axis); } //----------------------------------------------------------------------------- -template -inline VTKM_CONT void PointTransform::SetRotation(const S& angleDegrees, - const S& rx, - const S& ry, - const S& rz) +inline VTKM_CONT void PointTransform::SetRotation(const vtkm::FloatDefault& angleDegrees, + const vtkm::FloatDefault& rx, + const vtkm::FloatDefault& ry, + const vtkm::FloatDefault& rz) { this->Worklet.SetRotation(angleDegrees, rx, ry, rz); } //----------------------------------------------------------------------------- -template -inline VTKM_CONT void PointTransform::SetRotationX(const S& angleDegrees) +inline VTKM_CONT void PointTransform::SetRotationX(const vtkm::FloatDefault& angleDegrees) { this->Worklet.SetRotationX(angleDegrees); } //----------------------------------------------------------------------------- -template -inline VTKM_CONT void PointTransform::SetRotationY(const S& angleDegrees) +inline VTKM_CONT void PointTransform::SetRotationY(const vtkm::FloatDefault& angleDegrees) { this->Worklet.SetRotationY(angleDegrees); } //----------------------------------------------------------------------------- -template -inline VTKM_CONT void PointTransform::SetRotationZ(const S& angleDegrees) +inline VTKM_CONT void PointTransform::SetRotationZ(const vtkm::FloatDefault& angleDegrees) { this->Worklet.SetRotationZ(angleDegrees); } //----------------------------------------------------------------------------- -template -inline VTKM_CONT void PointTransform::SetScale(const S& s) +inline VTKM_CONT void PointTransform::SetScale(const vtkm::FloatDefault& s) { this->Worklet.SetScale(s); } //----------------------------------------------------------------------------- -template -inline VTKM_CONT void PointTransform::SetScale(const S& sx, const S& sy, const S& sz) +inline VTKM_CONT void PointTransform::SetScale(const vtkm::FloatDefault& sx, + const vtkm::FloatDefault& sy, + const vtkm::FloatDefault& sz) { this->Worklet.SetScale(sx, sy, sz); } //----------------------------------------------------------------------------- -template -inline VTKM_CONT void PointTransform::SetScale(const vtkm::Vec& v) +inline VTKM_CONT void PointTransform::SetScale(const vtkm::Vec3f& v) { this->Worklet.SetScale(v); } //----------------------------------------------------------------------------- -template -inline VTKM_CONT void PointTransform::SetTransform(const vtkm::Matrix& mtx) +inline VTKM_CONT void PointTransform::SetTransform( + const vtkm::Matrix& mtx) { this->Worklet.SetTransform(mtx); } +//----------------------------------------------------------------------------- +inline VTKM_CONT void PointTransform::SetChangeCoordinateSystem(bool flag) +{ + this->ChangeCoordinateSystem = flag; +} + +//----------------------------------------------------------------------------- +inline VTKM_CONT bool PointTransform::GetChangeCoordinateSystem() const +{ + return this->ChangeCoordinateSystem; +} //----------------------------------------------------------------------------- -template template -inline VTKM_CONT vtkm::cont::DataSet PointTransform::DoExecute( +inline VTKM_CONT vtkm::cont::DataSet PointTransform::DoExecute( const vtkm::cont::DataSet& inDataSet, const vtkm::cont::ArrayHandle& field, const vtkm::filter::FieldMetadata& fieldMetadata, @@ -115,7 +124,19 @@ inline VTKM_CONT vtkm::cont::DataSet PointTransform::DoExecute( vtkm::cont::ArrayHandle outArray; this->Invoke(this->Worklet, field, outArray); - return CreateResult(inDataSet, outArray, this->GetOutputFieldName(), fieldMetadata); + vtkm::cont::DataSet outData = + CreateResult(inDataSet, outArray, this->GetOutputFieldName(), fieldMetadata); + + if (this->GetChangeCoordinateSystem()) + { + vtkm::Id coordIndex = + this->GetUseCoordinateSystemAsField() ? this->GetActiveCoordinateSystemIndex() : 0; + outData.GetCoordinateSystem(coordIndex).SetData(outArray); + } + + return outData; } } } // namespace vtkm::filter + +#endif //vtk_m_filter_PointTransform_hxx diff --git a/vtkm/filter/testing/UnitTestPointTransform.cxx b/vtkm/filter/testing/UnitTestPointTransform.cxx index 761236a0e..11ea887b2 100644 --- a/vtkm/filter/testing/UnitTestPointTransform.cxx +++ b/vtkm/filter/testing/UnitTestPointTransform.cxx @@ -74,28 +74,36 @@ void ValidatePointTransform(const vtkm::cont::CoordinateSystem& coords, .GetData() .CopyTo(resultArrayHandle); + vtkm::cont::ArrayHandleVirtualCoordinates outPointsArrayHandle = + result.GetCoordinateSystem().GetData(); + auto points = coords.GetData(); VTKM_TEST_ASSERT(points.GetNumberOfValues() == resultArrayHandle.GetNumberOfValues(), "Incorrect number of points in point transform"); - auto pointsPortal = points.GetPortalControl(); - auto resultsPortal = resultArrayHandle.GetPortalControl(); + auto pointsPortal = points.GetPortalConstControl(); + auto resultsPortal = resultArrayHandle.GetPortalConstControl(); + auto outPointsPortal = outPointsArrayHandle.GetPortalConstControl(); for (vtkm::Id i = 0; i < points.GetNumberOfValues(); i++) + { VTKM_TEST_ASSERT( test_equal(resultsPortal.Get(i), vtkm::Transform3DPoint(matrix, pointsPortal.Get(i))), "Wrong result for PointTransform worklet"); + VTKM_TEST_ASSERT( + test_equal(outPointsPortal.Get(i), vtkm::Transform3DPoint(matrix, pointsPortal.Get(i))), + "Wrong result for PointTransform worklet"); + } } void TestPointTransformTranslation(const vtkm::cont::DataSet& ds, const vtkm::Vec3f& trans) { - vtkm::filter::PointTransform filter; + vtkm::filter::PointTransform filter; filter.SetOutputFieldName("translation"); - filter.SetUseCoordinateSystemAsField(true); filter.SetTranslation(trans); - auto result = filter.Execute(ds); + vtkm::cont::DataSet result = filter.Execute(ds); ValidatePointTransform( ds.GetCoordinateSystem(), "translation", result, Transform3DTranslate(trans)); @@ -103,12 +111,11 @@ void TestPointTransformTranslation(const vtkm::cont::DataSet& ds, const vtkm::Ve void TestPointTransformScale(const vtkm::cont::DataSet& ds, const vtkm::Vec3f& scale) { - vtkm::filter::PointTransform filter; + vtkm::filter::PointTransform filter; filter.SetOutputFieldName("scale"); - filter.SetUseCoordinateSystemAsField(true); filter.SetScale(scale); - auto result = filter.Execute(ds); + vtkm::cont::DataSet result = filter.Execute(ds); ValidatePointTransform(ds.GetCoordinateSystem(), "scale", result, Transform3DScale(scale)); } @@ -117,12 +124,11 @@ void TestPointTransformRotation(const vtkm::cont::DataSet& ds, const vtkm::FloatDefault& angle, const vtkm::Vec3f& axis) { - vtkm::filter::PointTransform filter; + vtkm::filter::PointTransform filter; filter.SetOutputFieldName("rotation"); - filter.SetUseCoordinateSystemAsField(true); filter.SetRotation(angle, axis); - auto result = filter.Execute(ds); + vtkm::cont::DataSet result = filter.Execute(ds); ValidatePointTransform( ds.GetCoordinateSystem(), "rotation", result, Transform3DRotate(angle, axis)); diff --git a/vtkm/worklet/PointTransform.h b/vtkm/worklet/PointTransform.h index a26f970fd..48fe2d765 100644 --- a/vtkm/worklet/PointTransform.h +++ b/vtkm/worklet/PointTransform.h @@ -32,64 +32,39 @@ public: PointTransform() {} //Translation - template - VTKM_CONT void SetTranslation(const S& tx, const S& ty, const S& tz) + VTKM_CONT void SetTranslation(const T& tx, const T& ty, const T& tz) { - matrix = vtkm::Transform3DTranslate(static_cast(tx), static_cast(ty), static_cast(tz)); + matrix = vtkm::Transform3DTranslate(tx, ty, tz); } - template - VTKM_CONT void SetTranslation(const vtkm::Vec& v) - { - SetTranslation(v[0], v[1], v[2]); - } + VTKM_CONT void SetTranslation(const vtkm::Vec& v) { SetTranslation(v[0], v[1], v[2]); } //Rotation - template - VTKM_CONT void SetRotation(const S& angleDegrees, const vtkm::Vec& axis) + VTKM_CONT void SetRotation(const T& angleDegrees, const vtkm::Vec& axis) { matrix = vtkm::Transform3DRotate(angleDegrees, axis); } - template - VTKM_CONT void SetRotation(const S& angleDegrees, const S& rx, const S& ry, const S& rz) + VTKM_CONT void SetRotation(const T& angleDegrees, const T& rx, const T& ry, const T& rz) { - SetRotation(angleDegrees, vtkm::Vec(rx, ry, rz)); + SetRotation(angleDegrees, { rx, ry, rz }); } - template - VTKM_CONT void SetRotationX(const S& angleDegrees) - { - SetRotation(angleDegrees, 1, 0, 0); - } + VTKM_CONT void SetRotationX(const T& angleDegrees) { SetRotation(angleDegrees, 1, 0, 0); } - template - VTKM_CONT void SetRotationY(const S& angleDegrees) - { - SetRotation(angleDegrees, 0, 1, 0); - } + VTKM_CONT void SetRotationY(const T& angleDegrees) { SetRotation(angleDegrees, 0, 1, 0); } - template - VTKM_CONT void SetRotationZ(const S& angleDegrees) - { - SetRotation(angleDegrees, 0, 0, 1); - } + VTKM_CONT void SetRotationZ(const T& angleDegrees) { SetRotation(angleDegrees, 0, 0, 1); } //Scaling - template - VTKM_CONT void SetScale(const S& s) + VTKM_CONT void SetScale(const T& s) { matrix = vtkm::Transform3DScale(s, s, s); } + + VTKM_CONT void SetScale(const T& sx, const T& sy, const T& sz) { - matrix = vtkm::Transform3DScale(s, s, s); + matrix = vtkm::Transform3DScale(sx, sy, sz); } - template - VTKM_CONT void SetScale(const S& sx, const S& sy, const S& sz) - { - matrix = vtkm::Transform3DScale(static_cast(sx), static_cast(sy), static_cast(sz)); - } - - template - VTKM_CONT void SetScale(const vtkm::Vec& v) + VTKM_CONT void SetScale(const vtkm::Vec& v) { matrix = vtkm::Transform3DScale(v[0], v[1], v[2]); }