-
Notifications
You must be signed in to change notification settings - Fork 3
/
vis.py
198 lines (174 loc) · 8.26 KB
/
vis.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
# -----------------------------------------------------------------------------
# SPDX-License-Identifier: MIT
# This file is part of the RDF project.
# Copyright (c) 2023 Idiap Research Institute <[email protected]>
# Contributor: Yimming Li <[email protected]>
# -----------------------------------------------------------------------------
import torch
import os
from panda_layer.panda_layer import PandaLayer
import bf_sdf
import matplotlib.pyplot as plt
import numpy as np
import trimesh
import utils
import argparse
def plot_2D_panda_sdf(pose,theta,bp_sdf,nbData,model,device):
domain_0 = torch.linspace(-1.0,1.0,nbData).to(device)
domain_1 = torch.linspace(-1.0,1.0,nbData).to(device)
grid_x, grid_y= torch.meshgrid(domain_0,domain_1)
p1 = torch.stack([grid_x.reshape(-1),grid_y.reshape(-1),torch.zeros_like(grid_x.reshape(-1))],dim=1)
p2 = torch.stack([torch.zeros_like(grid_x.reshape(-1)),grid_x.reshape(-1)*0.4, grid_y.reshape(-1)*0.4+0.375],dim=1)
p3 = torch.stack([grid_x.reshape(-1)*0.4 + 0.2,torch.zeros_like(grid_x.reshape(-1)),grid_y.reshape(-1)*0.4+0.375],dim=1)
grid_x, grid_y= grid_x.detach().cpu().numpy(), grid_y.detach().cpu().numpy()
plt.figure(figsize=(10,10))
plt.rc('font', size=25)
p2_split = torch.split(p2,1000,dim=0)
sdf,ana_grad = [],[]
for p_2 in p2_split:
sdf_split,ana_grad_split = bp_sdf.get_whole_body_sdf_batch(p_2,pose,theta,model,use_derivative=True)
sdf_split,ana_grad_split = sdf_split.squeeze(),ana_grad_split.squeeze()
sdf.append(sdf_split)
ana_grad.append(ana_grad_split)
sdf = torch.cat(sdf,dim=0)
ana_grad = torch.cat(ana_grad,dim=0)
p2 = p2.detach().cpu().numpy()
sdf =sdf.squeeze().reshape(nbData,nbData).detach().cpu().numpy()
ct1 = plt.contour(grid_x*0.4,grid_y*0.4+0.375,sdf,levels=12)
plt.clabel(ct1, inline=False, fontsize=10)
ana_grad_2d = -torch.nn.functional.normalize(ana_grad[:,[1,2]],dim=-1)*0.01
p2_3d = p2.reshape(nbData,nbData,3)
ana_grad_3d = ana_grad_2d.reshape(nbData,nbData,2)
plt.quiver(p2_3d[0:-1:4,0:-1:4,1],p2_3d[0:-1:4,0:-1:4,2],ana_grad_3d[0:-1:4,0:-1:4,0].detach().cpu().numpy(),ana_grad_3d[0:-1:4,0:-1:4,1].detach().cpu().numpy(),scale=0.5,color = [0.1,0.1,0.1])
plt.title('YoZ')
plt.show()
# plt.subplot(1,3,3)
plt.figure(figsize=(10,10))
plt.rc('font', size=25)
p3_split = torch.split(p3,1000,dim=0)
sdf,ana_grad = [],[]
for p_3 in p3_split:
sdf_split,ana_grad_split = bp_sdf.get_whole_body_sdf_batch(p_3,pose,theta,model,use_derivative=True)
sdf_split,ana_grad_split = sdf_split.squeeze(),ana_grad_split.squeeze()
sdf.append(sdf_split)
ana_grad.append(ana_grad_split)
sdf = torch.cat(sdf,dim=0)
ana_grad = torch.cat(ana_grad,dim=0)
p3 = p3.detach().cpu().numpy()
sdf =sdf.squeeze().reshape(nbData,nbData).detach().cpu().numpy()
ct1 = plt.contour(grid_x*0.4+0.2,grid_y*0.4+0.375,sdf,levels=12)
plt.clabel(ct1, inline=False, fontsize=10)
ana_grad_2d = -torch.nn.functional.normalize(ana_grad[:,[0,2]],dim=-1)*0.01
p3_3d = p3.reshape(nbData,nbData,3)
ana_grad_3d = ana_grad_2d.reshape(nbData,nbData,2)
plt.quiver(p3_3d[0:-1:4,0:-1:4,0],p3_3d[0:-1:4,0:-1:4,2],ana_grad_3d[0:-1:4,0:-1:4,0].detach().cpu().numpy(),ana_grad_3d[0:-1:4,0:-1:4,1].detach().cpu().numpy(),scale=0.5,color = [0.1,0.1,0.1])
plt.title('XoZ')
plt.show()
def plot_3D_panda_with_gradient(pose,theta,bp_sdf,model,device):
robot_mesh = panda.get_forward_robot_mesh(pose, theta)[0]
robot_mesh = np.sum(robot_mesh)
surface_points = robot_mesh.vertices
scene = trimesh.Scene()
# robot mesh
scene.add_geometry(robot_mesh)
choice = np.random.choice(len(surface_points), 1024, replace=False)
surface_points = surface_points[choice]
p =torch.from_numpy(surface_points).float().to(device)
ball_query = trimesh.creation.uv_sphere(1).vertices
choice_ball = np.random.choice(len(ball_query), 1024, replace=False)
ball_query = ball_query[choice_ball]
p = p + torch.from_numpy(ball_query).float().to(device)*0.5
sdf,ana_grad = bp_sdf.get_whole_body_sdf_batch(p,pose,theta,model,use_derivative=True,used_links = [0,1,2,3,4,5,6,7,8])
sdf,ana_grad = sdf.squeeze().detach().cpu().numpy(),ana_grad.squeeze().detach().cpu().numpy()
# points
pts = p.detach().cpu().numpy()
colors = np.zeros_like(pts,dtype=object)
colors[:,0] = np.abs(sdf)*400
# pc =trimesh.PointCloud(pts,colors)
# scene.add_geometry(pc)
# gradients
for i in range(len(pts)):
dg = ana_grad[i]
if dg.sum() ==0:
continue
c = colors[i]
print(c)
m = utils.create_arrow(-dg,pts[i],vec_length = 0.05,color=c)
scene.add_geometry(m)
scene.show()
def generate_panda_mesh_sdf_points(max_dist =0.10):
# represent SDF using basis functions
import glob
import mesh_to_sdf
mesh_path = os.path.dirname(os.path.realpath(__file__)) + "/panda_layer/meshes/voxel_128/*"
mesh_files = glob.glob(mesh_path)
mesh_files = sorted(mesh_files)[1:] #except finger
mesh_dict = {}
for i,mf in enumerate(mesh_files):
mesh_name = mf.split('/')[-1].split('.')[0]
print(mesh_name)
mesh = trimesh.load(mf)
mesh_dict[i] = {}
mesh_dict[i]['mesh_name'] = mesh_name
vert = mesh.vertices
points = vert + np.random.uniform(-max_dist,max_dist,size=vert.shape)
sdf = random_sdf = mesh_to_sdf.mesh_to_sdf(mesh,
points,
surface_point_method='scan',
sign_method='normal',
bounding_radius=None,
scan_count=100,
scan_resolution=400,
sample_point_count=10000000,
normal_sample_count=100)
mesh_dict[i]['points'] = points
mesh_dict[i]['sdf'] = sdf
np.save('data/panda_mesh_sdf.npy',mesh_dict)
def vis_panda_sdf(pose, theta,device):
data = np.load('data/panda_mesh_sdf.npy',allow_pickle=True).item()
trans = panda.get_transformations_each_link(pose,theta)
pts = []
for i,k in enumerate(data.keys()):
points = data[k]['points']
sdf = data[k]['sdf']
print(points.shape, sdf.shape)
choice = (sdf <0.05) * (sdf>0.045)
points = points[choice]
sdf = sdf[choice]
sample = np.random.choice(len(points), 128, replace=True)
points,sdf = points[sample], sdf[sample]
points = torch.from_numpy(points).float().to(device)
ones = torch.ones([len(points), 1],device =device).float()
points = torch.cat([points, ones], dim=-1)
t = trans[i].squeeze()
print(points.shape,t.shape)
trans_points = torch.matmul(t,points.t()).t()[:,:3]
pts.append(trans_points)
pts = torch.cat(pts,dim=0).detach().cpu().numpy()
print(pts.shape)
scene = trimesh.Scene()
robot_mesh = panda.get_forward_robot_mesh(pose, theta)[0]
robot_mesh = np.sum(robot_mesh)
scene.add_geometry(robot_mesh)
pc =trimesh.PointCloud(pts,colors = [255,0,0])
scene.add_geometry(pc)
scene.show()
if __name__ =='__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--device', default='cuda', type=str)
parser.add_argument('--domain_max', default=1.0, type=float)
parser.add_argument('--domain_min', default=-1.0, type=float)
parser.add_argument('--n_func', default=8, type=float)
parser.add_argument('--train', action='store_true')
args = parser.parse_args()
panda = PandaLayer(args.device)
bp_sdf = bf_sdf.BPSDF(args.n_func,args.domain_min,args.domain_max,panda,args.device)
# load model
model = torch.load(f'models/BP_{args.n_func}.pt')
# initial the robot configuration
theta = torch.tensor([0, -0.3, 0, -2.2, 0, 2.0, np.pi/4]).float().to(args.device).reshape(-1,7)
pose = torch.from_numpy(np.identity(4)).unsqueeze(0).to(args.device).expand(len(theta),4,4).float()
# # vis 2D SDF with gradient
# plot_2D_panda_sdf(pose,theta,bp_sdf,nbData=80,model=model,device=args.device)
# vis 3D SDF with gradient
plot_3D_panda_with_gradient(pose,theta,bp_sdf,model=model,device=args.device)