Skip to content

Commit fb4a177

Browse files
committed
Add a "ReferenceModels" component
Signed-off-by: Ashton Larkin <[email protected]>
1 parent 1924de9 commit fb4a177

File tree

3 files changed

+335
-120
lines changed

3 files changed

+335
-120
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
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 The models which this link serves as a canonical link for.
42+
std::set<Entity> models;
43+
44+
/// \brief Add a model that references this link as its canonical link.
45+
/// This should be called when a canonical link is assigned to a model.
46+
/// \param[in] _model The model to be added.
47+
public: void AddModel(const Entity &_model)
48+
{
49+
this->models.insert(_model);
50+
}
51+
52+
/// \brief Remove a model that no longer references this link as its
53+
/// canonical link. This should be called when a model is removed/deleted.
54+
/// \param[in] _model The model to be removed.
55+
public: void RemoveModel(const Entity &_model)
56+
{
57+
this->models.erase(_model);
58+
}
59+
60+
public: bool operator==(const ReferenceModelsInfo &_info) const
61+
{
62+
return this->models == _info.models;
63+
}
64+
65+
public: bool operator!=(const ReferenceModelsInfo &_info) const
66+
{
67+
return !(*this == _info);
68+
}
69+
};
70+
}
71+
72+
namespace serializers
73+
{
74+
/// \brief Serializer for ReferenceModelsInfo object
75+
class ReferenceModelsInfoSerializer
76+
{
77+
/// \brief Serialization for ReferenceModelsInfo.
78+
/// \param[out] _out Output stream.
79+
/// \param[in] _info ReferenceModelsInfo object to stream.
80+
/// \return The stream.
81+
public: static std::ostream &Serialize(std::ostream &_out,
82+
const components::ReferenceModelsInfo &_info)
83+
{
84+
for (const auto &model : _info.models)
85+
_out << model << " ";
86+
87+
return _out;
88+
}
89+
90+
/// \brief Deserialization for ReferenceModelsInfo.
91+
/// \param[in] _in Input stream.
92+
/// \param[out] _info ReferenceModelsInfo object to populate.
93+
/// \return The stream.
94+
public: static std::istream &Deserialize(std::istream &_in,
95+
components::ReferenceModelsInfo &_info)
96+
{
97+
_info.models.clear();
98+
Entity model;
99+
while (_in >> model)
100+
_info.AddModel(model);
101+
102+
return _in;
103+
};
104+
};
105+
}
106+
107+
namespace components
108+
{
109+
/// \brief A component that gives a mapping between a link and all of the
110+
/// models this link serves as a canonical link for. This component should
111+
/// only be applied to links.
112+
using ReferenceModels =
113+
Component<ReferenceModelsInfo, class ReferenceModelsTag,
114+
serializers::ReferenceModelsInfoSerializer>;
115+
IGN_GAZEBO_REGISTER_COMPONENT("ign_gazebo_components.ReferenceModels",
116+
ReferenceModels)
117+
}
118+
}
119+
}
120+
}
121+
122+
#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)