File size: 7,203 Bytes
6dac0b2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# Copyright 2023 Google LLC
#
# 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
#
#     https://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.
#
# Authors: spopov@google.com (Stefan Popov), kmaninis@google.com (Kevis-Kokitsi Maninis)
"""Functions for computing camera matrices."""

import math

import torch as t
import torch.nn.functional as F

import transformations
import misc_util


def look_at_rh(eye: t.Tensor, center: t.Tensor, up: t.Tensor) -> t.Tensor:
  """Computes a right-handed 4x4 look-at camera matrix.

  Args:
    eye: The camera location in 3D, float32[3].
    center: The camera faces towards center, float32[3].
    up: The "up" direction of the 3D world.

  Returns:
    The right-handed look-at matrix, float32[4, 4].
  """
  f = F.normalize(center - eye, dim=-1)
  s = F.normalize(t.cross(f, up), dim=-1)
  u = t.cross(s, f)

  return eye.new_tensor([
      [s[0], s[1], s[2], -t.dot(s, eye)],
      [u[0], u[1], u[2], -t.dot(u, eye)],
      [-f[0], -f[1], -f[2], t.dot(f, eye)],
      [0, 0, 0, 1]
  ], dtype=t.float32)

def look_at_lh(eye: t.Tensor, center: t.Tensor, up: t.Tensor) -> t.Tensor:
  """Computes a left-handed 4x4 look-at camera matrix.

  Args:
    eye: The camera location in 3D, float32[3].
    center: The camera faces towards center, float32[3].
    up: The "up" direction of the 3D world.

  Returns:
    The left-handed look-at matrix, float32[4, 4].
  """
  f = F.normalize(center - eye, dim=-1)
  s = F.normalize(t.cross(up, f), dim=-1)
  u = t.cross(f, s)

  return eye.new_tensor([
      [s[0], s[1], s[2], -t.dot(s, eye)],
      [u[0], u[1], u[2], -t.dot(u, eye)],
      [f[0], f[1], f[2], -t.dot(f, eye)],
      [0, 0, 0, 1],
  ], dtype=t.float32)


def perspective_rh(fov_y: t.Tensor, aspect: t.Tensor, z_near: t.Tensor,
    z_far: t.Tensor) -> t.Tensor:
  """Computes a 4x4 right-handed perspective projection matrix.

  Args:
    fov_y: The field of view in radians, float32.
    aspect: The aspect ratio, float32.
    z_near: The near plane, float32.
    z_far: The far plane, float32.

  Returns:
    The right-handed perspective projection matrix, float32[4, 4].
  """
  fov_y = misc_util.to_tensor(fov_y, dtype=t.float32)
  tan_half_fov_y = t.tan(fov_y / 2)
  fov_mat = [
      [1.0 / (aspect * tan_half_fov_y), 0, 0, 0],
      [0, 1.0 / tan_half_fov_y, 0, 0],
      [
          0, 0, -(z_far + z_near) / (z_far - z_near),
          -(2 * z_far * z_near) / (z_far - z_near)
      ],
      [0, 0, -1, 0],
  ]

  return fov_y.new_tensor(fov_mat, dtype=t.float32)

def perspective_lh(fov_y: t.Tensor, aspect: t.Tensor, z_near: t.Tensor,
                   z_far: t.Tensor) -> t.Tensor:
  """Computes a 4x4 left-handed perspective projection matrix.

  Args:
    fov_y: The field of view in radians, float32.
    aspect: The aspect ratio, float32.
    z_near: The near plane, float32.
    z_far: The far plane, float32.

  Returns:
    The left-handed perspective projection matrix, float32[4, 4].
  """
  fov_y = misc_util.to_tensor(fov_y, dtype=t.float32)
  tan_half_fov_y = t.tan(fov_y / 2)
  fov_mat = [
      [1.0 / (aspect * tan_half_fov_y), 0, 0, 0],
      [0, 1.0 / tan_half_fov_y, 0, 0],
      [
          0, 0, (z_far + z_near) / (z_far - z_near),
          -(2 * z_far * z_near) / (z_far - z_near)
      ],
      [0, 0, 1, 0],
  ]

  return fov_y.new_tensor(fov_mat, dtype=t.float32)


