-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmerge_frustums.py
95 lines (64 loc) · 2.86 KB
/
merge_frustums.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 argparse
import scipy.spatial
import numpy as np
def read_frustum_data(file_path):
with np.load(file_path) as data:
points = data['points']
return points
def get_unique_points(points):
distances = scipy.spatial.distance.pdist(points[:, :3])
close_points = distances < 1e-10
close_points_square = scipy.spatial.distance.squareform(close_points)
close_points_indices = np.nonzero(close_points_square)
tosto = list({(x, y) if x < y else (y, x) for x, y in zip(close_points_indices[0], close_points_indices[1])})
first = np.array([x[0] for x in tosto])
second = np.array([x[1] for x in tosto])
if len(first) == 0:
return points
duplicate_point_labels = (points[first, 3].astype(bool) | points[second, 3].astype(bool)).astype(np.float32)
new_points_mask = np.ones(len(points), dtype=bool)
new_points_mask[second] = False
points[first, 3] = duplicate_point_labels
return points[new_points_mask]
def sort_points(points):
sorted_points = np.copy(points)
sorted_points = sorted_points[sorted_points[:, 2].argsort()]
sorted_points = sorted_points[sorted_points[:, 1].argsort(kind='mergesort')]
sorted_points = sorted_points[sorted_points[:, 0].argsort(kind='mergesort')]
return sorted_points
def get_arguments():
parser = argparse.ArgumentParser(description='Script to merge different frustums of the same scene to a single file')
parser.add_argument(
'input', type=str,
help='Path to directory containg points and labels per frustum in npz format'
)
parser.add_argument(
'output', type=str,
help='Path to save resulting full-scene points'
)
return parser.parse_args()
if __name__ == '__main__':
args = get_arguments()
input_dir = args.input
if not input_dir or not os.path.isdir(input_dir):
exit('Invalid input directory')
output_dir = args.output
os.makedirs(output_dir, exist_ok=True)
frustum_files = os.listdir(input_dir)
frustum_files = [filename for filename in frustum_files if filename.endswith('.npz')]
scene_ids = set([filename.split('_')[0] for filename in frustum_files])
scene_ids = sorted(list(scene_ids))
for scene_id in scene_ids:
print(scene_id)
scene_frustums = [filename for filename in frustum_files if filename.startswith(scene_id)]
scene_points = list()
for frustum in scene_frustums:
frustum_file_path = os.path.join(input_dir, frustum)
frustum_points = read_frustum_data(frustum_file_path)
scene_points.append(frustum_points)
scene_points = np.vstack(scene_points)
unique_points = get_unique_points(scene_points)
sorted_points = sort_points(unique_points)
output_scene_path = os.path.join(output_dir, scene_id)
np.savez(output_scene_path, points=sorted_points)