"""LCM type definitions
This file automatically generated by lcm.
DO NOT MODIFY BY HAND!!!!
"""

try:
    import cStringIO.StringIO as BytesIO
except ImportError:
    from io import BytesIO
import struct

import compas_lcmtypes.stdlcm.header_t

class camera_info(object):
    __slots__ = ["header", "height", "width", "distortion_model", "dsize", "D", "K", "R", "P", "binning_x", "binning_y", "roi", "roi_do_rectify"]

    __typenames__ = ["compas_lcmtypes.stdlcm.header_t", "int32_t", "int32_t", "string", "int32_t", "double", "double", "double", "double", "int32_t", "int32_t", "int32_t", "boolean"]

    __dimensions__ = [None, None, None, None, None, ["dsize"], [9], [9], [12], None, None, [4], None]

    def __init__(self):
        self.header = compas_lcmtypes.stdlcm.header_t()
        self.height = 0
        self.width = 0
        self.distortion_model = ""
        self.dsize = 0
        self.D = []
        self.K = [ 0.0 for dim0 in range(9) ]
        self.R = [ 0.0 for dim0 in range(9) ]
        self.P = [ 0.0 for dim0 in range(12) ]
        self.binning_x = 0
        self.binning_y = 0
        self.roi = [ 0 for dim0 in range(4) ]
        self.roi_do_rectify = False

    def encode(self):
        buf = BytesIO()
        buf.write(camera_info._get_packed_fingerprint())
        self._encode_one(buf)
        return buf.getvalue()

    def _encode_one(self, buf):
        assert self.header._get_packed_fingerprint() == compas_lcmtypes.stdlcm.header_t._get_packed_fingerprint()
        self.header._encode_one(buf)
        buf.write(struct.pack(">ii", self.height, self.width))
        __distortion_model_encoded = self.distortion_model.encode('utf-8')
        buf.write(struct.pack('>I', len(__distortion_model_encoded)+1))
        buf.write(__distortion_model_encoded)
        buf.write(b"\0")
        buf.write(struct.pack(">i", self.dsize))
        buf.write(struct.pack('>%dd' % self.dsize, *self.D[:self.dsize]))
        buf.write(struct.pack('>9d', *self.K[:9]))
        buf.write(struct.pack('>9d', *self.R[:9]))
        buf.write(struct.pack('>12d', *self.P[:12]))
        buf.write(struct.pack(">ii", self.binning_x, self.binning_y))
        buf.write(struct.pack('>4i', *self.roi[:4]))
        buf.write(struct.pack(">b", self.roi_do_rectify))

    def decode(data):
        if hasattr(data, 'read'):
            buf = data
        else:
            buf = BytesIO(data)
        if buf.read(8) != camera_info._get_packed_fingerprint():
            raise ValueError("Decode error")
        return camera_info._decode_one(buf)
    decode = staticmethod(decode)

    def _decode_one(buf):
        self = camera_info()
        self.header = compas_lcmtypes.stdlcm.header_t._decode_one(buf)
        self.height, self.width = struct.unpack(">ii", buf.read(8))
        __distortion_model_len = struct.unpack('>I', buf.read(4))[0]
        self.distortion_model = buf.read(__distortion_model_len)[:-1].decode('utf-8', 'replace')
        self.dsize = struct.unpack(">i", buf.read(4))[0]
        self.D = struct.unpack('>%dd' % self.dsize, buf.read(self.dsize * 8))
        self.K = struct.unpack('>9d', buf.read(72))
        self.R = struct.unpack('>9d', buf.read(72))
        self.P = struct.unpack('>12d', buf.read(96))
        self.binning_x, self.binning_y = struct.unpack(">ii", buf.read(8))
        self.roi = struct.unpack('>4i', buf.read(16))
        self.roi_do_rectify = bool(struct.unpack('b', buf.read(1))[0])
        return self
    _decode_one = staticmethod(_decode_one)

    def _get_hash_recursive(parents):
        if camera_info in parents: return 0
        newparents = parents + [camera_info]
        tmphash = (0x7edeeeafed8d83c0+ compas_lcmtypes.stdlcm.header_t._get_hash_recursive(newparents)) & 0xffffffffffffffff
        tmphash  = (((tmphash<<1)&0xffffffffffffffff) + (tmphash>>63)) & 0xffffffffffffffff
        return tmphash
    _get_hash_recursive = staticmethod(_get_hash_recursive)
    _packed_fingerprint = None

    def _get_packed_fingerprint():
        if camera_info._packed_fingerprint is None:
            camera_info._packed_fingerprint = struct.pack(">Q", camera_info._get_hash_recursive([]))
        return camera_info._packed_fingerprint
    _get_packed_fingerprint = staticmethod(_get_packed_fingerprint)

    def get_hash(self):
        """Get the LCM hash of the struct"""
        return struct.unpack(">Q", camera_info._get_packed_fingerprint())[0]