def cameras_on_tetrahedron_vertices(coordinate_system='RH') -> t.Tensor:
  """Computes view matrices of cameras placed at tetrahedron vertices.

  Args:
    coordinate_system: "RH" (right-handed) or "LH" (left-handed).
  Returns:
    The 4x4 camera transformation matrices. The first three cameras are above
    the coordinate system origin and look at the origin, while the last camera
    looks at the origin from above.

  Assumes {coordinate_system} coordinate system where Y points up.
  """

  tetrahedron_vertices = t.tensor(
      [(math.sqrt(8.0 / 9), 1.0 / 3, 0),
       (-math.sqrt(2.0 / 9), 1.0 / 3, math.sqrt(2.0 / 3)),
       (-math.sqrt(2.0 / 9), 1.0 / 3, -math.sqrt(2.0 / 3)),
       (0, 1, 0)], dtype=t.float32)
  up_vectors = t.tensor([[0, 1, 0]] * 3 + [[1, -1, 0]], dtype=t.float32)
  matrices = []

  if coordinate_system == 'LH':
    look_at_fn = look_at_lh
  elif coordinate_system == 'RH':
    look_at_fn = look_at_rh
  else:
    raise ValueError ('Choose one of "RH" and "LH" as the coordinate system.')

  for camera_origin, up_vector in zip(tetrahedron_vertices, up_vectors):
    look_at = look_at_fn(camera_origin, t.zeros(3, dtype=t.float32), up_vector)
    matrices.append(look_at)
  return t.stack(matrices, 0)


def perspective_projection(aspect_ratio=1.0, znear=0.0001, zfar=10,
                           fovy_degrees=60, coordinate_system='RH') -> t.Tensor:
  """Returns a 4x4 perspective projection matrix."""
  if coordinate_system == 'RH':
    perspective_projection_func = perspective_rh
  elif coordinate_system == 'LH':
    perspective_projection_func = perspective_lh
  else:
    raise ValueError(f'Invalid coordinate system: {coordinate_system}')
  result = perspective_projection_func(
      fovy_degrees * math.pi / 180, aspect_ratio, znear, zfar)
  # Invert the Y axis, since the origin in 2D in OpenGL is the top left corner.
  return t.matmul(transformations.scale((1, -1, 1)), result)


def get_views_for_mesh(vertex_positions: t.Tensor,
                       move_away_mul=0.7, coordinate_system='RH') -> t.Tensor:
  """Computes 4 camera matrices, looking the scene from 4 suitable sides."""
  mesh_min = vertex_positions.reshape([-1, 3]).min(dim=0).values
  mesh_max = vertex_positions.reshape([-1, 3]).max(dim=0).values
  diagonal = (mesh_max - mesh_min).max()
  center = (mesh_min + mesh_max) / 2

  device = vertex_positions.device
  tetra_cameras = cameras_on_tetrahedron_vertices(
      coordinate_system=coordinate_system).to(device)
  center_scene = (
      transformations.translate(
          -center).to(device).expand(tetra_cameras.shape))
  move_away = (
      transformations.translate(t.tensor([0, 0, diagonal * move_away_mul]))
      .to(device).expand(tetra_cameras.shape))
  project = perspective_projection(
      1, zfar=diagonal * 3, znear=(diagonal + 10) / 1000)
  project = project.to(device).expand(tetra_cameras.shape)

  return transformations.chain([
      project,
      move_away,
      tetra_cameras,
      center_scene,
  ])


def get_default_camera_for_mesh(
    vertex_positions: t.Tensor, move_away_mul=0.7, camera_index=0,
    coordinate_system="RH") -> t.Tensor:
  """Computes a default camera matrix, looking the object from above."""
  if coordinate_system == 'RH':
    move_away_mul *= -1.

  return get_views_for_mesh(
      vertex_positions, move_away_mul=move_away_mul,
      coordinate_system=coordinate_system)[camera_index]