MCPcopy Create free account
hub / github.com/Layout-Parser/layout-parser / test_shape_operations

Function test_shape_operations

tests/test_elements.py:331–403  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

329
330
331def test_shape_operations():
332 i_1 = Interval(1, 2, axis="y", canvas_height=30, canvas_width=400)
333 i_2 = TextBlock(Interval(1, 2, axis="x"))
334 i_3 = Interval(1, 2, axis="y")
335
336 r_1 = Rectangle(0.5, 0.5, 2.5, 1.5)
337 r_2 = TextBlock(Rectangle(0.5, 0.5, 2, 2.5))
338
339 q_1 = Quadrilateral([[1, 1], [2.5, 1.2], [2.5, 3], [1.5, 3]])
340 q_2 = TextBlock(Quadrilateral([[0.5, 0.5], [2, 1], [1.5, 2.5], [0.5, 2]]))
341
342 # I and I in different axes
343 assert i_1.intersect(i_1) == i_1
344 assert i_1.intersect(i_2) == Rectangle(1, 1, 2, 2)
345 assert i_1.intersect(i_3) == i_1 # Ensure intersect copy the canvas size
346
347 assert i_1.union(i_1) == i_1
348 with pytest.raises(InvalidShapeError):
349 assert i_1.union(i_2) == Rectangle(1, 1, 2, 2)
350
351 # I and R in different axes
352 assert i_1.intersect(r_1) == Rectangle(0.5, 1, 2.5, 1.5)
353 assert i_2.intersect(r_1).block == Rectangle(1, 0.5, 2, 1.5)
354 assert i_1.union(r_1) == Rectangle(0.5, 0.5, 2.5, 2)
355 assert i_2.union(r_1).block == r_1
356
357 # I and Q in strict mode
358 with pytest.raises(NotSupportedShapeError):
359 i_1.intersect(q_1)
360 i_1.union(q_1)
361
362 # I and Q in different axes
363 assert i_1.intersect(q_1, strict=False) == Rectangle(1, 1, 2.5, 2)
364 assert i_1.union(q_1, strict=False) == Rectangle(1, 1, 2.5, 3)
365 assert i_2.intersect(q_1, strict=False).block == Rectangle(1, 1, 2, 3)
366 assert i_2.union(q_1, strict=False).block == Rectangle(1, 1, 2.5, 3)
367
368 # R and I
369 assert r_1.intersect(i_1) == i_1.intersect(r_1)
370
371 # R and R
372 assert r_1.intersect(r_2) == r_2.intersect(r_1).block == Rectangle(0.5, 0.5, 2, 1.5)
373 assert r_1.union(r_2) == r_2.union(r_1).block == Rectangle(0.5, 0.5, 2.5, 2.5)
374
375 # R and Q
376 with pytest.raises(NotSupportedShapeError):
377 r_1.intersect(q_1)
378 r_1.union(q_1)
379
380 assert r_1.intersect(q_1, strict=False) == Rectangle(1, 1, 2.5, 1.5)
381 assert r_1.union(q_1, strict=False) == Rectangle(0.5, 0.5, 2.5, 3)
382 assert r_1.intersect(q_2, strict=False) == r_1.intersect(q_2.to_rectangle())
383 assert r_1.union(q_2, strict=False) == r_1.union(q_2.to_rectangle())
384
385 # Q and others in strict mode
386 with pytest.raises(NotSupportedShapeError):
387 q_1.intersect(i_1)
388 q_1.intersect(r_1)

Callers

nothing calls this directly

Calls 13

intersectMethod · 0.95
unionMethod · 0.95
intersectMethod · 0.95
unionMethod · 0.95
intersectMethod · 0.95
unionMethod · 0.95
to_rectangleMethod · 0.95
intersectMethod · 0.95
unionMethod · 0.95
IntervalClass · 0.90
TextBlockClass · 0.90
RectangleClass · 0.90

Tested by

no test coverage detected