sensors.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 SENSORS_H
24 #define SENSORS_H
25 
26 #include "experimentsconfig.h"
27 #include "neuroninterfaces.h"
28 #include "world.h"
29 #include "robots.h"
30 #include "wheeledexperimenthelper.h"
31 #include "baseexception.h"
32 #include "helperresources.h"
33 #include "intervals.h"
34 #include <QVector>
35 
36 namespace farsa {
37 
63 class FARSA_EXPERIMENTS_API FakeSensor : public Sensor
64 {
65 public:
72  FakeSensor(ConfigurationParameters& params, QString prefix);
73 
77  ~FakeSensor();
78 
86  virtual void save(ConfigurationParameters& params, QString prefix);
87 
93  static void describe(QString type);
94 
98  virtual void update();
99 
105  virtual int size();
106 
118  virtual void shareResourcesWith(ResourcesUser* other);
119 
120 protected:
128  void resourceChanged(QString resourceName, ResourceChangeType changeType);
129 
134 
140 
146 
151 };
152 
156 class FARSA_EXPERIMENTS_API ObjectPositionSensor : public Sensor {
157 public:
162  ObjectPositionSensor( ConfigurationParameters& params, QString prefix );
169  void save( ConfigurationParameters& params, QString prefix );
171  static void describe( QString type );
173  void update();
175  int size();
176 protected:
182  void resourceChanged(QString resourceName, ResourceChangeType changeType);
186  QString objectName;
188  bool linearize;
190  wVector bbMin;
192  wVector bbMax;
193 };
194 
195 namespace LinearCameraOld {
196  namespace __LinearCamera_internal {
197  class LinearCameraGraphic;
198  }
199 
229  class FARSA_EXPERIMENTS_API LinearCamera : public ConcurrentResourcesUser
230  {
231  public:
247  LinearCamera(WObject* obj, wMatrix mtr, double aperture, unsigned int numReceptors, double maxDistance, QColor backgroundColor);
248 
252  virtual ~LinearCamera();
253 
257  void update();
258 
265  const QColor& colorForReceptor(int i) const
266  {
267  return m_receptors[i];
268  }
269 
275  double getAperture() const
276  {
277  return m_aperture;
278  }
279 
285  unsigned int getNumReceptors() const
286  {
287  return m_numReceptors;
288  }
289 
296  QColor getBackgroundColor() const
297  {
298  return m_backgroundColor;
299  }
300 
307  void setBackgroundColor(QColor color)
308  {
309  m_backgroundColor = color;
310  }
311 
317  void drawCamera(bool d);
318 
325  void ignoreWalls(bool ignore)
326  {
327  m_ignoreWalls = ignore;
328  }
329 
336  bool wallsIgnored() const
337  {
338  return m_ignoreWalls;
339  }
340 
341  private:
349  virtual void resourceChanged(QString resourceName, ResourceChangeType changeType);
350 
354  QVector<QColor> m_receptors;
355 
360  WObject* const m_object;
361 
369  const wMatrix m_transformation;
370 
374  const double m_aperture;
375 
379  const unsigned int m_numReceptors;
380 
384  const double m_maxDistance;
385 
391  QColor m_backgroundColor;
392 
398  const double m_apertureMin;
399 
405  const double m_apertureMax;
406 
412  const double m_receptorRange;
413 
417  Arena* m_arena;
418 
422  bool m_drawCamera;
423 
429  bool m_ignoreWalls;
430 
437  };
438 
439 }
440 
441 namespace LinearCameraNew {
442  namespace __LinearCamera_internal {
443  class LinearCameraGraphic;
444  }
445 
481  class FARSA_EXPERIMENTS_API LinearCamera : public ConcurrentResourcesUser
482  {
483  public:
501  LinearCamera(WObject* obj, wMatrix mtr, double aperture, unsigned int numReceptors, double maxDistance, QColor backgroundColor);
502 
520  LinearCamera(WObject* obj, wMatrix mtr, QVector<SimpleInterval> receptorsRanges, double maxDistance, QColor backgroundColor);
521 
525  virtual ~LinearCamera();
526 
530  void update();
531 
538  const QColor& colorForReceptor(int i) const
539  {
540  return m_receptors[i];
541  }
542 
548  unsigned int getNumReceptors() const
549  {
550  return m_receptors.size();
551  }
552 
559  QColor getBackgroundColor() const
560  {
561  return m_backgroundColor;
562  }
563 
570  void setBackgroundColor(QColor color)
571  {
572  m_backgroundColor = color;
573  }
574 
580  void drawCamera(bool d);
581 
588  void ignoreWalls(bool ignore)
589  {
590  m_ignoreWalls = ignore;
591  }
592 
599  bool wallsIgnored() const
600  {
601  return m_ignoreWalls;
602  }
603 
604  private:
612  virtual void resourceChanged(QString resourceName, ResourceChangeType changeType);
613 
617  QVector<QColor> m_receptors;
618 
623  WObject* const m_object;
624 
632  const wMatrix m_transformation;
633 
637  const QVector<SimpleInterval> m_receptorsRanges;
638 
642  const double m_maxDistance;
643 
649  QColor m_backgroundColor;
650 
654  Arena* m_arena;
655 
659  bool m_drawCamera;
660 
666  bool m_ignoreWalls;
667 
674  };
675 }
676 
680 typedef LinearCameraOld::LinearCamera LinearCamera;
681 // typedef LinearCameraNew::LinearCamera LinearCamera;
682 
687 class FARSA_EXPERIMENTS_TEMPLATE SampleFileLoadingException : public BaseException
688 {
689 public:
700  SampleFileLoadingException(const char* filename, const char* description) throw() :
701  BaseException()
702  {
703  strncpy(m_filename, filename, 256);
704  m_filename[255] = '\0';
705  strncpy(m_description, description, 256);
706  m_description[255] = '\0';
707  sprintf(m_errorMessage, "Error when loading the file with IR samples \"%s\". Description: %s", m_filename, m_description);
708  m_errorMessage[1023] = '\0';
709  }
710 
717  BaseException(other)
718  {
719  strncpy(m_filename, other.m_filename, 256);
720  m_filename[255] = '\0';
721  strncpy(m_description, other.m_description, 256);
722  m_description[255] = '\0';
723  strncpy(m_errorMessage, other.m_errorMessage, 1024);
724  m_errorMessage[1023] = '\0';
725  }
726 
733  {
734  if (&other == this) {
735  return *this;
736  }
737 
738  BaseException::operator=(other);
739  strncpy(m_filename, other.m_filename, 256);
740  m_filename[255] = '\0';
741  strncpy(m_description, other.m_description, 256);
742  m_description[255] = '\0';
743  strncpy(m_errorMessage, other.m_errorMessage, 1024);
744  m_errorMessage[1023] = '\0';
745 
746  return *this;
747  }
748 
752  virtual ~SampleFileLoadingException() throw()
753  {
754  }
755 
761  virtual const char *what() const throw()
762  {
763  return m_errorMessage;
764  }
765 
771  const char *filename() const throw()
772  {
773  return m_filename;
774  }
775 
781  const char *description() const throw()
782  {
783  return m_description;
784  }
785 
790  EXCEPTION_HELPER_FUNCTIONS(SampleFileLoadingException)
791 
792 private:
796  char m_filename[256];
797 
801  char m_description[256];
802 
806  char m_errorMessage[1024];
807 };
808 
843 class FARSA_EXPERIMENTS_API SampledIRDataLoader
844 {
845 public:
851  SampledIRDataLoader(QString filename);
852 
856  ~SampledIRDataLoader();
857 
863  const QString& filename() const
864  {
865  return m_filename;
866  }
867 
873  unsigned int numIR() const
874  {
875  return m_numIR;
876  }
877 
883  unsigned int numSamplingAngles() const
884  {
885  return m_numSamplingAngles;
886  }
887 
893  unsigned int numDistances() const
894  {
895  return m_numDistances;
896  }
897 
903  real initialDistance() const
904  {
905  return m_initialDistance;
906  }
907 
913  real distanceInterval() const
914  {
915  return m_distanceInterval;
916  }
917 
923  real finalDistance() const
924  {
925  return m_finalDistance;
926  }
927 
938  unsigned int getActivation(unsigned int i, real dist, real ang) const;
939 
952  QVector<unsigned int>::const_iterator getActivation(real dist, real ang) const;
953 
954 private:
963  unsigned int getLinearIndex(unsigned int id, unsigned int ang, unsigned int dist) const;
964 
968  const QString m_filename;
969 
973  unsigned int m_numIR;
974 
978  unsigned int m_numSamplingAngles;
979 
983  unsigned int m_numDistances;
984 
988  real m_initialDistance;
989 
993  real m_distanceInterval;
994 
998  real m_finalDistance;
999 
1006  QVector<unsigned int> m_activations;
1007 
1014  QVector<unsigned int> m_nullActivations;
1015 };
1016 
1028 QColor getColorAtArenaGroundPosition(Arena* arena, wVector pos);
1029 
1030 } // end namespace farsa
1031 
1032 #endif