apply affine transformation to a set of points. Args: data: input data to apply affine transformation, should be a tensor of shape (C, N, 2 or 3), where C represents the number of channels and N denotes the number of points. affine: affine matrix to be applied,
(data: torch.Tensor, affine: torch.Tensor, dtype: DtypeLike | torch.dtype | None = None)
| 2580 | |
| 2581 | |
| 2582 | def apply_affine_to_points(data: torch.Tensor, affine: torch.Tensor, dtype: DtypeLike | torch.dtype | None = None): |
| 2583 | """ |
| 2584 | apply affine transformation to a set of points. |
| 2585 | |
| 2586 | Args: |
| 2587 | data: input data to apply affine transformation, should be a tensor of shape (C, N, 2 or 3), |
| 2588 | where C represents the number of channels and N denotes the number of points. |
| 2589 | affine: affine matrix to be applied, should be a tensor of shape (3, 3) or (4, 4). |
| 2590 | dtype: output data dtype. |
| 2591 | """ |
| 2592 | data_: torch.Tensor = convert_to_tensor(data, track_meta=False, dtype=torch.float64) |
| 2593 | affine = to_affine_nd(data_.shape[-1], affine) |
| 2594 | |
| 2595 | homogeneous: torch.Tensor = concatenate((data_, torch.ones((data_.shape[0], data_.shape[1], 1))), axis=2) # type: ignore |
| 2596 | transformed_homogeneous = torch.matmul(homogeneous, affine.T) |
| 2597 | transformed_coordinates = transformed_homogeneous[:, :, :-1] |
| 2598 | out, *_ = convert_to_dst_type(transformed_coordinates, data, dtype=dtype) |
| 2599 | |
| 2600 | return out |
| 2601 | |
| 2602 | |
| 2603 | if __name__ == "__main__": |
no test coverage detected
searching dependent graphs…