|
| 1 | +""" |
| 2 | +I/O for the Wavefront .obj file format, cf. |
| 3 | +<https://en.wikipedia.org/wiki/Wavefront_.obj_file>. |
| 4 | +""" |
| 5 | +import datetime |
| 6 | + |
| 7 | +import numpy as np |
| 8 | + |
| 9 | +# from .._files import open_file |
| 10 | +# from .._helpers import register_format |
| 11 | +# from .._mesh import CellBlock, Mesh |
| 12 | + |
| 13 | +import meshio |
| 14 | + |
| 15 | + |
| 16 | +def read(filename): |
| 17 | + with open(filename, "r") as f: |
| 18 | + mesh = read_buffer(f) |
| 19 | + return mesh |
| 20 | + |
| 21 | + |
| 22 | +def read_buffer(f): |
| 23 | + points = [] |
| 24 | + vertex_normals = [] |
| 25 | + texture_coords = [] |
| 26 | + face_groups = [] |
| 27 | + face_normals = [] |
| 28 | + face_texture_coords = [] |
| 29 | + face_group_ids = [] |
| 30 | + face_group_id = -1 |
| 31 | + while True: |
| 32 | + line = f.readline() |
| 33 | + |
| 34 | + if not line: |
| 35 | + # EOF |
| 36 | + break |
| 37 | + |
| 38 | + strip = line.strip() |
| 39 | + |
| 40 | + if len(strip) == 0 or strip[0] == "#": |
| 41 | + continue |
| 42 | + |
| 43 | + split = strip.split() |
| 44 | + |
| 45 | + if split[0] == "v": |
| 46 | + points.append([float(item) for item in split[1:]]) |
| 47 | + elif split[0] == "vn": |
| 48 | + vertex_normals.append([float(item) for item in split[1:]]) |
| 49 | + elif split[0] == "vt": |
| 50 | + texture_coords.append([float(item) for item in split[1:]]) |
| 51 | + elif split[0] == "s": |
| 52 | + # "s 1" or "s off" controls smooth shading |
| 53 | + pass |
| 54 | + elif split[0] == "f": |
| 55 | + # old: dat = [int(item.split("/")[0]) for item in split[1:]] |
| 56 | + # A face in obj has one of the following formats: 1, 1/2, 1//3, 1/2/3 |
| 57 | + # We want to support all formats now amd store the texture and normal indices in other arrays |
| 58 | + face_indices = [] |
| 59 | + face_texture_indices = [] |
| 60 | + face_normal_indices = [] |
| 61 | + |
| 62 | + for item in split[1:]: |
| 63 | + indices = item.split("/") |
| 64 | + face_indices.append(int(indices[0])) |
| 65 | + if len(indices) > 1 and indices[1] != "": |
| 66 | + face_texture_indices.append(int(indices[1])) |
| 67 | + if len(indices) > 2: |
| 68 | + face_normal_indices.append(int(indices[2])) |
| 69 | + |
| 70 | + if len(face_groups) == 0 or ( |
| 71 | + len(face_groups[-1]) > 0 and len(face_groups[-1][-1]) != len(face_indices) |
| 72 | + ): |
| 73 | + face_groups.append([]) |
| 74 | + face_group_ids.append([]) |
| 75 | + face_texture_coords.append([]) |
| 76 | + face_normals.append([]) |
| 77 | + face_groups[-1].append(face_indices) |
| 78 | + face_group_ids[-1].append(face_group_id) |
| 79 | + if face_texture_indices: |
| 80 | + face_texture_coords[-1].append(face_texture_indices) |
| 81 | + if face_normal_indices: |
| 82 | + face_normals[-1].append(face_normal_indices) |
| 83 | + elif split[0] == "g": |
| 84 | + # new group |
| 85 | + face_groups.append([]) |
| 86 | + face_group_ids.append([]) |
| 87 | + face_texture_coords.append([]) |
| 88 | + face_normals.append([]) |
| 89 | + face_group_id += 1 |
| 90 | + else: |
| 91 | + # who knows |
| 92 | + pass |
| 93 | + |
| 94 | + # There may be empty groups, too. <https://github.com/nschloe/meshio/issues/770> |
| 95 | + # Remove them. |
| 96 | + face_groups = [f for f in face_groups if len(f) > 0] |
| 97 | + face_group_ids = [g for g in face_group_ids if len(g) > 0] |
| 98 | + face_normals = [n for n in face_normals if len(n) > 0] |
| 99 | + face_texture_coords = [t for t in face_texture_coords if len(t) > 0] |
| 100 | + |
| 101 | + # convert to numpy arrays and remove |
| 102 | + points = np.array(points) |
| 103 | + face_groups = [np.array(f) for f in face_groups] |
| 104 | + texture_coords = [np.array(t) for t in texture_coords] |
| 105 | + vertex_normals = [np.array(n) for n in vertex_normals] |
| 106 | + point_data = {} |
| 107 | + cell_data = {} |
| 108 | + field_data = {} |
| 109 | + |
| 110 | + if face_texture_coords and len(texture_coords) == max([max(max(face)) for face in face_texture_coords]): |
| 111 | + field_data["obj:vt"] = texture_coords |
| 112 | + cell_data["obj:vt_face_idx"] = face_texture_coords |
| 113 | + elif len(texture_coords) == len(points): |
| 114 | + point_data["obj:vt"] = texture_coords |
| 115 | + |
| 116 | + if face_normals and len(vertex_normals) == max([max(max(face)) for face in face_normals]): |
| 117 | + field_data["obj:vn"] = vertex_normals |
| 118 | + cell_data["obj:vn_face_idx"] = face_normals |
| 119 | + elif len(vertex_normals) == len(points): |
| 120 | + point_data["obj:vn"] = vertex_normals |
| 121 | + |
| 122 | + cell_data["obj:group_ids"] = [] |
| 123 | + cells = [] |
| 124 | + for f, gid in zip(face_groups, face_group_ids): |
| 125 | + if f.shape[1] == 3: |
| 126 | + cells.append(meshio.CellBlock("triangle", f - 1)) |
| 127 | + elif f.shape[1] == 4: |
| 128 | + cells.append(meshio.CellBlock("quad", f - 1)) |
| 129 | + else: |
| 130 | + cells.append(meshio.CellBlock("polygon", f - 1)) |
| 131 | + cell_data["obj:group_ids"].append(gid) |
| 132 | + |
| 133 | + return meshio.Mesh(points, cells, point_data=point_data, cell_data=cell_data, field_data=field_data) |
| 134 | + |
| 135 | +meshio.register_format("obj", [".obj"], read, {"obj": None}) |
0 commit comments