-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
95 lines (67 loc) · 2.94 KB
/
main.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
import os
import sys
import numpy as np
import Triangulate
vector = np.array
point = Triangulate.Point
def listIndex(index, indexToList):
if index > 0: return index - 1
return index + len(indexToList)
def copy(source, target):
if source is None or target is None:
return
if not os.path.exists(source):
print(f"File not found: {source}")
return
if os.path.exists(target):
print(f"File already exists: {target}")
return
vertex = []
with open(target, 'w') as targetFile:
with open(source, 'r') as sourceFile:
for line in sourceFile:
line = line.strip()
if line != '':
words = line.split()
command = words[0]
data = words[1:]
if command == 'v':
x, y, z = map(float, data[:3])
vertex.append(vector([x, y, z]))
elif command == 'f':
polygon = []
index_word = {}
for i in range(len(data)):
word = data[i]
indices = word.split('/')
if len(indices) > 0:
index = int(indices[0])
index = listIndex(index, vertex)
if index not in index_word:
index_word[index] = word
v = vertex[index]
p = point(v[0], v[1], v[2], index)
polygon.append(p)
if len(polygon) == 0:
continue
triangles, _ = Triangulate.triangulate(polygon)
if len(triangles) == 0:
continue
for triangle in triangles:
line = "f "
line += index_word[triangle.p0.i] + ' '
line += index_word[triangle.p1.i] + ' '
line += index_word[triangle.p2.i]
targetFile.write(line + '\n')
continue
targetFile.write(line + '\n')
targetFile.close()
sourceFile.close()
if __name__ == "__main__":
if len(sys.argv) != 3:
print("Usage: python main.py <source objfile> <target objfile>")
sys.exit(1)
source_file = sys.argv[1]
target_file = sys.argv[2]
copy(source_file, target_file)
print(f"File converted from {source_file} to {target_file}")