3
* @brief Utility class for starters
5
* This file PluginUtil.h is created at Almende B.V. It is open-source software and part
6
* of the Common Hybrid Agent Platform (CHAP). A toolbox with a lot of open-source tools.
7
* Ranging from thread pools, and TCP/IP components to control architectures and learning
8
* algorithms. This software is published under the GNU Lesser General Public license,
9
* but the software is not allowed to be used for military use, within the bio-industry
10
* or for animal experimentation.
12
* @author Anne C. van Rossum
14
* @project Replicator FP7
15
* @company Almende B.V.
25
#include <srCore/simulationArenaComponent.h>
27
// Delta3D inline files
28
#include <dtGame/gamemanager.inl>
31
* \addtogroup Utilities
35
/* **************************************************************************************
37
* **************************************************************************************/
40
* Helper class for generic if-routine. Regretfully only actors can be retrieved in this
41
* way, there is no analog for GMComponent%s.
43
class CheckForSpace3DRobots {
45
bool operator()(dtDAL::ActorProxy& proxy) {
46
std::string name = proxy.GetActorType().GetName();
47
return (name.find("RobotSpace3D") != std::string::npos);
52
* Returns true if the actor can be cast as a RobotActorBase class. First it has to be cast from ActorProxy
53
* to a GameActorProxy. Then that what GetGameActor on the proxy returns is cast into a RobotActorBase. If
54
* all this succeeds the operator() returns true.
56
class CheckForRobots {
58
bool operator()(dtDAL::ActorProxy& proxy) {
59
dtGame::GameActorProxy *robot_proxy = dynamic_cast<dtGame::GameActorProxy*>(&proxy);
60
if (robot_proxy == NULL) return false;
61
srCore::RobotActorBase *actor = dynamic_cast<srCore::RobotActorBase*>(&robot_proxy->GetGameActor());
62
bool result = (actor != NULL);
68
* Just assumes that there is "robot" somewhere in the name (accepts both lower-case and upper-case). This is
69
* of course a less general class than CheckForRobots, which does not have a "robot" in the name as constraint
72
class CheckForRobotsByName {
74
bool operator()(dtDAL::ActorProxy& proxy) {
75
std::string name = proxy.GetActorType().GetName();
76
std::transform(name.begin(), name.end(), name.begin(), (int(*)(int)) std::tolower);
77
bool result = (name.find("robot") != std::string::npos);
83
* Routines in this class can be called directly. See this class as a wrapper of all
84
* important classes that you might need.
89
* When the plugin is called, there is a SimulationArenaComponent constructed. This component
90
* provides an easy way of adding and deleting actors to the scene. It does - of course - not
91
* recognize actors that are known only by the plugin. It can however also be used to reposition
94
static srCore::SimulationArenaComponent* GetArena() {
95
dtGame::GameApplication *GA = dtGame::GameApplication::GetInstance(0);
96
srCore::SimulationArenaComponent* arena = static_cast<srCore::SimulationArenaComponent*>(
97
GA->GetGameManager()->GetComponentByName(srCore::SimulationArenaComponent::DEFAULT_NAME));
102
* Returns a robot. Can for example be used to know the sender of a message.
104
static srCore::RobotActorBase* GetRobot(dtCore::UniqueId &id) {
105
dtGame::GameApplication *GA = dtGame::GameApplication::GetInstance(0);
106
dtGame::GameActorProxy *proxy = GA->GetGameManager()->FindGameActorById(id);
107
assert (proxy != NULL);
108
srCore::RobotActorBase *actor = dynamic_cast<srCore::RobotActorBase*>(&proxy->GetGameActor());
109
assert (actor != NULL);
114
* Returns a robot by name
116
static srCore::RobotActorBase* GetRobot(const std::string &name) {
117
dtGame::GameApplication *GA = dtGame::GameApplication::GetInstance(0);
118
dtGame::GameActorProxy *proxy;
119
GA->GetGameManager()->FindActorByName(name, proxy);
121
LOG_WARNING("Actor proxy cannot be found");
123
srCore::RobotActorBase *actor = dynamic_cast<srCore::RobotActorBase*>(&proxy->GetGameActor());
125
LOG_WARNING("Proxy found, but cannot be casted properly");
131
* Returns all the robots (derived classes from RobotActorBase).
133
static void GetAllRobots(std::vector<srCore::RobotActorBase*> &robots) {
134
dtGame::GameApplication *GA = dtGame::GameApplication::GetInstance(0);
135
std::vector<dtDAL::ActorProxy*> actors;
136
CheckForRobots functor;
137
GA->GetGameManager()->FindActorsIf<CheckForRobots>(functor, actors);
139
// It would be nicer if we could have defined the return type directly
140
std::vector<dtDAL::ActorProxy*>::iterator iter;
141
for (iter = actors.begin(); iter != actors.end(); iter++) {
142
dtGame::GameActorProxy *robot_proxy = dynamic_cast<dtGame::GameActorProxy*>(*iter);
143
srCore::RobotActorBase *robot = dynamic_cast<srCore::RobotActorBase*>(&robot_proxy->GetGameActor());
144
// assert (robot != NULL);
145
robots.push_back(robot);
150
* Return robot by identifier.
152
static srCore::RobotActorBase* GetRobot(long int id) {
153
dtGame::GameApplication *GA = dtGame::GameApplication::GetInstance(0);
154
std::vector<dtDAL::ActorProxy*> actors;
155
CheckForRobots functor;
156
GA->GetGameManager()->FindActorsIf<CheckForRobots>(functor, actors);
158
// It would be nicer if we could have defined the return type directly
159
std::vector<dtDAL::ActorProxy*>::iterator iter;
160
for (iter = actors.begin(); iter != actors.end(); iter++) {
161
dtGame::GameActorProxy *robot_proxy = dynamic_cast<dtGame::GameActorProxy*>(*iter);
162
// if the asserts fail, the FindActorsIf routine is incorrect
163
assert (robot_proxy != NULL);
164
srCore::RobotActorBase *robot = dynamic_cast<srCore::RobotActorBase*>(&robot_proxy->GetGameActor());
165
assert (robot != NULL);
166
// std::ostringstream msg; msg.clear(); msg.str("");
167
// msg << "Check id " << id << " against robot with id " << robot->getID();
168
// LOG_INFO(msg.str());
169
if (robot->getID() == id) return robot;
175
* Returns the controller that belongs to the robot.
177
// static srCore::ControllerComponentBase* GetController(srCore::RobotActorBase *robot) {
178
// dtGame::GameApplication *GA = dtGame::GameApplication::GetInstance(0);
179
// srCore::SimulationArenaComponent* arena = static_cast<srCore::SimulationArenaComponent*>(
180
// GA->GetGameManager()->GetComponentByName(srCore::SimulationArenaComponent::DEFAULT_NAME));
181
// std::vector<dtCore::RefPtr<srCore::ControllerComponentBase> > controllers = arena->getRobotsController(robot);
182
// assert (controllers.size() > 0);
183
// return controllers.front();
187
* Returns a controller that belongs to a robot with the given id. This is the first one that is encountered
188
* if there are multiple controllers per robot.
190
// static dtCore::RefPtr<srCore::ControllerComponentBase> GetController(int robotId) {
191
// std::vector< dtGame::GMComponent* > allComponents;
192
// dtGame::GameApplication *GA = dtGame::GameApplication::GetInstance(0);
193
// GA->GetGameManager()->GetAllComponents(allComponents);
195
// for(unsigned int i=0; i < allComponents.size(); i++) {
196
// dtCore::RefPtr<srCore::ControllerComponentBase> controller = dynamic_cast<srCore::ControllerComponentBase*>(allComponents[i]);
197
// if(controller != NULL) {
198
// if(controller->getRobotID() == robotId) {
199
// return controller;
207
* Print the position of a robot.
209
// static void PrintPosition(dtUtil::Log::LogMessageType type, const srCore::RobotActorBase &robot) {
210
// osg::Vec3f pos = robot.getPosition();
211
// dtUtil::Log::GetInstance().LogMessage(type, __func__, __LINE__,
212
// "Get position of robot %li (%f,%f,%f)", robot.getID(), pos.x(), pos.y(), pos.z());
216
* Print orientation of a robot
218
// static void PrintOrientation(dtUtil::Log::LogMessageType type, const srCore::RobotActorBase &robot) {
219
// osg::Vec3f ori = robot.getRotation();
220
// dtUtil::Log::GetInstance().LogMessage(type, __func__, __LINE__,
221
// "Get orientation of robot %li (%f,%f,%f)", robot.getID(), ori.x(), ori.y(), ori.z());
229
#endif /* PLUGINUTIL_H_ */