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

Method union

src/layoutparser/elements/layout_elements.py:340–375  ·  view source on GitHub ↗
(self, other: BaseCoordElement, strict: bool = True)

Source from the content-addressed store, hash-verified

338
339 @support_textblock
340 def union(self, other: BaseCoordElement, strict: bool = True):
341 """"""
342 if isinstance(other, Interval):
343 if self.axis != other.axis:
344 raise InvalidShapeError(
345 f"Unioning two intervals of different axes is not allowed."
346 )
347 else:
348 return self.__class__(
349 min(self.start, other.start),
350 max(self.end, other.end),
351 self.axis,
352 self.canvas_height,
353 self.canvas_width,
354 )
355
356 elif isinstance(other, Rectangle):
357 x_1, y_1, x_2, y_2 = other.coordinates
358 if self.axis == "x":
359 return Rectangle(min(x_1, self.start), y_1, max(x_2, self.end), y_2)
360 elif self.axis == "y":
361 return Rectangle(x_1, min(y_1, self.start), x_2, max(y_2, self.end))
362
363 elif isinstance(other, Quadrilateral):
364 if strict:
365 raise NotSupportedShapeError(
366 "The intersection between an Interval and a Quadrilateral might generate Polygon shapes that are not supported in the current version of layoutparser. You can pass `strict=False` in the input that converts the Quadrilateral to Rectangle to avoid this Exception."
367 )
368 else:
369 warnings.warn(
370 f"With `strict=False`, the other of shape {other.__class__} will be converted to {Rectangle} for obtaining the intersection"
371 )
372 return self.union(other.to_rectangle())
373
374 else:
375 raise Exception(f"Invalid input type {other.__class__} for other")
376
377 def pad(self, left=0, right=0, top=0, bottom=0, safe_mode=True):
378

Callers 3

test_shape_operationsFunction · 0.95
unionMethod · 0.45
unionMethod · 0.45

Calls 4

InvalidShapeErrorClass · 0.85
RectangleClass · 0.85
to_rectangleMethod · 0.45

Tested by 1

test_shape_operationsFunction · 0.76