| 2264 | return verts, colors, faces, Height, Width, hFov, vFov, mean_loc_depth |
| 2265 | |
| 2266 | def read_ply(mesh_fi): |
| 2267 | #bty: implement binary support (assume same endianness for now) |
| 2268 | # read header in text mode |
| 2269 | ply_fi = open(mesh_fi, 'r', encoding="utf8", errors='ignore') # required to readline in bin file |
| 2270 | Height = None |
| 2271 | Width = None |
| 2272 | hFov = None |
| 2273 | vFov = None |
| 2274 | mean_loc_depth = None |
| 2275 | isBinary = True |
| 2276 | # read ascii header |
| 2277 | while True: |
| 2278 | line = ply_fi.readline().split('\n')[0] |
| 2279 | if line.startswith('element vertex'): |
| 2280 | num_vertex = int(line.split(' ')[-1]) |
| 2281 | elif line.startswith('element face'): |
| 2282 | num_face = int(line.split(' ')[-1]) |
| 2283 | elif line.startswith('comment'): |
| 2284 | if line.split(' ')[1] == 'H': |
| 2285 | Height = int(line.split(' ')[-1].split('\n')[0]) |
| 2286 | if line.split(' ')[1] == 'W': |
| 2287 | Width = int(line.split(' ')[-1].split('\n')[0]) |
| 2288 | if line.split(' ')[1] == 'hFov': |
| 2289 | hFov = float(line.split(' ')[-1].split('\n')[0]) |
| 2290 | if line.split(' ')[1] == 'vFov': |
| 2291 | vFov = float(line.split(' ')[-1].split('\n')[0]) |
| 2292 | #bty: this was the only value for which it needed the depthmap, so store it in the ply too |
| 2293 | if line.split(' ')[1] == 'meanLoc': |
| 2294 | mean_loc_depth = float(line.split(' ')[-1].split('\n')[0]) |
| 2295 | # check format |
| 2296 | elif line.startswith('format ascii'): |
| 2297 | isBinary = False |
| 2298 | elif line.startswith('end_header'): |
| 2299 | break |
| 2300 | |
| 2301 | if isBinary: |
| 2302 | # grab current file offset and re-open in binary mode |
| 2303 | endheader = ply_fi.tell() |
| 2304 | ply_fi.close() |
| 2305 | ply_fi = open(mesh_fi, 'rb') |
| 2306 | ply_fi.seek(endheader) |
| 2307 | |
| 2308 | verts = [None] * num_vertex |
| 2309 | colors = [None] * num_vertex |
| 2310 | faces = [None] * num_face |
| 2311 | |
| 2312 | pbar = tqdm.tqdm(total = num_vertex+num_face) |
| 2313 | pbar.set_description("Loading vertices") |
| 2314 | for i in range(num_vertex): |
| 2315 | x, y, z, r, g, b, a = struct.unpack('fffBBBB', ply_fi.read(16)) |
| 2316 | verts[i] = [x, y, z] |
| 2317 | colors[i] = [float(r), float(g), float(b), float(a)] |
| 2318 | pbar.update(1) |
| 2319 | verts = np.array(verts) |
| 2320 | colors = np.array(colors) |
| 2321 | colors[..., :3] = colors[..., :3] / 255. |
| 2322 | |
| 2323 | pbar.set_description("Loading faces") |