Last active
February 16, 2020 05:01
-
-
Save yzhong52/7c3e0b3a201af45f0cd12f10e06b9d95 to your computer and use it in GitHub Desktop.
Beyond data
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
def read_obj(filename): | |
triangles = [] | |
vertices = [] | |
with open(filename) as file: | |
for line in file: | |
components = line.strip(' \n').split(' ') | |
if components[0] == "f": # face data | |
# e.g. "f 1/1/1/ 2/2/2 3/3/3 4/4/4 ..." | |
indices = list(map(lambda c: int(c.split('/')[0]) - 1, components[1:])) | |
for i in range(0, len(indices) - 2): | |
triangles.append(indices[i: i+3]) | |
elif components[0] == "v": # vertex data | |
# e.g. "v 30.2180 89.5757 -76.8089" | |
vertex = list(map(lambda c: float(c), components[1:])) | |
vertices.append(vertex) | |
return np.array(vertices), np.array(triangles) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment