code: def crop_or_pad_nd(
      img: torch.Tensor,
      translation_mat,
      spatial_size: tuple[int, ...],
      mode: str,
      **kwargs,
  ):
      """
      Crop or pad using the translation matrix and spatial size. The translation coefficients are rounded
      to the nearest integers. For a more generic implementation, please see :py:class:`monai.transforms.SpatialResample`.
  
      Args:
          img: data to be transformed, assuming `img` is channel-first and padding doesn't apply to the channel dim.
          translation_mat: the translation matrix to be applied to the image. A translation matrix generated by,
              for example, :py:func:`monai.transforms.utils.create_translate`. The translation coefficients are rounded
              to the nearest integers.
          spatial_size: the spatial size of the output image.
          mode: the padding mode.
          kwargs: other arguments for the `np.pad` or `torch.pad` function.
      """
      ndim = len(img.shape) - 1
      matrix_np = np.round(
          to_affine_nd(
              ndim,
              convert_to_numpy(
                  translation_mat, wrap_sequence=True
              ).copy(),
          )
      )
      matrix_np = to_affine_nd(len(spatial_size), matrix_np)
      cc = np.asarray(
          np.meshgrid(
              *[[0.5, x - 0.5] for x in spatial_size], indexing="ij"
          )
      )
      cc = cc.reshape((len(spatial_size), -1))
      src_cc = np.floor(
          matrix_np @ np.concatenate((cc, np.ones_like(cc[:1])))
      )
      src_start, src_end = src_cc.min(axis=1), src_cc.max(axis=1)
      to_pad, to_crop, do_pad, do_crop = (
          [(0, 0)],
          [slice(None)],
          False,
          False,
      )
      for s, e, sp in zip(src_start, src_end, img.shape[1:]):
          do_pad, do_crop = (
              do_pad or s < 0 or e > sp - 1,
              do_crop or s > 0 or e < sp - 1,
          )
          to_pad += [
              (
                  0 if s >= 0 else int(-s),
                  0 if e < sp - 1 else int(e - sp + 1),
              )
          ]
          to_crop += [slice(int(max(s, 0)), int(e + 1 + to_pad[-1][0]))]
      if do_pad:
          _mode = _convert_pt_pad_mode(mode)
          img = pad_nd(img, to_pad, mode=_mode, **kwargs)
      if do_crop:
          img = img[to_crop]
      return img
  
file_path: MONAI/monai/transforms/croppad/functional.py
imports: from __future__ import annotations
  import warnings
  import numpy
  import torch
  from torch.nn.functional import pad
  from monai.config.type_definitions import NdarrayTensor
  from monai.data.meta_obj import get_track_meta
  from monai.data.meta_tensor import MetaTensor
  from monai.data.utils import to_affine_nd
  from monai.transforms.inverse import TraceableTransform
  from monai.transforms.utils import convert_pad_mode, create_translate
  from monai.utils import PytorchPadMode, convert_to_dst_type, convert_to_numpy, convert_to_tensor, ensure_tuple
label_desc: Use image node_id 5 for calling a global function with example usage: crop_or_pad_nd(img, translation_mat, spatial_size, mode) and returns: img