From 7d769a8f4ac0a5f27cfa9fde055bc41213804858 Mon Sep 17 00:00:00 2001 From: Kenneth Moreland Date: Wed, 14 May 2014 15:05:38 -0600 Subject: [PATCH] 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. --- vtkm/ListTag.h | 15 + vtkm/cont/CMakeLists.txt | 4 + vtkm/cont/ContainerListTag.h | 1 + vtkm/cont/DynamicArrayHandle.h | 33 +- vtkm/cont/DynamicPointCoordinates.h | 388 ++++++++++++++++++ vtkm/cont/PointCoordinatesArray.h | 75 ++++ vtkm/cont/PointCoordinatesListTag.h | 52 +++ vtkm/cont/PointCoordinatesUniform.h | 70 ++++ vtkm/cont/internal/CMakeLists.txt | 1 + vtkm/cont/internal/PointCoordinatesBase.h | 78 ++++ .../testing/UnitTestDynamicTransform.cxx | 16 +- vtkm/cont/testing/CMakeLists.txt | 2 + .../UnitTestDynamicPointCoordinates.cxx | 231 +++++++++++ .../cont/testing/UnitTestPointCoordinates.cxx | 132 ++++++ vtkm/testing/UnitTestListTag.cxx | 3 + 15 files changed, 1093 insertions(+), 8 deletions(-) create mode 100644 vtkm/cont/DynamicPointCoordinates.h create mode 100644 vtkm/cont/PointCoordinatesArray.h create mode 100644 vtkm/cont/PointCoordinatesListTag.h create mode 100644 vtkm/cont/PointCoordinatesUniform.h create mode 100644 vtkm/cont/internal/PointCoordinatesBase.h create mode 100644 vtkm/cont/testing/UnitTestDynamicPointCoordinates.cxx create mode 100644 vtkm/cont/testing/UnitTestPointCoordinates.cxx diff --git a/vtkm/ListTag.h b/vtkm/ListTag.h index ef25dd5ce..47a3e2ddd 100644 --- a/vtkm/ListTag.h +++ b/vtkm/ListTag.h @@ -26,6 +26,8 @@ namespace vtkm { namespace detail { +struct ListEmpty { }; + template struct ListBase { }; @@ -45,6 +47,12 @@ struct ListJoin { }; template 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 @@ -90,6 +98,13 @@ void ListForEach(const Functor &f, ListTag); namespace detail { +template +VTKM_CONT_EXPORT +void ListForEachImpl(Functor, ListEmpty) +{ + // Nothing to do for empty list +} + template VTKM_CONT_EXPORT void ListForEachImpl(Functor &f, ListBase) diff --git a/vtkm/cont/CMakeLists.txt b/vtkm/cont/CMakeLists.txt index e1adcfc21..aca6d149d 100644 --- a/vtkm/cont/CMakeLists.txt +++ b/vtkm/cont/CMakeLists.txt @@ -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 ) #----------------------------------------------------------------------------- diff --git a/vtkm/cont/ContainerListTag.h b/vtkm/cont/ContainerListTag.h index 0cb7d15b4..2159f8e52 100644 --- a/vtkm/cont/ContainerListTag.h +++ b/vtkm/cont/ContainerListTag.h @@ -27,6 +27,7 @@ #include #include +#include namespace vtkm { namespace cont { diff --git a/vtkm/cont/DynamicArrayHandle.h b/vtkm/cont/DynamicArrayHandle.h index 642559260..31496cf8d 100644 --- a/vtkm/cont/DynamicArrayHandle.h +++ b/vtkm/cont/DynamicArrayHandle.h @@ -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 > *container = this->TryCastArrayContainer(); 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."); } } diff --git a/vtkm/cont/DynamicPointCoordinates.h b/vtkm/cont/DynamicPointCoordinates.h new file mode 100644 index 000000000..def0560a2 --- /dev/null +++ b/vtkm/cont/DynamicPointCoordinates.h @@ -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 +#include +#include +#include + +#include + +#include + +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 +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 + VTKM_CONT_EXPORT + DynamicPointCoordinates( + const vtkm::cont::ArrayHandle &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 + 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 + VTKM_CONT_EXPORT + bool IsPointCoordinateType(PointCoordinatesType = PointCoordinatesType()) const + { + VTKM_IS_POINT_COORDINATES(PointCoordinatesType); + return (this->TryCastPointCoordinatesType() != 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 + VTKM_CONT_EXPORT + const PointCoordinatesType & + CastToPointCoordinates(PointCoordinatesType = PointCoordinatesType()) const { + VTKM_IS_POINT_COORDINATES(PointCoordinatesType); + PointCoordinatesType *pointCoordinates = + this->TryCastPointCoordinatesType(); + 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 + 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 + 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 + 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 + 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 + VTKM_CONT_EXPORT + void CastAndCall(const Functor &f, + PointCoordinatesList, + TypeList, + ContainerList) const; + +private: + boost::shared_ptr + PointCoordinatesContainer; + + template + VTKM_CONT_EXPORT + PointCoordinatesType * + TryCastPointCoordinatesType() const { + VTKM_IS_POINT_COORDINATES(PointCoordinatesType); + return dynamic_cast( + this->PointCoordinatesContainer.get()); + } +}; + +namespace detail { + +template +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 + 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 +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 +class DynamicPointCoordinatesCast : public vtkm::cont::DynamicPointCoordinates +{ +public: + VTKM_CONT_EXPORT + DynamicPointCoordinatesCast() : DynamicPointCoordinates() { } + + VTKM_CONT_EXPORT + DynamicPointCoordinatesCast(const vtkm::cont::DynamicPointCoordinates &coords) + : DynamicPointCoordinates(coords) { } + + template + VTKM_CONT_EXPORT + DynamicPointCoordinatesCast( + const DynamicPointCoordinatesCast &coords) + : DynamicPointCoordinates(coords) + { } + + template + VTKM_CONT_EXPORT + DynamicPointCoordinatesCast + ResetPointCoordinatesList( + NewPointCoordinatesList = NewPointCoordinatesList()) const { + return DynamicPointCoordinatesCast< + NewPointCoordinatesList,TypeList,ContainerList>(*this); + } + + template + VTKM_CONT_EXPORT + DynamicPointCoordinatesCast + ResetTypeList(NewTypeList = NewTypeList()) const { + return DynamicPointCoordinatesCast< + PointCoordinatesList,NewTypeList,ContainerList>(*this); + } + + template + VTKM_CONT_EXPORT + DynamicPointCoordinatesCast + ResetContainerList(NewContainerList = NewContainerList()) const { + return DynamicPointCoordinatesCast< + PointCoordinatesList,TypeList,NewContainerList>(*this); + } + + template + VTKM_CONT_EXPORT + void CastAndCall(const Functor &f) const + { + this->CastAndCall(f, PointCoordinatesList(), TypeList(), ContainerList()); + } + + template + VTKM_CONT_EXPORT + void CastAndCall(const Functor &f, PCL, TL, CL) const + { + this->DynamicPointCoordinates::CastAndCall(f, PCL(), TL(), CL()); + } +}; + +template<> +struct DynamicTransformTraits { + typedef vtkm::cont::internal::DynamicTransformTagCastAndCall DynamicTag; +}; + +template +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 diff --git a/vtkm/cont/PointCoordinatesArray.h b/vtkm/cont/PointCoordinatesArray.h new file mode 100644 index 000000000..373e1a7fb --- /dev/null +++ b/vtkm/cont/PointCoordinatesArray.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 + +#include +#include + +#include + +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 + VTKM_CONT_EXPORT + PointCoordinatesArray( + const vtkm::cont::ArrayHandle &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 + 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 diff --git a/vtkm/cont/PointCoordinatesListTag.h b/vtkm/cont/PointCoordinatesListTag.h new file mode 100644 index 000000000..7cb6cdc2e --- /dev/null +++ b/vtkm/cont/PointCoordinatesListTag.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 + +#include +#include + +namespace vtkm { +namespace cont { + +struct PointCoordinatesListTagArray : + vtkm::ListTagBase { }; +struct PointCoordinatesListTagUniform : + vtkm::ListTagBase { }; + +/// 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 diff --git a/vtkm/cont/PointCoordinatesUniform.h b/vtkm/cont/PointCoordinatesUniform.h new file mode 100644 index 000000000..e55eb84aa --- /dev/null +++ b/vtkm/cont/PointCoordinatesUniform.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 + +#include + +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 + 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 diff --git a/vtkm/cont/internal/CMakeLists.txt b/vtkm/cont/internal/CMakeLists.txt index 776eba2f8..3b70c7a2c 100644 --- a/vtkm/cont/internal/CMakeLists.txt +++ b/vtkm/cont/internal/CMakeLists.txt @@ -34,6 +34,7 @@ set(headers DeviceAdapterTagSerial.h DynamicTransform.h IteratorFromArrayPortal.h + PointCoordinatesBase.h SimplePolymorphicContainer.h ) diff --git a/vtkm/cont/internal/PointCoordinatesBase.h b/vtkm/cont/internal/PointCoordinatesBase.h new file mode 100644 index 000000000..9b1cc2703 --- /dev/null +++ b/vtkm/cont/internal/PointCoordinatesBase.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 + +#include +#include + +/// 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::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 +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 diff --git a/vtkm/cont/internal/testing/UnitTestDynamicTransform.cxx b/vtkm/cont/internal/testing/UnitTestDynamicTransform.cxx index 7b49cb012..85e2917f5 100644 --- a/vtkm/cont/internal/testing/UnitTestDynamicTransform.cxx +++ b/vtkm/cont/internal/testing/UnitTestDynamicTransform.cxx @@ -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::cont::ArrayHandle, - vtkm::cont::ArrayHandle)> &) const { + vtkm::cont::ArrayHandle, + vtkm::cont::ArrayHandleUniformPointCoordinates)> &) const { std::cout << " In FunctionInterface<...> functor." << std::endl; g_FunctionCalls++; } @@ -107,20 +109,24 @@ void TestFunctionTransform() vtkm::cont::ArrayHandle scalarArray; vtkm::cont::ArrayHandle 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(scalarArray, - scalarArray, - stringArray))); + vtkm::internal::make_FunctionInterface( + scalarArray, + scalarArray, + stringArray, + pointCoordinatesArray))); std::cout << " Trying dynamic cast" << std::endl; TRY_TRANSFORM( vtkm::internal::make_FunctionInterface( 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())); } diff --git a/vtkm/cont/testing/CMakeLists.txt b/vtkm/cont/testing/CMakeLists.txt index c46840f96..a47c0dd46 100644 --- a/vtkm/cont/testing/CMakeLists.txt +++ b/vtkm/cont/testing/CMakeLists.txt @@ -38,6 +38,8 @@ set(unit_tests UnitTestDeviceAdapterAlgorithmGeneral.cxx UnitTestDeviceAdapterSerial.cxx UnitTestDynamicArrayHandle.cxx + UnitTestDynamicPointCoordinates.cxx + UnitTestPointCoordinates.cxx ) vtkm_unit_tests(SOURCES ${unit_tests}) diff --git a/vtkm/cont/testing/UnitTestDynamicPointCoordinates.cxx b/vtkm/cont/testing/UnitTestDynamicPointCoordinates.cxx new file mode 100644 index 000000000..72327dc3f --- /dev/null +++ b/vtkm/cont/testing/UnitTestDynamicPointCoordinates.cxx @@ -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 + +#include +#include + +#include + +#include + +#include + +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 + void operator()( + const vtkm::cont::ArrayHandle &array) const + { + std::cout << " In CastAndCall functor" << std::endl; + g_CheckArrayInvocations++; + typename vtkm::cont::ArrayHandle::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 + 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 > +{ + typedef vtkm::cont::ArrayHandle > + Superclass; +public: + VTKM_CONT_EXPORT + ArrayHandleWithUnusualContainer() + : Superclass(Superclass::PortalConstControl()) { } +}; + +struct ContainerListTagUnusual : + vtkm::ListTagBase +{ }; + +struct PointCoordinatesUnusual : vtkm::cont::internal::PointCoordinatesBase +{ + template + void CastAndCall(const Functor &f, TypeList, ContainerList) const + { + f(ArrayHandleWithUnusualContainer()); + } +}; + +struct PointCoordinatesListUnusual + : vtkm::ListTagBase { }; + +void TryDefaultArray() +{ + std::cout << "Trying a basic point coordinates array with a default container." + << std::endl; + std::vector 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); +} diff --git a/vtkm/cont/testing/UnitTestPointCoordinates.cxx b/vtkm/cont/testing/UnitTestPointCoordinates.cxx new file mode 100644 index 000000000..3fa69da99 --- /dev/null +++ b/vtkm/cont/testing/UnitTestPointCoordinates.cxx @@ -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 +#include + +#include +#include + +#include +#include + +#include + +#include + +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 + void operator()( + const vtkm::cont::ArrayHandle &array) const + { + std::cout << " In CastAndCall functor" << std::endl; + typename vtkm::cont::ArrayHandle::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 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 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); +} diff --git a/vtkm/testing/UnitTestListTag.cxx b/vtkm/testing/UnitTestListTag.cxx index a6d669e9a..d0d9ea1fe 100644 --- a/vtkm/testing/UnitTestListTag.cxx +++ b/vtkm/testing/UnitTestListTag.cxx @@ -109,6 +109,9 @@ void TryList(const vtkm::Tuple &expected, ListTag) void TestLists() { + std::cout << "ListTagEmpty" << std::endl; + TryList(vtkm::Tuple(), vtkm::ListTagEmpty()); + std::cout << "ListTagBase" << std::endl; TryList(vtkm::Tuple(11), TestListTag1());