se2ez
scene.cpp
Go to the documentation of this file.
1 /* Author: Constantinos Chamzas */
2 
3 #include <se2ez/core/log.h>
4 #include <se2ez/core/math.h>
5 #include <se2ez/core/frame.h>
6 #include <se2ez/core/state.h>
7 #include <se2ez/core/collision.h>
8 
9 #include <se2ez/core/robot.h>
10 #include <se2ez/core/scene.h>
11 
12 using namespace se2ez;
13 
14 ///
15 /// Scene
16 ///
17 
18 Scene::RobotData::RobotData(const RobotPtr &r, const StatePtr &s) : robot(r), state(s)
19 {
20 }
21 
22 Scene::Scene(const RobotPtr &arobot) : arobot_(arobot), mrobot_(std::make_shared<Robot>())
23 {
24  mrobot_->addActiveRobot(arobot);
25  mrobot_->compileTree();
26 }
27 
28 ///
29 /// Frame / Kinematic Tree Updating
30 ///
31 
32 void Scene::addRobot(const RobotPtr &robot, const std::string &name)
33 {
34  auto state = std::make_shared<State>(robot);
35  addRobot(robot, name, state);
36 }
37 
38 void Scene::addRobot(const RobotPtr &robot, const std::string &name, const StatePtr &state)
39 {
40  if (not robots_
41  .emplace(std::piecewise_construct, //
42  std::forward_as_tuple(name), std::forward_as_tuple(robot, state))
43  .second)
44  SE2EZ_WARN("Robot Named %1 already exists thus it is not added", name);
45 
46  dirty = true;
47 }
48 
50 {
51  // Adding all the passive robots
52  mrobot_->clear();
53  for (const auto &entry : robots_)
54  mrobot_->addFixedRobot(entry.second.robot, entry.second.state, entry.first);
55 
56  // Adding the active robot
57  mrobot_->addActiveRobot(arobot_);
58 
59  // Disable all the collisions between passive frames
60  for (const auto &r1 : robots_)
61  for (const auto &f1 : r1.second.robot->getFrameNames())
62  for (const auto &r2 : robots_)
63  for (const auto &f2 : r2.second.robot->getFrameNames())
64  mrobot_->getACM()->disable(r1.first + ":" + f1, r2.first + ":" + f2);
65 
66  mrobot_->compileTree();
67 }
68 
70 {
71  if (robots_.count(name))
72  robots_.erase(name);
73  else
74  SE2EZ_WARN("Robot Named:%1, does not exist in Scene", name);
75 
76  dirty = true;
77 }
78 
80 {
81  robots_.clear();
82  dirty = true;
83 }
84 
86 {
87  return arobot_;
88 }
89 
91 {
92  if (dirty)
93  {
94  compileScene();
95  dirty = false;
96  }
97 
98  return mrobot_;
99 }
100 
102 {
103  return getSceneRobot()->getSignature();
104 }
105 
107 {
108  const auto &result = robots_.find(name);
109  if (result == robots_.end())
110  throw std::invalid_argument(log::format("Invalid robot name %1%!", name));
111 
112  return result->second.robot;
113 }
114 
116 {
118  for (const auto &entry : robots_)
119  names.emplace_back(entry.first);
120 
121  return names;
122 }
123 
125 {
126  for (const auto &entry : robots_)
127  entry.second.state->setRandom(entry.second.robot);
128 
129  dirty = true;
130 }
131 
RobotData(const RobotPtr &r, const StatePtr &s)
Constructor.
Definition: scene.cpp:18
A shared pointer wrapper for se2ez::State.
T forward_as_tuple(T... args)
const RobotPtr & getSceneRobot()
Returns a new aggregated robot including all the passive and active robots.
Definition: scene.cpp:90
Scene(const RobotPtr &arobot)
Constructor. Nothing interesting right now.
Definition: scene.cpp:22
const std::vector< std::string > getRobotNames()
Gets the all the names of the robots active and passive.
Definition: scene.cpp:115
const RobotPtr & getRobot(const std::string &name)
Returns robot the robot named name, throws error otherwise.
Definition: scene.cpp:106
void addRobot(const RobotPtr &robot, const std::string &name)
Attach a robot as passive to the scene at a zero state.
Definition: scene.cpp:32
void compileScene()
creates the scene, is called after a robot is added.
Definition: scene.cpp:49
A representation of a robot (in this case, a kinematic tree in the plane).
Definition: robot.h:119
bool dirty
Does the scene robot need updating?
Definition: scene.h:135
std::map< std::string, RobotData > robots_
Map of robots and their names.
Definition: scene.h:138
void removeRobot(const std::string &name)
Removes the specific robot from the Scene.
Definition: scene.cpp:69
#define SE2EZ_WARN(fmt,...)
Definition: log.h:41
void setRandom()
Sets every passive robot to a random state.
Definition: scene.cpp:124
A shared pointer wrapper for se2ez::Robot.
std::string format(const std::string &fmt, Args &&... args)
Definition: log.h:25
Main namespace.
Definition: collision.h:11
const std::string & getSignature()
Returns the mrobot signature.
Definition: scene.cpp:101
const RobotPtr & getActiveRobot()
Returns a the active robot.
Definition: scene.cpp:85
const RobotPtr arobot_
Active Robot.
Definition: scene.h:139
void clear()
Completely clears internal structures, a brand new scene!
Definition: scene.cpp:79
RobotPtr mrobot_
Mega Robot.
Definition: scene.h:140
T emplace_back(T... args)