forked from fengyingxin/geo1015_Assignment-4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_4_inte.py
96 lines (78 loc) · 3.37 KB
/
_4_inte.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
# The code is for interpolation
# Authors: Feng Yingxin(5692148), Gong Sicong(5711932), Rao Chengzhi(5841089)
import csv
import time
import json
import numpy as np
import startinpy
def get_dt(path_source):
with open('file/{}'.format(path_source), 'r') as ifile:
dt = startinpy.DT()
for line in ifile:
strip = line.strip('\n')
split = strip.split(',')
x, y, z = float(split[0]), float(split[1]), float(split[2])
dt.insert_one_pt(x, y, z)
print("DT created!")
return dt
def init_image(x_min, x_max, y_min, y_max, res_c):
row_num = round((x_max - x_min) / res_c)
col_num = round((y_max - y_min) / res_c)
x_start, x_end = x_min + res_c / 2, x_max - res_c / 2
y_start, y_end = y_min + res_c / 2, y_max - res_c / 2
coor_x = np.arange(x_start, x_end + res_c, res_c)
coor_y = np.arange(y_start, y_end + res_c, res_c)
result = np.zeros((row_num, col_num))
return coor_x, coor_y, result
def save_image_to_csv(x_max, x_min, y_max, y_min, coor_x, coor_y, result, path_source, res_c):
result = np.flipud(result)
outfile = str(path_source + '.csv')
print('row_num: ', round((x_max - x_min) / res_c))
print('col_num: ', round((y_max - y_min) / res_c))
with open('file/' + outfile, 'w', newline='') as f:
writer = csv.writer(f)
writer.writerow(['x', 'y', 'z'])
for col, x in enumerate(coor_x):
for row, y in enumerate(coor_y):
writer.writerow([x, y, result[row][col]])
def inte():
# Open the parameter file
with open('file/parameter.json', 'r') as pfile:
parameter = json.load(pfile)
res_c = parameter['inte']['res_c']
x_min, x_max = parameter['crop']['x_min'], parameter['crop']['x_max']
y_min, y_max = parameter['crop']['y_min'], parameter['crop']['y_max']
grid_size = parameter['thin']['grid_size']
thin_rate = parameter['thin']['thin_rate']
# Get the input parameters
lst_input = []
for size in grid_size:
for rate in thin_rate:
lst_input.append({'size': size, 'rate': rate})
lst_path_cloth = ['cloth_size_{}_rate_{}.txt'.format(input['size'], input['rate']) for input in lst_input]
lst_path_ground = ['ground_size_{}_rate_{}.txt'.format(input['size'], input['rate']) for input in
lst_input]
lst_path_source = lst_path_cloth + lst_path_ground
for path_source in lst_path_source:
print('Start inte {}!'.format(path_source))
start = time.time()
dt = get_dt(path_source)
image = init_image(x_min, x_max, y_min, y_max, res_c)
coor_x, coor_y, result = image[0], image[1], image[2]
for col, x in enumerate(coor_x):
for row, y in enumerate(coor_y):
if dt.is_inside_convex_hull(x, y) is False:
z = np.nan
result[row][col] = z
else:
z = dt.interpolate_laplace(x, y)
result[row][col] = z
result = np.flipud(result)
np.save("file/" + path_source.strip('.txt'), result)
print("Time cost is {:.3}s!".format(time.time() - start))
if path_source == 'cloth_size_10_rate_0.5.txt':
save_image_to_csv(x_max, x_min, y_max, y_min, coor_x, coor_y, result, path_source, res_c)
def main():
inte()
if __name__ == "__main__":
main()