~robot3d-team/robot3d/trunk

« back to all changes in this revision

Viewing changes to inc/srInterface/PluginUtil.h

  • Committer: Anne van Rossum
  • Date: 2010-08-10 15:58:55 UTC
  • Revision ID: anne@gamix-20100810155855-kve7x2vwouagdij9
Initial import

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
 * @file PluginUtil.h
 
3
 * @brief Utility class for starters
 
4
 *
 
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.
 
11
 *
 
12
 * @author  Anne C. van Rossum
 
13
 * @date    Mar 26, 2010
 
14
 * @project Replicator FP7
 
15
 * @company Almende B.V.
 
16
 */
 
17
 
 
18
 
 
19
#ifndef PLUGINUTIL_H_
 
20
#define PLUGINUTIL_H_
 
21
 
 
22
#include <cassert>
 
23
 
 
24
// Robot3D files
 
25
#include <srCore/simulationArenaComponent.h>
 
26
 
 
27
// Delta3D inline files
 
28
#include <dtGame/gamemanager.inl>
 
29
 
 
30
/**
 
31
 * \addtogroup Utilities
 
32
 * @{
 
33
 */
 
34
 
 
35
/* **************************************************************************************
 
36
 * Interface
 
37
 * **************************************************************************************/
 
38
 
 
39
/**
 
40
 * Helper class for generic if-routine. Regretfully only actors can be retrieved in this
 
41
 * way, there is no analog for GMComponent%s.
 
42
 */
 
43
class CheckForSpace3DRobots {
 
44
public:
 
45
        bool operator()(dtDAL::ActorProxy& proxy) {
 
46
                std::string name = proxy.GetActorType().GetName();
 
47
                return (name.find("RobotSpace3D") != std::string::npos);
 
48
        }
 
49
};
 
50
 
 
51
/**
 
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.
 
55
 */
 
56
class CheckForRobots {
 
57
public:
 
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);
 
63
                return result;
 
64
        }
 
65
};
 
66
 
 
67
/**
 
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
 
70
 * to be found.
 
71
 */
 
72
class CheckForRobotsByName {
 
73
public:
 
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);
 
78
                return result;
 
79
        }
 
80
};
 
81
 
 
82
/**
 
83
 * Routines in this class can be called directly. See this class as a wrapper of all
 
84
 * important classes that you might need.
 
85
 */
 
86
class PluginUtil {
 
87
public:
 
88
        /**
 
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
 
92
         * actors.
 
93
         */
 
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));
 
98
                return arena;
 
99
        }
 
100
 
 
101
        /**
 
102
         * Returns a robot. Can for example be used to know the sender of a message.
 
103
         */
 
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);
 
110
                return actor;
 
111
        }
 
112
 
 
113
        /**
 
114
         * Returns a robot by name
 
115
         */
 
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);
 
120
                if (proxy == NULL) {
 
121
                        LOG_WARNING("Actor proxy cannot be found");
 
122
                }
 
123
                srCore::RobotActorBase *actor = dynamic_cast<srCore::RobotActorBase*>(&proxy->GetGameActor());
 
124
                if (actor == NULL) {
 
125
                        LOG_WARNING("Proxy found, but cannot be casted properly");
 
126
                }
 
127
                return actor;
 
128
        }
 
129
 
 
130
        /**
 
131
         * Returns all the robots (derived classes from RobotActorBase).
 
132
         */
 
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);
 
138
 
 
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);
 
146
                }
 
147
        }
 
148
 
 
149
        /**
 
150
         * Return robot by identifier.
 
151
         */
 
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);
 
157
 
 
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;
 
170
                }
 
171
                return NULL;
 
172
        }
 
173
 
 
174
        /**
 
175
         * Returns the controller that belongs to the robot.
 
176
         */
 
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();
 
184
//      }
 
185
 
 
186
        /**
 
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.
 
189
         */
 
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);
 
194
//
 
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;
 
200
//                              }
 
201
//                      }
 
202
//              }
 
203
//              return NULL;
 
204
//      }
 
205
 
 
206
        /**
 
207
         * Print the position of a robot.
 
208
         */
 
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());
 
213
//      }
 
214
 
 
215
        /**
 
216
         * Print orientation of a robot
 
217
         */
 
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());
 
222
//      }
 
223
 
 
224
};
 
225
 
 
226
///@}
 
227
 
 
228
 
 
229
#endif /* PLUGINUTIL_H_ */