Gazebo Gazebo

API Reference

3.15.2
gz/sim/EntityComponentManager.hh
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2018 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_GAZEBO_ENTITYCOMPONENTMANAGER_HH_
18 #define GZ_GAZEBO_ENTITYCOMPONENTMANAGER_HH_
19 
20 #include <gz/msgs/serialized.pb.h>
21 #include <gz/msgs/serialized_map.pb.h>
22 
23 #include <map>
24 #include <memory>
25 #include <optional>
26 #include <set>
27 #include <string>
28 #include <typeinfo>
29 #include <type_traits>
30 #include <unordered_set>
31 #include <utility>
32 #include <vector>
33 
34 #include <gz/common/Console.hh>
35 #include <gz/math/graph/Graph.hh>
36 #include "gz/sim/Entity.hh"
37 #include "gz/sim/Export.hh"
38 #include "gz/sim/Types.hh"
39 
41 #include "gz/sim/detail/View.hh"
42 
43 namespace ignition
44 {
45  namespace gazebo
46  {
47  // Inline bracket to help doxygen filtering.
48  inline namespace IGNITION_GAZEBO_VERSION_NAMESPACE {
49  // Forward declarations.
50  class IGNITION_GAZEBO_HIDDEN EntityComponentManagerPrivate;
51 
57 
61  class IGNITION_GAZEBO_VISIBLE EntityComponentManager
66  {
68  public: EntityComponentManager();
69 
71  public: ~EntityComponentManager();
72 
75  public: Entity CreateEntity();
76 
79  public: size_t EntityCount() const;
80 
92  public: void RequestRemoveEntity(const Entity _entity,
93  bool _recursive = true);
94 
98  public: void RequestRemoveEntities();
99 
103  public: bool HasEntity(const Entity _entity) const;
104 
111  public: Entity ParentEntity(const Entity _entity) const;
112 
123  public: bool SetParentEntity(const Entity _child, const Entity _parent);
124 
128  public: bool HasComponentType(const ComponentTypeId _typeId) const;
129 
134  public: bool EntityHasComponent(const Entity _entity,
135  const ComponentKey &_key) const;
136 
142  public: bool EntityHasComponentType(const Entity _entity,
143  const ComponentTypeId &_typeId) const;
144 
149  public: bool EntityMatches(Entity _entity,
150  const std::set<ComponentTypeId> &_types) const;
151 
157  public: bool RemoveComponent(
158  const Entity _entity, const ComponentKey &_key);
159 
165  public: bool RemoveComponent(
166  const Entity _entity, const ComponentTypeId &_typeId);
167 
173  public: template<typename ComponentTypeT>
174  bool RemoveComponent(Entity _entity);
175 
178  public: void RebuildViews();
179 
186  public: template<typename ComponentTypeT>
187  ComponentKey CreateComponent(const Entity _entity,
188  const ComponentTypeT &_data);
189 
195  public: template<typename ComponentTypeT>
196  const ComponentTypeT *Component(const Entity _entity) const;
197 
203  public: template<typename ComponentTypeT>
204  ComponentTypeT *Component(const Entity _entity);
205 
210  public: template<typename ComponentTypeT>
211  const ComponentTypeT *Component(const ComponentKey &_key) const;
212 
217  public: template<typename ComponentTypeT>
218  ComponentTypeT *Component(const ComponentKey &_key);
219 
228  public: template<typename ComponentTypeT>
229  ComponentTypeT *ComponentDefault(Entity _entity,
230  const typename ComponentTypeT::Type &_default =
231  typename ComponentTypeT::Type());
232 
241  public: template<typename ComponentTypeT>
242  std::optional<typename ComponentTypeT::Type> ComponentData(
243  const Entity _entity) const;
244 
255  public: template<typename ComponentTypeT>
256  bool SetComponentData(const Entity _entity,
257  const typename ComponentTypeT::Type &_data);
258 
262  public: std::unordered_set<ComponentTypeId> ComponentTypes(
263  Entity _entity) const;
264 
268  public: template<typename ComponentTypeT>
269  const ComponentTypeT *First() const;
270 
274  public: template<typename ComponentTypeT>
275  ComponentTypeT *First();
276 
288  public: template<typename ...ComponentTypeTs>
289  Entity EntityByComponents(
290  const ComponentTypeTs &..._desiredComponents) const;
291 
304  public: template<typename ...ComponentTypeTs>
305  std::vector<Entity> EntitiesByComponents(
306  const ComponentTypeTs &..._desiredComponents) const;
307 
323  public: template<typename ...ComponentTypeTs>
324  std::vector<Entity> ChildrenByComponents(Entity _parent,
325  const ComponentTypeTs &..._desiredComponents) const;
326 
328  private: template <typename T>
329  struct identity; // NOLINT
330 
343  public: template<typename ...ComponentTypeTs>
344  void EachNoCache(typename identity<std::function<
345  bool(const Entity &_entity,
346  const ComponentTypeTs *...)>>::type _f) const;
347 
360  public: template<typename ...ComponentTypeTs>
361  void EachNoCache(typename identity<std::function<
362  bool(const Entity &_entity,
363  ComponentTypeTs *...)>>::type _f);
364 
377  public: template<typename ...ComponentTypeTs>
378  void Each(typename identity<std::function<
379  bool(const Entity &_entity,
380  const ComponentTypeTs *...)>>::type _f) const;
381 
394  public: template<typename ...ComponentTypeTs>
395  void Each(typename identity<std::function<
396  bool(const Entity &_entity,
397  ComponentTypeTs *...)>>::type _f);
398 
403  public: template <class Function, class... ComponentTypeTs>
404  static void ForEach(Function _f, const ComponentTypeTs &... _components);
405 
420  public: template <typename... ComponentTypeTs>
421  void EachNew(typename identity<std::function<
422  bool(const Entity &_entity,
423  ComponentTypeTs *...)>>::type _f);
424 
437  public: template <typename... ComponentTypeTs>
438  void EachNew(typename identity<std::function<
439  bool(const Entity &_entity,
440  const ComponentTypeTs *...)>>::type _f) const;
441 
452  public: template<typename ...ComponentTypeTs>
453  void EachRemoved(typename identity<std::function<
454  bool(const Entity &_entity,
455  const ComponentTypeTs *...)>>::type _f) const;
456 
460  public: const EntityGraph &Entities() const;
461 
467  public: std::unordered_set<Entity> Descendants(Entity _entity) const;
468 
477  public: msgs::SerializedState State(
478  const std::unordered_set<Entity> &_entities = {},
479  const std::unordered_set<ComponentTypeId> &_types = {}) const;
480 
495  public: msgs::SerializedState ChangedState() const;
496 
499  public: bool HasNewEntities() const;
500 
503  public: bool HasEntitiesMarkedForRemoval() const;
504 
508  public: bool HasOneTimeComponentChanges() const;
509 
514  ComponentTypesWithPeriodicChanges() const;
515 
524  public: void SetState(const msgs::SerializedState &_stateMsg);
525 
537  public: void State(
538  msgs::SerializedStateMap &_state,
539  const std::unordered_set<Entity> &_entities = {},
540  const std::unordered_set<ComponentTypeId> &_types = {},
541  bool _full = false) const;
542 
558  public: void ChangedState(msgs::SerializedStateMap &_state) const;
559 
568  public: void SetState(const msgs::SerializedStateMap &_stateMsg);
569 
574  public: void SetChanged(
575  const Entity _entity, const ComponentTypeId _type,
577 
582  public: gz::sim::ComponentState ComponentState(const Entity _entity,
583  const ComponentTypeId _typeId) const;
584 
589  public: void SetEntityCreateOffset(uint64_t _offset);
590 
594  protected: void ClearNewlyCreatedEntities();
595 
599  protected: void ProcessRemoveEntityRequests();
600 
602  protected: void SetAllComponentsUnchanged();
603 
610  private: bool IsNewEntity(const Entity _entity) const;
611 
615  private: bool IsMarkedForRemoval(const Entity _entity) const;
616 
620  private: bool RemoveEntity(const Entity _entity);
621 
625  private: components::BaseComponent *First(
626  const ComponentTypeId _componentTypeId);
627 
634  private: ComponentKey CreateComponentImplementation(
635  const Entity _entity,
636  const ComponentTypeId _componentTypeId,
637  const components::BaseComponent *_data);
638 
644  private: const components::BaseComponent *ComponentImplementation(
645  const Entity _entity,
646  const ComponentTypeId _type) const;
647 
653  private: components::BaseComponent *ComponentImplementation(
654  const Entity _entity,
655  const ComponentTypeId _type);
656 
661  private: const components::BaseComponent *ComponentImplementation(
662  const ComponentKey &_key) const;
663 
668  private: components::BaseComponent *ComponentImplementation(
669  const ComponentKey &_key);
670 
676  private: template<typename FirstComponent,
677  typename ...RemainingComponents,
678  typename std::enable_if<
679  sizeof...(RemainingComponents) == 0, int>::type = 0>
680  void AddComponentsToView(detail::View &_view,
681  const Entity _entity) const;
682 
688  private: template<typename FirstComponent,
689  typename ...RemainingComponents,
690  typename std::enable_if<
691  sizeof...(RemainingComponents) != 0, int>::type = 0>
692  void AddComponentsToView(detail::View &_view,
693  const Entity _entity) const;
694 
699  private: template<typename ...ComponentTypeTs>
700  detail::View &FindView() const;
701 
708  private: bool FindView(const std::set<ComponentTypeId> &_types,
709  std::map<detail::ComponentTypeKey,
710  detail::View>::iterator &_iter) const; // NOLINT
711 
718  AddView(const std::set<ComponentTypeId> &_types,
719  detail::View &&_view) const;
720 
723  private: void UpdateViews(const Entity _entity);
724 
728  private: ComponentId EntityComponentIdFromType(
729  const Entity _entity, const ComponentTypeId _type) const;
730 
736  private: void AddEntityToMessage(msgs::SerializedState &_msg,
737  Entity _entity,
738  const std::unordered_set<ComponentTypeId> &_types = {}) const;
739 
742 
752  private: void AddEntityToMessage(msgs::SerializedStateMap &_msg,
753  Entity _entity,
754  const std::unordered_set<ComponentTypeId> &_types = {},
755  bool _full = false) const;
756 
757  // Make runners friends so that they can manage entity creation and
758  // removal. This should be safe since runners are internal
759  // to Gazebo.
760  friend class GuiRunner;
761  friend class SimulationRunner;
762 
763  // Make network managers friends so they have control over component
764  // states. Like the runners, the managers are internal.
765  friend class NetworkManagerPrimary;
766  friend class NetworkManagerSecondary;
767 
768  // Make View a friend so that it can access components.
769  // This should be safe since View is internal to Gazebo.
770  friend class detail::View;
771  };
772  }
773  }
774 }
775 
776 #include "gz/sim/detail/EntityComponentManager.hh"
777 
778 #endif
void Each(typename identity< std::function< bool(const Entity &_entity, const ComponentTypeTs *...)>>::type _f) const
Get all entities which contain given component types, as well as the components. Note that an entity ...
bool EntityMatches(Entity _entity, const std::set< ComponentTypeId > &_types) const
Get whether an entity has all the given component types.
std::unordered_set< Entity > Descendants(Entity _entity) const
Get all entities which are descendants of a given entity, including the entity itself.
uint64_t Entity
An Entity identifies a single object in simulation such as a model, link, or light....
Definition: gz/sim/Entity.hh:59
bool RemoveComponent(const Entity _entity, const ComponentKey &_key)
Remove a component from an entity based on a key.
This library is part of the Ignition Robotics project.
The EntityComponentManager constructs, deletes, and returns components and entities....
Definition: gz/sim/EntityComponentManager.hh:65
bool HasOneTimeComponentChanges() const
Get whether there are one-time component changes. These changes do not happen frequently and should b...
void EachRemoved(typename identity< std::function< bool(const Entity &_entity, const ComponentTypeTs *...)>>::type _f) const
Get all entities which contain given component types and are about to be removed, as well as the comp...
Entity EntityByComponents(const ComponentTypeTs &..._desiredComponents) const
Get an entity which matches the value of all the given components. For example, the following will re...
friend class SimulationRunner
Definition: gz/sim/EntityComponentManager.hh:761
Component< Entity, class ParentEntityTag > ParentEntity
This component holds an entity's parent entity.
Definition: gz/sim/components/ParentEntity.hh:41
const ComponentTypeT * First() const
The first component instance of the specified type.
std::unordered_set< ComponentTypeId > ComponentTypesWithPeriodicChanges() const
Get the components types that are marked as periodic changes.
std::vector< Entity > ChildrenByComponents(Entity _parent, const ComponentTypeTs &..._desiredComponents) const
Get all entities which match the value of all the given components and are immediate children of a gi...
size_t EntityCount() const
Get the number of entities on the server.
void SetState(const msgs::SerializedState &_stateMsg)
Set the absolute state of the ECM from a serialized message. Entities / components that are in the ne...
void ClearNewlyCreatedEntities()
Clear the list of newly added entities so that a call to EachAdded after this will have no entities t...
Entity ParentEntity(const Entity _entity) const
Get the first parent of the given entity.
bool EntityHasComponentType(const Entity _entity, const ComponentTypeId &_typeId) const
Check whether an entity has a specific component type.
Entity CreateEntity()
Creates a new Entity.
bool EntityHasComponent(const Entity _entity, const ComponentKey &_key) const
Check whether an entity has a specific component.
std::optional< typename ComponentTypeT::Type > ComponentData(const Entity _entity) const
Get the data from a component.
bool SetComponentData(const Entity _entity, const typename ComponentTypeT::Type &_data)
Set the data from a component.
msgs::SerializedState ChangedState() const
Get a message with the serialized state of all entities and components that are changing in the curre...
std::unordered_set< ComponentTypeId > ComponentTypes(Entity _entity) const
Get the type IDs of all components attached to an entity.
bool SetParentEntity(const Entity _child, const Entity _parent)
Set the parent of an entity.
ComponentState
Possible states for a component.
Definition: include/gz/sim/Types.hh:64
ComponentKey CreateComponent(const Entity _entity, const ComponentTypeT &_data)
Create a component of a particular type. This will copy the _data parameter.
std::vector< Entity > EntitiesByComponents(const ComponentTypeTs &..._desiredComponents) const
Get all entities which match the value of all the given components. For example, the following will r...
const ComponentTypeT * Component(const Entity _entity) const
Get a component assigned to an entity based on a component type.
Base class for all components.
Definition: gz/sim/components/Component.hh:230
STL class.
void SetEntityCreateOffset(uint64_t _offset)
All future entities will have an id that starts at _offset. This can be used to avoid entity id colli...
bool HasEntity(const Entity _entity) const
Get whether an Entity exists.
void RequestRemoveEntities()
Request to remove all entities. This will insert the request into a queue. The queue is processed tow...
@ OneTimeChange
Component value has suffered a one-time change. This indicates to systems that this change must be pr...
int ComponentId
A unique identifier for a component instance. The uniqueness of a ComponentId is scoped to the compon...
Definition: include/gz/sim/Types.hh:82
void RequestRemoveEntity(const Entity _entity, bool _recursive=true)
Request an entity deletion. This will insert the request into a queue. The queue is processed toward ...
friend class NetworkManagerSecondary
Definition: gz/sim/EntityComponentManager.hh:766
bool HasComponentType(const ComponentTypeId _typeId) const
Get whether a component type has ever been created.
void SetAllComponentsUnchanged()
Mark all components as not changed.
void SetChanged(const Entity _entity, const ComponentTypeId _type, gz::sim::ComponentState _c=ComponentState::OneTimeChange)
Set the changed state of a component.
const EntityGraph & Entities() const
Get a graph with all the entities. Entities are vertices and edges point from parent to children.
static void ForEach(Function _f, const ComponentTypeTs &... _components)
Call a function for each parameter in a pack.
msgs::SerializedState State(const std::unordered_set< Entity > &_entities={}, const std::unordered_set< ComponentTypeId > &_types={}) const
Get a message with the serialized state of the given entities and components.
Responsible for running GUI systems as new states are received from the backend.
Definition: gz/sim/gui/GuiRunner.hh:40
class IGNITION_GAZEBO_HIDDEN EntityComponentManagerPrivate
Definition: gz/sim/EntityComponentManager.hh:50
bool HasNewEntities() const
Get whether there are new entities.
friend class NetworkManagerPrimary
Definition: gz/sim/EntityComponentManager.hh:765
ComponentTypeT * ComponentDefault(Entity _entity, const typename ComponentTypeT::Type &_default=typename ComponentTypeT::Type())
Get a mutable component assigned to an entity based on a component type. If the component doesn't exi...
gz::sim::ComponentState ComponentState(const Entity _entity, const ComponentTypeId _typeId) const
Get a component's state.
bool HasEntitiesMarkedForRemoval() const
Get whether there are any entities marked to be removed.
void ProcessRemoveEntityRequests()
Process all entity remove requests. This will remove entities and their components....
uint64_t ComponentTypeId
A unique identifier for a component type. A component type must be derived from components::BaseCompo...
Definition: include/gz/sim/Types.hh:87
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....
STL class.
void RebuildViews()
Rebuild all the views. This could be an expensive operation.
void EachNoCache(typename identity< std::function< bool(const Entity &_entity, const ComponentTypeTs *...)>>::type _f) const
A version of Each() that doesn't use a cache. The cached version, Each(), is preferred....