mirror of
https://gitlab.kitware.com/vtk/vtk-m
synced 2024-09-16 17:22:55 +00:00
Add Range struct
This is a simple struct that has a min and max scalar value to represent a range of scalar values. It also comes with several helper methods.
This commit is contained in:
parent
a338ac52a2
commit
b358fcb19f
@ -32,6 +32,7 @@ set(headers
|
||||
Matrix.h
|
||||
NewtonsMethod.h
|
||||
Pair.h
|
||||
Range.h
|
||||
StaticAssert.h
|
||||
TopologyElementTag.h
|
||||
TypeListTag.h
|
||||
|
150
vtkm/Range.h
Normal file
150
vtkm/Range.h
Normal file
@ -0,0 +1,150 @@
|
||||
//============================================================================
|
||||
// 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_Range_h
|
||||
#define vtk_m_Range_h
|
||||
|
||||
#include <vtkm/Assert.h>
|
||||
#include <vtkm/Math.h>
|
||||
#include <vtkm/Types.h>
|
||||
|
||||
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 Range also contains several helper functions for maintaining the range.
|
||||
///
|
||||
struct Range
|
||||
{
|
||||
vtkm::Float64 Min;
|
||||
vtkm::Float64 Max;
|
||||
|
||||
VTKM_EXEC_CONT_EXPORT
|
||||
Range() : Min(vtkm::Infinity64()), Max(vtkm::NegativeInfinity64()) { }
|
||||
|
||||
template<typename T1, typename T2>
|
||||
VTKM_EXEC_CONT_EXPORT
|
||||
Range(const T1 &min, const T2 &max)
|
||||
: Min(static_cast<vtkm::Float64>(min)), Max(static_cast<vtkm::Float64>(max))
|
||||
{ }
|
||||
|
||||
VTKM_EXEC_CONT_EXPORT
|
||||
const vtkm::Range &operator=(const vtkm::Range &src)
|
||||
{
|
||||
this->Min = src.Min;
|
||||
this->Max = src.Max;
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// \b Determine if the range is valid (i.e. has at least one valid point).
|
||||
///
|
||||
/// \c IsNonEmpty return true if the range contains some valid values between
|
||||
/// \c Min and \c Max. If \c Max is less than \c Min, then no values satisfy
|
||||
/// the range and \c IsNonEmpty returns false. Otherwise, return true.
|
||||
///
|
||||
/// \c IsNonEmpty assumes \c Min and \c Max are inclusive. That is, if they
|
||||
/// are equal then true is returned.
|
||||
///
|
||||
VTKM_EXEC_CONT_EXPORT
|
||||
bool IsNonEmpty() const
|
||||
{
|
||||
return (this->Min <= this->Max);
|
||||
}
|
||||
|
||||
/// \b Determines if a value is within the range.
|
||||
///
|
||||
/// \c Contains returns true if the give value is within the range, false
|
||||
/// otherwise. \c Contains treats the min and max as inclusive. That is, if
|
||||
/// the value is exactly the min or max, true is returned.
|
||||
///
|
||||
template<typename T>
|
||||
VTKM_EXEC_CONT_EXPORT
|
||||
bool Contains(const T &value) const
|
||||
{
|
||||
return ((this->Min <= static_cast<vtkm::Float64>(value)) &&
|
||||
(this->Max >= static_cast<vtkm::Float64>(value)));
|
||||
}
|
||||
|
||||
/// \b Expand range to include a value.
|
||||
///
|
||||
/// This version of \c Include expands the range just enough to include the
|
||||
/// given value. If the range already includes this value, then nothing is
|
||||
/// done.
|
||||
///
|
||||
template<typename T>
|
||||
VTKM_EXEC_CONT_EXPORT
|
||||
void Include(const T &value)
|
||||
{
|
||||
this->Min = vtkm::Min(this->Min, static_cast<vtkm::Float64>(value));
|
||||
this->Max = vtkm::Max(this->Max, static_cast<vtkm::Float64>(value));
|
||||
}
|
||||
|
||||
/// \b Expand range to include other range.
|
||||
///
|
||||
/// This version of \c Include expands this range just enough to include that
|
||||
/// of another range. Esentially it is the union of the two ranges.
|
||||
///
|
||||
VTKM_EXEC_CONT_EXPORT
|
||||
void Include(const vtkm::Range &range)
|
||||
{
|
||||
this->Include(range.Min);
|
||||
this->Include(range.Max);
|
||||
}
|
||||
|
||||
/// \b Return the union of this and another range.
|
||||
///
|
||||
/// This is a nondestructive form of \c Include.
|
||||
///
|
||||
VTKM_EXEC_CONT_EXPORT
|
||||
vtkm::Range Union(const vtkm::Range &otherRange) const
|
||||
{
|
||||
vtkm::Range unionRange(*this);
|
||||
unionRange.Include(otherRange);
|
||||
return unionRange;
|
||||
}
|
||||
|
||||
/// \b Operator for union
|
||||
///
|
||||
VTKM_EXEC_CONT_EXPORT
|
||||
vtkm::Range operator+(const vtkm::Range &otherRange) const
|
||||
{
|
||||
return this->Union(otherRange);
|
||||
}
|
||||
|
||||
VTKM_EXEC_CONT_EXPORT
|
||||
bool operator==(const vtkm::Range &otherRange) const
|
||||
{
|
||||
return ((this->Min == otherRange.Min) && (this->Max == otherRange.Max));
|
||||
}
|
||||
|
||||
VTKM_EXEC_CONT_EXPORT
|
||||
bool operator!=(const vtkm::Range &otherRange) const
|
||||
{
|
||||
return ((this->Min != otherRange.Min) && (this->Max != otherRange.Max));
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace vtkm
|
||||
|
||||
#endif //vtk_m_Range_h
|
@ -38,6 +38,7 @@ set(unit_tests
|
||||
UnitTestMatrix.cxx
|
||||
UnitTestNewtonsMethod.cxx
|
||||
UnitTestPair.cxx
|
||||
UnitTestRange.cxx
|
||||
UnitTestTesting.cxx
|
||||
UnitTestTypeListTag.cxx
|
||||
UnitTestTypes.cxx
|
||||
|
125
vtkm/testing/UnitTestRange.cxx
Normal file
125
vtkm/testing/UnitTestRange.cxx
Normal file
@ -0,0 +1,125 @@
|
||||
//============================================================================
|
||||
// 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/Range.h>
|
||||
|
||||
#include <vtkm/testing/Testing.h>
|
||||
|
||||
namespace {
|
||||
|
||||
void TestRange()
|
||||
{
|
||||
std::cout << "Empty range." << std::endl;
|
||||
vtkm::Range emptyRange;
|
||||
VTKM_TEST_ASSERT(!emptyRange.IsNonEmpty(), "Non empty range not empty.");
|
||||
|
||||
std::cout << "Single value range." << std::endl;
|
||||
vtkm::Range singleValueRange(5.0, 5.0);
|
||||
VTKM_TEST_ASSERT(singleValueRange.IsNonEmpty(), "Empty?");
|
||||
VTKM_TEST_ASSERT(singleValueRange.Contains(5.0), "Does not contain value");
|
||||
VTKM_TEST_ASSERT(!singleValueRange.Contains(0.0), "Contains outside");
|
||||
VTKM_TEST_ASSERT(!singleValueRange.Contains(10), "Contains outside");
|
||||
|
||||
vtkm::Range unionRange = emptyRange + singleValueRange;
|
||||
VTKM_TEST_ASSERT(unionRange.IsNonEmpty(), "Empty?");
|
||||
VTKM_TEST_ASSERT(unionRange.Contains(5.0), "Does not contain value");
|
||||
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");
|
||||
|
||||
std::cout << "Low range." << std::endl;
|
||||
vtkm::Range lowRange(-10.0, -5.0);
|
||||
VTKM_TEST_ASSERT(lowRange.IsNonEmpty(), "Empty?");
|
||||
VTKM_TEST_ASSERT(!lowRange.Contains(-20), "Contains fail");
|
||||
VTKM_TEST_ASSERT(lowRange.Contains(-7), "Contains fail");
|
||||
VTKM_TEST_ASSERT(!lowRange.Contains(0), "Contains fail");
|
||||
VTKM_TEST_ASSERT(!lowRange.Contains(10), "Contains fail");
|
||||
|
||||
unionRange = singleValueRange + lowRange;
|
||||
VTKM_TEST_ASSERT(unionRange.IsNonEmpty(), "Empty?");
|
||||
VTKM_TEST_ASSERT(!unionRange.Contains(-20), "Contains fail");
|
||||
VTKM_TEST_ASSERT(unionRange.Contains(-7), "Contains fail");
|
||||
VTKM_TEST_ASSERT(unionRange.Contains(0), "Contains fail");
|
||||
VTKM_TEST_ASSERT(!unionRange.Contains(10), "Contains fail");
|
||||
|
||||
std::cout << "High range." << std::endl;
|
||||
vtkm::Range highRange(15.0, 20.0);
|
||||
VTKM_TEST_ASSERT(highRange.IsNonEmpty(), "Empty?");
|
||||
VTKM_TEST_ASSERT(!highRange.Contains(-20), "Contains fail");
|
||||
VTKM_TEST_ASSERT(!highRange.Contains(-7), "Contains fail");
|
||||
VTKM_TEST_ASSERT(!highRange.Contains(0), "Contains fail");
|
||||
VTKM_TEST_ASSERT(!highRange.Contains(10), "Contains fail");
|
||||
VTKM_TEST_ASSERT(highRange.Contains(17), "Contains fail");
|
||||
VTKM_TEST_ASSERT(!highRange.Contains(25), "Contains fail");
|
||||
|
||||
unionRange = highRange.Union(singleValueRange);
|
||||
VTKM_TEST_ASSERT(unionRange.IsNonEmpty(), "Empty?");
|
||||
VTKM_TEST_ASSERT(!unionRange.Contains(-20), "Contains fail");
|
||||
VTKM_TEST_ASSERT(!unionRange.Contains(-7), "Contains fail");
|
||||
VTKM_TEST_ASSERT(!unionRange.Contains(0), "Contains fail");
|
||||
VTKM_TEST_ASSERT(unionRange.Contains(10), "Contains fail");
|
||||
VTKM_TEST_ASSERT(unionRange.Contains(17), "Contains fail");
|
||||
VTKM_TEST_ASSERT(!unionRange.Contains(25), "Contains fail");
|
||||
|
||||
unionRange.Include(-1);
|
||||
VTKM_TEST_ASSERT(unionRange.IsNonEmpty(), "Empty?");
|
||||
VTKM_TEST_ASSERT(!unionRange.Contains(-20), "Contains fail");
|
||||
VTKM_TEST_ASSERT(!unionRange.Contains(-7), "Contains fail");
|
||||
VTKM_TEST_ASSERT(unionRange.Contains(0), "Contains fail");
|
||||
VTKM_TEST_ASSERT(unionRange.Contains(10), "Contains fail");
|
||||
VTKM_TEST_ASSERT(unionRange.Contains(17), "Contains fail");
|
||||
VTKM_TEST_ASSERT(!unionRange.Contains(25), "Contains fail");
|
||||
|
||||
unionRange.Include(lowRange);
|
||||
VTKM_TEST_ASSERT(unionRange.IsNonEmpty(), "Empty?");
|
||||
VTKM_TEST_ASSERT(!unionRange.Contains(-20), "Contains fail");
|
||||
VTKM_TEST_ASSERT(unionRange.Contains(-7), "Contains fail");
|
||||
VTKM_TEST_ASSERT(unionRange.Contains(0), "Contains fail");
|
||||
VTKM_TEST_ASSERT(unionRange.Contains(10), "Contains fail");
|
||||
VTKM_TEST_ASSERT(unionRange.Contains(17), "Contains fail");
|
||||
VTKM_TEST_ASSERT(!unionRange.Contains(25), "Contains fail");
|
||||
|
||||
std::cout << "Try adding infinity." << std::endl;
|
||||
unionRange.Include(vtkm::Infinity64());
|
||||
VTKM_TEST_ASSERT(unionRange.IsNonEmpty(), "Empty?");
|
||||
VTKM_TEST_ASSERT(!unionRange.Contains(-20), "Contains fail");
|
||||
VTKM_TEST_ASSERT(unionRange.Contains(-7), "Contains fail");
|
||||
VTKM_TEST_ASSERT(unionRange.Contains(0), "Contains fail");
|
||||
VTKM_TEST_ASSERT(unionRange.Contains(10), "Contains fail");
|
||||
VTKM_TEST_ASSERT(unionRange.Contains(17), "Contains fail");
|
||||
VTKM_TEST_ASSERT(unionRange.Contains(25), "Contains fail");
|
||||
|
||||
std::cout << "Try adding NaN." << std::endl;
|
||||
unionRange.Include(vtkm::Nan64());
|
||||
VTKM_TEST_ASSERT(unionRange.IsNonEmpty(), "Empty?");
|
||||
VTKM_TEST_ASSERT(!unionRange.Contains(-20), "Contains fail");
|
||||
VTKM_TEST_ASSERT(unionRange.Contains(-7), "Contains fail");
|
||||
VTKM_TEST_ASSERT(unionRange.Contains(0), "Contains fail");
|
||||
VTKM_TEST_ASSERT(unionRange.Contains(10), "Contains fail");
|
||||
VTKM_TEST_ASSERT(unionRange.Contains(17), "Contains fail");
|
||||
VTKM_TEST_ASSERT(unionRange.Contains(25), "Contains fail");
|
||||
}
|
||||
|
||||
} // anonymous namespace
|
||||
|
||||
int UnitTestRange(int, char *[])
|
||||
{
|
||||
return vtkm::testing::Testing::Run(TestRange);
|
||||
}
|
Loading…
Reference in New Issue
Block a user