arena.h
1 /********************************************************************************
2  * FARSA Experimentes Library *
3  * Copyright (C) 2007-2012 *
4  * Gianluca Massera <emmegian@yahoo.it> *
5  * Stefano Nolfi <stefano.nolfi@istc.cnr.it> *
6  * Tomassino Ferrauto <tomassino.ferrauto@istc.cnr.it> *
7  * *
8  * This program is free software; you can redistribute it and/or modify *
9  * it under the terms of the GNU General Public License as published by *
10  * the Free Software Foundation; either version 2 of the License, or *
11  * (at your option) any later version. *
12  * *
13  * This program is distributed in the hope that it will be useful, *
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16  * GNU General Public License for more details. *
17  * *
18  * You should have received a copy of the GNU General Public License *
19  * along with this program; if not, write to the Free Software *
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA *
21  ********************************************************************************/
22 
23 #ifndef ARENA_H
24 #define ARENA_H
25 
26 #include "experimentsconfig.h"
27 #include "parametersettable.h"
28 #include "resourcesuser.h"
29 #include "world.h"
30 #include "wheeledexperimenthelper.h"
31 #include "baseexception.h"
32 #include <QVector>
33 #include <QMap>
34 
35 namespace farsa {
36 
47 class FARSA_EXPERIMENTS_API Arena : public ParameterSettableInConstructor, public ConcurrentResourcesUser
48 {
49 public:
60  Arena(ConfigurationParameters& params, QString prefix);
61 
65  virtual ~Arena();
66 
77  virtual void save(ConfigurationParameters& params, QString prefix);
78 
91  static void describe(QString type);
92 
98  const QVector<PhyObject2DWrapper*>& getObjects() const
99  {
100  return m_objects2DList;
101  }
102 
108  real getZ() const
109  {
110  return m_z;
111  }
112 
118  real getWidth() const
119  {
120  return m_plane->phyObject()->sideX();
121  }
122 
128  real getHeight() const
129  {
130  return m_plane->phyObject()->sideY();
131  }
132 
141  void addRobots(QStringList robots);
142 
151  const WheeledRobot2DWrapper* getRobotWrapper(QString robotName) const;
152 
158  Box2DWrapper* getPlane();
159 
165  const Box2DWrapper* getPlane() const;
166 
184  Box2DWrapper* createWall(QColor color, wVector start, wVector end, real thickness, real height = -1.0);
185 
196  Cylinder2DWrapper* createSmallCylinder(QColor color, real height = -1.0);
197 
208  Cylinder2DWrapper* createBigCylinder(QColor color, real height = -1.0);
209 
219  Cylinder2DWrapper* createCircularTargetArea(real radius, QColor color);
220 
231  Box2DWrapper* createRectangularTargetArea(real width, real depth, QColor color);
232 
246  bool delete2DObject(PhyObject2DWrapper* obj);
247 
256  void prepareToHandleKinematicRobotCollisions();
257 
266  void handleKinematicRobotCollisions();
267 
278  QVector<PhyObject2DWrapper*> getKinematicRobotCollisions(WheeledRobot2DWrapper* robot) const;
279 
291  QVector<PhyObject2DWrapper*> getKinematicRobotCollisions(QString robotResourceName) const;
292 
293 private:
304  Cylinder2DWrapper* createCylinder(QColor color, real radius, real height, Cylinder2DWrapper::Type type);
305 
317  Box2DWrapper* createBox(QColor color, real width, real depth, real height, Box2DWrapper::Type type);
318 
326  virtual void resourceChanged(QString resourceName, ResourceChangeType changeType);
327 
331  const real m_z;
332 
339  const real m_smallCylinderRadius;
340 
347  const real m_bigCylinderRadius;
348 
352  QVector<PhyObject2DWrapper*> m_objects2DList;
353 
357  Box2DWrapper* const m_plane;
358 
364  QMap<QString, WheeledRobot2DWrapper*> m_robotResourceWrappers;
365 
372  QMap<WheeledRobot2DWrapper*, QVector<PhyObject2DWrapper*> > m_kinematicRobotCollisions;
373 
377  World* m_world;
378 
388  static Box2DWrapper* createPlane(ConfigurationParameters& params, QString prefix, real z, Arena* arena);
389 
399  static Box2DWrapper* createPlane2(ConfigurationParameters& params, QString prefix, real z, Arena* arena);
400 };
401 
405 class FARSA_EXPERIMENTS_TEMPLATE ArenaException : public BaseException
406 {
407 public:
415  ArenaException(const char* reason) throw() :
416  BaseException()
417  {
418  strncpy(m_reason, reason, 256);
419  m_reason[255] = '\0';
420  sprintf(m_errorMessage, "Runtime error in the Arena, reason: %s", m_reason);
421  m_errorMessage[511] = '\0';
422  }
423 
429  ArenaException(const ArenaException& other) throw() :
430  BaseException(other)
431  {
432  strncpy(m_reason, other.m_reason, 256);
433  m_reason[255] = '\0';
434  strncpy(m_errorMessage, other.m_errorMessage, 512);
435  m_errorMessage[511] = '\0';
436  }
437 
443  ArenaException& operator=(const ArenaException& other) throw()
444  {
445  if (&other == this) {
446  return *this;
447  }
448 
449  BaseException::operator=(other);
450  strncpy(m_reason, other.m_reason, 256);
451  m_reason[255] = '\0';
452  strncpy(m_errorMessage, other.m_errorMessage, 512);
453  m_errorMessage[511] = '\0';
454 
455  return *this;
456  }
457 
461  virtual ~ArenaException() throw()
462  {
463  }
464 
470  virtual const char *what() const throw()
471  {
472  return m_errorMessage;
473  }
474 
480  const char *reason() const throw()
481  {
482  return m_reason;
483  }
484 
489  EXCEPTION_HELPER_FUNCTIONS(ArenaException)
490 
491 private:
495  char m_reason[256];
496 
500  char m_errorMessage[512];
501 };
502 
503 } // end namespace farsa
504 
505 #endif