Add classes to manage point coordinates.

Each type of point coordinates has its own class with the name
PointCoordinates*. Currently there is a PointCoordiantesArray that contains
an ArrayHandle holding the point coordinates and a PointCoordinatesUniform
that takes the standard extent, origin, and spacing for a uniform rectilinear
grid and defines point coordiantes for that. Creating new PointCoordinates
arrays is pretty easy, and we will almost definitely add more. For example,
we should have an elevation version that takes uniform coordinates for
a 2D grid and then an elevation in the third dimension. We can probably
also use a basic composite point coordinates that can build them from
other coordinates.

There is also a DynamicPointCoordinates class that polymorphically stores
an instance of a PointCoordinates class. It has a CastAndCall method that
behaves like DynamicArrayHandle; it can call a functor with an array handle
(possible implicit) that holds the point coordinates.
This commit is contained in:
Kenneth Moreland 2014-05-14 15:05:38 -06:00
parent ff31afca3e
commit 7d769a8f4a
15 changed files with 1093 additions and 8 deletions

@ -26,6 +26,8 @@ namespace vtkm {
namespace detail {
struct ListEmpty { };
template<typename T>
struct ListBase { };
@ -45,6 +47,12 @@ struct ListJoin { };
template<typename T> struct ListTagBase;
/// A special tag for an empty list.
///
struct ListTagEmpty {
typedef detail::ListEmpty List;
};
/// A basic tag for a list of one typename. This struct can be subclassed
/// and still behave like a list tag.
template<typename T>
@ -90,6 +98,13 @@ void ListForEach(const Functor &f, ListTag);
namespace detail {
template<typename Functor>
VTKM_CONT_EXPORT
void ListForEachImpl(Functor, ListEmpty)
{
// Nothing to do for empty list
}
template<typename Functor, typename T>
VTKM_CONT_EXPORT
void ListForEachImpl(Functor &f, ListBase<T>)

@ -34,6 +34,7 @@ set(headers
DeviceAdapter.h
DeviceAdapterSerial.h
DynamicArrayHandle.h
DynamicPointCoordinates.h
Error.h
ErrorControl.h
ErrorControlAssert.h
@ -41,6 +42,9 @@ set(headers
ErrorControlInternal.h
ErrorControlOutOfMemory.h
ErrorExecution.h
PointCoordinatesArray.h
PointCoordinatesListTag.h
PointCoordinatesUniform.h
)
#-----------------------------------------------------------------------------

@ -27,6 +27,7 @@
#include <vtkm/ListTag.h>
#include <vtkm/cont/ArrayContainerControl.h>
#include <vtkm/cont/ArrayContainerControlBasic.h>
namespace vtkm {
namespace cont {

@ -46,6 +46,33 @@ class DynamicArrayHandleCast;
} // namespace internal
/// \brief Holds an array handle without having to specify template parameters.
///
/// \c DynamicArrayHandle holds an \c ArrayHandle object using runtime
/// polymorphism to manage different value types and storage rather than
/// compile-time templates. This adds a programming convienience that helps
/// avoid a proliferation of templates. It also provides the management
/// necessary to interface VTK-m with data sources where types will not be
/// known until runtime.
///
/// To interface between the runtime polymorphism and the templated algorithms
/// in VTK-m, \c DynamicArrayHandle contains a method named \c CastAndCall that
/// will determine the correct type from some known list of types and
/// containers. This mechanism is used internally by VTK-m's worklet invocation
/// mechanism to determine the type when running algorithms.
///
/// By default, \c DynamicArrayHandle will assume that the value type in the
/// array matches one of the types specified by \c VTKM_DEFAULT_TYPE_LIST_TAG
/// and the container matches one of the tags specified by \c
/// VTKM_DEFAULT_CONTAINER_LIST_TAG. These lists can be changed by using the \c
/// ResetTypeList and \c ResetContainerList methods, respectively. It is
/// worthwhile to match these lists closely to the possible types that might be
/// used. If a type is missing you will get a runtime error. If there are more
/// types than necessary, then the template mechanism will create a lot of
/// object code that is never used, and keep in mind that the number of
/// combinations grows exponentally when using multiple \c DynamicArrayHandle
/// objects.
///
class DynamicArrayHandle
{
public:
@ -86,9 +113,9 @@ public:
vtkm::cont::ArrayHandle<Type,Container> > *container =
this->TryCastArrayContainer<Type,Container>();
if (container == NULL)
{
{
throw vtkm::cont::ErrorControlBadValue("Bad cast of dynamic array.");
}
}
return container->Item;
}
@ -237,7 +264,7 @@ void DynamicArrayHandle::CastAndCall(const Functor &f,
if (!tryType.FoundCast)
{
throw vtkm::cont::ErrorControlBadValue(
"Could not find appropriate cast in CastAndCall.");
"Could not find appropriate cast for array in CastAndCall.");
}
}

@ -0,0 +1,388 @@
//============================================================================
// 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 Sandia Corporation.
// Copyright 2014 UT-Battelle, LLC.
// Copyright 2014. Los Alamos National Security
//
// Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
// 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_cont_DynamicPointCoordinates_h
#define vtk_m_cont_DynamicPointCoordinates_h
#include <vtkm/cont/DynamicArrayHandle.h>
#include <vtkm/cont/ErrorControlBadValue.h>
#include <vtkm/cont/PointCoordinatesArray.h>
#include <vtkm/cont/PointCoordinatesListTag.h>
#include <vtkm/cont/internal/DynamicTransform.h>
#include <boost/shared_ptr.hpp>
namespace vtkm {
namespace cont {
namespace internal {
/// Behaves like (and is interchangable with) a \c DynamicPointCoordinates. The
/// difference is that the lists of point coordinates, base types, and
/// containers to try when calling \c CastAndCall is set to the class template
/// arguments.
///
template<typename PointCoordinatesList,
typename TypeList,
typename ContainerList>
class DynamicPointCoordinatesCast;
} // namespace internal
/// \brief Holds point coordinates polymorphically.
///
/// The \c DynamicPointCoordinates holds a point coordinate field for a mesh.
/// Like a \c DynamicArrayHandle it contains a \c CastAndCall method that
/// allows it to interface with templated functions and will automatically be
/// converted on a worklet invoke.
///
/// \c DynamicPointCoordinates differes from \c DynamicArrayHandle in the type
/// of arrays it will check. Point coordinates are often defined as implicit
/// (uniform), semi-implicit (structured), unstructured, or some combination
/// thereof. Methods for defining point coordinates are captured in \c
/// PointCoordinates classes, and \c DynamicPointCoordinates polymorphically
/// stores one of these \c PointCoordinates objects.
///
/// By default, \c DynamicPointCoordinates will assume that the stored point
/// coordinates are of a type specified by \c
/// VTKM_DEFAULT_POINT_COORDINATES_LIST_TAG. This can be overriden by using the
/// \c ResetPointCoordinatesList method.
///
/// Internally, some \c PointCoordinates objects will reference dynamic arrays.
/// Thus, \c DynamicPointCoordinates also maintains lists of types and
/// containers that these subarrays might use. These default to \c
/// VTKM_DEFAULT_TYPE_LIST_TAG and \c VTKM_DEFAULT_CONTAINER_LIST_TAG and can
/// be changed with the \c ResetTypeList and \c ResetContainerList methods.
///
class DynamicPointCoordinates
{
public:
VTKM_CONT_EXPORT
DynamicPointCoordinates() { }
/// Special constructor for the common case of using a basic array to store
/// point coordinates.
///
VTKM_CONT_EXPORT
DynamicPointCoordinates(const vtkm::cont::DynamicArrayHandle &array)
: PointCoordinatesContainer(new vtkm::cont::PointCoordinatesArray(array))
{ }
/// Special constructor for the common case of using a basic array to store
/// point coordinates.
///
template<typename Container>
VTKM_CONT_EXPORT
DynamicPointCoordinates(
const vtkm::cont::ArrayHandle<vtkm::Vector3,Container> &array)
: PointCoordinatesContainer(new vtkm::cont::PointCoordinatesArray(array))
{ }
/// Takes a concrete point coordinates class and stores it polymorphically.
/// Although the template will match any possible type, there is a check
/// to make sure that the type is a valid point coordinates class. If you get
/// a compile error in the check, follow the instantiation list to where the
/// constructor is called.
///
template<typename PointCoordinatesType>
VTKM_CONT_EXPORT
DynamicPointCoordinates(const PointCoordinatesType &pointCoordinates)
: PointCoordinatesContainer(new PointCoordinatesType(pointCoordinates))
{
VTKM_IS_POINT_COORDINATES(PointCoordinatesType);
}
/// Returns true if these point coordinates are stored in a \c
/// PointCoordinates class of the given type.
///
template<typename PointCoordinatesType>
VTKM_CONT_EXPORT
bool IsPointCoordinateType(PointCoordinatesType = PointCoordinatesType()) const
{
VTKM_IS_POINT_COORDINATES(PointCoordinatesType);
return (this->TryCastPointCoordinatesType<PointCoordinatesType>() != NULL);
}
/// Returns these point coordinates in a \c PointCoordinates class of the
/// given type. Throws \c ErrorControlBadValue if the cast does not work. Use
/// \c IsPointCoordinateType to check if the cast can happen.
///
template<typename PointCoordinatesType>
VTKM_CONT_EXPORT
const PointCoordinatesType &
CastToPointCoordinates(PointCoordinatesType = PointCoordinatesType()) const {
VTKM_IS_POINT_COORDINATES(PointCoordinatesType);
PointCoordinatesType *pointCoordinates =
this->TryCastPointCoordinatesType<PointCoordinatesType>();
if (pointCoordinates == NULL)
{
throw vtkm::cont::ErrorControlBadValue(
"Bad cast of dynamic point coordinates.");
}
return *pointCoordinates;
}
/// Changes the point coordinates objects to try casting to when resolving
/// dynamic arrays within the point coordinates container, which is specified
/// with a list tag like those in PointCoordinatesListTag.h. Since C++ does
/// not allow you to actually change the template arguments, this method
/// returns a new dynamic array object. This method is particularly useful to
/// narrow down (or expand) the types when using an array of particular
/// constraints.
///
template<typename NewPointCoordinatesList>
VTKM_CONT_EXPORT
internal::DynamicPointCoordinatesCast<
NewPointCoordinatesList,
VTKM_DEFAULT_TYPE_LIST_TAG,
VTKM_DEFAULT_CONTAINER_LIST_TAG>
ResetPointCoordinatesList(
NewPointCoordinatesList = NewPointCoordinatesList()) const {
return internal::DynamicPointCoordinatesCast<
NewPointCoordinatesList,
VTKM_DEFAULT_TYPE_LIST_TAG,
VTKM_DEFAULT_CONTAINER_LIST_TAG>(*this);
}
/// Changes the types to try casting to when resolving dynamic arrays within
/// the point coordinates container, which is specified with a list tag like
/// those in TypeListTag.h. Since C++ does not allow you to actually change
/// the template arguments, this method returns a new dynamic array object.
/// This method is particularly useful to narrow down (or expand) the types
/// when using an array of particular constraints.
///
template<typename NewTypeList>
VTKM_CONT_EXPORT
internal::DynamicPointCoordinatesCast<
VTKM_DEFAULT_POINT_COORDINATES_LIST_TAG,
NewTypeList,
VTKM_DEFAULT_CONTAINER_LIST_TAG>
ResetTypeList(NewTypeList = NewTypeList()) const {
return internal::DynamicPointCoordinatesCast<
VTKM_DEFAULT_POINT_COORDINATES_LIST_TAG,
NewTypeList,
VTKM_DEFAULT_CONTAINER_LIST_TAG>(*this);
}
/// Changes the array containers to try casting to when resolving dynamic
/// arrays within the point coordinates container, which is specified with a
/// list tag like those in ContainerListTag.h. Since C++ does not allow you
/// to actually change the template arguments, this method returns a new
/// dynamic array object. This method is particularly useful to narrow down
/// (or expand) the types when using an array of particular constraints.
///
template<typename NewContainerList>
VTKM_CONT_EXPORT
internal::DynamicPointCoordinatesCast<
VTKM_DEFAULT_POINT_COORDINATES_LIST_TAG,
VTKM_DEFAULT_TYPE_LIST_TAG,
NewContainerList>
ResetContainerList(NewContainerList = NewContainerList()) const {
return internal::DynamicPointCoordinatesCast<
VTKM_DEFAULT_POINT_COORDINATES_LIST_TAG,
VTKM_DEFAULT_TYPE_LIST_TAG,
NewContainerList>(*this);
}
/// Attempts to cast the held point coordinates to a specific array
/// representation and then call the given functor with the cast array. This
/// is generally done in two parts. The first part finds the concrete type of
/// \c PointCoordinates object by trying all those in \c
/// VTKM_DEFAULT_POINT_COORDINATES_LIST_TAG.
///
/// The second part then asks the concrete \c PointCoordinates object to cast
/// and call to a concrete array. This second cast might rely on \c
/// VTKM_DEFAULT_TYPE_LIST_TAG and \c VTKM_DEFAULT_CONTAINER_LIST_TAG.
///
template<typename Functor>
VTKM_CONT_EXPORT
void CastAndCall(const Functor &f) const
{
this->CastAndCall(f,
VTKM_DEFAULT_POINT_COORDINATES_LIST_TAG(),
VTKM_DEFAULT_TYPE_LIST_TAG(),
VTKM_DEFAULT_CONTAINER_LIST_TAG());
}
/// A version of \c CastAndCall that tries specified lists of point
/// coordinates, types, and containers.
///
template<typename Functor,
typename PointCoordinatesList,
typename TypeList,
typename ContainerList>
VTKM_CONT_EXPORT
void CastAndCall(const Functor &f,
PointCoordinatesList,
TypeList,
ContainerList) const;
private:
boost::shared_ptr<vtkm::cont::internal::PointCoordinatesBase>
PointCoordinatesContainer;
template<typename PointCoordinatesType>
VTKM_CONT_EXPORT
PointCoordinatesType *
TryCastPointCoordinatesType() const {
VTKM_IS_POINT_COORDINATES(PointCoordinatesType);
return dynamic_cast<PointCoordinatesType *>(
this->PointCoordinatesContainer.get());
}
};
namespace detail {
template<typename Functor, typename TypeList, typename ContainerList>
struct DynamicPointCoordinatesTryContainer
{
const DynamicPointCoordinates PointCoordinates;
const Functor &Function;
bool FoundCast;
VTKM_CONT_EXPORT
DynamicPointCoordinatesTryContainer(
const DynamicPointCoordinates &pointCoordinates,
const Functor &f)
: PointCoordinates(pointCoordinates), Function(f), FoundCast(false)
{ }
template<typename PointCoordinatesType>
VTKM_CONT_EXPORT
void operator()(PointCoordinatesType) {
if (!this->FoundCast &&
this->PointCoordinates.IsPointCoordinateType(PointCoordinatesType()))
{
PointCoordinatesType pointCoordinates =
this->PointCoordinates.CastToPointCoordinates(PointCoordinatesType());
pointCoordinates.CastAndCall(this->Function, TypeList(), ContainerList());
this->FoundCast = true;
}
}
};
} // namespace detail
template<typename Functor,
typename PointCoordinatesList,
typename TypeList,
typename ContainerList>
VTKM_CONT_EXPORT
void DynamicPointCoordinates::CastAndCall(const Functor &f,
PointCoordinatesList,
TypeList,
ContainerList) const
{
typedef detail::DynamicPointCoordinatesTryContainer<
Functor, TypeList, ContainerList> TryTypeType;
TryTypeType tryType = TryTypeType(*this, f);
vtkm::ListForEach(tryType, PointCoordinatesList());
if (!tryType.FoundCast)
{
throw vtkm::cont::ErrorControlBadValue(
"Could not find appropriate cast for point coordinates in CastAndCall.");
}
}
namespace internal {
template<typename PointCoordinatesList,
typename TypeList,
typename ContainerList>
class DynamicPointCoordinatesCast : public vtkm::cont::DynamicPointCoordinates
{
public:
VTKM_CONT_EXPORT
DynamicPointCoordinatesCast() : DynamicPointCoordinates() { }
VTKM_CONT_EXPORT
DynamicPointCoordinatesCast(const vtkm::cont::DynamicPointCoordinates &coords)
: DynamicPointCoordinates(coords) { }
template<typename SrcPointCoordinatesList,
typename SrcTypeList,
typename SrcContainerList>
VTKM_CONT_EXPORT
DynamicPointCoordinatesCast(
const DynamicPointCoordinatesCast<SrcPointCoordinatesList,SrcTypeList,SrcContainerList> &coords)
: DynamicPointCoordinates(coords)
{ }
template<typename NewPointCoordinatesList>
VTKM_CONT_EXPORT
DynamicPointCoordinatesCast<NewPointCoordinatesList,TypeList,ContainerList>
ResetPointCoordinatesList(
NewPointCoordinatesList = NewPointCoordinatesList()) const {
return DynamicPointCoordinatesCast<
NewPointCoordinatesList,TypeList,ContainerList>(*this);
}
template<typename NewTypeList>
VTKM_CONT_EXPORT
DynamicPointCoordinatesCast<PointCoordinatesList,NewTypeList,ContainerList>
ResetTypeList(NewTypeList = NewTypeList()) const {
return DynamicPointCoordinatesCast<
PointCoordinatesList,NewTypeList,ContainerList>(*this);
}
template<typename NewContainerList>
VTKM_CONT_EXPORT
DynamicPointCoordinatesCast<PointCoordinatesList,TypeList,NewContainerList>
ResetContainerList(NewContainerList = NewContainerList()) const {
return DynamicPointCoordinatesCast<
PointCoordinatesList,TypeList,NewContainerList>(*this);
}
template<typename Functor>
VTKM_CONT_EXPORT
void CastAndCall(const Functor &f) const
{
this->CastAndCall(f, PointCoordinatesList(), TypeList(), ContainerList());
}
template<typename Functor, typename PCL, typename TL, typename CL>
VTKM_CONT_EXPORT
void CastAndCall(const Functor &f, PCL, TL, CL) const
{
this->DynamicPointCoordinates::CastAndCall(f, PCL(), TL(), CL());
}
};
template<>
struct DynamicTransformTraits<vtkm::cont::DynamicPointCoordinates> {
typedef vtkm::cont::internal::DynamicTransformTagCastAndCall DynamicTag;
};
template<typename PointCoordinatesList,
typename TypeList,
typename ContainerList>
struct DynamicTransformTraits<
vtkm::cont::internal::DynamicPointCoordinatesCast<
PointCoordinatesList,TypeList,ContainerList> >
{
typedef vtkm::cont::internal::DynamicTransformTagCastAndCall DynamicTag;
};
} // namespace internal
}
} // namespace vtkm::cont
#endif //vtk_m_cont_DynamicPointCoordinates_h

@ -0,0 +1,75 @@
//============================================================================
// 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 Sandia Corporation.
// Copyright 2014 UT-Battelle, LLC.
// Copyright 2014. Los Alamos National Security
//
// Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
// 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_cont_PointCoordinatesArray_h
#define vtk_m_cont_PointCoordinatesArray_h
#include <vtkm/TypeListTag.h>
#include <vtkm/cont/ArrayHandle.h>
#include <vtkm/cont/DynamicArrayHandle.h>
#include <vtkm/cont/internal/PointCoordinatesBase.h>
namespace vtkm {
namespace cont {
/// \brief Point coordinates stored in a \c Vector3 array.
///
/// The \c PointCoordinatesArray class is a simple PointCoordinates class
/// that stores the point coordinates in a single array. The array is managed
/// by a \c DynamicArrayHandle.
///
/// Like other PointCoordinates classes, \c PointCoordinatesArray is intended
/// to be used in conjunction with \c DynamicPointCoordinates.
///
class PointCoordinatesArray : public internal::PointCoordinatesBase
{
public:
VTKM_CONT_EXPORT
PointCoordinatesArray() { }
VTKM_CONT_EXPORT
PointCoordinatesArray(const vtkm::cont::DynamicArrayHandle &array)
: Array(array) { }
template<typename Container>
VTKM_CONT_EXPORT
PointCoordinatesArray(
const vtkm::cont::ArrayHandle<vtkm::Vector3,Container> &array)
: Array(array) { }
/// In this \c CastAndCall, \c TypeList is ignored. All point coordinates are
/// expressed as Vector3, so that must be how the array is represented.
///
template<typename Functor, typename TypeList, typename ContainerList>
VTKM_CONT_EXPORT
void CastAndCall(const Functor &f, TypeList, ContainerList) const
{
this->Array.CastAndCall(f, vtkm::TypeListTagVector3(), ContainerList());
}
private:
vtkm::cont::DynamicArrayHandle Array;
};
}
} // namespace vtkm::cont
#endif //vtk_m_cont_PointCoordinatesArray_h

@ -0,0 +1,52 @@
//============================================================================
// 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 Sandia Corporation.
// Copyright 2014 UT-Battelle, LLC.
// Copyright 2014. Los Alamos National Security
//
// Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
// 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_cont_PointCoordinatesListTag_h
#define vtk_m_cont_PointCoordinatesListTag_h
#ifndef VTKM_DEFAULT_POINT_COORDINATES_LIST_TAG
#define VTKM_DEFAULT_POINT_COORDINATES_LIST_TAG \
::vtkm::cont::PointCoordinatesListTagCommon
#endif
#include <vtkm/ListTag.h>
#include <vtkm/cont/PointCoordinatesArray.h>
#include <vtkm/cont/PointCoordinatesUniform.h>
namespace vtkm {
namespace cont {
struct PointCoordinatesListTagArray :
vtkm::ListTagBase<vtkm::cont::PointCoordinatesArray> { };
struct PointCoordinatesListTagUniform :
vtkm::ListTagBase<vtkm::cont::PointCoordinatesUniform> { };
/// A list of the most commonly used point coordinate types. Includes \c
/// PointCoordinatesArray.
///
struct PointCoordinatesListTagCommon
: vtkm::ListTagBase2<
vtkm::cont::PointCoordinatesArray,
vtkm::cont::PointCoordinatesUniform>
{ };
}
} // namespace vtkm::cont
#endif //vtk_m_cont_PointCoordinatesListTag_h

@ -0,0 +1,70 @@
//============================================================================
// 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 Sandia Corporation.
// Copyright 2014 UT-Battelle, LLC.
// Copyright 2014. Los Alamos National Security
//
// Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
// 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_cont_PointCoordinatesUniform_h
#define vtk_m_cont_PointCoordinatesUniform_h
#include <vtkm/cont/ArrayHandleUniformPointCoordinates.h>
#include <vtkm/cont/internal/PointCoordinatesBase.h>
namespace vtkm {
namespace cont {
/// \brief Implicitly defined uniform point coordinates.
///
/// The \c PointCoordinatesUniform class is a PointCoordinates class that
/// implicitly defines the points for a uniform rectilinear grid of data
/// (defined by an extent, an origin, and spacing in each dimension).
///
/// Like other PointCoordinates classes, \c PointCoordinatesArray is intended
/// to be used in conjunction with \c DynamicPointCoordinates.
///
class PointCoordinatesUniform : public internal::PointCoordinatesBase
{
public:
VTKM_CONT_EXPORT
PointCoordinatesUniform() { }
VTKM_CONT_EXPORT
PointCoordinatesUniform(const vtkm::Extent3 &extent,
const vtkm::Vector3 &origin,
const vtkm::Vector3 &spacing)
: Array(extent, origin, spacing)
{ }
/// In this \c CastAndCall, both \c TypeList and \c ContainerList are
/// ignored. All point coordinates are expressed as Vector3, so that must be
/// how the array is represented.
///
template<typename Functor, typename TypeList, typename ContainerList>
VTKM_CONT_EXPORT
void CastAndCall(const Functor &f, TypeList, ContainerList) const
{
f(this->Array);
}
private:
vtkm::cont::ArrayHandleUniformPointCoordinates Array;
};
}
} // namespace vtkm::cont
#endif //vtk_m_cont_PointCoordinatesUniform_h

@ -34,6 +34,7 @@ set(headers
DeviceAdapterTagSerial.h
DynamicTransform.h
IteratorFromArrayPortal.h
PointCoordinatesBase.h
SimplePolymorphicContainer.h
)

@ -0,0 +1,78 @@
//============================================================================
// 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 Sandia Corporation.
// Copyright 2014 UT-Battelle, LLC.
// Copyright 2014. Los Alamos National Security
//
// Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
// 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_cont_internal_PointCoordinatesBase_h
#define vtk_m_cont_internal_PointCoordinatesBase_h
#include <vtkm/Types.h>
#include <boost/static_assert.hpp>
#include <boost/type_traits/is_base_of.hpp>
/// Checks that the argument is a proper \c PointCoordinates class. This is a
/// handy concept check for functions and classes to make sure that a template
/// argument is actually point coordinates. (You can get weird errors elsewhere
/// in the code when a mistake is made.)
#define VTKM_IS_POINT_COORDINATES(pctype) \
BOOST_STATIC_ASSERT_MSG( \
::vtkm::cont::internal::IsValidPointCoordinates<pctype>::type::value, \
"Provided type is not a valid VTK-m PointCoordinates type.")
namespace vtkm {
namespace cont {
namespace internal {
/// \brief Superclass for point coordinates classes.
///
/// \c PointCoordinatesBase is a simple superclass for all point coordinates
/// classes (by convention named \c PointCoordinates___).
///
/// The most important feature of this base class is to provide a common class
/// to perform a compile-time check to make sure a templated class is in fact
/// supposed to be a point coordinate class. (It is assumed that the subclass
/// will implement the expected methods.)
///
/// The second feature of this base class is to provide a type to perform safe
/// up and down casts, although this is easy to get around.
///
class PointCoordinatesBase
{
public:
// It is important to declare the destructor virtual so that subclasses will
// be properly destroyed.
virtual ~PointCoordinatesBase() { }
};
/// Checks to see if the given type is a valid point coordinates class. This
/// check is compatable with the Boost meta-template programming library (MPL).
/// It contains a typedef named type that is either boost::mpl::true_ or
/// boost::mpl::false_. Both of these have a typedef named value with the
/// respective boolean value.
///
template<typename Type>
struct IsValidPointCoordinates {
typedef typename boost::is_base_of<
vtkm::cont::internal::PointCoordinatesBase,Type>::type type;
};
}
}
} // namespace vtkm::cont::internal
#endif //vtk_m_cont_internal_PointCoordinatesBase_h

@ -22,6 +22,7 @@
#include "vtkm/cont/ArrayHandle.h"
#include "vtkm/cont/DynamicArrayHandle.h"
#include "vtkm/cont/DynamicPointCoordinates.h"
#include "vtkm/internal/FunctionInterface.h"
@ -72,7 +73,8 @@ struct FunctionInterfaceFunctor {
const vtkm::internal::FunctionInterface<
void(vtkm::cont::ArrayHandle<vtkm::Scalar>,
vtkm::cont::ArrayHandle<vtkm::Scalar>,
vtkm::cont::ArrayHandle<std::string>)> &) const {
vtkm::cont::ArrayHandle<std::string>,
vtkm::cont::ArrayHandleUniformPointCoordinates)> &) const {
std::cout << " In FunctionInterface<...> functor." << std::endl;
g_FunctionCalls++;
}
@ -107,20 +109,24 @@ void TestFunctionTransform()
vtkm::cont::ArrayHandle<vtkm::Scalar> scalarArray;
vtkm::cont::ArrayHandle<std::string> stringArray;
vtkm::cont::ArrayHandleUniformPointCoordinates pointCoordinatesArray;
std::cout << " Trying basic functor call w/o transform (make sure it works)."
<< std::endl;
TRY_TRANSFORM(FunctionInterfaceFunctor()(
vtkm::internal::make_FunctionInterface<void>(scalarArray,
scalarArray,
stringArray)));
vtkm::internal::make_FunctionInterface<void>(
scalarArray,
scalarArray,
stringArray,
pointCoordinatesArray)));
std::cout << " Trying dynamic cast" << std::endl;
TRY_TRANSFORM(
vtkm::internal::make_FunctionInterface<void>(
scalarArray,
vtkm::cont::DynamicArrayHandle(scalarArray),
vtkm::cont::DynamicArrayHandle(stringArray).ResetTypeList(TypeListTagString()))
vtkm::cont::DynamicArrayHandle(stringArray).ResetTypeList(TypeListTagString()),
vtkm::cont::DynamicPointCoordinates(vtkm::cont::PointCoordinatesUniform()))
.DynamicTransformCont(vtkm::cont::internal::DynamicTransform(),
FunctionInterfaceFunctor()));
}

