Data structure to represent an area (x,y,w,h) with a radius (r).
Mainly used to represent a rectangle in an image with a vagueness around the midpoint, which is a rectangle. The radius is half of the vagueness.
Data structure to represent an area (x,y,w,h) with a radius (r).
Mainly used to represent a rectangle in an image with a vagueness around the midpoint, which is a rectangle. The radius is half of the vagueness.
Create a sppasFuzzyRect instance.
The given coordinates of the midpoint can be a tuple of int values or a string representing it.
def __init__(self, coord, radius=None):
"""Create a sppasFuzzyRect instance.
The given coordinates of the midpoint can be a tuple of int values
or a string representing it.
:param coord: (int,int,int,int) x,y,w,h coords of the midpoint value.
:param radius: (int) the radius around the midpoint.
"""
super(sppasFuzzyRect, self).__init__()
self.__x = 0
self.__y = 0
self.__w = 0
self.__h = 0
self.__radius = None
self.set_midpoint(coord)
self.set_radius(radius)
Set self members from another sppasFuzzyRect instance.
def set(self, other):
"""Set self members from another sppasFuzzyRect instance.
:param other: (sppasFuzzyRect)
"""
if isinstance(other, sppasFuzzyRect) is False:
raise AnnDataTypeError(other, 'sppasFuzzyRect')
self.__radius = None
self.set_midpoint(other.get_midpoint())
self.set_radius(other.get_radius())
Return a deep copy of self.
def copy(self):
"""Return a deep copy of self."""
return sppasFuzzyRect((self.__x, self.__y, self.__w, self.__h), self.__radius)
Return the midpoint coords (x,y,w,h).
def get_midpoint(self):
"""Return the midpoint coords (x,y,w,h)."""
return (self.__x, self.__y, self.__w, self.__h)
Return a tuple (x,y,w,h) or (x,y,w,h,r).
@staticmethod
def parse(str_rect):
"""Return a tuple (x,y,w,h) or (x,y,w,h,r).
:param str_rect: (str) A string representing a fuzzy rect.
"""
tc = str_rect.lower()[1:-1]
tab = tc.split(',')
x = int(tab[0])
y = int(tab[1])
w = int(tab[2])
h = int(tab[3])
if len(tab) == 5:
return (x, y, w, h, int(tab[4]))
return (x, y, w, h, None)
Set the midpoint value.
AnnDataTypeError
def set_midpoint(self, midpoint):
"""Set the midpoint value.
:param midpoint: (tuple(int,int,int,int), str) the new midpoint coords.
:raise: AnnDataTypeError
"""
if isinstance(midpoint, str) is True:
x, y, w, h, r = sppasFuzzyRect.parse(midpoint)
midpoint = (x, y, w, h)
self.__check_coords(midpoint)
self.__x = int(midpoint[0])
self.__y = int(midpoint[1])
self.__w = int(midpoint[2])
self.__h = int(midpoint[3])
Return the radius value (float or None).
def get_radius(self):
"""Return the radius value (float or None)."""
return self.__radius
Fix the radius value, ie. the vagueness of the point.
The midpoint value must be set first.
AnnDataTypeError, AnnDataNegValueError
def set_radius(self, radius=None):
"""Fix the radius value, ie. the vagueness of the point.
The midpoint value must be set first.
:param radius: (int, str, None) the radius value
:raise: AnnDataTypeError, AnnDataNegValueError
"""
if radius is not None:
if isinstance(radius, str) is True:
try:
radius = int(radius)
except ValueError:
raise AnnDataTypeError(radius, 'int')
if isinstance(radius, int) is False:
raise AnnDataTypeError(radius, 'int')
self.__radius = radius
Check if the given point is inside the vagueness of self.
def contains(self, coord):
"""Check if the given point is inside the vagueness of self.
:param coord: (tuple) An (x, y) coordinates
"""
p = sppasFuzzyPoint(coord)
cx, cy = p.get_midpoint()
r = self.__radius
if self.__radius is None:
r = 0
if cx < self.__x - r:
return False
if cx > self.__x + self.__w + r:
return False
if cy < self.__y - r:
return False
if cy > self.__y + self.__h + r:
return False
return True
def __check_coords(self, midpoint):
if isinstance(midpoint, tuple) is False:
raise AnnDataTypeError(midpoint, 'tuple')
if len(midpoint) != 4:
raise AnnDataTypeError(midpoint, 'tuple(int, int, int, int)')
for i in range(4):
if isinstance(midpoint[i], (int, float)) is False:
raise AnnDataTypeError(midpoint, 'tuple(int, int, int, int)')
if midpoint[i] < 0:
raise AnnDataNegValueError(midpoint[i])
def __format__(self, fmt):
return str(self).__format__(fmt)
def __repr__(self):
if self.__radius is None:
return 'sppasFuzzyRect: ({:d},{:d},{:d},{:d})'.format(self.__x, self.__y, self.__w, self.__h)
return 'sppasFuzzyRect: ({:d},{:d},{:d},{:d},{:d})) '.format(self.__x, self.__y, self.__w, self.__h, self.__radius)
def __str__(self):
if self.__radius is None:
return '({:d},{:d},{:d},{:d})'.format(self.__x, self.__y, self.__w, self.__h)
return '({:d},{:d},{:d},{:d},{:d})'.format(self.__x, self.__y, self.__w, self.__h, self.__radius)
Test equality of self with other.
Two fuzzy points are equals if the midpoint of one of them is in the area of the other.
def __eq__(self, other):
"""Test equality of self with other.
Two fuzzy points are equals if the midpoint of one of them is in the
area of the other.
"""
rect = None
if isinstance(other, str) is True:
rect = sppasFuzzyRect.parse(other)
elif isinstance(other, tuple) is True:
if len(other) in (4, 5):
rect = sppasFuzzyRect((other[0], other[1], other[2], other[3]))
if len(other) == 5:
rect.set_radius(other[4])
elif isinstance(other, sppasFuzzyRect) is True:
rect = other
if rect is None:
raise AnnDataTypeError('sppasFuzzyRect', 'tuple(int,int,int,int)')
other_x, other_y, other_w, other_h = rect.get_midpoint()
other_r = rect.get_radius()
if other_r is None:
other_r = 0
if self.contains((other_x - other_r, other_y - other_r)) is True:
return True
if self.contains((other_x - other_r, other_y + other_h + other_r)) is True:
return True
if self.contains((other_x + other_w + other_r, other_y - other_r)) is True:
return True
if self.contains((other_x + other_w + other_r, other_y + other_h + other_r)) is True:
return True
return False