forked from mitevpi/urban-insights-backend
/
obj.py
118 lines (94 loc) · 3.46 KB
/
obj.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
108
109
110
111
112
113
114
115
116
117
118
import math
def cutObj():
filepath = r"models/sf.obj"
outputfile = r"sfParsed.obj"
# Coordinates for focus points (rhino coordinates)
cx = -1200.0
cy = -18200.0
radius = 1500.0
flipXY = True
moveToOrigin = True
# output lists
o_vertices = []
o_faces = []
o_verticenormals = []
# working lists
flines = []
vertices = []
verticenormals = []
# 2d distance
def dist(cx, cy, dx, dy):
dx = abs(cx-dx)
dy = abs(cy-dy)
return math.sqrt(dx * dx + dy * dy)
# readfile
with open(filepath) as f:
for fline in f:
flines.append(fline.replace("\\", ""))
# Main:
# find out if split for g or o!!!
# as test, using only objects 0-10
objects = "".join(flines).split("g ")[1:]
if len(objects) < 2:
# as test, using only objects 0-10
objects = "".join(flines).split("o ")[1:]
# print("Parsing through {} obj objects".format(len(objects)))
for objectnr, object in enumerate(objects):
# # print("object {}".format(str(objectnr)))
for linenr, line in enumerate(object.split("\n")):
if line[0:2] == "v ":
x, y, z = line[2:].split(" ")
x, y, z = float(x), float(y), float(z)
vertices.append([x, y, z])
elif line[0:2] == "f ":
# # print " - facenr %s" % linenr
facevertices = line.split(" ")[1:]
# facelist = " ,".join(facevertices)
penalty = []
temppoints = []
tempvertices = []
fv = []
for facevertice in facevertices:
fv.append(int(facevertice.split("//")[0].split("/")[0])-1)
for vn in fv:
vx, vy, vz = vertices[vn][0], vertices[vn][1], vertices[vn][2]
distance = dist(cx, cy, vx, vy)
if distance > radius:
penalty.append(True)
if(flipXY):
if(moveToOrigin):
tempvertices.append([vx-cx, vz, vy-cy])
else:
tempvertices.append([vx, vz, vy])
else:
if(moveToOrigin):
tempvertices.append([vx-cx, vy-cy, vz])
else:
tempvertices.append([vx, vy, vz])
temppoints.append(len(o_vertices)+len(temppoints)+1)
if len(penalty) == 0:
# # print "Added to o_faces: ", temppoints
o_faces.append(temppoints)
for x in tempvertices:
o_vertices.append(x)
# # print "Added to o_vertices [%s -> %s]: %s" % (vn, len(o_vertices), x)
nf = open(outputfile, "w")
nf.write("""# Rhino
o object_1""")
for coords in o_vertices:
x, y, z = coords
nf.write("\nv {} {} {}".format(x, y, z))
for faces in o_faces:
facestring = "\nf "
for vertice in list(reversed(faces)):
facestring = facestring + str(vertice) + "//" + str(vertice) + " "
nf.write(facestring)
nf.close()
# return json
flines = []
filepath = r"models/sf.obj"
with open(filepath) as f:
for fline in f:
flines.append(fline.replace("\\", ""))
return flines
#return jsonify({'parsedModel': flines})