| 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 | |