EquirectangularToFovSpeed.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. // EquirectangularToFovSpeed.cpp
  2. #include "EquirectangularToFovSpeed.h"
  3. #include "Util.h"
  4. #include "../arffHelper/ArffUtil.h"
  5. #include <cassert>
  6. #include <cmath>
  7. #define USECS_TO_SECS 1000000.0
  8. #define PI 3.14159265
  9. #define DELTA 0.00001
  10. using namespace std;
  11. EquirectangularToFovSpeed::EquirectangularToFovSpeed(Arff *pArff, double integrationPeriod) :
  12. EquirectangularToFovBase(pArff)
  13. {
  14. double samplingPeriod = ArffUtil::GetSamplingPeriod(m_pArff);
  15. m_step = ceil(integrationPeriod / samplingPeriod);
  16. m_step = m_step<1 ? 1 : m_step;
  17. FillVectors();
  18. }
  19. /*virtual*/ EquirectangularToFovSpeed::~EquirectangularToFovSpeed()
  20. {
  21. }
  22. void EquirectangularToFovSpeed::AddSpeed()
  23. {
  24. int rows, columns;
  25. m_pArff->Size(rows, columns);
  26. // add x,y within FOV
  27. m_pArff->AddColumn("x_fov", "numeric");
  28. for (int r=0; r<rows; r++)
  29. (*m_pArff)[r][columns] = m_vXfov[r];
  30. columns++;
  31. m_pArff->AddColumn("y_fov", "numeric");
  32. for (int r=0; r<rows; r++)
  33. (*m_pArff)[r][columns] = m_vYfov[r];
  34. columns++;
  35. // add speed attributes
  36. string eyeFovAttName = "eye_fov_speed_" + to_string(m_step);
  37. m_pArff->AddColumn(eyeFovAttName, "numeric");
  38. GetSpeed(m_vEyeFov, columns);
  39. columns++;
  40. string headAttName = "head_speed_" + to_string(m_step);
  41. m_pArff->AddColumn(headAttName, "numeric");
  42. GetSpeed(m_vHead, columns);
  43. columns++;
  44. string headEyeAttName = "head_plus_eye_speed_" + to_string(m_step);
  45. m_pArff->AddColumn(headEyeAttName, "numeric");
  46. GetSpeed(m_vHeadEye, columns);
  47. }
  48. vector<double> EquirectangularToFovSpeed::GetHeadSpeed()
  49. {
  50. return GetSpeed(m_vHead);
  51. }
  52. vector<double> EquirectangularToFovSpeed::GetEyeFovSpeed()
  53. {
  54. return GetSpeed(m_vEyeFov);
  55. }
  56. vector<double> EquirectangularToFovSpeed::GetHeadPlusEyeSpeed()
  57. {
  58. return GetSpeed(m_vHeadEye);
  59. }
  60. // PRIVATE:
  61. void EquirectangularToFovSpeed::GetSpeed(const vector<Vec3> &inVec, const int attInd)
  62. {
  63. vector<double> speed = GetSpeed(inVec);
  64. for (unsigned int ind=0; ind<speed.size(); ind++)
  65. (*m_pArff)[ind][attInd] = speed[ind];
  66. }
  67. vector<double> EquirectangularToFovSpeed::GetSpeed(const vector<Vec3> &inVec)
  68. {
  69. int rows, columns;
  70. m_pArff->Size(rows, columns);
  71. assert(inVec.size() == (unsigned int) rows);
  72. vector<double> res;
  73. res.resize(rows);
  74. for (int ind=m_step; ind<rows; ind++)
  75. {
  76. double dotProd = DotProd(inVec[ind], inVec[ind-m_step]);
  77. // even though vectors are nomalized rounding errors can push the dot product slightly above 1
  78. dotProd = dotProd > 1.0 && dotProd < 1.0 + DELTA? 1.0: dotProd;
  79. double rads = acos(dotProd);
  80. double degs = rads * 180 / PI;
  81. double time = (double)((*m_pArff)[ind][m_timeInd] - (*m_pArff)[ind-m_step][m_timeInd]) / USECS_TO_SECS;
  82. double degsSec = degs/time;
  83. assert(degsSec==degsSec); // check for NaN values
  84. //res[ind] = degsSec;
  85. res[ind - m_step/2] = degsSec;
  86. }
  87. return res;
  88. }
  89. void EquirectangularToFovSpeed::FillVectors()
  90. {
  91. m_vHead.clear();
  92. m_vHeadEye.clear();
  93. m_vEyeFov.clear();
  94. int rows, columns;
  95. m_pArff->Size(rows, columns);
  96. for (unsigned int ind=0; ind < (unsigned int) rows; ind++)
  97. {
  98. double xHead, yHead, tiltHead;
  99. GetHeadPos(ind, &xHead, &yHead, &tiltHead);
  100. double horHeadRads, verHeadRads;
  101. EquirectangularToSpherical(xHead, yHead, m_pArff->WidthPx(), m_pArff->HeightPx(), &horHeadRads, &verHeadRads);
  102. Vec3 headVec(0,0,0);
  103. SphericalToCartesian(horHeadRads, verHeadRads, &headVec);
  104. m_vHead.push_back(headVec);
  105. Vec3 vidVec(-1,0,0); // middle of the video is the center
  106. double tiltHeadRads = tiltHead * PI / 180;
  107. Matrix33 rot = HeadToVideoRotation(headVec, tiltHeadRads, vidVec);
  108. rot = Transpose(rot);
  109. // Convert gaze to FOV coordinates
  110. double xGaze, yGaze;
  111. GetEyePos(ind, &xGaze, &yGaze);
  112. double horGazeRads, verGazeRads;
  113. EquirectangularToSpherical(xGaze, yGaze, m_pArff->WidthPx(), m_pArff->HeightPx(), &horGazeRads, &verGazeRads);
  114. Vec3 GazeVec(0,0,0);
  115. SphericalToCartesian(horGazeRads, verGazeRads, &GazeVec);
  116. m_vHeadEye.push_back(GazeVec);
  117. Vec3 GazeWithinHeadVec = RotatePoint(rot, GazeVec);
  118. m_vEyeFov.push_back(GazeWithinHeadVec);
  119. double rotHorRads, rotVerRads;
  120. CartesianToSpherical(GazeWithinHeadVec, &rotHorRads, &rotVerRads);
  121. double xFov, yFov;
  122. SphericalToFov(rotHorRads, rotVerRads, &xFov, &yFov);
  123. m_vXfov.push_back(xFov);
  124. m_vYfov.push_back(yFov);
  125. }
  126. }
  127. // PRIVATE:
  128. void EquirectangularToFovSpeed::SphericalToFov(double horRads, double verRads, double *x, double *y)
  129. {
  130. // Change them to [0, 2*pi) and [0, pi) range
  131. horRads = horRads < 0? 2*PI + horRads: horRads;
  132. verRads = verRads < 0? PI + verRads: verRads;
  133. // convert them from the middle of the video to the start of the coordinates
  134. horRads -= PI;
  135. verRads -= PI / 2.0;
  136. double fovHorRads = m_fovWidthDeg * PI / 180.0;
  137. double fovVerRads = m_fovHeightDeg * PI / 180.0;
  138. *x = m_fovWidthPx * (horRads + fovHorRads / 2.0) / fovHorRads;
  139. *y = m_fovHeightPx * (verRads + fovVerRads / 2.0) / fovVerRads;
  140. }