From d305ce7a929f53cb20685dbb64a1b5e38b232ab3 Mon Sep 17 00:00:00 2001 From: Brian Pugh Date: Sun, 15 Dec 2024 21:12:44 -0500 Subject: [PATCH] Add more interpolation modes. --- py360convert/c2e.py | 8 ++------ py360convert/e2c.py | 8 ++------ py360convert/e2p.py | 8 ++------ py360convert/utils.py | 48 ++++++++++++++++++++++++++++++++++++++++++- 4 files changed, 53 insertions(+), 19 deletions(-) diff --git a/py360convert/c2e.py b/py360convert/c2e.py index 2eccfca..7c1ab16 100644 --- a/py360convert/c2e.py +++ b/py360convert/c2e.py @@ -12,6 +12,7 @@ cube_list2h, equirect_facetype, equirect_uvgrid, + mode_to_order, sample_cubefaces, ) @@ -72,12 +73,7 @@ def c2e( np.ndarray Equirectangular image. """ - if mode == "bilinear": - order = 1 - elif mode == "nearest": - order = 0 - else: - raise ValueError(f'Unknown mode "{mode}".') + order = mode_to_order(mode) if cube_format == "horizon": if not isinstance(cubemap, np.ndarray): diff --git a/py360convert/e2c.py b/py360convert/e2c.py index 1473753..d291b39 100644 --- a/py360convert/e2c.py +++ b/py360convert/e2c.py @@ -10,6 +10,7 @@ cube_h2dice, cube_h2dict, cube_h2list, + mode_to_order, sample_equirec, uv2coor, xyz2uv, @@ -77,12 +78,7 @@ def e2c( squeeze = False h, w = e_img.shape[:2] - if mode == "bilinear": - order = 1 - elif mode == "nearest": - order = 0 - else: - raise ValueError(f'Unknown mode: "{mode}".') + order = mode_to_order(mode) xyz = xyzcube(face_w) uv = xyz2uv(xyz) diff --git a/py360convert/e2p.py b/py360convert/e2p.py index 0eec00b..d619129 100644 --- a/py360convert/e2p.py +++ b/py360convert/e2p.py @@ -7,6 +7,7 @@ from .utils import ( DType, InterpolationMode, + mode_to_order, sample_equirec, uv2coor, xyz2uv, @@ -64,12 +65,7 @@ def e2p( in_rot = np.deg2rad(in_rot_deg) - if mode == "bilinear": - order = 1 - elif mode == "nearest": - order = 0 - else: - raise ValueError(f'Unknown mode: "{mode}".') + order = mode_to_order(mode) u = -u_deg * np.pi / 180 v = v_deg * np.pi / 180 diff --git a/py360convert/utils.py b/py360convert/utils.py index a0e79dc..e28fd77 100644 --- a/py360convert/utils.py +++ b/py360convert/utils.py @@ -5,11 +5,57 @@ from numpy.typing import NDArray from scipy.ndimage import map_coordinates +_mode_to_order = { + "nearest": 0, + "linear": 1, + "bilinear": 1, + "biquadratic": 2, + "quadratic": 2, + "quad": 2, + "bicubic": 3, + "cubic": 3, + "biquartic": 4, + "quartic": 4, + "biquintic": 5, + "quintic": 5, +} + CubeFormat = Literal["horizon", "list", "dict", "dice"] -InterpolationMode = Literal["bilinear", "nearest"] +InterpolationMode = Literal[ + "nearest", + "linear", + "bilinear", + "biquadratic", + "quadratic", + "quad", + "bicubic", + "cubic", + "biquartic", + "quartic", + "biquintic", + "quintic", +] DType = TypeVar("DType", bound=np.generic, covariant=True) +def mode_to_order(mode: InterpolationMode) -> int: + """Convert a human-friendly interpolation string to integer equivalent. + + Parameters + ---------- + mode: str + Human-friendly interpolation string. + + Returns + ------- + The order of the spline interpolation + """ + try: + return _mode_to_order[mode.lower()] + except KeyError: + raise ValueError(f'Unknown mode "{mode}".') from None + + def xyzcube(face_w: int) -> NDArray[np.float32]: """ Return the xyz coordinates of the unit cube in [F R B L U D] format.