Skip to content

Commit

Permalink
fix(qgsrange): consider include/exclude limits on both range for over…
Browse files Browse the repository at this point in the history
…laps
  • Loading branch information
Djedouas committed Jan 14, 2025
1 parent 82b6369 commit ad7596f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 20 deletions.
38 changes: 19 additions & 19 deletions src/core/qgsrange.h
Original file line number Diff line number Diff line change
Expand Up @@ -175,23 +175,23 @@ class QgsRange
*/
bool overlaps( const QgsRange<T> &other ) const
{
if ( ( ( mIncludeLower && mLower <= other.mLower ) || ( !mIncludeLower && mLower < other.mLower ) )
&& ( ( mIncludeUpper && mUpper >= other.mUpper ) || ( !mIncludeUpper && mUpper > other.mUpper ) ) )
if ( ( ( mIncludeLower && other.mIncludeLower && mLower <= other.mLower ) || ( ( !mIncludeLower || !other.mIncludeLower ) && mLower < other.mLower ) )
&& ( ( mIncludeUpper && other.mIncludeUpper && mUpper >= other.mUpper ) || ( ( !mIncludeUpper || !other.mIncludeUpper ) && mUpper > other.mUpper ) ) )
return true;

if ( ( ( mIncludeLower && mLower <= other.mLower ) || ( !mIncludeLower && mLower < other.mLower ) )
&& ( ( mIncludeUpper && mUpper >= other.mLower ) || ( !mIncludeUpper && mUpper > other.mLower ) ) )
if ( ( ( mIncludeLower && other.mIncludeLower && mLower <= other.mLower ) || ( ( !mIncludeLower || !other.mIncludeLower ) && mLower < other.mLower ) )
&& ( ( mIncludeUpper && other.mIncludeLower && mUpper >= other.mLower ) || ( ( !mIncludeUpper || !other.mIncludeLower ) && mUpper > other.mLower ) ) )
return true;

if ( ( ( mIncludeLower && mLower <= other.mUpper ) || ( !mIncludeLower && mLower < other.mUpper ) )
&& ( ( mIncludeUpper && mUpper >= other.mUpper ) || ( !mIncludeUpper && mUpper > other.mUpper ) ) )
if ( ( ( mIncludeLower && other.mIncludeUpper && mLower <= other.mUpper ) || ( ( !mIncludeLower || !other.mIncludeUpper ) && mLower < other.mUpper ) )
&& ( ( mIncludeUpper && other.mIncludeUpper && mUpper >= other.mUpper ) || ( ( !mIncludeUpper || !other.mIncludeUpper ) && mUpper > other.mUpper ) ) )
return true;

if ( ( ( mIncludeLower && mLower >= other.mLower ) || ( !mIncludeLower && mLower > other.mLower ) )
&& ( ( mIncludeLower && mLower <= other.mUpper ) || ( !mIncludeLower && mLower < other.mUpper ) ) )
if ( ( ( mIncludeLower && other.mIncludeLower && mLower >= other.mLower ) || ( ( !mIncludeLower || !other.mIncludeLower ) && mLower > other.mLower ) )
&& ( ( mIncludeLower && other.mIncludeUpper && mLower <= other.mUpper ) || ( ( !mIncludeLower || !other.mIncludeUpper ) && mLower < other.mUpper ) ) )
return true;

if ( mLower == other.mLower && mUpper == other.mUpper )
if ( mIncludeLower && mIncludeUpper && other.mIncludeLower && other.mIncludeUpper && mLower == other.mLower && mUpper == other.mUpper )
return true;

return false;
Expand Down Expand Up @@ -574,26 +574,26 @@ class QgsTemporalRange
*/
bool overlaps( const QgsTemporalRange<T> &other ) const
{
if ( !mUpper.isValid() && ( ( mIncludeLower && mLower <= other.mUpper ) || ( !mIncludeLower && mLower < other.mUpper ) ) )
if ( !mUpper.isValid() && ( ( mIncludeLower && other.mIncludeUpper && mLower <= other.mUpper ) || ( ( !mIncludeLower || !other.mIncludeUpper ) && mLower < other.mUpper ) ) )
return true;

if ( ( ( mIncludeLower && mLower <= other.mLower ) || ( !mIncludeLower && mLower < other.mLower ) )
&& ( ( mIncludeUpper && mUpper >= other.mUpper ) || ( !mIncludeUpper && mUpper > other.mUpper ) ) )
if ( ( ( mIncludeLower && other.mIncludeLower && mLower <= other.mLower ) || ( ( !mIncludeLower || !other.mIncludeLower ) && mLower < other.mLower ) )
&& ( ( mIncludeUpper && other.mIncludeUpper && mUpper >= other.mUpper ) || ( ( !mIncludeUpper || !other.mIncludeUpper ) && mUpper > other.mUpper ) ) )
return true;

if ( ( ( mIncludeLower && mLower <= other.mLower ) || ( !mIncludeLower && mLower < other.mLower ) )
&& ( ( mIncludeUpper && mUpper >= other.mLower ) || ( !mIncludeUpper && mUpper > other.mLower ) ) )
if ( ( ( mIncludeLower && other.mIncludeLower && mLower <= other.mLower ) || ( ( !mIncludeLower || other.mIncludeLower ) && mLower < other.mLower ) )
&& ( ( mIncludeUpper && other.mIncludeLower && mUpper >= other.mLower ) || ( ( !mIncludeUpper || !other.mIncludeLower ) && mUpper > other.mLower ) ) )
return true;

if ( ( ( mIncludeLower && mLower <= other.mUpper ) || ( !mIncludeLower && mLower < other.mUpper ) )
&& ( ( mIncludeUpper && mUpper >= other.mUpper ) || ( !mIncludeUpper && mUpper > other.mUpper ) ) )
if ( ( ( mIncludeLower && other.mIncludeUpper && mLower <= other.mUpper ) || ( ( !mIncludeLower || other.mIncludeUpper ) && mLower < other.mUpper ) )
&& ( ( mIncludeUpper && other.mIncludeUpper && mUpper >= other.mUpper ) || ( ( !mIncludeUpper || !other.mIncludeUpper ) && mUpper > other.mUpper ) ) )
return true;

if ( ( ( mIncludeLower && mLower >= other.mLower ) || ( !mIncludeLower && mLower > other.mLower ) )
&& ( ( mIncludeLower && mLower <= other.mUpper ) || ( !mIncludeLower && mLower < other.mUpper ) ) )
if ( ( ( mIncludeLower && other.mIncludeLower && mLower >= other.mLower ) || ( ( !mIncludeLower || !other.mIncludeLower ) && mLower > other.mLower ) )
&& ( ( mIncludeLower && other.mIncludeUpper && mLower <= other.mUpper ) || ( ( !mIncludeLower || !other.mIncludeUpper ) && mLower < other.mUpper ) ) )
return true;

if ( mLower == other.mLower && mUpper == other.mUpper )
if ( mIncludeLower && other.mIncludeLower && mIncludeUpper && other.mIncludeUpper && mLower == other.mLower && mUpper == other.mUpper )
return true;

return false;
Expand Down
8 changes: 7 additions & 1 deletion tests/src/python/test_qgsrange.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,8 @@ def testOverlaps(self):
self.assertTrue(range.overlaps(QgsIntRange(-1, 0)))
self.assertFalse(range.overlaps(QgsIntRange(-10, -1)))
self.assertFalse(range.overlaps(QgsIntRange(11, 12)))
self.assertFalse(range.overlaps(QgsIntRange(10, 12, True, False)))
self.assertFalse(QgsIntRange(10, 12, True, False).overlaps(range))

# includes right end
range = QgsIntRange(0, 10, False, True)
Expand All @@ -212,14 +214,16 @@ def testOverlaps(self):
self.assertFalse(range.overlaps(QgsIntRange(-1, 0)))
self.assertFalse(range.overlaps(QgsIntRange(-10, -1)))
self.assertFalse(range.overlaps(QgsIntRange(11, 12)))
self.assertFalse(range.overlaps(QgsIntRange(10, 12, False, True)))
self.assertFalse(QgsIntRange(10, 12, False, True).overlaps(range))

# includes neither end
range = QgsIntRange(0, 10, False, False)
self.assertTrue(range.overlaps(QgsIntRange(1, 9)))
self.assertTrue(range.overlaps(QgsIntRange(1, 10)))
self.assertTrue(range.overlaps(QgsIntRange(1, 11)))
self.assertTrue(range.overlaps(QgsIntRange(0, 9)))
self.assertTrue(range.overlaps(QgsIntRange(0, 10)))
self.assertFalse(range.overlaps(QgsIntRange(0, 10)))
self.assertTrue(range.overlaps(QgsIntRange(-1, 10)))
self.assertTrue(range.overlaps(QgsIntRange(-1, 9)))
self.assertTrue(range.overlaps(QgsIntRange(1, 11)))
Expand All @@ -228,6 +232,8 @@ def testOverlaps(self):
self.assertFalse(range.overlaps(QgsIntRange(-1, 0)))
self.assertFalse(range.overlaps(QgsIntRange(-10, -1)))
self.assertFalse(range.overlaps(QgsIntRange(11, 12)))
self.assertFalse(range.overlaps(QgsIntRange(10, 12, True, True)))
self.assertFalse(QgsIntRange(-3, 0, True, True).overlaps(range))


class TestQgsDoubleRange(unittest.TestCase):
Expand Down

0 comments on commit ad7596f

Please sign in to comment.