forked from RobotLocomotion/drake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeometry_visualization.cc
205 lines (176 loc) · 7.53 KB
/
geometry_visualization.cc
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
#include "drake/geometry/geometry_visualization.h"
#include <string>
#include <vector>
#include "drake/common/drake_copyable.h"
#include "drake/geometry/geometry_state.h"
#include "drake/geometry/geometry_system.h"
#include "drake/geometry/internal_geometry.h"
#include "drake/geometry/shape_specification.h"
#include "drake/lcm/drake_lcm.h"
#include "drake/lcmt_viewer_geometry_data.hpp"
#include "drake/math/rotation_matrix.h"
namespace drake {
namespace geometry {
namespace {
// Simple class for converting shape specifications into LCM-compatible shapes.
class ShapeToLcm : public ShapeReifier {
public:
DRAKE_NO_COPY_NO_MOVE_NO_ASSIGN(ShapeToLcm)
ShapeToLcm() = default;
~ShapeToLcm() override = default;
lcmt_viewer_geometry_data Convert(const Shape& shape,
const Isometry3<double>& X_PG,
const Eigen::Vector4d& in_color) {
X_PG_ = X_PG;
// NOTE: Reify *may* change X_PG_ based on the shape. For example, the
// half-space requires an additional offset to shift the box representing
// the plane *to* the plane.
shape.Reify(this);
// Saves the location and orientation of the visualization geometry in the
// `lcmt_viewer_geometry_data` object. The location and orientation are
// specified in the body's frame.
Eigen::Map<Eigen::Vector3f> position(geometry_data_.position);
position = X_PG_.translation().cast<float>();
// LCM quaternion must be w, x, y, z.
Eigen::Quaternion<double> q(X_PG_.linear());
geometry_data_.quaternion[0] = q.w();
geometry_data_.quaternion[1] = q.x();
geometry_data_.quaternion[2] = q.y();
geometry_data_.quaternion[3] = q.z();
Eigen::Map<Eigen::Vector4f> color(geometry_data_.color);
color = in_color.cast<float>();
return geometry_data_;
}
void ImplementGeometry(const Sphere& sphere, void*) override {
geometry_data_.type = geometry_data_.SPHERE;
geometry_data_.num_float_data = 1;
geometry_data_.float_data.push_back(static_cast<float>(
sphere.get_radius()));
}
void ImplementGeometry(const Cylinder& cylinder, void*) override {
geometry_data_.type = geometry_data_.CYLINDER;
geometry_data_.num_float_data = 2;
geometry_data_.float_data.push_back(static_cast<float>(
cylinder.get_radius()));
geometry_data_.float_data.push_back(static_cast<float>(
cylinder.get_length()));
}
void ImplementGeometry(const HalfSpace&, void*) override {
// Currently representing a half space as a big box. This assumes that the
// underlying box representation is centered on the origin.
geometry_data_.type = geometry_data_.BOX;
geometry_data_.num_float_data = 3;
// Box width, height, and thickness.
geometry_data_.float_data.push_back(50);
geometry_data_.float_data.push_back(50);
const float thickness = 1;
geometry_data_.float_data.push_back(thickness);
// The final pose of the box is the half-space's pose pre-multiplied by
// an offset sufficient to move the box down so it's top face lies on the
// z = 0 plane.
Isometry3<double> box_xform = Isometry3<double>::Identity();
// Shift it down so that the origin lies on the top surface.
box_xform.translation() << 0, 0, -thickness / 2;
X_PG_ = X_PG_ * box_xform;
}
void ImplementGeometry(const Mesh& mesh, void*) override {
geometry_data_.type = geometry_data_.MESH;
geometry_data_.num_float_data = 3;
geometry_data_.float_data.push_back(static_cast<float>(mesh.scale()));
geometry_data_.float_data.push_back(static_cast<float>(mesh.scale()));
geometry_data_.float_data.push_back(static_cast<float>(mesh.scale()));
geometry_data_.string_data = mesh.filename();
}
private:
lcmt_viewer_geometry_data geometry_data_{};
// The transform from the geometry frame to its parent frame.
Eigen::Isometry3d X_PG_;
};
lcmt_viewer_geometry_data MakeGeometryData(const Shape& shape,
const Isometry3<double>& X_PG,
const Eigen::Vector4d& in_color) {
ShapeToLcm converter;
return converter.Convert(shape, X_PG, in_color);
}
} // namespace
namespace internal {
lcmt_viewer_load_robot GeometryVisualizationImpl::BuildLoadMessage(
const GeometryState<double>& state) {
using internal::InternalAnchoredGeometry;
using internal::InternalGeometry;
lcmt_viewer_load_robot message{};
// Populate the message.
const int frame_count = state.get_num_frames();
const int anchored_count =
static_cast<int>(state.anchored_geometry_index_id_map_.size());
// Include the world frame as one of the frames (if there are anchored
// geometries).
int total_link_count = frame_count + (anchored_count > 0 ? 1 : 0);
message.num_links = total_link_count;
message.link.resize(total_link_count);
int link_index = 0;
// Load anchored geometry into the world frame.
{
if (anchored_count) {
message.link[0].name = "world";
message.link[0].robot_num = 0;
message.link[0].num_geom = anchored_count;
message.link[0].geom.resize(anchored_count);
int geom_index = 0;
for (const auto& pair : state.anchored_geometries_) {
const InternalAnchoredGeometry& geometry = pair.second;
const Shape& shape = geometry.get_shape();
const Eigen::Vector4d& color =
geometry.get_visual_material().diffuse();
message.link[0].geom[geom_index] = MakeGeometryData(
shape, geometry.get_pose_in_parent(), color);
++geom_index;
}
link_index = 1;
}
}
// Load dynamic geometry into their own frames.
for (const auto& pair : state.frames_) {
const internal::InternalFrame& frame = pair.second;
SourceId s_id = state.get_source_id(frame.get_id());
const std::string& src_name = state.get_source_name(s_id);
// TODO(SeanCurtis-TRI): The name in the load message *must* match the name
// in the update message. Make sure this code and the GeometrySystem output
// use a common code-base to translate (source_id, frame) -> name.
message.link[link_index].name = src_name + "::" + frame.get_name();
message.link[link_index].robot_num = frame.get_frame_group();
const int geom_count = static_cast<int>(
frame.get_child_geometries().size());
message.link[link_index].num_geom = geom_count;
message.link[link_index].geom.resize(geom_count);
int geom_index = 0;
for (GeometryId geom_id : frame.get_child_geometries()) {
const InternalGeometry& geometry = state.geometries_.at(geom_id);
GeometryIndex index = geometry.get_engine_index();
const Isometry3<double> X_FG = state.X_FG_.at(index);
const Shape& shape = geometry.get_shape();
const Eigen::Vector4d& color =
geometry.get_visual_material().diffuse();
message.link[link_index].geom[geom_index] =
MakeGeometryData(shape, X_FG, color);
++geom_index;
}
++link_index;
}
return message;
}
} // namespace internal
void DispatchLoadMessage(const GeometryState<double>& state) {
using lcm::DrakeLcm;
lcmt_viewer_load_robot message =
internal::GeometryVisualizationImpl::BuildLoadMessage(state);
// Send a load message.
DrakeLcm lcm;
Publish(&lcm, "DRAKE_VIEWER_LOAD_ROBOT", message);
}
void DispatchLoadMessage(const GeometrySystem<double>& system) {
system.ThrowIfContextAllocated("DisplatchLoadMessage");
DispatchLoadMessage(*system.initial_state_);
}
} // namespace geometry
} // namespace drake