Gazebo Gazebo

API Reference

3.15.1
gz/sim/Util.hh
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2019 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_UTIL_HH_
18 #define GZ_GAZEBO_UTIL_HH_
19 
20 #include <gz/msgs/entity.pb.h>
21 
22 #include <string>
23 #include <unordered_set>
24 #include <vector>
25 
26 #include <gz/math/Pose3.hh>
27 #include "gz/sim/config.hh"
28 #include "gz/sim/Entity.hh"
30 #include "gz/sim/Export.hh"
31 #include "gz/sim/Types.hh"
32 
33 namespace ignition
34 {
35  namespace gazebo
36  {
37  // Inline bracket to help doxygen filtering.
38  inline namespace IGNITION_GAZEBO_VERSION_NAMESPACE {
39  //
44  math::Pose3d IGNITION_GAZEBO_VISIBLE worldPose(const Entity &_entity,
45  const EntityComponentManager &_ecm);
46 
53  std::string IGNITION_GAZEBO_VISIBLE scopedName(const Entity &_entity,
54  const EntityComponentManager &_ecm, const std::string &_delim = "/",
55  bool _includePrefix = true);
56 
80  std::unordered_set<Entity> IGNITION_GAZEBO_VISIBLE entitiesFromScopedName(
81  const std::string &_scopedName, const EntityComponentManager &_ecm,
82  Entity _relativeTo = kNullEntity,
83  const std::string &_delim = "::");
84 
105  ComponentTypeId IGNITION_GAZEBO_VISIBLE entityTypeId(const Entity &_entity,
106  const EntityComponentManager &_ecm);
107 
125  std::string IGNITION_GAZEBO_VISIBLE entityTypeStr(const Entity &_entity,
126  const EntityComponentManager &_ecm);
127 
132  Entity IGNITION_GAZEBO_VISIBLE worldEntity(const Entity &_entity,
133  const EntityComponentManager &_ecm);
134 
138  Entity IGNITION_GAZEBO_VISIBLE worldEntity(
139  const EntityComponentManager &_ecm);
140 
146  std::string IGNITION_GAZEBO_VISIBLE removeParentScope(
147  const std::string &_name, const std::string &_delim);
148 
157  std::string IGNITION_GAZEBO_VISIBLE asFullPath(const std::string &_uri,
158  const std::string &_filePath);
159 
162  std::vector<std::string> IGNITION_GAZEBO_VISIBLE resourcePaths();
163 
168  void IGNITION_GAZEBO_VISIBLE addResourcePaths(
169  const std::vector<std::string> &_paths = {});
170 
176  gz::sim::Entity IGNITION_GAZEBO_VISIBLE topLevelModel(
177  const Entity &_entity, const EntityComponentManager &_ecm);
178 
191  std::string IGNITION_GAZEBO_VISIBLE validTopic(
192  const std::vector<std::string> &_topics);
193 
202  template <class ComponentType>
204  Entity _entity, bool _enable = true)
205  {
206  bool changed{false};
207 
208  auto exists = _ecm.Component<ComponentType>(_entity);
209  if (_enable && !exists)
210  {
211  _ecm.CreateComponent(_entity, ComponentType());
212  changed = true;
213  }
214  else if (!_enable && exists)
215  {
216  _ecm.RemoveComponent<ComponentType>(_entity);
217  changed = true;
218  }
219  return changed;
220  }
221 
239  Entity IGNITION_GAZEBO_VISIBLE entityFromMsg(
240  const EntityComponentManager &_ecm, const msgs::Entity &_msg);
241 
243  const std::string kResourcePathEnv{"IGN_GAZEBO_RESOURCE_PATH"};
244 
247  const std::string kSdfPathEnv{"SDF_PATH"};
248 
250  const std::string kServerConfigPathEnv{"IGN_GAZEBO_SERVER_CONFIG_PATH"};
251 
254  const std::string kRenderPluginPathEnv{"IGN_GAZEBO_RENDER_ENGINE_PATH"};
255  }
256  }
257 }
258 #endif
std::string validTopic(const std::vector< std::string > &_topics)
Helper function to generate a valid transport topic, given a list of topics ordered by preference....
const std::string kRenderPluginPathEnv
Environment variable holding paths to custom rendering engine plugins.
Definition: gz/sim/Util.hh:254
void addResourcePaths(const std::vector< std::string > &_paths={})
Add resource paths based on latest environment variables. This will update the SDF and Ignition envir...
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.
STL class.
The EntityComponentManager constructs, deletes, and returns components and entities....
Definition: gz/sim/EntityComponentManager.hh:65
bool enableComponent(EntityComponentManager &_ecm, Entity _entity, bool _enable=true)
Helper function to "enable" a component (i.e. create it if it doesn't exist) or "disable" a component...
Definition: gz/sim/Util.hh:203
Entity entityFromMsg(const EntityComponentManager &_ecm, const msgs::Entity &_msg)
Helper function to get an entity from an entity message. The returned entity is not guaranteed to exi...
std::string removeParentScope(const std::string &_name, const std::string &_delim)
Helper function to remove a parent scope from a given name. This removes the first name found before ...
STL class.
bool exists(const std::string &_path)
std::unordered_set< Entity > entitiesFromScopedName(const std::string &_scopedName, const EntityComponentManager &_ecm, Entity _relativeTo=kNullEntity, const std::string &_delim="::")
Helper function to get an entity given its scoped name. The scope may start at any level by default....
std::string asFullPath(const std::string &_uri, const std::string &_filePath)
Combine a URI and a file path into a full path. If the URI is already a full path or contains a schem...
Pose3< double > Pose3d
const std::string kServerConfigPathEnv
Environment variable holding server config paths.
Definition: gz/sim/Util.hh:250
std::vector< std::string > resourcePaths()
Get resource paths based on latest environment variables.
const Entity kNullEntity
Indicates a non-existant or invalid Entity.
Definition: gz/sim/Entity.hh:62
ComponentKey CreateComponent(const Entity _entity, const ComponentTypeT &_data)
Create a component of a particular type. This will copy the _data parameter.
ComponentTypeId entityTypeId(const Entity &_entity, const EntityComponentManager &_ecm)
Generally, each entity will be of some specific high-level type, such as World, Sensor,...
std::string scopedName(const Entity &_entity, const EntityComponentManager &_ecm, const std::string &_delim="/", bool _includePrefix=true)
Helper function to generate scoped name for an entity.
math::Pose3d worldPose(const Entity &_entity, const EntityComponentManager &_ecm)
Helper function to compute world pose of an entity.
const ComponentTypeT * Component(const Entity _entity) const
Get a component assigned to an entity based on a component type.
std::string entityTypeStr(const Entity &_entity, const EntityComponentManager &_ecm)
Generally, each entity will be of some specific high-level type, such as "world", "sensor",...
gz::sim::Entity topLevelModel(const Entity &_entity, const EntityComponentManager &_ecm)
Get the top level model of an entity.
Entity worldEntity(const Entity &_entity, const EntityComponentManager &_ecm)
Get the world to which the given entity belongs.
const std::string kResourcePathEnv
Environment variable holding resource paths.
Definition: gz/sim/Util.hh:243
const std::string kSdfPathEnv
Environment variable used by SDFormat to find URIs inside <include>
Definition: gz/sim/Util.hh:247
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