Skip to content

Instantly share code, notes, and snippets.

@yzhong52
Last active February 16, 2020 05:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yzhong52/7c3e0b3a201af45f0cd12f10e06b9d95 to your computer and use it in GitHub Desktop.
Save yzhong52/7c3e0b3a201af45f0cd12f10e06b9d95 to your computer and use it in GitHub Desktop.
Beyond data
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