From 6eb0ffae57ddb8e37ad1dc398eecc1ce85ef729e Mon Sep 17 00:00:00 2001 From: dajiaohuang Date: Sat, 24 Oct 2026 04:58:43 +0800 Subject: [PATCH] fix: classify one-sided and degenerate rectangles correctly Fixes #424 --- python/opengen/constraints/rectangle.py | 17 +++++++++-------- python/test/test_constraints.py | 13 +++++++++++++ 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/python/opengen/constraints/rectangle.py b/python/opengen/constraints/rectangle.py index f77e92b8..da0e4df7 100644 --- a/python/opengen/constraints/rectangle.py +++ b/python/opengen/constraints/rectangle.py @@ -193,11 +193,12 @@ def is_orthant(self): :rtype: boolean """ - chk_orthant = True - for x_min_i in self.__xmin: - chk_orthant &= (x_min_i == 0 or x_min_i == float( - 'inf') or x_min_i == float('-inf')) - for x_max_i in self.__xmax: - chk_orthant &= (x_max_i == 0 or x_max_i == float( - 'inf') or x_max_i == float('-inf')) - return chk_orthant + xmin = ([float('-inf')] * self.dimension() + if self.__xmin is None else self.__xmin) + xmax = ([float('inf')] * self.dimension() + if self.__xmax is None else self.__xmax) + return all( + (lower == 0 and upper == float('inf')) or + (lower == float('-inf') and upper == 0) + for lower, upper in zip(xmin, xmax) + ) diff --git a/python/test/test_constraints.py b/python/test/test_constraints.py index 2f58acc1..16436791 100644 --- a/python/test/test_constraints.py +++ b/python/test/test_constraints.py @@ -209,6 +209,19 @@ def test_rectangle_is_orthant(self): rect = og.constraints.Rectangle([-1.0, float('-inf')], [10.0, 3.0]) self.assertFalse(rect.is_orthant()) + def test_rectangle_is_orthant_one_sided_and_degenerate(self): + cases = [ + (None, [0], True), + ([0], None, True), + ([0], [0], False), + ([float('-inf')], [float('inf')], False), + ([-1], [1], False), + ] + for xmin, xmax, expected in cases: + with self.subTest(xmin=xmin, xmax=xmax): + self.assertEqual( + og.constraints.Rectangle(xmin, xmax).is_orthant(), expected) + def test_rectangle_dimension(self): rec_only_xmin = og.constraints.Rectangle(xmin=[1]) rec_only_xmax = og.constraints.Rectangle(xmax=[5, 6])