def test_is_colliding_with(self): a = Rectangle(10, 10, 10, 10) r_list = [ Rectangle(0, 0, 4, 4), Rectangle(12, 0, 4, 4), Rectangle(22, 0, 4, 4), Rectangle(0, 12, 4, 4), Rectangle(22, 12, 4, 4), Rectangle(0, 22, 4, 4), Rectangle(12, 22, 4, 4), Rectangle(22, 22, 4, 4), ] for b in r_list: self.assertFalse(a.is_colliding_with(b)) self.assertFalse(b.is_colliding_with(a)) r_list = [ Rectangle( 8, 8, 4, 4), Rectangle(12, 8, 4, 4), Rectangle(18, 8, 4, 4), Rectangle( 8, 12, 4, 4), Rectangle(12, 12, 4, 4), Rectangle(18, 12, 4, 4), Rectangle( 8, 18, 4, 4), Rectangle(12, 18, 4, 4), Rectangle(18, 18, 4, 4), ] for b in r_list: self.assertTrue(a.is_colliding_with(b)) self.assertTrue(b.is_colliding_with(a))
def test_join_two_rectangles_overlapped(self): joined = Rectangle(2 , 1 , 6 , 5) joined2 = Rectangle(1 , 2 , 8, 15) first_rect = Rectangle (2, 2, 5, 4) second_rect = Rectangle (3, 1, 5, 4) third_rect = Rectangle (1, 5 , 8 , 12) self.assertEquals(joined, first_rect.join(second_rect)) self.assertEquals(joined2, first_rect.join(third_rect))
def test_is_colliding_with2(self): a = Rectangle(10, 10, 10, 10) r_list = [ Rectangle(0, 8, 30, 4), Rectangle(0, 12, 30, 6), Rectangle(0, 18, 30, 6), Rectangle(8, 0, 4, 30), Rectangle(12, 0, 6, 30), Rectangle(18, 0, 6, 30), ] for b in r_list: self.assertTrue(a.is_colliding_with(b), repr(a) + " is not colliding with " + repr(b)) self.assertTrue(b.is_colliding_with(a), repr(b) + " is not colliding with " + repr(a))
def _extents(self, extents_func, line_width=False): """ Calculate the bounding box for a given drawing operation. if @line_width is True, the current line-width is taken into account. """ cr = self._cairo cr.save() cr.identity_matrix() x0, y0, x1, y1 = extents_func() b = Rectangle(x0, y0, x1=x1, y1=y1) cr.restore() if b and line_width: # Do this after the restore(), so we can get the proper width. lw = cr.get_line_width()/2 d = cr.user_to_device_distance(lw, lw) b.expand(d[0]+d[1]) self._update_bounds(b) return b
def test_contains_point(self): r = Rectangle(10, 10, 10, 10) self.assertEquals(True, r.contains_point((10, 10,))) self.assertEquals(True, r.contains_point((20, 10,))) self.assertEquals(True, r.contains_point((10, 20,))) self.assertEquals(True, r.contains_point((20, 20,))) self.assertEquals(True, r.contains_point((15, 15,))) self.assertEquals(False, r.contains_point((0, 0,))) self.assertEquals(False, r.contains_point((0, 15,))) self.assertEquals(False, r.contains_point((0, 30,))) self.assertEquals(False, r.contains_point((15, 0,))) self.assertEquals(False, r.contains_point((15, 30,))) self.assertEquals(False, r.contains_point((30, 0,))) self.assertEquals(False, r.contains_point((30, 15,))) self.assertEquals(False, r.contains_point((30, 30,)))
def test_contains2_should_return_false_if_the_figure_to_evaluate_is_a_rectangle_outside_of_the_first_rectangle(self): main_rect = Rectangle (0, 0 , 6 , 8) other_rect = Rectangle (1 , 3 , 8 , 3) self.assertFalse(main_rect.contains2(other_rect))
def test_is_inside(self): a = Rectangle(10, 10, 10, 10) self.assertTrue(a.is_inside(a)), self.assertTrue(a.is_overlaying(a)) b = Rectangle(12, 12, 6, 6) self.assertTrue(b.is_inside(a)), self.assertTrue(a.is_overlaying(b)) b = Rectangle(8, 8, 12, 12) self.assertTrue(a.is_inside(b)), self.assertTrue(b.is_overlaying(a)) r_list = [ Rectangle(0, 0, 4, 4), Rectangle(12, 0, 4, 4), Rectangle(22, 0, 4, 4), Rectangle(0, 12, 4, 4), Rectangle(22, 12, 4, 4), Rectangle(0, 22, 4, 4), Rectangle(12, 22, 4, 4), Rectangle(22, 22, 4, 4), ] for b in r_list: self.assertFalse(a.is_inside(b)) self.assertFalse(b.is_inside(a)) self.assertFalse(a.is_overlaying(b)) self.assertFalse(b.is_overlaying(a))
def test_contains2_should_return_False_if_point_entered_is_outside_of_the_rectangle(self): point = Point (0, -1) main_rect = Rectangle (0 , 0 , 4 , 5) self.assertFalse(main_rect.contains2(point))
def test_contains2_should_return_True_if_point_entered_is_inside_of_the_rectangle(self): point = Point (0, 4) main_rect = Rectangle (0 , 0 , 5 , 12) self.assertTrue(main_rect.contains2(point))
def test_permiter_should_return_a_positive_value(self): rect = Rectangle(0 , 0 , 4 , 5) self.assertEquals(18, rect.perimeter())
def test_contains2_should_return_true_if_the_figure_to_evaluate_is_a_rectangle_inside_the_first_rectangle(self): main_rect = Rectangle (0, 0 , 6 , 8) other_rect = Rectangle (1 , 3 , 2 , 3) self.assertTrue(main_rect.contains2(other_rect))
def test_rectangle_contains_a_point_with_same_coordinates_as_its_x_y_attributes(self): point = Point(0 , 0) rect = Rectangle (0 , 0 , 4 , 5) self.assertTrue(rect.contains(point))
def test_rectangle_area(): rect_s = Rectangle(7, -2, 3, 2, 0) assert rect_s.area() == 24
def reset(self): super(MaxRects, self).reset() self._max_rects = [Rectangle(0, 0, self.width, self.height)]
def test_rectangle_contains_a_circle_totally(self): rect = Rectangle(0 , 0 , 10 , 15) circ = Circle ( 2, 3 , 4) new_rectangle = Rectangle(circ.x, circ.y, 2 * circ.r, 2 * circ.r) self.assertTrue(rect.contains2(new_rectangle))
class ObjectDetector: def __init__(self): self.threshold = (25, 25, 25, 0) self.resolution = (640, 480) #todo: make some training method for these values, especially self.resolution[1] * 0.9 self.region_of_interest = Rectangle(0, 0, self.resolution[0], self.resolution[1] * 0.7) self.verbose = False #todo: same goes for these two points self.left_arm_point = (240, 300) self.right_arm_point = (410, 300) self.mininum_surface = 220 def __detect(self, img): '''Detect objects in the image. It will classify the left and right arms and filter out small 'objects'. Returns an instance of Observation.''' storage = cv.CreateMemStorage(0) contours = cv.FindContours(img, storage, cv.CV_RETR_LIST, cv.CV_CHAIN_APPROX_SIMPLE) observation = self.Observation() # for every group of lines while contours: area = abs(cv.ContourArea(contours)) rect = Rectangle.from_cv_rect(cv.BoundingRect(contours)) rect.contour = contours contours = contours.h_next() # Find the arms based on their expected position. If the found # rectangle contains the pixel, it might be the arm you're looking # for. #small stuff, which has almost no surface, is ignored if area < self.mininum_surface: continue #left arm elif rect.contains(self.left_arm_point): observation.left_arm = rect #right arm elif rect.contains(self.right_arm_point): observation.right_arm = rect #anything else that isn't eeny teeny tiny small, is an object. else: observation.objects.append(rect) return observation def __preprocess(self, img): # smoothing the image to remove noise that was present in (and created by) the # gradient, and the sum of the gradient and the original image smoothed = cv.CreateImage(self.resolution, img.depth, img.channels) cv.Smooth(img, smoothed, cv.CV_GAUSSIAN, 3, 3) if self.verbose: cv.ShowImage("img_objects", img) # Cut off the NAO itself, no need to detect it or next to it as # there is no possible way the object is floating next to the # table, or the table is in the same position as the Nao. If this # would be true, then Arnoud and Jelmer are not the ones who wrote # this software. cv.SetImageROI(img, self.region_of_interest.as_cv_rect()) cv.ShowImage("image",img) return img def detect(self, img): obj_img = self.__preprocess(img) return self.__detect(obj_img) class Observation: '''Structure to store the object observation''' def __init__(self): self.left_arm = None self.right_arm = None self.objects = [] def __repr__(self): return "<Observation [left_arm: %s] [right_arm: %s] [objects: %s]>" % (self.left_arm, self.right_arm, self.objects) def draw(self, img): '''Draw the observation into an image. Left and right arm are colored red and green. Objects are white''' if self.left_arm: self.__draw_rect(img, self.left_arm, (0, 0, 255, 0)) if self.right_arm: self.__draw_rect(img, self.right_arm, (0, 255, 0, 0)) for obj in self.objects: self.__draw_rect(img, obj, (255,255,255,0)) def __draw_rect(self, display, rect, colour): '''Little helper function that actually draws the rectangles''' cv.Rectangle(display, rect.top_left, rect.bottom_right, colour, 3, cv.CV_AA) cv.Circle(display, rect.center, 3, (255, 255, 0, 0), 2, cv.CV_AA) for i,p in enumerate(rect.contour): if i == 0: continue cv.Line(display, rect.contour[i - 1], p, colour, 1, cv.CV_AA)
def test_rectangle_perimeter(): rect_p = Rectangle(4, 20, 4, 6, 0) assert rect_p.perimeter() == 40
def test_negative_rectangle_area(): negative_rect_s = Rectangle(15, 17, -5, 4, 0) assert negative_rect_s.area() == 80
def test_contains_should_return_false_if_point_in_y_coordinates_is_outside_of_the_rectangle(self): point = Point (0, -1) rect = Rectangle (0 , 0 , 4 , 5) self.assertFalse(rect.contains(point))
def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.hovered = False # FIXME: this is a duplicate if inheriting from Widget -> fix design self._rectangle = Rectangle() self._dragging = False
def reset(self): super(Guillotine, self).reset() self._sections = [] self._add_section(Rectangle(0, 0, self.width, self.height))
def test_return_false_because_the_rectangle_doesnot_contains_a_circle(self): rect = Rectangle(0 , 0 , 10 , 15) circ = Circle ( 2, 8 , 4) new_rectangle = Rectangle(circ.x, circ.y, 2 * circ.r, 2 * circ.r) self.assertFalse(rect.contains2(new_rectangle))
def __init__(self, imageLocation, location): self.image = pygame.image.load(imageLocation) self.size = self.image.get_rect().size self.location = location self.rect = Rectangle(self.location.x, self.location.y, self.size[0], self.size[1])
def buildGeometry(self): C = self.corners self.obj = Rectangle(*C[:-1])