-
Notifications
You must be signed in to change notification settings - Fork 58
/
visualization_utils.py
225 lines (187 loc) · 7.83 KB
/
visualization_utils.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
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
# Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved.
#
# NVIDIA CORPORATION and its licensors retain all intellectual property
# and proprietary rights in and to this software, related documentation
# and any modifications thereto. Any use, reproduction, disclosure or
# distribution of this software and related documentation without an express
# license agreement from NVIDIA CORPORATION is strictly prohibited.
from __future__ import print_function
import mayavi.mlab as mlab
import tf_utils
import numpy as np
import sample
import trimesh
def get_color_plasma_org(x):
import matplotlib.pyplot as plt
return tuple([x for i, x in enumerate(plt.cm.plasma(x)) if i < 3])
def get_color_plasma(x):
return tuple([float(1 - x), float(x) , float(0)])
def plot_mesh(mesh):
assert type(mesh) == trimesh.base.Trimesh
mlab.triangular_mesh (
mesh.vertices[:, 0],
mesh.vertices[:, 1],
mesh.vertices[:, 2],
mesh.faces,
colormap='Blues'
)
def draw_scene(
pc,
grasps=[],
grasp_scores=None,
grasp_color=None,
gripper_color=(0,1,0),
mesh=None,
show_gripper_mesh=False,
grasps_selection=None,
visualize_diverse_grasps=False,
min_seperation_distance=0.03,
pc_color=None,
plasma_coloring=False):
"""
Draws the 3D scene for the object and the scene.
Args:
pc: point cloud of the object
grasps: list of 4x4 numpy array indicating the transformation of the grasps.
grasp_scores: grasps will be colored based on the scores. If left
empty, grasps are visualized in green.
grasp_color: if it is a tuple, sets the color for all the grasps. If list
is provided it is the list of tuple(r,g,b) for each grasp.
mesh: If not None, shows the mesh of the object. Type should be trimesh
mesh.
show_gripper_mesh: If True, shows the gripper mesh for each grasp.
grasp_selection: if provided, filters the grasps based on the value of
each selection. 1 means select ith grasp. 0 means exclude the grasp.
visualize_diverse_grasps: sorts the grasps based on score. Selects the
top score grasp to visualize and then choose grasps that are not within
min_seperation_distance distance of any of the previously selected
grasps. Only set it to True to declutter the grasps for better
visualization.
pc_color: if provided, should be a n x 3 numpy array for color of each
point in the point cloud pc. Each number should be between 0 and 1.
plasma_coloring: If True, sets the plasma colormap for visualizting the
pc.
"""
max_grasps = 200
grasps = np.array(grasps)
if grasp_scores is not None:
grasp_scores = np.array(grasp_scores)
if len(grasps) > max_grasps:
print('Downsampling grasps, there are too many')
chosen_ones = np.random.randint(low=0, high=len(grasps), size=max_grasps)
grasps = grasps[chosen_ones]
if grasp_scores is not None:
grasp_scores = grasp_scores[chosen_ones]
if mesh is not None:
if type(mesh) == list:
for elem in mesh:
plot_mesh(elem)
else:
plot_mesh(mesh)
if pc_color is None and pc is not None:
if plasma_coloring:
mlab.points3d(pc[:, 0], pc[:, 1], pc[:, 2], pc[:, 2], colormap='plasma')
else:
mlab.points3d(pc[:, 0], pc[:, 1], pc[:, 2], color=(0.1,0.1,1),scale_factor=0.01)
elif pc is not None:
if plasma_coloring:
mlab.points3d(pc[:, 0], pc[:, 1], pc[:, 2], pc_color[:, 0], colormap='plasma')
else:
rgba = np.zeros((pc.shape[0], 4), dtype=np.uint8)
rgba[:, :3] = np.asarray(pc_color)
rgba[:, 3] = 255
src = mlab.pipeline.scalar_scatter(pc[:, 0], pc[:, 1], pc[:, 2])
src.add_attribute(rgba, 'colors')
src.data.point_data.set_active_scalars('colors')
g = mlab.pipeline.glyph(src)
g.glyph.scale_mode = "data_scaling_off"
g.glyph.glyph.scale_factor = 0.01
grasp_pc = np.squeeze(tf_utils.get_control_point_tensor(1, False), 0)
print(grasp_pc.shape)
grasp_pc[2, 2] = 0.059
grasp_pc[3, 2] = 0.059
mid_point = 0.5*(grasp_pc[2, :] + grasp_pc[3, :])
modified_grasp_pc = []
modified_grasp_pc.append(np.zeros((3,), np.float32))
modified_grasp_pc.append(mid_point)
modified_grasp_pc.append(grasp_pc[2])
modified_grasp_pc.append(grasp_pc[4])
modified_grasp_pc.append(grasp_pc[2])
modified_grasp_pc.append(grasp_pc[3])
modified_grasp_pc.append(grasp_pc[5])
grasp_pc = np.asarray(modified_grasp_pc)
def transform_grasp_pc(g):
output = np.matmul(grasp_pc, g[:3, :3].T)
output += np.expand_dims(g[:3, 3], 0)
return output
if grasp_scores is not None:
indexes = np.argsort(-np.asarray(grasp_scores))
else:
indexes = range(len(grasps))
print('draw scene ', len(grasps))
selected_grasps_so_far = []
removed = 0
if grasp_scores is not None:
min_score = np.min(grasp_scores)
max_score = np.max(grasp_scores)
top5 = np.array(grasp_scores).argsort()[-5:][::-1]
for ii in range(len(grasps)):
i = indexes[ii]
if grasps_selection is not None:
if grasps_selection[i] == False:
continue
g = grasps[i]
is_diverse = True
for prevg in selected_grasps_so_far:
distance = np.linalg.norm(prevg[:3, 3] - g[:3, 3])
if distance < min_seperation_distance:
is_diverse = False
break
if visualize_diverse_grasps:
if not is_diverse:
removed += 1
continue
else:
if grasp_scores is not None:
print('selected', i, grasp_scores[i], min_score, max_score)
else:
print('selected', i)
selected_grasps_so_far.append(g)
if isinstance(gripper_color, list):
pass
elif grasp_scores is not None:
normalized_score = (grasp_scores[i] - min_score) / (max_score - min_score + 0.0001)
if grasp_color is not None:
gripper_color = grasp_color[ii]
else:
gripper_color = get_color_plasma(normalized_score)
if min_score == 1.0:
gripper_color = (0.0, 1.0, 0.0)
if show_gripper_mesh:
gripper_mesh = sample.Object('panda_gripper.obj').mesh
gripper_mesh.apply_transform(g)
mlab.triangular_mesh(
gripper_mesh.vertices[:, 0],
gripper_mesh.vertices[:, 1],
gripper_mesh.vertices[:, 2],
gripper_mesh.faces,
color=gripper_color,
opacity=1 if visualize_diverse_grasps else 0.5
)
else:
pts = np.matmul(grasp_pc, g[:3, :3].T)
pts += np.expand_dims(g[:3, 3], 0)
if isinstance(gripper_color, list):
mlab.plot3d(pts[:, 0], pts[:, 1], pts[:, 2], color=gripper_color[i], tube_radius=0.003, opacity=1)
else:
tube_radius = 0.001
mlab.plot3d(pts[:, 0], pts[:, 1], pts[:, 2], color=gripper_color, tube_radius=tube_radius, opacity=1)
print('removed {} similar grasps'.format(removed))
def get_axis():
# hacky axis for mayavi
axis = np.array([[1,0,0], [0,1,0], [0,0,1]])
axis_x = np.array([np.linspace(0, 0.10, 50), np.zeros(50), np.zeros(50)]).T
axis_y = np.array([np.zeros(50), np.linspace(0, 0.10, 50), np.zeros(50)]).T
axis_z = np.array([np.zeros(50), np.zeros(50), np.linspace(0, 0.10, 50)]).T
axis = np.concatenate([axis_x, axis_y, axis_z], axis=0)
return axis