-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
107 lines (79 loc) · 2.82 KB
/
model.py
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
from dataclasses import dataclass
import re
import typing
from vector import Vector3
class Model:
_vert_flag = "v"
_face_flag = "f"
def __init__(self, filename: str) -> None:
self._verts = []
self._faces = []
with open(filename, "r") as f:
for l in f:
segs = re.split(r"[ ]+", l)
if len(segs) != 4:
continue
if segs[0] == Model._vert_flag:
self._verts.append(tuple([float(i) for i in segs[1:]]))
elif segs[0] == Model._face_flag:
self._faces.append(
tuple(int(seg.split("/")[0]) - 1 for seg in segs[1:])
)
def n_verts(self) -> int:
return len(self._verts)
def n_faces(self) -> int:
return len(self._faces)
def vert(self, idx: int) -> typing.Tuple[int]:
return self._verts[idx]
def vt(self, idx: int) -> typing.Tuple[float]:
return self._vts[idx]
def face(self, idx: int) -> typing.Tuple[int]:
return self._faces[idx]
def faces(self) -> typing.Iterable[typing.Tuple[int]]:
for f in self._faces:
yield f
@dataclass
class FaceData:
vert_idx: Vector3
texture_idx: Vector3
# normal: Vector3
class Model1:
_vert_flag = "v"
_texture_flag = "vt"
_face_flag = "f"
def __init__(self, filename: str) -> None:
self._verts = []
self._textures = []
self._faces = []
with open(filename, "r") as f:
for l in f:
segs = re.split(r"[ ]+", l)
if len(segs) != 4:
continue
if segs[0] == Model1._vert_flag:
self._verts.append(Vector3(*[float(i) for i in segs[1:]], float))
elif segs[0] == Model1._texture_flag:
self._textures.append(Vector3(*[float(i) for i in segs[1:]], float))
elif segs[0] == Model1._face_flag:
flat_elements = [
int(idx) - 1 for seg in segs[1:] for idx in seg.split("/")
]
self._faces.append(
FaceData(
vert_idx=Vector3(*flat_elements[0::3]),
texture_idx=Vector3(*flat_elements[1::3]),
)
)
def n_verts(self) -> int:
return len(self._verts)
def n_faces(self) -> int:
return len(self._faces)
def vert(self, idx: int) -> Vector3:
return self._verts[idx]
def texture(self, idx: int) -> Vector3:
return self._textures[idx]
def face(self, idx: int) -> Vector3:
return self._faces[idx]
def faces(self) -> typing.Iterable[FaceData]:
for f in self._faces:
yield f