-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_visualization.py
More file actions
544 lines (448 loc) · 20.2 KB
/
data_visualization.py
File metadata and controls
544 lines (448 loc) · 20.2 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
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
"""
ToF Sensor Data Visualization Tool
This script is designed for near real-time visualization of sensor data in a 3D environment.
It processes and interpolates sensor data from multiple sources, generating 3D surface
reconstruction and visual representations. The script supports dynamic updates based on
new sensor data files and allows for interactive control of the visualization parameters.
Author: Yi Wang
Copyright: 2023, Yi Wang, Group 5, MRIGI, Imperial College London
Dependencies:
- Python 3.8
- NumPy for numerical operations
- Pandas for data manipulation
- PyVista for 3D visualization
- SciPy for scientific computations and interpolations
- Pillow for image processing
- Keyboard for capturing keypresses
- Time for handling time-related functions
- OS for file and directory operations
- IO for handling I/O operations
Usage:
Run this script in a Python environment where all dependencies are installed. The script
listens for new sensor data files in the specified directory and updates the 3D
visualization accordingly. Interactive controls are available for manipulating the
visualization during runtime. Ensure the sensor data files are formatted correctly
and placed in the 'data' directory.
"""
import os
import numpy as np
import pandas as pd
import pyvista as pv
from scipy.interpolate import griddata
import time
from PIL import Image
import io
import keyboard
def read_sensor_data_file(filename):
data = []
with open(filename, 'r') as file:
for line in file:
data_row = list(map(int, line.strip().split('\t')))
data_row = [x/10 for x in data_row]
data.append(data_row)
return data
def median_threshold(df, cell, window, threshold):
i, j = cell
start_row, end_row = max(0, i - window), min(df.shape[0], i + window + 1)
start_col, end_col = max(0, j - window), min(df.shape[1], j + window + 1)
window_values = df.iloc[start_row:end_row, start_col:end_col]
median_val = window_values.median().median()
return df.at[i, j] if abs(df.at[i, j] - median_val) <= threshold else np.nan
def preprocess_data(data):
df = pd.DataFrame(data)
df = df.reindex(columns=range(8), method='nearest')
## print(df)
## When the sensor measurement distance is less than a certain value, the output value will be large
df[(df > 10) & (df < 100)] = np.nan
df[(df > 100)] = 0
## print(df)
for i in range(df.shape[0]):
for j in range(df.shape[1]):
df.at[i, j] = median_threshold(df, (i, j), 3, 1)
## print(df)
x, y = np.indices(df.shape)
values = df.values.flatten()
points = np.column_stack((x.flatten(), y.flatten()))
known_points = points[~np.isnan(values)]
known_values = values[~np.isnan(values)]
grid_x, grid_y = np.indices(df.shape)
grid_z = griddata(known_points, known_values, (grid_x, grid_y), method='linear')
if np.any(np.isnan(grid_z)):
grid_z = griddata(known_points, known_values, (grid_x, grid_y), method='nearest')
if np.any(np.isnan(grid_z)):
avg_val = np.nanmean(grid_z)
grid_z = np.nan_to_num(grid_z, nan=avg_val)
df = pd.DataFrame(grid_z, columns=df.columns)
## df.interpolate(method='linear', axis=1, inplace=True)
## print(df)
return df.values
def generate_ellipse_surface():
center = (0.0, 0.0)
major_axis = 3.0
minor_axis = 1.5
x, y = np.meshgrid(range(8), range(8))
distance_data = np.sqrt(((x - center[0]) / major_axis) ** 2 + ((y - center[1]) / minor_axis) ** 2)
return distance_data
def eight_sensor_blocks():
rectangle_length = 1.3
rectangle_width = 0.65
rectangle_thickness = 0.05
sensor_blocks = []
for i in range(8):
angle = i * (360 / 8)
center_x = (rectangle_width / 2 + rectangle_width * np.cos(np.deg2rad(45))) * np.cos(np.deg2rad(angle))
center_y = (rectangle_width / 2 + rectangle_width * np.cos(np.deg2rad(45))) * np.sin(np.deg2rad(angle))
center_z = 0
box = pv.Box(bounds=(-rectangle_width / 2, rectangle_width / 2,
-rectangle_thickness / 2, rectangle_thickness / 2,
-rectangle_length / 2, rectangle_length / 2))
if i % 2 == 0:
box = box.rotate_z(-angle + 90)
else:
box = box.rotate_z(-angle)
box = box.translate((center_x, center_y, center_z))
sensor_blocks.append(box)
return sensor_blocks
def convert_to_point_cloud(data):
rows, cols = data.shape
points = []
for i in range(rows):
for j in range(cols):
distance = data[i, j]
points.append([(i-3.5)*0.343, (j-3.5)*0.343, distance+1.81])
points = np.array(points)
return points
def transform(points, translation_vector, rotation_vector):
homogeneous_points = np.column_stack((points, np.ones(points.shape[0])))
translation_matrix = [
[1, 0, 0, translation_vector[0]],
[0, 1, 0, translation_vector[1]],
[0, 0, 1, translation_vector[2]],
[0, 0, 0, 1 ]
]
alpha, beta, gamma = np.deg2rad(rotation_vector[3:])
## Rx = np.array([
## [1, 0, 0, 0],
## [0, np.cos(alpha), -np.sin(alpha), 0],
## [0, np.sin(alpha), np.cos(alpha), 0],
## [0, 0, 0, 1]
## ])
## Ry = np.array([
## [np.cos(beta), 0, np.sin(beta), 0],
## [0, 1, 0, 0],
## [-np.sin(beta), 0, np.cos(beta), 0],
## [0, 0, 0, 1]
## ])
## Rz = np.array([
## [np.cos(gamma), -np.sin(gamma), 0, 0],
## [np.sin(gamma), np.cos(gamma), 0, 0],
## [0, 0, 1, 0],
## [0, 0, 0, 1]
## ])
Rx = np.array([
[1, 0, 0, 0],
[0, np.cos(alpha), -np.sin(alpha), rotation_vector[1]*(1 - np.cos(alpha)) + rotation_vector[2]*np.sin(alpha)],
[0, np.sin(alpha), np.cos(alpha), rotation_vector[2]*(1-np.cos(alpha)) - rotation_vector[1]*np.sin(alpha)],
[0, 0, 0, 1]
])
Ry = np.array([
[np.cos(beta), 0, np.sin(beta), rotation_vector[0]*(1-np.cos(beta))-rotation_vector[2]*np.sin(beta)],
[0, 1, 0, 0],
[-np.sin(beta), 0, np.cos(beta), rotation_vector[2]*(1-np.cos(beta))+rotation_vector[0]*np.sin(beta)],
[0, 0, 0, 1]
])
Rz = np.array([
[np.cos(gamma), -np.sin(gamma), 0, rotation_vector[0]*(1-np.cos(gamma))+rotation_vector[1]*np.sin(gamma)],
[np.sin(gamma), np.cos(gamma), 0, rotation_vector[1]*(1-np.cos(gamma))-rotation_vector[0]*np.sin(gamma)],
[0, 0, 1, 0],
[0, 0, 0, 1]
])
rotated_points = np.dot(np.dot(np.dot(Rx, Ry), Rz), homogeneous_points.T).T
transformed_points = np.dot(translation_matrix, rotated_points.T).T
transformed_points = transformed_points[:, :3]
return transformed_points
def grid_transform(grid, translation_vector, rotation_vector):
translation_matrix = np.array([
[1, 0, 0, translation_vector[0]],
[0, 1, 0, translation_vector[1]],
[0, 0, 1, translation_vector[2]],
[0, 0, 0, 1 ]
])
alpha, beta, gamma = np.deg2rad(rotation_vector[3:])
Rx = np.array([
[1, 0, 0, 0],
[0, np.cos(alpha), -np.sin(alpha), 0],
[0, np.sin(alpha), np.cos(alpha), 0],
[0, 0, 0, 1]
])
Ry = np.array([
[np.cos(beta), 0, np.sin(beta), 0],
[0, 1, 0, 0],
[-np.sin(beta), 0, np.cos(beta), 0],
[0, 0, 0, 1]
])
Rz = np.array([
[np.cos(gamma), -np.sin(gamma), 0, 0],
[np.sin(gamma), np.cos(gamma), 0, 0],
[0, 0, 1, 0],
[0, 0, 0, 1]
])
combined_matrix = np.dot(translation_matrix, Rx)
combined_matrix = np.dot(combined_matrix, Ry)
combined_matrix = np.dot(combined_matrix, Rz)
grid.transform(combined_matrix)
def interpolate_to_surface(data):
start = -1.13
stop = 1.13
num_points = data.shape[0]
step = (stop - start) / (num_points - 1)
x = np.arange(start, stop + step, step)
y = np.arange(start, stop + step, step)
X, Y = np.meshgrid(x, y)
Z = griddata((X.flatten(), Y.flatten()), data.flatten(), (X, Y), method='linear')
Z += 1.81
return X.astype(np.float32), Y.astype(np.float32), Z.astype(np.float32)
## X = data[:, 0]
## Y = data[:, 1]
## Z = data[:, 2]
## grid_x, grid_y = np.mgrid[min(X):max(X):100j, min(Y):max(Y):100j]
## grid_z = griddata((X, Y), Z, (grid_x, grid_y), method='linear')
## return grid_x, grid_y, grid_z
def update(frame, plotter):
plotter.clear()
eight_sensors = eight_sensor_blocks()
eight_points = [];
for i in range(8):
plotter.add_mesh(eight_sensors[i], color='red')
for i in range(8 * frame, 8 * frame + 8):
filename = os.path.join(folder_path, files[i])
raw_data = read_sensor_data_file(filename)
new_data = preprocess_data(raw_data)
points = convert_to_point_cloud(new_data)
translation_vector = [0.0, 0.0, 0.0]
rotation_vector = [0.0, 0.0, 0.0, 0, 90, 0]
points = transform(points, translation_vector, rotation_vector)
rotation_vector = [0.0, 0.0, 0.0, 0, 0, 45 * i]
points = transform(points, translation_vector, rotation_vector)
X, Y, Z = interpolate_to_surface(new_data)
grid = pv.StructuredGrid(X, Y, Z)
eight_points.append(points)
eight_points = np.concatenate(eight_points, axis=0)
plotter.add_points(eight_points, color='blue', point_size=8.0, render_points_as_spheres=True)
## plotter.add_mesh(surf, color='blue', smooth_shading=True)
## plotter.add_mesh(grid, scalars=Z.ravel(), cmap='viridis', show_scalar_bar=True)
## plotter.show_grid()
## plotter.set_background('white')
timeRecord = time.time()
## plotter.add_title(f'Time: {timeRecord}', font_size=18, color=None, font=None, shadow=False)
# Determine the boundary points of each grid
def find_boundary_column(grid, boundary='right'):
center = np.array([0, 0, 0])
angles = np.arctan2(grid.points[:, 1] - center[1], grid.points[:, 0] - center[0])
points = grid.points
column_points = []
if boundary == 'right':
## column_points = points[:8]
for i in range(8):
point = points[i * 8]
column_points.append(point.tolist())
else:
## column_points = points[-8:]
for i in range(8):
point = points[i * 8 + 7]
column_points.append(point.tolist())
return np.array(column_points)
def create_bridge_between_grids(grid1, grid2):
right_column = find_boundary_column(grid1, 'right')
left_column = find_boundary_column(grid2, 'left')
## plotter.add_points(right_column)
## plotter.add_points(left_column)
num_points = min(len(right_column), len(left_column))
bridge_points = np.vstack([right_column, left_column])
faces = []
for i in range(num_points - 1):
faces.extend([4, i, i + num_points, i + num_points + 1, i + 1])
faces = np.array(faces, dtype=np.int_)
bridge = pv.PolyData()
bridge.points = bridge_points
bridge.faces = faces
return bridge
def plot_cylinder(up, bottom):
height = 2
long_diameter = 6.8
short_diameter = 6.8
num_columns = 64
a = long_diameter / 2
b = short_diameter / 2
z_shift = height / 2 + 1.5
cylinder_polydata = pv.PolyData()
h = ((a - b)**2) / ((a + b)**2)
circumference = np.pi * (a + b) * (1 + (3 * h) / (10 + np.sqrt(4 - 3 * h)))
delta = circumference / num_columns
num_rows = int(height / delta)
theta = np.linspace(0, 2 * np.pi, num_columns, endpoint=False)
z = np.linspace(-height / 2 + z_shift, height / 2 + z_shift, num_rows, endpoint=True)
Theta, Z = np.meshgrid(theta, z)
X = a * np.cos(Theta)
Y = b * np.sin(Theta)
points = np.column_stack((X.flatten(), Y.flatten(), Z.flatten()))
cells = []
for i in range(num_rows - 1):
for j in range(num_columns):
p1 = i * num_columns + j
p2 = p1 + num_columns
p4 = i * num_columns + (j + 1) % num_columns
p3 = p2 + 1 - num_columns if j == num_columns - 1 else p2 + 1
cells.append([3, p1, p2, p3])
cells.append([3, p1, p3, p4])
mesh = pv.PolyData(points, np.hstack(cells))
cylinder_polydata = cylinder_polydata.merge(mesh)
connect_points = np.array(points[:64].tolist())
shift = 36
connect_points = np.concatenate((connect_points[shift:], connect_points[:shift]))
## plotter.add_points(connect_points[:8], color = 'blue')
cells_up_to_connect = []
num_points = len(connect_points)
for i in range(num_points):
p1 = i
p2 = (i + 1) % num_points
p3 = num_points + (i + 1) % num_points
p4 = num_points + i
cells_up_to_connect.append([3, p1, p2, p3])
cells_up_to_connect.append([3, p1, p3, p4])
combined_points = np.vstack((up, connect_points))
mesh_up_to_connect = pv.PolyData(combined_points, np.hstack(cells_up_to_connect))
cylinder_polydata = cylinder_polydata.merge(mesh_up_to_connect)
## plotter.add_mesh(mesh_up_to_connect, color='black', style='wireframe')
z = np.linspace(-height / 2 - z_shift, height / 2 - z_shift, num_rows, endpoint=True)
Theta, Z = np.meshgrid(theta, z)
points = np.column_stack((X.flatten(), Y.flatten(), Z.flatten()))
cells = []
for i in range(num_rows - 1):
for j in range(num_columns):
p1 = i * num_columns + j
p2 = p1 + num_columns
p4 = i * num_columns + (j + 1) % num_columns
p3 = p2 + 1 - num_columns if j == num_columns - 1 else p2 + 1
cells.append([3, p1, p2, p3])
cells.append([3, p1, p3, p4])
connect_points = np.array(points[-64:].tolist())
## plotter.add_points(connect_points)
shift = 36
connect_points = np.concatenate((connect_points[shift:], connect_points[:shift]))
cells_up_to_connect = []
num_points = len(connect_points)
for i in range(num_points):
p1 = i
p2 = (i + 1) % num_points
p3 = num_points + (i + 1) % num_points
p4 = num_points + i
cells_up_to_connect.append([3, p1, p2, p3])
cells_up_to_connect.append([3, p1, p3, p4])
combined_points = np.vstack((connect_points, bottom))
mesh_up_to_connect = pv.PolyData(combined_points, np.hstack(cells_up_to_connect))
cylinder_polydata = cylinder_polydata.merge(mesh_up_to_connect)
## plotter.add_mesh(mesh_up_to_connect, color='black', style='wireframe')
mesh = pv.PolyData(points, np.hstack(cells))
cylinder_polydata = cylinder_polydata.merge(mesh)
## plotter.add_mesh(cylinder_polydata, color='black', opacity=0.5)
return cylinder_polydata
def visualize_data(folder_path):
processed_files = set()
plotter = pv.Plotter()
gif_filename = f"animation.gif"
plotter.open_gif(gif_filename)
mesh1_added = False
mesh2_added = False
update_mesh2 = True
frame_count = 0
last_time = time.time()
text_actor = None
hz_actor = None
while True:
if keyboard.is_pressed('y'):
update_mesh2 = True
elif keyboard.is_pressed('n'):
update_mesh2 = False
files = [filename for filename in os.listdir(folder_path) if filename.endswith('.txt')]
new_files = [f for f in files if f not in processed_files]
if len(new_files) >= 8:
count_files = 0
eight_grids = []
up_points = []
bottom_points = []
for file in new_files:
count_files += 1
if count_files <= 8:
filename = os.path.join(folder_path, file)
raw_data = read_sensor_data_file(filename)
new_data = preprocess_data(raw_data)
translation_vector = [0.0, 0.0, 0.0]
X, Y, Z = interpolate_to_surface(new_data)
grid = pv.StructuredGrid(X, Y, Z)
rotation_vector = [0.0, 0.0, 0.0, 0, -90, 0]
combined_matrix = grid_transform(grid, translation_vector, rotation_vector)
rotation_vector = [0.0, 0.0, 0.0, 0, 0, 45 * count_files]
combined_matrix = grid_transform(grid, translation_vector, rotation_vector)
up_points.append(grid.points[-8:].tolist()[::-1])
bottom_points.append(grid.points[:8].tolist()[::-1])
eight_grids.append(grid)
processed_files.add(file)
else:
break
if len(eight_grids) == 8:
combined_polydata = pv.PolyData()
for i in range(len(eight_grids) - 1):
current_grid = eight_grids[i]
next_grid = eight_grids[i + 1]
bridge = create_bridge_between_grids(current_grid, next_grid)
combined_polydata = combined_polydata.merge(current_grid.extract_surface())
combined_polydata = combined_polydata.merge(bridge)
if i == len(eight_grids) - 2:
combined_polydata = combined_polydata.merge(next_grid.extract_surface())
final_bridge = create_bridge_between_grids(eight_grids[-1], eight_grids[0])
combined_polydata = combined_polydata.merge(final_bridge)
up_points_array = np.vstack(up_points)
bottom_points_array = np.vstack(bottom_points)
cylinder_polydata = plot_cylinder(up_points_array, bottom_points_array)
## combined_polydata = combined_polydata.merge(cylinder_polydata)
distances = np.sqrt(combined_polydata.points[:, 0]**2 + combined_polydata.points[:, 1]**2)
combined_polydata['Distances'] = distances
if not mesh1_added:
combined_polydata = combined_polydata
mesh_actor = plotter.add_mesh(combined_polydata, scalars="Distances", cmap='viridis', show_scalar_bar=True)
mesh1_added = True
elif mesh1_added:
combined_polydata = combined_polydata
mesh_actor.GetMapper().SetInputData(combined_polydata)
mesh_actor.GetMapper().Update()
if update_mesh2:
cylinder_polydata = cylinder_polydata
if not mesh2_added:
mesh_actor2 = plotter.add_mesh(cylinder_polydata, color='grey', opacity=1)
mesh2_added = True
elif mesh2_added:
cylinder_polydata = cylinder_polydata
mesh_actor2.GetMapper().SetInputData(cylinder_polydata)
mesh_actor2.GetMapper().Update()
else:
if mesh2_added and mesh_actor2 is not None:
plotter.remove_actor(mesh_actor2)
mesh_actor2 = None
mesh2_added = False
current_time = time.time()
elapsed_time = current_time - last_time
last_time = current_time
hz = 1.0 / elapsed_time if elapsed_time else 0
if text_actor:
plotter.remove_actor(text_actor)
if hz_actor:
plotter.remove_actor(hz_actor)
frame_text = f"Frame: {frame_count}"
text_actor = plotter.add_text(frame_text, position='upper_left', font_size=10, color='black')
hz_text = f"Hz: {hz:.2f}"
## hz_actor = plotter.add_text(hz_text, position='upper_right', font_size=10, color='black')
plotter.write_frame()
frame_count += 1
if __name__ == "__main__":
visualize_data("data")