Module: Gsplat::RasterizationHelpers

Defined in:
lib/gsplat/rasterization_helpers.rb

Overview

Feature preparation and metadata helpers for the high-level renderer.

Class Method Summary collapse

Class Method Details

.camera_broadcast(value, camera_count) ⇒ Object



8
9
10
11
12
13
# File 'lib/gsplat/rasterization_helpers.rb', line 8

def camera_broadcast(value, camera_count)
  shape = Ops::TensorOps.data(value).shape
  return value if shape[0] == camera_count && shape.length >= 2

  Ops::TensorOps.apply(Ops::CameraBroadcast, value, camera_count)
end

.metadata(radii, means2d, depths, conics, opacities, tile_width, tile_height, tiles_per_gauss, isect_ids, flatten_ids, isect_offsets, width, height, tile_size, camera_count) ⇒ Object

rubocop:disable Metrics/ParameterLists



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/gsplat/rasterization_helpers.rb', line 137

def (radii, means2d, depths, conics, opacities, tile_width, tile_height,
             tiles_per_gauss, isect_ids, flatten_ids, isect_offsets, width, height,
             tile_size, camera_count)
  # rubocop:enable Metrics/ParameterLists
  {
    camera_ids: nil,
    gaussian_ids: nil,
    radii: radii,
    means2d: means2d,
    depths: depths,
    conics: conics,
    opacities: opacities,
    tile_width: tile_width,
    tile_height: tile_height,
    tiles_per_gauss: tiles_per_gauss,
    isect_ids: isect_ids,
    flatten_ids: flatten_ids,
    isect_offsets: isect_offsets,
    width: width,
    height: height,
    tile_size: tile_size,
    n_cameras: camera_count
  }
end

.prepare_colors(means, colors, viewmats, radii, camera_count, sh_degree) ⇒ Object

rubocop:disable Metrics/ParameterLists



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/gsplat/rasterization_helpers.rb', line 16

def prepare_colors(means, colors, viewmats, radii, camera_count, sh_degree)
  # rubocop:enable Metrics/ParameterLists
  unless sh_degree
    return colors if Ops::TensorOps.data(colors).ndim == 3

    return Ops::TensorOps.apply(Ops::CameraBroadcast, colors, camera_count)
  end

  directions = Ops::TensorOps.apply(Ops::CameraDirections, means, viewmats)
  coefficients = Ops::TensorOps.apply(Ops::CameraBroadcast, colors, camera_count)
  evaluated = Gsplat.spherical_harmonics(
    sh_degree,
    directions,
    coefficients,
    masks: visible_radii(radii)
  )
  Ops::TensorOps.apply(Ops::AddClampMin, evaluated, offset: 0.5, minimum: 0.0)
end

.rasterize_eval3d_features(means, quats, scales, features, opacities, viewmats, intrinsics, width, height, tile_size, offsets, flatten_ids, backgrounds:, camera_model:, use_hit_distance:, return_normals:) ⇒ Object

rubocop:disable Metrics/ParameterLists

Raises:

  • (ArgumentError)


112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/gsplat/rasterization_helpers.rb', line 112

def rasterize_eval3d_features(means, quats, scales, features, opacities, viewmats, intrinsics,
                              width, height, tile_size, offsets, flatten_ids, backgrounds:,
                              camera_model:, use_hit_distance:, return_normals:)
  # rubocop:enable Metrics/ParameterLists
  raise ArgumentError, "world-space rendering requires quats and scales" unless quats && scales

  inputs = [means, quats, scales, features, opacities, backgrounds]
  options = {
    viewmats: Ops::TensorOps.data(viewmats),
    intrinsics: Ops::TensorOps.data(intrinsics),
    width: width,
    height: height,
    tile_size: tile_size,
    offsets: offsets,
    flatten_ids: flatten_ids,
    camera_model: camera_model.to_s,
    use_hit_distance: use_hit_distance,
    return_normals: return_normals
  }
  return Ops::Eval3dRasterize.apply(*inputs, **options) if inputs.any?(Autograd::Variable)

  Backend::RubyEval3dRasterizer.forward(*inputs, **options)
end

.rasterize_features(means2d, conics, features, opacities, width, height, tile_size, offsets, flatten_ids, backgrounds:, channel_chunk:, absgrad:) ⇒ Object

rubocop:disable Metrics/ParameterLists



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
# File 'lib/gsplat/rasterization_helpers.rb', line 53

def rasterize_features(means2d, conics, features, opacities, width, height, tile_size, offsets, flatten_ids,
                       backgrounds:, channel_chunk:, absgrad:)
  # rubocop:enable Metrics/ParameterLists
  channel_count = Ops::TensorOps.data(features).shape[-1]
  if channel_count <= channel_chunk
    return Gsplat.rasterize_to_pixels(
      means2d,
      conics,
      features,
      opacities,
      width,
      height,
      tile_size,
      offsets,
      flatten_ids,
      backgrounds: backgrounds,
      absgrad: absgrad
    )
  end

  rasterize_feature_chunks(
    means2d, conics, features, opacities, width, height, tile_size, offsets, flatten_ids,
    backgrounds: backgrounds, channel_chunk: channel_chunk, absgrad: absgrad
  )
end

.render_features(colors, depths, render_mode) ⇒ Object



42
43
44
45
46
47
48
49
50
# File 'lib/gsplat/rasterization_helpers.rb', line 42

def render_features(colors, depths, render_mode)
  return [colors, nil] if render_mode == "RGB"

  depth_features = Ops::TensorOps.apply(Ops::DepthFeatures, depths)
  return [depth_features, 0] if %w[d Ed D ED].include?(render_mode)

  feature_count = Ops::TensorOps.data(colors).shape[-1]
  [Ops::TensorOps.apply(Ops::ConcatDepth, colors, depth_features), feature_count]
end

.visible_radii(radii) ⇒ Object



35
36
37
38
39
40
# File 'lib/gsplat/rasterization_helpers.rb', line 35

def visible_radii(radii)
  values = Ops::TensorOps.data(radii)
  return values.gt(0) unless values.ndim >= 3 && values.shape[-1] == 2

  values.min(axis: values.ndim - 1).gt(0)
end