| 1 | # -------------------------------------------------------- |
| 2 | # Octree-based Sparse Convolutional Neural Networks |
| 3 | # Copyright (c) 2022 Peng-Shuai Wang <wangps@hotmail.com> |
| 4 | # Licensed under The MIT License [see LICENSE for details] |
| 5 | # Written by Peng-Shuai Wang |
| 6 | # -------------------------------------------------------- |
| 7 | |
| 8 | import torch |
| 9 | from typing import Optional, Union |
| 10 | |
| 11 | |
| 12 | class KeyLUT: |
| 13 | def __init__(self): |
| 14 | r256 = torch.arange(256, dtype=torch.int64) |
| 15 | r512 = torch.arange(512, dtype=torch.int64) |
| 16 | zero = torch.zeros(256, dtype=torch.int64) |
| 17 | device = torch.device("cpu") |
| 18 | |
| 19 | self._encode = { |
| 20 | device: ( |
| 21 | self.xyz2key(r256, zero, zero, 8), |
| 22 | self.xyz2key(zero, r256, zero, 8), |
| 23 | self.xyz2key(zero, zero, r256, 8), |
| 24 | ) |
| 25 | } |
| 26 | self._decode = {device: self.key2xyz(r512, 9)} |
| 27 | |
| 28 | def encode_lut(self, device=torch.device("cpu")): |
| 29 | if device not in self._encode: |
| 30 | cpu = torch.device("cpu") |
| 31 | self._encode[device] = tuple(e.to(device) for e in self._encode[cpu]) |
| 32 | return self._encode[device] |
| 33 | |
| 34 | def decode_lut(self, device=torch.device("cpu")): |
| 35 | if device not in self._decode: |
| 36 | cpu = torch.device("cpu") |
| 37 | self._decode[device] = tuple(e.to(device) for e in self._decode[cpu]) |
| 38 | return self._decode[device] |
| 39 | |
| 40 | def xyz2key(self, x, y, z, depth): |
| 41 | key = torch.zeros_like(x) |
| 42 | for i in range(depth): |
| 43 | mask = 1 << i |
| 44 | key = ( |
| 45 | key |
| 46 | | ((x & mask) << (2 * i + 2)) |
| 47 | | ((y & mask) << (2 * i + 1)) |
| 48 | | ((z & mask) << (2 * i + 0)) |
| 49 | ) |
| 50 | return key |
| 51 | |
| 52 | def key2xyz(self, key, depth): |
| 53 | x = torch.zeros_like(key) |
| 54 | y = torch.zeros_like(key) |
| 55 | z = torch.zeros_like(key) |
| 56 | for i in range(depth): |
| 57 | x = x | ((key & (1 << (3 * i + 2))) >> (2 * i + 2)) |
| 58 | y = y | ((key & (1 << (3 * i + 1))) >> (2 * i + 1)) |
| 59 | z = z | ((key & (1 << (3 * i + 0))) >> (2 * i + 0)) |
| 60 | return x, y, z |
| 61 | |
| 62 | |
| 63 | _key_lut = KeyLUT() |
| 64 | |
| 65 | |
| 66 | def xyz2key( |
| 67 | x: torch.Tensor, |
| 68 | y: torch.Tensor, |
| 69 | z: torch.Tensor, |
| 70 | b: Optional[Union[torch.Tensor, int]] = None, |
| 71 | depth: int = 16, |
| 72 | ): |
| 73 | r"""Encodes :attr:`x`, :attr:`y`, :attr:`z` coordinates to the shuffled keys |
| 74 | based on pre-computed look up tables. The speed of this function is much |
| 75 | faster than the method based on for-loop. |
| 76 | |
| 77 | Args: |
| 78 | x (torch.Tensor): The x coordinate. |
| 79 | y (torch.Tensor): The y coordinate. |
| 80 | z (torch.Tensor): The z coordinate. |
| 81 | b (torch.Tensor or int): The batch index of the coordinates, and should be |
| 82 | smaller than 32768. If :attr:`b` is :obj:`torch.Tensor`, the size of |
| 83 | :attr:`b` must be the same as :attr:`x`, :attr:`y`, and :attr:`z`. |
| 84 | depth (int): The depth of the shuffled key, and must be smaller than 17 (< 17). |
| 85 | """ |
| 86 | |
| 87 | EX, EY, EZ = _key_lut.encode_lut(x.device) |
| 88 | x, y, z = x.long(), y.long(), z.long() |
| 89 | |
| 90 | mask = 255 if depth > 8 else (1 << depth) - 1 |
| 91 | key = EX[x & mask] | EY[y & mask] | EZ[z & mask] |
| 92 | if depth > 8: |
| 93 | mask = (1 << (depth - 8)) - 1 |
| 94 | key16 = EX[(x >> 8) & mask] | EY[(y >> 8) & mask] | EZ[(z >> 8) & mask] |
| 95 | key = key16 << 24 | key |
| 96 | |
| 97 | if b is not None: |
| 98 | b = b.long() |
| 99 | key = b << 48 | key |
| 100 | |
| 101 | return key |
| 102 | |
| 103 | |
| 104 | def key2xyz(key: torch.Tensor, depth: int = 16): |
| 105 | r"""Decodes the shuffled key to :attr:`x`, :attr:`y`, :attr:`z` coordinates |
| 106 | and the batch index based on pre-computed look up tables. |
| 107 | |
| 108 | Args: |
| 109 | key (torch.Tensor): The shuffled key. |
| 110 | depth (int): The depth of the shuffled key, and must be smaller than 17 (< 17). |
| 111 | """ |
| 112 | |
| 113 | DX, DY, DZ = _key_lut.decode_lut(key.device) |
| 114 | x, y, z = torch.zeros_like(key), torch.zeros_like(key), torch.zeros_like(key) |
| 115 | |
| 116 | b = key >> 48 |
| 117 | key = key & ((1 << 48) - 1) |
| 118 | |
| 119 | n = (depth + 2) // 3 |
| 120 | for i in range(n): |
| 121 | k = key >> (i * 9) & 511 |
| 122 | x = x | (DX[k] << (i * 3)) |
| 123 | y = y | (DY[k] << (i * 3)) |
| 124 | z = z | (DZ[k] << (i * 3)) |
| 125 | |
| 126 | return x, y, z, b |