|
25 | 25 | EPS = 1e-6
|
26 | 26 |
|
27 | 27 |
|
| 28 | +def shift(cloud: Union[torch.Tensor, PointCloud], |
| 29 | + shf: Union[float, int, torch.Tensor], |
| 30 | + inplace: Optional[bool] = True): |
| 31 | + """Shift the input pointcloud by a shift factor. |
| 32 | +
|
| 33 | + Args: |
| 34 | + cloud (torch.Tensor or kaolin.rep.PointCloud): pointcloud (ndims >= 2). |
| 35 | + shf (float, int, torch.Tensor): shift factor (scaler, or tensor). |
| 36 | + inplace (bool, optional): Bool to make the transform in-place |
| 37 | + |
| 38 | + Returns: |
| 39 | + (torch.Tensor): shifted pointcloud pf the same shape as input. |
| 40 | + |
| 41 | + Shape: |
| 42 | + - cloud: :math:`(B x N x D)` (or) :math:`(N x D)`, where :math:`(B)` |
| 43 | + is the batchsize, :math:`(N)` is the number of points per cloud, |
| 44 | + and :math:`(D)` is the dimensionality of each cloud. |
| 45 | + - shf: :math:`(1)` or :math:`(B)`. |
| 46 | +
|
| 47 | + Example: |
| 48 | + >>> points = torch.rand(1000,3) |
| 49 | + >>> points2 = shift(points, torch.FloatTensor([3])) |
| 50 | + """ |
| 51 | + |
| 52 | + if isinstance(cloud, np.ndarray): |
| 53 | + cloud = torch.from_numpy(cloud) |
| 54 | + |
| 55 | + if isinstance(shf, np.ndarray): |
| 56 | + shf = torch.from_numpy(shf) |
| 57 | + |
| 58 | + if isinstance(cloud, PointCloud): |
| 59 | + cloud = cloud.points |
| 60 | + |
| 61 | + if isinstance(shf, int) or isinstance(shf, float): |
| 62 | + shf = torch.Tensor([shf]).to(cloud.device) |
| 63 | + |
| 64 | + helpers._assert_tensor(cloud) |
| 65 | + helpers._assert_tensor(shf) |
| 66 | + helpers._assert_dim_ge(cloud, 2) |
| 67 | + helpers._assert_gt(shf, 0.) |
| 68 | + |
| 69 | + if not inplace: |
| 70 | + cloud = cloud.clone() |
| 71 | + |
| 72 | + return shf + cloud |
| 73 | + |
| 74 | + |
28 | 75 | def scale(cloud: Union[torch.Tensor, PointCloud],
|
29 | 76 | scf: Union[float, int, torch.Tensor],
|
30 | 77 | inplace: Optional[bool] = True):
|
@@ -74,6 +121,56 @@ def scale(cloud: Union[torch.Tensor, PointCloud],
|
74 | 121 | return scf * cloud
|
75 | 122 |
|
76 | 123 |
|
| 124 | +def translate(cloud: Union[torch.Tensor, PointCloud], tranmat: torch.Tensor, |
| 125 | + inplace: Optional[bool] = True): |
| 126 | + """Translate the input pointcloud by a translation matrix. |
| 127 | +
|
| 128 | + Args: |
| 129 | + cloud (Tensor or np.array): pointcloud (ndims = 2 or 3) |
| 130 | + tranmat (Tensor or np.array): translation matrix (1 x 3, 1 per cloud). |
| 131 | +
|
| 132 | + Returns: |
| 133 | + cloud_tran (Tensor): Translated pointcloud of the same shape as input. |
| 134 | +
|
| 135 | + Shape: |
| 136 | + - cloud: :math:`(B x N x 3)` (or) :math:`(N x 3)`, where :math:`(B)` |
| 137 | + is the batchsize, :math:`(N)` is the number of points per cloud, |
| 138 | + and :math:`(3)` is the dimensionality of each cloud. |
| 139 | + - tranmat: :math:`(1, 3)` or :math:`(B, 1, 3)`. |
| 140 | +
|
| 141 | + Example: |
| 142 | + >>> points = torch.rand(1000,3) |
| 143 | + >>> t_mat = torch.rand(1,3) |
| 144 | + >>> points2 = translate(points, t_mat) |
| 145 | +
|
| 146 | + """ |
| 147 | + if isinstance(cloud, np.ndarray): |
| 148 | + cloud = torch.from_numpy(cloud) |
| 149 | + if isinstance(cloud, PointCloud): |
| 150 | + cloud = cloud.points |
| 151 | + if isinstance(tranmat, np.ndarray): |
| 152 | + trainmat = torch.from_numpy(tranmat) |
| 153 | + |
| 154 | + helpers._assert_tensor(cloud) |
| 155 | + helpers._assert_tensor(tranmat) |
| 156 | + helpers._assert_dim_ge(cloud, 2) |
| 157 | + helpers._assert_dim_ge(tranmat, 2) |
| 158 | + # Rotation matrix must have last two dimensions of shape 3. |
| 159 | + helpers._assert_shape_eq(tranmat, (1, 3), dim=-1) |
| 160 | + helpers._assert_shape_eq(tranmat, (1, 3), dim=-2) |
| 161 | + |
| 162 | + if not inplace: |
| 163 | + cloud = cloud.clone() |
| 164 | + |
| 165 | + if tranmat.dim() == 2 and cloud.dim() == 2: |
| 166 | + cloud = torch.add(tranmat, cloud) |
| 167 | + else: |
| 168 | + if tranmat.dim() == 2: |
| 169 | + tranmat = tranmat.expand(cloud.shape[0], 1, 3) |
| 170 | + cloud = torch.add(tranmat, cloud) |
| 171 | + |
| 172 | + return cloud |
| 173 | + |
77 | 174 | def rotate(cloud: Union[torch.Tensor, PointCloud], rotmat: torch.Tensor,
|
78 | 175 | inplace: Optional[bool] = True):
|
79 | 176 | """Rotates the the input pointcloud by a rotation matrix.
|
|
0 commit comments