From a7c3362e9347affebf92dabcb96980e3bf6a2640 Mon Sep 17 00:00:00 2001 From: Théo de la Hogue Date: Wed, 29 May 2024 14:20:14 +0200 Subject: Improving AOI polygon management. Adding is_valid property. --- src/argaze/AreaOfInterest/AOIFeatures.py | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/src/argaze/AreaOfInterest/AOIFeatures.py b/src/argaze/AreaOfInterest/AOIFeatures.py index c2714ca..25046ff 100644 --- a/src/argaze/AreaOfInterest/AOIFeatures.py +++ b/src/argaze/AreaOfInterest/AOIFeatures.py @@ -40,6 +40,10 @@ class AreaOfInterest(numpy.ndarray): return numpy.array(points).view(AreaOfInterest) + def __init__(self, points: numpy.array = numpy.empty(0)): + + self.__polygon = Polygon(points) + def __repr__(self): """String representation""" @@ -81,8 +85,7 @@ class AreaOfInterest(numpy.ndarray): # TODO: Use pygeos N = 32 - points = [(math.cos(2 * math.pi / N * x) * radius + cx, math.sin(2 * math.pi / N * x) * radius + cy) for x - in range(0, N + 1)] + points = [(math.cos(2 * math.pi / N * x) * radius + cx, math.sin(2 * math.pi / N * x) * radius + cy) for x in range(0, N + 1)] return AreaOfInterest(points) @@ -95,8 +98,7 @@ class AreaOfInterest(numpy.ndarray): # TODO: Use pygeos N = 32 - points = [(math.cos(2 * math.pi / N * x) * rx + cx, math.sin(2 * math.pi / N * x) * ry + cy) for x in - range(0, N + 1)] + points = [(math.cos(2 * math.pi / N * x) * rx + cx, math.sin(2 * math.pi / N * x) * ry + cy) for x in range(0, N + 1)] @property def dimension(self) -> int: @@ -135,7 +137,7 @@ class AreaOfInterest(numpy.ndarray): @property def area(self) -> float: """Area of the polygon defined by aoi's points.""" - return Polygon(self).area + return self.__polygon.area @property def bounding_box(self) -> numpy.array: @@ -151,6 +153,12 @@ class AreaOfInterest(numpy.ndarray): return numpy.array([(min_x, min_y), (max_x, min_y), (max_x, max_y), (min_x, max_y)]) + @property + def is_valid(self) -> bool: + """Is area polygon valid?""" + + return self.__polygon.is_valid + def clockwise(self) -> Self: """Get area points in clockwise order. !!! warning @@ -232,9 +240,7 @@ class AreaOfInterest(numpy.ndarray): assert (self.dimension == 2) - self_polygon = Polygon(self) - - if not self_polygon.is_valid: + if not self.__polygon.is_valid: logging.warning('AreaOfInterest.circle_intersection: AOI polygon is not valid.') @@ -244,14 +250,13 @@ class AreaOfInterest(numpy.ndarray): args_circle = Point(center).buffer(radius) - if self_polygon.intersects(args_circle): + if self.__polygon.intersects(args_circle): - intersection = self_polygon.intersection(args_circle) + intersection = self.__polygon.intersection(args_circle) - intersection_array = numpy.array([list(xy) for xy in intersection.exterior.coords[:]]).astype( - numpy.float32).view(AreaOfInterest) + intersection_array = numpy.array([list(xy) for xy in intersection.exterior.coords[:]]).astype(numpy.float32).view(AreaOfInterest) - return intersection_array, intersection.area / self_polygon.area, intersection.area / args_circle.area + return intersection_array, intersection.area / self.__polygon.area, intersection.area / args_circle.area else: -- cgit v1.1