Robots.h
Go to the documentation of this file.
1 /*
2  * Copyright 2015-2022 CNRS-UM LIRMM, CNRS-AIST JRL
3  */
4 
5 #pragma once
6 
7 #include <mc_rbdyn/Robot.h>
8 
9 #include <mc_rtc/iterators.h>
10 #include <mc_rtc/shared.h>
11 
12 namespace mc_rbdyn
13 {
14 
15 struct MC_RBDYN_DLLAPI Robots : public mc_rtc::shared<Robots>
16 {
17  friend struct Robot;
19 
20 public:
29  using reverse_iterator = std::reverse_iterator<iterator>;
30  using const_reverse_iterator = std::reverse_iterator<const_iterator>;
31  using size_type = std::vector<RobotPtr>::size_type;
35  const std::vector<mc_rbdyn::RobotModule> & robotModules() const;
36 
38  std::vector<rbd::MultiBody> & mbs();
40  const std::vector<rbd::MultiBody> & mbs() const;
41 
43  std::vector<rbd::MultiBodyConfig> & mbcs();
45  const std::vector<rbd::MultiBodyConfig> & mbcs() const;
46 
48  bool hasRobot(const std::string & name) const;
49 
51  inline mc_rbdyn::Robots & robots() noexcept { return *this; }
52 
54  inline const mc_rbdyn::Robots & robots() const noexcept { return *this; }
55 
57  unsigned int robotIndex() const;
59  unsigned int envIndex() const;
61  unsigned int robotIndex(const std::string & name) const;
62 
81  Robot & load(const std::string & name, const RobotModule & module, const LoadRobotParameters & params = {});
82 
87  inline Robot & load(const RobotModule & module, const LoadRobotParameters & params = {})
88  {
89  return load(module.name, module, params);
90  }
91 
96  void load(const std::vector<std::shared_ptr<RobotModule>> & modules);
97 
102  void rename(const std::string & oldName, const std::string & newName);
103 
114  Robot & loadFromUrdf(const std::string & name,
115  const std::string & urdf,
116  const rbd::parsers::ParserParameters & parser_params = {},
117  const LoadRobotParameters & load_params = {});
118 
119  void robotCopy(const Robot & robot, const std::string & copyName);
120 
121  void removeRobot(const std::string & name);
122 
123  void removeRobot(unsigned int idx);
124 
126  /* End of Robot(s) loading/unloading functions group */
127 
128  const RobotModule & robotModule(size_t idx) const;
129 
130  Robot & robot();
131  const Robot & robot() const;
132 
133  Robot & env();
134  const Robot & env() const;
135 
136  Robot & robot(size_t idx);
137  const Robot & robot(size_t idx) const;
138 
139  Robot & robot(const std::string & name);
140  const Robot & robot(const std::string & name) const;
141 
148  iterator begin() noexcept;
149  const_iterator begin() const noexcept;
150  const_iterator cbegin() const noexcept;
151 
152  iterator end() noexcept;
153  const_iterator end() const noexcept;
154  const_iterator cend() const noexcept;
155 
156  reverse_iterator rbegin() noexcept;
157  const_reverse_iterator rbegin() const noexcept;
158  const_reverse_iterator crbegin() const noexcept;
159 
160  reverse_iterator rend() noexcept;
161  const_reverse_iterator rend() const noexcept;
162  const_reverse_iterator crend() const noexcept;
170  size_type size() const noexcept;
171 
178  void reserve(size_type new_cap);
179 
184  inline mc_rbdyn::Robot & operator[](size_t idx)
185  {
186  assert(idx < robots_.size());
187  return *robots_[idx];
188  }
189 
194  inline const mc_rbdyn::Robot & operator[](size_t idx) const
195  {
196  assert(idx < robots_.size());
197  return *robots_[idx];
198  }
199 
204  inline mc_rbdyn::Robot & at(size_t idx)
205  {
206  assert(idx < robots_.size());
207  return *robots_.at(idx);
208  }
209 
214  inline const mc_rbdyn::Robot & at(size_t idx) const
215  {
216  assert(idx < robots_.size());
217  return *robots_.at(idx);
218  }
219 
221  inline static RobotsPtr make() noexcept { return std::make_shared<Robots>(NewRobotsToken{}); }
222 
224  template<class Deleter>
225  inline static RobotsPtr make(Deleter deleter) noexcept
226  {
227  return std::shared_ptr<Robots>(new Robots{NewRobotsToken{}}, deleter);
228  }
229 
234  void copy(mc_rbdyn::Robots & out) const;
235 
236 protected:
238  {
239  };
240 
241 public:
243 
244 protected:
245  Robots(const Robots & rhs) = delete;
246  Robots & operator=(const Robots & rhs) = delete;
247  Robots(Robots && robots) = delete;
248  Robots & operator=(Robots && robots) = delete;
249 
251  std::vector<RobotPtr> robots_;
252  std::vector<rbd::MultiBody> mbs_;
253  std::vector<rbd::MultiBodyConfig> mbcs_;
254  std::vector<rbd::MultiBodyGraph> mbgs_;
255  unsigned int robotIndex_;
256  unsigned int envIndex_;
257  void updateIndexes();
258  std::unordered_map<std::string, unsigned int> robotNameToIndex_;
259 };
260 
261 /* Static pendant of the loader functions to create Robots directly */
262 MC_RBDYN_DLLAPI RobotsPtr loadRobot(const RobotModule & module, const LoadRobotParameters & params = {});
263 
264 MC_RBDYN_DLLAPI RobotsPtr loadRobot(const std::string & name,
265  const RobotModule & module,
266  const LoadRobotParameters & params = {});
267 
268 MC_RBDYN_DLLAPI RobotsPtr loadRobots(const std::vector<std::shared_ptr<RobotModule>> & modules);
269 
270 MC_RBDYN_DLLAPI RobotsPtr loadRobotAndEnv(const RobotModule & module, const RobotModule & envModule);
271 
272 MC_RBDYN_DLLAPI RobotsPtr loadRobotFromUrdf(const std::string & name,
273  const std::string & urdf,
274  const rbd::parsers::ParserParameters & parser_params = {},
275  const LoadRobotParameters & load_params = {});
276 
277 } // namespace mc_rbdyn
mc_rbdyn::Robots::robots
const mc_rbdyn::Robots & robots() const noexcept
Definition: Robots.h:54
mc_rbdyn::loadRobotFromUrdf
MC_RBDYN_DLLAPI RobotsPtr loadRobotFromUrdf(const std::string &name, const std::string &urdf, const rbd::parsers::ParserParameters &parser_params={}, const LoadRobotParameters &load_params={})
mc_rbdyn::Robots
Definition: Robots.h:15
mc_rtc::SharedPtrVectorIterator
Definition: iterators.h:16
mc_rbdyn::Robots::robots_
std::vector< RobotPtr > robots_
Definition: Robots.h:251
mc_rtc::SharedPtrVectorConstIterator
Definition: iterators.h:34
mc_rbdyn::Robots::mbs_
std::vector< rbd::MultiBody > mbs_
Definition: Robots.h:252
mc_rbdyn::Robots::const_reverse_iterator
std::reverse_iterator< const_iterator > const_reverse_iterator
Definition: Robots.h:30
mc_rbdyn::Robots::make
static RobotsPtr make(Deleter deleter) noexcept
Definition: Robots.h:225
mc_rbdyn::RobotModuleVector
std::vector< RobotModule, Eigen::aligned_allocator< RobotModule > > RobotModuleVector
Definition: RobotModule.h:638
mc_rbdyn::Robots::load
Robot & load(const RobotModule &module, const LoadRobotParameters &params={})
Definition: Robots.h:87
mc_rbdyn::Robots::NewRobotsToken
Definition: Robots.h:237
mc_rbdyn::Robots::mbgs_
std::vector< rbd::MultiBodyGraph > mbgs_
Definition: Robots.h:254
mc_rtc::shared
Definition: shared.h:27
mc_rbdyn::Robots::at
mc_rbdyn::Robot & at(size_t idx)
Definition: Robots.h:204
mc_rbdyn::Robot
Definition: Robot.h:62
mc_rbdyn::RobotsPtr
std::shared_ptr< Robots > RobotsPtr
Definition: fwd.h:17
mc_rbdyn::Robots::mbcs_
std::vector< rbd::MultiBodyConfig > mbcs_
Definition: Robots.h:253
mc_rbdyn::Robots::size_type
std::vector< RobotPtr >::size_type size_type
Definition: Robots.h:31
mc_rbdyn::RobotModule::name
std::string name
Definition: RobotModule.h:556
mc_rbdyn::Robots::robotNameToIndex_
std::unordered_map< std::string, unsigned int > robotNameToIndex_
Correspondance between robot name and index.
Definition: Robots.h:258
mc_rbdyn::RobotModule
Definition: RobotModule.h:69
mc_rbdyn::LoadRobotParameters
Definition: Robot.h:32
mc_rbdyn::Robots::envIndex_
unsigned int envIndex_
Definition: Robots.h:256
mc_rbdyn::Robots::operator[]
const mc_rbdyn::Robot & operator[](size_t idx) const
Definition: Robots.h:194
mc_rbdyn::Robots::robotIndex_
unsigned int robotIndex_
Definition: Robots.h:255
Robot.h
MC_RBDYN_DLLAPI
#define MC_RBDYN_DLLAPI
Definition: api.h:50
mc_rbdyn::Robots::make
static RobotsPtr make() noexcept
Definition: Robots.h:221
mc_rbdyn::Robots::at
const mc_rbdyn::Robot & at(size_t idx) const
Definition: Robots.h:214
mc_rbdyn::loadRobot
MC_RBDYN_DLLAPI RobotsPtr loadRobot(const RobotModule &module, const LoadRobotParameters &params={})
mc_rbdyn::Robots::reverse_iterator
std::reverse_iterator< iterator > reverse_iterator
Definition: Robots.h:29
mc_rbdyn::Robots::robots
mc_rbdyn::Robots & robots() noexcept
Definition: Robots.h:51
iterators.h
mc_rbdyn::loadRobotAndEnv
MC_RBDYN_DLLAPI RobotsPtr loadRobotAndEnv(const RobotModule &module, const RobotModule &envModule)
mc_rbdyn
Definition: generic_gripper.h:14
mc_rtc::gui::Robot
auto Robot(const std::string &name, GetT get_fn)
Definition: Robot.h:56
mc_rbdyn::loadRobots
MC_RBDYN_DLLAPI RobotsPtr loadRobots(const std::vector< std::shared_ptr< RobotModule >> &modules)
shared.h
mc_rbdyn::Robots::robot_modules_
RobotModuleVector robot_modules_
Definition: Robots.h:250