Gazebo Gazebo

API Reference

6.16.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 IGNITION_GAZEBO_SYSTEMS_PHYSICS_CANONICAL_LINK_MODEL_TRACKER_HH_
18 #define IGNITION_GAZEBO_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 ignition::gazebo
30 {
31 inline namespace IGNITION_GAZEBO_VERSION_NAMESPACE {
32 namespace systems::physics_system
33 {
56  {
59  public: void AddNewModels(const EntityComponentManager &_ecm);
60 
66  public: const std::set<Entity> &CanonicalLinkModels(
67  const Entity _canonicalLink) const;
68 
72  public: void RemoveLink(const Entity &_link);
73 
78  private: std::unordered_map<Entity, std::set<Entity>> linkModelMap;
79 
82  private: const std::set<Entity> emptyModelOrdering{};
83  };
84 
85  void CanonicalLinkModelTracker::AddNewModels(
86  const EntityComponentManager &_ecm)
87  {
88  _ecm.EachNew<components::Model, components::ModelCanonicalLink>(
89  [this](const Entity &_model, const components::Model *,
90  const components::ModelCanonicalLink *_canonicalLinkComp)
91  {
92  this->linkModelMap[_canonicalLinkComp->Data()].insert(_model);
93  return true;
94  });
95  }
96 
97  const std::set<Entity> &CanonicalLinkModelTracker::CanonicalLinkModels(
98  const Entity _canonicalLink) const
99  {
100  auto it = this->linkModelMap.find(_canonicalLink);
101  if (it != this->linkModelMap.end())
102  return it->second;
103 
104  // if an invalid entity was given, it maps to no models
105  return this->emptyModelOrdering;
106  }
107 
108  void CanonicalLinkModelTracker::RemoveLink(const Entity &_link)
109  {
110  this->linkModelMap.erase(_link);
111  }
112 }
113 }
114 }
115 
116 #endif
const std::set< Entity > & CanonicalLinkModels(const Entity _canonicalLink) const
Get a topological ordering of models that have a particular canonical link.
Definition: CanonicalLinkModelTracker.hh:97
void RemoveLink(const Entity &_link)
Remove a link from the mapping. This method should be called when a link is removed from simulation.
Definition: CanonicalLinkModelTracker.hh:108
uint64_t Entity
An Entity identifies a single object in simulation such as a model, link, or light....
Definition: gz/sim/Entity.hh:58
The EntityComponentManager constructs, deletes, and returns components and entities....
Definition: gz/sim/EntityComponentManager.hh:66
T find(T... args)
Gazebo is a leading open source robotics simulator, that provides high fidelity physics,...
Definition: gz/sim/Actor.hh:35
Helper class that keeps track of which models have a particular canonical link. This is useful in the...
Definition: CanonicalLinkModelTracker.hh:55
T erase(T... args)
Component< NoData, class ModelTag > Model
A component that identifies an entity as being a model.
Definition: gz/sim/components/Model.hh:111
A component type that wraps any data type. The intention is for this class to be used to create simpl...
Definition: gz/sim/components/Component.hh:324
void AddNewModels(const EntityComponentManager &_ecm)
Save mappings for new models and their canonical links.
Definition: CanonicalLinkModelTracker.hh:85
T insert(T... args)
T end(T... args)
void EachNew(typename identity< std::function< bool(const Entity &_entity, ComponentTypeTs *...)>>::type _f)
Get all newly created entities which contain given component types, as well as the components....