Class: Gsplat::Ops::CameraDirections

Inherits:
Autograd::Function show all
Defined in:
lib/gsplat/ops/tensor_shape_ops.rb

Overview

Computes camera-to-Gaussian viewing directions from world-to-camera matrices.

Class Method Summary collapse

Methods inherited from Autograd::Function

apply

Class Method Details

.backward(_context, gradient) ⇒ Object



64
65
66
# File 'lib/gsplat/ops/tensor_shape_ops.rb', line 64

def backward(_context, gradient)
  [gradient.sum(axis: 0), nil]
end

.forward(context, means, viewmats) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/gsplat/ops/tensor_shape_ops.rb', line 45

def forward(context, means, viewmats)
  unless means.ndim == 2 && means.shape[-1] == 3 &&
         viewmats.ndim == 3 && viewmats.shape[1..] == [4, 4]
    raise ShapeError,
          "expected means [N,3] and viewmats [C,4,4], " \
          "got #{means.shape.inspect} and #{viewmats.shape.inspect}"
  end

  output = means.class.zeros(viewmats.shape[0], means.shape[0], 3)
  viewmats.shape[0].times do |camera_index|
    rotation = means.class.cast(viewmats[camera_index, 0...3, 0...3])
    translation = means.class.cast(viewmats[camera_index, 0...3, 3])
    camera_center = -translation.dot(rotation)
    output[camera_index, true, true] = means - camera_center
  end
  context.save(means.shape)
  output
end