mirror of
https://github.com/nerfstudio-project/nerfstudio.git
synced 2026-12-07 00:09:14 +08:00
Improving eval script with camera trajectory videos (#124)
* spiral traj working, specify version for hydra, better documentation * cleaned up spiral trajectory a bit
This commit is contained in:
1 parent
7cda0158c7
commit
4ddd146633
16 files changed
+3379
-769
No files matched your search
@@ -0,0 +1,316 @@
|
||||
# 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.
|
||||
|
||||
# pylint: skip-file
|
||||
"""
|
||||
Camera transformation helper code.
|
||||
"""
|
||||
|
||||
import math
|
||||
|
||||
import numpy as np
|
||||
import torch
|
||||
|
||||
_EPS = np.finfo(float).eps * 4.0
|
||||
|
||||
|
||||
def unit_vector(data, axis=None, out=None):
|
||||
"""Return ndarray normalized by length, i.e. Euclidean norm, along axis.
|
||||
>>> v0 = np.random.random(3)
|
||||
>>> v1 = unit_vector(v0)
|
||||
>>> np.allclose(v1, v0 / np.linalg.norm(v0))
|
||||
True
|
||||
>>> v0 = np.random.rand(5, 4, 3)
|
||||
>>> v1 = unit_vector(v0, axis=-1)
|
||||
>>> v2 = v0 / np.expand_dims(np.sqrt(np.sum(v0*v0, axis=2)), 2)
|
||||
>>> np.allclose(v1, v2)
|
||||
True
|
||||
>>> v1 = unit_vector(v0, axis=1)
|
||||
>>> v2 = v0 / np.expand_dims(np.sqrt(np.sum(v0*v0, axis=1)), 1)
|
||||
>>> np.allclose(v1, v2)
|
||||
True
|
||||
>>> v1 = np.empty((5, 4, 3))
|
||||
>>> unit_vector(v0, axis=1, out=v1)
|
||||
>>> np.allclose(v1, v2)
|
||||
True
|
||||
>>> list(unit_vector([]))
|
||||
[]
|
||||
>>> list(unit_vector([1]))
|
||||
[1.0]
|
||||
"""
|
||||
if out is None:
|
||||
data = np.array(data, dtype=np.float64, copy=True)
|
||||
if data.ndim == 1:
|
||||
data /= math.sqrt(np.dot(data, data))
|
||||
return data
|
||||
else:
|
||||
if out is not data:
|
||||
out[:] = np.array(data, copy=False)
|
||||
data = out
|
||||
length = np.atleast_1d(np.sum(data * data, axis))
|
||||
np.sqrt(length, length)
|
||||
if axis is not None:
|
||||
length = np.expand_dims(length, axis)
|
||||
data /= length
|
||||
if out is None:
|
||||
return data
|
||||
|
||||
|
||||
def quaternion_from_matrix(matrix, isprecise=False):
|
||||
"""Return quaternion from rotation matrix.
|
||||
If isprecise is True, the input matrix is assumed to be a precise rotation
|
||||
matrix and a faster algorithm is used.
|
||||
>>> q = quaternion_from_matrix(np.identity(4), True)
|
||||
>>> np.allclose(q, [1, 0, 0, 0])
|
||||
True
|
||||
>>> q = quaternion_from_matrix(np.diag([1, -1, -1, 1]))
|
||||
>>> np.allclose(q, [0, 1, 0, 0]) or np.allclose(q, [0, -1, 0, 0])
|
||||
True
|
||||
>>> R = rotation_matrix(0.123, (1, 2, 3))
|
||||
>>> q = quaternion_from_matrix(R, True)
|
||||
>>> np.allclose(q, [0.9981095, 0.0164262, 0.0328524, 0.0492786])
|
||||
True
|
||||
>>> R = [[-0.545, 0.797, 0.260, 0], [0.733, 0.603, -0.313, 0],
|
||||
... [-0.407, 0.021, -0.913, 0], [0, 0, 0, 1]]
|
||||
>>> q = quaternion_from_matrix(R)
|
||||
>>> np.allclose(q, [0.19069, 0.43736, 0.87485, -0.083611])
|
||||
True
|
||||
>>> R = [[0.395, 0.362, 0.843, 0], [-0.626, 0.796, -0.056, 0],
|
||||
... [-0.677, -0.498, 0.529, 0], [0, 0, 0, 1]]
|
||||
>>> q = quaternion_from_matrix(R)
|
||||
>>> np.allclose(q, [0.82336615, -0.13610694, 0.46344705, -0.29792603])
|
||||
True
|
||||
>>> R = random_rotation_matrix()
|
||||
>>> q = quaternion_from_matrix(R)
|
||||
>>> is_same_transform(R, quaternion_matrix(q))
|
||||
True
|
||||
>>> R = euler_matrix(0.0, 0.0, np.pi/2.0)
|
||||
>>> np.allclose(quaternion_from_matrix(R, isprecise=False),
|
||||
... quaternion_from_matrix(R, isprecise=True))
|
||||
True
|
||||
"""
|
||||
M = np.array(matrix, dtype=np.float64, copy=False)[:4, :4]
|
||||
if isprecise:
|
||||
q = np.empty((4,))
|
||||
t = np.trace(M)
|
||||
if t > M[3, 3]:
|
||||
q[0] = t
|
||||
q[3] = M[1, 0] - M[0, 1]
|
||||
q[2] = M[0, 2] - M[2, 0]
|
||||
q[1] = M[2, 1] - M[1, 2]
|
||||
else:
|
||||
i, j, k = 1, 2, 3
|
||||
if M[1, 1] > M[0, 0]:
|
||||
i, j, k = 2, 3, 1
|
||||
if M[2, 2] > M[i, i]:
|
||||
i, j, k = 3, 1, 2
|
||||
t = M[i, i] - (M[j, j] + M[k, k]) + M[3, 3]
|
||||
q[i] = t
|
||||
q[j] = M[i, j] + M[j, i]
|
||||
q[k] = M[k, i] + M[i, k]
|
||||
q[3] = M[k, j] - M[j, k]
|
||||
q *= 0.5 / math.sqrt(t * M[3, 3])
|
||||
else:
|
||||
m00 = M[0, 0]
|
||||
m01 = M[0, 1]
|
||||
m02 = M[0, 2]
|
||||
m10 = M[1, 0]
|
||||
m11 = M[1, 1]
|
||||
m12 = M[1, 2]
|
||||
m20 = M[2, 0]
|
||||
m21 = M[2, 1]
|
||||
m22 = M[2, 2]
|
||||
# symmetric matrix K
|
||||
K = np.array(
|
||||
[
|
||||
[m00 - m11 - m22, 0.0, 0.0, 0.0],
|
||||
[m01 + m10, m11 - m00 - m22, 0.0, 0.0],
|
||||
[m02 + m20, m12 + m21, m22 - m00 - m11, 0.0],
|
||||
[m21 - m12, m02 - m20, m10 - m01, m00 + m11 + m22],
|
||||
]
|
||||
)
|
||||
K /= 3.0
|
||||
# quaternion is eigenvector of K that corresponds to largest eigenvalue
|
||||
w, V = np.linalg.eigh(K)
|
||||
q = V[[3, 0, 1, 2], np.argmax(w)]
|
||||
if q[0] < 0.0:
|
||||
np.negative(q, q)
|
||||
return q
|
||||
|
||||
|
||||
def quaternion_slerp(quat0, quat1, fraction, spin=0, shortestpath=True):
|
||||
"""Return spherical linear interpolation between two quaternions.
|
||||
>>> q0 = random_quaternion()
|
||||
>>> q1 = random_quaternion()
|
||||
>>> q = quaternion_slerp(q0, q1, 0)
|
||||
>>> np.allclose(q, q0)
|
||||
True
|
||||
>>> q = quaternion_slerp(q0, q1, 1, 1)
|
||||
>>> np.allclose(q, q1)
|
||||
True
|
||||
>>> q = quaternion_slerp(q0, q1, 0.5)
|
||||
>>> angle = math.acos(np.dot(q0, q))
|
||||
>>> (np.allclose(2, math.acos(np.dot(q0, q1)) / angle) or
|
||||
np.allclose(2, math.acos(-np.dot(q0, q1)) / angle))
|
||||
True
|
||||
"""
|
||||
q0 = unit_vector(quat0[:4])
|
||||
q1 = unit_vector(quat1[:4])
|
||||
if fraction == 0.0:
|
||||
return q0
|
||||
elif fraction == 1.0:
|
||||
return q1
|
||||
d = np.dot(q0, q1)
|
||||
if abs(abs(d) - 1.0) < _EPS:
|
||||
return q0
|
||||
if shortestpath and d < 0.0:
|
||||
# invert rotation
|
||||
d = -d
|
||||
np.negative(q1, q1)
|
||||
angle = math.acos(d) + spin * math.pi
|
||||
if abs(angle) < _EPS:
|
||||
return q0
|
||||
isin = 1.0 / math.sin(angle)
|
||||
q0 *= math.sin((1.0 - fraction) * angle) * isin
|
||||
q1 *= math.sin(fraction * angle) * isin
|
||||
q0 += q1
|
||||
return q0
|
||||
|
||||
|
||||
def quaternion_matrix(quaternion):
|
||||
"""Return homogeneous rotation matrix from quaternion.
|
||||
>>> M = quaternion_matrix([0.99810947, 0.06146124, 0, 0])
|
||||
>>> np.allclose(M, rotation_matrix(0.123, [1, 0, 0]))
|
||||
True
|
||||
>>> M = quaternion_matrix([1, 0, 0, 0])
|
||||
>>> np.allclose(M, np.identity(4))
|
||||
True
|
||||
>>> M = quaternion_matrix([0, 1, 0, 0])
|
||||
>>> np.allclose(M, np.diag([1, -1, -1, 1]))
|
||||
True
|
||||
"""
|
||||
q = np.array(quaternion, dtype=np.float64, copy=True)
|
||||
n = np.dot(q, q)
|
||||
if n < _EPS:
|
||||
return np.identity(4)
|
||||
q *= math.sqrt(2.0 / n)
|
||||
q = np.outer(q, q)
|
||||
return np.array(
|
||||
[
|
||||
[1.0 - q[2, 2] - q[3, 3], q[1, 2] - q[3, 0], q[1, 3] + q[2, 0], 0.0],
|
||||
[q[1, 2] + q[3, 0], 1.0 - q[1, 1] - q[3, 3], q[2, 3] - q[1, 0], 0.0],
|
||||
[q[1, 3] - q[2, 0], q[2, 3] + q[1, 0], 1.0 - q[1, 1] - q[2, 2], 0.0],
|
||||
[0.0, 0.0, 0.0, 1.0],
|
||||
]
|
||||
)
|
||||
|
||||
|
||||
def get_interpolated_poses(poseA, poseB, steps=10):
|
||||
"""Return interpolation of poses with specified number of steps.
|
||||
Args:
|
||||
poseA
|
||||
poseB
|
||||
Return:
|
||||
list of poses
|
||||
"""
|
||||
|
||||
quatA = quaternion_from_matrix(poseA[:3, :3])
|
||||
quatB = quaternion_from_matrix(poseB[:3, :3])
|
||||
|
||||
ts = np.linspace(0, 1, steps)
|
||||
quats = [quaternion_slerp(quatA, quatB, t) for t in ts]
|
||||
trans = [(1 - t) * poseA[:3, 3] + t * poseB[:3, 3] for t in ts]
|
||||
|
||||
posesAB = []
|
||||
for quat, tran in zip(quats, trans):
|
||||
pose = np.identity(4)
|
||||
pose[:3, :3] = quaternion_matrix(quat)[:3, :3]
|
||||
pose[:3, 3] = tran
|
||||
posesAB.append(pose)
|
||||
return posesAB
|
||||
|
||||
|
||||
def get_interpolated_K(KA, KB, steps=10):
|
||||
"""
|
||||
Interpolate between two camera poses.
|
||||
Args:
|
||||
Returns:
|
||||
"""
|
||||
Ks = []
|
||||
ts = np.linspace(0, 1, steps)
|
||||
for t in ts:
|
||||
newK = KA * (1.0 - t) + KB * t
|
||||
Ks.append(newK)
|
||||
return Ks
|
||||
|
||||
|
||||
def get_interpolated_poses_many(list_of_poses, list_of_Ks, steps_per_transition=10):
|
||||
"""
|
||||
Args:
|
||||
list_of_poses
|
||||
steps
|
||||
distances_per_step
|
||||
Returns:
|
||||
"""
|
||||
traj = []
|
||||
Ks = []
|
||||
for idx in range(len(list_of_poses) - 1):
|
||||
poseA = list_of_poses[idx]
|
||||
poseB = list_of_poses[idx + 1]
|
||||
# steps = int(np.linalg.norm(poseA[:3,3] - poseB[:3,3], ord=2) / distance_per_step)
|
||||
posesAB = get_interpolated_poses(poseA, poseB, steps=steps_per_transition)
|
||||
traj += posesAB
|
||||
Ks += get_interpolated_K(list_of_Ks[idx], list_of_Ks[idx + 1], steps_per_transition)
|
||||
return traj, Ks
|
||||
|
||||
|
||||
def get_swirl_poses(pose):
|
||||
"""
|
||||
Returns a swirl around the camera poses.
|
||||
# TODO(ethan): add parameters and make this work
|
||||
"""
|
||||
N_frames = 30 * 4
|
||||
dx = np.linspace(0, 0.03, N_frames)
|
||||
dy = np.linspace(0, -0.1, N_frames)
|
||||
dz = np.linspace(0, 0.5, N_frames)
|
||||
poses = np.tile(pose, (N_frames, 1, 1))
|
||||
for i in range(N_frames):
|
||||
poses[i, 0, 3] += dx[i]
|
||||
poses[i, 1, 3] += dy[i]
|
||||
poses[i, 2, 3] += dz[i]
|
||||
return poses
|
||||
|
||||
|
||||
def normalize(x):
|
||||
"""Returns a normalized vector."""
|
||||
return x / torch.linalg.norm(x)
|
||||
|
||||
|
||||
def viewmatrix(lookat, up, pos):
|
||||
"""Returns a camera transformation matrix.
|
||||
|
||||
Args:
|
||||
lookat: The direction the camera is looking.
|
||||
up: The upward direction of the camera.
|
||||
pos: The position of the camera.
|
||||
|
||||
Returns:
|
||||
torch.Tensor: A camera transformation matrix.
|
||||
"""
|
||||
vec2 = normalize(lookat)
|
||||
vec1_avg = normalize(up)
|
||||
vec0 = normalize(torch.cross(vec1_avg, vec2))
|
||||
vec1 = normalize(torch.cross(vec2, vec0))
|
||||
m = torch.stack([vec0, vec1, vec2, pos], 1)
|
||||
return m
|
||||
Reference in new issue
Block a user