mirror of
https://github.com/nerfstudio-project/nerfstudio.git
synced 2026-11-30 12:59:40 +08:00
feat: improve type hints (#1515)
* feat(plugins/registry): Improve Type Hints * feat(exporter/texture_utils): Improve Type Hints * feat(exporter/tsdf_utils): Improve Type Hints * feat(cameras/camera_utils): Improve Type Hints * feat(cameras/cameras): Improve Type Hints * feat(cameras/rays): Improve Type Hints * fix: add default value for device * fix: ArrayLike type hint
This commit is contained in:
1 parent
7309273d92
commit
4545dcb6b3
6 files changed
+44
-27
No files matched your search
@@ -21,13 +21,14 @@ from typing import List, Optional, Tuple
|
||||
|
||||
import numpy as np
|
||||
import torch
|
||||
from numpy.typing import ArrayLike
|
||||
from torchtyping import TensorType
|
||||
from typing_extensions import Literal
|
||||
|
||||
_EPS = np.finfo(float).eps * 4.0
|
||||
|
||||
|
||||
def unit_vector(data, axis: Optional[int] = None) -> np.ndarray:
|
||||
def unit_vector(data: ArrayLike, axis: Optional[int] = None) -> np.ndarray:
|
||||
"""Return ndarray normalized by length, i.e. Euclidean norm, along axis.
|
||||
|
||||
Args:
|
||||
@@ -46,7 +47,7 @@ def unit_vector(data, axis: Optional[int] = None) -> np.ndarray:
|
||||
return data
|
||||
|
||||
|
||||
def quaternion_from_matrix(matrix, isprecise: bool = False) -> np.ndarray:
|
||||
def quaternion_from_matrix(matrix: ArrayLike, isprecise: bool = False) -> np.ndarray:
|
||||
"""Return quaternion from rotation matrix.
|
||||
|
||||
Args:
|
||||
@@ -102,7 +103,9 @@ def quaternion_from_matrix(matrix, isprecise: bool = False) -> np.ndarray:
|
||||
return q
|
||||
|
||||
|
||||
def quaternion_slerp(quat0, quat1, fraction: float, spin: int = 0, shortestpath: bool = True) -> np.ndarray:
|
||||
def quaternion_slerp(
|
||||
quat0: ArrayLike, quat1: ArrayLike, fraction: float, spin: int = 0, shortestpath: bool = True
|
||||
) -> np.ndarray:
|
||||
"""Return spherical linear interpolation between two quaternions.
|
||||
Args:
|
||||
quat0: first quaternion
|
||||
@@ -136,7 +139,7 @@ def quaternion_slerp(quat0, quat1, fraction: float, spin: int = 0, shortestpath:
|
||||
return q0
|
||||
|
||||
|
||||
def quaternion_matrix(quaternion) -> np.ndarray:
|
||||
def quaternion_matrix(quaternion: ArrayLike) -> np.ndarray:
|
||||
"""Return homogeneous rotation matrix from quaternion.
|
||||
|
||||
Args:
|
||||
@@ -158,7 +161,7 @@ def quaternion_matrix(quaternion) -> np.ndarray:
|
||||
)
|
||||
|
||||
|
||||
def get_interpolated_poses(pose_a, pose_b, steps: int = 10) -> List[float]:
|
||||
def get_interpolated_poses(pose_a: ArrayLike, pose_b: ArrayLike, steps: int = 10) -> List[float]:
|
||||
"""Return interpolation of poses with specified number of steps.
|
||||
Args:
|
||||
poseA: first pose
|
||||
@@ -229,7 +232,7 @@ def get_interpolated_poses_many(
|
||||
return torch.tensor(traj), torch.tensor(k_interp)
|
||||
|
||||
|
||||
def normalize(x) -> TensorType[...]:
|
||||
def normalize(x: torch.Tensor) -> TensorType[...]:
|
||||
"""Returns a normalized vector."""
|
||||
return x / torch.linalg.norm(x)
|
||||
|
||||
@@ -249,7 +252,7 @@ def normalize_with_norm(x: torch.Tensor, dim: int) -> Tuple[torch.Tensor, torch.
|
||||
return x / norm, norm
|
||||
|
||||
|
||||
def viewmatrix(lookat, up, pos) -> TensorType[...]:
|
||||
def viewmatrix(lookat: torch.Tensor, up: torch.Tensor, pos: torch.Tensor) -> TensorType[...]:
|
||||
"""Returns a camera transformation matrix.
|
||||
|
||||
Args:
|
||||
|
||||
@@ -36,6 +36,8 @@ from nerfstudio.data.scene_box import SceneBox
|
||||
from nerfstudio.utils.misc import strtobool
|
||||
from nerfstudio.utils.tensor_dataclass import TensorDataclass
|
||||
|
||||
TORCH_DEVICE = Union[torch.device, str] # pylint: disable=invalid-name
|
||||
|
||||
|
||||
class CameraType(Enum):
|
||||
"""Supported camera types."""
|
||||
@@ -109,7 +111,7 @@ class Cameras(TensorDataclass):
|
||||
]
|
||||
] = CameraType.PERSPECTIVE,
|
||||
times: Optional[TensorType["num_cameras"]] = None,
|
||||
):
|
||||
) -> None:
|
||||
"""Initializes the Cameras object.
|
||||
|
||||
Note on Input Tensor Dimensions: All of these tensors have items of dimensions TensorType[3, 4]
|
||||
@@ -148,7 +150,7 @@ class Cameras(TensorDataclass):
|
||||
|
||||
self._use_nerfacc = strtobool(os.environ.get("INTERSECT_WITH_NERFACC", "TRUE"))
|
||||
|
||||
def _init_get_fc_xy(self, fc_xy, name):
|
||||
def _init_get_fc_xy(self, fc_xy: Union[float, torch.Tensor], name: str) -> torch.Tensor:
|
||||
"""
|
||||
Parses the input focal length / principle point x or y and returns a tensor of the correct shape
|
||||
|
||||
@@ -243,7 +245,7 @@ class Cameras(TensorDataclass):
|
||||
raise ValueError("Height must be an int, tensor, or None, received: " + str(type(h_w)))
|
||||
return h_w
|
||||
|
||||
def _init_get_times(self, times):
|
||||
def _init_get_times(self, times: Union[None, torch.Tensor]) -> Union[None, torch.Tensor]:
|
||||
if times is None:
|
||||
times = None
|
||||
elif isinstance(times, torch.Tensor):
|
||||
@@ -255,7 +257,7 @@ class Cameras(TensorDataclass):
|
||||
return times
|
||||
|
||||
@property
|
||||
def device(self):
|
||||
def device(self) -> TORCH_DEVICE:
|
||||
"""Returns the device that the camera is on."""
|
||||
return self.camera_to_worlds.device
|
||||
|
||||
@@ -270,7 +272,7 @@ class Cameras(TensorDataclass):
|
||||
return self.width
|
||||
|
||||
@property
|
||||
def is_jagged(self):
|
||||
def is_jagged(self) -> bool:
|
||||
"""
|
||||
Returns whether or not the cameras are "jagged" (i.e. the height and widths are different, meaning that
|
||||
you cannot concatenate the image coordinate maps together)
|
||||
|
||||
@@ -17,7 +17,7 @@ Some ray datastructures.
|
||||
"""
|
||||
import random
|
||||
from dataclasses import dataclass
|
||||
from typing import Callable, Dict, Optional
|
||||
from typing import Callable, Dict, Optional, Union
|
||||
|
||||
import torch
|
||||
from torchtyping import TensorType
|
||||
@@ -25,6 +25,8 @@ from torchtyping import TensorType
|
||||
from nerfstudio.utils.math import Gaussians, conical_frustum_to_gaussian
|
||||
from nerfstudio.utils.tensor_dataclass import TensorDataclass
|
||||
|
||||
TORCH_DEVICE = Union[str, torch.device] # pylint: disable=invalid-name
|
||||
|
||||
|
||||
@dataclass
|
||||
class Frustums(TensorDataclass):
|
||||
@@ -77,7 +79,7 @@ class Frustums(TensorDataclass):
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def get_mock_frustum(cls, device="cpu") -> "Frustums":
|
||||
def get_mock_frustum(cls, device: Optional[TORCH_DEVICE] = "cpu") -> "Frustums":
|
||||
"""Helper function to generate a placeholder frustum.
|
||||
|
||||
Returns:
|
||||
@@ -169,7 +171,7 @@ class RayBundle(TensorDataclass):
|
||||
"""
|
||||
self.camera_indices = torch.ones_like(self.origins[..., 0:1]).long() * camera_index
|
||||
|
||||
def __len__(self):
|
||||
def __len__(self) -> int:
|
||||
num_rays = torch.numel(self.origins) // self.origins.shape[-1]
|
||||
return num_rays
|
||||
|
||||
|
||||
Reference in new issue
Block a user