def select_polygon(self, polygon, add): """ Select by a polygon which has to contain whole pixels. """ if self.data and self.lsx and self.lsy: polygon = [(p.x(), p.y()) for p in polygon] # a polygon should contain all pixel shiftx = _shift(self.lsx) shifty = _shift(self.lsy) points_edges = [self.data_points + [[shiftx, shifty]], self.data_points + [[-shiftx, shifty]], self.data_points + [[shiftx, -shifty]], self.data_points + [[-shiftx, -shifty]]] inp = in_polygon(points_edges[0], polygon) for p in points_edges[1:]: inp *= in_polygon(p, polygon) self.make_selection(inp, add)
def test_point(self): poly = [(0, 1), (1, 0), (2, 1), (3, 0), (3, 2), (0, 1)] # non-convex self.assertFalse(in_polygon([0, 0], poly)) self.assertTrue(in_polygon([1, 1.1], poly)) self.assertTrue(in_polygon([1, 1], poly)) self.assertTrue(in_polygon([1, 0.5], poly)) self.assertFalse(in_polygon([2, 0], poly)) self.assertFalse(in_polygon([0, 2], poly)) # multiple points at once np.testing.assert_equal([False, True], in_polygon([[0, 0], [1, 1]], poly))
def test_order(self): poly = [(0, 0), (1, 0), (1, 1), (0, 1), (0, 0)] # square self.assertTrue(in_polygon([0.5, 0.5], poly)) self.assertTrue(in_polygon([0.5, 0.5], list(reversed(poly))))