grid.py 578 Bytes
Newer Older
rusty1s's avatar
rusty1s committed
1
2
3
from .utils.ffi import grid


rusty1s's avatar
rusty1s committed
4
def grid_cluster(pos, size, start=None, end=None):
rusty1s's avatar
rusty1s committed
5
    pos = pos.unsqueeze(-1) if pos.dim() == 1 else pos
rusty1s's avatar
rusty1s committed
6

rusty1s's avatar
rusty1s committed
7
8
9
    assert pos.size(1) == size.size(0), (
        'Last dimension of position tensor must have same size as size tensor')

rusty1s's avatar
rusty1s committed
10
11
12
13
14
15
16
17
18
19
20
    start = pos.t().min(dim=1)[0] if start is None else start
    end = pos.t().max(dim=1)[0] if end is None else end
    pos, end = pos - start, end - start

    size = size.type_as(pos)
    count = (end / size).long() + 1

    cluster = count.new(pos.size(0))
    grid(cluster, pos, size, count)

    return cluster