# Copyright 2022 The Plenoptix Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """ Code for camera paths. """ from typing import Optional, Tuple import torch import nerfactory.cameras.utils as camera_utils from nerfactory.cameras.cameras import Cameras from nerfactory.cameras.utils import get_interpolated_poses_many def get_interpolated_camera_path(cameras: Cameras, steps: int) -> Cameras: """Generate a camera path between two cameras. Args: camera_a: The first camera. camera_b: The second camera. steps: The number of steps to interpolate between the two cameras. Returns: A new set of cameras along a path. """ Ks = cameras.get_intrinsics_matrices().cpu().numpy() poses = cameras.camera_to_worlds().cpu().numpy() poses, Ks = get_interpolated_poses_many(poses, Ks, steps_per_transition=steps) cameras = Cameras(fx=Ks[:, 0, 0], fy=Ks[:, 1, 1], cx=Ks[0, 0, 2], cy=Ks[0, 1, 2], camera_to_worlds=poses) return cameras def get_spiral_path( camera: Cameras, steps: int = 30, radius: Optional[float] = None, radiuses: Optional[Tuple[float]] = None, rots: int = 2, zrate: float = 0.5, ) -> Cameras: """ Returns a list of camera in a sprial trajectory. Args: camera: The camera to start the spiral from. steps: The number of cameras in the generated path. radius: The radius of the spiral for all xyz directions. radiuses: The list of radii for the spiral in xyz directions. rots: The number of rotations to apply to the camera. zrate: How much to change the z position of the camera. Returns: A spiral camera path. """ assert radius is not None or radiuses is not None, "Either radius or radiuses must be specified." if radius is not None and radiuses is None: rad = torch.tensor([radius] * 3, device=camera.device) elif radiuses is not None and radius is None: rad = torch.tensor(radiuses, device=camera.device) else: raise ValueError("Only one of radius or radiuses must be specified.") # TODO: don't hardcode this. pass this in up = camera.camera_to_worlds[0, :3, 2] # scene is z up # up = camera.camera_to_world[:3, 1] # this will rotate 90 degrees focal = torch.min(camera.fx[0], camera.fy[0]) target = torch.tensor([0, 0, -focal], device=camera.device) # camera looking in -z direction c2w = camera.camera_to_worlds[0] ones = torch.tensor([0, 0, 0, 1], device=c2w.device)[None] c2wh_global = torch.cat([c2w, ones], dim=0) local_c2whs = [] for theta in torch.linspace(0.0, 2.0 * torch.pi * rots, steps + 1)[:-1]: center = ( torch.tensor([torch.cos(theta), -torch.sin(theta), -torch.sin(theta * zrate)], device=camera.device) * rad ) lookat = center - target c2w = camera_utils.viewmatrix(lookat, up, center) ones = torch.tensor([0, 0, 0, 1], device=c2w.device)[None] c2wh = torch.cat([c2w, ones], dim=0) local_c2whs.append(c2wh) new_c2ws = [] for local_c2wh in local_c2whs: c2wh = torch.matmul(c2wh_global, local_c2wh) new_c2ws.append(c2wh[:3, :4]) return Cameras(fx=camera.fx[0], fy=camera.fy[0], cx=camera.cx, cy=camera.cy, camera_to_worlds=new_c2ws)