Esempio n. 1
0
def parse_transform(node):
    ret = torch.eye(4)
    for child in node:
        if child.tag == 'matrix':
            value = torch.from_numpy(\
                np.reshape(\
                    # support both ',' and ' ' seperator

                    np.fromstring(child.attrib['value'], dtype=np.float32, sep=',' if ',' in child.attrib['value'] else ' '),
                    (4, 4)))
            ret = value @ ret
        elif child.tag == 'translate':
            x = float(child.attrib['x'])
            y = float(child.attrib['y'])
            z = float(child.attrib['z'])
            value = transform.gen_translate_matrix(torch.tensor([x, y, z]))
            ret = value @ ret
        elif child.tag == 'scale':
            # single scale value
            if 'value' in child.attrib:
                x = y = z = float(child.attrib['value'])
            else:
                x = float(child.attrib['x'])
                y = float(child.attrib['y'])
                z = float(child.attrib['z'])
            value = transform.gen_scale_matrix(torch.tensor([x, y, z]))
            ret = value @ ret
    return ret
Esempio n. 2
0
def parse_transform(node, param_dict):
    ret = torch.eye(4)
    for child in node:
        if child.tag == 'matrix':
            value = torch.from_numpy(\
                np.reshape(\
                    # support both ',' and ' ' seperator
                    np.fromstring(child.attrib['value'], dtype=np.float32, sep=',' if ',' in child.attrib['value'] else ' '),
                    (4, 4)))
            ret = value @ ret
        elif child.tag == 'translate':
            x = float(check_default(child.attrib['x'], param_dict))
            y = float(check_default(child.attrib['y'], param_dict))
            z = float(check_default(child.attrib['z'], param_dict))
            value = transform.gen_translate_matrix(torch.tensor([x, y, z]))
            ret = value @ ret
        elif child.tag == 'scale':
            # single scale value
            if 'value' in child.attrib:
                x = y = z = float(child.attrib['value'])
            else:
                x = float(check_default(child.attrib['x'], param_dict))
                y = float(check_default(child.attrib['y'], param_dict))
                z = float(check_default(child.attrib['z'], param_dict))
            value = transform.gen_scale_matrix(torch.tensor([x, y, z]))
            ret = value @ ret
        elif child.tag == 'rotate':
            x = float(check_default(child.attrib['x'], param_dict)) if 'x' in child.attrib else 0.0
            y = float(check_default(child.attrib['y'], param_dict)) if 'y' in child.attrib else 0.0
            z = float(check_default(child.attrib['z'], param_dict)) if 'z' in child.attrib else 0.0
            angle = transform.radians(float(check_default(child.attrib['angle'], param_dict)))
            axis = np.array([x, y, z])
            axis = axis / np.linalg.norm(axis)
            cos_theta = math.cos(angle)
            sin_theta = math.sin(angle)
            mat = torch.zeros(4, 4)
            mat[0, 0] = axis[0] * axis[0] + (1.0 - axis[0] * axis[0]) * cos_theta
            mat[0, 1] = axis[0] * axis[1] * (1.0 - cos_theta) - axis[2] * sin_theta
            mat[0, 2] = axis[0] * axis[2] * (1.0 - cos_theta) + axis[1] * sin_theta

            mat[1, 0] = axis[0] * axis[1] * (1.0 - cos_theta) + axis[2] * sin_theta
            mat[1, 1] = axis[1] * axis[1] + (1.0 - axis[1] * axis[1]) * cos_theta
            mat[1, 2] = axis[1] * axis[2] * (1.0 - cos_theta) - axis[0] * sin_theta

            mat[2, 0] = axis[0] * axis[2] * (1.0 - cos_theta) - axis[1] * sin_theta
            mat[2, 1] = axis[1] * axis[2] * (1.0 - cos_theta) + axis[0] * sin_theta
            mat[2, 2] = axis[2] * axis[2] + (1.0 - axis[2] * axis[2]) * cos_theta

            mat[3, 3] = 1.0
            ret = mat @ ret
    return ret
Esempio n. 3
0
def parse_transform(node):
    ret = torch.eye(4)
    for child in node:
        if child.tag == 'matrix':
            value = torch.from_numpy(\
                np.reshape(\
                    np.fromstring(child.attrib['value'], dtype=np.float32, sep=' '),
                    (4, 4)))
            ret = value @ ret
        elif child.tag == 'translate':
            x = float(child.attrib['x'])
            y = float(child.attrib['y'])
            z = float(child.attrib['z'])
            value = transform.gen_translate_matrix(torch.tensor([x, y, z]))
            ret = value @ ret
        elif child.tag == 'scale':
            x = float(child.attrib['x'])
            y = float(child.attrib['y'])
            z = float(child.attrib['z'])
            value = transform.gen_scale_matrix(torch.tensor([x, y, z]))
            ret = value @ ret
    return ret