forked from fengyingxin/geo1015_Assignment-4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
_2_thin.py
99 lines (78 loc) · 2.93 KB
/
_2_thin.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
# The code is for thinning a point cloud file into different thinning rates
# Authors: Feng Yingxin(5692148), Gong Sicong(5711932), Rao Chengzhi(5841089)
import time
import json
import numpy as np
class Grid:
def __init__(self, x_min, x_max, y_min, y_max, index):
self.x_min = x_min
self.x_max = x_max
self.y_min = y_min
self.y_max = y_max
self.index = index
self.lst_point = []
# Initialize the grids
def init_grid(x_min, y_min, col_num, row_num, size):
lst_grid = []
for i in range(row_num):
y = y_min + i * size
for j in range(col_num):
x = x_min + j * size
grid = Grid(x, x + size, y, y + size, i * col_num + j)
lst_grid.append(grid)
return lst_grid
# Fill the grids
def fill_grid(x_min, y_min, col_num, size, lst_grid, line):
split = line.split(',')
x, y = float(split[0]), float(split[1])
col = round((x - x_min) // size)
row = round((y - y_min) // size)
index = row * col_num + col
grid = lst_grid[index]
grid.lst_point.append(line)
# Thin the grids
def thin_grid(grid, rate):
points = grid.lst_point
points_size = len(points)
indice_thin = np.random.choice(points_size, round(points_size * rate), replace=False)
points_thin = [points[index] for index in indice_thin]
return points_thin
def thin():
# Open the parameter file
with open('file/parameter.json', 'r') as pfile:
parameter = json.load(pfile)
path_source = parameter['crop']['path_target']
grid_size = parameter['thin']['grid_size']
thin_rate = parameter['thin']['thin_rate']
x_min, x_max = parameter['crop']['x_min'], parameter['crop']['x_max']
y_min, y_max = parameter['crop']['y_min'], parameter['crop']['y_max']
# Get the output parameters
list_output = []
for size in grid_size:
for rate in thin_rate:
list_output.append({'size': size, 'rate': rate})
for output in list_output:
print('Start thin {}!'.format(output))
start = time.time()
size = output['size']
rate = output['rate']
# Initialize the grids
col_num, row_num = round((x_max - x_min) / size), round((y_max - y_min) / size)
lst_grid = init_grid(x_min, y_min, col_num, row_num, size)
# Open the input file
with open(path_source, mode='r') as ifile:
for line in ifile:
# Fill the grids
fill_grid(x_min, y_min, col_num, size, lst_grid, line)
# Open the output files
with open("file/size_{}_rate_{}.txt".format(size, rate), mode='w') as ofile:
for grid in lst_grid:
# Thin the grids
result = thin_grid(grid, rate)
result = ''.join(result)
ofile.write(result)
print("Time cost is {:.3}s!".format(time.time() - start))
def main():
thin()
if __name__ == "__main__":
main()