-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvector_alignment_vizualization_utils.py
More file actions
540 lines (460 loc) · 16.2 KB
/
vector_alignment_vizualization_utils.py
File metadata and controls
540 lines (460 loc) · 16.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
import plotly.graph_objects as go
import numpy as np
import matplotlib.colors as mcolors
from scipy.__config__ import show
#def color_name_to_rgb(color_name):
#return mcolors.to_rgb(color_name)
def color_name_to_rgb(color_name):
"""
Convert a color name to an RGB tuple.
Uses an extended color list if the color name is not recognized by Matplotlib.
Parameters:
- color_name: The color name (e.g., 'red', 'teal', 'turquoise').
Returns:
- A tuple representing the RGB color.
"""
# Extended color list with hex codes
extended_colors = {
"teal": "#008080", # Pair 1
"coral": "#FF7F50",
"navy": "#000080", # Pair 2
"gold": "#FFD700",
"crimson": "#DC143C", # Pair 3
"turquoise": "#40E0D0",
"mint": "#98FF98", # Pair 4
"salmon": "#FA8072",
"lavender": "#E6E6FA", # Pair 5
"olive": "#808000",
"lime": "#00FF00", # Pair 6
"maroon": "#800000",
"indigo": "#4B0082", # Pair 7
"orange": "#FFA500",
"cyan": "#00FFFF", # Pair 8
"magenta": "#FF00FF",
"violet": "#EE82EE", # Pair 9
"brown": "#A52A2A",
"royalblue": "#4169E1", # Pair 10
"yellow": "#FFFF00",
"pink": "#FFC0CB", # Pair 11
"darkgreen": "#006400",
"skyblue": "#87CEEB", # Pair 12
"chocolate": "#D2691E",
"plum": "#DDA0DD", # Pair 13
"forestgreen": "#228B22",
"slategray": "#708090", # Pair 14
"lightcoral": "#F08080",
"darkorchid": "#9932CC", # Pair 15
"chartreuse": "#7FFF00",
# Add more colors as needed
}
# Check in the extended color list first
if color_name.lower() in extended_colors:
hex_color = extended_colors[color_name.lower()]
return mcolors.hex2color(hex_color)
# If not found in extended list, use Matplotlib's named colors
try:
return mcolors.to_rgb(color_name)
except ValueError:
raise ValueError(f"Color '{color_name}' is not recognized. Use a valid color name or hex code.")
def viz1pcl(v1_array, color="red", marker_size=3, opacity=1, legend_label='Point Cloud', show_legend=True,save_figure=False, save_path='test.pdf', file_format='pdf'):
"""
Visualize a 3D point cloud using Plotly with a transparent sphere surface.
Parameters:
- v1_array: A (n, 3) array of 3D points.
- color: Color of the point cloud.
- marker_size: Size of the markers.
- opacity: Opacity of the markers.
- legend_label: Label for the point cloud in the legend.
"""
if v1_array.shape[1] != 3:
raise ValueError("v1_array must have shape (n, 3)")
# Create the scatter plot for v1_array
trace = go.Scatter3d(
x=v1_array[:, 0],
y=v1_array[:, 1],
z=v1_array[:, 2],
mode="markers",
marker=dict(size=marker_size, color=color, opacity=opacity),
name=legend_label
)
data = [trace]
# Generate a transparent sphere surface
phi = np.linspace(0, np.pi, 100)
theta = np.linspace(0, 2 * np.pi, 100)
phi, theta = np.meshgrid(phi, theta)
x = np.sin(phi) * np.cos(theta)
y = np.sin(phi) * np.sin(theta)
z = np.cos(phi)
sphere_surface = go.Surface(
x=x,
y=y,
z=z,
opacity=0.03, # Set a low opacity for the sphere
colorscale=[[0, 'blue'], [1, 'blue']],
showscale=False,
name='Sphere Surface'
)
data.append(sphere_surface)
# Define Layout
layout = go.Layout(
title="3D Point Cloud Visualization",
scene=dict(
xaxis=dict(visible=False, showticklabels=False),
yaxis=dict(visible=False, showticklabels=False),
zaxis=dict(visible=False, showticklabels=False),
aspectmode="cube", # Equal aspect ratio
bgcolor="white" # Set background color to white
),
margin=dict(l=0, r=0, b=0, t=0), # Tight Layout
width=800,
height=600,
showlegend=True,
legend=dict(
x=0.95, # x-position in the lower right corner
y=0.05, # y-position in the lower right corner
xanchor="right",
yanchor="bottom",
bgcolor="rgba(255, 255, 255, 0.8)", # Slightly transparent white background
font=dict(size=16),
showlegend=show_legend
)
)
# Create figure and add trace
fig = go.Figure(data=data, layout=layout)
# Additional updates to layout
fig.update_layout(
scene=dict(
xaxis=dict(visible=False, showticklabels=False),
yaxis=dict(visible=False, showticklabels=False),
zaxis=dict(visible=False, showticklabels=False),
camera=dict(eye=dict(x=0.9, y=-1.1, z=0.4)) # Set camera position
)
)
# Add the reference frame with X, Y, Z axes
fig.add_trace(go.Scatter3d(
x=[0, 0.25, 0, 0, 0, 0],
y=[0, 0, 0, 0.25, 0, 0],
z=[0, 0, 0, 0, 0, 0.25],
mode='lines',
line=dict(width=6, color='red'),
name='X Axis',
showlegend=False
))
fig.add_trace(go.Scatter3d(
x=[0, 0],
y=[0, 0.25],
z=[0, 0],
mode='lines',
line=dict(width=6, color='green'),
name='Y Axis',
showlegend=False
))
fig.add_trace(go.Scatter3d(
x=[0, 0],
y=[0, 0],
z=[0, 0.25],
mode='lines',
line=dict(width=6, color='blue'),
name='Z Axis',
showlegend=False
))
# Origin point
fig.add_trace(go.Scatter3d(
x=[0],
y=[0],
z=[0],
mode='markers',
marker=dict(size=8, color='black'),
name='Origin',
showlegend=False
))
if save_figure:
if save_path is not None:
if file_format == 'pdf':
fig.write_image(save_path)
fig.show()
def viz2pcl(v1_array, v2_array=None, color1="coral", color2="royalblue", marker_size=3, opacity=1,show_legend=True, legend_1='Destination', legend_2='Source_gt', save_figure=False, save_path='test.pdf', file_format='pdf'):
"""
Visualize one or two 3D point clouds using Plotly with a transparent sphere surface.
Parameters:
- v1_array: A (n, 3) array of 3D points.
- v2_array: An optional (m, 3) array of 3D points for the second point cloud.
- color1: Color of the first point cloud.
- color2: Color of the second point cloud.
- marker_size: Size of the markers.
- opacity: Opacity of the markers.
- legend_1: Label for the first point cloud in the legend.
- legend_2: Label for the second point cloud in the legend.
"""
if v1_array.shape[1] != 3:
raise ValueError("v1_array must have shape (n, 3)")
# Create the scatter plot for v1_array
trace1 = go.Scatter3d(
x=v1_array[:, 0],
y=v1_array[:, 1],
z=v1_array[:, 2],
mode="markers",
marker=dict(size=marker_size, color=color1, opacity=opacity),
name=legend_1,
showlegend=show_legend
)
data = [trace1]
# Create the scatter plot for v2_array if provided
if v2_array is not None:
if v2_array.shape[1] != 3:
raise ValueError("v2_array must have shape (m, 3)")
trace2 = go.Scatter3d(
x=v2_array[:, 0],
y=v2_array[:, 1],
z=v2_array[:, 2],
mode="markers",
marker=dict(size=marker_size, color=color2, opacity=opacity),
name=legend_2,
showlegend=show_legend
)
data.append(trace2)
# Generate a transparent sphere surface
phi = np.linspace(0, np.pi, 100)
theta = np.linspace(0, 2 * np.pi, 100)
phi, theta = np.meshgrid(phi, theta)
x = np.sin(phi) * np.cos(theta)
y = np.sin(phi) * np.sin(theta)
z = np.cos(phi)
sphere_surface = go.Surface(
x=x,
y=y,
z=z,
opacity=0.03, # Set a low opacity for the sphere
colorscale=[[0, 'blue'], [1, 'blue']],
showscale=False,
name='Sphere Surface'
)
data.append(sphere_surface)
# Define Layout
layout = go.Layout(
title="3D Point Cloud Visualization",
scene=dict(
xaxis=dict(visible=False, showticklabels=False),
yaxis=dict(visible=False, showticklabels=False),
zaxis=dict(visible=False, showticklabels=False),
aspectmode="cube", # Equal aspect ratio
bgcolor="white" # Set background color to white
),
margin=dict(l=0, r=0, b=0, t=0), # Tight Layout
width=800,
height=600,
showlegend=True,
legend=dict(
x=0.95, # x-position in the lower right corner
y=0.05, # y-position in the lower right corner
xanchor="right",
yanchor="bottom",
bgcolor="rgba(255, 255, 255, 0.8)", # Slightly transparent white background
font=dict(size=16)
)
)
# Create figure and add trace
fig = go.Figure(data=data, layout=layout)
# Additional updates to layout
fig.update_layout(
scene=dict(
xaxis=dict(visible=False, showticklabels=False),
yaxis=dict(visible=False, showticklabels=False),
zaxis=dict(visible=False, showticklabels=False),
camera=dict(eye=dict(x=0.9, y=-1.1, z=0.4)) # Set camera position
)
)
# Add the reference frame with X, Y, Z axes
fig.add_trace(go.Scatter3d(
x=[0, 0.25, 0, 0, 0, 0],
y=[0, 0, 0, 0.25, 0, 0],
z=[0, 0, 0, 0, 0, 0.25],
mode='lines',
line=dict(width=6, color='red'),
name='X Axis',
showlegend=False
))
fig.add_trace(go.Scatter3d(
x=[0, 0],
y=[0, 0.25],
z=[0, 0],
mode='lines',
line=dict(width=6, color='green'),
name='Y Axis',
showlegend=False
))
fig.add_trace(go.Scatter3d(
x=[0, 0],
y=[0, 0],
z=[0, 0.25],
mode='lines',
line=dict(width=6, color='blue'),
name='Z Axis',
showlegend=False
))
# Origin point
fig.add_trace(go.Scatter3d(
x=[0],
y=[0],
z=[0],
mode='markers',
marker=dict(size=8, color='black'),
name='Origin',
showlegend=False
))
if save_figure:
if save_path is not None:
if file_format == 'pdf':
fig.write_image(save_path)
fig.show()
def viz3pcl(v1_array, v2_array=None, v3_array=None, color1="coral", color2="royalblue", color3="teal",show_legend=True, marker_size=3, opacity=1, legend_1='Destination', legend_2='Source_gt', legend_3='Source_aligned',save_figure=False,save_path='test.pdf',file_format='pdf'):
"""
Visualize up to three 3D point clouds using Plotly with a transparent sphere surface.
Parameters:
- v1_array: A (n, 3) array of 3D points.
- v2_array: An optional (m, 3) array of 3D points for the second point cloud.
- v3_array: An optional (o, 3) array of 3D points for the third point cloud.
- color1: Color of the first point cloud.
- color2: Color of the second point cloud.
- color3: Color of the third point cloud.
- marker_size: Size of the markers.
- opacity: Opacity of the markers.
- legend_1: Label for the first point cloud in the legend.
- legend_2: Label for the second point cloud in the legend.
- legend_3: Label for the third point cloud in the legend.
"""
if v1_array.shape[1] != 3:
raise ValueError("v1_array must have shape (n, 3)")
# Create the scatter plot for v1_array
trace1 = go.Scatter3d(
x=v1_array[:, 0],
y=v1_array[:, 1],
z=v1_array[:, 2],
mode="markers",
marker=dict(size=marker_size, color=color1, opacity=opacity),
name=legend_1,
showlegend=show_legend
)
data = [trace1]
# Create the scatter plot for v2_array if provided
if v2_array is not None:
if v2_array.shape[1] != 3:
raise ValueError("v2_array must have shape (m, 3)")
trace2 = go.Scatter3d(
x=v2_array[:, 0],
y=v2_array[:, 1],
z=v2_array[:, 2],
mode="markers",
marker=dict(size=marker_size, color=color2, opacity=opacity),
name=legend_2,
showlegend=show_legend
)
data.append(trace2)
# Create the scatter plot for v3_array if provided
if v3_array is not None:
if v3_array.shape[1] != 3:
raise ValueError("v3_array must have shape (o, 3)")
trace3 = go.Scatter3d(
x=v3_array[:, 0],
y=v3_array[:, 1],
z=v3_array[:, 2],
mode="markers",
marker=dict(size=marker_size, color=color3, opacity=opacity),
name=legend_3,
showlegend=show_legend
)
data.append(trace3)
# Generate a transparent sphere surface
phi = np.linspace(0, np.pi, 100)
theta = np.linspace(0, 2 * np.pi, 100)
phi, theta = np.meshgrid(phi, theta)
x = np.sin(phi) * np.cos(theta)
y = np.sin(phi) * np.sin(theta)
z = np.cos(phi)
sphere_surface = go.Surface(
x=x,
y=y,
z=z,
opacity=0.03, # Set a low opacity for the sphere
colorscale=[[0, 'blue'], [1, 'blue']],
showscale=False,
name='Sphere Surface'
)
data.append(sphere_surface)
# Define Layout
layout = go.Layout(
title="3D Point Cloud Visualization",
scene=dict(
xaxis=dict(visible=False, showticklabels=False),
yaxis=dict(visible=False, showticklabels=False),
zaxis=dict(visible=False, showticklabels=False),
aspectmode="cube", # Equal aspect ratio
bgcolor="white" # Set background color to white
),
margin=dict(l=0, r=0, b=0, t=0), # Tight Layout
width=800,
height=600,
showlegend=True,
legend=dict(
x=0.95, # x-position in the lower right corner
y=0.05, # y-position in the lower right corner
xanchor="right",
yanchor="bottom",
bgcolor="rgba(255, 255, 255, 0.8)", # Slightly transparent white background
font=dict(size=16) # Font size for the legend
)
)
# Create figure and add trace
fig = go.Figure(data=data, layout=layout)
# Additional updates to layout
fig.update_layout(
scene=dict(
xaxis=dict(visible=False, showticklabels=False),
yaxis=dict(visible=False, showticklabels=False),
zaxis=dict(visible=False, showticklabels=False),
camera=dict(eye=dict(x=0.9, y=-1.1, z=0.4)) # Set camera position
)
)
# Add the reference frame with X, Y, Z axes
fig.add_trace(go.Scatter3d(
x=[0, 0.25, 0, 0, 0, 0],
y=[0, 0, 0, 0.25, 0, 0],
z=[0, 0, 0, 0, 0, 0.25],
mode='lines',
line=dict(width=6, color='red'),
name='X Axis',
showlegend=False
))
fig.add_trace(go.Scatter3d(
x=[0, 0],
y=[0, 0.25],
z=[0, 0],
mode='lines',
line=dict(width=6, color='green'),
name='Y Axis',
showlegend=False
))
fig.add_trace(go.Scatter3d(
x=[0, 0],
y=[0, 0],
z=[0, 0.25],
mode='lines',
line=dict(width=6, color='blue'),
name='Z Axis',
showlegend=False
))
# Origin point
fig.add_trace(go.Scatter3d(
x=[0],
y=[0],
z=[0],
mode='markers',
marker=dict(size=8, color='black'),
name='Origin',
showlegend=False
))
if save_figure:
if save_path is not None:
if file_format == 'pdf':
fig.write_image(save_path)
fig.show()