Exemplo n.º 1
0
    def read_boundin_box(self, ret_fd=False, ret_dim=False):
        fd = open(self.filename, "r")

        while 1:
            try:
                line = fd.readline()
            except:
                output("reading " + fd.name + " failed!")
                raise
            if len(line) == 0:
                break
            if len(line) == 1:
                continue
            if line[0] == "#":
                continue
            aux = line.split()
            if aux[0] == "Dimension":
                if len(aux) == 2:
                    dim = int(aux[1])
                else:
                    dim = int(fd.readline())
            elif aux[0] == "Vertices":
                num = int(read_token(fd))
                nod = read_array(fd, num, dim + 1, nm.float64)
                break

        bbox = [[nod[0][0]] * 2, [nod[0][1]] * 2, [nod[0][2]] * 2]

        for inod in nod[1:]:
            for idim in range(dim):
                if inod[idim] < bbox[idim][0]:
                    bbox[idim][0] = inod[idim]
                if inod[idim] > bbox[idim][1]:
                    bbox[idim][1] = inod[idim]

        if ret_dim:
            if ret_fd:
                return bbox, dim, fd
            else:
                fd.close()
                return bbox, dim
        else:
            if ret_fd:
                return bbox, fd
            else:
                fd.close()
                return bbox
Exemplo n.º 2
0
    def read(self, mesh, **kwargs):
        dim, fd = self.read_dimension(ret_fd=True)

        conns_in = []
        descs = []
        while 1:
            try:
                line = fd.readline()
                if len(line) == 0:
                    break
                if len(line) == 1:
                    continue
            except EOFError:
                break
            except:
                output("reading " + fd.name + " failed!")
                raise
            ls = line.strip()
            if ls == "Vertices":
                num = int(read_token(fd))
                nod = read_array(fd, num, dim + 1, nm.float64)
            ##                 print nod
            elif ls == "Tetrahedra":
                num = int(read_token(fd))
                conns_in.append(read_array(fd, num, 5, nm.int32))
                conns_in[-1][:, :-1] -= 1
                descs.append("3_4")
            elif ls == "Hexahedra":
                num = int(read_token(fd))
                conns_in.append(read_array(fd, num, 9, nm.int32))
                conns_in[-1][:, :-1] -= 1
                descs.append("3_8")
            elif ls == "Triangles":
                num = int(read_token(fd))
                conns_in.append(read_array(fd, num, 4, nm.int32))
                conns_in[-1][:, :-1] -= 1
                descs.append("2_3")
            elif ls == "Quadrilaterals":
                num = int(read_token(fd))
                conns_in.append(read_array(fd, num, 5, nm.int32))
                conns_in[-1][:, :-1] -= 1
                descs.append("2_4")
            elif ls == "End":
                break
            elif line[0] == "#":
                continue
            else:
                msg = "corrupted file (line '%s')!" % line
                raise ValueError(msg)
        fd.close()

        conns_in, mat_ids = sort_by_mat_id(conns_in)

        # Detect wedges and pyramides -> separate groups.
        if "3_8" in descs:
            ic = descs.index("3_8")

            conn_in = conns_in.pop(ic)
            flag = nm.zeros((conn_in.shape[0],), nm.int32)
            for ii, el in enumerate(conn_in):
                if el[4] == el[5]:
                    if el[5] == el[6]:
                        flag[ii] = 2
                    else:
                        flag[ii] = 1

            conn = []
            desc = []

            ib = nm.where(flag == 0)[0]
            if len(ib) > 0:
                conn.append(conn_in[ib])
                desc.append("3_8")

            iw = nm.where(flag == 1)[0]
            if len(iw) > 0:
                ar = nm.array([0, 1, 2, 3, 4, 6, 8], nm.int32)
                conn.append(la.rect(conn_in, iw, ar))
                desc.append("3_6")

            ip = nm.where(flag == 2)[0]
            if len(ip) > 0:
                ar = nm.array([0, 1, 2, 3, 4, 8], nm.int32)
                conn.append(la.rect(conn_in, ip, ar))
                desc.append("3_5")

            ##             print "brick split:", ic, ":", ib, iw, ip, desc

            conns_in[ic:ic] = conn
            del (descs[ic])
            descs[ic:ic] = desc

        conns, mat_ids, descs = split_by_mat_id(conns_in, mat_ids, descs)
        mesh._set_data(nod, conns, mat_ids, descs)

        return mesh