Module: Gsplat::Math::CameraProjection

Defined in:
lib/gsplat/math/camera_projection.rb

Overview

Camera-space transformation and pinhole Gaussian projection primitives.

Class Method Summary collapse

Class Method Details

.distorted_proj(means, covars, intrinsics, camera_model, radial_coeffs: nil, tangential_coeffs: nil, thin_prism_coeffs: nil) ⇒ Object

rubocop:disable Metrics/ParameterLists



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
# File 'lib/gsplat/math/camera_projection.rb', line 76

def distorted_proj(means, covars, intrinsics, camera_model, radial_coeffs: nil,
                   tangential_coeffs: nil, thin_prism_coeffs: nil)
  # rubocop:enable Metrics/ParameterLists
  camera_count = means.shape[0]
  gaussian_count = means.shape[1]
  means2d = means.class.zeros(camera_count, gaussian_count, 2)
  covars2d = means.class.zeros(camera_count, gaussian_count, 2, 2)
  camera_count.times do |camera_index|
    projected, jacobians = CameraDistortion.project_camera(
      means[camera_index, true, true],
      intrinsics[camera_index, true, true],
      camera_model,
      radial: radial_coeffs && radial_coeffs[camera_index, true].to_a,
      tangential: tangential_coeffs && tangential_coeffs[camera_index, true].to_a,
      thin_prism: thin_prism_coeffs && thin_prism_coeffs[camera_index, true].to_a
    )
    means2d[camera_index, true, true] = projected
    transformed = Mat.matmul_batch(jacobians, covars[camera_index, true, true, true])
    covars2d[camera_index, true, true, true] = Mat.matmul_batch(
      transformed,
      jacobians.transpose(0, 2, 1)
    )
  end
  [means2d, covars2d]
end

.ortho_camera(means, intrinsics) ⇒ Object



191
192
193
194
195
196
197
198
199
# File 'lib/gsplat/math/camera_projection.rb', line 191

def ortho_camera(means, intrinsics)
  jacobians = means.class.zeros(means.shape[0], 2, 3)
  jacobians[true, 0, 0] = intrinsics[0, 0]
  jacobians[true, 1, 1] = intrinsics[1, 1]
  means2d = means.class.zeros(means.shape[0], 2)
  means2d[true, 0] = (means[true, 0] * intrinsics[0, 0]) + intrinsics[0, 2]
  means2d[true, 1] = (means[true, 1] * intrinsics[1, 1]) + intrinsics[1, 2]
  [means2d, jacobians]
end

.ortho_proj(means, covars, intrinsics, width, height) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/gsplat/math/camera_projection.rb', line 54

def ortho_proj(means, covars, intrinsics, width, height)
  means, covars, intrinsics = validate_projection_inputs(means, covars, intrinsics, width, height)
  camera_count = means.shape[0]
  gaussian_count = means.shape[1]
  means2d = means.class.zeros(camera_count, gaussian_count, 2)
  covars2d = means.class.zeros(camera_count, gaussian_count, 2, 2)
  camera_count.times do |camera_index|
    projected_means, jacobians = ortho_camera(
      means[camera_index, true, true],
      intrinsics[camera_index, true, true]
    )
    means2d[camera_index, true, true] = projected_means
    transformed = Mat.matmul_batch(jacobians, covars[camera_index, true, true, true])
    covars2d[camera_index, true, true, true] = Mat.matmul_batch(
      transformed,
      jacobians.transpose(0, 2, 1)
    )
  end
  [means2d, covars2d]
end

.persp_proj(means, covars, intrinsics, width, height) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/gsplat/math/camera_projection.rb', line 30

def persp_proj(means, covars, intrinsics, width, height)
  means, covars, intrinsics = validate_projection_inputs(means, covars, intrinsics, width, height)
  camera_count = means.shape[0]
  gaussian_count = means.shape[1]
  means2d = means.class.zeros(camera_count, gaussian_count, 2)
  covars2d = means.class.zeros(camera_count, gaussian_count, 2, 2)
  camera_count.times do |camera_index|
    projected_means, jacobians = pinhole_camera(
      means[camera_index, true, true],
      intrinsics[camera_index, true, true],
      width,
      height
    )
    means2d[camera_index, true, true] = projected_means
    covariance = covars[camera_index, true, true, true]
    transformed = Mat.matmul_batch(jacobians, covariance)
    covars2d[camera_index, true, true, true] = Mat.matmul_batch(
      transformed,
      jacobians.transpose(0, 2, 1)
    )
  end
  [means2d, covars2d]
end

.pinhole_camera(means, intrinsics, width, height) ⇒ Object

rubocop:disable Metrics/AbcSize



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
# File 'lib/gsplat/math/camera_projection.rb', line 147

def pinhole_camera(means, intrinsics, width, height)
  x_coord = means[true, 0]
  y_coord = means[true, 1]
  z_coord = means[true, 2]
  focal_x = intrinsics[0, 0]
  focal_y = intrinsics[1, 1]
  center_x = intrinsics[0, 2]
  center_y = intrinsics[1, 2]
  clipped_x = clip_ratio(
    x_coord / z_coord,
    -((center_x / focal_x) + (0.15 * width / focal_x)),
    ((width - center_x) / focal_x) + (0.15 * width / focal_x)
  ) * z_coord
  clipped_y = clip_ratio(
    y_coord / z_coord,
    -((center_y / focal_y) + (0.15 * height / focal_y)),
    ((height - center_y) / focal_y) + (0.15 * height / focal_y)
  ) * z_coord
  jacobians = means.class.zeros(means.shape[0], 2, 3)
  jacobians[true, 0, 0] = focal_x / z_coord
  jacobians[true, 0, 2] = -focal_x * clipped_x / (z_coord**2)
  jacobians[true, 1, 1] = focal_y / z_coord
  jacobians[true, 1, 2] = -focal_y * clipped_y / (z_coord**2)
  homogeneous_x = (intrinsics[0, 0] * x_coord) + (intrinsics[0, 1] * y_coord) +
                  (intrinsics[0, 2] * z_coord)
  homogeneous_y = (intrinsics[1, 0] * x_coord) + (intrinsics[1, 1] * y_coord) +
                  (intrinsics[1, 2] * z_coord)
  means2d = means.class.zeros(means.shape[0], 2)
  means2d[true, 0] = homogeneous_x / z_coord
  means2d[true, 1] = homogeneous_y / z_coord
  [means2d, jacobians]
end

.world_to_cam(means, covars, viewmats) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/gsplat/math/camera_projection.rb', line 9

def world_to_cam(means, covars, viewmats)
  means, covars, viewmats = validate_world_inputs(means, covars, viewmats)
  camera_count = viewmats.shape[0]
  gaussian_count = means.shape[0]
  camera_means = means.class.zeros(camera_count, gaussian_count, 3)
  camera_covars = means.class.zeros(camera_count, gaussian_count, 3, 3)
  camera_count.times do |camera_index|
    rotation = viewmats[camera_index, 0...3, 0...3]
    translation = viewmats[camera_index, 0...3, 3]
    rotation_batch = means.class.zeros(gaussian_count, 3, 3)
    rotation_batch[true, true, true] = rotation
    camera_means[camera_index, true, true] = means.dot(rotation.transpose) + translation
    rotated = Mat.matmul_batch(rotation_batch, covars)
    camera_covars[camera_index, true, true, true] = Mat.matmul_batch(
      rotated,
      rotation_batch.transpose(0, 2, 1)
    )
  end
  [camera_means, camera_covars]
end