File size: 4,611 Bytes
42bed50 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 | """
Camera model and viewpoint geometry for 3D Gaussian Splatting rendering.
"""
from dataclasses import dataclass
from typing import Tuple, Union
import numpy as np
@dataclass
class Camera:
"""
Pinhole perspective camera for 3D scene rendering.
Attributes:
position: (3,) camera position in world space.
R: (3, 3) World-to-Camera rotation matrix.
t: (3,) World-to-Camera translation vector: x_cam = R @ x_world + t.
width: Image width in pixels.
height: Image height in pixels.
fov_x: Horizontal field of view in degrees.
fov_y: Vertical field of view in degrees.
"""
position: np.ndarray
R: np.ndarray
t: np.ndarray
width: int
height: int
fov_x: float
fov_y: float
@property
def fx(self) -> float:
"""Focal length along X in pixels."""
return float(self.width / (2.0 * np.tan(np.radians(self.fov_x) / 2.0)))
@property
def fy(self) -> float:
"""Focal length along Y in pixels."""
return float(self.height / (2.0 * np.tan(np.radians(self.fov_y) / 2.0)))
@property
def cx(self) -> float:
"""Principal point X in pixels."""
return float(self.width / 2.0)
@property
def cy(self) -> float:
"""Principal point Y in pixels."""
return float(self.height / 2.0)
def world_to_camera(self, points: np.ndarray) -> np.ndarray:
"""
Transform 3D points from world space to camera space:
p_cam = R @ p_world + t
"""
pts = np.asarray(points, dtype=np.float32)
# (N, 3) @ (3, 3)^T + (3,) = (N, 3)
return (np.matmul(pts, self.R.T) + self.t).astype(np.float32)
def create_lookat_camera(
eye: Tuple[float, float, float],
target: Tuple[float, float, float] = (0.0, 0.0, 0.0),
up: Tuple[float, float, float] = (0.0, 1.0, 0.0),
width: int = 512,
height: int = 512,
fov: float = 60.0,
) -> Camera:
"""
Create a Camera looking from `eye` towards `target` with given `up` vector.
Camera coordinate convention:
+X: Right
+Y: Down (image space) or Up (standard 3DGS view convention: camera looks along +Z, right +X, down +Y)
+Z: Forward (towards scene)
"""
eye_pt = np.array(eye, dtype=np.float32)
target_pt = np.array(target, dtype=np.float32)
up_vec = np.array(up, dtype=np.float32)
# Forward direction (+Z in camera space)
forward = target_pt - eye_pt
f_norm = np.linalg.norm(forward)
if f_norm == 0:
forward = np.array([0.0, 0.0, 1.0], dtype=np.float32)
else:
forward = forward / f_norm
# Right direction (+X in camera space)
right = np.cross(forward, up_vec)
r_norm = np.linalg.norm(right)
if r_norm == 0:
right = np.array([1.0, 0.0, 0.0], dtype=np.float32)
else:
right = right / r_norm
# True Up direction (-Y in screen space / +Y in camera view)
cam_up = np.cross(right, forward)
# Camera rotation matrix: rows are (right, -cam_up, forward) for standard screen Y down
R = np.stack([right, -cam_up, forward], axis=0).astype(np.float32)
t = (-np.matmul(R, eye_pt)).astype(np.float32)
aspect = width / float(height)
fov_x = fov
fov_y = float(2.0 * np.degrees(np.arctan(np.tan(np.radians(fov) / 2.0) / aspect)))
return Camera(
position=eye_pt,
R=R,
t=t,
width=width,
height=height,
fov_x=fov_x,
fov_y=fov_y,
)
def create_orbit_camera(
target: Tuple[float, float, float] = (0.0, 0.0, 0.0),
distance: float = 2.5,
azimuth_deg: float = 0.0,
elevation_deg: float = 0.0,
width: int = 512,
height: int = 512,
fov: float = 60.0,
) -> Camera:
"""
Create an orbital Camera placed on a sphere around `target`.
Args:
target: 3D point the camera is looking at.
distance: Distance from target in meters.
azimuth_deg: Horizontal rotation around Y axis in degrees.
elevation_deg: Vertical elevation angle in degrees (-89 to +89).
width: Render width.
height: Render height.
fov: Field of view in degrees.
"""
az_rad = np.radians(azimuth_deg)
el_rad = np.radians(np.clip(elevation_deg, -89.0, 89.0))
x = target[0] + distance * np.cos(el_rad) * np.sin(az_rad)
y = target[1] + distance * np.sin(el_rad)
z = target[2] + distance * np.cos(el_rad) * np.cos(az_rad)
return create_lookat_camera(
eye=(x, y, z),
target=target,
width=width,
height=height,
fov=fov,
)
|