-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.py
More file actions
90 lines (86 loc) · 2.62 KB
/
parser.py
File metadata and controls
90 lines (86 loc) · 2.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
def vecMul(a: list, b: list) -> list:
assert len(a) == 3
assert len(b) == 3
c = [0, 0, 0]
c[2] = a[0] * b[1] - a[1] * b[0]
c[0] = a[1] * b[2] - a[2] * b[1]
c[1] = a[2] * b[0] - a[0] * b[2]
return c
def unitvector(c: list) -> list:
assert len(c) == 3
norm = (c[0] ** 2 + c[1] ** 2 + c[2] ** 2) ** 0.5
if (norm == 0):
return [0, 0, 0]
return [c[k] / norm for k in range(3)]
def parseOBJ(objPath: str) -> dict:
obj = open(objPath)
v = []
vn = []
vt = []
f = []
fn = []
ft = []
x = obj.readline()
while (x):
a = x.split()
if (len(a) == 0):
x = obj.readline()
continue
if (a[0] == 'v'):
v.append([float(a[1]), float(a[2]), float(a[3])])
elif (a[0] == "vn"):
vn.append([float(a[1]), float(a[2]), float(a[3])])
elif (a[0] == "vt"):
vt.append([float(a[1]), 1 - float(a[2])])
elif (a[0] == "f"):
ar1 = a[1].split('/')
ar2 = a[2].split('/')
ar3 = a[3].split('/')
assert len(ar1) == len(ar2) == len(ar3)
f.append([int(ar1[0]) - 1, int(ar2[0]) - 1, int(ar3[0]) - 1])
if (len(ar1) > 1):
if (ar1[1] != ""):
ft.append([int(ar1[1]) - 1, int(ar2[1]) - 1, int(ar3[1]) - 1])
# computing normals...
a = [v[f[-1][1]][k] - v[f[-1][0]][k] for k in range(3)]
b = [v[f[-1][2]][k] - v[f[-1][0]][k] for k in range(3)]
c = unitvector(vecMul(a, b))
# this is also crap...
# we work only with triangles, so 3 points define normals
# fn.append([int(ar1[2]) - 1, int(ar2[2]) - 1, int(ar3[2]) - 1])
for _ in range(3):
fn.extend(c)
elif (a[0] in ['o', 'g', "vp", 's', "usemtl", "mtllib"]):
# ignore this crap
pass
else:
pass
x = obj.readline()
# Now we need to make links to vertices unique
vUsed = set()
tc = []
ind = []
vertex = []
#TODO vnUsed = set()
# vtUsed = set() --- it's already ok
for i in range(len(f)):
_v = [v[f[i][k]] for k in range(3)]
for k in range(3):
ind.append(len(vertex) // 3)
vertex.extend(_v[k])
for i in ft:
for k in range(3):
tc.extend(vt[i[k]])
'''print(len(vertex))
print(*vertex)
print(*ind)
print(*tc)'''
return {
"v": vertex,
"vn": vn,
"vt": vt,
"f": ind,
"fn": fn,
"ft": ft,
"tc": tc,
}