-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpotree_to_ply.py
138 lines (103 loc) · 4.46 KB
/
potree_to_ply.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import os
import json
import sys
import struct
import argparse
from functools import partial
class HierarchyNode:
def __init__(self, byte_data):
self._type = byte_data[0]
self._bitmask = byte_data[1]
self._points = byte_data[2]
self._byte_offset = byte_data[3]
self._byte_size = byte_data[4]
def __str__(self):
return f"Bitmask: {self._bitmask}\nPoints: {self._points}\nByteOffset: {self._byte_offset}\nByteSize: {self._byte_size}\nChildren: {self.children()}"
def children(self):
return bin(self._bitmask).count("1")
def offset(self):
return self._byte_offset
def size(self):
return self._byte_size
def parse_arguments(argv):
parser = argparse.ArgumentParser(prog="potree_to_ply", description='Convert Potree data to PLY files.')
parser.add_argument('-i', type=str, help='potree input data directory')
parser.add_argument('-o', type=str, help='ply output data directory')
args = parser.parse_args()
potree_path = args.i
output_path = args.o if args.o is not None else potree_path
if not potree_path:
parser.print_help()
exit()
return potree_path, output_path
if __name__ == "__main__":
path, output_path = parse_arguments(sys.argv)
struct_fmt = '=BBIQQ'
struct_len = struct.calcsize(struct_fmt)
struct_unpack = struct.Struct(struct_fmt).unpack_from
hierarchy_file = os.path.join(path, "hierarchy.bin")
octree_file = os.path.join(path, "octree.bin")
metadata_file = os.path.join(path, "metadata.json")
with open(octree_file, "rb") as octree:
with open(metadata_file) as f:
metadata = json.load(f)
points = metadata["points"]
hierarchy = metadata["hierarchy"]
chunk_size = hierarchy["firstChunkSize"]
step_size = hierarchy["stepSize"]
depth = hierarchy["depth"]
node_amount = chunk_size / 22
with open(hierarchy_file, "rb") as h:
hierarchy = [HierarchyNode(struct_unpack(chunk)) for chunk in iter(partial(h.read, struct_len), b'')]
index = 0
visited = [False] * int(node_amount)
# Create a queue for BFS
queue = []
# Mark the source node as
# visited and enqueue it
queue.append((hierarchy[0], 0))
visited[index] = True
points_to_export = []
data_to_export = []
leaf_node_data = []
leaf_node_points = 0
while queue:
node, level = queue.pop(0)
if len(points_to_export) == level:
points_to_export.append(0)
data_to_export.append([])
# Fetch data from octree
octree.seek(node.offset(), 0)
data = octree.read(node.size())
# Store data and update
points_to_export[level] += int(node.size() / 18)
data_to_export[level].append(data)
if node.children() == 0:
leaf_node_points += int(node.size() / 18)
leaf_node_data.append(data)
for _ in range(node.children()):
index += 1
if visited[index] == False:
queue.append((hierarchy[index], level + 1))
visited[index] = True
points_to_export.append(leaf_node_points)
data_to_export.append(leaf_node_data)
for level in range(len(points_to_export)):
header = f'''ply
format binary_little_endian 1.0
comment Created with PotreeToPly Converter
element vertex {points_to_export[level]}
property uint x
property uint y
property uint z
property ushort red
property ushort green
property ushort blue
end_header
'''
ply_file = os.path.join(output_path, f"converted_level_{level if level < (len(points_to_export) - 1) else 'leaf'}.ply")
with open(ply_file, "wb") as out:
out.write(header.encode("ascii"))
for p in data_to_export[level]:
out.write(p)
print(f"Exported points: {points_to_export[level]}")