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.
This commit is contained in:
Kenneth Moreland 2019-08-22 14:46:52 -06:00
parent 673e3d38df
commit 69226803c2
6 changed files with 141 additions and 95 deletions

@ -53,6 +53,12 @@ const vtkm::cont::CoordinateSystem& DataSet::GetCoordinateSystem(vtkm::Id index)
return this->CoordSystems[static_cast<std::size_t>(index)];
}
vtkm::cont::CoordinateSystem& DataSet::GetCoordinateSystem(vtkm::Id index)
{
VTKM_ASSERT((index >= 0) && (index < this->GetNumberOfCoordinateSystems()));
return this->CoordSystems[static_cast<std::size_t>(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()));

@ -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); }

@ -21,8 +21,7 @@ namespace filter
/// \brief
///
/// Generate scalar field from a dataset.
template <typename S>
class PointTransform : public vtkm::filter::FilterField<PointTransform<S>>
class PointTransform : public vtkm::filter::FilterField<PointTransform>
{
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<S, 3>& v);
void SetTranslation(const vtkm::Vec3f& v);
void SetRotation(const S& angleDegrees, const vtkm::Vec<S, 3>& 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<vtkm::FloatDefault, 4, 4>& mtx);
void SetChangeCoordinateSystem(bool flag);
bool GetChangeCoordinateSystem() const;
void SetScale(const vtkm::Vec<S, 3>& v);
void SetTransform(const vtkm::Matrix<S, 4, 4>& mtx);
template <typename T, typename StorageType, typename DerivedPolicy>
VTKM_CONT vtkm::cont::DataSet DoExecute(const vtkm::cont::DataSet& input,
@ -59,11 +70,14 @@ public:
vtkm::filter::PolicyBase<DerivedPolicy> policy);
private:
vtkm::worklet::PointTransform<S> Worklet;
vtkm::worklet::PointTransform<vtkm::FloatDefault> Worklet;
bool ChangeCoordinateSystem;
};
}
} // namespace vtkm::filter
#ifndef vtk_m_filter_PointTransform_hxx
#include <vtkm/filter/PointTransform.hxx>
#endif
#endif // vtk_m_filter_PointTransform_h

