-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata.py
More file actions
64 lines (52 loc) · 1.77 KB
/
data.py
File metadata and controls
64 lines (52 loc) · 1.77 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
"""
Read triangulation from a .ply file.
"""
from helpers import sorted_tuple
def get_triangulation(file):
""" Returns list of points and list of triangle indices.
Ex.: points = [[0.0, 0.0, 0.0], [1.0, 0.0, 0.0], [0.0, 0.0, 1.0]],
triangulation = [(0, 1, 2)]
"""
points = []
triangulation = set()
with open(file, "r") as f:
# skip header
line = f.readline()
while line != "end_header\n":
line = f.readline()
for line in f.readlines():
data = line.strip().split(" ")
# lines of form x, y, z, and some data we don't need
if len(data) == 5:
points.append([float(x) for x in data[:3]])
# lines of form 3, i, j, k (indices of points)
elif len(data) == 4 and data[0] == '3':
triangulation.add(sorted_tuple(*[int(i) for i in data[1:]]))
return points, list(triangulation)
def export_ply(triangulation, points):
triangulation = [t for t in triangulation
if len(t) == 3]
data = """ply
format ascii 1.0
comment Exported triangulation
comment
element vertex {}
property float x
property float y
property float z
element face {}
property list uchar int vertex_index
end_header
""".format(len(points), len(triangulation))
for point in points:
data += " ".join(map(str, point)) + "\n"
for simplex in triangulation:
line = "{} ".format(len(simplex))
line += " ".join(map(str, simplex))
line += "\n"
data += line
return data
def save_ply(filename, triangulation, points):
data = export_ply(triangulation, points)
with open(filename, "w") as f:
f.writelines(data)