|
| 1 | +"""Module defining file formats that point cloud data can be exported to.""" |
| 2 | + |
| 3 | +import _zivid |
| 4 | + |
| 5 | + |
| 6 | +class ColorSpace: # pylint: disable=too-few-public-methods |
| 7 | + """Color space for saving point cloud.""" |
| 8 | + |
| 9 | + linear_rgb = "linear_rgb" |
| 10 | + srgb = "srgb" |
| 11 | + |
| 12 | + @staticmethod |
| 13 | + def valid_values(): |
| 14 | + """Get valid values for color space. |
| 15 | +
|
| 16 | + Returns: |
| 17 | + List of valid color spaces. |
| 18 | + """ |
| 19 | + return [ColorSpace.linear_rgb, ColorSpace.srgb] |
| 20 | + |
| 21 | + @classmethod |
| 22 | + def _to_internal(cls, value): |
| 23 | + if value == ColorSpace.linear_rgb: |
| 24 | + return _zivid.point_cloud_export.ColorSpace.linear_rgb |
| 25 | + if value == ColorSpace.srgb: |
| 26 | + return _zivid.point_cloud_export.ColorSpace.srgb |
| 27 | + raise ValueError( |
| 28 | + "Invalid color space '{}'. Valid color spaces are: {}".format( |
| 29 | + value, cls.valid_values() |
| 30 | + ) |
| 31 | + ) |
| 32 | + |
| 33 | + |
| 34 | +class ZDF: # pylint: disable=too-few-public-methods |
| 35 | + """Specification for saving frame in ZDF (*.zdf) format.""" |
| 36 | + |
| 37 | + def __init__(self, file_name): |
| 38 | + """Create a ZDF file format specification with file name. |
| 39 | +
|
| 40 | + Args: |
| 41 | + file_name: File name. |
| 42 | +
|
| 43 | + Raises: |
| 44 | + TypeError: If file_name is not a string. |
| 45 | + """ |
| 46 | + if not isinstance(file_name, str): |
| 47 | + raise TypeError( |
| 48 | + "Unsupported type for argument file_name. Got {}, expected {}".format( |
| 49 | + type(file_name), str |
| 50 | + ) |
| 51 | + ) |
| 52 | + self.__impl = _zivid.point_cloud_export.file_format.ZDF(file_name) |
| 53 | + |
| 54 | + def __str__(self): |
| 55 | + return str(self.__impl) |
| 56 | + |
| 57 | + |
| 58 | +class PLY: # pylint: disable=too-few-public-methods |
| 59 | + """Specification for saving frame in PLY (*.ply) format. |
| 60 | +
|
| 61 | + PLY is a file format developed at Stanford. To learn more about the PLY file format, |
| 62 | + see https://paulbourke.net/dataformats/ply/. |
| 63 | + """ |
| 64 | + |
| 65 | + class Layout: |
| 66 | + """Layout for saving point cloud.""" |
| 67 | + |
| 68 | + ordered = "ordered" |
| 69 | + unordered = "unordered" |
| 70 | + |
| 71 | + @staticmethod |
| 72 | + def valid_values(): |
| 73 | + """Get valid values for layout. |
| 74 | +
|
| 75 | + Returns: |
| 76 | + List of valid layouts. |
| 77 | + """ |
| 78 | + return [PLY.Layout.ordered, PLY.Layout.unordered] |
| 79 | + |
| 80 | + @classmethod |
| 81 | + def _to_internal(cls, value): |
| 82 | + if value == PLY.Layout.ordered: |
| 83 | + return _zivid.point_cloud_export.file_format.PLY.Layout.ordered |
| 84 | + if value == PLY.Layout.unordered: |
| 85 | + return _zivid.point_cloud_export.file_format.PLY.Layout.unordered |
| 86 | + raise ValueError( |
| 87 | + "Invalid layout '{}'. Valid layouts are: {}".format( |
| 88 | + value, cls.valid_values() |
| 89 | + ) |
| 90 | + ) |
| 91 | + |
| 92 | + def __init__(self, file_name, layout=Layout.ordered, color_space=ColorSpace.srgb): |
| 93 | + """Create a PLY file format specification with file name. |
| 94 | +
|
| 95 | + Args: |
| 96 | + file_name: File name. |
| 97 | + layout: Layout of point cloud. Default is ordered. |
| 98 | + color_space: Color space of point cloud. Default is sRGB. |
| 99 | +
|
| 100 | + Raises: |
| 101 | + TypeError: If file_name, layout, or color_space are not strings. |
| 102 | + """ |
| 103 | + if not isinstance(file_name, str): |
| 104 | + raise TypeError( |
| 105 | + "Unsupported type for argument file_name. Got {}, expected {}".format( |
| 106 | + type(file_name), str |
| 107 | + ) |
| 108 | + ) |
| 109 | + if not isinstance(layout, str): |
| 110 | + raise TypeError( |
| 111 | + "Unsupported type for argument layout. Got {}, expected {}".format( |
| 112 | + type(layout), str |
| 113 | + ) |
| 114 | + ) |
| 115 | + if not isinstance(color_space, str): |
| 116 | + raise TypeError( |
| 117 | + "Unsupported type for argument color_space. Got {}, expected {}".format( |
| 118 | + type(color_space), str |
| 119 | + ) |
| 120 | + ) |
| 121 | + self.__impl = _zivid.point_cloud_export.file_format.PLY( |
| 122 | + file_name, |
| 123 | + PLY.Layout._to_internal(layout), |
| 124 | + ColorSpace._to_internal(color_space), |
| 125 | + ) |
| 126 | + |
| 127 | + def __str__(self): |
| 128 | + return str(self.__impl) |
| 129 | + |
| 130 | + |
| 131 | +class XYZ: # pylint: disable=too-few-public-methods |
| 132 | + """Specification for saving frame in ASCII (*.xyz) format. |
| 133 | +
|
| 134 | + ASCII characters are used to store cartesian coordinates of XYZ points and RGB color values. |
| 135 | + """ |
| 136 | + |
| 137 | + def __init__(self, file_name, color_space=ColorSpace.srgb): |
| 138 | + """Create a XYZ file format specification with file name. |
| 139 | +
|
| 140 | + Sets color space to linear RGB. |
| 141 | +
|
| 142 | + Args: |
| 143 | + file_name: File name. |
| 144 | + color_space: Color space of point cloud. Default is sRGB. |
| 145 | +
|
| 146 | + Raises: |
| 147 | + TypeError: If file_name or color_space are not strings. |
| 148 | + """ |
| 149 | + if not isinstance(file_name, str): |
| 150 | + raise TypeError( |
| 151 | + "Unsupported type for argument file_name. Got {}, expected {}".format( |
| 152 | + type(file_name), str |
| 153 | + ) |
| 154 | + ) |
| 155 | + if not isinstance(color_space, str): |
| 156 | + raise TypeError( |
| 157 | + "Unsupported type for argument color_space. Got {}, expected {}".format( |
| 158 | + type(color_space), str |
| 159 | + ) |
| 160 | + ) |
| 161 | + self.__impl = _zivid.point_cloud_export.file_format.XYZ( |
| 162 | + file_name, ColorSpace._to_internal(color_space) |
| 163 | + ) |
| 164 | + |
| 165 | + def __str__(self): |
| 166 | + return str(self.__impl) |
| 167 | + |
| 168 | + |
| 169 | +class PCD: # pylint: disable=too-few-public-methods |
| 170 | + """Specification for saving frame in PCD (*.pcd) format. |
| 171 | +
|
| 172 | + PCD is a file format native to the Point Cloud Library (PCL). To learn more about |
| 173 | + the PCD file format, see |
| 174 | + https://pcl.readthedocs.io/projects/tutorials/en/latest/pcd_file_format.html#pcd-file-format. |
| 175 | + """ |
| 176 | + |
| 177 | + def __init__(self, file_name, color_space=ColorSpace.srgb): |
| 178 | + """Create a PCD file format specification with file name. |
| 179 | +
|
| 180 | + Args: |
| 181 | + file_name: File name. |
| 182 | + color_space: Color space of point cloud. Default is sRGB. |
| 183 | +
|
| 184 | + Raises: |
| 185 | + TypeError: If file_name or color_space are not strings. |
| 186 | + """ |
| 187 | + if not isinstance(file_name, str): |
| 188 | + raise TypeError( |
| 189 | + "Unsupported type for argument file_name. Got {}, expected {}".format( |
| 190 | + type(file_name), str |
| 191 | + ) |
| 192 | + ) |
| 193 | + if not isinstance(color_space, str): |
| 194 | + raise TypeError( |
| 195 | + "Unsupported type for argument color_space. Got {}, expected {}".format( |
| 196 | + type(color_space), str |
| 197 | + ) |
| 198 | + ) |
| 199 | + self.__impl = _zivid.point_cloud_export.file_format.PCD( |
| 200 | + file_name, |
| 201 | + ColorSpace._to_internal(color_space), |
| 202 | + ) |
| 203 | + |
| 204 | + def __str__(self): |
| 205 | + return str(self.__impl) |
0 commit comments