Add Bounds struct

This is a simple struct that defines min and max values over X, Y, and Z
to define an axis-aligned bounding box.
This commit is contained in:
Kenneth Moreland 2016-05-26 10:45:03 -06:00
parent b358fcb19f
commit d75857d0bc
6 changed files with 332 additions and 5 deletions

186
vtkm/Bounds.h Normal file

@ -0,0 +1,186 @@
//============================================================================
// 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 2016 Sandia Corporation.
// Copyright 2016 UT-Battelle, LLC.
// Copyright 2016 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_Bounds_h
#define vtk_m_Bounds_h
#include <vtkm/Range.h>
namespace vtkm {
/// \brief Represent an axis-aligned 3D bounds in space.
///
/// \c vtkm::Bounds is a helper class for representing the axis-aligned box
/// representing some region in space. The typical use of this class is to
/// express the containing box of some geometry. The box is specified as ranges
/// in the x, y, and z directions.
///
/// \c Bounds also contains several helper functions for computing and
/// maintaining the bounds.
///
struct Bounds
{
vtkm::Range X;
vtkm::Range Y;
vtkm::Range Z;
VTKM_EXEC_CONT_EXPORT
Bounds() { }
VTKM_EXEC_CONT_EXPORT
Bounds(const vtkm::Range &xRange,
const vtkm::Range &yRange,
const vtkm::Range &zRange)
: X(xRange), Y(yRange), Z(zRange) { }
template<typename T>
VTKM_EXEC_CONT_EXPORT
Bounds(const T &minX, const T &maxX,
const T &minY, const T &maxY,
const T &minZ, const T &maxZ)
: X(vtkm::Range(minX, maxX)),
Y(vtkm::Range(minY, maxY)),
Z(vtkm::Range(minZ, maxZ))
{ }
/// Initialize bounds with an array of 6 values in the order xmin, xmax,
/// ymin, ymax, zmin, zmax.
///
template<typename T>
VTKM_EXEC_CONT_EXPORT
explicit Bounds(const T bounds[6])
: X(vtkm::Range(bounds[0], bounds[1])),
Y(vtkm::Range(bounds[2], bounds[3])),
Z(vtkm::Range(bounds[4], bounds[5]))
{ }
/// Initialize bounds with the minimum corner point and the maximum corner
/// point.
///
template<typename T>
VTKM_EXEC_CONT_EXPORT
Bounds(const vtkm::Vec<T,3> &minPoint, const vtkm::Vec<T,3> &maxPoint)
: X(vtkm::Range(minPoint[0], maxPoint[0])),
Y(vtkm::Range(minPoint[1], maxPoint[1])),
Z(vtkm::Range(minPoint[2], maxPoint[2]))
{ }
VTKM_EXEC_CONT_EXPORT
const vtkm::Bounds &operator=(const vtkm::Bounds &src)
{
this->X = src.X;
this->Y = src.Y;
this->Z = src.Z;
return *this;
}
/// \b Determine if the bounds are valid (i.e. has at least one valid point).
///
/// \c IsNonEmpty returns true if the bounds contain some valid points. If
/// the bounds are any real region, even if a single point or it expands to
/// infinity, true is returned.
///
VTKM_EXEC_CONT_EXPORT
bool IsNonEmpty() const
{
return (this->X.IsNonEmpty() &&
this->Y.IsNonEmpty() &&
this->Z.IsNonEmpty());
}
/// \b Determines if a point coordinate is within the bounds.
///
template<typename T>
VTKM_EXEC_CONT_EXPORT
bool Contains(const vtkm::Vec<T,3> &point) const
{
return (this->X.Contains(point[0]) &&
this->Y.Contains(point[1]) &&
this->Z.Contains(point[2]));
}
/// \b Expand bounds to include a point.
///
/// This version of \c Include expands the bounds just enough to include the
/// given point coordinates. If the bounds already include this point, then
/// nothing is done.
///
template<typename T>
VTKM_EXEC_CONT_EXPORT
void Include(const vtkm::Vec<T,3> &point)
{
this->X.Include(point[0]);
this->Y.Include(point[1]);
this->Z.Include(point[2]);
}
/// \b Expand bounds to include other bounds.
///
/// This version of \c Include expands these bounds just enough to include
/// that of another bounds. Esentially it is the union of the two bounds.
///
VTKM_EXEC_CONT_EXPORT
void Include(const vtkm::Bounds &bounds)
{
this->X.Include(bounds.X);
this->Y.Include(bounds.Y);
this->Z.Include(bounds.Z);
}
/// \b Return the union of this and another bounds.
///
/// This is a nondestructive form of \c Include.
///
VTKM_EXEC_CONT_EXPORT
vtkm::Bounds Union(const vtkm::Bounds &otherBounds) const
{
vtkm::Bounds unionBounds(*this);
unionBounds.Include(otherBounds);
return unionBounds;
}
/// \b Operator for union
///
VTKM_EXEC_CONT_EXPORT
vtkm::Bounds operator+(const vtkm::Bounds &otherBounds) const
{
return this->Union(otherBounds);
}
VTKM_EXEC_CONT_EXPORT
bool operator==(const vtkm::Bounds &bounds) const
{
return ((this->X == bounds.X) &&
(this->Y == bounds.Y) &&
(this->Z == bounds.Z));
}
VTKM_EXEC_CONT_EXPORT
bool operator!=(const vtkm::Bounds &bounds) const
{
return ((this->X != bounds.X) ||
(this->Y != bounds.Y) ||
(this->Z != bounds.Z));
}
};
} // namespace vtkm
#endif //vtk_m_Bounds_h

