Module: Gsplat::Backend::RubyIsectTiles

Defined in:
lib/gsplat/backend/ruby/isect_tiles.rb

Overview

Numo/Ruby tile intersection enumeration and offset encoding.

Constant Summary collapse

TILE_KEY_LOW_BITS =
32

Class Method Summary collapse

Class Method Details

.forward(means2d, radii, depths, tile_size, tile_width, tile_height, sort:) ⇒ Object

rubocop:disable Metrics/ParameterLists



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
# File 'lib/gsplat/backend/ruby/isect_tiles.rb', line 12

def forward(means2d, radii, depths, tile_size, tile_width, tile_height, sort:)
  # rubocop:enable Metrics/ParameterLists
  means2d, radii, depths = validate_inputs(
    means2d,
    radii,
    depths,
    tile_size,
    tile_width,
    tile_height
  )
  camera_count, gaussian_count = means2d.shape[0...2]
  bounds, tiles_per_gauss = tile_bounds(
    means2d,
    radii,
    tile_size,
    tile_width,
    tile_height
  )
  intersection_count = tiles_per_gauss.sum.to_i
  isect_ids = Numo::Int64.zeros(intersection_count)
  flatten_ids = Numo::Int32.zeros(intersection_count)
  tile_bits = tile_n_bits(tile_width, tile_height)
  validate_key_width!(camera_count, tile_bits)
  write_intersections!(
    isect_ids,
    flatten_ids,
    bounds,
    tiles_per_gauss,
    depths,
    gaussian_count,
    tile_width,
    tile_bits
  )
  if sort && intersection_count.positive?
    keys = isect_ids.to_a
    order = (0...intersection_count).sort_by { |index| [keys[index], index] }
    isect_ids = isect_ids[order].dup
    flatten_ids = flatten_ids[order].dup
  end
  [tiles_per_gauss, isect_ids, flatten_ids]
end

.offset_encode(isect_ids, camera_count, tile_width, tile_height) ⇒ Object

Raises:



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/gsplat/backend/ruby/isect_tiles.rb', line 54

def offset_encode(isect_ids, camera_count, tile_width, tile_height)
  validate_grid!(camera_count, tile_width, tile_height)
  raise ShapeError, "expected isect_ids [M]" unless isect_ids.is_a?(Numo::NArray) && isect_ids.ndim == 1

  keys = Numo::Int64.cast(isect_ids)
  counts = Numo::Int64.zeros(camera_count, tile_height, tile_width)
  tile_bits = tile_n_bits(tile_width, tile_height)
  tile_mask = (1 << tile_bits) - 1
  keys.each do |key|
    upper = key >> TILE_KEY_LOW_BITS
    camera_id = upper >> tile_bits
    tile_id = upper & tile_mask
    validate_decoded_key!(camera_id, tile_id, camera_count, tile_width, tile_height)
    counts[camera_id, tile_id / tile_width, tile_id % tile_width] += 1
  end
  offsets = Numo::Int32.zeros(camera_count, tile_height, tile_width)
  running = 0
  counts.size.times do |index|
    offsets[index] = running
    running += counts[index]
  end
  offsets
end