from .core import add_acast, acast

try:
    import open3d as o3d
    import numpy as np

    def numpy_array_to_vector3d(x):
        array_shape = x.shape
        if len(array_shape) != 2 or array_shape[-1] != 3:
            assert array_shape[-1] == 3, "Input array must have shape (..., 3)"
            x = x.reshape(-1, 3)
        return o3d.utility.Vector3dVector(np.asarray(x))

    add_acast(np.ndarray, o3d.utility.Vector3dVector, numpy_array_to_vector3d)

    def tuple_to_pointcloud(x):
        assert isinstance(x, (tuple, list)) and len(x) == 2, "Input must be (points, colors)"
        pts, cols = x
        pcd = o3d.geometry.PointCloud()
        pcd.points = acast(pts, o3d.utility.Vector3dVector)
        pcd.colors = acast(cols, o3d.utility.Vector3dVector)
        return pcd

    add_acast(tuple, o3d.geometry.PointCloud, tuple_to_pointcloud)
    add_acast(list, o3d.geometry.PointCloud, tuple_to_pointcloud)

except ImportError:
    pass
