-
Notifications
You must be signed in to change notification settings - Fork 252
/
SceneBroadcaster.cc
371 lines (308 loc) · 11.4 KB
/
SceneBroadcaster.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
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
/*
* Copyright (C) 2018 Open Source Robotics Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#include <ignition/msgs/scene.pb.h>
#include <ignition/math/graph/Graph.hh>
#include <ignition/plugin/RegisterMore.hh>
#include <ignition/transport/Node.hh>
#include "ignition/gazebo/components/Geometry.hh"
#include "ignition/gazebo/components/Link.hh"
#include "ignition/gazebo/components/Material.hh"
#include "ignition/gazebo/components/Model.hh"
#include "ignition/gazebo/components/Name.hh"
#include "ignition/gazebo/components/ParentEntity.hh"
#include "ignition/gazebo/components/Pose.hh"
#include "ignition/gazebo/components/Visual.hh"
#include "ignition/gazebo/components/World.hh"
#include "ignition/gazebo/Conversions.hh"
#include "ignition/gazebo/EntityComponentManager.hh"
#include "ignition/gazebo/systems/SceneBroadcaster.hh"
using namespace ignition;
using namespace gazebo;
using namespace systems;
//////////////////////////////////////////////////
void AddVisuals(msgs::LinkSharedPtr _msg,
const EntityId _id,
const math::graph::DirectedGraph<
std::shared_ptr<google::protobuf::Message>, bool> &_graph)
{
if (!_msg)
return;
for (const auto &vertex : _graph.AdjacentsFrom(_id))
{
auto visualMsg = std::dynamic_pointer_cast<msgs::Visual>(
vertex.second.get().Data());
if (!visualMsg)
continue;
_msg->add_visual()->CopyFrom(*visualMsg.get());
}
}
//////////////////////////////////////////////////
void AddLinks(msgs::ModelSharedPtr _msg,
const EntityId _id,
const math::graph::DirectedGraph<
std::shared_ptr<google::protobuf::Message>, bool> &_graph)
{
if (!_msg)
return;
for (const auto &vertex : _graph.AdjacentsFrom(_id))
{
auto linkMsg = std::dynamic_pointer_cast<msgs::Link>(
vertex.second.get().Data());
if (!linkMsg)
continue;
// Visuals
AddVisuals(linkMsg, vertex.second.get().Id(), _graph);
_msg->add_link()->CopyFrom(*linkMsg.get());
}
}
//////////////////////////////////////////////////
template<typename T>
void AddModels(T _msg,
const EntityId _id,
const math::graph::DirectedGraph<
std::shared_ptr<google::protobuf::Message>, bool> &_graph)
{
for (const auto &vertex : _graph.AdjacentsFrom(_id))
{
auto modelMsg = std::dynamic_pointer_cast<msgs::Model>(
vertex.second.get().Data());
if (!modelMsg)
continue;
// Nested models
AddModels(modelMsg, vertex.second.get().Id(), _graph);
// Links
AddLinks(modelMsg, vertex.second.get().Id(), _graph);
_msg->add_model()->CopyFrom(*modelMsg.get());
}
}
// Private data class.
class ignition::gazebo::systems::SceneBroadcasterPrivate
{
/// \brief Setup Ignition transport services and publishers
/// \param[in] _worldName Name of world.
public: void SetupTransport(const std::string &_worldName);
/// \brief Callback for scene info service.
/// \param[out] _res Response containing the latest scene message.
/// \return True if successful.
public: bool SceneInfoService(ignition::msgs::Scene &_res);
/// \brief Callback for scene graph service.
/// \param[out] _res Response containing the the scene graph in DOT format.
/// \return True if successful.
public: bool SceneGraphService(ignition::msgs::StringMsg &_res);
/// \brief Transport node.
public: transport::Node node;
/// \brief Pose publisher.
public: transport::Node::Publisher posePub;
/// \brief Graph containing latest information from entities.
public: math::graph::DirectedGraph<
std::shared_ptr<google::protobuf::Message>, bool> sceneGraph;
/// \brief Keep the id of the world entity so we know how to traverse the
/// graph.
public: EntityId worldId;
/// \brief Protects scene graph.
public: std::mutex graphMutex;
};
//////////////////////////////////////////////////
SceneBroadcaster::SceneBroadcaster()
: System(), dataPtr(std::make_unique<SceneBroadcasterPrivate>())
{
}
//////////////////////////////////////////////////
SceneBroadcaster::~SceneBroadcaster()
{
}
//////////////////////////////////////////////////
void SceneBroadcaster::PostUpdate(const UpdateInfo &/*_info*/,
const EntityComponentManager &_manager)
{
std::lock_guard<std::mutex> lock(this->dataPtr->graphMutex);
// TODO(louise) Get <scene> from SDF
// TODO(louise) Fill message header
// Populate a graph with latest information from all entities
// TODO(louise) once we know what entities are added/deleted process only
// those. For now, recreating graph at every iteration.
this->dataPtr->sceneGraph = math::graph::DirectedGraph<
std::shared_ptr<google::protobuf::Message>, bool>();
// Populate pose message
msgs::Pose_V poseMsg;
// World
// \todo(anyone) It would be convenient to have the following functions:
// * _manager.Has<components::World, components::Name>: to tell whether there
// is an entity which has a given set of components.
// * _manager.EntityCount<components::World, components::Name>: returns the
// number of entities which have all the given components.
this->dataPtr->worldId = kNullEntity;
_manager.Each<components::World,
components::Name>(
[&](const EntityId &_entity,
const components::World */*_worldComp*/,
const components::Name *_nameComp)
{
if (kNullEntity != this->dataPtr->worldId)
{
ignerr << "Internal error, more than one world found." << std::endl;
return;
}
this->dataPtr->worldId = _entity;
if (!this->dataPtr->posePub)
{
this->dataPtr->SetupTransport(_nameComp->Data());
}
// Add to graph
this->dataPtr->sceneGraph.AddVertex(_nameComp->Data(), nullptr, _entity);
});
if (kNullEntity == this->dataPtr->worldId)
{
ignerr << "Failed to find world entity" << std::endl;
return;
}
// Models
_manager.Each<components::Model,
components::Name,
components::ParentEntity,
components::Pose>(
[&](const EntityId &_entity,
const components::Model */*_modelComp*/,
const components::Name *_nameComp,
const components::ParentEntity *_parentComp,
const components::Pose *_poseComp)
{
auto modelMsg = std::make_shared<msgs::Model>();
modelMsg->set_id(_entity);
modelMsg->set_name(_nameComp->Data());
modelMsg->mutable_pose()->CopyFrom(msgs::Convert(
_poseComp->Data()));
// Add to graph
this->dataPtr->sceneGraph.AddVertex(_nameComp->Data(), modelMsg, _entity);
this->dataPtr->sceneGraph.AddEdge({_parentComp->Id(), _entity}, true);
// Add to pose msg
auto pose = poseMsg.add_pose();
msgs::Set(pose, _poseComp->Data());
pose->set_name(_nameComp->Data());
pose->set_id(_entity);
});
// Links
_manager.Each<components::Link,
components::Name,
components::ParentEntity,
components::Pose>(
[&](const EntityId &_entity,
const components::Link */*_linkComp*/,
const components::Name *_nameComp,
const components::ParentEntity *_parentComp,
const components::Pose *_poseComp)
{
auto linkMsg = std::make_shared<msgs::Link>();
linkMsg->set_id(_entity);
linkMsg->set_name(_nameComp->Data());
linkMsg->mutable_pose()->CopyFrom(msgs::Convert(
_poseComp->Data()));
// Add to graph
this->dataPtr->sceneGraph.AddVertex(_nameComp->Data(), linkMsg, _entity);
this->dataPtr->sceneGraph.AddEdge({_parentComp->Id(), _entity}, true);
// Add to pose msg
auto pose = poseMsg.add_pose();
msgs::Set(pose, _poseComp->Data());
pose->set_name(_nameComp->Data());
pose->set_id(_entity);
});
// Visuals
_manager.Each<components::Visual,
components::Name,
components::ParentEntity,
components::Pose>(
[&](const EntityId &_entity,
const components::Visual */*_visualComp*/,
const components::Name *_nameComp,
const components::ParentEntity *_parentComp,
const components::Pose *_poseComp)
{
auto visualMsg = std::make_shared<msgs::Visual>();
visualMsg->set_id(_entity);
visualMsg->set_parent_id(_parentComp->Id());
visualMsg->set_name(_nameComp->Data());
visualMsg->mutable_pose()->CopyFrom(msgs::Convert(
_poseComp->Data()));
// Geometry is optional
auto geometryComp = _manager.Component<components::Geometry>(_entity);
if (geometryComp)
{
visualMsg->mutable_geometry()->CopyFrom(
Convert<msgs::Geometry>(geometryComp->Data()));
}
// Material is optional
auto materialComp = _manager.Component<components::Material>(_entity);
if (materialComp)
{
visualMsg->mutable_material()->CopyFrom(
Convert<msgs::Material>(materialComp->Data()));
}
// Add to graph
this->dataPtr->sceneGraph.AddVertex(
_nameComp->Data(), visualMsg, _entity);
this->dataPtr->sceneGraph.AddEdge(
{_parentComp->Id(), _entity}, true);
// Add to pose msg
auto pose = poseMsg.add_pose();
msgs::Set(pose, _poseComp->Data());
pose->set_name(_nameComp->Data());
pose->set_id(_entity);
});
this->dataPtr->posePub.Publish(poseMsg);
}
//////////////////////////////////////////////////
void SceneBroadcasterPrivate::SetupTransport(const std::string &_worldName)
{
// Scene info service
std::string infoService{"/world/" + _worldName + "/scene/info"};
this->node.Advertise(infoService, &SceneBroadcasterPrivate::SceneInfoService,
this);
ignmsg << "Serving scene information on [" << infoService << "]" << std::endl;
// Scene graph service
std::string graphService{"/world/" + _worldName + "/scene/graph"};
this->node.Advertise(graphService,
&SceneBroadcasterPrivate::SceneGraphService, this);
ignmsg << "Serving scene graph on [" << graphService << "]" << std::endl;
// Pose info publisher
std::string topic{"/world/" + _worldName + "/pose/info"};
transport::AdvertiseMessageOptions advertOpts;
advertOpts.SetMsgsPerSec(60);
this->posePub = this->node.Advertise<msgs::Pose_V>(topic, advertOpts);
ignmsg << "Publishing pose messages on [" << topic << "]" << std::endl;
}
//////////////////////////////////////////////////
bool SceneBroadcasterPrivate::SceneInfoService(ignition::msgs::Scene &_res)
{
std::lock_guard<std::mutex> lock(this->graphMutex);
_res.Clear();
// Populate scene message
AddModels(&_res, this->worldId, this->sceneGraph);
return true;
}
//////////////////////////////////////////////////
bool SceneBroadcasterPrivate::SceneGraphService(ignition::msgs::StringMsg &_res)
{
std::lock_guard<std::mutex> lock(this->graphMutex);
_res.Clear();
std::stringstream graphStr;
graphStr << this->sceneGraph;
_res.set_data(graphStr.str());
return true;
}
IGNITION_ADD_PLUGIN(ignition::gazebo::systems::SceneBroadcaster,
ignition::gazebo::System,
SceneBroadcaster::ISystemPostUpdate)