Commit 912d393b authored by Dave Pugmire's avatar Dave Pugmire Committed by Kitware Robot
Browse files

Merge topic 'coordSysFilter'

80b12e32 Merge branch 'coordSysFilter' of https://gitlab.kitware.com/dpugmire/vtk-m into coordSysFilter
ab5eeab1 Fixes for making the filters non-templated.
27dade14 Fixes for coordinate systems w/ help from Sujin.
db5ded3a Add files for coord sys transform filters.
17087a26

 Filter for coordinate system transform.
Acked-by: Kitware Robot's avatarKitware Robot <kwrobot@kitware.com>
Acked-by: Kenneth Moreland's avatarKenneth Moreland <kmorel@sandia.gov>
Merge-request: !1284
parents a8305bad 80b12e32
......@@ -25,6 +25,7 @@ set(headers
ClipWithField.h
ClipWithImplicitFunction.h
ContourTreeUniform.h
CoordinateSystemTransform.h
CrossProduct.h
DotProduct.h
Entropy.h
......@@ -71,6 +72,7 @@ set(header_template_sources
ClipWithField.hxx
ClipWithImplicitFunction.hxx
ContourTreeUniform.hxx
CoordinateSystemTransform.hxx
CrossProduct.hxx
DotProduct.hxx
Entropy.hxx
......
//============================================================================
// 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.
//
// Copyright 2014 National Technology & Engineering Solutions of Sandia, LLC (NTESS).
// Copyright 2014 UT-Battelle, LLC.
// Copyright 2014 Los Alamos National Security.
//
// Under the terms of Contract DE-NA0003525 with NTESS,
// the U.S. Government retains certain rights in this software.
//
// Under the terms of Contract DE-AC52-06NA25396 with Los Alamos National
// Laboratory (LANL), the U.S. Government retains certain rights in
// this software.
//============================================================================
#ifndef vtk_m_filter_CoordianteSystemTransform_h
#define vtk_m_filter_CoordianteSystemTransform_h
#include <vtkm/filter/FilterField.h>
#include <vtkm/worklet/CoordinateSystemTransform.h>
namespace vtkm
{
namespace filter
{
/// \brief
///
/// Generate a coordinate transformation on coordiantes from a dataset.
class CylindricalCoordinateTransform
: public vtkm::filter::FilterField<CylindricalCoordinateTransform>
{
public:
VTKM_CONT
CylindricalCoordinateTransform();
VTKM_CONT void SetCartesianToCylindrical() { Worklet.SetCartesianToCylindrical(); }
VTKM_CONT void SetCylindricalToCartesian() { Worklet.SetCylindricalToCartesian(); }
template <typename T, typename StorageType, typename DerivedPolicy, typename DeviceAdapter>
VTKM_CONT vtkm::cont::DataSet DoExecute(const vtkm::cont::DataSet& input,
const vtkm::cont::ArrayHandle<T, StorageType>& field,
const vtkm::filter::FieldMetadata& fieldMeta,
const vtkm::filter::PolicyBase<DerivedPolicy>& policy,
const DeviceAdapter& tag);
private:
vtkm::worklet::CylindricalCoordinateTransform Worklet;
};
template <>
class FilterTraits<CylindricalCoordinateTransform>
{
public:
//Point Elevation can only convert Float and Double Vec3 arrays
using InputFieldTypeList = vtkm::TypeListTagFieldVec3;
};
class SphericalCoordinateTransform : public vtkm::filter::FilterField<SphericalCoordinateTransform>
{
public:
VTKM_CONT
SphericalCoordinateTransform();
VTKM_CONT void SetCartesianToSpherical() { Worklet.SetCartesianToSpherical(); }
VTKM_CONT void SetSphericalToCartesian() { Worklet.SetSphericalToCartesian(); }
template <typename T, typename StorageType, typename DerivedPolicy, typename DeviceAdapter>
VTKM_CONT vtkm::cont::DataSet DoExecute(const vtkm::cont::DataSet& input,
const vtkm::cont::ArrayHandle<T, StorageType>& field,
const vtkm::filter::FieldMetadata& fieldMeta,
const vtkm::filter::PolicyBase<DerivedPolicy>& policy,
const DeviceAdapter& tag) const;
private:
vtkm::worklet::SphericalCoordinateTransform Worklet;
};
template <>
class FilterTraits<SphericalCoordinateTransform>
{
public:
//Point Elevation can only convert Float and Double Vec3 arrays
using InputFieldTypeList = vtkm::TypeListTagFieldVec3;
};
}
} // namespace vtkm::filter
#include <vtkm/filter/CoordinateSystemTransform.hxx>
#endif // vtk_m_filter_CoordianteSystemTransform_h
//============================================================================
// 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.
//
// Copyright 2014 National Technology & Engineering Solutions of Sandia, LLC (NTESS).
// Copyright 2014 UT-Battelle, LLC.
// Copyright 2014 Los Alamos National Security.
//
// Under the terms of Contract DE-NA0003525 with NTESS,
// the U.S. Government retains certain rights in this software.
//
// Under the terms of Contract DE-AC52-06NA25396 with Los Alamos National
// Laboratory (LANL), the U.S. Government retains certain rights in
// this software.
//============================================================================
#include <vtkm/filter/internal/CreateResult.h>
#include <vtkm/worklet/DispatcherMapField.h>
namespace vtkm
{
namespace filter
{
//-----------------------------------------------------------------------------
inline VTKM_CONT CylindricalCoordinateTransform::CylindricalCoordinateTransform()
: Worklet()
{
this->SetOutputFieldName("cylindricalCoordinateSystemTransform");
}
//-----------------------------------------------------------------------------
template <typename T, typename StorageType, typename DerivedPolicy, typename DeviceAdapter>
inline VTKM_CONT vtkm::cont::DataSet CylindricalCoordinateTransform::DoExecute(
const vtkm::cont::DataSet& inDataSet,
const vtkm::cont::ArrayHandle<T, StorageType>& field,
const vtkm::filter::FieldMetadata& fieldMetadata,
const vtkm::filter::PolicyBase<DerivedPolicy>&,
const DeviceAdapter& device)
{
vtkm::cont::ArrayHandle<T> outArray;
Worklet.Run(field, outArray, device);
return internal::CreateResult(inDataSet,
outArray,
this->GetOutputFieldName(),
fieldMetadata.GetAssociation(),
fieldMetadata.GetCellSetName());
}
//-----------------------------------------------------------------------------
inline VTKM_CONT SphericalCoordinateTransform::SphericalCoordinateTransform()
: Worklet()
{
this->SetOutputFieldName("sphericalCoordinateSystemTransform");
}
//-----------------------------------------------------------------------------
template <typename T, typename StorageType, typename DerivedPolicy, typename DeviceAdapter>
inline VTKM_CONT vtkm::cont::DataSet SphericalCoordinateTransform::DoExecute(
const vtkm::cont::DataSet& inDataSet,
const vtkm::cont::ArrayHandle<T, StorageType>& field,
const vtkm::filter::FieldMetadata& fieldMetadata,
const vtkm::filter::PolicyBase<DerivedPolicy>&,
const DeviceAdapter& device) const
{
vtkm::cont::ArrayHandle<T> outArray;
Worklet.Run(field, outArray, device);
return internal::CreateResult(inDataSet,
outArray,
this->GetOutputFieldName(),
fieldMetadata.GetAssociation(),
fieldMetadata.GetCellSetName());
}
}
} // namespace vtkm::filter
......@@ -25,6 +25,7 @@ set(unit_tests
UnitTestClipWithFieldFilter.cxx
UnitTestClipWithImplicitFunctionFilter.cxx
UnitTestContourTreeUniformFilter.cxx
UnitTestCoordinateSystemTransform.cxx
UnitTestCrossProductFilter.cxx
UnitTestDotProductFilter.cxx
UnitTestEntropyFilter.cxx
......
//============================================================================
// 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.
//
// Copyright 2014 National Technology & Engineering Solutions of Sandia, LLC (NTESS).
// Copyright 2014 UT-Battelle, LLC.
// Copyright 2014 Los Alamos National Security.
//
// Under the terms of Contract DE-NA0003525 with NTESS,
// the U.S. Government retains certain rights in this software.
//
// Under the terms of Contract DE-AC52-06NA25396 with Los Alamos National
// Laboratory (LANL), the U.S. Government retains certain rights in
// this software.
//============================================================================
#include <vtkm/cont/testing/Testing.h>
#include <vtkm/filter/CoordinateSystemTransform.h>
#include <string>
#include <vector>
namespace
{
enum CoordinateType
{
CART = 0,
CYL,
SPH
};
vtkm::cont::DataSet MakeTestDataSet(const CoordinateType& cType)
{
vtkm::cont::DataSet dataSet;
std::vector<vtkm::Vec<vtkm::FloatDefault, 3>> coordinates;
const vtkm::Id dim = 5;
if (cType == CART)
{
for (vtkm::Id j = 0; j < dim; ++j)
{
vtkm::FloatDefault z =
static_cast<vtkm::FloatDefault>(j) / static_cast<vtkm::FloatDefault>(dim - 1);
for (vtkm::Id i = 0; i < dim; ++i)
{
vtkm::FloatDefault x =
static_cast<vtkm::FloatDefault>(i) / static_cast<vtkm::FloatDefault>(dim - 1);
vtkm::FloatDefault y = (x * x + z * z) / 2.0f;
coordinates.push_back(vtkm::make_Vec(x + 0, y + 0, z + 0));
}
}
}
else if (cType == CYL)
{
vtkm::FloatDefault R = 1.0f;
for (vtkm::Id j = 0; j < dim; j++)
{
vtkm::FloatDefault Z =
static_cast<vtkm::FloatDefault>(j) / static_cast<vtkm::FloatDefault>(dim - 1);
for (vtkm::Id i = 0; i < dim; i++)
{
vtkm::FloatDefault Theta = vtkm::TwoPif() *
(static_cast<vtkm::FloatDefault>(i) / static_cast<vtkm::FloatDefault>(dim - 1));
coordinates.push_back(vtkm::make_Vec(R, Theta, Z));
}
}
}
else if (cType == SPH)
{
//Spherical coordinates have some degenerate cases, so provide some good cases.
vtkm::FloatDefault R = 1.0f;
vtkm::FloatDefault eps = vtkm::Epsilon<float>();
std::vector<vtkm::FloatDefault> Thetas = {
eps, vtkm::Pif() / 4, vtkm::Pif() / 3, vtkm::Pif() / 2, vtkm::Pif() - eps
};
std::vector<vtkm::FloatDefault> Phis = {
eps, vtkm::TwoPif() / 4, vtkm::TwoPif() / 3, vtkm::TwoPif() / 2, vtkm::TwoPif() - eps
};
for (std::size_t i = 0; i < Thetas.size(); i++)
for (std::size_t j = 0; j < Phis.size(); j++)
coordinates.push_back(vtkm::make_Vec(R, Thetas[i], Phis[j]));
}
vtkm::Id numCells = (dim - 1) * (dim - 1);
dataSet.AddCoordinateSystem(
vtkm::cont::make_CoordinateSystem("coordinates", coordinates, vtkm::CopyFlag::On));
vtkm::cont::CellSetExplicit<> cellSet("cells");
cellSet.PrepareToAddCells(numCells, numCells * 4);
for (vtkm::Id j = 0; j < dim - 1; ++j)
{
for (vtkm::Id i = 0; i < dim - 1; ++i)
{
cellSet.AddCell(vtkm::CELL_SHAPE_QUAD,
4,
vtkm::make_Vec<vtkm::Id>(
j * dim + i, j * dim + i + 1, (j + 1) * dim + i + 1, (j + 1) * dim + i));
}
}
cellSet.CompleteAddingCells(vtkm::Id(coordinates.size()));
dataSet.AddCellSet(cellSet);
return dataSet;
}
void ValidateCoordTransform(const vtkm::cont::DataSet& ds,
const vtkm::cont::DataSet& dsTrn,
const std::vector<bool>& isAngle)
{
auto points = ds.GetCoordinateSystem().GetData();
auto pointsTrn = dsTrn.GetCoordinateSystem().GetData();
VTKM_TEST_ASSERT(points.GetNumberOfValues() == pointsTrn.GetNumberOfValues(),
"Incorrect number of points in point transform");
auto pointsPortal = points.GetPortalConstControl();
auto pointsTrnPortal = pointsTrn.GetPortalConstControl();
for (vtkm::Id i = 0; i < points.GetNumberOfValues(); i++)
{
vtkm::Vec<vtkm::FloatDefault, 3> p = pointsPortal.Get(i);
vtkm::Vec<vtkm::FloatDefault, 3> r = pointsTrnPortal.Get(i);
bool isEqual = true;
for (vtkm::IdComponent j = 0; j < 3; j++)
{
if (isAngle[static_cast<std::size_t>(j)])
isEqual &= (test_equal(p[j], r[j]) || test_equal(p[j] + vtkm::TwoPif(), r[j]) ||
test_equal(p[j], r[j] + vtkm::TwoPif()));
else
isEqual &= test_equal(p[j], r[j]);
}
VTKM_TEST_ASSERT(isEqual, "Wrong result for PointTransform worklet");
}
}
}
void TestCoordinateSystemTransform()
{
std::cout << "Testing CylindricalCoordinateTransform Filter" << std::endl;
//Test cartesian to cyl
vtkm::cont::DataSet dsCart = MakeTestDataSet(CART);
vtkm::filter::CylindricalCoordinateTransform cylTrn;
cylTrn.SetOutputFieldName("cylindricalCoords");
cylTrn.SetUseCoordinateSystemAsField(true);
cylTrn.SetCartesianToCylindrical();
vtkm::cont::DataSet carToCylDataSet = cylTrn.Execute(dsCart);
cylTrn.SetCylindricalToCartesian();
cylTrn.SetUseCoordinateSystemAsField(true);
cylTrn.SetOutputFieldName("cartesianCoords");
vtkm::cont::DataSet cylToCarDataSet = cylTrn.Execute(carToCylDataSet);
ValidateCoordTransform(dsCart, cylToCarDataSet, { false, false, false });
//Test cyl to cart.
vtkm::cont::DataSet dsCyl = MakeTestDataSet(CYL);
cylTrn.SetCylindricalToCartesian();
cylTrn.SetUseCoordinateSystemAsField(true);
cylTrn.SetOutputFieldName("cartesianCoords");
cylToCarDataSet = cylTrn.Execute(dsCyl);
cylTrn.SetCartesianToCylindrical();
cylTrn.SetUseCoordinateSystemAsField(true);
cylTrn.SetOutputFieldName("cylindricalCoords");
carToCylDataSet = cylTrn.Execute(cylToCarDataSet);
ValidateCoordTransform(dsCyl, carToCylDataSet, { false, true, false });
std::cout << "Testing SphericalCoordinateTransform Filter" << std::endl;
vtkm::filter::SphericalCoordinateTransform sphTrn;
sphTrn.SetOutputFieldName("sphericalCoords");
sphTrn.SetUseCoordinateSystemAsField(true);
sphTrn.SetCartesianToSpherical();
vtkm::cont::DataSet carToSphDataSet = sphTrn.Execute(dsCart);
sphTrn.SetOutputFieldName("cartesianCoords");
sphTrn.SetUseCoordinateSystemAsField(true);
sphTrn.SetSphericalToCartesian();
vtkm::cont::DataSet sphToCarDataSet = sphTrn.Execute(carToSphDataSet);
ValidateCoordTransform(dsCart, sphToCarDataSet, { false, true, true });
vtkm::cont::DataSet dsSph = MakeTestDataSet(SPH);
sphTrn.SetSphericalToCartesian();
sphTrn.SetUseCoordinateSystemAsField(true);
sphTrn.SetOutputFieldName("sphericalCoords");
sphToCarDataSet = sphTrn.Execute(dsSph);
sphTrn.SetCartesianToSpherical();
sphTrn.SetUseCoordinateSystemAsField(true);
sphTrn.SetOutputFieldName("sphericalCoords");
carToSphDataSet = cylTrn.Execute(sphToCarDataSet);
ValidateCoordTransform(dsSph, carToSphDataSet, { false, true, true });
}
int UnitTestCoordinateSystemTransform(int, char* [])
{
return vtkm::cont::testing::Testing::Run(TestCoordinateSystemTransform);
}
......@@ -123,7 +123,6 @@ struct CarToSphere : public vtkm::worklet::WorkletMapField
};
};
template <typename T>
class CylindricalCoordinateTransform
{
public:
......@@ -136,9 +135,9 @@ public:
VTKM_CONT void SetCartesianToCylindrical() { cartesianToCylindrical = true; }
VTKM_CONT void SetCylindricalToCartesian() { cartesianToCylindrical = false; }
template <typename CoordsStorageType, typename DeviceAdapterTag>
void Run(const vtkm::cont::ArrayHandle<vtkm::Vec<T, 3>, CoordsStorageType>& inPoints,
vtkm::cont::ArrayHandle<vtkm::Vec<T, 3>, CoordsStorageType>& outPoints,
template <typename T, typename InStorageType, typename OutStorageType, typename DeviceAdapterTag>
void Run(const vtkm::cont::ArrayHandle<vtkm::Vec<T, 3>, InStorageType>& inPoints,
vtkm::cont::ArrayHandle<vtkm::Vec<T, 3>, OutStorageType>& outPoints,
DeviceAdapterTag) const
{
if (cartesianToCylindrical)
......@@ -153,7 +152,7 @@ public:
}
}
template <typename CoordsStorageType, typename DeviceAdapterTag>
template <typename T, typename CoordsStorageType, typename DeviceAdapterTag>
void Run(const vtkm::cont::CoordinateSystem& inPoints,
vtkm::cont::ArrayHandle<vtkm::Vec<T, 3>, CoordsStorageType>& outPoints,
DeviceAdapterTag) const
......@@ -174,7 +173,6 @@ private:
bool cartesianToCylindrical;
};
template <typename T>
class SphericalCoordinateTransform
{
public:
......@@ -187,9 +185,9 @@ public:
VTKM_CONT void SetCartesianToSpherical() { CartesianToSpherical = true; }
VTKM_CONT void SetSphericalToCartesian() { CartesianToSpherical = false; }
template <typename CoordsStorageType, typename DeviceAdapterTag>
void Run(const vtkm::cont::ArrayHandle<vtkm::Vec<T, 3>, CoordsStorageType>& inPoints,
vtkm::cont::ArrayHandle<vtkm::Vec<T, 3>, CoordsStorageType>& outPoints,
template <typename T, typename InStorageType, typename OutStorageType, typename DeviceAdapterTag>
void Run(const vtkm::cont::ArrayHandle<vtkm::Vec<T, 3>, InStorageType>& inPoints,
vtkm::cont::ArrayHandle<vtkm::Vec<T, 3>, OutStorageType>& outPoints,
DeviceAdapterTag) const
{
if (CartesianToSpherical)
......@@ -204,7 +202,7 @@ public:
}
}
template <typename CoordsStorageType, typename DeviceAdapterTag>
template <typename T, typename CoordsStorageType, typename DeviceAdapterTag>
void Run(const vtkm::cont::CoordinateSystem& inPoints,
vtkm::cont::ArrayHandle<vtkm::Vec<T, 3>, CoordsStorageType>& outPoints,
DeviceAdapterTag) const
......
......@@ -153,7 +153,7 @@ void TestCoordinateSystemTransform()
//Test cartesian to cyl
vtkm::cont::DataSet dsCart = MakeTestDataSet(CART);
vtkm::worklet::CylindricalCoordinateTransform<vtkm::FloatDefault> cylTrn;
vtkm::worklet::CylindricalCoordinateTransform cylTrn;
vtkm::cont::ArrayHandle<vtkm::Vec<vtkm::FloatDefault, 3>> carToCylPts;
vtkm::cont::ArrayHandle<vtkm::Vec<vtkm::FloatDefault, 3>> revResult;
......@@ -179,7 +179,7 @@ void TestCoordinateSystemTransform()
//Spherical transform
//Test cartesian to sph
vtkm::worklet::SphericalCoordinateTransform<vtkm::FloatDefault> sphTrn;
vtkm::worklet::SphericalCoordinateTransform sphTrn;
vtkm::cont::ArrayHandle<vtkm::Vec<vtkm::FloatDefault, 3>> carToSphPts;
sphTrn.SetCartesianToSpherical();
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment