From e7e39e9ed6ad5e6bd61fe54ee76615cb686cb4e5 Mon Sep 17 00:00:00 2001 From: Kenneth Moreland Date: Mon, 19 Feb 2018 10:13:07 -0700 Subject: [PATCH] Fix including empty range There was a bug where if you included an empty range into another range, the range suddenly became infinite. This is because empty ranges have the max at -infinity and the min at infinity. If you just include the endpoints independently (which is what include does), then the min gets -infinity and the max gets infinity. This gets a check to make sure that empty ranges are ignored. --- vtkm/Range.h | 7 +++++-- vtkm/testing/UnitTestBounds.cxx | 7 +++++++ vtkm/testing/UnitTestRange.cxx | 6 ++++++ 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/vtkm/Range.h b/vtkm/Range.h index 240131794..e46b06a06 100644 --- a/vtkm/Range.h +++ b/vtkm/Range.h @@ -144,8 +144,11 @@ struct Range VTKM_EXEC_CONT void Include(const vtkm::Range& range) { - this->Include(range.Min); - this->Include(range.Max); + if (range.IsNonEmpty()) + { + this->Include(range.Min); + this->Include(range.Max); + } } /// \b Return the union of this and another range. diff --git a/vtkm/testing/UnitTestBounds.cxx b/vtkm/testing/UnitTestBounds.cxx index 52bc76f46..43b3b369c 100644 --- a/vtkm/testing/UnitTestBounds.cxx +++ b/vtkm/testing/UnitTestBounds.cxx @@ -33,6 +33,13 @@ void TestBounds() vtkm::Bounds emptyBounds; VTKM_TEST_ASSERT(!emptyBounds.IsNonEmpty(), "Non empty bounds not empty."); + vtkm::Bounds emptyBounds2; + VTKM_TEST_ASSERT(!emptyBounds2.IsNonEmpty(), "2nd empty bounds not empty."); + VTKM_TEST_ASSERT(!emptyBounds.Union(emptyBounds2).IsNonEmpty(), + "Union of empty bounds not empty."); + emptyBounds2.Include(emptyBounds); + VTKM_TEST_ASSERT(!emptyBounds2.IsNonEmpty(), "Include empty in empty is 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?"); diff --git a/vtkm/testing/UnitTestRange.cxx b/vtkm/testing/UnitTestRange.cxx index 1238ca541..c2e16b37f 100644 --- a/vtkm/testing/UnitTestRange.cxx +++ b/vtkm/testing/UnitTestRange.cxx @@ -32,6 +32,12 @@ void TestRange() VTKM_TEST_ASSERT(!emptyRange.IsNonEmpty(), "Non empty range not empty."); VTKM_TEST_ASSERT(test_equal(emptyRange.Length(), 0.0), "Bad length."); + vtkm::Range emptyRange2; + VTKM_TEST_ASSERT(!emptyRange2.IsNonEmpty(), "2nd empty range not empty."); + VTKM_TEST_ASSERT(!emptyRange.Union(emptyRange2).IsNonEmpty(), "Union of empty ranges not empty."); + emptyRange2.Include(emptyRange); + VTKM_TEST_ASSERT(!emptyRange2.IsNonEmpty(), "Include empty in empty is not empty."); + std::cout << "Single value range." << std::endl; vtkm::Range singleValueRange(5.0, 5.0); VTKM_TEST_ASSERT(singleValueRange.IsNonEmpty(), "Empty?");