File size: 3,653 Bytes
4893ce0
 
 
 
164964b
 
 
 
 
 
 
 
 
 
 
 
4893ce0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import plotly.graph_objects as go
import open3d as o3d
import numpy as np
import textwrap
import torch
import random


def set_seed():
    seed = 123
    torch.manual_seed(seed)
    torch.cuda.manual_seed(seed)
    torch.cuda.manual_seed_all(seed)
    np.random.seed(seed)
    random.seed(seed)


def read_pcd(pcd_path):
    pcd = o3d.io.read_point_cloud(pcd_path)
    xyz = np.asarray(pcd.points)
    rgb = np.asarray(pcd.colors)
    if not pcd.has_normals():
        pcd.estimate_normals(search_param=o3d.geometry.KDTreeSearchParamHybrid(radius=0.1, max_nn=100))
    normal = np.asarray(pcd.normals)
    return xyz, rgb, normal

def render_pcd_file(pcd_path):
    pcd = o3d.io.read_point_cloud(pcd_path)
    xyz = np.asarray(pcd.points)
    rgb = np.asarray(pcd.colors)
    return render_point_cloud(xyz, rgb)

def render_point_cloud(xyz, rgb, legend=None):
    x = xyz[:, 0]
    y = xyz[:, 1]
    z = xyz[:, 2]
    rgb = rgb * 255
    hex_colors = [f'#{int(r):02x}{int(g):02x}{int(b):02x}' for r, g, b in rgb]

    fig = go.Figure(data=[go.Scatter3d(x=x, y=y, z=z, mode='markers',
                                    marker=dict(size=2, color=hex_colors, colorscale='Viridis', opacity=0.8))])
    
    if legend:
        fig.add_annotation(x=0.5, y=1.25, text="<br>".join(textwrap.wrap(legend, width=30)), showarrow=False, xref="paper", yref="paper")

    # Customize layout
    fig.update_layout(
        scene=dict(
            xaxis=dict(title='x', showgrid=False, zeroline=False, visible=False),
            yaxis=dict(title='y', showgrid=False, zeroline=False, visible=False),
            zaxis=dict(title='z', showgrid=False, zeroline=False, visible=False),
            aspectmode='manual'),
        scene_camera=dict(
            up=dict(x=0, y=1, z=0),  # Adjust these values for your point cloud
            eye=dict(x=0, y=-0.9, z=2),  # Increase the values to move further away
            center = dict(x=0,y=0,z=0)
        )
    )
    # fig.update_layout(
    #     height=450,
    #     autosize=True,
    #     hovermode=False,
    #     margin=go.layout.Margin(l=0, r=0, b=0, t=0),
    #     showlegend=False,
    #     legend=dict(
    #         yanchor='bottom',
    #         y=0.01,
    #         xanchor='right',
    #         x=0.99,
    #     ),
    #     scene=dict(
    #         aspectmode='manual',
    #         aspectratio=dict(x=1, y=1, z=1.0),
    #         camera=dict(
    #             eye=dict(x=base_radius - 1.6, y=0.0, z=0.6),
    #             center=dict(x=0.0, y=0.0, z=0.0),
    #             up=dict(x=0.0, y=0.0, z=1.0)),
    #         xaxis_title='',
    #         yaxis_title='',
    #         zaxis_title='',
    #         xaxis=dict(
    #             range=[-scene_bounds, scene_bounds],
    #             showticklabels=False,
    #             showgrid=True,
    #             zeroline=False,
    #             showbackground=True,
    #             showspikes=False,
    #             showline=False,
    #             ticks=''),
    #         yaxis=dict(
    #             range=[-scene_bounds, scene_bounds],
    #             showticklabels=False,
    #             showgrid=True,
    #             zeroline=False,
    #             showbackground=True,
    #             showspikes=False,
    #             showline=False,
    #             ticks=''),
    #         zaxis=dict(
    #             range=[-scene_bounds, scene_bounds],
    #             showticklabels=False,
    #             showgrid=True,
    #             zeroline=False,
    #             showbackground=True,
    #             showspikes=False,
    #             showline=False,
    #             ticks='')))

    return fig