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 "farsamiscutilities.h"
33 #include <QVector>
34 #include <QMap>
35 #include <QString>
36 #include <QSet>
37 
38 namespace farsa {
39 
50 class FARSA_EXPERIMENTS_API Arena : public ParameterSettableInConstructor, public ConcurrentResourcesUser
51 {
52 private:
56  enum KinematicCollisionHandlers {
57  SimpleCollisions,
60  CircleCollisions,
64  UnknownCollisionHandler
65  };
66 
76  static KinematicCollisionHandlers stringToCollisionHandler(QString str);
77 
85  static QString collisionHandlerToString(KinematicCollisionHandlers h);
86 
87 public:
98  Arena(ConfigurationParameters& params, QString prefix);
99 
103  virtual ~Arena();
104 
115  virtual void save(ConfigurationParameters& params, QString prefix);
116 
129  static void describe(QString type);
130 
136  const QVector<PhyObject2DWrapper*>& getObjects() const
137  {
138  return m_objects2DList;
139  }
140 
146  real getZ() const
147  {
148  return m_z;
149  }
150 
156  real getWidth() const
157  {
158  return m_plane->phyObject()->sideX();
159  }
160 
166  real getHeight() const
167  {
168  return m_plane->phyObject()->sideY();
169  }
170 
179  void addRobots(QStringList robots);
180 
189  const WheeledRobot2DWrapper* getRobotWrapper(QString robotName) const;
190 
196  Box2DWrapper* getPlane();
197 
203  const Box2DWrapper* getPlane() const;
204 
222  Box2DWrapper* createWall(QColor color, wVector start, wVector end, real thickness, real height = -1.0);
223 
234  Cylinder2DWrapper* createSmallCylinder(QColor color, real height = -1.0);
235 
246  Cylinder2DWrapper* createBigCylinder(QColor color, real height = -1.0);
247 
261  Cylinder2DWrapper* createCylinder(real radius, QColor color, real height = -1.0);
262 
272  Cylinder2DWrapper* createCircularTargetArea(real radius, QColor color);
273 
284  Box2DWrapper* createRectangularTargetArea(real width, real depth, QColor color);
285 
295  Sphere2DWrapper* createLightBulb(QColor color);
296 
310  bool delete2DObject(PhyObject2DWrapper* obj);
311 
320  void prepareToHandleKinematicRobotCollisions();
321 
330  void handleKinematicRobotCollisions();
331 
344  FARSA_DEPRECATED QVector<PhyObject2DWrapper*> getKinematicRobotCollisions(WheeledRobot2DWrapper* robot) const;
345 
359  FARSA_DEPRECATED QVector<PhyObject2DWrapper*> getKinematicRobotCollisions(QString robotResourceName) const;
360 
371  QSet<PhyObject2DWrapper*> getKinematicRobotCollisionsSet(WheeledRobot2DWrapper* robot) const;
372 
384  QSet<PhyObject2DWrapper*> getKinematicRobotCollisionsSet(QString robotResourceName) const;
385 
386 private:
397  Cylinder2DWrapper* createCylinder(QColor color, real radius, real height, Cylinder2DWrapper::Type type);
398 
410  Box2DWrapper* createBox(QColor color, real width, real depth, real height, Box2DWrapper::Type type);
411 
420  Sphere2DWrapper* createSphere(QColor color, real radius, Sphere2DWrapper::Type type);
421 
429  virtual void resourceChanged(QString resourceName, ResourceChangeType changeType);
430 
435  void simpleCollisionsPreAdvance();
436 
441  void simpleCollisionsHandle();
442 
447  void circleCollisionsPreAdvance();
448 
453  void circleCollisionsHandle();
454 
458  const real m_z;
459 
466  const real m_smallCylinderRadius;
467 
474  const real m_bigCylinderRadius;
475 
481  const real m_lightBulbRadius;
482 
486  const KinematicCollisionHandlers m_collisionHandler;
487 
491  QVector<PhyObject2DWrapper*> m_objects2DList;
492 
496  Box2DWrapper* const m_plane;
497 
503  QMap<QString, WheeledRobot2DWrapper*> m_robotResourceWrappers;
504 
511  QMap<WheeledRobot2DWrapper*, QSet<PhyObject2DWrapper*> > m_kinematicRobotCollisions;
512 
516  World* m_world;
517 
527  static Box2DWrapper* createPlane(ConfigurationParameters& params, QString prefix, real z, Arena* arena);
528 
538  static Box2DWrapper* createPlane2(ConfigurationParameters& params, QString prefix, real z, Arena* arena);
539 };
540 
541 // All the suff below is to avoid warnings on Windows about the use of the
542 // unsafe function sprintf and strcpy...
543 #if defined(_MSC_VER)
544  #pragma warning(push)
545  #pragma warning(disable:4996)
546 #endif
547 
551 class FARSA_EXPERIMENTS_TEMPLATE ArenaException : public BaseException
552 {
553 public:
561  ArenaException(const char* reason) throw() :
562  BaseException()
563  {
564  strncpy(m_reason, reason, 256);
565  m_reason[255] = '\0';
566  sprintf(m_errorMessage, "Runtime error in the Arena, reason: %s", m_reason);
567  m_errorMessage[511] = '\0';
568  }
569 
575  ArenaException(const ArenaException& other) throw() :
576  BaseException(other)
577  {
578  strncpy(m_reason, other.m_reason, 256);
579  m_reason[255] = '\0';
580  strncpy(m_errorMessage, other.m_errorMessage, 512);
581  m_errorMessage[511] = '\0';
582  }
583 
589  ArenaException& operator=(const ArenaException& other) throw()
590  {
591  if (&other == this) {
592  return *this;
593  }
594 
595  BaseException::operator=(other);
596  strncpy(m_reason, other.m_reason, 256);
597  m_reason[255] = '\0';
598  strncpy(m_errorMessage, other.m_errorMessage, 512);
599  m_errorMessage[511] = '\0';
600 
601  return *this;
602  }
603 
607  virtual ~ArenaException() throw()
608  {
609  }
610 
616  virtual const char *what() const throw()
617  {
618  return m_errorMessage;
619  }
620 
626  const char *reason() const throw()
627  {
628  return m_reason;
629  }
630 
635  EXCEPTION_HELPER_FUNCTIONS(ArenaException)
636 
637 private:
641  char m_reason[256];
642 
646  char m_errorMessage[512];
647 };
648 
649 // All the suff below is to restore the warning state on Windows
650 #if defined(_MSC_VER)
651  #pragma warning(pop)
652 #endif
653 
654 } // end namespace farsa
655 
656 #endif
real getWidth() const
Returns the width of the main plane.
Definition: arena.h:156
ArenaException & operator=(const ArenaException &other)
Copy operator.
Definition: arena.h:589
The subclass of PhyObject2DWrapper wrapping a box.
The exception thrown at runtime by the arena.
Definition: arena.h:551
The subclass of PhyObject2DWrapper wrapping a wheeled robot.
ArenaException(const ArenaException &other)
Copy constructor.
Definition: arena.h:575
real getHeight() const
Returns the height of the main plane.
Definition: arena.h:166
The class modelling an arena.
Definition: arena.h:50
This file contains the common type defitions used on the whole framework.
real getZ() const
Returns the z of the plane.
Definition: arena.h:146
const char * reason() const
Returns the description of why the exception was thrown.
Definition: arena.h:626
const QVector< PhyObject2DWrapper * > & getObjects() const
Returns the list of 2D objects.
Definition: arena.h:136
The subclass of PhyObject2DWrapper wrapping a cylinder.
A class wrapping a PhyObject to add methods suitable for wheeled robots simulations.
float real
Type
The possible type of wrapped objects.
The subclass of PhyObject2DWrapper wrapping a sphere.
virtual const char * what() const
Returns a C string describing the exception.
Definition: arena.h:616
virtual ~ArenaException()
Destructor.
Definition: arena.h:607
ArenaException(const char *reason)
Constructor.
Definition: arena.h:561