@ -38,6 +38,8 @@ set(unit_tests
UnitTestDeviceAdapterAlgorithmGeneral.cxx
UnitTestDeviceAdapterSerial.cxx
UnitTestDynamicArrayHandle.cxx
UnitTestDynamicPointCoordinates.cxx
UnitTestPointCoordinates.cxx
)
vtkm_unit_tests(SOURCES ${unit_tests})

@ -0,0 +1,231 @@
//============================================================================
// 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 Sandia Corporation.
// Copyright 2014 UT-Battelle, LLC.
// Copyright 2014. Los Alamos National Security
//
// Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
// 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/DynamicPointCoordinates.h>
#include <vtkm/cont/ArrayContainerControlImplicit.h>
#include <vtkm/cont/ContainerListTag.h>
#include <vtkm/cont/internal/IteratorFromArrayPortal.h>
#include <vtkm/cont/testing/Testing.h>
#include <vector>
namespace {
const vtkm::Extent3 EXTENT = vtkm::Extent3(vtkm::Id3(0,0,0), vtkm::Id3(9,9,9));
const vtkm::Vector3 ORIGIN = vtkm::Vector3(0, 0, 0);
const vtkm::Vector3 SPACING = vtkm::Vector3(1, 1, 1);
const vtkm::Id3 DIMENSION = vtkm::ExtentPointDimensions(EXTENT);
const vtkm::Id ARRAY_SIZE = DIMENSION[0]*DIMENSION[1]*DIMENSION[2];
vtkm::Vector3 TestValue(vtkm::Id index)
{
vtkm::Id3 index3d = vtkm::ExtentPointFlatIndexToTopologyIndex(index, EXTENT);
return vtkm::Vector3(index3d[0], index3d[1], index3d[2]);
}
int g_CheckArrayInvocations;
struct CheckArray
{
CheckArray() {
g_CheckArrayInvocations = 0;
}
template<typename Container>
void operator()(
const vtkm::cont::ArrayHandle<vtkm::Vector3,Container> &array) const
{
std::cout << " In CastAndCall functor" << std::endl;
g_CheckArrayInvocations++;
typename vtkm::cont::ArrayHandle<vtkm::Vector3,Container>::PortalConstControl portal =
array.GetPortalConstControl();
VTKM_TEST_ASSERT(portal.GetNumberOfValues() == ARRAY_SIZE,
"Array has wrong number of values.");
for (vtkm::Id index = 0; index < ARRAY_SIZE; index++)
{
const vtkm::Vector3 receivedValue = portal.Get(index);
const vtkm::Vector3 expectedValue = TestValue(index);
VTKM_TEST_ASSERT(receivedValue == expectedValue,
"Got bad value in array.");
}
}
};
struct UnusualPortal
{
typedef vtkm::Vector3 ValueType;
VTKM_EXEC_CONT_EXPORT
vtkm::Id GetNumberOfValues() const { return ARRAY_SIZE; }
VTKM_EXEC_CONT_EXPORT
ValueType Get(vtkm::Id index) const { return TestValue(index); }
typedef vtkm::cont::internal::IteratorFromArrayPortal<UnusualPortal>
IteratorType;
VTKM_CONT_EXPORT
IteratorType GetIteratorBegin() const {
return IteratorType(*this);
}
VTKM_CONT_EXPORT
IteratorType GetIteratorEnd() const {
return IteratorType(*this, this->GetNumberOfValues());
}
};
class ArrayHandleWithUnusualContainer
: public vtkm::cont::ArrayHandle<vtkm::Vector3, vtkm::cont::ArrayContainerControlTagImplicit<UnusualPortal> >
{
typedef vtkm::cont::ArrayHandle<vtkm::Vector3, vtkm::cont::ArrayContainerControlTagImplicit<UnusualPortal> >
Superclass;
public:
VTKM_CONT_EXPORT
ArrayHandleWithUnusualContainer()
: Superclass(Superclass::PortalConstControl()) { }
};
struct ContainerListTagUnusual :
vtkm::ListTagBase<ArrayHandleWithUnusualContainer::ArrayContainerControlTag>
{ };
struct PointCoordinatesUnusual : vtkm::cont::internal::PointCoordinatesBase
{
template<typename Functor, typename TypeList, typename ContainerList>
void CastAndCall(const Functor &f, TypeList, ContainerList) const
{
f(ArrayHandleWithUnusualContainer());
}
};
struct PointCoordinatesListUnusual
: vtkm::ListTagBase<PointCoordinatesUnusual> { };
void TryDefaultArray()
{
std::cout << "Trying a basic point coordinates array with a default container."
<< std::endl;
std::vector<vtkm::Vector3> buffer(ARRAY_SIZE);
for (vtkm::Id index = 0; index < ARRAY_SIZE; index++)
{
buffer[index] = TestValue(index);
}
vtkm::cont::DynamicPointCoordinates pointCoordinates =
vtkm::cont::DynamicPointCoordinates(
vtkm::cont::make_ArrayHandle(buffer));
pointCoordinates.CastAndCall(CheckArray());
VTKM_TEST_ASSERT(g_CheckArrayInvocations == 1,
"CastAndCall functor not called expected number of times.");
}
void TryUnusualContainer()
{
std::cout << "Trying a basic point coordinates array with an unusual container."
<< std::endl;
vtkm::cont::DynamicPointCoordinates pointCoordinates =
vtkm::cont::DynamicPointCoordinates(
vtkm::cont::PointCoordinatesArray(ArrayHandleWithUnusualContainer()));
std::cout << " Make sure we get an exception when we can't find the type."
<< std::endl;
try
{
pointCoordinates.CastAndCall(CheckArray());
VTKM_TEST_FAIL("CastAndCall failed to error for unrecognized container.");
}
catch (vtkm::cont::ErrorControlBadValue error)
{
std::cout << " Caught expected exception for unrecognized container: "
<< std::endl << " " << error.GetMessage() << std::endl;
}
std::cout << " Recast containers and try again." << std::endl;
pointCoordinates.ResetContainerList(ContainerListTagUnusual())
.CastAndCall(CheckArray());
VTKM_TEST_ASSERT(g_CheckArrayInvocations == 1,
"CastAndCall functor not called expected number of times.");
}
void TryUniformPointCoordinates()
{
std::cout << "Trying uniform point coordinates." << std::endl;
vtkm::cont::DynamicPointCoordinates pointCoordinates =
vtkm::cont::DynamicPointCoordinates(
vtkm::cont::PointCoordinatesUniform(EXTENT, ORIGIN, SPACING));
pointCoordinates.CastAndCall(CheckArray());
VTKM_TEST_ASSERT(g_CheckArrayInvocations == 1,
"CastAndCall functor not called expected number of times.");
}
void TryUnusualPointCoordinates()
{
std::cout << "Trying an unusual point coordinates object." << std::endl;
vtkm::cont::DynamicPointCoordinates pointCoordinates =
vtkm::cont::DynamicPointCoordinates(PointCoordinatesUnusual());
std::cout << " Make sure we get an exception when we can't find the type."
<< std::endl;
try
{
pointCoordinates.CastAndCall(CheckArray());
VTKM_TEST_FAIL("CastAndCall failed to error for unrecognized container.");
}
catch (vtkm::cont::ErrorControlBadValue error)
{
std::cout << " Caught expected exception for unrecognized point coordinates: "
<< std::endl << " " << error.GetMessage() << std::endl;
}
std::cout << " Recast containers and try again." << std::endl;
pointCoordinates.ResetPointCoordinatesList(PointCoordinatesListUnusual())
.CastAndCall(CheckArray());
VTKM_TEST_ASSERT(g_CheckArrayInvocations == 1,
"CastAndCall functor not called expected number of times.");
}
void DynamicPointCoordiantesTest()
{
TryDefaultArray();
TryUnusualContainer();
TryUniformPointCoordinates();
TryUnusualPointCoordinates();
}
} // anonymous namespace
int UnitTestDynamicPointCoordinates(int, char *[])
{
return vtkm::cont::testing::Testing::Run(DynamicPointCoordiantesTest);
}

