-
Notifications
You must be signed in to change notification settings - Fork 25
/
visualizer.jl
280 lines (251 loc) · 10.3 KB
/
visualizer.jl
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
"""
visualize(mechanism, storage; vis, build, show_contact, animation, color, name)
visualize mechanism using trajectory from storage
mechanism: Mechanism
storage: Storage
vis: Visualizer
build: flag to construct mechanism visuals (only needs to be built once)
show_contact: flag to show contact locations on system
color: RGBA
name: unique identifier for mechanism
"""
function visualize(mechanism::Mechanism, storage::Storage{T,N};
vis::Visualizer=Visualizer(),
framerate=60, # Inf for 1/timestep
build::Bool=true,
show_joint=false,
joint_radius=0.1,
show_contact=false,
show_frame=false,
animation=nothing,
color=nothing,
name::Symbol=:robot,
return_animation=false,
visualize_floor=true) where {T,N}
storage = deepcopy(storage)
bodies = mechanism.bodies
origin = mechanism.origin
# Build robot in the visualizer
build && build_robot(mechanism;
vis, show_joint, show_contact, show_frame, color, name, visualize_floor)
# Create animations
timestep_max = 1/framerate
time_factor = Int64(maximum([1;floor(timestep_max/mechanism.timestep)]))
framerate = Int64(round(1/(mechanism.timestep*time_factor)))
(animation === nothing) && (animation =
MeshCat.Animation(Dict{MeshCat.SceneTrees.Path,MeshCat.AnimationClip}(), framerate))
# Bodies and Contacts
for (id, body) in enumerate(bodies)
shape = body.shape
visshape = convert_shape(shape)
subvisshape = nothing
showshape = false
if visshape !== nothing
subvisshape = vis[name][:bodies][Symbol(body.name, "__id_$id")]
showshape = true
end
animate_node!(storage, id, shape, animation, subvisshape, showshape, time_factor)
if show_joint
for (jd, joint) in enumerate(mechanism.joints)
if joint.child_id == body.id
radius = 0.1
joint_shape = Sphere(radius,
position_offset=joint.translational.vertices[2],
color=RGBA(0.0, 0.0, 1.0, 0.5))
visshape = convert_shape(joint_shape)
subvisshape = nothing
showshape = false
if visshape !== nothing
subvisshape = vis[name][:joints][Symbol(joint.name, "__id_$(jd)")]
showshape = true
end
animate_node!(storage, id, joint_shape, animation, subvisshape, showshape, time_factor)
end
end
end
if show_contact
for (jd, contact) in enumerate(mechanism.contacts)
if contact.parent_id == body.id
radius = abs(contact.model.collision.contact_radius)
(radius == 0.0) && (radius = 0.01)
contact_shape = Sphere(radius,
position_offset=contact.model.collision.contact_origin, #TODO: generalize for collision checking
orientation_offset=one(Quaternion),
color=RGBA(1.0, 0.0, 0.0, 0.5))
visshape = convert_shape(contact_shape)
subvisshape = nothing
showshape = false
if visshape !== nothing
subvisshape = vis[name][:contacts][Symbol(contact.name, "__id_$(jd)")]
showshape = true
end
animate_node!(storage, id, contact_shape, animation, subvisshape, showshape, time_factor)
end
end
end
if show_frame
frame_shape = FrameShape(scale=0.33*ones(3))
visshape = convert_shape(frame_shape)
subvisshape = vis[name][:frames][Symbol(body.name, "__id_$id")]
showshape = true
animate_node!(storage, id, frame_shape, animation, subvisshape, showshape, time_factor)
end
end
# Origin
id = origin.id
shape = origin.shape
visshape = convert_shape(shape)
if visshape !== nothing
subvisshape = vis[name][:bodies][Symbol(:origin, "_id")]
shapetransform = transform(szeros(T,3), one(Quaternion{T}), shape)
settransform!(subvisshape, shapetransform)
end
setanimation!(vis, animation)
return_animation ? (return vis, animation) : (return vis)
end
"""
build_robot(mechanism; vis, show_contact, name, color)
construct visuals for mechanism
mechanism: Mechanism
vis: Visualizer
show_contact: flag to show contact locations on mechanism
name: unique identifier
color: RGBA
"""
function build_robot(mechanism::Mechanism;
vis::Visualizer=Visualizer(),
show_joint=false,
joint_radius=0.1,
show_contact=false,
show_frame=false,
name::Symbol=:robot,
color=nothing,
visualize_floor=true)
bodies = mechanism.bodies
origin = mechanism.origin
set_background!(vis)
set_light!(vis)
visualize_floor && set_floor!(vis)
# Bodies and Contacts
for (id, body) in enumerate(bodies)
if color !== nothing
shape = deepcopy(body.shape)
set_color!(shape, color)
else
shape = body.shape
end
visshape = convert_shape(shape)
subvisshape = nothing
if visshape !== nothing
subvisshape = vis[name][:bodies][Symbol(body.name, "__id_$id")]
setobject!(subvisshape, visshape, shape,
transparent=(show_joint || show_contact))
end
if show_joint
for (jd, joint) in enumerate(mechanism.joints)
if joint.child_id == body.id
radius = joint_radius
joint_shape = Sphere(radius,
position_offset=joint.translational.vertices[2],
color=RGBA(0.0, 0.0, 1.0, 0.5))
visshape = convert_shape(joint_shape)
subvisshape = nothing
if visshape !== nothing
subvisshape = vis[name][:joints][Symbol(joint.name, "__id_$(jd)")]
setobject!(subvisshape, visshape, joint_shape,
transparent=false)
end
end
end
end
if show_contact
for (jd, contact) in enumerate(mechanism.contacts)
if contact.parent_id == body.id
radius = abs(contact.model.collision.contact_radius)
(radius == 0.0) && (radius = 0.01)
contact_shape = Sphere(radius,
position_offset=(contact.model.collision.contact_origin),
orientation_offset=one(Quaternion), color=RGBA(1.0, 0.0, 0.0, 0.5))
visshape = convert_shape(contact_shape)
subvisshape = nothing
if visshape !== nothing
subvisshape = vis[name][:contacts][Symbol(contact.name, "__id_$(jd)")]
setobject!(subvisshape,visshape,contact_shape,transparent=false)
end
end
end
end
if show_frame
frame_shape = FrameShape(scale=0.33*ones(3))
visshape = convert_shape(frame_shape)
subvisshape = vis[name][:frames][Symbol(body.name, "__id_$id")]
setobject!(subvisshape, visshape, frame_shape,
transparent=false)
end
end
# Origin
id = origin.id
shape = origin.shape
visshape = convert_shape(shape)
if visshape !== nothing
subvisshape = vis[name][:bodies][Symbol(:origin, "_id")]
setobject!(subvisshape,visshape,shape,transparent=(show_joint || show_contact))
end
return vis
end
function transform(x, q, shape)
scale_transform = MeshCat.LinearMap(diagm(shape.scale))
x_transform = MeshCat.Translation(x + vector_rotate(shape.position_offset, q))
q_transform = MeshCat.LinearMap(q * shape.orientation_offset)
return MeshCat.compose(x_transform, q_transform, scale_transform)
end
MeshCat.js_scaling(s::AbstractVector) = s
MeshCat.js_position(p::AbstractVector) = p
function set_node!(x, q, shape, shapevisualizer, showshape)
if showshape
# TODO currently setting props directly because MeshCat/Rotations doesn't convert scaled rotation properly.
# If this changes, do similarily to origin
setprop!(shapevisualizer, "scale", MeshCat.js_scaling(shape.scale))
setprop!(shapevisualizer, "position", MeshCat.js_position(x + vector_rotate(shape.position_offset, q)))
setprop!(shapevisualizer, "quaternion", MeshCat.js_quaternion(q * shape.orientation_offset))
end
return
end
function animate_node!(storage::Storage{T,N}, id, shape, animation, shapevisualizer, showshape, time_factor) where {T,N}
frame_id = 1
for i=1:time_factor:N
x = storage.x[id][i]
q = storage.q[id][i]
atframe(animation, frame_id) do
set_node!(x, q, shape, shapevisualizer, showshape)
end
frame_id += 1
end
return
end
function MeshCat.setobject!(subvisshape, visshapes::Vector, shape::CombinedShapes; transparent=false)
for (i,visshape) in enumerate(visshapes)
v = subvisshape["shape"*string(i)]
s = shape.shapes[i]
setobject!(v, visshape, s; transparent)
scale_transform = MeshCat.LinearMap(diagm(s.scale))
x_transform = MeshCat.Translation(s.position_offset)
q_transform = MeshCat.LinearMap(s.orientation_offset)
t = MeshCat.compose(x_transform, q_transform, scale_transform)
settransform!(v, t)
end
end
function MeshCat.setobject!(subvisshape, visshape, shape::Shape; transparent=false)
setobject!(subvisshape, visshape, MeshPhongMaterial(color=(transparent ? RGBA(0.75, 0.75, 0.75, 0.5) : shape.color)))
end
function MeshCat.setobject!(subvisshape, visshape, shape::FrameShape; transparent=false)
setobject!(subvisshape, visshape)
end
function MeshCat.setobject!(subvisshape, visshape, shape::Mesh; transparent=false)
if visshape.mtl_library == ""
visshape = MeshFileGeometry(visshape.contents, visshape.format)
setobject!(subvisshape, visshape, MeshPhongMaterial(color=(transparent ? RGBA(0.75, 0.75, 0.75, 0.5) : shape.color)))
else
setobject!(subvisshape, visshape)
end
end