"""
Implementation of an OmniGraph node that runs a simple deformer on the GPU
"""

from contextlib import suppress

with suppress(ImportError):
    import torch


class OgnDeformerGpu:
    @staticmethod
    def compute(db) -> bool:
        try:
            # TODO: This method of GPU compute isn't recommended. Use CPU compute or Warp.
            if torch:
                input_points = db.inputs.points
                multiplier = db.inputs.multiplier
                db.outputs.points_size = input_points.shape[0]
                # Nothing to evaluate if there are no input points
                if db.outputs.points_size == 0:
                    return True
                output_points = db.outputs.points

                pt = input_points.copy()
                tx = (pt[:, 0] - 310.0) / 310.0
                ty = (pt[:, 1] - 310.0) / 310.0

                disp = 10.0 * (torch.sin(tx * 10.0) + torch.cos(ty * 15.0))
                pt[:, 2] += disp * multiplier

                output_points[:] = pt[:]
                return True
        except NameError:
            db.log_warning("Torch not found, no compute")
        return False
