Grid.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. //
  2. // Created by wrede on 06.06.16.
  3. //
  4. #include "Grid.h"
  5. namespace util
  6. {
  7. Grid::Grid(int width_count, int height_count, double width, double height)
  8. : Grid(width_count, height_count, 1, width, height, 0.0)
  9. {
  10. /* EMPTY */
  11. }
  12. Grid::Grid(int width_count, int height_count, int depth_count,
  13. double width, double height, double depth)
  14. : width_count_(width_count),
  15. height_count_(height_count),
  16. depth_count_(depth_count),
  17. width_(width),
  18. height_(height),
  19. depth_(depth),
  20. cell_width_(width / width_count),
  21. cell_height_(height / height_count),
  22. cell_depth_(depth / depth_count)
  23. {
  24. for (int z = 0; z < depth_count; ++z)
  25. {
  26. values_.push_back(std::vector<std::vector<core::ObjectDataPtr>>());
  27. for (int y = 0; y < height_count; ++y)
  28. {
  29. values_[z].push_back(std::vector<core::ObjectDataPtr>());
  30. for (int x = 0; x < width_count; ++x)
  31. {
  32. values_[z][y].push_back(core::ObjectDataPtr());
  33. }
  34. }
  35. }
  36. }
  37. void Grid::PositionToIndex(double x, double y, double z,
  38. int& xi, int& yi, int& zi) const
  39. {
  40. xi = (int) (x / cell_width_);
  41. yi = (int) (y / cell_height_);
  42. if (depth_count_ > 1)
  43. {
  44. zi = (int) (z / cell_depth_);
  45. }
  46. else
  47. {
  48. zi = 0;
  49. }
  50. }
  51. void Grid::SetValue(core::ObjectDataPtr value, int x, int y, int z)
  52. {
  53. values_[z][y][x] = value;
  54. }
  55. void Grid::SetValue(core::ObjectDataPtr value, double x, double y, double z)
  56. {
  57. int xi, yi, zi;
  58. PositionToIndex(x, y, z, xi, yi, zi);
  59. SetValue(value, xi, yi, zi);
  60. }
  61. core::ObjectDataPtr Grid::GetValue(int x, int y, int z) const
  62. {
  63. return values_[z][y][x];
  64. }
  65. core::ObjectDataPtr Grid::GetValue(double x, double y, double z) const
  66. {
  67. int xi, yi, zi;
  68. PositionToIndex(x, y, z, xi, yi, zi);
  69. return GetValue(xi, yi, zi);
  70. }
  71. int Grid::GetWidthCount() const
  72. {
  73. return width_count_;
  74. }
  75. int Grid::GetHeightCount() const
  76. {
  77. return height_count_;
  78. }
  79. int Grid::GetDepthCount() const
  80. {
  81. return depth_count_;
  82. }
  83. double Grid::GetWidth() const
  84. {
  85. return width_;
  86. }
  87. double Grid::GetHeight() const
  88. {
  89. return height_;
  90. }
  91. double Grid::GetDepth() const
  92. {
  93. return depth_;
  94. }
  95. void Grid::Convolve2D(int vicinity, double* mask, double multiplier)
  96. {
  97. // [x,y,z] position in grid
  98. // [vx,vy,vz] position in vicinity
  99. // [nx,ny,nz] position in grid
  100. // [mx,my,mz] position in mask
  101. // [mi] index in mask
  102. int mask_size = vicinity * 2 + 1;
  103. for (int z = 0; z < depth_count_; ++z)
  104. {
  105. for (int y = 0; y < height_count_; ++y)
  106. {
  107. for (int x = 0; x < width_count_; ++x)
  108. {
  109. double score = 0.0;
  110. for (int vy = -vicinity; vy <= vicinity; ++vy)
  111. {
  112. int ny = y + vy;
  113. if (ny < 0 || ny >= height_count_) continue;
  114. int my = vy + vicinity;
  115. for (int vx = -vicinity; vx <= vicinity; ++vx)
  116. {
  117. int nx = x + vx;
  118. if (nx < 0 || nx >= width_count_) continue;
  119. int mx = vx + vicinity;
  120. int mi = my * mask_size + mx;
  121. score += GetValue(nx, ny, z)->GetDetectionScore() * mask[mi];
  122. }
  123. }
  124. GetValue(x, y, z)->SetDetectionScore(score * multiplier);
  125. }
  126. }
  127. }
  128. }
  129. void Grid::Convolve3D(int vicinity, double* mask, double multiplier)
  130. {
  131. // [x,y,z] position in grid
  132. // [vx,vy,vz] position in vicinity
  133. // [nx,ny,nz] position in grid
  134. // [mx,my,mz] position in mask
  135. // [mi] index in mask
  136. int mask_size = vicinity * 2 + 1;
  137. for (int z = 0; z < depth_count_; ++z)
  138. {
  139. for (int y = 0; y < height_count_; ++y)
  140. {
  141. for (int x = 0; x < width_count_; ++x)
  142. {
  143. double score = 0.0;
  144. for (int vz = -vicinity; vz <= vicinity; ++vz)
  145. {
  146. int nz = z + vz;
  147. if (nz < 0 || nz >= depth_count_) continue;
  148. int mz = vz + vicinity;
  149. for (int vy = -vicinity; vy <= vicinity; ++vy)
  150. {
  151. int ny = y + vy;
  152. if (ny < 0 || ny >= height_count_) continue;
  153. int my = vy + vicinity;
  154. for (int vx = -vicinity; vx <= vicinity; ++vx)
  155. {
  156. int nx = x + vx;
  157. if (nx < 0 || nx >= width_count_) continue;
  158. int mx = vx + vicinity;
  159. int mi = mz * mask_size * mask_size + my * mask_size + mx;
  160. score += GetValue(nx, ny, nz)->GetDetectionScore() * mask[mi];
  161. }
  162. }
  163. }
  164. GetValue(x, y, z)->SetDetectionScore(score * multiplier);
  165. }
  166. }
  167. }
  168. }
  169. }