Gazebo Sim

API Reference

8.7.0
CanonicalLinkModelTracker.hh
Go to the documentation of this file.
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 GZ_SIM_SYSTEMS_PHYSICS_CANONICAL_LINK_MODEL_TRACKER_HH_
18 #define GZ_SIM_SYSTEMS_PHYSICS_CANONICAL_LINK_MODEL_TRACKER_HH_
19 
20 #include <set>
21 #include <unordered_map>
22 
23 #include "gz/sim/Entity.hh"
27 #include "gz/sim/config.hh"
28 
29 namespace gz::sim
30 {
31 inline namespace GZ_SIM_VERSION_NAMESPACE {
32 namespace systems::physics_system
33 {
56  {
59  public: void AddNewModels(const EntityComponentManager &_ecm);
60 
63  public: void AddAllModels(const EntityComponentManager &_ecm);
64 
70  public: const std::set<Entity> &CanonicalLinkModels(
71  const Entity _canonicalLink) const;
72 
76  public: void RemoveLink(const Entity &_link);
77 
82  private: std::unordered_map<Entity, std::set<Entity>> linkModelMap;
83 
86  private: static inline const std::set<Entity> kEmptyModelOrdering{};
87  };
88 
89  void CanonicalLinkModelTracker::AddNewModels(
90  const EntityComponentManager &_ecm)
91  {
92  _ecm.EachNew<components::Model, components::ModelCanonicalLink>(
93  [this](const Entity &_model, const components::Model *,
94  const components::ModelCanonicalLink *_canonicalLinkComp)
95  {
96  this->linkModelMap[_canonicalLinkComp->Data()].insert(_model);
97  return true;
98  });
99  }
100  void CanonicalLinkModelTracker::AddAllModels(
101  const EntityComponentManager &_ecm)
102  {
103  _ecm.Each<components::Model, components::ModelCanonicalLink>(
104  [this](const Entity &_model, const components::Model *,
105  const components::ModelCanonicalLink *_canonicalLinkComp)
106  {
107  this->linkModelMap[_canonicalLinkComp->Data()].insert(_model);
108  return true;
109  });
110  }
111 
112  const std::set<Entity> &CanonicalLinkModelTracker::CanonicalLinkModels(
113  const Entity _canonicalLink) const
114  {
115  auto it = this->linkModelMap.find(_canonicalLink);
116  if (it != this->linkModelMap.end())
117  return it->second;
118 
119  // if an invalid entity was given, it maps to no models
120  return this->kEmptyModelOrdering;
121  }
122 
123  void CanonicalLinkModelTracker::RemoveLink(const Entity &_link)
124  {
125  this->linkModelMap.erase(_link);
126  }
127 }
128 }
129 }
130 
131 #endif