Commit b5415169 authored by Kenneth Moreland's avatar Kenneth Moreland
Browse files

Change Field and related methods to use Range and Bounds

First, be more explicit when we mean a range of values in a field or a
spacial bounds. Use the Range and Bounds structs in Field and
CoordinateSystem to make all of this more clear (and reduce a bit of
code as well).
parent d75857d0
...@@ -50,11 +50,16 @@ struct Bounds ...@@ -50,11 +50,16 @@ struct Bounds
const vtkm::Range &zRange) const vtkm::Range &zRange)
: X(xRange), Y(yRange), Z(zRange) { } : X(xRange), Y(yRange), Z(zRange) { }
template<typename T> template<typename T1,
typename T2,
typename T3,
typename T4,
typename T5,
typename T6>
VTKM_EXEC_CONT_EXPORT VTKM_EXEC_CONT_EXPORT
Bounds(const T &minX, const T &maxX, Bounds(const T1 &minX, const T2 &maxX,
const T &minY, const T &maxY, const T3 &minY, const T4 &maxY,
const T &minZ, const T &maxZ) const T5 &minZ, const T6 &maxZ)
: X(vtkm::Range(minX, maxX)), : X(vtkm::Range(minX, maxX)),
Y(vtkm::Range(minY, maxY)), Y(vtkm::Range(minY, maxY)),
Z(vtkm::Range(minZ, maxZ)) Z(vtkm::Range(minZ, maxZ))
...@@ -116,6 +121,19 @@ struct Bounds ...@@ -116,6 +121,19 @@ struct Bounds
this->Z.Contains(point[2])); this->Z.Contains(point[2]));
} }
/// \b Returns the center of the range.
///
/// \c Center computes the point at the middle of the bounds. If the bounds
/// are empty, the results are undefined.
///
VTKM_EXEC_CONT_EXPORT
vtkm::Vec<vtkm::Float64,3> Center() const
{
return vtkm::Vec<vtkm::Float64,3>(this->X.Center(),
this->Y.Center(),
this->Z.Center());
}
/// \b Expand bounds to include a point. /// \b Expand bounds to include a point.
/// ///
/// This version of \c Include expands the bounds just enough to include the /// This version of \c Include expands the bounds just enough to include the
...@@ -183,4 +201,14 @@ struct Bounds ...@@ -183,4 +201,14 @@ struct Bounds
} // namespace vtkm } // namespace vtkm
/// Helper function for printing bounds during testing
///
VTKM_CONT_EXPORT
std::ostream &operator<<(std::ostream &stream, const vtkm::Bounds &bounds)
{
return stream << "{ X:" << bounds.X
<< ", Y:" << bounds.Y
<< ", Z:" << bounds.Z << " }";
}
#endif //vtk_m_Bounds_h #endif //vtk_m_Bounds_h
...@@ -87,6 +87,42 @@ struct Range ...@@ -87,6 +87,42 @@ struct Range
(this->Max >= static_cast<vtkm::Float64>(value))); (this->Max >= static_cast<vtkm::Float64>(value)));
} }
/// \b Returns the length of the range.
///
/// \c Length computes the distance between the min and max. If the range
/// is empty, 0 is returned.
///
VTKM_EXEC_CONT_EXPORT
vtkm::Float64 Length() const
{
if (this->IsNonEmpty())
{
return (this->Max - this->Min);
}
else
{
return 0.0;
}
}
/// \b Returns the center of the range.
///
/// \c Center computes the middle value of the range. If the range is empty,
/// NaN is returned.
///
VTKM_EXEC_CONT_EXPORT
vtkm::Float64 Center() const
{
if (this->IsNonEmpty())
{
return 0.5*(this->Max + this->Min);
}
else
{
return vtkm::Nan64();
}
}
/// \b Expand range to include a value. /// \b Expand range to include a value.
/// ///
/// This version of \c Include expands the range just enough to include the /// This version of \c Include expands the range just enough to include the
...@@ -148,4 +184,12 @@ struct Range ...@@ -148,4 +184,12 @@ struct Range
} // namespace vtkm } // namespace vtkm
/// Helper function for printing ranges during testing
///
VTKM_CONT_EXPORT
std::ostream &operator<<(std::ostream &stream, const vtkm::Range &range)
{
return stream << "[" << range.Min << ".." << range.Max << "]";
}
#endif //vtk_m_Range_h #endif //vtk_m_Range_h
...@@ -20,6 +20,8 @@ ...@@ -20,6 +20,8 @@
#ifndef vtk_m_cont_CoordinateSystem_h #ifndef vtk_m_cont_CoordinateSystem_h
#define vtk_m_cont_CoordinateSystem_h #define vtk_m_cont_CoordinateSystem_h
#include <vtkm/Bounds.h>
#include <vtkm/cont/ArrayHandleUniformPointCoordinates.h> #include <vtkm/cont/ArrayHandleUniformPointCoordinates.h>
#include <vtkm/cont/ArrayHandleCompositeVector.h> #include <vtkm/cont/ArrayHandleCompositeVector.h>
#include <vtkm/cont/ArrayHandleCartesianProduct.h> #include <vtkm/cont/ArrayHandleCartesianProduct.h>
...@@ -136,10 +138,10 @@ public: ...@@ -136,10 +138,10 @@ public:
template<typename DeviceAdapterTag> template<typename DeviceAdapterTag>
VTKM_CONT_EXPORT VTKM_CONT_EXPORT
void GetBounds(vtkm::Float64 *bounds, DeviceAdapterTag) const void GetRange(vtkm::Range *range, DeviceAdapterTag) const
{ {
this->Superclass::GetBounds( this->Superclass::GetRange(
bounds, range,
DeviceAdapterTag(), DeviceAdapterTag(),
VTKM_DEFAULT_COORDINATE_SYSTEM_TYPE_LIST_TAG(), VTKM_DEFAULT_COORDINATE_SYSTEM_TYPE_LIST_TAG(),
VTKM_DEFAULT_COORDINATE_SYSTEM_STORAGE_LIST_TAG()); VTKM_DEFAULT_COORDINATE_SYSTEM_STORAGE_LIST_TAG());
...@@ -147,10 +149,10 @@ public: ...@@ -147,10 +149,10 @@ public:
template<typename DeviceAdapterTag, typename TypeList> template<typename DeviceAdapterTag, typename TypeList>
VTKM_CONT_EXPORT VTKM_CONT_EXPORT
void GetBounds(vtkm::Float64 *bounds, DeviceAdapterTag, TypeList) const void GetRange(vtkm::Range *range, DeviceAdapterTag, TypeList) const
{ {
this->Superclass::GetBounds( this->Superclass::GetRange(
bounds, range,
DeviceAdapterTag(), DeviceAdapterTag(),
TypeList(), TypeList(),
VTKM_DEFAULT_COORDINATE_SYSTEM_STORAGE_LIST_TAG()); VTKM_DEFAULT_COORDINATE_SYSTEM_STORAGE_LIST_TAG());
...@@ -158,10 +160,10 @@ public: ...@@ -158,10 +160,10 @@ public:
template<typename DeviceAdapterTag, typename TypeList, typename StorageList> template<typename DeviceAdapterTag, typename TypeList, typename StorageList>
VTKM_CONT_EXPORT VTKM_CONT_EXPORT
void GetBounds(vtkm::Float64 *bounds, DeviceAdapterTag, TypeList, StorageList) const void GetRange(vtkm::Range *range, DeviceAdapterTag, TypeList, StorageList) const
{ {
this->Superclass::GetBounds( this->Superclass::GetRange(
bounds, range,
DeviceAdapterTag(), DeviceAdapterTag(),
TypeList(), TypeList(),
StorageList()); StorageList());
...@@ -169,9 +171,9 @@ public: ...@@ -169,9 +171,9 @@ public:
template<typename DeviceAdapterTag> template<typename DeviceAdapterTag>
VTKM_CONT_EXPORT VTKM_CONT_EXPORT
const vtkm::cont::ArrayHandle<vtkm::Float64>& GetBounds(DeviceAdapterTag) const const vtkm::cont::ArrayHandle<vtkm::Range>& GetRange(DeviceAdapterTag) const
{ {
return this->Superclass::GetBounds( return this->Superclass::GetRange(
DeviceAdapterTag(), DeviceAdapterTag(),
VTKM_DEFAULT_COORDINATE_SYSTEM_TYPE_LIST_TAG(), VTKM_DEFAULT_COORDINATE_SYSTEM_TYPE_LIST_TAG(),
VTKM_DEFAULT_COORDINATE_SYSTEM_STORAGE_LIST_TAG()); VTKM_DEFAULT_COORDINATE_SYSTEM_STORAGE_LIST_TAG());
...@@ -179,10 +181,10 @@ public: ...@@ -179,10 +181,10 @@ public:
template<typename DeviceAdapterTag, typename TypeList> template<typename DeviceAdapterTag, typename TypeList>
VTKM_CONT_EXPORT VTKM_CONT_EXPORT
const vtkm::cont::ArrayHandle<vtkm::Float64>& GetBounds(DeviceAdapterTag, const vtkm::cont::ArrayHandle<vtkm::Range>& GetRange(DeviceAdapterTag,
TypeList) const TypeList) const
{ {
return this->Superclass::GetBounds( return this->Superclass::GetRange(
DeviceAdapterTag(), DeviceAdapterTag(),
TypeList(), TypeList(),
VTKM_DEFAULT_COORDINATE_SYSTEM_STORAGE_LIST_TAG()); VTKM_DEFAULT_COORDINATE_SYSTEM_STORAGE_LIST_TAG());
...@@ -190,16 +192,59 @@ public: ...@@ -190,16 +192,59 @@ public:
template<typename DeviceAdapterTag, typename TypeList, typename StorageList> template<typename DeviceAdapterTag, typename TypeList, typename StorageList>
VTKM_CONT_EXPORT VTKM_CONT_EXPORT
const vtkm::cont::ArrayHandle<vtkm::Float64>& GetBounds(DeviceAdapterTag, const vtkm::cont::ArrayHandle<vtkm::Range>& GetRange(DeviceAdapterTag,
TypeList, TypeList,
StorageList) const StorageList) const
{ {
return this->Superclass::GetBounds( return this->Superclass::GetRange(
DeviceAdapterTag(), DeviceAdapterTag(),
TypeList(), TypeList(),
StorageList()); StorageList());
} }
template<typename DeviceAdapterTag>
VTKM_CONT_EXPORT
vtkm::Bounds GetBounds(DeviceAdapterTag) const
{
VTKM_IS_DEVICE_ADAPTER_TAG(DeviceAdapterTag);
return this->GetBounds(DeviceAdapterTag(),
VTKM_DEFAULT_COORDINATE_SYSTEM_TYPE_LIST_TAG(),
VTKM_DEFAULT_COORDINATE_SYSTEM_STORAGE_LIST_TAG());
}
template<typename DeviceAdapterTag, typename TypeList>
VTKM_CONT_EXPORT
vtkm::Bounds GetBounds(DeviceAdapterTag, TypeList) const
{
VTKM_IS_DEVICE_ADAPTER_TAG(DeviceAdapterTag);
VTKM_IS_LIST_TAG(TypeList);
return this->GetBounds(DeviceAdapterTag(),
TypeList(),
VTKM_DEFAULT_COORDINATE_SYSTEM_STORAGE_LIST_TAG());
}
template<typename DeviceAdapterTag, typename TypeList, typename StorageList>
VTKM_CONT_EXPORT
vtkm::Bounds GetBounds(DeviceAdapterTag, TypeList, StorageList) const
{
VTKM_IS_DEVICE_ADAPTER_TAG(DeviceAdapterTag);
VTKM_IS_LIST_TAG(TypeList);
VTKM_IS_LIST_TAG(StorageList);
vtkm::cont::ArrayHandle<vtkm::Range> ranges =
this->GetRange(DeviceAdapterTag(), TypeList(), StorageList());
VTKM_ASSERT(ranges.GetNumberOfValues() == 3);
vtkm::cont::ArrayHandle<vtkm::Range>::PortalConstControl rangePortal =
ranges.GetPortalConstControl();
return vtkm::Bounds(rangePortal.Get(0),
rangePortal.Get(1),
rangePortal.Get(2));
}
VTKM_CONT_EXPORT VTKM_CONT_EXPORT
......
...@@ -20,8 +20,9 @@ ...@@ -20,8 +20,9 @@
#ifndef vtk_m_cont_Field_h #ifndef vtk_m_cont_Field_h
#define vtk_m_cont_Field_h #define vtk_m_cont_Field_h
#include <vtkm/Types.h>
#include <vtkm/Math.h> #include <vtkm/Math.h>
#include <vtkm/Range.h>
#include <vtkm/Types.h>
#include <vtkm/VecTraits.h> #include <vtkm/VecTraits.h>
#include <vtkm/cont/ArrayHandle.h> #include <vtkm/cont/ArrayHandle.h>
...@@ -37,13 +38,13 @@ namespace cont { ...@@ -37,13 +38,13 @@ namespace cont {
namespace internal { namespace internal {
struct BoundsMin struct RangeMin
{ {
template<typename T> template<typename T>
T operator()(const T& a, const T& b)const { return vtkm::Min(a,b); } T operator()(const T& a, const T& b)const { return vtkm::Min(a,b); }
}; };
struct BoundsMax struct RangeMax
{ {
template<typename T> template<typename T>
T operator()(const T& a, const T& b)const { return vtkm::Max(a,b); } T operator()(const T& a, const T& b)const { return vtkm::Max(a,b); }
...@@ -51,10 +52,10 @@ struct BoundsMax ...@@ -51,10 +52,10 @@ struct BoundsMax
template<typename DeviceAdapterTag> template<typename DeviceAdapterTag>
class ComputeBounds class ComputeRange
{ {
public: public:
ComputeBounds(ArrayHandle<vtkm::Float64>& bounds) : Bounds(&bounds) {} ComputeRange(ArrayHandle<vtkm::Range>& range) : Range(&range) {}
template<typename ArrayHandleType> template<typename ArrayHandleType>
void operator()(const ArrayHandleType &input) const void operator()(const ArrayHandleType &input) const
...@@ -70,16 +71,15 @@ public: ...@@ -70,16 +71,15 @@ public:
ValueType initialMin = input.GetPortalConstControl().Get(0); ValueType initialMin = input.GetPortalConstControl().Get(0);
ValueType initialMax = initialMin; ValueType initialMax = initialMin;
ValueType minResult = Algorithm::Reduce(input, initialMin, BoundsMin()); ValueType minResult = Algorithm::Reduce(input, initialMin, RangeMin());
ValueType maxResult = Algorithm::Reduce(input, initialMax, BoundsMax()); ValueType maxResult = Algorithm::Reduce(input, initialMax, RangeMax());
this->Bounds->Allocate(NumberOfComponents * 2); this->Range->Allocate(NumberOfComponents);
for (vtkm::IdComponent i = 0; i < NumberOfComponents; ++i) for (vtkm::IdComponent i = 0; i < NumberOfComponents; ++i)
{ {
this->Bounds->GetPortalControl().Set(i * 2, this->Range->GetPortalControl().Set(
static_cast<vtkm::Float64>(VecType::GetComponent(minResult, i) ) ); i, vtkm::Range(VecType::GetComponent(minResult, i),
this->Bounds->GetPortalControl().Set(i * 2 + 1, VecType::GetComponent(maxResult, i)));
static_cast<vtkm::Float64>(VecType::GetComponent(maxResult, i) ) );
} }
} }
...@@ -99,19 +99,16 @@ public: ...@@ -99,19 +99,16 @@ public:
vtkm::Vec<vtkm::FloatDefault,3> maximum = vtkm::Vec<vtkm::FloatDefault,3> maximum =
portal.Get(portal.GetNumberOfValues()-1); portal.Get(portal.GetNumberOfValues()-1);
this->Bounds->Allocate(6); this->Range->Allocate(3);
vtkm::cont::ArrayHandle<vtkm::Float64>::PortalControl outPortal = vtkm::cont::ArrayHandle<vtkm::Range>::PortalControl outPortal =
this->Bounds->GetPortalControl(); this->Range->GetPortalControl();
outPortal.Set(0, minimum[0]); outPortal.Set(0, vtkm::Range(minimum[0], maximum[0]));
outPortal.Set(1, maximum[0]); outPortal.Set(1, vtkm::Range(minimum[1], maximum[1]));
outPortal.Set(2, minimum[1]); outPortal.Set(2, vtkm::Range(minimum[2], maximum[2]));
outPortal.Set(3, maximum[1]);
outPortal.Set(4, minimum[2]);
outPortal.Set(5, maximum[2]);
} }
private: private:
vtkm::cont::ArrayHandle<vtkm::Float64> *Bounds; vtkm::cont::ArrayHandle<vtkm::Range> *Range;
}; };
} // namespace internal } // namespace internal
...@@ -143,7 +140,7 @@ public: ...@@ -143,7 +140,7 @@ public:
AssocCellSetName(), AssocCellSetName(),
AssocLogicalDim(-1), AssocLogicalDim(-1),
Data(data), Data(data),
Bounds(), Range(),
ModifiedFlag(true) ModifiedFlag(true)
{ {
VTKM_ASSERT(this->Association == ASSOC_WHOLE_MESH || VTKM_ASSERT(this->Association == ASSOC_WHOLE_MESH ||
...@@ -160,7 +157,7 @@ public: ...@@ -160,7 +157,7 @@ public:
AssocCellSetName(), AssocCellSetName(),
AssocLogicalDim(-1), AssocLogicalDim(-1),
Data(data), Data(data),
Bounds(), Range(),
ModifiedFlag(true) ModifiedFlag(true)
{ {
VTKM_ASSERT((this->Association == ASSOC_WHOLE_MESH) || VTKM_ASSERT((this->Association == ASSOC_WHOLE_MESH) ||
...@@ -176,7 +173,7 @@ public: ...@@ -176,7 +173,7 @@ public:
Association(association), Association(association),
AssocCellSetName(), AssocCellSetName(),
AssocLogicalDim(-1), AssocLogicalDim(-1),
Bounds(), Range(),
ModifiedFlag(true) ModifiedFlag(true)
{ {
VTKM_ASSERT((this->Association == ASSOC_WHOLE_MESH) || VTKM_ASSERT((this->Association == ASSOC_WHOLE_MESH) ||
...@@ -194,7 +191,7 @@ public: ...@@ -194,7 +191,7 @@ public:
Association(association), Association(association),
AssocCellSetName(), AssocCellSetName(),
AssocLogicalDim(-1), AssocLogicalDim(-1),
Bounds(), Range(),
ModifiedFlag(true) ModifiedFlag(true)
{ {
VTKM_ASSERT((this->Association == ASSOC_WHOLE_MESH) || VTKM_ASSERT((this->Association == ASSOC_WHOLE_MESH) ||
...@@ -213,7 +210,7 @@ public: ...@@ -213,7 +210,7 @@ public:
AssocCellSetName(cellSetName), AssocCellSetName(cellSetName),
AssocLogicalDim(-1), AssocLogicalDim(-1),
Data(data), Data(data),
Bounds(), Range(),
ModifiedFlag(true) ModifiedFlag(true)
{ {
VTKM_ASSERT(this->Association == ASSOC_CELL_SET); VTKM_ASSERT(this->Association == ASSOC_CELL_SET);
...@@ -230,7 +227,7 @@ public: ...@@ -230,7 +227,7 @@ public:
AssocCellSetName(cellSetName), AssocCellSetName(cellSetName),
AssocLogicalDim(-1), AssocLogicalDim(-1),
Data(data), Data(data),
Bounds(), Range(),
ModifiedFlag(true) ModifiedFlag(true)
{ {
VTKM_ASSERT(this->Association == ASSOC_CELL_SET); VTKM_ASSERT(this->Association == ASSOC_CELL_SET);
...@@ -246,7 +243,7 @@ public: ...@@ -246,7 +243,7 @@ public:
Association(association), Association(association),
AssocCellSetName(cellSetName), AssocCellSetName(cellSetName),
AssocLogicalDim(-1), AssocLogicalDim(-1),
Bounds(), Range(),
ModifiedFlag(true) ModifiedFlag(true)
{ {
VTKM_ASSERT(this->Association == ASSOC_CELL_SET); VTKM_ASSERT(this->Association == ASSOC_CELL_SET);
...@@ -264,7 +261,7 @@ public: ...@@ -264,7 +261,7 @@ public:
Association(association), Association(association),
AssocCellSetName(cellSetName), AssocCellSetName(cellSetName),
AssocLogicalDim(-1), AssocLogicalDim(-1),
Bounds(), Range(),
ModifiedFlag(true) ModifiedFlag(true)
{ {
VTKM_ASSERT(this->Association == ASSOC_CELL_SET); VTKM_ASSERT(this->Association == ASSOC_CELL_SET);
...@@ -282,7 +279,7 @@ public: ...@@ -282,7 +279,7 @@ public:
AssocCellSetName(), AssocCellSetName(),
AssocLogicalDim(logicalDim), AssocLogicalDim(logicalDim),
Data(data), Data(data),
Bounds(), Range(),
ModifiedFlag(true) ModifiedFlag(true)
{ {
VTKM_ASSERT(this->Association == ASSOC_LOGICAL_DIM); VTKM_ASSERT(this->Association == ASSOC_LOGICAL_DIM);
...@@ -298,7 +295,7 @@ public: ...@@ -298,7 +295,7 @@ public:
Association(association), Association(association),
AssocLogicalDim(logicalDim), AssocLogicalDim(logicalDim),
Data(data), Data(data),
Bounds(), Range(),
ModifiedFlag(true) ModifiedFlag(true)
{ {
VTKM_ASSERT(this->Association == ASSOC_LOGICAL_DIM); VTKM_ASSERT(this->Association == ASSOC_LOGICAL_DIM);
...@@ -313,7 +310,7 @@ public: ...@@ -313,7 +310,7 @@ public:
: Name(name), : Name(name),
Association(association), Association(association),
AssocLogicalDim(logicalDim), AssocLogicalDim(logicalDim),
Bounds(), Range(),
ModifiedFlag(true) ModifiedFlag(true)
{ {
VTKM_ASSERT(this->Association == ASSOC_LOGICAL_DIM); VTKM_ASSERT(this->Association == ASSOC_LOGICAL_DIM);
...@@ -329,7 +326,7 @@ public: ...@@ -329,7 +326,7 @@ public:
: Name(name), : Name(name),
Association(association), Association(association),
AssocLogicalDim(logicalDim), AssocLogicalDim(logicalDim),
Bounds(), Range(),
ModifiedFlag(true) ModifiedFlag(true)
{ {
VTKM_ASSERT(this->Association == ASSOC_LOGICAL_DIM); VTKM_ASSERT(this->Association == ASSOC_LOGICAL_DIM);
...@@ -343,7 +340,7 @@ public: ...@@ -343,7 +340,7 @@ public:
AssocCellSetName(), AssocCellSetName(),
AssocLogicalDim(), AssocLogicalDim(),
Data(), Data(),
Bounds(), Range(),
ModifiedFlag(true) ModifiedFlag(true)
{ {
//Generate an empty field //Generate an empty field
...@@ -375,67 +372,73 @@ public: ...@@ -375,67 +372,73 @@ public:
template<typename DeviceAdapterTag, typename TypeList, typename StorageList> template<typename DeviceAdapterTag, typename TypeList, typename StorageList>
VTKM_CONT_EXPORT VTKM_CONT_EXPORT
const vtkm::cont::ArrayHandle<vtkm::Float64>& GetBounds(DeviceAdapterTag, const vtkm::cont::ArrayHandle<vtkm::Range>& GetRange(DeviceAdapterTag,
TypeList, TypeList,
StorageList) const StorageList) const
{ {
if (this->ModifiedFlag) if (this->ModifiedFlag)
{ {
internal::ComputeBounds<DeviceAdapterTag> computeBounds(this->Bounds); internal::ComputeRange<DeviceAdapterTag> computeRange(this->Range);