@ -24,6 +24,7 @@ set(headers
Assert.h
BinaryPredicates.h
BinaryOperators.h
Bounds.h
CellShape.h
CellTraits.h
ImplicitFunctions.h

@ -29,11 +29,12 @@ namespace vtkm {
/// \brief Represent a continuous scalar range of values.
///
/// A \c vtkm::Range is a helper class for representing a range of floating
/// point values from a minimum value to a maximum value. This is specified
/// simply enough with a \c Min and \c Max value.
/// \c vtkm::Range is a helper class for representing a range of floating point
/// values from a minimum value to a maximum value. This is specified simply
/// enough with a \c Min and \c Max value.
///
/// \c Range also contains several helper functions for maintaining the range.
/// \c Range also contains several helper functions for computing and
/// maintaining the range.
///
struct Range
{
@ -141,7 +142,7 @@ struct Range
VTKM_EXEC_CONT_EXPORT
bool operator!=(const vtkm::Range &otherRange) const
{
return ((this->Min != otherRange.Min) && (this->Max != otherRange.Max));
return ((this->Min != otherRange.Min) || (this->Max != otherRange.Max));
}
};

@ -31,6 +31,7 @@ VTKM_declare_headers(${headers})
set(unit_tests
UnitTestBinaryPredicates.cxx
UnitTestBinaryOperators.cxx
UnitTestBounds.cxx
UnitTestCellShape.cxx
UnitTestImplicitFunctions.cxx
UnitTestListTag.cxx

@ -0,0 +1,137 @@
//============================================================================
// 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 2016 Sandia Corporation.
// Copyright 2016 UT-Battelle, LLC.
// Copyright 2016 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/Bounds.h>
#include <vtkm/testing/Testing.h>
namespace {
void TestBounds()
{
typedef vtkm::Vec<vtkm::Float64,3> Vec3;
std::cout << "Empty bounds." << std::endl;
vtkm::Bounds emptyBounds;
VTKM_TEST_ASSERT(!emptyBounds.IsNonEmpty(), "Non empty bounds not empty.");
std::cout << "Single value bounds." << std::endl;
vtkm::Bounds singleValueBounds(1.0, 1.0, 2.0, 2.0, 3.0, 3.0);
VTKM_TEST_ASSERT(singleValueBounds.IsNonEmpty(), "Empty?");
VTKM_TEST_ASSERT(singleValueBounds.Contains(Vec3(1,2,3)), "Contains fail");
VTKM_TEST_ASSERT(!singleValueBounds.Contains(Vec3(0,0,0)), "Contains fail");
VTKM_TEST_ASSERT(!singleValueBounds.Contains(Vec3(2,2,2)), "contains fail");
VTKM_TEST_ASSERT(!singleValueBounds.Contains(Vec3(5,5,5)), "contains fail");
vtkm::Bounds unionBounds = emptyBounds + singleValueBounds;
VTKM_TEST_ASSERT(unionBounds.IsNonEmpty(), "Empty?");
VTKM_TEST_ASSERT(unionBounds.Contains(Vec3(1,2,3)), "Contains fail");
VTKM_TEST_ASSERT(!unionBounds.Contains(Vec3(0,0,0)), "Contains fail");
VTKM_TEST_ASSERT(!unionBounds.Contains(Vec3(2,2,2)), "contains fail");
VTKM_TEST_ASSERT(!unionBounds.Contains(Vec3(5,5,5)), "contains fail");
VTKM_TEST_ASSERT(singleValueBounds == unionBounds, "Union not equal");
std::cout << "Low bounds." << std::endl;
vtkm::Bounds lowBounds(Vec3(-10,-5,-1), Vec3(-5, -2, 0));
VTKM_TEST_ASSERT(lowBounds.IsNonEmpty(), "Empty?");
VTKM_TEST_ASSERT(!lowBounds.Contains(Vec3(-20)), "Contains fail");
VTKM_TEST_ASSERT(!lowBounds.Contains(Vec3(-2)), "Contains fail");
VTKM_TEST_ASSERT(lowBounds.Contains(Vec3(-7,-2,-0.5)), "Contains fail");
VTKM_TEST_ASSERT(!lowBounds.Contains(Vec3(0)), "Contains fail");
VTKM_TEST_ASSERT(!lowBounds.Contains(Vec3(10)), "Contains fail");
unionBounds = singleValueBounds + lowBounds;
VTKM_TEST_ASSERT(unionBounds.IsNonEmpty(), "Empty?");
VTKM_TEST_ASSERT(!unionBounds.Contains(Vec3(-20)), "Contains fail");
VTKM_TEST_ASSERT(!unionBounds.Contains(Vec3(-2)), "Contains fail");
VTKM_TEST_ASSERT(unionBounds.Contains(Vec3(-7,-2,-0.5)), "Contains fail");
VTKM_TEST_ASSERT(unionBounds.Contains(Vec3(0)), "Contains fail");
VTKM_TEST_ASSERT(!unionBounds.Contains(Vec3(10)), "Contains fail");
std::cout << "High bounds." << std::endl;
vtkm::Float64 highBoundsArray[6] = {15.0, 20.0, 2.0, 5.0, 5.0, 10.0 };
vtkm::Bounds highBounds(highBoundsArray);
VTKM_TEST_ASSERT(highBounds.IsNonEmpty(), "Empty?");
VTKM_TEST_ASSERT(!highBounds.Contains(Vec3(-20)), "Contains fail");
VTKM_TEST_ASSERT(!highBounds.Contains(Vec3(-2)), "Contains fail");
VTKM_TEST_ASSERT(!highBounds.Contains(Vec3(-7,-2,-0.5)), "Contains fail");
VTKM_TEST_ASSERT(!highBounds.Contains(Vec3(0)), "Contains fail");
VTKM_TEST_ASSERT(!highBounds.Contains(Vec3(4)), "Contains fail");
VTKM_TEST_ASSERT(highBounds.Contains(Vec3(17,3,7)), "Contains fail");
VTKM_TEST_ASSERT(!highBounds.Contains(Vec3(25)), "Contains fail");
unionBounds = highBounds.Union(singleValueBounds);
VTKM_TEST_ASSERT(unionBounds.IsNonEmpty(), "Empty?");
VTKM_TEST_ASSERT(!unionBounds.Contains(Vec3(-20)), "Contains fail");
VTKM_TEST_ASSERT(!unionBounds.Contains(Vec3(-2)), "Contains fail");
VTKM_TEST_ASSERT(!unionBounds.Contains(Vec3(-7,-2,-0.5)), "Contains fail");
VTKM_TEST_ASSERT(!unionBounds.Contains(Vec3(0)), "Contains fail");
VTKM_TEST_ASSERT(unionBounds.Contains(Vec3(4)), "Contains fail");
VTKM_TEST_ASSERT(unionBounds.Contains(Vec3(17,3,7)), "Contains fail");
VTKM_TEST_ASSERT(!unionBounds.Contains(Vec3(25)), "Contains fail");
unionBounds.Include(Vec3(-1));
VTKM_TEST_ASSERT(unionBounds.IsNonEmpty(), "Empty?");
VTKM_TEST_ASSERT(!unionBounds.Contains(Vec3(-20)), "Contains fail");
VTKM_TEST_ASSERT(!unionBounds.Contains(Vec3(-2)), "Contains fail");
VTKM_TEST_ASSERT(!unionBounds.Contains(Vec3(-7,-2,-0.5)), "Contains fail");
VTKM_TEST_ASSERT(unionBounds.Contains(Vec3(0)), "Contains fail");
VTKM_TEST_ASSERT(unionBounds.Contains(Vec3(4)), "Contains fail");
VTKM_TEST_ASSERT(unionBounds.Contains(Vec3(17,3,7)), "Contains fail");
VTKM_TEST_ASSERT(!unionBounds.Contains(Vec3(25)), "Contains fail");
unionBounds.Include(lowBounds);
VTKM_TEST_ASSERT(unionBounds.IsNonEmpty(), "Empty?");
VTKM_TEST_ASSERT(!unionBounds.Contains(Vec3(-20)), "Contains fail");
VTKM_TEST_ASSERT(!unionBounds.Contains(Vec3(-2)), "Contains fail");
VTKM_TEST_ASSERT(unionBounds.Contains(Vec3(-7,-2,-0.5)), "Contains fail");
VTKM_TEST_ASSERT(unionBounds.Contains(Vec3(0)), "Contains fail");
VTKM_TEST_ASSERT(unionBounds.Contains(Vec3(4)), "Contains fail");
VTKM_TEST_ASSERT(unionBounds.Contains(Vec3(17,3,7)), "Contains fail");
VTKM_TEST_ASSERT(!unionBounds.Contains(Vec3(25)), "Contains fail");
std::cout << "Try adding infinity." << std::endl;
unionBounds.Include(Vec3(vtkm::Infinity64()));
VTKM_TEST_ASSERT(unionBounds.IsNonEmpty(), "Empty?");
VTKM_TEST_ASSERT(!unionBounds.Contains(Vec3(-20)), "Contains fail");
VTKM_TEST_ASSERT(!unionBounds.Contains(Vec3(-2)), "Contains fail");
VTKM_TEST_ASSERT(unionBounds.Contains(Vec3(-7,-2,-0.5)), "Contains fail");
VTKM_TEST_ASSERT(unionBounds.Contains(Vec3(0)), "Contains fail");
VTKM_TEST_ASSERT(unionBounds.Contains(Vec3(4)), "Contains fail");
VTKM_TEST_ASSERT(unionBounds.Contains(Vec3(17,3,7)), "Contains fail");
VTKM_TEST_ASSERT(unionBounds.Contains(Vec3(25)), "Contains fail");
std::cout << "Try adding NaN." << std::endl;
unionBounds.Include(Vec3(vtkm::Nan64()));
VTKM_TEST_ASSERT(!unionBounds.Contains(Vec3(-20)), "Contains fail");
VTKM_TEST_ASSERT(!unionBounds.Contains(Vec3(-2)), "Contains fail");
VTKM_TEST_ASSERT(unionBounds.Contains(Vec3(-7,-2,-0.5)), "Contains fail");
VTKM_TEST_ASSERT(unionBounds.Contains(Vec3(0)), "Contains fail");
VTKM_TEST_ASSERT(unionBounds.Contains(Vec3(4)), "Contains fail");
VTKM_TEST_ASSERT(unionBounds.Contains(Vec3(17,3,7)), "Contains fail");
VTKM_TEST_ASSERT(unionBounds.Contains(Vec3(25)), "Contains fail");
}
} // anonymous namespace
int UnitTestBounds(int, char *[])
{
return vtkm::testing::Testing::Run(TestBounds);
}

@ -43,6 +43,7 @@ void TestRange()
VTKM_TEST_ASSERT(!unionRange.Contains(0.0), "Contains outside");
VTKM_TEST_ASSERT(!unionRange.Contains(10), "Contains outside");
VTKM_TEST_ASSERT(singleValueRange == unionRange, "Union not equal");
VTKM_TEST_ASSERT(!(singleValueRange == unionRange), "Union not equal");
std::cout << "Low range." << std::endl;
vtkm::Range lowRange(-10.0, -5.0);