def test_rect(): with pytest.raises(ValueError): rect(pt(0, 0), pt(1, 1, dtype=np.int)) # dtypes do not match with pytest.raises(ValueError): rect(pt(0, 0), pt(0, 0)) # doesn't form rect with pytest.raises(ValueError): rect(pt(1, 1), pt(0, 0)) # doesn't form rect with pytest.raises(ValueError): rect(pt(0, 0), pt(1, 0)) # doesn't form rect a = pt(0, 0) b = pt(1, 1) r = rect(a, b) assert r.dtype == a.dtype == b.dtype assert np.array_equal(r[0], a) assert np.array_equal(r[1], b) a = pt(-3, -1) b = pt(8, 1.2) r = rect(a, b) assert r.dtype == a.dtype == b.dtype assert np.array_equal(r[0], a) assert np.array_equal(r[1], b)
def test_rectarea(): a = pt(0, 0) b = pt(1, 1) r = rect(a, b) assert rectarea(r) == 1 a = pt(-3, -1) b = pt(2, 5) r = rect(a, b) assert rectarea(r) == 30
def test_rectcenter(): a = pt(0, 0) b = pt(1, 1) r = rect(a, b) center = rectcenter(r) assert type(center) is np.ndarray assert np.array_equal(center, pt(0.5, 0.5)) a = pt(-3, -1) b = pt(2, 5) r = rect(a, b) assert np.array_equal(rectcenter(r), pt(-0.5, 2))
def make_grid_from_positions(colpos, rowpos): """ Create a page grid from list of column positions <colpos> and a list of row positions <rowpos>. Both positions lists must be sorted from low to high! The returned page grid is a list of rows. Each row in turn contains a "grid cell", i.e. a rect (see pdftabextract.geom). """ if len(colpos) == 0: raise ValueError("List of column positions is empty.") if len(rowpos) == 0: raise ValueError("List of row positions is empty.") row_spans = subsequent_pairs(rowpos) col_spans = subsequent_pairs(colpos) # create a grid with rectangles of table cells grid = [] for top, bottom in row_spans: row = [] for left, right in col_spans: cell = rect(pt(left, top), pt(right, bottom)) row.append(cell) grid.append(row) return grid
def test_rectintersect(): a = rect(pt(0, 0), pt(1, 1)) b = rect(pt(-3, -1), pt(2, 5)) assert rectintersect(a, a) == rectarea(a) assert rectintersect(b, b) == rectarea(b) assert rectintersect(a, a, norm_intersect_area='a') == 1 assert rectintersect(a, a, norm_intersect_area='b') == 1 with pytest.raises(ValueError): rectintersect(a, a, norm_intersect_area='c') # complete intersect assert rectintersect(a, b) == rectarea(a) assert rectintersect(b, a) == rectarea(a) assert rectintersect(a, b, norm_intersect_area='a') == 1 assert rectintersect(b, a, norm_intersect_area='b') == 1 assert rectintersect(b, a, norm_intersect_area='a') < 1 assert rectintersect(a, b, norm_intersect_area='b') < 1 # partial intersect a = rect(pt(0, 0), pt(1, 1)) b = rect(pt(0.5, 0.5), pt(1.5, 1.5)) assert rectintersect(a, b) == 0.25 assert rectintersect(a, b, norm_intersect_area='a') == 0.25 assert rectintersect(a, b, norm_intersect_area='b') == 0.25 b = rect(pt(0.75, 0.5), pt(1.5, 1.5)) assert rectintersect(a, b) == 0.125 # touch a = rect(pt(0, 0), pt(1, 1)) b = rect(pt(1, 1), pt(1.5, 1.5)) assert rectintersect(a, b) == 0 # no intersection a = rect(pt(0, 0), pt(1, 1)) b = rect(pt(1.1, 1.1), pt(1.5, 1.5)) assert rectintersect(a, b) is None