Skip to content

Commit b2b0ee8

Browse files
committed
Map canonical links to their models
Signed-off-by: Ashton Larkin <[email protected]>
1 parent 2683276 commit b2b0ee8

File tree

7 files changed

+718
-148
lines changed

7 files changed

+718
-148
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
/*
2+
* Copyright (C) 2021 Open Source Robotics Foundation
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
#ifndef IGNITION_GAZEBO_COMPONENTS_REFERENCEMODELS_HH_
18+
#define IGNITION_GAZEBO_COMPONENTS_REFERENCEMODELS_HH_
19+
20+
#include <iostream>
21+
#include <set>
22+
23+
#include <ignition/gazebo/Entity.hh>
24+
#include <ignition/gazebo/components/Factory.hh>
25+
#include <ignition/gazebo/components/Component.hh>
26+
#include <ignition/gazebo/config.hh>
27+
28+
namespace ignition
29+
{
30+
namespace gazebo
31+
{
32+
// Inline bracket to help doxygen filtering.
33+
inline namespace IGNITION_GAZEBO_VERSION_NAMESPACE {
34+
namespace components
35+
{
36+
/// \brief Data structure that holds information about which models for
37+
/// a given link view this link as the model's canonical link. An example
38+
/// of multiple models having the same canonical link could be nested models.
39+
struct ReferenceModelsInfo
40+
{
41+
/// \brief A topological ordering of the models which view this link as its
42+
/// canonical link.
43+
std::set<Entity> models;
44+
45+
/// \brief Add a model that references this link as its canonical link.
46+
/// This should be called when a canonical link is assigned to a model.
47+
/// \param[in] _model The model to be added.
48+
public: void AddModel(const Entity &_model)
49+
{
50+
this->models.insert(_model);
51+
}
52+
53+
/// \brief Remove a model that no longer references this link as its
54+
/// canonical link. This should be called when a model is removed/deleted.
55+
/// \param[in] _model The model to be removed.
56+
public: void RemoveModel(const Entity &_model)
57+
{
58+
this->models.erase(_model);
59+
}
60+
61+
public: bool operator==(const ReferenceModelsInfo &_info) const
62+
{
63+
return this->models == _info.models;
64+
}
65+
66+
public: bool operator!=(const ReferenceModelsInfo &_info) const
67+
{
68+
return !(*this == _info);
69+
}
70+
};
71+
}
72+
73+
namespace serializers
74+
{
75+
/// \brief Serializer for ReferenceModelsInfo object
76+
class ReferenceModelsInfoSerializer
77+
{
78+
/// \brief Serialization for ReferenceModelsInfo.
79+
/// \param[out] _out Output stream.
80+
/// \param[in] _info ReferenceModelsInfo object to stream.
81+
/// \return The stream.
82+
public: static std::ostream &Serialize(std::ostream &_out,
83+
const components::ReferenceModelsInfo &_info)
84+
{
85+
for (const auto &model : _info.models)
86+
_out << model << " ";
87+
88+
return _out;
89+
}
90+
91+
/// \brief Deserialization for ReferenceModelsInfo.
92+
/// \param[in] _in Input stream.
93+
/// \param[out] _info ReferenceModelsInfo object to populate.
94+
/// \return The stream.
95+
public: static std::istream &Deserialize(std::istream &_in,
96+
components::ReferenceModelsInfo &_info)
97+
{
98+
_info.models.clear();
99+
Entity model;
100+
while (_in >> model)
101+
_info.AddModel(model);
102+
103+
return _in;
104+
};
105+
};
106+
}
107+
108+
namespace components
109+
{
110+
/// \brief A component that gives a mapping between a link and all of the
111+
/// models this link serves as a canonical link for. The models in the
112+
/// mapping are in topological order. This component should only be applied
113+
/// to links.
114+
using ReferenceModels =
115+
Component<ReferenceModelsInfo, class ReferenceModelsTag,
116+
serializers::ReferenceModelsInfoSerializer>;
117+
IGN_GAZEBO_REGISTER_COMPONENT("ign_gazebo_components.ReferenceModels",
118+
ReferenceModels)
119+
}
120+
}
121+
}
122+
}
123+
124+
#endif

src/SdfEntityCreator.cc

+16
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
#include "ignition/gazebo/components/ParentEntity.hh"
6060
#include "ignition/gazebo/components/Physics.hh"
6161
#include "ignition/gazebo/components/Pose.hh"
62+
#include "ignition/gazebo/components/ReferenceModels.hh"
6263
#include "ignition/gazebo/components/RgbdCamera.hh"
6364
#include "ignition/gazebo/components/Scene.hh"
6465
#include "ignition/gazebo/components/SelfCollide.hh"
@@ -405,6 +406,21 @@ Entity SdfEntityCreator::CreateEntities(const sdf::Model *_model,
405406
{
406407
this->dataPtr->ecm->CreateComponent(
407408
modelEntity, components::ModelCanonicalLink(canonicalLinkEntity));
409+
410+
// Map this canonical link to the model. Since a link may be the
411+
// canonical link for multiple models (example: nested models), create
412+
// a component that completes this mapping if it doesn't already exist.
413+
if (!this->dataPtr->ecm->Component<components::ReferenceModels>(
414+
canonicalLinkEntity))
415+
{
416+
components::ReferenceModelsInfo compInfo;
417+
this->dataPtr->ecm->CreateComponent(canonicalLinkEntity,
418+
components::ReferenceModels(compInfo));
419+
}
420+
auto refModelComp =
421+
this->dataPtr->ecm->Component<components::ReferenceModels>(
422+
canonicalLinkEntity);
423+
refModelComp->Data().AddModel(modelEntity);
408424
}
409425
else
410426
{

0 commit comments

Comments
 (0)