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 
33 namespace farsa {
34 
38 class FARSA_EXPERIMENTS_API ObjectPositionSensor : public Sensor {
39 public:
44  ObjectPositionSensor( ConfigurationParameters& params, QString prefix );
51  void save( ConfigurationParameters& params, QString prefix );
53  static void describe( QString type );
55  void update();
57  int size();
58 protected:
64  void resourceChanged(QString resourceName, ResourceChangeType changeType);
68  QString objectName;
70  bool linearize;
72  wVector bbMin;
74  wVector bbMax;
75 };
76 
77 namespace __LinearCamera_internal {
78  class LinearCameraGraphic;
79 }
80 
107 class FARSA_EXPERIMENTS_API LinearCamera : public ConcurrentResourcesUser
108 {
109 public:
123  LinearCamera(WObject* obj, wMatrix mtr, double aperture, unsigned int numReceptors, QColor backgroundColor);
124 
128  virtual ~LinearCamera();
129 
133  void update();
134 
141  const QColor& colorForReceptor(int i) const
142  {
143  return m_receptors[i];
144  }
145 
151  double getAperture() const
152  {
153  return m_aperture;
154  }
155 
161  unsigned int getNumReceptors() const
162  {
163  return m_numReceptors;
164  }
165 
172  QColor getBackgroundColor() const
173  {
174  return m_backgroundColor;
175  }
176 
183  void setBackgroundColor(QColor color)
184  {
185  m_backgroundColor = color;
186  }
187 
193  void drawCamera(bool d);
194 
195 private:
203  virtual void resourceChanged(QString resourceName, ResourceChangeType changeType);
204 
208  QVector<QColor> m_receptors;
209 
214  WObject* const m_object;
215 
223  const wMatrix m_transformation;
224 
228  const double m_aperture;
229 
233  const unsigned int m_numReceptors;
234 
240  QColor m_backgroundColor;
241 
247  const double m_apertureMin;
248 
254  const double m_apertureMax;
255 
261  const double m_receptorRange;
262 
266  Arena* m_arena;
267 
271  bool m_drawCamera;
272 
279 };
280 
285 class FARSA_EXPERIMENTS_TEMPLATE SampleFileLoadingException : public BaseException
286 {
287 public:
298  SampleFileLoadingException(const char* filename, const char* description) throw() :
299  BaseException()
300  {
301  strncpy(m_filename, filename, 256);
302  m_filename[255] = '\0';
303  strncpy(m_description, description, 256);
304  m_description[255] = '\0';
305  sprintf(m_errorMessage, "Error when loading the file with IR samples \"%s\". Description: %s", m_filename, m_description);
306  m_errorMessage[1023] = '\0';
307  }
308 
315  BaseException(other)
316  {
317  strncpy(m_filename, other.m_filename, 256);
318  m_filename[255] = '\0';
319  strncpy(m_description, other.m_description, 256);
320  m_description[255] = '\0';
321  strncpy(m_errorMessage, other.m_errorMessage, 1024);
322  m_errorMessage[1023] = '\0';
323  }
324 
331  {
332  if (&other == this) {
333  return *this;
334  }
335 
336  BaseException::operator=(other);
337  strncpy(m_filename, other.m_filename, 256);
338  m_filename[255] = '\0';
339  strncpy(m_description, other.m_description, 256);
340  m_description[255] = '\0';
341  strncpy(m_errorMessage, other.m_errorMessage, 1024);
342  m_errorMessage[1023] = '\0';
343 
344  return *this;
345  }
346 
350  virtual ~SampleFileLoadingException() throw()
351  {
352  }
353 
359  virtual const char *what() const throw()
360  {
361  return m_errorMessage;
362  }
363 
369  const char *filename() const throw()
370  {
371  return m_filename;
372  }
373 
379  const char *description() const throw()
380  {
381  return m_description;
382  }
383 
388  EXCEPTION_HELPER_FUNCTIONS(SampleFileLoadingException)
389 
390 private:
394  char m_filename[256];
395 
399  char m_description[256];
400 
404  char m_errorMessage[1024];
405 };
406 
437 class FARSA_EXPERIMENTS_API SampledIRDataLoader
438 {
439 public:
445  SampledIRDataLoader(QString filename);
446 
450  ~SampledIRDataLoader();
451 
457  const QString& filename() const
458  {
459  return m_filename;
460  }
461 
467  unsigned int numIR() const
468  {
469  return m_numIR;
470  }
471 
477  unsigned int numSamplingAngles() const
478  {
479  return m_numSamplingAngles;
480  }
481 
487  unsigned int numDistances() const
488  {
489  return m_numDistances;
490  }
491 
497  real initialDistance() const
498  {
499  return m_initialDistance;
500  }
501 
507  real distanceInterval() const
508  {
509  return m_distanceInterval;
510  }
511 
517  real finalDistance() const
518  {
519  return m_finalDistance;
520  }
521 
532  unsigned int getActivation(unsigned int i, real dist, real ang) const;
533 
546  QVector<unsigned int>::const_iterator getActivation(real dist, real ang) const;
547 
548 private:
557  unsigned int getLinearIndex(unsigned int id, unsigned int ang, unsigned int dist) const;
558 
568  void getIndexes(unsigned int i, unsigned int &id, unsigned int &ang, unsigned int &dist) const;
569 
573  const QString m_filename;
574 
578  unsigned int m_numIR;
579 
583  unsigned int m_numSamplingAngles;
584 
588  unsigned int m_numDistances;
589 
593  real m_initialDistance;
594 
598  real m_distanceInterval;
599 
603  real m_finalDistance;
604 
611  QVector<unsigned int> m_activations;
612 
619  QVector<unsigned int> m_nullActivations;
620 };
621 
622 } // end namespace farsa
623 
624 #endif