@ -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 <vtkm/filter/PointTransform.h>
namespace vtkm
{
namespace filter
{
//-----------------------------------------------------------------------------
template <typename S>
inline VTKM_CONT PointTransform<S>::PointTransform()
inline VTKM_CONT PointTransform::PointTransform()
: Worklet()
, ChangeCoordinateSystem(true)
{
this->SetOutputFieldName("transform");
this->SetUseCoordinateSystemAsField(true);
}
//-----------------------------------------------------------------------------
template <typename S>
inline VTKM_CONT void PointTransform<S>::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 <typename S>
inline VTKM_CONT void PointTransform<S>::SetTranslation(const vtkm::Vec<S, 3>& v)
inline VTKM_CONT void PointTransform::SetTranslation(const vtkm::Vec3f& v)
{
this->Worklet.SetTranslation(v);
}
//-----------------------------------------------------------------------------
template <typename S>
inline VTKM_CONT void PointTransform<S>::SetRotation(const S& angleDegrees,
const vtkm::Vec<S, 3>& axis)
inline VTKM_CONT void PointTransform::SetRotation(const vtkm::FloatDefault& angleDegrees,
const vtkm::Vec3f& axis)
{
this->Worklet.SetRotation(angleDegrees, axis);
}
//-----------------------------------------------------------------------------
template <typename S>
inline VTKM_CONT void PointTransform<S>::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 <typename S>
inline VTKM_CONT void PointTransform<S>::SetRotationX(const S& angleDegrees)
inline VTKM_CONT void PointTransform::SetRotationX(const vtkm::FloatDefault& angleDegrees)
{
this->Worklet.SetRotationX(angleDegrees);
}
//-----------------------------------------------------------------------------
template <typename S>
inline VTKM_CONT void PointTransform<S>::SetRotationY(const S& angleDegrees)
inline VTKM_CONT void PointTransform::SetRotationY(const vtkm::FloatDefault& angleDegrees)
{
this->Worklet.SetRotationY(angleDegrees);
}
//-----------------------------------------------------------------------------
template <typename S>
inline VTKM_CONT void PointTransform<S>::SetRotationZ(const S& angleDegrees)
inline VTKM_CONT void PointTransform::SetRotationZ(const vtkm::FloatDefault& angleDegrees)
{
this->Worklet.SetRotationZ(angleDegrees);
}
//-----------------------------------------------------------------------------
template <typename S>
inline VTKM_CONT void PointTransform<S>::SetScale(const S& s)
inline VTKM_CONT void PointTransform::SetScale(const vtkm::FloatDefault& s)
{
this->Worklet.SetScale(s);
}
//-----------------------------------------------------------------------------
template <typename S>
inline VTKM_CONT void PointTransform<S>::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 <typename S>
inline VTKM_CONT void PointTransform<S>::SetScale(const vtkm::Vec<S, 3>& v)
inline VTKM_CONT void PointTransform::SetScale(const vtkm::Vec3f& v)
{
this->Worklet.SetScale(v);
}
//-----------------------------------------------------------------------------
template <typename S>
inline VTKM_CONT void PointTransform<S>::SetTransform(const vtkm::Matrix<S, 4, 4>& mtx)
inline VTKM_CONT void PointTransform::SetTransform(
const vtkm::Matrix<vtkm::FloatDefault, 4, 4>& 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 <typename S>
template <typename T, typename StorageType, typename DerivedPolicy>
inline VTKM_CONT vtkm::cont::DataSet PointTransform<S>::DoExecute(
inline VTKM_CONT vtkm::cont::DataSet PointTransform::DoExecute(
const vtkm::cont::DataSet& inDataSet,
const vtkm::cont::ArrayHandle<T, StorageType>& field,
const vtkm::filter::FieldMetadata& fieldMetadata,
@ -115,7 +124,19 @@ inline VTKM_CONT vtkm::cont::DataSet PointTransform<S>::DoExecute(
vtkm::cont::ArrayHandle<T> 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

@ -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<vtkm::FloatDefault> 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<vtkm::FloatDefault> 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<vtkm::FloatDefault> 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));

@ -32,64 +32,39 @@ public:
PointTransform() {}
//Translation
template <typename S>
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<T>(tx), static_cast<T>(ty), static_cast<T>(tz));
matrix = vtkm::Transform3DTranslate(tx, ty, tz);
}
template <typename S>
VTKM_CONT void SetTranslation(const vtkm::Vec<S, 3>& v)
{
SetTranslation(v[0], v[1], v[2]);
}
VTKM_CONT void SetTranslation(const vtkm::Vec<T, 3>& v) { SetTranslation(v[0], v[1], v[2]); }
//Rotation
template <typename S>
VTKM_CONT void SetRotation(const S& angleDegrees, const vtkm::Vec<S, 3>& axis)
VTKM_CONT void SetRotation(const T& angleDegrees, const vtkm::Vec<T, 3>& axis)
{
matrix = vtkm::Transform3DRotate(angleDegrees, axis);
}
template <typename S>
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<S, 3>(rx, ry, rz));
SetRotation(angleDegrees, { rx, ry, rz });
}
template <typename S>
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 <typename S>
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 <typename S>
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 <typename S>
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 <typename S>
VTKM_CONT void SetScale(const S& sx, const S& sy, const S& sz)
{
matrix = vtkm::Transform3DScale(static_cast<T>(sx), static_cast<T>(sy), static_cast<T>(sz));
}
template <typename S>
VTKM_CONT void SetScale(const vtkm::Vec<S, 3>& v)
VTKM_CONT void SetScale(const vtkm::Vec<T, 3>& v)
{
matrix = vtkm::Transform3DScale(v[0], v[1], v[2]);
}