461bf6fd40
CI / lint (3.11) (push) Has been cancelled
CI / lint (3.12) (push) Has been cancelled
CI / lint (3.13) (push) Has been cancelled
CI / shellcheck (push) Has been cancelled
CI / shfmt (push) Has been cancelled
CI / setup (3.11) (push) Has been cancelled
CI / setup (3.12) (push) Has been cancelled
CI / setup (3.13) (push) Has been cancelled
CI / check-licenses (3.12) (push) Has been cancelled
CI / test_unit (3.11) (push) Has been cancelled
CI / test_unit (3.12) (push) Has been cancelled
CI / test_unit (3.13) (push) Has been cancelled
CI / test_unit_no_extras (3.11) (push) Has been cancelled
CI / test_unit_no_extras (3.12) (push) Has been cancelled
CI / test_json_to_html (3.12) (push) Has been cancelled
CI / test_unit_no_extras (3.13) (push) Has been cancelled
CI / test_unit_dependency_extras (csv, 3.12, --extra csv) (push) Has been cancelled
CI / test_unit_dependency_extras (xlsx, 3.11, --extra xlsx) (push) Has been cancelled
CI / test_unit_dependency_extras (xlsx, 3.12, --extra xlsx) (push) Has been cancelled
CI / test_unit_dependency_extras (csv, 3.11, --extra csv) (push) Has been cancelled
CI / test_unit_dependency_extras (csv, 3.13, --extra csv) (push) Has been cancelled
CI / test_unit_dependency_extras (docx, 3.11, --extra docx) (push) Has been cancelled
CI / test_unit_dependency_extras (docx, 3.12, --extra docx) (push) Has been cancelled
CI / test_unit_dependency_extras (docx, 3.13, --extra docx) (push) Has been cancelled
CI / test_unit_dependency_extras (markdown, 3.11, --extra md) (push) Has been cancelled
CI / test_unit_dependency_extras (markdown, 3.12, --extra md) (push) Has been cancelled
CI / test_unit_dependency_extras (markdown, 3.13, --extra md) (push) Has been cancelled
CI / test_unit_dependency_extras (odt, 3.11, --extra odt) (push) Has been cancelled
CI / test_unit_dependency_extras (odt, 3.12, --extra odt) (push) Has been cancelled
CI / test_unit_dependency_extras (odt, 3.13, --extra odt) (push) Has been cancelled
CI / test_unit_dependency_extras (pdf-image, 3.11, --extra pdf --extra image --extra paddleocr) (push) Has been cancelled
CI / test_unit_dependency_extras (pdf-image, 3.12, --extra pdf --extra image --extra paddleocr) (push) Has been cancelled
CI / test_unit_dependency_extras (pdf-image, 3.13, --extra pdf --extra image --extra paddleocr) (push) Has been cancelled
CI / test_unit_dependency_extras (pptx, 3.11, --extra pptx) (push) Has been cancelled
CI / test_unit_dependency_extras (pptx, 3.12, --extra pptx) (push) Has been cancelled
CI / test_unit_dependency_extras (pptx, 3.13, --extra pptx) (push) Has been cancelled
CI / test_unit_dependency_extras (pypandoc, 3.11, --extra epub --extra org --extra rtf --extra rst) (push) Has been cancelled
CI / test_unit_dependency_extras (pypandoc, 3.12, --extra epub --extra org --extra rtf --extra rst) (push) Has been cancelled
CI / test_unit_dependency_extras (pypandoc, 3.13, --extra epub --extra org --extra rtf --extra rst) (push) Has been cancelled
Build And Push Docker Image / set-short-sha (push) Has been cancelled
Partition Benchmark / setup (push) Has been cancelled
Partition Benchmark / Measure and compare partition() runtime (push) Has been cancelled
CI / test_unit_dependency_extras (xlsx, 3.13, --extra xlsx) (push) Has been cancelled
CI / test_ingest_src (3.12) (push) Has been cancelled
CI / test_json_to_markdown (3.12) (push) Has been cancelled
CI / changelog (push) Has been cancelled
CI / test_dockerfile (push) Has been cancelled
CodeQL / Analyze (python) (push) Has been cancelled
Build And Push Docker Image / build-images (linux/amd64, opensource-linux-8core) (push) Has been cancelled
Build And Push Docker Image / build-images (linux/arm64, ubuntu-24.04-arm) (push) Has been cancelled
Build And Push Docker Image / publish-images (push) Has been cancelled
114 lines
3.8 KiB
Python
114 lines
3.8 KiB
Python
from __future__ import annotations
|
|
|
|
from enum import Enum
|
|
from typing import Any, Dict, Sequence, Tuple, Union
|
|
|
|
|
|
class Orientation(Enum):
|
|
SCREEN = (1, -1) # Origin in top left, y increases in the down direction
|
|
CARTESIAN = (1, 1) # Origin in bottom left, y increases in upward direction
|
|
|
|
|
|
def convert_coordinate(old_t, old_t_max, new_t_max, t_orientation):
|
|
"""Convert a coordinate into another system along an axis using a linear transformation"""
|
|
return (
|
|
(1 - old_t / old_t_max) * (1 - t_orientation) / 2
|
|
+ old_t / old_t_max * (1 + t_orientation) / 2
|
|
) * new_t_max
|
|
|
|
|
|
class CoordinateSystem:
|
|
"""A finite coordinate plane with given width and height."""
|
|
|
|
orientation: Orientation
|
|
|
|
def __init__(self, width: Union[int, float], height: Union[int, float]):
|
|
self.width = width
|
|
self.height = height
|
|
|
|
def __eq__(self, other: object):
|
|
if not isinstance(other, CoordinateSystem):
|
|
return False
|
|
return (
|
|
str(self.__class__.__name__) == str(other.__class__.__name__)
|
|
and self.width == other.width
|
|
and self.height == other.height
|
|
and self.orientation == other.orientation
|
|
)
|
|
|
|
def convert_from_relative(
|
|
self,
|
|
x: Union[float, int],
|
|
y: Union[float, int],
|
|
) -> Tuple[Union[float, int], Union[float, int]]:
|
|
"""Convert to this coordinate system from a relative coordinate system."""
|
|
x_orientation, y_orientation = self.orientation.value
|
|
new_x = convert_coordinate(x, 1, self.width, x_orientation)
|
|
new_y = convert_coordinate(y, 1, self.height, y_orientation)
|
|
return new_x, new_y
|
|
|
|
def convert_to_relative(
|
|
self,
|
|
x: Union[float, int],
|
|
y: Union[float, int],
|
|
) -> Tuple[Union[float, int], Union[float, int]]:
|
|
"""Convert from this coordinate system to a relative coordinate system."""
|
|
x_orientation, y_orientation = self.orientation.value
|
|
new_x = convert_coordinate(x, self.width, 1, x_orientation)
|
|
new_y = convert_coordinate(y, self.height, 1, y_orientation)
|
|
return new_x, new_y
|
|
|
|
def convert_coordinates_to_new_system(
|
|
self,
|
|
new_system: CoordinateSystem,
|
|
x: Union[float, int],
|
|
y: Union[float, int],
|
|
) -> Tuple[Union[float, int], Union[float, int]]:
|
|
"""Convert from this coordinate system to another given coordinate system."""
|
|
rel_x, rel_y = self.convert_to_relative(x, y)
|
|
return new_system.convert_from_relative(rel_x, rel_y)
|
|
|
|
def convert_multiple_coordinates_to_new_system(
|
|
self,
|
|
new_system: CoordinateSystem,
|
|
coordinates: Sequence[Tuple[Union[float, int], Union[float, int]]],
|
|
) -> Tuple[Tuple[Union[float, int], Union[float, int]], ...]:
|
|
"""Convert (x, y) coordinates from current system to another coordinate system."""
|
|
new_system_coordinates = []
|
|
for x, y in coordinates:
|
|
new_system_coordinates.append(
|
|
self.convert_coordinates_to_new_system(new_system=new_system, x=x, y=y),
|
|
)
|
|
return tuple(new_system_coordinates)
|
|
|
|
|
|
class RelativeCoordinateSystem(CoordinateSystem):
|
|
"""Relative coordinate system where x and y are on a scale from 0 to 1."""
|
|
|
|
orientation = Orientation.CARTESIAN
|
|
|
|
def __init__(self):
|
|
self.width = 1
|
|
self.height = 1
|
|
|
|
|
|
class PixelSpace(CoordinateSystem):
|
|
"""Coordinate system representing a pixel space, such as an image. The origin is at the top
|
|
left."""
|
|
|
|
orientation = Orientation.SCREEN
|
|
|
|
|
|
class PointSpace(CoordinateSystem):
|
|
"""Coordinate system representing a point space, such as a pdf. The origin is at the bottom
|
|
left."""
|
|
|
|
orientation = Orientation.CARTESIAN
|
|
|
|
|
|
TYPE_TO_COORDINATE_SYSTEM_MAP: Dict[str, Any] = {
|
|
"PixelSpace": PixelSpace,
|
|
"PointSpace": PointSpace,
|
|
"CoordinateSystem": CoordinateSystem,
|
|
}
|