@ -0,0 +1,132 @@
//============================================================================
// 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 Sandia Corporation.
// Copyright 2014 UT-Battelle, LLC.
// Copyright 2014. Los Alamos National Security
//
// Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
// 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.
//============================================================================
// Make sure nothing relies on default container or device adapter.
#define VTKM_ARRAY_CONTAINER_CONTROL VTKM_ARRAY_CONTAINER_CONTROL_ERROR
#define VTKM_DEVICE_ADAPTER VTKM_DEVICE_ADAPTER_ERROR
// Make sure nothing relies on default lists.
#define VTKM_DEFAULT_TYPE_LIST_TAG ::vtkm::ListTagEmpty
#define VTKM_DEFAULT_CONTAINER_LIST_TAG ::vtkm::ListTagEmpty
#include <vtkm/cont/PointCoordinatesArray.h>
#include <vtkm/cont/PointCoordinatesUniform.h>
#include <vtkm/Extent.h>
#include <vtkm/TypeListTag.h>
#include <vtkm/cont/ArrayContainerControlBasic.h>
#include <vtkm/cont/DeviceAdapterSerial.h>
#include <vtkm/cont/testing/Testing.h>
#include <vector>
namespace {
const vtkm::Extent3 EXTENT = vtkm::Extent3(vtkm::Id3(0,0,0), vtkm::Id3(9,9,9));
const vtkm::Vector3 ORIGIN = vtkm::Vector3(0, 0, 0);
const vtkm::Vector3 SPACING = vtkm::Vector3(1, 1, 1);
const vtkm::Id3 DIMENSION = vtkm::ExtentPointDimensions(EXTENT);
const vtkm::Id ARRAY_SIZE = DIMENSION[0]*DIMENSION[1]*DIMENSION[2];
typedef vtkm::cont::ArrayContainerControlTagBasic Container;
struct ContainerListTag : vtkm::cont::ContainerListTagBasic { };
vtkm::Vector3 TestValue(vtkm::Id index)
{
vtkm::Id3 index3d = vtkm::ExtentPointFlatIndexToTopologyIndex(index, EXTENT);
return vtkm::Vector3(index3d[0], index3d[1], index3d[2]);
}
struct CheckArray
{
template<typename C>
void operator()(
const vtkm::cont::ArrayHandle<vtkm::Vector3,C> &array) const
{
std::cout << " In CastAndCall functor" << std::endl;
typename vtkm::cont::ArrayHandle<vtkm::Vector3,C>::PortalConstControl portal =
array.GetPortalConstControl();
VTKM_TEST_ASSERT(portal.GetNumberOfValues() == ARRAY_SIZE,
"Array has wrong number of values.");
for (vtkm::Id index = 0; index < ARRAY_SIZE; index++)
{
const vtkm::Vector3 receivedValue = portal.Get(index);
const vtkm::Vector3 expectedValue = TestValue(index);
VTKM_TEST_ASSERT(receivedValue == expectedValue,
"Got bad value in array.");
}
}
};
void TestPointCoordinatesArray()
{
std::cout << "Testing PointCoordinatesArray" << std::endl;
std::cout << " Creating buffer of data values" << std::endl;
std::vector<vtkm::Vector3> buffer(ARRAY_SIZE);
for (vtkm::Id index = 0; index < ARRAY_SIZE; index++)
{
buffer[index] = TestValue(index);
}
std::cout << " Creating and checking array handle" << std::endl;
vtkm::cont::ArrayHandle<vtkm::Vector3,Container> array =
vtkm::cont::make_ArrayHandle(buffer, Container());
CheckArray()(array);
std::cout << " Creating and checking PointCoordinatesArray" << std::endl;
vtkm::cont::PointCoordinatesArray pointCoordinates =
vtkm::cont::PointCoordinatesArray(array);
pointCoordinates.CastAndCall(
CheckArray(),
vtkm::ListTagEmpty(), // Internally sets to Vector3
vtkm::cont::ContainerListTagBasic());
}
void TestPointCoordinatesUniform()
{
std::cout << "Testing PointCoordinatesUniform" << std::endl;
vtkm::cont::PointCoordinatesUniform pointCoordinates =
vtkm::cont::PointCoordinatesUniform(EXTENT, ORIGIN, SPACING);
pointCoordinates.CastAndCall(
CheckArray(),
vtkm::ListTagEmpty(), // Not used
vtkm::ListTagEmpty()); // Not used
}
void PointCoordinatesTests()
{
TestPointCoordinatesArray();
TestPointCoordinatesUniform();
}
} // anonymous namespace
int UnitTestPointCoordinates(int, char *[])
{
return vtkm::cont::testing::Testing::Run(PointCoordinatesTests);
}

@ -109,6 +109,9 @@ void TryList(const vtkm::Tuple<int,N> &expected, ListTag)
void TestLists()
{
std::cout << "ListTagEmpty" << std::endl;
TryList(vtkm::Tuple<int,0>(), vtkm::ListTagEmpty());
std::cout << "ListTagBase" << std::endl;
TryList(vtkm::Tuple<int,1>(11), TestListTag1());