Gazebo Gazebo

API Reference

6.16.0
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 
209  std::string IGNITION_GAZEBO_VISIBLE topicFromScopedName(
210  const Entity &_entity,
211  const EntityComponentManager &_ecm,
212  bool _excludeWorld = true);
213 
229  std::string IGNITION_GAZEBO_VISIBLE resolveSdfWorldFile(
230  const std::string &_sdfFilename,
231  const std::string &_fuelResourceCache = "");
232 
241  template <class ComponentType>
243  Entity _entity, bool _enable = true)
244  {
245  bool changed{false};
246 
247  auto exists = _ecm.Component<ComponentType>(_entity);
248  if (_enable && !exists)
249  {
250  _ecm.CreateComponent(_entity, ComponentType());
251  changed = true;
252  }
253  else if (!_enable && exists)
254  {
255  _ecm.RemoveComponent<ComponentType>(_entity);
256  changed = true;
257  }
258  return changed;
259  }
260 
278  Entity IGNITION_GAZEBO_VISIBLE entityFromMsg(
279  const EntityComponentManager &_ecm, const msgs::Entity &_msg);
280 
287  std::optional<math::Vector3d> IGNITION_GAZEBO_VISIBLE sphericalCoordinates(
288  Entity _entity, const EntityComponentManager &_ecm);
289 
292  const std::string kResourcePathEnv{"IGN_GAZEBO_RESOURCE_PATH"};
294  const std::string kResourcePathEnvGzSim{"GZ_SIM_RESOURCE_PATH"};
295 
298  const std::string kSdfPathEnv{"SDF_PATH"};
299 
303  const std::string kServerConfigPathEnv{"IGN_GAZEBO_SERVER_CONFIG_PATH"};
305  const std::string kServerConfigPathEnvGzSim{"GZ_SIM_SERVER_CONFIG_PATH"};
306 
310  const std::string kRenderPluginPathEnv{"IGN_GAZEBO_RENDER_ENGINE_PATH"};
313  const std::string kRenderPluginPathEnvGzSim{"GZ_SIM_RENDER_ENGINE_PATH"};
314 
315  }
316  }
317 }
318 #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. Prefer using GZ_SIM_RENDER_ENG...
Definition: gz/sim/Util.hh:310
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:58
bool RemoveComponent(const Entity _entity, const ComponentKey &_key)
Remove a component from an entity based on a key.
This library is part of the Gazebo project.
STL class.
The EntityComponentManager constructs, deletes, and returns components and entities....
Definition: gz/sim/EntityComponentManager.hh:66
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:242
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::string topicFromScopedName(const Entity &_entity, const EntityComponentManager &_ecm, bool _excludeWorld=true)
Helper function that returns a valid Ignition Transport topic consisting of the scoped name for the p...
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...
const std::string kResourcePathEnvGzSim
Environment variable holding resource paths.
Definition: gz/sim/Util.hh:294
const std::string kRenderPluginPathEnvGzSim
Environment variable holding paths to custom rendering engine plugins.
Definition: gz/sim/Util.hh:313
Pose3< double > Pose3d
ComponentTypeT * CreateComponent(const Entity _entity, const ComponentTypeT &_data)
Create a component of a particular type. This will copy the _data parameter.
const std::string kServerConfigPathEnv
Environment variable holding server config paths. Prefer using GZ_SIM_SERVER_CONFIG_PATH for compatib...
Definition: gz/sim/Util.hh:303
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:61
ComponentTypeId entityTypeId(const Entity &_entity, const EntityComponentManager &_ecm)
Generally, each entity will be of some specific high-level type, such as World, Sensor,...
const std::string kServerConfigPathEnvGzSim
Environment variable holding server config paths.
Definition: gz/sim/Util.hh:305
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. Prefer using GZ_SIM_RESOURCE_PATH for compatibility with...
Definition: gz/sim/Util.hh:292
std::optional< math::Vector3d > sphericalCoordinates(Entity _entity, const EntityComponentManager &_ecm)
Get the spherical coordinates for an entity.
const std::string kSdfPathEnv
Environment variable used by SDFormat to find URIs inside <include>
Definition: gz/sim/Util.hh:298
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:90
std::string resolveSdfWorldFile(const std::string &_sdfFilename, const std::string &_fuelResourceCache="")
Convert an SDF world filename string, such as "shapes.sdf", to full system file path....