GazeSpeed.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // GazeSpeed.cpp
  2. #include "GazeSpeed.h"
  3. #include "../arffHelper/ArffUtil.h"
  4. #include <cmath>
  5. #include <iostream>
  6. #define USECS_TO_SECS 1000000.0
  7. #define PI 3.14159265
  8. const char *c_widthName = "width_mm";
  9. const char *c_heightName = "height_mm";
  10. const char *c_distanceName = "distance_mm";
  11. using namespace std;
  12. GazeSpeed::GazeSpeed(Arff *pArff, double integrationPeriod)
  13. {
  14. m_pArff = pArff;
  15. double samplingPeriod = ArffUtil::GetSamplingPeriod(m_pArff);
  16. m_step = ceil(integrationPeriod / samplingPeriod);
  17. m_step = m_step<1 ? 1 : m_step;
  18. FillVectors();
  19. }
  20. vector<double> GazeSpeed::GetSpeed()
  21. {
  22. vector<double> speed;
  23. speed.resize(m_vXdeg.size());
  24. for (int ind=m_step; ind<(int)speed.size(); ind++)
  25. {
  26. double xDisp = pow(m_vXdeg[ind] - m_vXdeg[ind-m_step], 2);
  27. double yDisp = pow(m_vYdeg[ind] - m_vYdeg[ind-m_step], 2);
  28. double disp = sqrt(xDisp + yDisp);
  29. double time = ((*m_pArff)[ind][m_timeInd] - (*m_pArff)[ind-m_step][m_timeInd]) / USECS_TO_SECS;
  30. speed[ind - m_step/2] = disp / time;
  31. }
  32. return speed;
  33. }
  34. void GazeSpeed::FillVectors()
  35. {
  36. double width_px = m_pArff->WidthPx();
  37. double height_px = m_pArff->HeightPx();
  38. string value;
  39. if(!m_pArff->GetMetadata(c_widthName, value))
  40. {
  41. cout << "ERROR: METADATA " << c_widthName << " is missing" << endl;
  42. exit(-1);
  43. }
  44. double width_mm = stod(value);
  45. if(!m_pArff->GetMetadata(c_heightName, value))
  46. {
  47. cout << "ERROR: METADATA " << c_heightName << " is missing" << endl;
  48. exit(-1);
  49. }
  50. double height_mm = stod(value);
  51. if(!m_pArff->GetMetadata(c_distanceName, value))
  52. {
  53. cout << "ERROR: METADATA " << c_distanceName << " is missing" << endl;
  54. exit(-1);
  55. }
  56. double distance_mm = stod(value);
  57. double thetaWidth = 2 * atan(width_mm/(2*distance_mm)) * 180/PI;
  58. double ppdx = width_px/thetaWidth;
  59. double thetaHeight = 2 * atan(height_mm/(2*distance_mm)) * 180/PI;
  60. double ppdy = height_px/thetaHeight;
  61. int xInd, yInd, confInd;
  62. bool res = ArffUtil::GetTXYCindex(m_pArff, m_timeInd, xInd, yInd, confInd);
  63. if (!res)
  64. {
  65. cout << "ERROR: could not find time or x or y or confidence in the provided ARFF file." << endl;
  66. exit(-1);
  67. }
  68. int rows, columns;
  69. m_pArff->Size(rows, columns);
  70. m_vXdeg.resize(rows);
  71. m_vYdeg.resize(rows);
  72. for (int ind=0; ind<rows; ind++)
  73. {
  74. m_vXdeg[ind] = (*m_pArff)[ind][xInd]/ppdx;
  75. m_vYdeg[ind] = (*m_pArff)[ind][yInd]/ppdy;
  76. }
  77. }