diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/argaze.test/AreaOfInterest/AOIFeatures.py | 14 | ||||
-rw-r--r-- | src/argaze/AreaOfInterest/AOIFeatures.py | 14 |
2 files changed, 22 insertions, 6 deletions
diff --git a/src/argaze.test/AreaOfInterest/AOIFeatures.py b/src/argaze.test/AreaOfInterest/AOIFeatures.py index c5e4ff4..f894eb8 100644 --- a/src/argaze.test/AreaOfInterest/AOIFeatures.py +++ b/src/argaze.test/AreaOfInterest/AOIFeatures.py @@ -13,14 +13,15 @@ class TestAreaOfInterestClass(unittest.TestCase): def test_new(self): """Test AreaOfInterest creation.""" - # Check that 0D AreaOfInterest creation fails - with self.assertRaises(TypeError): + # Check that 0D AreaOfInterest creation is considered as empty + aoi_0D = AOIFeatures.AreaOfInterest() - aoi_0D = AOIFeatures.AreaOfInterest() + self.assertTrue(aoi_0D.empty) # Check 1 point 1D AreaOfInterest creation aoi_1D = AOIFeatures.AreaOfInterest([[0]]) + self.assertFalse(aoi_1D.empty) self.assertEqual(aoi_1D.dimension, 1) self.assertEqual(aoi_1D.size, 1) self.assertEqual(aoi_1D.center, [0]) @@ -31,6 +32,7 @@ class TestAreaOfInterestClass(unittest.TestCase): # Check 2 points 1D AreaOfInterest creation aoi_1D = AOIFeatures.AreaOfInterest([[0], [1]]) + self.assertFalse(aoi_1D.empty) self.assertEqual(aoi_1D.dimension, 1) self.assertEqual(aoi_1D.size, 2) self.assertEqual(aoi_1D.center, [0.5]) @@ -41,6 +43,7 @@ class TestAreaOfInterestClass(unittest.TestCase): # Check 4 points 2D AreaOfInterest creation aoi_2D = AOIFeatures.AreaOfInterest([[0, 0], [0, 1], [1, 0], [1, 1]]) + self.assertFalse(aoi_2D.empty) self.assertEqual(aoi_2D.dimension, 2) self.assertEqual(aoi_2D.size, 4) self.assertIsNone(numpy.testing.assert_array_equal(aoi_2D.center, [0.5, 0.5])) @@ -49,6 +52,7 @@ class TestAreaOfInterestClass(unittest.TestCase): # Check 8 points 3D AreaOfInterest creation aoi_3D = AOIFeatures.AreaOfInterest([[0, 0, 0], [0, 1, 0], [1, 0, 0], [1, 1, 0], [0, 0, 1], [0, 1, 1], [1, 0, 1], [1, 1, 1]]) + self.assertFalse(aoi_3D.empty) self.assertEqual(aoi_3D.dimension, 3) self.assertEqual(aoi_3D.size, 8) @@ -82,8 +86,8 @@ class TestAreaOfInterestClass(unittest.TestCase): aoi_2D_clockwise = AOIFeatures.AreaOfInterest([[0, 0], [0, 1], [1, 0], [1, 1]]).clockwise() - assertEqual(type(aoi_2D_clockwise), AOIFeatures.AreaOfInterest) - self.assertIsNone(numpy.testing.assert_array_equal(aoi_2D_clockwise, [0, 0], [0, 1], [1, 1], [1, 0])) + self.assertEqual(type(aoi_2D_clockwise), AOIFeatures.AreaOfInterest) + self.assertIsNone(numpy.testing.assert_array_equal(aoi_2D_clockwise, [[0, 0], [1, 0], [1, 1], [0, 1]])) def test_contains_point(self): """Test AreaOfInterest contains_point method.""" diff --git a/src/argaze/AreaOfInterest/AOIFeatures.py b/src/argaze/AreaOfInterest/AOIFeatures.py index dd24acc..3dcccfd 100644 --- a/src/argaze/AreaOfInterest/AOIFeatures.py +++ b/src/argaze/AreaOfInterest/AOIFeatures.py @@ -17,7 +17,7 @@ AreaOfInterestType = TypeVar('AreaOfInterest', bound="AreaOfInterest") class AreaOfInterest(numpy.ndarray): """Define Area Of Interest as an array of points of any dimension.""" - def __new__(cls, points: numpy.ndarray) -> AreaOfInterestType: + def __new__(cls, points: numpy.ndarray = numpy.empty(0)) -> AreaOfInterestType: """View casting inheritance.""" return numpy.array(points).view(AreaOfInterest) @@ -45,12 +45,24 @@ class AreaOfInterest(numpy.ndarray): return self.shape[0] @property + def empty(self) -> bool: + """Is AOI empty ?""" + + return self.shape[0] == 0 + + @property def center(self) -> numpy.array: """Center of mass.""" return self.mean(axis=0) @property + def area(self) -> float: + """Area of the polygon defined by aoi's points.""" + + return Polygon(self).area + + @property def bounding_box(self) -> numpy.array: """Get area's bounding box. .. warning:: |